blob: f4bfb52b6b62f3fb2b4cfb308687f54a4e971a34 [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
Shuzhen Wang2b407e52023-08-21 15:07:24 -07002339 // Make sure status tracker is flushed
2340 mStatusTracker->flushPendingStates();
2341
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002342 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002343 if (checkAbandonedStreamsLocked()) {
2344 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2345 __FUNCTION__);
2346 return true;
2347 }
2348
Emilian Peev3bead5f2020-05-28 17:29:08 -07002349 status_t rc = NO_ERROR;
2350 bool markClientActive = false;
2351 if (mStatus == STATUS_ACTIVE) {
2352 markClientActive = true;
2353 mPauseStateNotify = true;
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00002354 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
Emilian Peev3bead5f2020-05-28 17:29:08 -07002355
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00002356 // This is essentially the same as calling rc = internalPauseAndWaitLocked(..), except that
2357 // we don't want to call setPaused(true) to avoid it interfering with setPaused() called
2358 // from createInputStream/createStream.
2359 rc = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
2360 /*requestThreadInvocation*/ true);
2361 if (rc != OK) {
2362 mStatusTracker->dumpActiveComponents();
2363 SET_ERR_L("Can't idle device in %f seconds!",
2364 maxExpectedDuration/1e9);
2365 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07002366 }
2367
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002368 if (rc == NO_ERROR) {
2369 mNeedConfig = true;
2370 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2371 if (rc == NO_ERROR) {
2372 ret = true;
2373 mPauseStateNotify = false;
2374 //Moving to active state while holding 'mLock' is important.
2375 //There could be pending calls to 'create-/deleteStream' which
2376 //will trigger another stream configuration while the already
2377 //present streams end up with outstanding buffers that will
2378 //not get drained.
2379 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang25152e42023-06-13 22:18:58 +00002380
2381 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode,
2382 true /*internalReconfig*/, ns2ms(systemTime() - startTime));
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002383 } else if (rc == DEAD_OBJECT) {
2384 // DEAD_OBJECT can be returned if either the consumer surface is
2385 // abandoned, or the HAL has died.
2386 // - If the HAL has died, configureStreamsLocked call will set
2387 // device to error state,
2388 // - If surface is abandoned, we should not set device to error
2389 // state.
2390 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002391 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002392 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002393 }
2394 } else {
2395 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2396 }
2397
Emilian Peev3bead5f2020-05-28 17:29:08 -07002398 if (markClientActive) {
2399 mStatusTracker->markComponentActive(clientStatusId);
2400 }
2401
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002402 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002403}
2404
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002405status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002406 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002407 ATRACE_CALL();
2408 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002409
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002410 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002411 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412 return INVALID_OPERATION;
2413 }
2414
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002415 if (operatingMode < 0) {
2416 CLOGE("Invalid operating mode: %d", operatingMode);
2417 return BAD_VALUE;
2418 }
2419
2420 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002421 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002422
2423 if (mOperatingMode != operatingMode) {
2424 mNeedConfig = true;
2425 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2426 mOperatingMode = operatingMode;
2427 }
2428
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002429 // Reset min expected duration when session is reconfigured.
2430 mMinExpectedDuration = 0;
2431
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002432 // In case called from configureStreams, abort queued input buffers not belonging to
2433 // any pending requests.
2434 if (mInputStream != NULL && notifyRequestThread) {
2435 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002436 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002437 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002438 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002439 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002440 if (res != OK) {
2441 // Exhausted acquiring all input buffers.
2442 break;
2443 }
2444
Emilian Peevf4816702020-04-03 15:44:51 -07002445 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002446 res = mInputStream->returnInputBuffer(inputBuffer);
2447 if (res != OK) {
2448 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2449 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2450 }
2451 }
2452 }
2453
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002454 if (!mNeedConfig) {
2455 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2456 return OK;
2457 }
2458
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002459 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002460 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002461 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2462 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002463 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002464 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002465 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002466 }
2467
Shuzhen Wang16610a62022-12-15 22:38:07 -08002468 // Override stream use case based on "adb shell command"
2469 overrideStreamUseCaseLocked();
2470
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002471 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002472 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002473
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002474 mPreparerThread->pause();
2475
Emilian Peevf4816702020-04-03 15:44:51 -07002476 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002477 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002478 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002479 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480
Emilian Peevf4816702020-04-03 15:44:51 -07002481 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002483 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002484
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002485
2486 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002487 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002488 inputStream = mInputStream->startConfiguration();
2489 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002490 CLOGE("Can't start input stream configuration");
2491 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002492 return INVALID_OPERATION;
2493 }
2494 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002495
2496 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002497 }
2498
Shuzhen Wang99080502021-03-07 21:08:20 -08002499 mGroupIdPhysicalCameraMap.clear();
Emilian Peevd865f0d2023-03-17 17:13:07 -07002500 mComposerOutput = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002501 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002502
2503 // Don't configure bidi streams twice, nor add them twice to the list
2504 if (mOutputStreams[i].get() ==
2505 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2506
2507 config.num_streams--;
2508 continue;
2509 }
2510
Emilian Peevf4816702020-04-03 15:44:51 -07002511 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002512 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002514 CLOGE("Can't start output stream configuration");
2515 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002516 return INVALID_OPERATION;
2517 }
2518 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002519
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002520 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002521 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2522 // always occupy the initial entry.
Emilian Peeve579d8b2023-02-28 14:16:08 -08002523 if ((outputStream->data_space == HAL_DATASPACE_V0_JFIF) ||
2524 (outputStream->data_space ==
2525 static_cast<android_dataspace_t>(
2526 aidl::android::hardware::graphics::common::Dataspace::JPEG_R))) {
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002527 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002528 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2529 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002530 } else if (outputStream->data_space ==
2531 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2532 bufferSizes[k] = outputStream->width * outputStream->height;
2533 } else {
2534 ALOGW("%s: Blob dataSpace %d not supported",
2535 __FUNCTION__, outputStream->data_space);
2536 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002537 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002538
2539 if (mOutputStreams[i]->isMultiResolution()) {
2540 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2541 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2542 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2543 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002544
2545 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07002546 mComposerOutput = true;
Emilian Peeve23f1d92021-09-20 14:56:01 -07002547 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002548 }
2549
2550 config.streams = streams.editArray();
2551
2552 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002553 // max_buffers, usage, and priv fields, as well as data_space and format
2554 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002555
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002556 int64_t logId = mCameraServiceProxyWrapper->getCurrentLogIdForCamera(mId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002557 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002558 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes, logId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002559 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002560
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002561 if (res == BAD_VALUE) {
2562 // HAL rejected this set of streams as unsupported, clean up config
2563 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002564 CLOGE("Set of requested inputs/outputs not supported by HAL");
2565 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002566 return BAD_VALUE;
2567 } else if (res != OK) {
2568 // Some other kind of error from configure_streams - this is not
2569 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002570 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2571 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002572 return res;
2573 }
2574
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002575 // Finish all stream configuration immediately.
2576 // TODO: Try to relax this later back to lazy completion, which should be
2577 // faster
2578
Igor Murashkin073f8572013-05-02 14:59:28 -07002579 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002580 bool streamReConfigured = false;
2581 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002582 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002583 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002584 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002585 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002586 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2587 return DEAD_OBJECT;
2588 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002589 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002590 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002591 if (streamReConfigured) {
2592 mInterface->onStreamReConfigured(mInputStream->getId());
2593 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002594 }
2595
2596 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002597 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002598 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002599 bool streamReConfigured = false;
2600 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002601 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002602 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002603 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002604 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002605 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2606 return DEAD_OBJECT;
2607 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002608 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002609 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002610 if (streamReConfigured) {
2611 mInterface->onStreamReConfigured(outputStream->getId());
2612 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002613 }
2614 }
2615
Emilian Peevd865f0d2023-03-17 17:13:07 -07002616 mRequestThread->setComposerSurface(mComposerOutput);
Emilian Peeve23f1d92021-09-20 14:56:01 -07002617
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002618 // Request thread needs to know to avoid using repeat-last-settings protocol
2619 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002620 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002621 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2622 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002623 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002624
Zhijun He90f7c372016-08-16 16:19:43 -07002625 char value[PROPERTY_VALUE_MAX];
2626 property_get("camera.fifo.disable", value, "0");
2627 int32_t disableFifo = atoi(value);
2628 if (disableFifo != 1) {
2629 // Boost priority of request thread to SCHED_FIFO.
2630 pid_t requestThreadTid = mRequestThread->getTid();
2631 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002632 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002633 if (res != OK) {
2634 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2635 strerror(-res), res);
2636 } else {
2637 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2638 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002639 }
2640
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002641 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002642 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2643 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2644 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2645 sessionParams.unlock(newSessionParams);
2646 mSessionParams.unlock(currentSessionParams);
2647 if (updateSessionParams) {
2648 mSessionParams = sessionParams;
2649 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002651 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002652
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002653 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002654 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002655
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002656 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002657
Zhijun He0a210512014-07-24 13:45:15 -07002658 // tear down the deleted streams after configure streams.
2659 mDeletedStreams.clear();
2660
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002661 auto rc = mPreparerThread->resume();
2662 if (rc != OK) {
2663 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2664 return rc;
2665 }
2666
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002667 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002668 mRequestBufferSM.onStreamsConfigured();
2669 }
2670
Cliff Wu3b268182021-07-06 15:44:43 +08002671 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002672 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002673 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002674 // configure the injection streams.
2675 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002676 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002677 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2678 res = mInjectionMethods->injectCamera(config, bufferSizes);
2679 if (res != OK) {
2680 ALOGE("Can't finish inject camera process!");
2681 return res;
2682 }
Cliff Wu3b268182021-07-06 15:44:43 +08002683 } else {
2684 // First run configureStreamsLocked() and then call injectCamera() case:
2685 // If the stream configuration has been completed and camera deive is active, but the
2686 // injection camera has not been injected yet, we need to store the stream configuration of
2687 // the internal camera (because the stream configuration of the injection camera is based
2688 // on the internal camera). When injecting occurs later, this configuration can be used by
2689 // the injection camera.
2690 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2691 " injection camera has not been injected yet.", __FUNCTION__);
2692 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002693 }
2694
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002695 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002696}
2697
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002698status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002699 ATRACE_CALL();
2700 status_t res;
2701
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002702 if (mFakeStreamId != NO_STREAM) {
2703 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002704 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002705 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002706 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002707 return INVALID_OPERATION;
2708 }
2709
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002710 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002711
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002712 sp<Camera3OutputStreamInterface> fakeStream =
2713 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002714
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002715 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002716 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002717 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002718 return res;
2719 }
2720
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002721 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002722 mNextStreamId++;
2723
2724 return OK;
2725}
2726
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002727status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002728 ATRACE_CALL();
2729 status_t res;
2730
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002731 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002732 if (mOutputStreams.size() == 1) return OK;
2733
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002734 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002735
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002736 // Ok, have a fake stream and there's at least one other output stream,
2737 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002738
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002739 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002740 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002741 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002742 return INVALID_OPERATION;
2743 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002744 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002745
2746 // Free up the stream endpoint so that it can be used by some other stream
2747 res = deletedStream->disconnect();
2748 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002749 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002750 // fall through since we want to still list the stream as deleted.
2751 }
2752 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002753 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002754
2755 return res;
2756}
2757
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002758void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002759 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002760 Mutex::Autolock l(mLock);
2761 va_list args;
2762 va_start(args, fmt);
2763
2764 setErrorStateLockedV(fmt, args);
2765
2766 va_end(args);
2767}
2768
2769void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002770 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002771 Mutex::Autolock l(mLock);
2772 setErrorStateLockedV(fmt, args);
2773}
2774
2775void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2776 va_list args;
2777 va_start(args, fmt);
2778
2779 setErrorStateLockedV(fmt, args);
2780
2781 va_end(args);
2782}
2783
2784void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002785 // Print out all error messages to log
2786 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002787 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002788
2789 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002790 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002791
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002792 mErrorCause = errorCause;
2793
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002794 if (mRequestThread != nullptr) {
2795 mRequestThread->setPaused(true);
2796 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002797 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002798
2799 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002800 sp<NotificationListener> listener = mListener.promote();
2801 if (listener != NULL) {
2802 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002803 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002804 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002805 }
2806
2807 // Save stack trace. View by dumping it later.
2808 CameraTraces::saveTrace();
2809 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002810}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002811
2812/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002813 * In-flight request management
2814 */
2815
Jianing Weicb0652e2014-03-12 18:29:36 -07002816status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002817 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002818 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002819 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002820 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002821 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002822 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002823 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002824 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002825
2826 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002827 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002828 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002829 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2830 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002831 if (res < 0) return res;
2832
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002833 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002834 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2835 // avoid a deadlock during reprocess requests.
2836 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002837 if (mStatusTracker != nullptr) {
2838 mStatusTracker->markComponentActive(mInFlightStatusId);
2839 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002840 }
2841
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002842 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002843 return OK;
2844}
2845
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002846void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002847 // Indicate idle inFlightMap to the status tracker
2848 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002849 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002850 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2851 // avoid a deadlock during reprocess requests.
2852 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002853 if (mStatusTracker != nullptr) {
2854 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2855 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002856 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002857 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002858}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002859
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002860void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002861 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002862 // something has likely gone wrong. This might still be legit only if application send in
2863 // a long burst of long exposure requests.
2864 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2865 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2866 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2867 mInFlightMap.size(), mExpectedInflightDuration);
2868 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2869 kInFlightWarnLimitHighSpeed) {
2870 CLOGW("In-flight list too large for high speed configuration: %zu,"
2871 "total inflight duration %" PRIu64,
2872 mInFlightMap.size(), mExpectedInflightDuration);
2873 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002874 }
2875}
2876
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002877void Camera3Device::onInflightMapFlushedLocked() {
2878 mExpectedInflightDuration = 0;
2879}
2880
2881void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002882 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002883 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2884 mInFlightMap.removeItemsAt(idx, 1);
2885
2886 onInflightEntryRemovedLocked(duration);
2887}
2888
2889
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002890void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002891 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002892 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002893 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002894 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002895 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002896 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002897
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002898 FlushInflightReqStates states {
2899 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002900 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002901
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002902 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002903}
2904
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002905CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002906 ALOGV("%s", __FUNCTION__);
2907
Igor Murashkin1e479c02013-09-06 16:55:14 -07002908 CameraMetadata retVal;
2909
2910 if (mRequestThread != NULL) {
2911 retVal = mRequestThread->getLatestRequest();
2912 }
2913
Igor Murashkin1e479c02013-09-06 16:55:14 -07002914 return retVal;
2915}
2916
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002917void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002918 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002919 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002920 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2921 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002922
2923 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002924 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002925}
2926
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002927void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002928 std::vector<native_handle_t*> *handles, bool closeFd) {
2929 if (handles == nullptr) {
2930 return;
2931 }
2932 if (closeFd) {
2933 for (auto& handle : *handles) {
2934 native_handle_close(handle);
2935 }
2936 }
2937 for (auto& handle : *handles) {
2938 native_handle_delete(handle);
2939 }
2940 handles->clear();
2941 return;
2942}
2943
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002944/**
2945 * HalInterface inner class methods
2946 */
2947
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002948void Camera3Device::HalInterface::getInflightBufferKeys(
2949 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002950 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002951 return;
2952}
2953
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002954void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2955 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002956 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002957 return;
2958}
2959
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002960bool Camera3Device::HalInterface::verifyBufferIds(
2961 int32_t streamId, std::vector<uint64_t>& bufIds) {
2962 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002963}
2964
2965status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002966 int32_t frameNumber, int32_t streamId,
2967 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002968 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002969}
2970
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002971status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002972 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002973 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002974}
2975
2976// Find and pop a buffer_handle_t based on bufferId
2977status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002978 uint64_t bufferId,
2979 /*out*/ buffer_handle_t** buffer,
2980 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002981 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002982}
2983
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002984std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2985 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002986 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002987}
2988
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002989uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2990 const native_handle_t* handle) {
2991 return mBufferRecords.removeOneBufferCache(streamId, handle);
2992}
2993
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002994void Camera3Device::HalInterface::onBufferFreed(
2995 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002996 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2997 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2998 if (bufferId != BUFFER_ID_NO_BUFFER) {
2999 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003000 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003001}
3002
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003003void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003004 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
3005 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3006 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003007 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3008 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003009}
3010
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003011/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003012 * RequestThread inner class methods
3013 */
3014
3015Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003016 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003017 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003018 bool useHalBufManager,
Austin Borger18b30a72022-10-27 12:20:29 -07003019 bool supportCameraMute,
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003020 bool overrideToPortrait,
3021 bool supportSettingsOverride) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003022 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003023 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003024 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003025 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003026 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003027 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003028 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07003029 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030 mReconfigured(false),
3031 mDoPause(false),
3032 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003033 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003034 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003035 mLatestRequestId(NAME_NOT_FOUND),
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09003036 mLatestFailedRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003037 mCurrentAfTriggerId(0),
3038 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003039 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00003040 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07003041 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07003042 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003043 mCameraMuteChanged(false),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003044 mSettingsOverride(ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003045 mRepeatingLastFrameNumber(
3046 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003047 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003048 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003049 mRequestLatency(kRequestLatencyBinSize),
3050 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003051 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003052 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07003053 mSupportCameraMute(supportCameraMute),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003054 mOverrideToPortrait(overrideToPortrait),
3055 mSupportSettingsOverride(supportSettingsOverride) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003056 mStatusId = statusTracker->addComponent("RequestThread");
Emilian Peeva1b26262023-02-06 17:27:41 -08003057 mVndkVersion = property_get_int32("ro.vndk.version", __ANDROID_API_FUTURE__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058}
3059
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003060Camera3Device::RequestThread::~RequestThread() {}
3061
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003062void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003063 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003064 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003065 Mutex::Autolock l(mRequestLock);
3066 mListener = listener;
3067}
3068
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003069void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08003070 const CameraMetadata& sessionParams,
3071 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003072 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003073 Mutex::Autolock l(mRequestLock);
3074 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003075 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08003076 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003077 // Prepare video stream for high speed recording.
3078 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003079 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003080}
3081
Jianing Wei90e59c92014-03-12 18:29:36 -07003082status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003083 List<sp<CaptureRequest> > &requests,
3084 /*out*/
3085 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003086 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003087 Mutex::Autolock l(mRequestLock);
3088 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3089 ++it) {
3090 mRequestQueue.push_back(*it);
3091 }
3092
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003093 if (lastFrameNumber != NULL) {
3094 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3095 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3096 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3097 *lastFrameNumber);
3098 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003099
Jianing Wei90e59c92014-03-12 18:29:36 -07003100 unpauseForNewRequests();
3101
3102 return OK;
3103}
3104
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003105
3106status_t Camera3Device::RequestThread::queueTrigger(
3107 RequestTrigger trigger[],
3108 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003109 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003110 Mutex::Autolock l(mTriggerMutex);
3111 status_t ret;
3112
3113 for (size_t i = 0; i < count; ++i) {
3114 ret = queueTriggerLocked(trigger[i]);
3115
3116 if (ret != OK) {
3117 return ret;
3118 }
3119 }
3120
3121 return OK;
3122}
3123
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003124const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3125 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003126 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003127 if (d != nullptr) return d->mId;
3128 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003129}
3130
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003131status_t Camera3Device::RequestThread::queueTriggerLocked(
3132 RequestTrigger trigger) {
3133
3134 uint32_t tag = trigger.metadataTag;
3135 ssize_t index = mTriggerMap.indexOfKey(tag);
3136
3137 switch (trigger.getTagType()) {
3138 case TYPE_BYTE:
3139 // fall-through
3140 case TYPE_INT32:
3141 break;
3142 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003143 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3144 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003145 return INVALID_OPERATION;
3146 }
3147
3148 /**
3149 * Collect only the latest trigger, since we only have 1 field
3150 * in the request settings per trigger tag, and can't send more than 1
3151 * trigger per request.
3152 */
3153 if (index != NAME_NOT_FOUND) {
3154 mTriggerMap.editValueAt(index) = trigger;
3155 } else {
3156 mTriggerMap.add(tag, trigger);
3157 }
3158
3159 return OK;
3160}
3161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003162status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003163 const RequestList &requests,
3164 /*out*/
3165 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003166 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003167 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003168 if (lastFrameNumber != NULL) {
3169 *lastFrameNumber = mRepeatingLastFrameNumber;
3170 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003171 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003172 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003173 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3174 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003175
3176 unpauseForNewRequests();
3177
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003178 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003179 return OK;
3180}
3181
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003182bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003183 if (mRepeatingRequests.empty()) {
3184 return false;
3185 }
3186 int32_t requestId = requestIn->mResultExtras.requestId;
3187 const RequestList &repeatRequests = mRepeatingRequests;
3188 // All repeating requests are guaranteed to have same id so only check first quest
3189 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3190 return (firstRequest->mResultExtras.requestId == requestId);
3191}
3192
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003193status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003194 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003195 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003196 return clearRepeatingRequestsLocked(lastFrameNumber);
3197
3198}
3199
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003200status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3201 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003202 std::vector<int32_t> streamIds;
3203 for (const auto& request : mRepeatingRequests) {
3204 for (const auto& stream : request->mOutputStreams) {
3205 streamIds.push_back(stream->getId());
3206 }
3207 }
3208
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003209 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003210 if (lastFrameNumber != NULL) {
3211 *lastFrameNumber = mRepeatingLastFrameNumber;
3212 }
Emilian Peev2295df72021-11-12 18:14:10 -08003213
3214 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3215
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003216 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003217 return OK;
3218}
3219
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003220status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003221 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003222 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003223 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003224 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003225
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003226 // Send errors for all requests pending in the request queue, including
3227 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003228 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003229 if (listener != NULL) {
3230 for (RequestList::iterator it = mRequestQueue.begin();
3231 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003232 // Abort the input buffers for reprocess requests.
3233 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003234 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003235 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003236 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003237 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003238 if (res != OK) {
3239 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3240 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3241 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003242 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003243 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3244 if (res != OK) {
3245 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3246 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3247 }
3248 }
3249 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003250 // Set the frame number this request would have had, if it
3251 // had been submitted; this frame number will not be reused.
3252 // The requestId and burstId fields were set when the request was
3253 // submitted originally (in convertMetadataListToRequestListLocked)
3254 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003255 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003256 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003257 }
3258 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003259 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003260
3261 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003262 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003263 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003264 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003265 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003266 return OK;
3267}
3268
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003269status_t Camera3Device::RequestThread::flush() {
3270 ATRACE_CALL();
3271 Mutex::Autolock l(mFlushLock);
3272
Emilian Peev08dd2452017-04-06 16:55:14 +01003273 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003274}
3275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003276void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003277 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003278 Mutex::Autolock l(mPauseLock);
3279 mDoPause = paused;
3280 mDoPauseSignal.signal();
3281}
3282
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003283status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3284 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003285 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003286 Mutex::Autolock l(mLatestRequestMutex);
3287 status_t res;
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09003288 while (mLatestRequestId != requestId && mLatestFailedRequestId != requestId) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003289 nsecs_t startTime = systemTime();
3290
3291 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3292 if (res != OK) return res;
3293
3294 timeout -= (systemTime() - startTime);
3295 }
3296
3297 return OK;
3298}
3299
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003300void Camera3Device::RequestThread::requestExit() {
3301 // Call parent to set up shutdown
3302 Thread::requestExit();
3303 // The exit from any possible waits
3304 mDoPauseSignal.signal();
3305 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003306
3307 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3308 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003309}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003310
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003311void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003312 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003313 bool surfaceAbandoned = false;
3314 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003315 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003316 {
3317 Mutex::Autolock l(mRequestLock);
3318 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3319 // repeating requests.
3320 for (const auto& request : mRepeatingRequests) {
3321 for (const auto& s : request->mOutputStreams) {
3322 if (s->isAbandoned()) {
3323 surfaceAbandoned = true;
3324 clearRepeatingRequestsLocked(&lastFrameNumber);
3325 break;
3326 }
3327 }
3328 if (surfaceAbandoned) {
3329 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003330 }
3331 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003332 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003333 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003334
3335 if (listener != NULL && surfaceAbandoned) {
3336 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003337 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003338}
3339
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003340bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003341 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003342 status_t res;
3343 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003344 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003345 uint32_t numRequestProcessed = 0;
3346 for (size_t i = 0; i < batchSize; i++) {
3347 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003348 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003349 }
3350
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003351 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3352
3353 bool triggerRemoveFailed = false;
3354 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3355 for (size_t i = 0; i < numRequestProcessed; i++) {
3356 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3357 nextRequest.submitted = true;
3358
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003359 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003360
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003361 if (!triggerRemoveFailed) {
3362 // Remove any previously queued triggers (after unlock)
3363 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3364 if (removeTriggerRes != OK) {
3365 triggerRemoveFailed = true;
3366 triggerFailedRequest = nextRequest;
3367 }
3368 }
3369 }
3370
3371 if (triggerRemoveFailed) {
3372 SET_ERR("RequestThread: Unable to remove triggers "
3373 "(capture request %d, HAL device: %s (%d)",
3374 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3375 cleanUpFailedRequests(/*sendRequestError*/ false);
3376 return false;
3377 }
3378
3379 if (res != OK) {
3380 // Should only get a failure here for malformed requests or device-level
3381 // errors, so consider all errors fatal. Bad metadata failures should
3382 // come through notify.
3383 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3384 mNextRequests[numRequestProcessed].halRequest.frame_number,
3385 strerror(-res), res);
3386 cleanUpFailedRequests(/*sendRequestError*/ false);
3387 return false;
3388 }
3389 return true;
3390}
3391
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003392Camera3Device::RequestThread::ExpectedDurationInfo
3393 Camera3Device::RequestThread::calculateExpectedDurationRange(
3394 const camera_metadata_t *request) {
3395 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003396 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003397 InFlightRequest::kDefaultMaxExpectedDuration,
3398 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003399 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3400 find_camera_metadata_ro_entry(request,
3401 ANDROID_CONTROL_AE_MODE,
3402 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003403 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003404
3405 switch (e.data.u8[0]) {
3406 case ANDROID_CONTROL_AE_MODE_OFF:
3407 find_camera_metadata_ro_entry(request,
3408 ANDROID_SENSOR_EXPOSURE_TIME,
3409 &e);
3410 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003411 expectedDurationInfo.minDuration = e.data.i64[0];
3412 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003413 }
3414 find_camera_metadata_ro_entry(request,
3415 ANDROID_SENSOR_FRAME_DURATION,
3416 &e);
3417 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003418 expectedDurationInfo.minDuration =
3419 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3420 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003421 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003422 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003423 break;
3424 default:
3425 find_camera_metadata_ro_entry(request,
3426 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3427 &e);
3428 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003429 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3430 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003431 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003432 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003433 break;
3434 }
3435
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003436 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003437}
3438
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003439bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3440 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3441 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3442 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3443 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3444 return true;
3445 }
3446
3447 return false;
3448}
3449
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003450void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3451 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003452 camera_capture_request_t& halRequest = nextRequest.halRequest;
3453 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003454 Mutex::Autolock al(mLatestRequestMutex);
3455
Shuzhen Wang83bff122020-11-20 15:51:39 -08003456 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003457 mLatestRequest.acquire(cloned);
3458
3459 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003460 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3461 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3462 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003463 CameraMetadata(cloned));
3464 }
3465
3466 sp<Camera3Device> parent = mParent.promote();
3467 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003468 int32_t inputStreamId = -1;
3469 if (halRequest.input_buffer != nullptr) {
3470 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3471 }
3472
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003473 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003474 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003475 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3476 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003477 }
3478 }
3479
Shuzhen Wang83bff122020-11-20 15:51:39 -08003480 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003481 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003482 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003483 }
3484
Shuzhen Wang83bff122020-11-20 15:51:39 -08003485 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003486}
3487
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003488bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3489 ATRACE_CALL();
3490 bool updatesDetected = false;
3491
Emilian Peev4ec17882019-01-24 17:16:58 -08003492 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003493 for (auto tag : mSessionParamKeys) {
3494 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003495 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003496
3497 if (entry.count > 0) {
3498 bool isDifferent = false;
3499 if (lastEntry.count > 0) {
3500 // Have a last value, compare to see if changed
3501 if (lastEntry.type == entry.type &&
3502 lastEntry.count == entry.count) {
3503 // Same type and count, compare values
3504 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3505 size_t entryBytes = bytesPerValue * lastEntry.count;
3506 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3507 if (cmp != 0) {
3508 isDifferent = true;
3509 }
3510 } else {
3511 // Count or type has changed
3512 isDifferent = true;
3513 }
3514 } else {
3515 // No last entry, so always consider to be different
3516 isDifferent = true;
3517 }
3518
3519 if (isDifferent) {
3520 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003521 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3522 updatesDetected = true;
3523 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003524 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003525 }
3526 } else if (lastEntry.count > 0) {
3527 // Value has been removed
3528 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003529 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003530 updatesDetected = true;
3531 }
3532 }
3533
Emilian Peev4ec17882019-01-24 17:16:58 -08003534 bool reconfigureRequired;
3535 if (updatesDetected) {
3536 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3537 updatedParams);
3538 mLatestSessionParams = updatedParams;
3539 } else {
3540 reconfigureRequired = false;
3541 }
3542
3543 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003544}
3545
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003546bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003547 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003548 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003549 // Any function called from threadLoop() must not hold mInterfaceLock since
3550 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3551 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003552
3553 // Handle paused state.
3554 if (waitIfPaused()) {
3555 return true;
3556 }
3557
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003558 // Wait for the next batch of requests.
3559 waitForNextRequestBatch();
3560 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003561 return true;
3562 }
3563
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003564 // Get the latest request ID, if any
3565 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003566 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003567 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003568 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003569 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003570 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003571 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3572 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003573 }
3574
Emilian Peevd865f0d2023-03-17 17:13:07 -07003575 for (size_t i = 0; i < mNextRequests.size(); i++) {
3576 auto& nextRequest = mNextRequests.editItemAt(i);
3577 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3578 // Do not override rotate&crop for stream configurations that include
3579 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3580 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3581 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3582 overrideAutoRotateAndCrop(captureRequest);
3583 captureRequest->mAutoframingChanged = overrideAutoframing(captureRequest);
3584 }
3585
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003586 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3587 // or a single request from streaming or burst. In either case the first element
3588 // should contain the latest camera settings that we need to check for any session
3589 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003590 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003591 res = OK;
3592
3593 //Input stream buffers are already acquired at this point so an input stream
3594 //will not be able to move to idle state unless we force it.
3595 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3596 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3597 if (res != OK) {
3598 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3599 cleanUpFailedRequests(/*sendRequestError*/ false);
3600 return false;
3601 }
3602 }
3603
3604 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003605 sp<Camera3Device> parent = mParent.promote();
3606 if (parent != nullptr) {
3607 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003608 }
3609
3610 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3611 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3612 if (res != OK) {
3613 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3614 cleanUpFailedRequests(/*sendRequestError*/ false);
3615 return false;
3616 }
3617 }
3618 }
3619 }
3620
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003621 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003622 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003623 if (res == TIMED_OUT) {
3624 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003625 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003626 // Check if any stream is abandoned.
3627 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003628 return true;
3629 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003630 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003631 return false;
3632 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003633
Zhijun Hecc27e112013-10-03 16:12:43 -07003634 // Inform waitUntilRequestProcessed thread of a new request ID
3635 {
3636 Mutex::Autolock al(mLatestRequestMutex);
3637
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003638 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003639 mLatestRequestSignal.signal();
3640 }
3641
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003642 // Submit a batch of requests to HAL.
3643 // Use flush lock only when submitting multilple requests in a batch.
3644 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3645 // which may take a long time to finish so synchronizing flush() and
3646 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3647 // For now, only synchronize for high speed recording and we should figure something out for
3648 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003649 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003650
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003651 if (useFlushLock) {
3652 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003653 }
3654
Zhijun Hef0645c12016-08-02 00:58:11 -07003655 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003656 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003657
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003658 sp<Camera3Device> parent = mParent.promote();
3659 if (parent != nullptr) {
3660 parent->mRequestBufferSM.onSubmittingRequest();
3661 }
3662
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003663 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003664 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003665 submitRequestSuccess = sendRequestsBatch();
3666
Shuzhen Wang686f6442017-06-20 16:16:04 -07003667 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3668 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003669
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003670 if (useFlushLock) {
3671 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003672 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003673
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003674 // Unset as current request
3675 {
3676 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003677 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003678 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003679 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003680
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003681 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003682}
3683
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003684status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3685 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3686 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3687 ANDROID_SCALER_CROP_REGION_SET};
3688 if (request == nullptr) {
3689 ALOGE("%s request metadata nullptr", __FUNCTION__);
3690 return BAD_VALUE;
3691 }
3692 status_t res = OK;
3693 for (const auto &key : kFwkOnlyRegionKeys) {
3694 if (request->exists(key)) {
3695 res = request->erase(key);
3696 if (res != OK) {
3697 return res;
3698 }
3699 }
3700 }
3701 return OK;
3702}
3703
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003704status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003705 ATRACE_CALL();
3706
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003707 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003708 for (size_t i = 0; i < mNextRequests.size(); i++) {
3709 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003710 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003711 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3712 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003713
3714 // Prepare a request to HAL
3715 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3716
3717 // Insert any queued triggers (before metadata is locked)
3718 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003719 if (res < 0) {
3720 SET_ERR("RequestThread: Unable to insert triggers "
3721 "(capture request %d, HAL device: %s (%d)",
3722 halRequest->frame_number, strerror(-res), res);
3723 return INVALID_OPERATION;
3724 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003725
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003726 int triggerCount = res;
3727 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3728 mPrevTriggers = triggerCount;
3729
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003730 bool testPatternChanged = overrideTestPattern(captureRequest);
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003731 bool settingsOverrideChanged = overrideSettingsOverride(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003732
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003733 // If the request is the same as last, or we had triggers now or last time or
3734 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003735 bool newRequest =
Emilian Peevd865f0d2023-03-17 17:13:07 -07003736 (mPrevRequest != captureRequest || triggersMixedIn ||
3737 captureRequest->mRotateAndCropChanged ||
3738 captureRequest->mAutoframingChanged ||
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003739 testPatternChanged || settingsOverrideChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003740 // Request settings are all the same within one batch, so only treat the first
3741 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003742 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003743 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003744 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003745 /**
3746 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003747 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003748 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003749 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003750 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003751 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003752 "(capture request %d, HAL device: %s (%d)",
3753 halRequest->frame_number, strerror(-res), res);
3754 return INVALID_OPERATION;
3755 }
3756
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003757 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003758 sp<Camera3Device> parent = mParent.promote();
3759 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003760 List<PhysicalCameraSettings>::iterator it;
3761 for (it = captureRequest->mSettingsList.begin();
3762 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003763 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3764 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003765 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3766 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3767 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3768 res);
3769 return INVALID_OPERATION;
3770 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003771 continue;
3772 }
3773
3774 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3775 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3776 updateCaptureRequest(&(it->metadata));
3777 if (res != OK) {
3778 SET_ERR("RequestThread: Unable to correct capture requests "
3779 "for scaler crop region and metering regions for request "
3780 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3781 res);
3782 return INVALID_OPERATION;
3783 }
3784 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003785 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3786 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3787 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3788 res);
3789 return INVALID_OPERATION;
3790 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003791 }
3792 }
3793
3794 // Correct metadata regions for distortion correction if enabled
3795 for (it = captureRequest->mSettingsList.begin();
3796 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003797 if (parent->mDistortionMappers.find(it->cameraId) ==
3798 parent->mDistortionMappers.end()) {
3799 continue;
3800 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003801
3802 if (!captureRequest->mDistortionCorrectionUpdated) {
3803 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3804 &(it->metadata));
3805 if (res != OK) {
3806 SET_ERR("RequestThread: Unable to correct capture requests "
3807 "for lens distortion for request %d: %s (%d)",
3808 halRequest->frame_number, strerror(-res), res);
3809 return INVALID_OPERATION;
3810 }
3811 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003812 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003813 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003814
3815 for (it = captureRequest->mSettingsList.begin();
3816 it != captureRequest->mSettingsList.end(); it++) {
3817 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3818 parent->mZoomRatioMappers.end()) {
3819 continue;
3820 }
3821
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003822 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003823 cameraIdsWithZoom.insert(it->cameraId);
3824 }
3825
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003826 if (!captureRequest->mZoomRatioUpdated) {
3827 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3828 &(it->metadata));
3829 if (res != OK) {
3830 SET_ERR("RequestThread: Unable to correct capture requests "
3831 "for zoom ratio for request %d: %s (%d)",
3832 halRequest->frame_number, strerror(-res), res);
3833 return INVALID_OPERATION;
3834 }
3835 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003836 }
3837 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003838 if (captureRequest->mRotateAndCropAuto &&
3839 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003840 for (it = captureRequest->mSettingsList.begin();
3841 it != captureRequest->mSettingsList.end(); it++) {
3842 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3843 if (mapper != parent->mRotateAndCropMappers.end()) {
3844 res = mapper->second.updateCaptureRequest(&(it->metadata));
3845 if (res != OK) {
3846 SET_ERR("RequestThread: Unable to correct capture requests "
3847 "for rotate-and-crop for request %d: %s (%d)",
3848 halRequest->frame_number, strerror(-res), res);
3849 return INVALID_OPERATION;
3850 }
3851 }
3852 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003853 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003854 }
Emilian Peeva1b26262023-02-06 17:27:41 -08003855
3856 for (it = captureRequest->mSettingsList.begin();
3857 it != captureRequest->mSettingsList.end(); it++) {
3858 res = hardware::cameraservice::utils::conversion::aidl::filterVndkKeys(
3859 mVndkVersion, it->metadata, false /*isStatic*/);
3860 if (res != OK) {
3861 SET_ERR("RequestThread: Failed during VNDK filter of capture requests "
3862 "%d: %s (%d)", halRequest->frame_number, strerror(-res), res);
3863 return INVALID_OPERATION;
3864 }
3865 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003866 }
3867 }
3868
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003869 /**
3870 * The request should be presorted so accesses in HAL
3871 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3872 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003873 captureRequest->mSettingsList.begin()->metadata.sort();
3874 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003875 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003876 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003877 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3878
3879 IF_ALOGV() {
3880 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3881 find_camera_metadata_ro_entry(
3882 halRequest->settings,
3883 ANDROID_CONTROL_AF_TRIGGER,
3884 &e
3885 );
3886 if (e.count > 0) {
3887 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3888 __FUNCTION__,
3889 halRequest->frame_number,
3890 e.data.u8[0]);
3891 }
3892 }
3893 } else {
3894 // leave request.settings NULL to indicate 'reuse latest given'
3895 ALOGVV("%s: Request settings are REUSED",
3896 __FUNCTION__);
3897 }
3898
Emilian Peevaebbe412018-01-15 13:53:24 +00003899 if (captureRequest->mSettingsList.size() > 1) {
3900 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3901 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003902 if (newRequest) {
3903 halRequest->physcam_settings =
3904 new const camera_metadata* [halRequest->num_physcam_settings];
3905 } else {
3906 halRequest->physcam_settings = nullptr;
3907 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003908 auto it = ++captureRequest->mSettingsList.begin();
3909 size_t i = 0;
3910 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3911 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003912 if (newRequest) {
3913 it->metadata.sort();
3914 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3915 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003916 }
3917 }
3918
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003919 uint32_t totalNumBuffers = 0;
3920
3921 // Fill in buffers
3922 if (captureRequest->mInputStream != NULL) {
3923 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003924
3925 halRequest->input_width = captureRequest->mInputBufferSize.width;
3926 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003927 totalNumBuffers += 1;
3928 } else {
3929 halRequest->input_buffer = NULL;
3930 }
3931
Emilian Peevf4816702020-04-03 15:44:51 -07003932 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003933 captureRequest->mOutputStreams.size());
3934 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003935 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003936
3937 sp<Camera3Device> parent = mParent.promote();
3938 if (parent == NULL) {
3939 // Should not happen, and nowhere to send errors to, so just log it
3940 CLOGE("RequestThread: Parent is gone");
3941 return INVALID_OPERATION;
3942 }
3943 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3944
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003945 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003946 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003947 sp<Camera3OutputStreamInterface> outputStream =
3948 captureRequest->mOutputStreams.editItemAt(j);
3949 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003950
3951 // Prepare video buffers for high speed recording on the first video request.
3952 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3953 // Only try to prepare video stream on the first video request.
3954 mPrepareVideoStream = false;
3955
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003956 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3957 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003958 while (res == NOT_ENOUGH_DATA) {
3959 res = outputStream->prepareNextBuffer();
3960 }
3961 if (res != OK) {
3962 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3963 __FUNCTION__, strerror(-res), res);
3964 outputStream->cancelPrepare();
3965 }
3966 }
3967
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003968 std::vector<size_t> uniqueSurfaceIds;
3969 res = outputStream->getUniqueSurfaceIds(
3970 captureRequest->mOutputSurfaces[streamId],
3971 &uniqueSurfaceIds);
3972 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3973 if (res != OK && res != INVALID_OPERATION) {
3974 ALOGE("%s: failed to query stream %d unique surface IDs",
3975 __FUNCTION__, streamId);
3976 return res;
3977 }
3978 if (res == OK) {
3979 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3980 }
3981
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003982 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003983 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003984 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003985 return TIMED_OUT;
3986 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003987 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003988 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003989 buffer.stream = outputStream->asHalStream();
3990 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003991 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003992 buffer.acquire_fence = -1;
3993 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003994 // Mark the output stream as unpreparable to block clients from calling
3995 // 'prepare' after this request reaches CameraHal and before the respective
3996 // buffers are requested.
3997 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003998 } else {
3999 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4000 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004001 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004002 if (res != OK) {
4003 // Can't get output buffer from gralloc queue - this could be due to
4004 // abandoned queue or other consumer misbehavior, so not a fatal
4005 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004006 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004007 " %s (%d)", strerror(-res), res);
4008
4009 return TIMED_OUT;
4010 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004011 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004012
4013 {
4014 sp<Camera3Device> parent = mParent.promote();
4015 if (parent != nullptr) {
4016 const String8& streamCameraId = outputStream->getPhysicalCameraId();
Emilian Peevdf6d0502023-07-26 22:39:33 +00004017 // Consider the case where clients are sending a single logical camera request
4018 // to physical output/outputs
4019 bool singleRequest = captureRequest->mSettingsList.size() == 1;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004020 for (const auto& settings : captureRequest->mSettingsList) {
Emilian Peevdf6d0502023-07-26 22:39:33 +00004021 if (((streamCameraId.isEmpty() || singleRequest) &&
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004022 parent->getId() == settings.cameraId.c_str()) ||
4023 streamCameraId == settings.cameraId.c_str()) {
4024 outputStream->fireBufferRequestForFrameNumber(
4025 captureRequest->mResultExtras.frameNumber,
4026 settings.metadata);
4027 }
4028 }
4029 }
4030 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004031
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004032 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08004033 int32_t streamGroupId = outputStream->getHalStreamGroupId();
4034 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4035 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4036 } else if (!physicalCameraId.isEmpty()) {
4037 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004038 }
4039 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004040 }
4041 totalNumBuffers += halRequest->num_output_buffers;
4042
4043 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004044 // If this request list is for constrained high speed recording (not
4045 // preview), and the current request is not the last one in the batch,
4046 // do not send callback to the app.
4047 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004048 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004049 hasCallback = false;
4050 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004051 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004052 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004053 const camera_metadata_t* settings = halRequest->settings;
4054 bool shouldUnlockSettings = false;
4055 if (settings == nullptr) {
4056 shouldUnlockSettings = true;
4057 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4058 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004059 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4060 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004061 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004062 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4063 isStillCapture = true;
4064 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4065 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004066
Emilian Peevaf8416e2021-02-04 17:52:43 -08004067 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004068 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004069 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4070 isZslCapture = true;
4071 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004072 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004073 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004074 res = parent->registerInFlight(halRequest->frame_number,
4075 totalNumBuffers, captureRequest->mResultExtras,
4076 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004077 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004078 expectedDurationInfo.minDuration,
4079 expectedDurationInfo.maxDuration,
4080 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004081 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004082 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
4083 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004084 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004085 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004086 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4087 ", burstId = %" PRId32 ".",
4088 __FUNCTION__,
4089 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4090 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004091
4092 if (shouldUnlockSettings) {
4093 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4094 }
4095
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004096 if (res != OK) {
4097 SET_ERR("RequestThread: Unable to register new in-flight request:"
4098 " %s (%d)", strerror(-res), res);
4099 return INVALID_OPERATION;
4100 }
4101 }
4102
4103 return OK;
4104}
4105
Igor Murashkin1e479c02013-09-06 16:55:14 -07004106CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004107 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004108 Mutex::Autolock al(mLatestRequestMutex);
4109
4110 ALOGV("RequestThread::%s", __FUNCTION__);
4111
4112 return mLatestRequest;
4113}
4114
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004115bool Camera3Device::RequestThread::isStreamPending(
4116 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004117 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004118 Mutex::Autolock l(mRequestLock);
4119
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004120 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004121 if (!nextRequest.submitted) {
4122 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4123 if (stream == s) return true;
4124 }
4125 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004126 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004127 }
4128
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004129 for (const auto& request : mRequestQueue) {
4130 for (const auto& s : request->mOutputStreams) {
4131 if (stream == s) return true;
4132 }
4133 if (stream == request->mInputStream) return true;
4134 }
4135
4136 for (const auto& request : mRepeatingRequests) {
4137 for (const auto& s : request->mOutputStreams) {
4138 if (stream == s) return true;
4139 }
4140 if (stream == request->mInputStream) return true;
4141 }
4142
4143 return false;
4144}
Jianing Weicb0652e2014-03-12 18:29:36 -07004145
Emilian Peev40ead602017-09-26 15:46:36 +01004146bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4147 ATRACE_CALL();
4148 Mutex::Autolock l(mRequestLock);
4149
4150 for (const auto& nextRequest : mNextRequests) {
4151 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4152 if (s.first == streamId) {
4153 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4154 if (it != s.second.end()) {
4155 return true;
4156 }
4157 }
4158 }
4159 }
4160
4161 for (const auto& request : mRequestQueue) {
4162 for (const auto& s : request->mOutputSurfaces) {
4163 if (s.first == streamId) {
4164 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4165 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004166 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004167 }
4168 }
4169 }
4170 }
4171
4172 for (const auto& request : mRepeatingRequests) {
4173 for (const auto& s : request->mOutputSurfaces) {
4174 if (s.first == streamId) {
4175 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4176 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004177 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004178 }
4179 }
4180 }
4181 }
4182
4183 return false;
4184}
4185
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004186void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4187 if (!mUseHalBufManager) {
4188 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4189 return;
4190 }
4191
4192 Mutex::Autolock pl(mPauseLock);
4193 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004194 mInterface->signalPipelineDrain(streamIds);
4195 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004196 }
4197 // If request thread is still busy, wait until paused then notify HAL
4198 mNotifyPipelineDrain = true;
4199 mStreamIdsToBeDrained = streamIds;
4200}
4201
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004202void Camera3Device::RequestThread::resetPipelineDrain() {
4203 Mutex::Autolock pl(mPauseLock);
4204 mNotifyPipelineDrain = false;
4205 mStreamIdsToBeDrained.clear();
4206}
4207
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004208void Camera3Device::RequestThread::clearPreviousRequest() {
4209 Mutex::Autolock l(mRequestLock);
4210 mPrevRequest.clear();
4211}
4212
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004213status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4214 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4215 ATRACE_CALL();
4216 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004217 mRotateAndCropOverride = rotateAndCropValue;
4218 return OK;
4219}
4220
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004221status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4222 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4223 ATRACE_CALL();
4224 Mutex::Autolock l(mTriggerMutex);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004225 mAutoframingOverride = autoframingValue;
4226 return OK;
4227}
4228
Emilian Peeve23f1d92021-09-20 14:56:01 -07004229status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4230 ATRACE_CALL();
4231 Mutex::Autolock l(mTriggerMutex);
4232 mComposerOutput = composerSurfacePresent;
4233 return OK;
4234}
4235
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004236status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004237 ATRACE_CALL();
4238 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004239 if (muteMode != mCameraMute) {
4240 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004241 mCameraMuteChanged = true;
4242 }
4243 return OK;
4244}
4245
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004246status_t Camera3Device::RequestThread::setZoomOverride(int32_t zoomOverride) {
4247 ATRACE_CALL();
4248 Mutex::Autolock l(mTriggerMutex);
4249 mSettingsOverride = zoomOverride;
4250 return OK;
4251}
4252
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004253nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004254 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004255 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004256 return mExpectedInflightDuration > kMinInflightDuration ?
4257 mExpectedInflightDuration : kMinInflightDuration;
4258}
4259
Emilian Peevaebbe412018-01-15 13:53:24 +00004260void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004261 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004262 if ((request == nullptr) || (halRequest == nullptr)) {
4263 ALOGE("%s: Invalid request!", __FUNCTION__);
4264 return;
4265 }
4266
4267 if (halRequest->num_physcam_settings > 0) {
4268 if (halRequest->physcam_id != nullptr) {
4269 delete [] halRequest->physcam_id;
4270 halRequest->physcam_id = nullptr;
4271 }
4272 if (halRequest->physcam_settings != nullptr) {
4273 auto it = ++(request->mSettingsList.begin());
4274 size_t i = 0;
4275 for (; it != request->mSettingsList.end(); it++, i++) {
4276 it->metadata.unlock(halRequest->physcam_settings[i]);
4277 }
4278 delete [] halRequest->physcam_settings;
4279 halRequest->physcam_settings = nullptr;
4280 }
4281 }
4282}
4283
Ravneetaeb20dc2022-03-30 05:33:03 +00004284status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4285 Mutex::Autolock il(mInterfaceLock);
4286 Mutex::Autolock l(mLock);
4287
4288 if (mCameraServiceWatchdog != NULL) {
4289 mCameraServiceWatchdog->setEnabled(enabled);
4290 }
4291
4292 return OK;
4293}
4294
Shuzhen Wang16610a62022-12-15 22:38:07 -08004295void Camera3Device::setStreamUseCaseOverrides(
4296 const std::vector<int64_t>& useCaseOverrides) {
4297 Mutex::Autolock il(mInterfaceLock);
4298 Mutex::Autolock l(mLock);
4299 mStreamUseCaseOverrides = useCaseOverrides;
4300}
4301
4302void Camera3Device::clearStreamUseCaseOverrides() {
4303 Mutex::Autolock il(mInterfaceLock);
4304 Mutex::Autolock l(mLock);
4305 mStreamUseCaseOverrides.clear();
4306}
4307
Shuzhen Wang03fe6232023-02-05 12:41:15 -08004308bool Camera3Device::hasDeviceError() {
4309 Mutex::Autolock il(mInterfaceLock);
4310 Mutex::Autolock l(mLock);
4311 return mStatus == STATUS_ERROR;
4312}
4313
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004314void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4315 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004316 return;
4317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004318
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004319 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004320 // Skip the ones that have been submitted successfully.
4321 if (nextRequest.submitted) {
4322 continue;
4323 }
4324
4325 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004326 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4327 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004328
4329 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004330 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004331 }
4332
Emilian Peevaebbe412018-01-15 13:53:24 +00004333 cleanupPhysicalSettings(captureRequest, halRequest);
4334
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004335 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004336 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004337 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4338 }
4339
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004340 // No output buffer can be returned when using HAL buffer manager
4341 if (!mUseHalBufManager) {
4342 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4343 //Buffers that failed processing could still have
4344 //valid acquire fence.
4345 int acquireFence = (*outputBuffers)[i].acquire_fence;
4346 if (0 <= acquireFence) {
4347 close(acquireFence);
4348 outputBuffers->editItemAt(i).acquire_fence = -1;
4349 }
Emilian Peevf4816702020-04-03 15:44:51 -07004350 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004351 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4352 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004353 /*timestampIncreasing*/true, std::vector<size_t> (),
4354 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004355 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004356 }
4357
4358 if (sendRequestError) {
4359 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004360 sp<NotificationListener> listener = mListener.promote();
4361 if (listener != NULL) {
4362 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004363 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004364 captureRequest->mResultExtras);
4365 }
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09004366 {
4367 Mutex::Autolock al(mLatestRequestMutex);
4368
4369 mLatestFailedRequestId = captureRequest->mResultExtras.requestId;
4370 mLatestRequestSignal.signal();
4371 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004372 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004373
4374 // Remove yet-to-be submitted inflight request from inflightMap
4375 {
4376 sp<Camera3Device> parent = mParent.promote();
4377 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004378 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004379 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4380 if (idx >= 0) {
4381 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4382 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4383 parent->removeInFlightMapEntryLocked(idx);
4384 }
4385 }
4386 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004387 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004388
4389 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004390 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004391}
4392
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004393void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004394 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004395 // Optimized a bit for the simple steady-state case (single repeating
4396 // request), to avoid putting that request in the queue temporarily.
4397 Mutex::Autolock l(mRequestLock);
4398
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004399 assert(mNextRequests.empty());
4400
4401 NextRequest nextRequest;
4402 nextRequest.captureRequest = waitForNextRequestLocked();
4403 if (nextRequest.captureRequest == nullptr) {
4404 return;
4405 }
4406
Emilian Peevf4816702020-04-03 15:44:51 -07004407 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004408 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004409 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004410
4411 // Wait for additional requests
4412 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4413
4414 for (size_t i = 1; i < batchSize; i++) {
4415 NextRequest additionalRequest;
4416 additionalRequest.captureRequest = waitForNextRequestLocked();
4417 if (additionalRequest.captureRequest == nullptr) {
4418 break;
4419 }
4420
Emilian Peevf4816702020-04-03 15:44:51 -07004421 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004422 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004423 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004424 }
4425
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004426 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004427 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004428 mNextRequests.size(), batchSize);
4429 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004430 }
4431
4432 return;
4433}
4434
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004435void Camera3Device::RequestThread::setRequestClearing() {
4436 Mutex::Autolock l(mRequestLock);
4437 mRequestClearing = true;
4438}
4439
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004440sp<Camera3Device::CaptureRequest>
4441 Camera3Device::RequestThread::waitForNextRequestLocked() {
4442 status_t res;
4443 sp<CaptureRequest> nextRequest;
4444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004445 while (mRequestQueue.empty()) {
4446 if (!mRepeatingRequests.empty()) {
4447 // Always atomically enqueue all requests in a repeating request
4448 // list. Guarantees a complete in-sequence set of captures to
4449 // application.
4450 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004451 if (mFirstRepeating) {
4452 mFirstRepeating = false;
4453 } else {
4454 for (auto& request : requests) {
4455 // For repeating requests, override timestamp request using
4456 // the time a request is inserted into the request queue,
4457 // because the original repeating request will have an old
4458 // fixed timestamp.
4459 request->mRequestTimeNs = systemTime();
4460 }
4461 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004462 RequestList::const_iterator firstRequest =
4463 requests.begin();
4464 nextRequest = *firstRequest;
4465 mRequestQueue.insert(mRequestQueue.end(),
4466 ++firstRequest,
4467 requests.end());
4468 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004469
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004470 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004471
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004472 break;
4473 }
4474
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004475 if (!mRequestClearing) {
4476 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4477 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004478
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004479 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4480 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004481 Mutex::Autolock pl(mPauseLock);
4482 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004483 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004484 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004485 if (mNotifyPipelineDrain) {
4486 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4487 mNotifyPipelineDrain = false;
4488 mStreamIdsToBeDrained.clear();
4489 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004490 // Let the tracker know
4491 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4492 if (statusTracker != 0) {
4493 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4494 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004495 sp<Camera3Device> parent = mParent.promote();
4496 if (parent != nullptr) {
4497 parent->mRequestBufferSM.onRequestThreadPaused();
4498 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004499 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004500 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004501 // Stop waiting for now and let thread management happen
4502 return NULL;
4503 }
4504 }
4505
4506 if (nextRequest == NULL) {
4507 // Don't have a repeating request already in hand, so queue
4508 // must have an entry now.
4509 RequestList::iterator firstRequest =
4510 mRequestQueue.begin();
4511 nextRequest = *firstRequest;
4512 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004513 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4514 sp<NotificationListener> listener = mListener.promote();
4515 if (listener != NULL) {
4516 listener->notifyRequestQueueEmpty();
4517 }
4518 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004519 }
4520
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004521 // In case we've been unpaused by setPaused clearing mDoPause, need to
4522 // update internal pause state (capture/setRepeatingRequest unpause
4523 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004524 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004525 if (mPaused) {
4526 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4527 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4528 if (statusTracker != 0) {
4529 statusTracker->markComponentActive(mStatusId);
4530 }
4531 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004532 mPaused = false;
4533
4534 // Check if we've reconfigured since last time, and reset the preview
4535 // request if so. Can't use 'NULL request == repeat' across configure calls.
4536 if (mReconfigured) {
4537 mPrevRequest.clear();
4538 mReconfigured = false;
4539 }
4540
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004541 if (nextRequest != NULL) {
4542 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004543 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4544 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004545
4546 // Since RequestThread::clear() removes buffers from the input stream,
4547 // get the right buffer here before unlocking mRequestLock
4548 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004549 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4550 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004551 if (res != OK) {
4552 // Can't get input buffer from gralloc queue - this could be due to
4553 // disconnected queue or other producer misbehavior, so not a fatal
4554 // error
4555 ALOGE("%s: Can't get input buffer, skipping request:"
4556 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004557
4558 sp<NotificationListener> listener = mListener.promote();
4559 if (listener != NULL) {
4560 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004561 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004562 nextRequest->mResultExtras);
4563 }
4564 return NULL;
4565 }
4566 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004567 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004569 return nextRequest;
4570}
4571
4572bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004573 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004574 status_t res;
4575 Mutex::Autolock l(mPauseLock);
4576 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004577 if (mPaused == false) {
4578 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004579 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004580 if (mNotifyPipelineDrain) {
4581 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4582 mNotifyPipelineDrain = false;
4583 mStreamIdsToBeDrained.clear();
4584 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004585 // Let the tracker know
4586 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4587 if (statusTracker != 0) {
4588 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4589 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004590 sp<Camera3Device> parent = mParent.promote();
4591 if (parent != nullptr) {
4592 parent->mRequestBufferSM.onRequestThreadPaused();
4593 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004594 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004595
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004596 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004597 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004598 return true;
4599 }
4600 }
4601 // We don't set mPaused to false here, because waitForNextRequest needs
4602 // to further manage the paused state in case of starvation.
4603 return false;
4604}
4605
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004606void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004607 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004608 // With work to do, mark thread as unpaused.
4609 // If paused by request (setPaused), don't resume, to avoid
4610 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004611 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004612 Mutex::Autolock p(mPauseLock);
4613 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004614 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4615 if (mPaused) {
4616 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4617 if (statusTracker != 0) {
4618 statusTracker->markComponentActive(mStatusId);
4619 }
4620 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004621 mPaused = false;
4622 }
4623}
4624
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004625void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4626 sp<Camera3Device> parent = mParent.promote();
4627 if (parent != NULL) {
4628 va_list args;
4629 va_start(args, fmt);
4630
4631 parent->setErrorStateV(fmt, args);
4632
4633 va_end(args);
4634 }
4635}
4636
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004637status_t Camera3Device::RequestThread::insertTriggers(
4638 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004639 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004640 Mutex::Autolock al(mTriggerMutex);
4641
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004642 sp<Camera3Device> parent = mParent.promote();
4643 if (parent == NULL) {
4644 CLOGE("RequestThread: Parent is gone");
4645 return DEAD_OBJECT;
4646 }
4647
Emilian Peevaebbe412018-01-15 13:53:24 +00004648 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004649 size_t count = mTriggerMap.size();
4650
4651 for (size_t i = 0; i < count; ++i) {
4652 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004653 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004654
4655 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4656 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4657 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004658 if (isAeTrigger) {
4659 request->mResultExtras.precaptureTriggerId = triggerId;
4660 mCurrentPreCaptureTriggerId = triggerId;
4661 } else {
4662 request->mResultExtras.afTriggerId = triggerId;
4663 mCurrentAfTriggerId = triggerId;
4664 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004665 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004666 }
4667
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004668 camera_metadata_entry entry = metadata.find(tag);
4669
4670 if (entry.count > 0) {
4671 /**
4672 * Already has an entry for this trigger in the request.
4673 * Rewrite it with our requested trigger value.
4674 */
4675 RequestTrigger oldTrigger = trigger;
4676
4677 oldTrigger.entryValue = entry.data.u8[0];
4678
4679 mTriggerReplacedMap.add(tag, oldTrigger);
4680 } else {
4681 /**
4682 * More typical, no trigger entry, so we just add it
4683 */
4684 mTriggerRemovedMap.add(tag, trigger);
4685 }
4686
4687 status_t res;
4688
4689 switch (trigger.getTagType()) {
4690 case TYPE_BYTE: {
4691 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4692 res = metadata.update(tag,
4693 &entryValue,
4694 /*count*/1);
4695 break;
4696 }
4697 case TYPE_INT32:
4698 res = metadata.update(tag,
4699 &trigger.entryValue,
4700 /*count*/1);
4701 break;
4702 default:
4703 ALOGE("%s: Type not supported: 0x%x",
4704 __FUNCTION__,
4705 trigger.getTagType());
4706 return INVALID_OPERATION;
4707 }
4708
4709 if (res != OK) {
4710 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4711 ", value %d", __FUNCTION__, trigger.getTagName(),
4712 trigger.entryValue);
4713 return res;
4714 }
4715
4716 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4717 trigger.getTagName(),
4718 trigger.entryValue);
4719 }
4720
4721 mTriggerMap.clear();
4722
4723 return count;
4724}
4725
4726status_t Camera3Device::RequestThread::removeTriggers(
4727 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004728 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004729 Mutex::Autolock al(mTriggerMutex);
4730
Emilian Peevaebbe412018-01-15 13:53:24 +00004731 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004732
4733 /**
4734 * Replace all old entries with their old values.
4735 */
4736 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4737 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4738
4739 status_t res;
4740
4741 uint32_t tag = trigger.metadataTag;
4742 switch (trigger.getTagType()) {
4743 case TYPE_BYTE: {
4744 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4745 res = metadata.update(tag,
4746 &entryValue,
4747 /*count*/1);
4748 break;
4749 }
4750 case TYPE_INT32:
4751 res = metadata.update(tag,
4752 &trigger.entryValue,
4753 /*count*/1);
4754 break;
4755 default:
4756 ALOGE("%s: Type not supported: 0x%x",
4757 __FUNCTION__,
4758 trigger.getTagType());
4759 return INVALID_OPERATION;
4760 }
4761
4762 if (res != OK) {
4763 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4764 ", trigger value %d", __FUNCTION__,
4765 trigger.getTagName(), trigger.entryValue);
4766 return res;
4767 }
4768 }
4769 mTriggerReplacedMap.clear();
4770
4771 /**
4772 * Remove all new entries.
4773 */
4774 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4775 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4776 status_t res = metadata.erase(trigger.metadataTag);
4777
4778 if (res != OK) {
4779 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4780 ", trigger value %d", __FUNCTION__,
4781 trigger.getTagName(), trigger.entryValue);
4782 return res;
4783 }
4784 }
4785 mTriggerRemovedMap.clear();
4786
4787 return OK;
4788}
4789
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004790status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004791 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004792 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004793 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004794 status_t res;
4795
Emilian Peevaebbe412018-01-15 13:53:24 +00004796 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004797
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004798 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004799 // exists
4800 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4801 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4802 if (afTrigger.count > 0 &&
4803 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4804 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004805 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004806 if (res != OK) return res;
4807 }
4808
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004809 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004810 // if none already exists
4811 camera_metadata_entry pcTrigger =
4812 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4813 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4814 if (pcTrigger.count > 0 &&
4815 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4816 pcId.count == 0) {
4817 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004818 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004819 if (res != OK) return res;
4820 }
4821
4822 return OK;
4823}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004824
Emilian Peevd865f0d2023-03-17 17:13:07 -07004825bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4826 ATRACE_CALL();
4827 Mutex::Autolock l(mTriggerMutex);
4828 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4829 this->mRotateAndCropOverride);
4830}
4831
4832bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4833 bool overrideToPortrait,
4834 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004835 ATRACE_CALL();
4836
Emilian Peevd865f0d2023-03-17 17:13:07 -07004837 if (overrideToPortrait) {
4838 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
Austin Borger18b30a72022-10-27 12:20:29 -07004839 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4840 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4841 &rotateAndCrop_u8, 1);
4842 return true;
4843 }
4844
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004845 if (request->mRotateAndCropAuto) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004846 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4847
4848 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4849 if (rotateAndCropEntry.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004850 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004851 return false;
4852 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004853 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004854 return true;
4855 }
4856 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004857 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4858 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004859 return true;
4860 }
4861 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004862
4863 return false;
4864}
4865
4866bool Camera3Device::overrideAutoframing(const sp<CaptureRequest> &request /*out*/,
4867 camera_metadata_enum_android_control_autoframing_t autoframingOverride) {
4868 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4869 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4870 if (autoframingEntry.count > 0) {
4871 if (autoframingEntry.data.u8[0] == autoframingOverride) {
4872 return false;
4873 } else {
4874 autoframingEntry.data.u8[0] = autoframingOverride;
4875 return true;
4876 }
4877 } else {
4878 uint8_t autoframing_u8 = autoframingOverride;
4879 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4880 &autoframing_u8, 1);
4881 return true;
4882 }
4883
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004884 return false;
4885}
4886
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004887bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4888 ATRACE_CALL();
4889
4890 if (request->mAutoframingAuto) {
4891 Mutex::Autolock l(mTriggerMutex);
Emilian Peevd865f0d2023-03-17 17:13:07 -07004892 return Camera3Device::overrideAutoframing(request, mAutoframingOverride);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004893 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004894
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004895 return false;
4896}
4897
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004898bool Camera3Device::RequestThread::overrideTestPattern(
4899 const sp<CaptureRequest> &request) {
4900 ATRACE_CALL();
4901
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004902 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004903
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004904 Mutex::Autolock l(mTriggerMutex);
4905
4906 bool changed = false;
4907
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004908 // For a multi-camera, the physical cameras support the same set of
4909 // test pattern modes as the logical camera.
4910 for (auto& settings : request->mSettingsList) {
4911 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004912
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004913 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4914 int32_t testPatternData[4] = {
4915 settings.mOriginalTestPatternData[0],
4916 settings.mOriginalTestPatternData[1],
4917 settings.mOriginalTestPatternData[2],
4918 settings.mOriginalTestPatternData[3]
4919 };
4920 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4921 testPatternMode = mCameraMute;
4922 testPatternData[0] = 0;
4923 testPatternData[1] = 0;
4924 testPatternData[2] = 0;
4925 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004926 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004927
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004928 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4929 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4930 if (testPatternEntry.count > 0) {
4931 if (testPatternEntry.data.i32[0] != testPatternMode) {
4932 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004933 changed = true;
4934 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004935 } else if (supportTestPatternModeKey) {
4936 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4937 &testPatternMode, 1);
4938 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004939 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004940
4941 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4942 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4943 if (testPatternColor.count >= 4) {
4944 for (size_t i = 0; i < 4; i++) {
4945 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4946 testPatternColor.data.i32[i] = testPatternData[i];
4947 changed = true;
4948 }
4949 }
4950 } else if (supportTestPatternDataKey) {
4951 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4952 testPatternData, 4);
4953 changed = true;
4954 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004955 }
4956
4957 return changed;
4958}
4959
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004960bool Camera3Device::RequestThread::overrideSettingsOverride(
4961 const sp<CaptureRequest> &request) {
4962 ATRACE_CALL();
4963
4964 if (!mSupportSettingsOverride) return false;
4965
4966 Mutex::Autolock l(mTriggerMutex);
4967
4968 // For a multi-camera, only override the logical camera's metadata.
4969 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4970 camera_metadata_entry entry = metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
4971 int32_t originalValue = request->mSettingsList.begin()->mOriginalSettingsOverride;
4972 if (mSettingsOverride != -1 &&
4973 (entry.count == 0 || entry.data.i32[0] != mSettingsOverride)) {
4974 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4975 &mSettingsOverride, 1);
4976 return true;
4977 } else if (mSettingsOverride == -1 &&
4978 (entry.count == 0 || entry.data.i32[0] != originalValue)) {
4979 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4980 &originalValue, 1);
4981 return true;
4982 }
4983
4984 return false;
4985}
4986
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004987status_t Camera3Device::RequestThread::setHalInterface(
4988 sp<HalInterface> newHalInterface) {
4989 if (newHalInterface.get() == nullptr) {
4990 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4991 return DEAD_OBJECT;
4992 }
4993
4994 mInterface = newHalInterface;
4995
4996 return OK;
4997}
4998
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004999/**
5000 * PreparerThread inner class methods
5001 */
5002
5003Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005004 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005005 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005006}
5007
5008Camera3Device::PreparerThread::~PreparerThread() {
5009 Thread::requestExitAndWait();
5010 if (mCurrentStream != nullptr) {
5011 mCurrentStream->cancelPrepare();
5012 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5013 mCurrentStream.clear();
5014 }
5015 clear();
5016}
5017
Ruben Brunkc78ac262015-08-13 17:58:46 -07005018status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005019 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005020 status_t res;
5021
5022 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005023 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005024
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005025 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005026 if (res == OK) {
5027 // No preparation needed, fire listener right off
5028 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005029 if (listener != NULL) {
5030 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005031 }
5032 return OK;
5033 } else if (res != NOT_ENOUGH_DATA) {
5034 return res;
5035 }
5036
5037 // Need to prepare, start up thread if necessary
5038 if (!mActive) {
5039 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5040 // isn't running
5041 Thread::requestExitAndWait();
5042 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5043 if (res != OK) {
5044 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005045 if (listener != NULL) {
5046 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005047 }
5048 return res;
5049 }
5050 mCancelNow = false;
5051 mActive = true;
5052 ALOGV("%s: Preparer stream started", __FUNCTION__);
5053 }
5054
5055 // queue up the work
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005056 mPendingStreams.push_back(
5057 std::tuple<int, sp<camera3::Camera3StreamInterface>>(maxCount, stream));
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005058 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5059
5060 return OK;
5061}
5062
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005063void Camera3Device::PreparerThread::pause() {
5064 ATRACE_CALL();
5065
5066 Mutex::Autolock l(mLock);
5067
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005068 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> pendingStreams;
5069 pendingStreams.insert(pendingStreams.begin(), mPendingStreams.begin(), mPendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005070 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5071 int currentMaxCount = mCurrentMaxCount;
5072 mPendingStreams.clear();
5073 mCancelNow = true;
5074 while (mActive) {
5075 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5076 if (res == TIMED_OUT) {
5077 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5078 return;
5079 } else if (res != OK) {
5080 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5081 return;
5082 }
5083 }
5084
5085 //Check whether the prepare thread was able to complete the current
5086 //stream. In case work is still pending emplace it along with the rest
5087 //of the streams in the pending list.
5088 if (currentStream != nullptr) {
5089 if (!mCurrentPrepareComplete) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005090 pendingStreams.push_back(std::tuple(currentMaxCount, currentStream));
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005091 }
5092 }
5093
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005094 mPendingStreams.insert(mPendingStreams.begin(), pendingStreams.begin(), pendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005095 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005096 std::get<1>(it)->cancelPrepare();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005097 }
5098}
5099
5100status_t Camera3Device::PreparerThread::resume() {
5101 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005102 ALOGV("%s: PreparerThread", __FUNCTION__);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005103 status_t res;
5104
5105 Mutex::Autolock l(mLock);
5106 sp<NotificationListener> listener = mListener.promote();
5107
5108 if (mActive) {
5109 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5110 return NO_INIT;
5111 }
5112
5113 auto it = mPendingStreams.begin();
5114 for (; it != mPendingStreams.end();) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005115 res = std::get<1>(*it)->startPrepare(std::get<0>(*it), true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005116 if (res == OK) {
5117 if (listener != NULL) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005118 listener->notifyPrepared(std::get<1>(*it)->getId());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005119 }
5120 it = mPendingStreams.erase(it);
5121 } else if (res != NOT_ENOUGH_DATA) {
5122 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5123 res, strerror(-res));
5124 it = mPendingStreams.erase(it);
5125 } else {
5126 it++;
5127 }
5128 }
5129
5130 if (mPendingStreams.empty()) {
5131 return OK;
5132 }
5133
5134 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5135 if (res != OK) {
5136 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5137 __FUNCTION__, res, strerror(-res));
5138 return res;
5139 }
5140 mCancelNow = false;
5141 mActive = true;
5142 ALOGV("%s: Preparer stream started", __FUNCTION__);
5143
5144 return OK;
5145}
5146
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005147status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005148 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005149 Mutex::Autolock l(mLock);
5150
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005151 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005152 std::get<1>(it)->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005153 }
5154 mPendingStreams.clear();
5155 mCancelNow = true;
5156
5157 return OK;
5158}
5159
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005160void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005161 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005162 Mutex::Autolock l(mLock);
5163 mListener = listener;
5164}
5165
5166bool Camera3Device::PreparerThread::threadLoop() {
5167 status_t res;
5168 {
5169 Mutex::Autolock l(mLock);
5170 if (mCurrentStream == nullptr) {
5171 // End thread if done with work
5172 if (mPendingStreams.empty()) {
5173 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5174 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5175 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5176 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005177 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005178 return false;
5179 }
5180
5181 // Get next stream to prepare
5182 auto it = mPendingStreams.begin();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005183 mCurrentMaxCount = std::get<0>(*it);
5184 mCurrentStream = std::get<1>(*it);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005185 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005186 mPendingStreams.erase(it);
5187 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5188 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5189 } else if (mCancelNow) {
5190 mCurrentStream->cancelPrepare();
5191 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5192 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5193 mCurrentStream.clear();
5194 mCancelNow = false;
5195 return true;
5196 }
5197 }
5198
5199 res = mCurrentStream->prepareNextBuffer();
5200 if (res == NOT_ENOUGH_DATA) return true;
5201 if (res != OK) {
5202 // Something bad happened; try to recover by cancelling prepare and
5203 // signalling listener anyway
5204 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5205 mCurrentStream->getId(), res, strerror(-res));
5206 mCurrentStream->cancelPrepare();
5207 }
5208
5209 // This stream has finished, notify listener
5210 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005211 sp<NotificationListener> listener = mListener.promote();
5212 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005213 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5214 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005215 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005216 }
5217
5218 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5219 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005220 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005221
5222 return true;
5223}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005224
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005225status_t Camera3Device::RequestBufferStateMachine::initialize(
5226 sp<camera3::StatusTracker> statusTracker) {
5227 if (statusTracker == nullptr) {
5228 ALOGE("%s: statusTracker is null", __FUNCTION__);
5229 return BAD_VALUE;
5230 }
5231
5232 std::lock_guard<std::mutex> lock(mLock);
5233 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005234 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005235 return OK;
5236}
5237
5238bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5239 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005240 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005241 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005242 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005243 return true;
5244 }
5245 return false;
5246}
5247
5248void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5249 std::lock_guard<std::mutex> lock(mLock);
5250 if (!mRequestBufferOngoing) {
5251 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5252 return;
5253 }
5254 mRequestBufferOngoing = false;
5255 if (mStatus == RB_STATUS_PENDING_STOP) {
5256 checkSwitchToStopLocked();
5257 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005258 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005259}
5260
5261void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5262 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005263 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005264 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005265 return;
5266}
5267
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005268void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005269 std::lock_guard<std::mutex> lock(mLock);
5270 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005271 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5272 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005273 mInflightMapEmpty = false;
5274 if (mStatus == RB_STATUS_STOPPED) {
5275 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005276 }
5277 return;
5278}
5279
5280void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5281 std::lock_guard<std::mutex> lock(mLock);
5282 mRequestThreadPaused = true;
5283 if (mStatus == RB_STATUS_PENDING_STOP) {
5284 checkSwitchToStopLocked();
5285 }
5286 return;
5287}
5288
5289void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5290 std::lock_guard<std::mutex> lock(mLock);
5291 mInflightMapEmpty = true;
5292 if (mStatus == RB_STATUS_PENDING_STOP) {
5293 checkSwitchToStopLocked();
5294 }
5295 return;
5296}
5297
5298void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5299 std::lock_guard<std::mutex> lock(mLock);
5300 if (!checkSwitchToStopLocked()) {
5301 mStatus = RB_STATUS_PENDING_STOP;
5302 }
5303 return;
5304}
5305
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005306bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5307 std::lock_guard<std::mutex> lock(mLock);
5308 if (mRequestBufferOngoing) {
5309 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5310 __FUNCTION__);
5311 return false;
5312 }
5313 mSwitchedToOffline = true;
5314 mInflightMapEmpty = true;
5315 mRequestThreadPaused = true;
5316 mStatus = RB_STATUS_STOPPED;
5317 return true;
5318}
5319
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005320void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5321 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5322 if (statusTracker != nullptr) {
5323 if (active) {
5324 statusTracker->markComponentActive(mRequestBufferStatusId);
5325 } else {
5326 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5327 }
5328 }
5329}
5330
5331bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5332 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5333 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005334 return true;
5335 }
5336 return false;
5337}
5338
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005339bool Camera3Device::startRequestBuffer() {
5340 return mRequestBufferSM.startRequestBuffer();
5341}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005342
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005343void Camera3Device::endRequestBuffer() {
5344 mRequestBufferSM.endRequestBuffer();
5345}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005346
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005347nsecs_t Camera3Device::getWaitDuration() {
5348 return kBaseGetBufferWait + getExpectedInFlightDuration();
5349}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005350
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005351void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5352 mInterface->getInflightBufferKeys(out);
5353}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005354
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005355void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5356 mInterface->getInflightRequestBufferKeys(out);
5357}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005358
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005359std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5360 std::vector<sp<Camera3StreamInterface>> ret;
5361 bool hasInputStream = mInputStream != nullptr;
5362 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5363 if (hasInputStream) {
5364 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005365 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005366 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5367 ret.push_back(mOutputStreams[i]);
5368 }
5369 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5370 ret.push_back(mDeletedStreams[i]);
5371 }
5372 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005373}
5374
Emilian Peevcc0b7952020-01-07 13:54:47 -08005375void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5376 ATRACE_CALL();
5377
5378 if (offlineStreamIds == nullptr) {
5379 return;
5380 }
5381
5382 Mutex::Autolock il(mInterfaceLock);
5383
5384 auto streamIds = mOutputStreams.getStreamIds();
5385 bool hasInputStream = mInputStream != nullptr;
5386 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5387 offlineStreamIds->push_back(mInputStream->getId());
5388 }
5389
5390 for (const auto & streamId : streamIds) {
5391 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5392 // Streams that use the camera buffer manager are currently not supported in
5393 // offline mode
5394 if (stream->getOfflineProcessingSupport() &&
5395 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5396 offlineStreamIds->push_back(streamId);
5397 }
5398 }
5399}
5400
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005401status_t Camera3Device::setRotateAndCropAutoBehavior(
5402 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5403 ATRACE_CALL();
5404 Mutex::Autolock il(mInterfaceLock);
5405 Mutex::Autolock l(mLock);
5406 if (mRequestThread == nullptr) {
5407 return INVALID_OPERATION;
5408 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005409 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5410 return BAD_VALUE;
5411 }
5412 mRotateAndCropOverride = rotateAndCropValue;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005413 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5414}
5415
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005416status_t Camera3Device::setAutoframingAutoBehavior(
5417 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5418 ATRACE_CALL();
5419 Mutex::Autolock il(mInterfaceLock);
5420 Mutex::Autolock l(mLock);
5421 if (mRequestThread == nullptr) {
5422 return INVALID_OPERATION;
5423 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005424 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
5425 return BAD_VALUE;
5426 }
5427 mAutoframingOverride = autoframingValue;
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005428 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5429}
5430
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005431bool Camera3Device::supportsCameraMute() {
5432 Mutex::Autolock il(mInterfaceLock);
5433 Mutex::Autolock l(mLock);
5434
5435 return mSupportCameraMute;
5436}
5437
5438status_t Camera3Device::setCameraMute(bool enabled) {
5439 ATRACE_CALL();
5440 Mutex::Autolock il(mInterfaceLock);
5441 Mutex::Autolock l(mLock);
5442
5443 if (mRequestThread == nullptr || !mSupportCameraMute) {
5444 return INVALID_OPERATION;
5445 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005446 int32_t muteMode =
5447 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5448 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5449 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5450 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005451}
5452
Shuzhen Wangaf22e912023-04-11 16:03:17 -07005453bool Camera3Device::supportsZoomOverride() {
5454 Mutex::Autolock il(mInterfaceLock);
5455 Mutex::Autolock l(mLock);
5456
5457 return mSupportZoomOverride;
5458}
5459
5460status_t Camera3Device::setZoomOverride(int32_t zoomOverride) {
5461 ATRACE_CALL();
5462 Mutex::Autolock il(mInterfaceLock);
5463 Mutex::Autolock l(mLock);
5464
5465 if (mRequestThread == nullptr || !mSupportZoomOverride) {
5466 return INVALID_OPERATION;
5467 }
5468
5469 return mRequestThread->setZoomOverride(zoomOverride);
5470}
5471
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005472status_t Camera3Device::injectCamera(const String8& injectedCamId,
5473 sp<CameraProviderManager> manager) {
5474 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5475 ATRACE_CALL();
5476 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005477 // When the camera device is active, injectCamera() and stopInjection() will call
5478 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5479 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5480 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5481 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5482 // waitUntilStateThenRelock().
5483 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005484
5485 status_t res = NO_ERROR;
5486 if (mInjectionMethods->isInjecting()) {
5487 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5488 return OK;
5489 } else {
5490 res = mInjectionMethods->stopInjection();
5491 if (res != OK) {
5492 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5493 __FUNCTION__, res);
5494 return res;
5495 }
5496 }
5497 }
5498
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005499 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005500 if (res != OK) {
5501 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5502 __FUNCTION__, res);
5503 return res;
5504 }
5505
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005506 // When the second display of android is cast to the remote device, and the opened camera is
5507 // also cast to the second display, in this case, because the camera has configured the streams
5508 // at this time, we can directly call injectCamera() to replace the internal camera with
5509 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005510 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5511 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5512
5513 camera3::camera_stream_configuration injectionConfig;
5514 std::vector<uint32_t> injectionBufferSizes;
5515 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5516 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5517 || injectionBufferSizes.size() <= 0) {
5518 ALOGE("Failed to inject camera due to abandoned configuration! "
5519 "mOperatingMode: %d injectionConfig.num_streams: %d "
5520 "injectionBufferSizes.size(): %zu", mOperatingMode,
5521 injectionConfig.num_streams, injectionBufferSizes.size());
5522 return DEAD_OBJECT;
5523 }
5524
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005525 res = mInjectionMethods->injectCamera(
5526 injectionConfig, injectionBufferSizes);
5527 if (res != OK) {
5528 ALOGE("Can't finish inject camera process!");
5529 return res;
5530 }
5531 }
5532
5533 return OK;
5534}
5535
5536status_t Camera3Device::stopInjection() {
5537 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5538 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005539 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005540 return mInjectionMethods->stopInjection();
5541}
5542
Shuzhen Wang16610a62022-12-15 22:38:07 -08005543void Camera3Device::overrideStreamUseCaseLocked() {
5544 if (mStreamUseCaseOverrides.size() == 0) {
5545 return;
5546 }
5547
5548 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5549 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5550 // Refer to CameraService::printHelp for details.
5551 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5552 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5553 outputStreamsIndices[i] = i;
5554 }
5555
5556 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5557 [&](int a, int b) -> bool {
5558
5559 auto formatScore = [](int format) {
5560 switch (format) {
5561 case HAL_PIXEL_FORMAT_BLOB:
5562 return 4;
5563 case HAL_PIXEL_FORMAT_RAW16:
5564 case HAL_PIXEL_FORMAT_RAW10:
5565 case HAL_PIXEL_FORMAT_RAW12:
5566 return 3;
5567 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5568 return 2;
5569 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5570 return 1;
5571 default:
5572 return 0;
5573 }
5574 };
5575
5576 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5577 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5578 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5579 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5580 if (sizeA > sizeB ||
5581 (sizeA == sizeB && formatAScore >= formatBScore)) {
5582 return true;
5583 } else {
5584 return false;
5585 }
5586 });
5587
5588 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5589 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5590 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5591 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5592 }
5593}
5594
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005595}; // namespace android