blob: e55d724b9f8c5a390a54db75a1b244a15df77bb6 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070066#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080068#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080069#include <tuple>
70
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080072using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073
74namespace android {
75
Austin Borger3560b7e2022-10-27 12:20:29 -070076Camera3Device::Camera3Device(const String8 &id, bool overrideForPerfClass, bool overrideToPortrait,
77 bool legacyClient):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080078 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070079 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080080 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070081 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070082 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070083 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070084 mUsePartialResult(false),
85 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080086 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080087 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070088 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070089 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070090 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070092 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000094 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010095 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070096 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070097 mNeedFixupMonochromeTags(false),
Austin Borger3560b7e2022-10-27 12:20:29 -070098 mOverrideForPerfClass(overrideForPerfClass),
Austin Borgerc8099762023-01-12 17:08:46 -080099 mOverrideToPortrait(overrideToPortrait),
Emilian Peevb9f83812023-03-17 17:13:07 -0700100 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
101 mComposerOutput(false),
Austin Borgerc8099762023-01-12 17:08:46 -0800102 mActivePhysicalId("")
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103{
104 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800105 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106}
107
108Camera3Device::~Camera3Device()
109{
110 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800111 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700112 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113}
114
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800116 return mId;
117}
118
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800119status_t Camera3Device::initializeCommonLocked() {
120
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700121 /** Start up status tracker thread */
122 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700124 if (res != OK) {
125 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
126 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800127 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700128 mStatusTracker.clear();
129 return res;
130 }
131
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700132 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700133 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700134
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700135 if (mUseHalBufManager) {
136 res = mRequestBufferSM.initialize(mStatusTracker);
137 if (res != OK) {
138 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
139 strerror(-res), res);
140 mInterface->close();
141 mStatusTracker.clear();
142 return res;
143 }
144 }
145
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800146 /** Create buffer manager */
147 mBufferManager = new Camera3BufferManager();
148
149 Vector<int32_t> sessionParamKeys;
150 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
151 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
152 if (sessionKeysEntry.count > 0) {
153 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
154 }
155
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700156 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
157 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
158 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
159 if (availableTestPatternModes.data.i32[i] ==
160 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
161 mSupportCameraMute = true;
162 mSupportTestPatternSolidColor = true;
163 break;
164 } else if (availableTestPatternModes.data.i32[i] ==
165 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
166 mSupportCameraMute = true;
167 mSupportTestPatternSolidColor = false;
168 }
169 }
170
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700171 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800172 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700173 this, mStatusTracker, mInterface, sessionParamKeys,
Austin Borger3560b7e2022-10-27 12:20:29 -0700174 mUseHalBufManager, mSupportCameraMute, mOverrideToPortrait);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800175 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700177 SET_ERR_L("Unable to start request queue thread: %s (%d)",
178 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800179 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800180 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 return res;
182 }
183
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700184 mPreparerThread = new PreparerThread();
185
Ruben Brunk183f0562015-08-12 12:55:02 -0700186 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400188 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700189 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800191 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800193 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800194 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800195 camera_metadata_entry timestampSource =
196 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
197 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
198 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800199 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800200 }
201
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700202 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100203 camera_metadata_entry partialResultsCount =
204 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
205 if (partialResultsCount.count > 0) {
206 mNumPartialResults = partialResultsCount.data.i32[0];
207 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700208 }
209
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800210 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
211 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700212 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700213 if (res != OK) {
214 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
215 return res;
216 }
217 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800218
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800219 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800220 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800221
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700222 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
223 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
224 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
225 }
226
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800227 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
228 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
229 }
230
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800231 // Hidl/AidlCamera3DeviceInjectionMethods
232 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800233
Ravneet74cd3732022-03-30 05:33:03 +0000234 /** Start watchdog thread */
235 mCameraServiceWatchdog = new CameraServiceWatchdog();
236 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
237 if (res != OK) {
238 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
239 strerror(-res), res);
240 return res;
241 }
242
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800243 return OK;
244}
245
246status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700247 return disconnectImpl();
248}
249
250status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800251 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700252 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800253
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700254 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700255 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800257 Mutex::Autolock il(mInterfaceLock);
258 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
259 {
260 Mutex::Autolock l(mLock);
261 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 if (mStatus == STATUS_ACTIVE ||
264 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
265 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700266 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800267 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800269 } else {
270 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
271 if (res != OK) {
272 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
273 maxExpectedDuration);
274 // Continue to close device even in case of error
275 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700277 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800278
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800279 if (mStatus == STATUS_ERROR) {
280 CLOGE("Shutting down in an error state");
281 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800283 if (mStatusTracker != NULL) {
284 mStatusTracker->requestExit();
285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800287 if (mRequestThread != NULL) {
288 mRequestThread->requestExit();
289 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800291 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
292 for (size_t i = 0; i < mOutputStreams.size(); i++) {
293 streams.push_back(mOutputStreams[i]);
294 }
295 if (mInputStream != nullptr) {
296 streams.push_back(mInputStream);
297 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700298 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800300 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
301 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700302 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
303 // HAL may be in a bad state, so waiting for request thread
304 // (which may be stuck in the HAL processCaptureRequest call)
305 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800306 // give up mInterfaceLock here and then lock it again. Could this lead
307 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mRequestThread->join();
309 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800311 Mutex::Autolock il(mInterfaceLock);
312 if (mStatusTracker != NULL) {
313 mStatusTracker->join();
314 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800316 if (mInjectionMethods->isInjecting()) {
317 mInjectionMethods->stopInjection();
318 }
319
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800320 HalInterface* interface;
321 {
322 Mutex::Autolock l(mLock);
323 mRequestThread.clear();
324 Mutex::Autolock stLock(mTrackerLock);
325 mStatusTracker.clear();
326 interface = mInterface.get();
327 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700328
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800329 // Call close without internal mutex held, as the HAL close may need to
330 // wait on assorted callbacks,etc, to complete before it can return.
Ravneet98ffa752022-03-02 07:22:46 +0000331 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700332
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800333 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800334
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800335 {
336 Mutex::Autolock l(mLock);
337 mInterface->clear();
338 mOutputStreams.clear();
339 mInputStream.clear();
340 mDeletedStreams.clear();
341 mBufferManager.clear();
342 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
343 }
344
345 for (auto& weakStream : streams) {
346 sp<Camera3StreamInterface> stream = weakStream.promote();
347 if (stream != nullptr) {
348 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
349 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
350 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700351 }
352 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700353 ALOGI("%s: X", __FUNCTION__);
Ravneet98ffa752022-03-02 07:22:46 +0000354
355 if (mCameraServiceWatchdog != NULL) {
356 mCameraServiceWatchdog->requestExit();
357 mCameraServiceWatchdog.clear();
358 }
359
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700360 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800361}
362
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700363// For dumping/debugging only -
364// try to acquire a lock a few times, eventually give up to proceed with
365// debug/dump operations
366bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
367 bool gotLock = false;
368 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
369 if (lock.tryLock() == NO_ERROR) {
370 gotLock = true;
371 break;
372 } else {
373 usleep(kDumpSleepDuration);
374 }
375 }
376 return gotLock;
377}
378
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800379nsecs_t Camera3Device::getMonoToBoottimeOffset() {
380 // try three times to get the clock offset, choose the one
381 // with the minimum gap in measurements.
382 const int tries = 3;
383 nsecs_t bestGap, measured;
384 for (int i = 0; i < tries; ++i) {
385 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
386 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
387 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
388 const nsecs_t gap = tmono2 - tmono;
389 if (i == 0 || gap < bestGap) {
390 bestGap = gap;
391 measured = tbase - ((tmono + tmono2) >> 1);
392 }
393 }
394 return measured;
395}
396
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800397ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
398 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700399 // Get max jpeg size (area-wise) for default sensor pixel mode
400 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800401 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700402 /*isUltraHighResolutionSensor*/false);
403 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
404 // not ultra high res sensor
405 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800406 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700407 /*isUltraHighResolution*/true);
408 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800409 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
410 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700411 return BAD_VALUE;
412 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700413 bool useMaxSensorPixelModeThreshold = false;
414 if (uhrMaxJpegResolution.width != 0 &&
415 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
416 // Use the ultra high res max jpeg size and max jpeg buffer size
417 useMaxSensorPixelModeThreshold = true;
418 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700419
Zhijun Hef7da0962014-04-24 13:27:56 -0700420 // Get max jpeg buffer size
421 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800422 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700423 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800424 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
425 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700426 return BAD_VALUE;
427 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700428 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700429
430 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
431 if (useMaxSensorPixelModeThreshold) {
432 maxJpegBufferSize =
433 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
434 maxDefaultJpegResolution, maxJpegBufferSize);
435 chosenMaxJpegResolution = uhrMaxJpegResolution;
436 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800437 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700438
439 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700440 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700441 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800442 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
443 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800445 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
446 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700447 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700448 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700449 return jpegBufferSize;
450}
451
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800452ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700453 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800454 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700455 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800456 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
457 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700458 return BAD_VALUE;
459 }
460 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
461 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
462 return maxBytesForPointCloud;
463}
464
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800465ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
466 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800467 const int PER_CONFIGURATION_SIZE = 3;
468 const int WIDTH_OFFSET = 0;
469 const int HEIGHT_OFFSET = 1;
470 const int SIZE_OFFSET = 2;
471 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800472 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800473 camera3::SessionConfigurationUtils::getAppropriateModeTag(
474 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
475 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800476 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800477 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800478 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
479 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800480 return BAD_VALUE;
481 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800483 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
484 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
485 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
486 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
487 }
488 }
489
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800490 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
491 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800492 return BAD_VALUE;
493}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700494
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800495status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
496 ATRACE_CALL();
497 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700498
499 // Try to lock, but continue in case of failure (to avoid blocking in
500 // deadlocks)
501 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
502 bool gotLock = tryLockSpinRightRound(mLock);
503
504 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800505 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
506 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700507 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800508 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
509 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700510
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800511 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700512
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800513 String16 templatesOption("-t");
514 int n = args.size();
515 for (int i = 0; i < n; i++) {
516 if (args[i] == templatesOption) {
517 dumpTemplates = true;
518 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000519 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700520 if (i + 1 < n) {
521 String8 monitorTags = String8(args[i + 1]);
522 if (monitorTags == "off") {
523 mTagMonitor.disableMonitoring();
524 } else {
525 mTagMonitor.parseTagsToMonitor(monitorTags);
526 }
527 } else {
528 mTagMonitor.disableMonitoring();
529 }
530 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800531 }
532
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800533 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800535 const char *status =
536 mStatus == STATUS_ERROR ? "ERROR" :
537 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
539 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 mStatus == STATUS_ACTIVE ? "ACTIVE" :
541 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 if (mStatus == STATUS_ERROR) {
545 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
546 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800547 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800548 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000549 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
550 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
551 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800552 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800553
554 if (mInputStream != NULL) {
555 write(fd, lines.string(), lines.size());
556 mInputStream->dump(fd, args);
557 } else {
558 lines.appendFormat(" No input stream.\n");
559 write(fd, lines.string(), lines.size());
560 }
561 for (size_t i = 0; i < mOutputStreams.size(); i++) {
562 mOutputStreams[i]->dump(fd,args);
563 }
564
Zhijun He431503c2016-03-07 17:30:16 -0800565 if (mBufferManager != NULL) {
566 lines = String8(" Camera3 Buffer Manager:\n");
567 write(fd, lines.string(), lines.size());
568 mBufferManager->dump(fd, args);
569 }
Zhijun He125684a2015-12-26 15:07:30 -0800570
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700571 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700572 if (mInFlightLock.try_lock()) {
573 if (mInFlightMap.size() == 0) {
574 lines.append(" None\n");
575 } else {
576 for (size_t i = 0; i < mInFlightMap.size(); i++) {
577 InFlightRequest r = mInFlightMap.valueAt(i);
578 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
579 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
580 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
581 r.numBuffersLeft);
582 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700584 mInFlightLock.unlock();
585 } else {
586 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700587 }
588 write(fd, lines.string(), lines.size());
589
Shuzhen Wang686f6442017-06-20 16:16:04 -0700590 if (mRequestThread != NULL) {
591 mRequestThread->dumpCaptureRequestLatency(fd,
592 " ProcessCaptureRequest latency histogram:");
593 }
594
Igor Murashkin1e479c02013-09-06 16:55:14 -0700595 {
596 lines = String8(" Last request sent:\n");
597 write(fd, lines.string(), lines.size());
598
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700599 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700600 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
601 }
602
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800603 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700604 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800605 "TEMPLATE_PREVIEW",
606 "TEMPLATE_STILL_CAPTURE",
607 "TEMPLATE_VIDEO_RECORD",
608 "TEMPLATE_VIDEO_SNAPSHOT",
609 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800610 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800611 };
612
Emilian Peevf4816702020-04-03 15:44:51 -0700613 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800614 camera_metadata_t *templateRequest = nullptr;
615 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700616 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800617 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800618 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800619 lines.append(" Not supported\n");
620 write(fd, lines.string(), lines.size());
621 } else {
622 write(fd, lines.string(), lines.size());
623 dump_indented_camera_metadata(templateRequest,
624 fd, /*verbosity*/2, /*indentation*/8);
625 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800626 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800627 }
628 }
629
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700630 mTagMonitor.dumpMonitoredMetadata(fd);
631
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800632 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800633 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800635 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800636 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 if (gotLock) mLock.unlock();
639 if (gotInterfaceLock) mInterfaceLock.unlock();
640
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800641 return OK;
642}
643
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700644status_t Camera3Device::startWatchingTags(const String8 &tags) {
645 mTagMonitor.parseTagsToMonitor(tags);
646 return OK;
647}
648
649status_t Camera3Device::stopWatchingTags() {
650 mTagMonitor.disableMonitoring();
651 return OK;
652}
653
654status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
655 mTagMonitor.getLatestMonitoredTagEvents(out);
656 return OK;
657}
658
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800659const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800661 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
662 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700663 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 mStatus == STATUS_ERROR ?
665 "when in error state" : "before init");
666 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700667 if (physicalId.isEmpty()) {
668 return mDeviceInfo;
669 } else {
670 std::string id(physicalId.c_str());
671 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
672 return mPhysicalDeviceInfoMap.at(id);
673 } else {
674 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
675 return mDeviceInfo;
676 }
677 }
678}
679
680const CameraMetadata& Camera3Device::info() const {
681 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800682 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683}
684
Jianing Wei90e59c92014-03-12 18:29:36 -0700685status_t Camera3Device::checkStatusOkToCaptureLocked() {
686 switch (mStatus) {
687 case STATUS_ERROR:
688 CLOGE("Device has encountered a serious error");
689 return INVALID_OPERATION;
690 case STATUS_UNINITIALIZED:
691 CLOGE("Device not initialized");
692 return INVALID_OPERATION;
693 case STATUS_UNCONFIGURED:
694 case STATUS_CONFIGURED:
695 case STATUS_ACTIVE:
696 // OK
697 break;
698 default:
699 SET_ERR_L("Unexpected status: %d", mStatus);
700 return INVALID_OPERATION;
701 }
702 return OK;
703}
704
705status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000706 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700707 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700708 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700709 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700710 if (requestList == NULL) {
711 CLOGE("requestList cannot be NULL.");
712 return BAD_VALUE;
713 }
714
Jianing Weicb0652e2014-03-12 18:29:36 -0700715 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000716 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700717 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
718 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
719 ++metadataIt, ++surfaceMapIt) {
720 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700721 if (newRequest == 0) {
722 CLOGE("Can't create capture request");
723 return BAD_VALUE;
724 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700725
Shuzhen Wang9d066012016-09-30 11:30:20 -0700726 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700727 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700728
Jianing Weicb0652e2014-03-12 18:29:36 -0700729 // Setup burst Id and request Id
730 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800731 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
732 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700733 CLOGE("RequestID does not exist in metadata");
734 return BAD_VALUE;
735 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800736 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700737
Jianing Wei90e59c92014-03-12 18:29:36 -0700738 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700739
740 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700741 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700742 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
743 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
744 return BAD_VALUE;
745 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700746
747 // Setup batch size if this is a high speed video recording request.
748 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
749 auto firstRequest = requestList->begin();
750 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
751 if (outputStream->isVideoStream()) {
752 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800753 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700754 break;
755 }
756 }
757 }
758
Jianing Wei90e59c92014-03-12 18:29:36 -0700759 return OK;
760}
761
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800762status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800763 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800764
Emilian Peevaebbe412018-01-15 13:53:24 +0000765 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000767 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700768
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800769 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700770}
771
Emilian Peevaebbe412018-01-15 13:53:24 +0000772void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773 std::list<const SurfaceMap>& surfaceMaps,
774 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000775 PhysicalCameraSettingsList requestList;
776 requestList.push_back({std::string(getId().string()), request});
777 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700778
779 SurfaceMap surfaceMap;
780 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
781 // With no surface list passed in, stream and surface will have 1-to-1
782 // mapping. So the surface index is 0 for each stream in the surfaceMap.
783 for (size_t i = 0; i < streams.count; i++) {
784 surfaceMap[streams.data.i32[i]].push_back(0);
785 }
786 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800787}
788
Jianing Wei90e59c92014-03-12 18:29:36 -0700789status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000790 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791 const std::list<const SurfaceMap> &surfaceMaps,
792 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700793 /*out*/
794 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700795 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700796 nsecs_t requestTimeNs = systemTime();
797
Jianing Wei90e59c92014-03-12 18:29:36 -0700798 Mutex::Autolock il(mInterfaceLock);
799 Mutex::Autolock l(mLock);
800
801 status_t res = checkStatusOkToCaptureLocked();
802 if (res != OK) {
803 // error logged by previous call
804 return res;
805 }
806
807 RequestList requestList;
808
Shuzhen Wang0129d522016-10-30 22:43:41 -0700809 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700810 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700811 if (res != OK) {
812 // error logged by previous call
813 return res;
814 }
815
816 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700817 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700818 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700819 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700820 }
821
822 if (res == OK) {
823 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
824 if (res != OK) {
825 SET_ERR_L("Can't transition to active in %f seconds!",
826 kActiveTimeout/1e9);
827 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800828 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700829 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700830 } else {
831 CLOGE("Cannot queue request. Impossible.");
832 return BAD_VALUE;
833 }
834
835 return res;
836}
837
Emilian Peevaebbe412018-01-15 13:53:24 +0000838status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700839 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700840 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700841 ATRACE_CALL();
842
Emilian Peevaebbe412018-01-15 13:53:24 +0000843 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700844}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845
Jianing Weicb0652e2014-03-12 18:29:36 -0700846status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
847 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800849
Emilian Peevaebbe412018-01-15 13:53:24 +0000850 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700851 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000852 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700855 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856}
857
Emilian Peevaebbe412018-01-15 13:53:24 +0000858status_t Camera3Device::setStreamingRequestList(
859 const List<const PhysicalCameraSettingsList> &requestsList,
860 const std::list<const SurfaceMap> &surfaceMaps, 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*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700864}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865
866sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000867 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 status_t res;
869
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800871 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
872 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000873 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700874 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700875 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700877 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700879 } else if (mStatus == STATUS_UNCONFIGURED) {
880 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700881 CLOGE("No streams configured");
882 return NULL;
883 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 }
885
Shuzhen Wang0129d522016-10-30 22:43:41 -0700886 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800887 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800888}
889
Jianing Weicb0652e2014-03-12 18:29:36 -0700890status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800891 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700892 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800894
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 switch (mStatus) {
896 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700897 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 return INVALID_OPERATION;
899 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700900 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700902 case STATUS_UNCONFIGURED:
903 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 case STATUS_ACTIVE:
905 // OK
906 break;
907 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700908 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800909 return INVALID_OPERATION;
910 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800911 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700912
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700913 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800914}
915
916status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
917 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700918 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800919
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700920 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800921}
922
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700923status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800924 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700925 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700926 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700927 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700928 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800929 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
930 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700931
932 status_t res;
933 bool wasActive = false;
934
935 switch (mStatus) {
936 case STATUS_ERROR:
937 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
938 return INVALID_OPERATION;
939 case STATUS_UNINITIALIZED:
940 ALOGE("%s: Device not initialized", __FUNCTION__);
941 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700942 case STATUS_UNCONFIGURED:
943 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700944 // OK
945 break;
946 case STATUS_ACTIVE:
947 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700948 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700949 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700951 return res;
952 }
953 wasActive = true;
954 break;
955 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700957 return INVALID_OPERATION;
958 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700960
961 if (mInputStream != 0) {
962 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
963 return INVALID_OPERATION;
964 }
965
966 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
967 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700968 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700969
970 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800971 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700972
973 *id = mNextStreamId++;
974
975 // Continue captures if active at start
976 if (wasActive) {
977 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100978 // Reuse current operating mode and session parameters for new stream config
979 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700980 if (res != OK) {
981 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
982 __FUNCTION__, mNextStreamId, strerror(-res), res);
983 return res;
984 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700985 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700986 }
987
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800988 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700989 return OK;
990}
991
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700992status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700993 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700994 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800995 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800996 const std::unordered_set<int32_t> &sensorPixelModesUsed,
997 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800998 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800999 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001000 ATRACE_CALL();
1001
1002 if (consumer == nullptr) {
1003 ALOGE("%s: consumer must not be null", __FUNCTION__);
1004 return BAD_VALUE;
1005 }
1006
1007 std::vector<sp<Surface>> consumers;
1008 consumers.push_back(consumer);
1009
1010 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001011 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001012 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001013 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001014}
1015
1016static bool isRawFormat(int format) {
1017 switch (format) {
1018 case HAL_PIXEL_FORMAT_RAW16:
1019 case HAL_PIXEL_FORMAT_RAW12:
1020 case HAL_PIXEL_FORMAT_RAW10:
1021 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1022 return true;
1023 default:
1024 return false;
1025 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001026}
1027
1028status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1029 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001030 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001031 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001032 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001033 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1034 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001036
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001038 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001040 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001041 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001042 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1043 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001044 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001045 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001046 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001047
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 status_t res;
1049 bool wasActive = false;
1050
1051 switch (mStatus) {
1052 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001053 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return INVALID_OPERATION;
1055 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001058 case STATUS_UNCONFIGURED:
1059 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 // OK
1061 break;
1062 case STATUS_ACTIVE:
1063 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001064 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001066 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return res;
1068 }
1069 wasActive = true;
1070 break;
1071 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001072 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 return INVALID_OPERATION;
1074 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001075 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076
1077 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001078
Shuzhen Wang0129d522016-10-30 22:43:41 -07001079 if (consumers.size() == 0 && !hasDeferredConsumer) {
1080 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1081 return BAD_VALUE;
1082 }
Zhijun He5d677d12016-05-29 16:52:39 -07001083
Shuzhen Wang0129d522016-10-30 22:43:41 -07001084 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001085 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1086 return BAD_VALUE;
1087 }
1088
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001089 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1090 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1091 // be found in only one sensor pixel mode.
1092 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1093 return BAD_VALUE;
1094 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001095 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001097 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001098 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001099 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001100 if (blobBufferSize <= 0) {
1101 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1102 return BAD_VALUE;
1103 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001104 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1105 blobBufferSize = width * height;
1106 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001107 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001108 if (blobBufferSize <= 0) {
1109 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1110 return BAD_VALUE;
1111 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001112 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001113 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001114 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001115 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001116 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001117 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001118 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001119 bool maxResolution =
1120 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1121 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001122 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1123 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001124 if (rawOpaqueBufferSize <= 0) {
1125 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1126 return BAD_VALUE;
1127 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001128 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001129 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001130 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001131 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001132 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001133 } else if (isShared) {
1134 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1135 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001136 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001137 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001138 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001139 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001140 newStream = new Camera3OutputStream(mNextStreamId,
1141 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001142 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001143 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001144 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001145 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001146 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001147 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001148 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001149 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001150 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 }
Emilian Peev40ead602017-09-26 15:46:36 +01001152
1153 size_t consumerCount = consumers.size();
1154 for (size_t i = 0; i < consumerCount; i++) {
1155 int id = newStream->getSurfaceId(consumers[i]);
1156 if (id < 0) {
1157 SET_ERR_L("Invalid surface id");
1158 return BAD_VALUE;
1159 }
1160 if (surfaceIds != nullptr) {
1161 surfaceIds->push_back(id);
1162 }
1163 }
1164
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001165 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166
Emilian Peev08dd2452017-04-06 16:55:14 +01001167 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001168
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001169 newStream->setImageDumpMask(mImageDumpMask);
1170
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 res = mOutputStreams.add(mNextStreamId, newStream);
1172 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001173 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 return res;
1175 }
1176
Shuzhen Wang316781a2020-08-18 18:11:01 -07001177 mSessionStatsBuilder.addStream(mNextStreamId);
1178
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001180 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181
1182 // Continue captures if active at start
1183 if (wasActive) {
1184 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001185 // Reuse current operating mode and session parameters for new stream config
1186 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001188 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1189 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190 return res;
1191 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001192 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001194 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196}
1197
Emilian Peev710c1422017-08-30 11:19:38 +01001198status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001200 if (nullptr == streamInfo) {
1201 return BAD_VALUE;
1202 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001203 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001206 switch (mStatus) {
1207 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001208 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001209 return INVALID_OPERATION;
1210 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001211 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213 case STATUS_UNCONFIGURED:
1214 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 case STATUS_ACTIVE:
1216 // OK
1217 break;
1218 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001219 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001220 return INVALID_OPERATION;
1221 }
1222
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001223 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1224 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001225 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001226 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 }
1228
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001229 streamInfo->width = stream->getWidth();
1230 streamInfo->height = stream->getHeight();
1231 streamInfo->format = stream->getFormat();
1232 streamInfo->dataSpace = stream->getDataSpace();
1233 streamInfo->formatOverridden = stream->isFormatOverridden();
1234 streamInfo->originalFormat = stream->getOriginalFormat();
1235 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1236 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001237 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239}
1240
1241status_t Camera3Device::setStreamTransform(int id,
1242 int transform) {
1243 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001244 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 switch (mStatus) {
1248 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001249 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 return INVALID_OPERATION;
1251 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001252 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001254 case STATUS_UNCONFIGURED:
1255 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 case STATUS_ACTIVE:
1257 // OK
1258 break;
1259 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 return INVALID_OPERATION;
1262 }
1263
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001264 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1265 if (stream == nullptr) {
1266 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001267 return BAD_VALUE;
1268 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001269 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001270}
1271
1272status_t Camera3Device::deleteStream(int id) {
1273 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001274 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 Mutex::Autolock l(mLock);
1276 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001278 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001279
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 // CameraDevice semantics require device to already be idle before
1281 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001282 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001283 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001284 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001285 }
1286
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001287 if (mStatus == STATUS_ERROR) {
1288 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1289 __FUNCTION__, mId.string());
1290 return -EBUSY;
1291 }
1292
Igor Murashkin2fba5842013-04-22 14:03:54 -07001293 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001294 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 if (mInputStream != NULL && id == mInputStream->getId()) {
1296 deletedStream = mInputStream;
1297 mInputStream.clear();
1298 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001299 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001300 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301 return BAD_VALUE;
1302 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001303 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001304 }
1305
1306 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001307 if (stream != nullptr) {
1308 deletedStream = stream;
1309 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 }
1311
1312 // Free up the stream endpoint so that it can be used by some other stream
1313 res = deletedStream->disconnect();
1314 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001315 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316 // fall through since we want to still list the stream as deleted.
1317 }
1318 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001319 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320
1321 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001322}
1323
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001324status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001325 ATRACE_CALL();
1326 ALOGV("%s: E", __FUNCTION__);
1327
1328 Mutex::Autolock il(mInterfaceLock);
1329 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001330
Emilian Peev811d2952018-05-25 11:08:40 +01001331 // In case the client doesn't include any session parameter, try a
1332 // speculative configuration using the values from the last cached
1333 // default request.
1334 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001335 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001336 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1337 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1338 mLastTemplateId);
1339 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1340 operatingMode);
1341 }
1342
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001343 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1344}
1345
1346status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1347 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001348 //Filter out any incoming session parameters
1349 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001350 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1351 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001352 CameraMetadata filteredParams(availableSessionKeys.count);
1353 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1354 filteredParams.getAndLock());
1355 set_camera_metadata_vendor_id(meta, mVendorTagId);
1356 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001357 if (availableSessionKeys.count > 0) {
Emilian Peevb9f83812023-03-17 17:13:07 -07001358 bool rotateAndCropSessionKey = false;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001359 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1360 camera_metadata_ro_entry entry = params.find(
1361 availableSessionKeys.data.i32[i]);
1362 if (entry.count > 0) {
1363 filteredParams.update(entry);
1364 }
Emilian Peevb9f83812023-03-17 17:13:07 -07001365 if (ANDROID_SCALER_ROTATE_AND_CROP == availableSessionKeys.data.i32[i]) {
1366 rotateAndCropSessionKey = true;
1367 }
1368 }
1369
1370 if (rotateAndCropSessionKey) {
1371 sp<CaptureRequest> request = new CaptureRequest();
1372 PhysicalCameraSettings settingsList;
1373 settingsList.metadata = filteredParams;
1374 request->mSettingsList.push_back(settingsList);
1375
1376 auto rotateAndCropEntry = filteredParams.find(ANDROID_SCALER_ROTATE_AND_CROP);
1377 if (rotateAndCropEntry.count > 0 &&
1378 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
1379 request->mRotateAndCropAuto = true;
1380 } else {
1381 request->mRotateAndCropAuto = false;
1382 }
1383
1384 overrideAutoRotateAndCrop(request, mOverrideToPortrait, mRotateAndCropOverride);
1385 filteredParams = request->mSettingsList.begin()->metadata;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001386 }
1387 }
1388
1389 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001390}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001392status_t Camera3Device::getInputBufferProducer(
1393 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001394 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001395 Mutex::Autolock il(mInterfaceLock);
1396 Mutex::Autolock l(mLock);
1397
1398 if (producer == NULL) {
1399 return BAD_VALUE;
1400 } else if (mInputStream == NULL) {
1401 return INVALID_OPERATION;
1402 }
1403
1404 return mInputStream->getInputBufferProducer(producer);
1405}
1406
Emilian Peevf4816702020-04-03 15:44:51 -07001407status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001408 CameraMetadata *request) {
1409 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001410 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001411
Emilian Peevf4816702020-04-03 15:44:51 -07001412 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001413 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001414 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001415 return BAD_VALUE;
1416 }
1417
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001418 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001420 {
1421 Mutex::Autolock l(mLock);
1422 switch (mStatus) {
1423 case STATUS_ERROR:
1424 CLOGE("Device has encountered a serious error");
1425 return INVALID_OPERATION;
1426 case STATUS_UNINITIALIZED:
1427 CLOGE("Device is not initialized!");
1428 return INVALID_OPERATION;
1429 case STATUS_UNCONFIGURED:
1430 case STATUS_CONFIGURED:
1431 case STATUS_ACTIVE:
1432 // OK
1433 break;
1434 default:
1435 SET_ERR_L("Unexpected status: %d", mStatus);
1436 return INVALID_OPERATION;
1437 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001438
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001439 if (!mRequestTemplateCache[templateId].isEmpty()) {
1440 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001441 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001442 return OK;
1443 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001444 }
1445
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001446 camera_metadata_t *rawRequest;
1447 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001448 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001449
1450 {
1451 Mutex::Autolock l(mLock);
1452 if (res == BAD_VALUE) {
1453 ALOGI("%s: template %d is not supported on this camera device",
1454 __FUNCTION__, templateId);
1455 return res;
1456 } else if (res != OK) {
1457 CLOGE("Unable to construct request template %d: %s (%d)",
1458 templateId, strerror(-res), res);
1459 return res;
1460 }
1461
1462 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1463 mRequestTemplateCache[templateId].acquire(rawRequest);
1464
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001465 // Override the template request with zoomRatioMapper
1466 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1467 &mRequestTemplateCache[templateId]);
1468 if (res != OK) {
1469 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1470 templateId, strerror(-res), res);
1471 return res;
1472 }
1473
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001474 // Fill in JPEG_QUALITY if not available
1475 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1476 static const uint8_t kDefaultJpegQuality = 95;
1477 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1478 &kDefaultJpegQuality, 1);
1479 }
1480
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001481 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001482 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001484 return OK;
1485}
1486
1487status_t Camera3Device::waitUntilDrained() {
1488 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001489 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001490 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001491 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001492
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001493 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001494}
1495
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001496status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001497 switch (mStatus) {
1498 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001499 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001500 ALOGV("%s: Already idle", __FUNCTION__);
1501 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001502 case STATUS_CONFIGURED:
1503 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504 case STATUS_ERROR:
1505 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001506 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001507 break;
1508 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001509 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 return INVALID_OPERATION;
1511 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001512 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1513 maxExpectedDuration);
1514 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001515 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001516 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001517 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1518 res);
1519 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520 return res;
1521}
1522
Ruben Brunk183f0562015-08-12 12:55:02 -07001523void Camera3Device::internalUpdateStatusLocked(Status status) {
1524 mStatus = status;
1525 mRecentStatusUpdates.add(mStatus);
1526 mStatusChanged.broadcast();
1527}
1528
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001529// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001530status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001531 if (mRequestThread.get() != nullptr) {
1532 mRequestThread->setPaused(true);
1533 } else {
1534 return NO_INIT;
1535 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001536
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001537 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1538 maxExpectedDuration);
1539 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001540 if (res != OK) {
1541 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001542 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 }
1544
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001545 return res;
1546}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001547
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001548// Resume after internalPauseAndWaitLocked
1549status_t Camera3Device::internalResumeLocked() {
1550 status_t res;
1551
1552 mRequestThread->setPaused(false);
1553
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001554 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1555 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1557 if (res != OK) {
1558 SET_ERR_L("Can't transition to active in %f seconds!",
1559 kActiveTimeout/1e9);
1560 }
1561 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001563}
1564
Ruben Brunk183f0562015-08-12 12:55:02 -07001565status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001567
1568 size_t startIndex = 0;
1569 if (mStatusWaiters == 0) {
1570 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1571 // this status list
1572 mRecentStatusUpdates.clear();
1573 } else {
1574 // If other threads are waiting on updates to this status list, set the position of the
1575 // first element that this list will check rather than clearing the list.
1576 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577 }
1578
Ruben Brunk183f0562015-08-12 12:55:02 -07001579 mStatusWaiters++;
1580
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001581 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001582 if (!active && mUseHalBufManager) {
1583 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001584 if (mStatus == STATUS_ACTIVE) {
1585 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001586 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001587 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001588 mRequestBufferSM.onWaitUntilIdle();
1589 }
1590
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001591 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001592 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001594 if (active == (mStatus == STATUS_ACTIVE)) {
1595 // Desired state is current
1596 break;
1597 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001598
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001599 nsecs_t timeElapsed = systemTime() - startTime;
1600 nsecs_t timeToWait = timeout - timeElapsed;
1601 if (timeToWait <= 0) {
1602 // Thread woke up spuriously but has timed out since.
1603 // Force out of loop with TIMED_OUT result.
1604 res = TIMED_OUT;
1605 break;
1606 }
1607 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001608 if (res != OK) break;
1609
Ruben Brunk183f0562015-08-12 12:55:02 -07001610 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1611 // transitions.
1612 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1613 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1614 __FUNCTION__);
1615
1616 // Encountered desired state since we began waiting
1617 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001618 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1619 stateSeen = true;
1620 break;
1621 }
1622 }
1623 } while (!stateSeen);
1624
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001625 if (signalPipelineDrain) {
1626 mRequestThread->resetPipelineDrain();
1627 }
1628
Ruben Brunk183f0562015-08-12 12:55:02 -07001629 mStatusWaiters--;
1630
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001631 return res;
1632}
1633
1634
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001635status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001636 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001637 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001639 if (listener != NULL && mListener != NULL) {
1640 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1641 }
1642 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001643 mRequestThread->setNotificationListener(listener);
1644 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001645
1646 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001647}
1648
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001649bool Camera3Device::willNotify3A() {
1650 return false;
1651}
1652
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001653status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001654 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001655 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001656
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001657 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001658 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1659 if (st == std::cv_status::timeout) {
1660 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001661 }
1662 }
1663 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001664}
1665
Jianing Weicb0652e2014-03-12 18:29:36 -07001666status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001667 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001668 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001669
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001670 if (mResultQueue.empty()) {
1671 return NOT_ENOUGH_DATA;
1672 }
1673
Jianing Weicb0652e2014-03-12 18:29:36 -07001674 if (frame == NULL) {
1675 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1676 return BAD_VALUE;
1677 }
1678
1679 CaptureResult &result = *(mResultQueue.begin());
1680 frame->mResultExtras = result.mResultExtras;
1681 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001682 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001683 mResultQueue.erase(mResultQueue.begin());
1684
1685 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001686}
1687
1688status_t Camera3Device::triggerAutofocus(uint32_t id) {
1689 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001691
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001692 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1693 // Mix-in this trigger into the next request and only the next request.
1694 RequestTrigger trigger[] = {
1695 {
1696 ANDROID_CONTROL_AF_TRIGGER,
1697 ANDROID_CONTROL_AF_TRIGGER_START
1698 },
1699 {
1700 ANDROID_CONTROL_AF_TRIGGER_ID,
1701 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001702 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001703 };
1704
1705 return mRequestThread->queueTrigger(trigger,
1706 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707}
1708
1709status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1710 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001711 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001712
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001713 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1714 // Mix-in this trigger into the next request and only the next request.
1715 RequestTrigger trigger[] = {
1716 {
1717 ANDROID_CONTROL_AF_TRIGGER,
1718 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1719 },
1720 {
1721 ANDROID_CONTROL_AF_TRIGGER_ID,
1722 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001723 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001724 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001725
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001726 return mRequestThread->queueTrigger(trigger,
1727 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001728}
1729
1730status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1731 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001732 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001733
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001734 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1735 // Mix-in this trigger into the next request and only the next request.
1736 RequestTrigger trigger[] = {
1737 {
1738 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1739 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1740 },
1741 {
1742 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1743 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001744 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001745 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001746
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001747 return mRequestThread->queueTrigger(trigger,
1748 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001749}
1750
Jianing Weicb0652e2014-03-12 18:29:36 -07001751status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001752 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001753 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001754 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001755
Zhijun He7ef20392014-04-21 16:04:17 -07001756 {
1757 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001758
1759 // b/116514106 "disconnect()" can get called twice for the same device. The
1760 // camera device will not be initialized during the second run.
1761 if (mStatus == STATUS_UNINITIALIZED) {
1762 return OK;
1763 }
1764
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001765 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001766
1767 // Stop session and stream counter
1768 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001769 }
1770
Ravneet98ffa752022-03-02 07:22:46 +00001771 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal72c96652022-06-29 01:34:01 +00001772 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneet98ffa752022-03-02 07:22:46 +00001773 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1774 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1775
1776 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001777}
1778
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001779status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001780 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1781}
1782
1783status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001784 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001785 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001786 Mutex::Autolock il(mInterfaceLock);
1787 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001788
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001789 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1790 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001791 CLOGE("Stream %d does not exist", streamId);
1792 return BAD_VALUE;
1793 }
1794
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001795 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001796 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001797 return BAD_VALUE;
1798 }
1799
1800 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001801 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001802 return BAD_VALUE;
1803 }
1804
Ruben Brunkc78ac262015-08-13 17:58:46 -07001805 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001806}
1807
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001808status_t Camera3Device::tearDown(int streamId) {
1809 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001810 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001811 Mutex::Autolock il(mInterfaceLock);
1812 Mutex::Autolock l(mLock);
1813
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001814 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1815 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001816 CLOGE("Stream %d does not exist", streamId);
1817 return BAD_VALUE;
1818 }
1819
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001820 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1821 CLOGE("Stream %d is a target of a in-progress request", streamId);
1822 return BAD_VALUE;
1823 }
1824
1825 return stream->tearDown();
1826}
1827
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001828status_t Camera3Device::addBufferListenerForStream(int streamId,
1829 wp<Camera3StreamBufferListener> listener) {
1830 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001831 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001832 Mutex::Autolock il(mInterfaceLock);
1833 Mutex::Autolock l(mLock);
1834
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001835 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1836 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001837 CLOGE("Stream %d does not exist", streamId);
1838 return BAD_VALUE;
1839 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001840 stream->addBufferListener(listener);
1841
1842 return OK;
1843}
1844
Austin Borger4a870a32022-02-25 01:48:41 +00001845float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1846 camera_metadata_entry minDurations =
1847 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1848 for (size_t i = 0; i < minDurations.count; i += 4) {
1849 if (minDurations.data.i64[i] == stream->getFormat()
1850 && minDurations.data.i64[i+1] == stream->getWidth()
1851 && minDurations.data.i64[i+2] == stream->getHeight()) {
1852 int64_t minFrameDuration = minDurations.data.i64[i+3];
1853 return 1e9f / minFrameDuration;
1854 }
1855 }
1856 return 0.0f;
1857}
1858
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001859/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001860 * Methods called by subclasses
1861 */
1862
1863void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001864 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001865 std::vector<int> streamIds;
1866 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001867 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001868
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001869 {
1870 // Need mLock to safely update state and synchronize to current
1871 // state of methods in flight.
1872 Mutex::Autolock l(mLock);
1873 // We can get various system-idle notices from the status tracker
1874 // while starting up. Only care about them if we've actually sent
1875 // in some requests recently.
1876 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1877 return;
1878 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001879 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1880 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001881 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001882
1883 // Skip notifying listener if we're doing some user-transparent
1884 // state changes
1885 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001886
Austin Borger4a870a32022-02-25 01:48:41 +00001887 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1888 auto stream = mOutputStreams[i];
1889 if (stream.get() == nullptr) continue;
1890
1891 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1892 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1893
1894 // Populate stream statistics in case of Idle
1895 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001896 streamIds.push_back(stream->getId());
1897 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1898 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001899 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001900 if (camera3Stream != nullptr) {
1901 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001902 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001903 }
1904 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001905 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001906 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001907 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001908 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001909 }
1910 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001911 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001912
1913 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001914 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001915 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001916 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001917 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001918 status_t res = OK;
1919 if (listener != nullptr) {
1920 if (idle) {
1921 // Get session stats from the builder, and notify the listener.
1922 int64_t requestCount, resultErrorCount;
1923 bool deviceError;
1924 std::map<int, StreamStats> streamStatsMap;
1925 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1926 &deviceError, &streamStatsMap);
1927 for (size_t i = 0; i < streamIds.size(); i++) {
1928 int streamId = streamIds[i];
1929 auto stats = streamStatsMap.find(streamId);
1930 if (stats != streamStatsMap.end()) {
1931 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1932 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1933 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1934 streamStats[i].mHistogramType =
1935 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1936 streamStats[i].mHistogramBins.assign(
1937 stats->second.mCaptureLatencyBins.begin(),
1938 stats->second.mCaptureLatencyBins.end());
1939 streamStats[i].mHistogramCounts.assign(
1940 stats->second.mCaptureLatencyHistogram.begin(),
1941 stats->second.mCaptureLatencyHistogram.end());
1942 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001943 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001944 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1945 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001946 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001947 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001948 }
1949 if (res != OK) {
1950 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1951 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 }
1953}
1954
Shuzhen Wang758c2152017-01-10 18:26:18 -08001955status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001956 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001957 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001958 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1959 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001960
1961 if (surfaceIds == nullptr) {
1962 return BAD_VALUE;
1963 }
1964
Zhijun He5d677d12016-05-29 16:52:39 -07001965 Mutex::Autolock il(mInterfaceLock);
1966 Mutex::Autolock l(mLock);
1967
Shuzhen Wang758c2152017-01-10 18:26:18 -08001968 if (consumers.size() == 0) {
1969 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001970 return BAD_VALUE;
1971 }
1972
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001973 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1974 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001975 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001976 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001977 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001978
1979 // isConsumerConfigurationDeferred will be off after setConsumers
1980 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001981 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001982 if (res != OK) {
1983 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1984 return res;
1985 }
1986
Emilian Peev40ead602017-09-26 15:46:36 +01001987 for (auto &consumer : consumers) {
1988 int id = stream->getSurfaceId(consumer);
1989 if (id < 0) {
1990 CLOGE("Invalid surface id!");
1991 return BAD_VALUE;
1992 }
1993 surfaceIds->push_back(id);
1994 }
1995
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001996 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001997 if (!stream->isConfiguring()) {
1998 CLOGE("Stream %d was already fully configured.", streamId);
1999 return INVALID_OPERATION;
2000 }
Zhijun He5d677d12016-05-29 16:52:39 -07002001
Shuzhen Wang0129d522016-10-30 22:43:41 -07002002 res = stream->finishConfiguration();
2003 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002004 // If finishConfiguration fails due to abandoned surface, do not set
2005 // device to error state.
2006 bool isSurfaceAbandoned =
2007 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2008 if (!isSurfaceAbandoned) {
2009 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2010 stream->getId(), strerror(-res), res);
2011 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002012 return res;
2013 }
Zhijun He5d677d12016-05-29 16:52:39 -07002014 }
2015
2016 return OK;
2017}
2018
Emilian Peev40ead602017-09-26 15:46:36 +01002019status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2020 const std::vector<OutputStreamInfo> &outputInfo,
2021 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2022 Mutex::Autolock il(mInterfaceLock);
2023 Mutex::Autolock l(mLock);
2024
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002025 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2026 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002027 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002028 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002029 }
2030
2031 for (const auto &it : removedSurfaceIds) {
2032 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2033 CLOGE("Shared surface still part of a pending request!");
2034 return -EBUSY;
2035 }
2036 }
2037
Emilian Peev40ead602017-09-26 15:46:36 +01002038 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2039 if (res != OK) {
2040 CLOGE("Stream %d failed to update stream (error %d %s) ",
2041 streamId, res, strerror(-res));
2042 if (res == UNKNOWN_ERROR) {
2043 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2044 __FUNCTION__);
2045 }
2046 return res;
2047 }
2048
2049 return res;
2050}
2051
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002052status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2053 Mutex::Autolock il(mInterfaceLock);
2054 Mutex::Autolock l(mLock);
2055
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002056 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2057 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002058 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2059 return BAD_VALUE;
2060 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002061
2062 if (dropping) {
2063 mSessionStatsBuilder.stopCounter(streamId);
2064 } else {
2065 mSessionStatsBuilder.startCounter(streamId);
2066 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002067 return stream->dropBuffers(dropping);
2068}
2069
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071 * Camera3Device private methods
2072 */
2073
2074sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002075 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002078 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002079 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002080
2081 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002082 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083 if (inputStreams.count > 0) {
2084 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002085 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002086 CLOGE("Request references unknown input stream %d",
2087 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002088 return NULL;
2089 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002090
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002091 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002092 SET_ERR_L("%s: input stream %d is not configured!",
2093 __FUNCTION__, mInputStream->getId());
2094 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002095 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002096 // Check if stream prepare is blocking requests.
2097 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002098 CLOGE("Request references an input stream that's being prepared!");
2099 return NULL;
2100 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002101
2102 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002103 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002104 }
2105
2106 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002107 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002108 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002109 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110 return NULL;
2111 }
2112
2113 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002114 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2115 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002116 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002117 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118 return NULL;
2119 }
Zhijun He5d677d12016-05-29 16:52:39 -07002120 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002121 auto iter = surfaceMap.find(streams.data.i32[i]);
2122 if (iter != surfaceMap.end()) {
2123 const std::vector<size_t>& surfaces = iter->second;
2124 for (const auto& surface : surfaces) {
2125 if (stream->isConsumerConfigurationDeferred(surface)) {
2126 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2127 "due to deferred consumer", stream->getId(), surface);
2128 return NULL;
2129 }
2130 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002131 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002132 }
2133
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002134 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002135 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2136 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002137 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002138 // Check if stream prepare is blocking requests.
2139 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002140 CLOGE("Request references an output stream that's being prepared!");
2141 return NULL;
2142 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002143
2144 newRequest->mOutputStreams.push(stream);
2145 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002146 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002147 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002148
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002149 auto rotateAndCropEntry =
2150 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2151 if (rotateAndCropEntry.count > 0 &&
2152 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2153 newRequest->mRotateAndCropAuto = true;
2154 } else {
2155 newRequest->mRotateAndCropAuto = false;
2156 }
2157
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002158 auto zoomRatioEntry =
2159 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2160 if (zoomRatioEntry.count > 0 &&
2161 zoomRatioEntry.data.f[0] == 1.0f) {
2162 newRequest->mZoomRatioIs1x = true;
2163 } else {
2164 newRequest->mZoomRatioIs1x = false;
2165 }
2166
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002167 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002168 for (auto& settings : newRequest->mSettingsList) {
2169 auto testPatternModeEntry =
2170 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2171 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2172 testPatternModeEntry.data.i32[0] :
2173 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002174
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002175 auto testPatternDataEntry =
2176 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2177 if (testPatternDataEntry.count >= 4) {
2178 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2179 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2180 } else {
2181 settings.mOriginalTestPatternData[0] = 0;
2182 settings.mOriginalTestPatternData[1] = 0;
2183 settings.mOriginalTestPatternData[2] = 0;
2184 settings.mOriginalTestPatternData[3] = 0;
2185 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002186 }
2187 }
2188
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002189 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002190}
2191
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002192void Camera3Device::cancelStreamsConfigurationLocked() {
2193 int res = OK;
2194 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2195 res = mInputStream->cancelConfiguration();
2196 if (res != OK) {
2197 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2198 mInputStream->getId(), strerror(-res), res);
2199 }
2200 }
2201
2202 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002203 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002204 if (outputStream->isConfiguring()) {
2205 res = outputStream->cancelConfiguration();
2206 if (res != OK) {
2207 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2208 outputStream->getId(), strerror(-res), res);
2209 }
2210 }
2211 }
2212
2213 // Return state to that at start of call, so that future configures
2214 // properly clean things up
2215 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2216 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002217
2218 res = mPreparerThread->resume();
2219 if (res != OK) {
2220 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2221 }
2222}
2223
Emilian Peev0d0191e2020-04-21 17:01:18 -07002224bool Camera3Device::checkAbandonedStreamsLocked() {
2225 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2226 return true;
2227 }
2228
2229 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2230 auto stream = mOutputStreams[i];
2231 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2232 return true;
2233 }
2234 }
2235
2236 return false;
2237}
2238
Emilian Peev3bead5f2020-05-28 17:29:08 -07002239bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002240 ATRACE_CALL();
2241 bool ret = false;
2242
Shuzhen Wang316781a2020-08-18 18:11:01 -07002243 nsecs_t startTime = systemTime();
2244
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002245 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002246 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2247
2248 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002249 if (checkAbandonedStreamsLocked()) {
2250 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2251 __FUNCTION__);
2252 return true;
2253 }
2254
Emilian Peev3bead5f2020-05-28 17:29:08 -07002255 status_t rc = NO_ERROR;
2256 bool markClientActive = false;
2257 if (mStatus == STATUS_ACTIVE) {
2258 markClientActive = true;
2259 mPauseStateNotify = true;
2260 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2261
2262 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2263 }
2264
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002265 if (rc == NO_ERROR) {
2266 mNeedConfig = true;
2267 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2268 if (rc == NO_ERROR) {
2269 ret = true;
2270 mPauseStateNotify = false;
2271 //Moving to active state while holding 'mLock' is important.
2272 //There could be pending calls to 'create-/deleteStream' which
2273 //will trigger another stream configuration while the already
2274 //present streams end up with outstanding buffers that will
2275 //not get drained.
2276 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002277 } else if (rc == DEAD_OBJECT) {
2278 // DEAD_OBJECT can be returned if either the consumer surface is
2279 // abandoned, or the HAL has died.
2280 // - If the HAL has died, configureStreamsLocked call will set
2281 // device to error state,
2282 // - If surface is abandoned, we should not set device to error
2283 // state.
2284 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002285 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002286 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002287 }
2288 } else {
2289 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2290 }
2291
Shuzhen Wang316781a2020-08-18 18:11:01 -07002292 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2293 ns2ms(systemTime() - startTime));
2294
Emilian Peev3bead5f2020-05-28 17:29:08 -07002295 if (markClientActive) {
2296 mStatusTracker->markComponentActive(clientStatusId);
2297 }
2298
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002299 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002300}
2301
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002302status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002303 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002304 ATRACE_CALL();
2305 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002306
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002307 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002308 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002309 return INVALID_OPERATION;
2310 }
2311
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002312 if (operatingMode < 0) {
2313 CLOGE("Invalid operating mode: %d", operatingMode);
2314 return BAD_VALUE;
2315 }
2316
2317 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002318 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002319
2320 if (mOperatingMode != operatingMode) {
2321 mNeedConfig = true;
2322 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2323 mOperatingMode = operatingMode;
2324 }
2325
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002326 // Reset min expected duration when session is reconfigured.
2327 mMinExpectedDuration = 0;
2328
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002329 // In case called from configureStreams, abort queued input buffers not belonging to
2330 // any pending requests.
2331 if (mInputStream != NULL && notifyRequestThread) {
2332 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002333 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002334 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002335 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002336 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002337 if (res != OK) {
2338 // Exhausted acquiring all input buffers.
2339 break;
2340 }
2341
Emilian Peevf4816702020-04-03 15:44:51 -07002342 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002343 res = mInputStream->returnInputBuffer(inputBuffer);
2344 if (res != OK) {
2345 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2346 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2347 }
2348 }
2349 }
2350
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002351 if (!mNeedConfig) {
2352 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2353 return OK;
2354 }
2355
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002356 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002357 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002358 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2359 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002360 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002361 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002362 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002363 }
2364
Shuzhen Wangabe5ea12022-12-15 22:38:07 -08002365 // Override stream use case based on "adb shell command"
2366 overrideStreamUseCaseLocked();
2367
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002369 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002370
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002371 mPreparerThread->pause();
2372
Emilian Peevf4816702020-04-03 15:44:51 -07002373 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002374 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002375 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002376 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377
Emilian Peevf4816702020-04-03 15:44:51 -07002378 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002379 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002380 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002381
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382
2383 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002384 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002385 inputStream = mInputStream->startConfiguration();
2386 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002387 CLOGE("Can't start input stream configuration");
2388 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002389 return INVALID_OPERATION;
2390 }
2391 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002392
2393 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002394 }
2395
Shuzhen Wang99080502021-03-07 21:08:20 -08002396 mGroupIdPhysicalCameraMap.clear();
Emilian Peevb9f83812023-03-17 17:13:07 -07002397 mComposerOutput = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002398 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002399
2400 // Don't configure bidi streams twice, nor add them twice to the list
2401 if (mOutputStreams[i].get() ==
2402 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2403
2404 config.num_streams--;
2405 continue;
2406 }
2407
Emilian Peevf4816702020-04-03 15:44:51 -07002408 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002409 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002410 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002411 CLOGE("Can't start output stream configuration");
2412 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413 return INVALID_OPERATION;
2414 }
2415 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002416
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002417 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002418 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2419 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002420 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2421 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002422 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2423 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002424 } else if (outputStream->data_space ==
2425 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2426 bufferSizes[k] = outputStream->width * outputStream->height;
2427 } else {
2428 ALOGW("%s: Blob dataSpace %d not supported",
2429 __FUNCTION__, outputStream->data_space);
2430 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002431 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002432
2433 if (mOutputStreams[i]->isMultiResolution()) {
2434 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2435 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2436 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2437 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002438
2439 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
Emilian Peevb9f83812023-03-17 17:13:07 -07002440 mComposerOutput = true;
Emilian Peeve23f1d92021-09-20 14:56:01 -07002441 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002442 }
2443
2444 config.streams = streams.editArray();
2445
2446 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002447 // max_buffers, usage, and priv fields, as well as data_space and format
2448 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002449
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002450 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002451 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002452 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002454 if (res == BAD_VALUE) {
2455 // HAL rejected this set of streams as unsupported, clean up config
2456 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002457 CLOGE("Set of requested inputs/outputs not supported by HAL");
2458 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002459 return BAD_VALUE;
2460 } else if (res != OK) {
2461 // Some other kind of error from configure_streams - this is not
2462 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002463 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2464 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002465 return res;
2466 }
2467
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002468 // Finish all stream configuration immediately.
2469 // TODO: Try to relax this later back to lazy completion, which should be
2470 // faster
2471
Igor Murashkin073f8572013-05-02 14:59:28 -07002472 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002473 bool streamReConfigured = false;
2474 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002475 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002476 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002477 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002478 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002479 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2480 return DEAD_OBJECT;
2481 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002482 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002483 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002484 if (streamReConfigured) {
2485 mInterface->onStreamReConfigured(mInputStream->getId());
2486 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002487 }
2488
2489 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002490 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002491 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002492 bool streamReConfigured = false;
2493 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002494 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002495 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002496 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002497 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002498 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2499 return DEAD_OBJECT;
2500 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002501 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002502 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002503 if (streamReConfigured) {
2504 mInterface->onStreamReConfigured(outputStream->getId());
2505 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002506 }
2507 }
2508
Emilian Peevb9f83812023-03-17 17:13:07 -07002509 mRequestThread->setComposerSurface(mComposerOutput);
Emilian Peeve23f1d92021-09-20 14:56:01 -07002510
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002511 // Request thread needs to know to avoid using repeat-last-settings protocol
2512 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002513 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002514 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2515 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002516 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002517
Zhijun He90f7c372016-08-16 16:19:43 -07002518 char value[PROPERTY_VALUE_MAX];
2519 property_get("camera.fifo.disable", value, "0");
2520 int32_t disableFifo = atoi(value);
2521 if (disableFifo != 1) {
2522 // Boost priority of request thread to SCHED_FIFO.
2523 pid_t requestThreadTid = mRequestThread->getTid();
2524 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002525 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002526 if (res != OK) {
2527 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2528 strerror(-res), res);
2529 } else {
2530 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2531 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002532 }
2533
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002534 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002535 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2536 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2537 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2538 sessionParams.unlock(newSessionParams);
2539 mSessionParams.unlock(currentSessionParams);
2540 if (updateSessionParams) {
2541 mSessionParams = sessionParams;
2542 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002543
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002544 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002545
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002546 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002547 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002548
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002549 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002550
Zhijun He0a210512014-07-24 13:45:15 -07002551 // tear down the deleted streams after configure streams.
2552 mDeletedStreams.clear();
2553
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002554 auto rc = mPreparerThread->resume();
2555 if (rc != OK) {
2556 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2557 return rc;
2558 }
2559
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002560 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002561 mRequestBufferSM.onStreamsConfigured();
2562 }
2563
Cliff Wu3b268182021-07-06 15:44:43 +08002564 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002565 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002566 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002567 // configure the injection streams.
2568 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002569 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002570 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2571 res = mInjectionMethods->injectCamera(config, bufferSizes);
2572 if (res != OK) {
2573 ALOGE("Can't finish inject camera process!");
2574 return res;
2575 }
Cliff Wu3b268182021-07-06 15:44:43 +08002576 } else {
2577 // First run configureStreamsLocked() and then call injectCamera() case:
2578 // If the stream configuration has been completed and camera deive is active, but the
2579 // injection camera has not been injected yet, we need to store the stream configuration of
2580 // the internal camera (because the stream configuration of the injection camera is based
2581 // on the internal camera). When injecting occurs later, this configuration can be used by
2582 // the injection camera.
2583 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2584 " injection camera has not been injected yet.", __FUNCTION__);
2585 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002586 }
2587
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002588 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002589}
2590
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002591status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002592 ATRACE_CALL();
2593 status_t res;
2594
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002595 if (mFakeStreamId != NO_STREAM) {
2596 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002597 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002598 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002599 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002600 return INVALID_OPERATION;
2601 }
2602
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002603 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002604
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002605 sp<Camera3OutputStreamInterface> fakeStream =
2606 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002607
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002608 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002609 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002610 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002611 return res;
2612 }
2613
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002614 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002615 mNextStreamId++;
2616
2617 return OK;
2618}
2619
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002620status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002621 ATRACE_CALL();
2622 status_t res;
2623
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002624 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002625 if (mOutputStreams.size() == 1) return OK;
2626
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002627 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002628
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002629 // Ok, have a fake stream and there's at least one other output stream,
2630 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002631
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002632 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002633 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002634 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002635 return INVALID_OPERATION;
2636 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002637 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002638
2639 // Free up the stream endpoint so that it can be used by some other stream
2640 res = deletedStream->disconnect();
2641 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002642 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002643 // fall through since we want to still list the stream as deleted.
2644 }
2645 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002646 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002647
2648 return res;
2649}
2650
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002651void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002652 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002653 Mutex::Autolock l(mLock);
2654 va_list args;
2655 va_start(args, fmt);
2656
2657 setErrorStateLockedV(fmt, args);
2658
2659 va_end(args);
2660}
2661
2662void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002663 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002664 Mutex::Autolock l(mLock);
2665 setErrorStateLockedV(fmt, args);
2666}
2667
2668void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2669 va_list args;
2670 va_start(args, fmt);
2671
2672 setErrorStateLockedV(fmt, args);
2673
2674 va_end(args);
2675}
2676
2677void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002678 // Print out all error messages to log
2679 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002680 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002681
2682 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002683 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002684
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002685 mErrorCause = errorCause;
2686
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002687 if (mRequestThread != nullptr) {
2688 mRequestThread->setPaused(true);
2689 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002690 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002691
2692 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002693 sp<NotificationListener> listener = mListener.promote();
2694 if (listener != NULL) {
2695 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002696 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002697 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002698 }
2699
2700 // Save stack trace. View by dumping it later.
2701 CameraTraces::saveTrace();
2702 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002703}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002704
2705/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002706 * In-flight request management
2707 */
2708
Jianing Weicb0652e2014-03-12 18:29:36 -07002709status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002710 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002711 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002712 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Shuzhen Wang99080502021-03-07 21:08:20 -08002713 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2714 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002715 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002716 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002717 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002718
2719 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002720 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002721 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002722 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2723 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002724 if (res < 0) return res;
2725
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002726 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002727 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2728 // avoid a deadlock during reprocess requests.
2729 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002730 if (mStatusTracker != nullptr) {
2731 mStatusTracker->markComponentActive(mInFlightStatusId);
2732 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002733 }
2734
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002735 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002736 return OK;
2737}
2738
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002739void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002740 // Indicate idle inFlightMap to the status tracker
2741 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002742 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002743 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2744 // avoid a deadlock during reprocess requests.
2745 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002746 if (mStatusTracker != nullptr) {
2747 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2748 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002749 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002750 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002751}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002752
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002753void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002754 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002755 // something has likely gone wrong. This might still be legit only if application send in
2756 // a long burst of long exposure requests.
2757 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2758 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2759 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2760 mInFlightMap.size(), mExpectedInflightDuration);
2761 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2762 kInFlightWarnLimitHighSpeed) {
2763 CLOGW("In-flight list too large for high speed configuration: %zu,"
2764 "total inflight duration %" PRIu64,
2765 mInFlightMap.size(), mExpectedInflightDuration);
2766 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002767 }
2768}
2769
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002770void Camera3Device::onInflightMapFlushedLocked() {
2771 mExpectedInflightDuration = 0;
2772}
2773
2774void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002775 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002776 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2777 mInFlightMap.removeItemsAt(idx, 1);
2778
2779 onInflightEntryRemovedLocked(duration);
2780}
2781
2782
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002783void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002784 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002785 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002786 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002787 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002788 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002789 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002790
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002791 FlushInflightReqStates states {
2792 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002793 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002794
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002795 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002796}
2797
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002798CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002799 ALOGV("%s", __FUNCTION__);
2800
Igor Murashkin1e479c02013-09-06 16:55:14 -07002801 CameraMetadata retVal;
2802
2803 if (mRequestThread != NULL) {
2804 retVal = mRequestThread->getLatestRequest();
2805 }
2806
Igor Murashkin1e479c02013-09-06 16:55:14 -07002807 return retVal;
2808}
2809
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002810void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002811 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002812 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002813 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2814 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002815
2816 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002817 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002818}
2819
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002820void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002821 std::vector<native_handle_t*> *handles, bool closeFd) {
2822 if (handles == nullptr) {
2823 return;
2824 }
2825 if (closeFd) {
2826 for (auto& handle : *handles) {
2827 native_handle_close(handle);
2828 }
2829 }
2830 for (auto& handle : *handles) {
2831 native_handle_delete(handle);
2832 }
2833 handles->clear();
2834 return;
2835}
2836
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002837/**
2838 * HalInterface inner class methods
2839 */
2840
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002841void Camera3Device::HalInterface::getInflightBufferKeys(
2842 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002843 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002844 return;
2845}
2846
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002847void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2848 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002849 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002850 return;
2851}
2852
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002853bool Camera3Device::HalInterface::verifyBufferIds(
2854 int32_t streamId, std::vector<uint64_t>& bufIds) {
2855 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002856}
2857
2858status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002859 int32_t frameNumber, int32_t streamId,
2860 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002861 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002862}
2863
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002864status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002865 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002866 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002867}
2868
2869// Find and pop a buffer_handle_t based on bufferId
2870status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002871 uint64_t bufferId,
2872 /*out*/ buffer_handle_t** buffer,
2873 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002874 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002875}
2876
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002877std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2878 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002879 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002880}
2881
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002882uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2883 const native_handle_t* handle) {
2884 return mBufferRecords.removeOneBufferCache(streamId, handle);
2885}
2886
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002887void Camera3Device::HalInterface::onBufferFreed(
2888 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002889 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2890 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2891 if (bufferId != BUFFER_ID_NO_BUFFER) {
2892 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002893 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002894}
2895
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002896void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002897 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2898 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2899 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002900 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2901 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002902}
2903
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002904/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002905 * RequestThread inner class methods
2906 */
2907
2908Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002909 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002910 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002911 bool useHalBufManager,
Austin Borger3560b7e2022-10-27 12:20:29 -07002912 bool supportCameraMute,
2913 bool overrideToPortrait) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002914 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002915 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002916 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002917 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002918 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002919 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002920 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002921 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002922 mReconfigured(false),
2923 mDoPause(false),
2924 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002925 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002926 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002927 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002928 mCurrentAfTriggerId(0),
2929 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002930 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002931 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002932 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002933 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002934 mRepeatingLastFrameNumber(
2935 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002936 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002937 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002938 mRequestLatency(kRequestLatencyBinSize),
2939 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002940 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002941 mUseHalBufManager(useHalBufManager),
Austin Borger3560b7e2022-10-27 12:20:29 -07002942 mSupportCameraMute(supportCameraMute),
2943 mOverrideToPortrait(overrideToPortrait) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002944 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002945}
2946
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002947Camera3Device::RequestThread::~RequestThread() {}
2948
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002949void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002950 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002951 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002952 Mutex::Autolock l(mRequestLock);
2953 mListener = listener;
2954}
2955
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002956void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002957 const CameraMetadata& sessionParams,
2958 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002959 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002960 Mutex::Autolock l(mRequestLock);
2961 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002962 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002963 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002964 // Prepare video stream for high speed recording.
2965 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002966 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002967}
2968
Jianing Wei90e59c92014-03-12 18:29:36 -07002969status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002970 List<sp<CaptureRequest> > &requests,
2971 /*out*/
2972 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002973 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002974 Mutex::Autolock l(mRequestLock);
2975 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2976 ++it) {
2977 mRequestQueue.push_back(*it);
2978 }
2979
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002980 if (lastFrameNumber != NULL) {
2981 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2982 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2983 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2984 *lastFrameNumber);
2985 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002986
Jianing Wei90e59c92014-03-12 18:29:36 -07002987 unpauseForNewRequests();
2988
2989 return OK;
2990}
2991
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002992
2993status_t Camera3Device::RequestThread::queueTrigger(
2994 RequestTrigger trigger[],
2995 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002996 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002997 Mutex::Autolock l(mTriggerMutex);
2998 status_t ret;
2999
3000 for (size_t i = 0; i < count; ++i) {
3001 ret = queueTriggerLocked(trigger[i]);
3002
3003 if (ret != OK) {
3004 return ret;
3005 }
3006 }
3007
3008 return OK;
3009}
3010
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003011const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3012 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003013 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003014 if (d != nullptr) return d->mId;
3015 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003016}
3017
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003018status_t Camera3Device::RequestThread::queueTriggerLocked(
3019 RequestTrigger trigger) {
3020
3021 uint32_t tag = trigger.metadataTag;
3022 ssize_t index = mTriggerMap.indexOfKey(tag);
3023
3024 switch (trigger.getTagType()) {
3025 case TYPE_BYTE:
3026 // fall-through
3027 case TYPE_INT32:
3028 break;
3029 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003030 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3031 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003032 return INVALID_OPERATION;
3033 }
3034
3035 /**
3036 * Collect only the latest trigger, since we only have 1 field
3037 * in the request settings per trigger tag, and can't send more than 1
3038 * trigger per request.
3039 */
3040 if (index != NAME_NOT_FOUND) {
3041 mTriggerMap.editValueAt(index) = trigger;
3042 } else {
3043 mTriggerMap.add(tag, trigger);
3044 }
3045
3046 return OK;
3047}
3048
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003049status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003050 const RequestList &requests,
3051 /*out*/
3052 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003053 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003054 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003055 if (lastFrameNumber != NULL) {
3056 *lastFrameNumber = mRepeatingLastFrameNumber;
3057 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003059 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003060 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3061 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003062
3063 unpauseForNewRequests();
3064
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003065 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003066 return OK;
3067}
3068
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003069bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003070 if (mRepeatingRequests.empty()) {
3071 return false;
3072 }
3073 int32_t requestId = requestIn->mResultExtras.requestId;
3074 const RequestList &repeatRequests = mRepeatingRequests;
3075 // All repeating requests are guaranteed to have same id so only check first quest
3076 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3077 return (firstRequest->mResultExtras.requestId == requestId);
3078}
3079
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003080status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003081 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003082 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003083 return clearRepeatingRequestsLocked(lastFrameNumber);
3084
3085}
3086
3087status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003088 std::vector<int32_t> streamIds;
3089 for (const auto& request : mRepeatingRequests) {
3090 for (const auto& stream : request->mOutputStreams) {
3091 streamIds.push_back(stream->getId());
3092 }
3093 }
3094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003095 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003096 if (lastFrameNumber != NULL) {
3097 *lastFrameNumber = mRepeatingLastFrameNumber;
3098 }
Emilian Peev2295df72021-11-12 18:14:10 -08003099
3100 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3101
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003102 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003103 return OK;
3104}
3105
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003106status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003107 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003108 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003109 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003110 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003111
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003112 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003113
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003114 // Send errors for all requests pending in the request queue, including
3115 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003116 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003117 if (listener != NULL) {
3118 for (RequestList::iterator it = mRequestQueue.begin();
3119 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003120 // Abort the input buffers for reprocess requests.
3121 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003122 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003123 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003124 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003125 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003126 if (res != OK) {
3127 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3128 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3129 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003130 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003131 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3132 if (res != OK) {
3133 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3134 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3135 }
3136 }
3137 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003138 // Set the frame number this request would have had, if it
3139 // had been submitted; this frame number will not be reused.
3140 // The requestId and burstId fields were set when the request was
3141 // submitted originally (in convertMetadataListToRequestListLocked)
3142 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003143 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003144 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003145 }
3146 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003147 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003148
3149 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003150 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003151 if (lastFrameNumber != NULL) {
3152 *lastFrameNumber = mRepeatingLastFrameNumber;
3153 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003154 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003155 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003156 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003157 return OK;
3158}
3159
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003160status_t Camera3Device::RequestThread::flush() {
3161 ATRACE_CALL();
3162 Mutex::Autolock l(mFlushLock);
3163
Emilian Peev08dd2452017-04-06 16:55:14 +01003164 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003165}
3166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003167void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003168 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003169 Mutex::Autolock l(mPauseLock);
3170 mDoPause = paused;
3171 mDoPauseSignal.signal();
3172}
3173
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003174status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3175 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003176 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003177 Mutex::Autolock l(mLatestRequestMutex);
3178 status_t res;
3179 while (mLatestRequestId != requestId) {
3180 nsecs_t startTime = systemTime();
3181
3182 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3183 if (res != OK) return res;
3184
3185 timeout -= (systemTime() - startTime);
3186 }
3187
3188 return OK;
3189}
3190
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003191void Camera3Device::RequestThread::requestExit() {
3192 // Call parent to set up shutdown
3193 Thread::requestExit();
3194 // The exit from any possible waits
3195 mDoPauseSignal.signal();
3196 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003197
3198 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3199 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003200}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003201
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003202void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003203 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003204 bool surfaceAbandoned = false;
3205 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003206 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003207 {
3208 Mutex::Autolock l(mRequestLock);
3209 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3210 // repeating requests.
3211 for (const auto& request : mRepeatingRequests) {
3212 for (const auto& s : request->mOutputStreams) {
3213 if (s->isAbandoned()) {
3214 surfaceAbandoned = true;
3215 clearRepeatingRequestsLocked(&lastFrameNumber);
3216 break;
3217 }
3218 }
3219 if (surfaceAbandoned) {
3220 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003221 }
3222 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003223 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003224 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003225
3226 if (listener != NULL && surfaceAbandoned) {
3227 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003228 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003229}
3230
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003231bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003232 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003233 status_t res;
3234 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003235 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003236 uint32_t numRequestProcessed = 0;
3237 for (size_t i = 0; i < batchSize; i++) {
3238 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003239 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003240 }
3241
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003242 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3243
3244 bool triggerRemoveFailed = false;
3245 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3246 for (size_t i = 0; i < numRequestProcessed; i++) {
3247 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3248 nextRequest.submitted = true;
3249
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003250 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003251
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003252 if (!triggerRemoveFailed) {
3253 // Remove any previously queued triggers (after unlock)
3254 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3255 if (removeTriggerRes != OK) {
3256 triggerRemoveFailed = true;
3257 triggerFailedRequest = nextRequest;
3258 }
3259 }
3260 }
3261
3262 if (triggerRemoveFailed) {
3263 SET_ERR("RequestThread: Unable to remove triggers "
3264 "(capture request %d, HAL device: %s (%d)",
3265 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3266 cleanUpFailedRequests(/*sendRequestError*/ false);
3267 return false;
3268 }
3269
3270 if (res != OK) {
3271 // Should only get a failure here for malformed requests or device-level
3272 // errors, so consider all errors fatal. Bad metadata failures should
3273 // come through notify.
3274 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3275 mNextRequests[numRequestProcessed].halRequest.frame_number,
3276 strerror(-res), res);
3277 cleanUpFailedRequests(/*sendRequestError*/ false);
3278 return false;
3279 }
3280 return true;
3281}
3282
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003283Camera3Device::RequestThread::ExpectedDurationInfo
3284 Camera3Device::RequestThread::calculateExpectedDurationRange(
3285 const camera_metadata_t *request) {
3286 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003287 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003288 InFlightRequest::kDefaultMaxExpectedDuration,
3289 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003290 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3291 find_camera_metadata_ro_entry(request,
3292 ANDROID_CONTROL_AE_MODE,
3293 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003294 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003295
3296 switch (e.data.u8[0]) {
3297 case ANDROID_CONTROL_AE_MODE_OFF:
3298 find_camera_metadata_ro_entry(request,
3299 ANDROID_SENSOR_EXPOSURE_TIME,
3300 &e);
3301 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003302 expectedDurationInfo.minDuration = e.data.i64[0];
3303 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003304 }
3305 find_camera_metadata_ro_entry(request,
3306 ANDROID_SENSOR_FRAME_DURATION,
3307 &e);
3308 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003309 expectedDurationInfo.minDuration =
3310 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3311 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003312 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003313 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003314 break;
3315 default:
3316 find_camera_metadata_ro_entry(request,
3317 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3318 &e);
3319 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003320 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3321 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003322 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003323 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003324 break;
3325 }
3326
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003327 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003328}
3329
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003330bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3331 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3332 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3333 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3334 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3335 return true;
3336 }
3337
3338 return false;
3339}
3340
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003341void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3342 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003343 camera_capture_request_t& halRequest = nextRequest.halRequest;
3344 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003345 Mutex::Autolock al(mLatestRequestMutex);
3346
Shuzhen Wang83bff122020-11-20 15:51:39 -08003347 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003348 mLatestRequest.acquire(cloned);
3349
3350 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003351 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3352 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3353 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003354 CameraMetadata(cloned));
3355 }
3356
3357 sp<Camera3Device> parent = mParent.promote();
3358 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003359 int32_t inputStreamId = -1;
3360 if (halRequest.input_buffer != nullptr) {
3361 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3362 }
3363
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003364 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003365 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003366 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3367 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003368 }
3369 }
3370
Shuzhen Wang83bff122020-11-20 15:51:39 -08003371 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003372 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003373 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003374 }
3375
Shuzhen Wang83bff122020-11-20 15:51:39 -08003376 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003377}
3378
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003379bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3380 ATRACE_CALL();
3381 bool updatesDetected = false;
3382
Emilian Peev4ec17882019-01-24 17:16:58 -08003383 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003384 for (auto tag : mSessionParamKeys) {
3385 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003386 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003387
3388 if (entry.count > 0) {
3389 bool isDifferent = false;
3390 if (lastEntry.count > 0) {
3391 // Have a last value, compare to see if changed
3392 if (lastEntry.type == entry.type &&
3393 lastEntry.count == entry.count) {
3394 // Same type and count, compare values
3395 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3396 size_t entryBytes = bytesPerValue * lastEntry.count;
3397 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3398 if (cmp != 0) {
3399 isDifferent = true;
3400 }
3401 } else {
3402 // Count or type has changed
3403 isDifferent = true;
3404 }
3405 } else {
3406 // No last entry, so always consider to be different
3407 isDifferent = true;
3408 }
3409
3410 if (isDifferent) {
3411 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003412 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3413 updatesDetected = true;
3414 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003415 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003416 }
3417 } else if (lastEntry.count > 0) {
3418 // Value has been removed
3419 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003420 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003421 updatesDetected = true;
3422 }
3423 }
3424
Emilian Peev4ec17882019-01-24 17:16:58 -08003425 bool reconfigureRequired;
3426 if (updatesDetected) {
3427 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3428 updatedParams);
3429 mLatestSessionParams = updatedParams;
3430 } else {
3431 reconfigureRequired = false;
3432 }
3433
3434 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003435}
3436
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003437bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003438 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003439 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003440 // Any function called from threadLoop() must not hold mInterfaceLock since
3441 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3442 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003443
3444 // Handle paused state.
3445 if (waitIfPaused()) {
3446 return true;
3447 }
3448
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003449 // Wait for the next batch of requests.
3450 waitForNextRequestBatch();
3451 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003452 return true;
3453 }
3454
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003455 // Get the latest request ID, if any
3456 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003457 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003458 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003459 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003460 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003461 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003462 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3463 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003464 }
3465
Emilian Peevb9f83812023-03-17 17:13:07 -07003466 for (size_t i = 0; i < mNextRequests.size(); i++) {
3467 auto& nextRequest = mNextRequests.editItemAt(i);
3468 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3469 // Do not override rotate&crop for stream configurations that include
3470 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3471 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3472 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3473 overrideAutoRotateAndCrop(captureRequest);
3474 }
3475
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003476 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3477 // or a single request from streaming or burst. In either case the first element
3478 // should contain the latest camera settings that we need to check for any session
3479 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003480 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003481 res = OK;
3482
3483 //Input stream buffers are already acquired at this point so an input stream
3484 //will not be able to move to idle state unless we force it.
3485 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3486 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3487 if (res != OK) {
3488 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3489 cleanUpFailedRequests(/*sendRequestError*/ false);
3490 return false;
3491 }
3492 }
3493
3494 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003495 sp<Camera3Device> parent = mParent.promote();
3496 if (parent != nullptr) {
3497 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003498 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003499 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003500
3501 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3502 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3503 if (res != OK) {
3504 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3505 cleanUpFailedRequests(/*sendRequestError*/ false);
3506 return false;
3507 }
3508 }
3509 }
3510 }
3511
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003512 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003513 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003514 if (res == TIMED_OUT) {
3515 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003516 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003517 // Check if any stream is abandoned.
3518 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003519 return true;
3520 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003521 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003522 return false;
3523 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003524
Zhijun Hecc27e112013-10-03 16:12:43 -07003525 // Inform waitUntilRequestProcessed thread of a new request ID
3526 {
3527 Mutex::Autolock al(mLatestRequestMutex);
3528
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003529 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003530 mLatestRequestSignal.signal();
3531 }
3532
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003533 // Submit a batch of requests to HAL.
3534 // Use flush lock only when submitting multilple requests in a batch.
3535 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3536 // which may take a long time to finish so synchronizing flush() and
3537 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3538 // For now, only synchronize for high speed recording and we should figure something out for
3539 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003540 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003541
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003542 if (useFlushLock) {
3543 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003544 }
3545
Zhijun Hef0645c12016-08-02 00:58:11 -07003546 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003547 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003548
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003549 sp<Camera3Device> parent = mParent.promote();
3550 if (parent != nullptr) {
3551 parent->mRequestBufferSM.onSubmittingRequest();
3552 }
3553
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003554 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003555 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003556 submitRequestSuccess = sendRequestsBatch();
3557
Shuzhen Wang686f6442017-06-20 16:16:04 -07003558 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3559 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003560
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003561 if (useFlushLock) {
3562 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003563 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003564
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003565 // Unset as current request
3566 {
3567 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003568 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003569 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003570 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003571
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003572 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003573}
3574
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003575status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3576 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3577 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3578 ANDROID_SCALER_CROP_REGION_SET};
3579 if (request == nullptr) {
3580 ALOGE("%s request metadata nullptr", __FUNCTION__);
3581 return BAD_VALUE;
3582 }
3583 status_t res = OK;
3584 for (const auto &key : kFwkOnlyRegionKeys) {
3585 if (request->exists(key)) {
3586 res = request->erase(key);
3587 if (res != OK) {
3588 return res;
3589 }
3590 }
3591 }
3592 return OK;
3593}
3594
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003595status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003596 ATRACE_CALL();
3597
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003598 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003599 for (size_t i = 0; i < mNextRequests.size(); i++) {
3600 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003601 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003602 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3603 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003604
3605 // Prepare a request to HAL
3606 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3607
3608 // Insert any queued triggers (before metadata is locked)
3609 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003610 if (res < 0) {
3611 SET_ERR("RequestThread: Unable to insert triggers "
3612 "(capture request %d, HAL device: %s (%d)",
3613 halRequest->frame_number, strerror(-res), res);
3614 return INVALID_OPERATION;
3615 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003616
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003617 int triggerCount = res;
3618 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3619 mPrevTriggers = triggerCount;
3620
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003621 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003622
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003623 // If the request is the same as last, or we had triggers now or last time or
3624 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003625 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003626 (mPrevRequest != captureRequest || triggersMixedIn ||
Emilian Peevb9f83812023-03-17 17:13:07 -07003627 captureRequest->mRotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003628 // Request settings are all the same within one batch, so only treat the first
3629 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003630 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003631 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003632 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003633 /**
3634 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003635 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003636 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003637 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003638 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003639 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003640 "(capture request %d, HAL device: %s (%d)",
3641 halRequest->frame_number, strerror(-res), res);
3642 return INVALID_OPERATION;
3643 }
3644
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003645 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003646 sp<Camera3Device> parent = mParent.promote();
3647 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003648 List<PhysicalCameraSettings>::iterator it;
3649 for (it = captureRequest->mSettingsList.begin();
3650 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003651 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3652 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003653 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3654 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3655 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3656 res);
3657 return INVALID_OPERATION;
3658 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003659 continue;
3660 }
3661
3662 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3663 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3664 updateCaptureRequest(&(it->metadata));
3665 if (res != OK) {
3666 SET_ERR("RequestThread: Unable to correct capture requests "
3667 "for scaler crop region and metering regions for request "
3668 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3669 res);
3670 return INVALID_OPERATION;
3671 }
3672 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003673 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3674 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3675 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3676 res);
3677 return INVALID_OPERATION;
3678 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003679 }
3680 }
3681
3682 // Correct metadata regions for distortion correction if enabled
3683 for (it = captureRequest->mSettingsList.begin();
3684 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003685 if (parent->mDistortionMappers.find(it->cameraId) ==
3686 parent->mDistortionMappers.end()) {
3687 continue;
3688 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003689
3690 if (!captureRequest->mDistortionCorrectionUpdated) {
3691 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3692 &(it->metadata));
3693 if (res != OK) {
3694 SET_ERR("RequestThread: Unable to correct capture requests "
3695 "for lens distortion for request %d: %s (%d)",
3696 halRequest->frame_number, strerror(-res), res);
3697 return INVALID_OPERATION;
3698 }
3699 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003700 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003701 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003702
3703 for (it = captureRequest->mSettingsList.begin();
3704 it != captureRequest->mSettingsList.end(); it++) {
3705 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3706 parent->mZoomRatioMappers.end()) {
3707 continue;
3708 }
3709
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003710 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003711 cameraIdsWithZoom.insert(it->cameraId);
3712 }
3713
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003714 if (!captureRequest->mZoomRatioUpdated) {
3715 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3716 &(it->metadata));
3717 if (res != OK) {
3718 SET_ERR("RequestThread: Unable to correct capture requests "
3719 "for zoom ratio for request %d: %s (%d)",
3720 halRequest->frame_number, strerror(-res), res);
3721 return INVALID_OPERATION;
3722 }
3723 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003724 }
3725 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003726 if (captureRequest->mRotateAndCropAuto &&
3727 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003728 for (it = captureRequest->mSettingsList.begin();
3729 it != captureRequest->mSettingsList.end(); it++) {
3730 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3731 if (mapper != parent->mRotateAndCropMappers.end()) {
3732 res = mapper->second.updateCaptureRequest(&(it->metadata));
3733 if (res != OK) {
3734 SET_ERR("RequestThread: Unable to correct capture requests "
3735 "for rotate-and-crop for request %d: %s (%d)",
3736 halRequest->frame_number, strerror(-res), res);
3737 return INVALID_OPERATION;
3738 }
3739 }
3740 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003741 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003742 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003743 }
3744 }
3745
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003746 /**
3747 * The request should be presorted so accesses in HAL
3748 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3749 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003750 captureRequest->mSettingsList.begin()->metadata.sort();
3751 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003752 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003753 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003754 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3755
3756 IF_ALOGV() {
3757 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3758 find_camera_metadata_ro_entry(
3759 halRequest->settings,
3760 ANDROID_CONTROL_AF_TRIGGER,
3761 &e
3762 );
3763 if (e.count > 0) {
3764 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3765 __FUNCTION__,
3766 halRequest->frame_number,
3767 e.data.u8[0]);
3768 }
3769 }
3770 } else {
3771 // leave request.settings NULL to indicate 'reuse latest given'
3772 ALOGVV("%s: Request settings are REUSED",
3773 __FUNCTION__);
3774 }
3775
Emilian Peevaebbe412018-01-15 13:53:24 +00003776 if (captureRequest->mSettingsList.size() > 1) {
3777 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3778 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003779 if (newRequest) {
3780 halRequest->physcam_settings =
3781 new const camera_metadata* [halRequest->num_physcam_settings];
3782 } else {
3783 halRequest->physcam_settings = nullptr;
3784 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003785 auto it = ++captureRequest->mSettingsList.begin();
3786 size_t i = 0;
3787 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3788 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003789 if (newRequest) {
3790 it->metadata.sort();
3791 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3792 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003793 }
3794 }
3795
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003796 uint32_t totalNumBuffers = 0;
3797
3798 // Fill in buffers
3799 if (captureRequest->mInputStream != NULL) {
3800 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003801
3802 halRequest->input_width = captureRequest->mInputBufferSize.width;
3803 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003804 totalNumBuffers += 1;
3805 } else {
3806 halRequest->input_buffer = NULL;
3807 }
3808
Emilian Peevf4816702020-04-03 15:44:51 -07003809 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003810 captureRequest->mOutputStreams.size());
3811 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003812 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003813
3814 sp<Camera3Device> parent = mParent.promote();
3815 if (parent == NULL) {
3816 // Should not happen, and nowhere to send errors to, so just log it
3817 CLOGE("RequestThread: Parent is gone");
3818 return INVALID_OPERATION;
3819 }
3820 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3821
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003822 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003823 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003824 sp<Camera3OutputStreamInterface> outputStream =
3825 captureRequest->mOutputStreams.editItemAt(j);
3826 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003827
3828 // Prepare video buffers for high speed recording on the first video request.
3829 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3830 // Only try to prepare video stream on the first video request.
3831 mPrepareVideoStream = false;
3832
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003833 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3834 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003835 while (res == NOT_ENOUGH_DATA) {
3836 res = outputStream->prepareNextBuffer();
3837 }
3838 if (res != OK) {
3839 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3840 __FUNCTION__, strerror(-res), res);
3841 outputStream->cancelPrepare();
3842 }
3843 }
3844
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003845 std::vector<size_t> uniqueSurfaceIds;
3846 res = outputStream->getUniqueSurfaceIds(
3847 captureRequest->mOutputSurfaces[streamId],
3848 &uniqueSurfaceIds);
3849 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3850 if (res != OK && res != INVALID_OPERATION) {
3851 ALOGE("%s: failed to query stream %d unique surface IDs",
3852 __FUNCTION__, streamId);
3853 return res;
3854 }
3855 if (res == OK) {
3856 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3857 }
3858
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003859 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003860 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003861 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003862 return TIMED_OUT;
3863 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003864 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003865 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003866 buffer.stream = outputStream->asHalStream();
3867 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003868 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003869 buffer.acquire_fence = -1;
3870 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003871 // Mark the output stream as unpreparable to block clients from calling
3872 // 'prepare' after this request reaches CameraHal and before the respective
3873 // buffers are requested.
3874 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003875 } else {
3876 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3877 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003878 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003879 if (res != OK) {
3880 // Can't get output buffer from gralloc queue - this could be due to
3881 // abandoned queue or other consumer misbehavior, so not a fatal
3882 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003883 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003884 " %s (%d)", strerror(-res), res);
3885
3886 return TIMED_OUT;
3887 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003888 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003889
3890 {
3891 sp<Camera3Device> parent = mParent.promote();
3892 if (parent != nullptr) {
3893 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3894 for (const auto& settings : captureRequest->mSettingsList) {
3895 if ((streamCameraId.isEmpty() &&
3896 parent->getId() == settings.cameraId.c_str()) ||
3897 streamCameraId == settings.cameraId.c_str()) {
3898 outputStream->fireBufferRequestForFrameNumber(
3899 captureRequest->mResultExtras.frameNumber,
3900 settings.metadata);
3901 }
3902 }
3903 }
3904 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003905
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003906 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003907 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3908 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3909 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3910 } else if (!physicalCameraId.isEmpty()) {
3911 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003912 }
3913 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003914 }
3915 totalNumBuffers += halRequest->num_output_buffers;
3916
3917 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003918 // If this request list is for constrained high speed recording (not
3919 // preview), and the current request is not the last one in the batch,
3920 // do not send callback to the app.
3921 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003922 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003923 hasCallback = false;
3924 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003925 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003926 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003927 const camera_metadata_t* settings = halRequest->settings;
3928 bool shouldUnlockSettings = false;
3929 if (settings == nullptr) {
3930 shouldUnlockSettings = true;
3931 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3932 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003933 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3934 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003935 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003936 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3937 isStillCapture = true;
3938 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3939 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003940
Emilian Peevaf8416e2021-02-04 17:52:43 -08003941 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003942 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003943 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3944 isZslCapture = true;
3945 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003946 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003947 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003948 res = parent->registerInFlight(halRequest->frame_number,
3949 totalNumBuffers, captureRequest->mResultExtras,
3950 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003951 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003952 expectedDurationInfo.minDuration,
3953 expectedDurationInfo.maxDuration,
3954 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003955 requestedPhysicalCameras, isStillCapture, isZslCapture,
3956 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003957 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003958 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003959 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3960 ", burstId = %" PRId32 ".",
3961 __FUNCTION__,
3962 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3963 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003964
3965 if (shouldUnlockSettings) {
3966 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3967 }
3968
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003969 if (res != OK) {
3970 SET_ERR("RequestThread: Unable to register new in-flight request:"
3971 " %s (%d)", strerror(-res), res);
3972 return INVALID_OPERATION;
3973 }
3974 }
3975
3976 return OK;
3977}
3978
Igor Murashkin1e479c02013-09-06 16:55:14 -07003979CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003980 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003981 Mutex::Autolock al(mLatestRequestMutex);
3982
3983 ALOGV("RequestThread::%s", __FUNCTION__);
3984
3985 return mLatestRequest;
3986}
3987
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003988bool Camera3Device::RequestThread::isStreamPending(
3989 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003990 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003991 Mutex::Autolock l(mRequestLock);
3992
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003993 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003994 if (!nextRequest.submitted) {
3995 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3996 if (stream == s) return true;
3997 }
3998 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003999 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004000 }
4001
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004002 for (const auto& request : mRequestQueue) {
4003 for (const auto& s : request->mOutputStreams) {
4004 if (stream == s) return true;
4005 }
4006 if (stream == request->mInputStream) return true;
4007 }
4008
4009 for (const auto& request : mRepeatingRequests) {
4010 for (const auto& s : request->mOutputStreams) {
4011 if (stream == s) return true;
4012 }
4013 if (stream == request->mInputStream) return true;
4014 }
4015
4016 return false;
4017}
Jianing Weicb0652e2014-03-12 18:29:36 -07004018
Emilian Peev40ead602017-09-26 15:46:36 +01004019bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4020 ATRACE_CALL();
4021 Mutex::Autolock l(mRequestLock);
4022
4023 for (const auto& nextRequest : mNextRequests) {
4024 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4025 if (s.first == streamId) {
4026 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4027 if (it != s.second.end()) {
4028 return true;
4029 }
4030 }
4031 }
4032 }
4033
4034 for (const auto& request : mRequestQueue) {
4035 for (const auto& s : request->mOutputSurfaces) {
4036 if (s.first == streamId) {
4037 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4038 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004039 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004040 }
4041 }
4042 }
4043 }
4044
4045 for (const auto& request : mRepeatingRequests) {
4046 for (const auto& s : request->mOutputSurfaces) {
4047 if (s.first == streamId) {
4048 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4049 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004050 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004051 }
4052 }
4053 }
4054 }
4055
4056 return false;
4057}
4058
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004059void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4060 if (!mUseHalBufManager) {
4061 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4062 return;
4063 }
4064
4065 Mutex::Autolock pl(mPauseLock);
4066 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004067 mInterface->signalPipelineDrain(streamIds);
4068 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004069 }
4070 // If request thread is still busy, wait until paused then notify HAL
4071 mNotifyPipelineDrain = true;
4072 mStreamIdsToBeDrained = streamIds;
4073}
4074
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004075void Camera3Device::RequestThread::resetPipelineDrain() {
4076 Mutex::Autolock pl(mPauseLock);
4077 mNotifyPipelineDrain = false;
4078 mStreamIdsToBeDrained.clear();
4079}
4080
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004081void Camera3Device::RequestThread::clearPreviousRequest() {
4082 Mutex::Autolock l(mRequestLock);
4083 mPrevRequest.clear();
4084}
4085
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004086status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4087 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4088 ATRACE_CALL();
4089 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004090 mRotateAndCropOverride = rotateAndCropValue;
4091 return OK;
4092}
4093
Emilian Peeve23f1d92021-09-20 14:56:01 -07004094status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4095 ATRACE_CALL();
4096 Mutex::Autolock l(mTriggerMutex);
4097 mComposerOutput = composerSurfacePresent;
4098 return OK;
4099}
4100
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004101status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004102 ATRACE_CALL();
4103 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004104 if (muteMode != mCameraMute) {
4105 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004106 mCameraMuteChanged = true;
4107 }
4108 return OK;
4109}
4110
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004111nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004112 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004113 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004114 return mExpectedInflightDuration > kMinInflightDuration ?
4115 mExpectedInflightDuration : kMinInflightDuration;
4116}
4117
Emilian Peevaebbe412018-01-15 13:53:24 +00004118void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004119 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004120 if ((request == nullptr) || (halRequest == nullptr)) {
4121 ALOGE("%s: Invalid request!", __FUNCTION__);
4122 return;
4123 }
4124
4125 if (halRequest->num_physcam_settings > 0) {
4126 if (halRequest->physcam_id != nullptr) {
4127 delete [] halRequest->physcam_id;
4128 halRequest->physcam_id = nullptr;
4129 }
4130 if (halRequest->physcam_settings != nullptr) {
4131 auto it = ++(request->mSettingsList.begin());
4132 size_t i = 0;
4133 for (; it != request->mSettingsList.end(); it++, i++) {
4134 it->metadata.unlock(halRequest->physcam_settings[i]);
4135 }
4136 delete [] halRequest->physcam_settings;
4137 halRequest->physcam_settings = nullptr;
4138 }
4139 }
4140}
4141
Ravneet74cd3732022-03-30 05:33:03 +00004142status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4143 Mutex::Autolock il(mInterfaceLock);
4144 Mutex::Autolock l(mLock);
4145
4146 if (mCameraServiceWatchdog != NULL) {
4147 mCameraServiceWatchdog->setEnabled(enabled);
4148 }
4149
4150 return OK;
4151}
4152
Shuzhen Wangabe5ea12022-12-15 22:38:07 -08004153void Camera3Device::setStreamUseCaseOverrides(
4154 const std::vector<int64_t>& useCaseOverrides) {
4155 Mutex::Autolock il(mInterfaceLock);
4156 Mutex::Autolock l(mLock);
4157 mStreamUseCaseOverrides = useCaseOverrides;
4158}
4159
4160void Camera3Device::clearStreamUseCaseOverrides() {
4161 Mutex::Autolock il(mInterfaceLock);
4162 Mutex::Autolock l(mLock);
4163 mStreamUseCaseOverrides.clear();
4164}
4165
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004166void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4167 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004168 return;
4169 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004170
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004171 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004172 // Skip the ones that have been submitted successfully.
4173 if (nextRequest.submitted) {
4174 continue;
4175 }
4176
4177 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004178 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4179 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004180
4181 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004182 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004183 }
4184
Emilian Peevaebbe412018-01-15 13:53:24 +00004185 cleanupPhysicalSettings(captureRequest, halRequest);
4186
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004187 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004188 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004189 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4190 }
4191
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004192 // No output buffer can be returned when using HAL buffer manager
4193 if (!mUseHalBufManager) {
4194 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4195 //Buffers that failed processing could still have
4196 //valid acquire fence.
4197 int acquireFence = (*outputBuffers)[i].acquire_fence;
4198 if (0 <= acquireFence) {
4199 close(acquireFence);
4200 outputBuffers->editItemAt(i).acquire_fence = -1;
4201 }
Emilian Peevf4816702020-04-03 15:44:51 -07004202 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004203 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4204 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004205 /*timestampIncreasing*/true, std::vector<size_t> (),
4206 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004207 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004208 }
4209
4210 if (sendRequestError) {
4211 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004212 sp<NotificationListener> listener = mListener.promote();
4213 if (listener != NULL) {
4214 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004215 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004216 captureRequest->mResultExtras);
4217 }
4218 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004219
4220 // Remove yet-to-be submitted inflight request from inflightMap
4221 {
4222 sp<Camera3Device> parent = mParent.promote();
4223 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004224 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004225 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4226 if (idx >= 0) {
4227 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4228 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4229 parent->removeInFlightMapEntryLocked(idx);
4230 }
4231 }
4232 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004233 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004234
4235 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004236 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004237}
4238
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004239void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004240 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004241 // Optimized a bit for the simple steady-state case (single repeating
4242 // request), to avoid putting that request in the queue temporarily.
4243 Mutex::Autolock l(mRequestLock);
4244
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004245 assert(mNextRequests.empty());
4246
4247 NextRequest nextRequest;
4248 nextRequest.captureRequest = waitForNextRequestLocked();
4249 if (nextRequest.captureRequest == nullptr) {
4250 return;
4251 }
4252
Emilian Peevf4816702020-04-03 15:44:51 -07004253 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004254 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004255 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004256
4257 // Wait for additional requests
4258 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4259
4260 for (size_t i = 1; i < batchSize; i++) {
4261 NextRequest additionalRequest;
4262 additionalRequest.captureRequest = waitForNextRequestLocked();
4263 if (additionalRequest.captureRequest == nullptr) {
4264 break;
4265 }
4266
Emilian Peevf4816702020-04-03 15:44:51 -07004267 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004268 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004269 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004270 }
4271
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004272 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004273 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004274 mNextRequests.size(), batchSize);
4275 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004276 }
4277
4278 return;
4279}
4280
4281sp<Camera3Device::CaptureRequest>
4282 Camera3Device::RequestThread::waitForNextRequestLocked() {
4283 status_t res;
4284 sp<CaptureRequest> nextRequest;
4285
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004286 while (mRequestQueue.empty()) {
4287 if (!mRepeatingRequests.empty()) {
4288 // Always atomically enqueue all requests in a repeating request
4289 // list. Guarantees a complete in-sequence set of captures to
4290 // application.
4291 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004292 if (mFirstRepeating) {
4293 mFirstRepeating = false;
4294 } else {
4295 for (auto& request : requests) {
4296 // For repeating requests, override timestamp request using
4297 // the time a request is inserted into the request queue,
4298 // because the original repeating request will have an old
4299 // fixed timestamp.
4300 request->mRequestTimeNs = systemTime();
4301 }
4302 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004303 RequestList::const_iterator firstRequest =
4304 requests.begin();
4305 nextRequest = *firstRequest;
4306 mRequestQueue.insert(mRequestQueue.end(),
4307 ++firstRequest,
4308 requests.end());
4309 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004310
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004311 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004312
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004313 break;
4314 }
4315
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004316 if (!mRequestClearing) {
4317 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4318 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004319
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004320 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4321 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004322 Mutex::Autolock pl(mPauseLock);
4323 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004324 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004325 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004326 if (mNotifyPipelineDrain) {
4327 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4328 mNotifyPipelineDrain = false;
4329 mStreamIdsToBeDrained.clear();
4330 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004331 // Let the tracker know
4332 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4333 if (statusTracker != 0) {
4334 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4335 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004336 sp<Camera3Device> parent = mParent.promote();
4337 if (parent != nullptr) {
4338 parent->mRequestBufferSM.onRequestThreadPaused();
4339 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004340 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004341 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004342 // Stop waiting for now and let thread management happen
4343 return NULL;
4344 }
4345 }
4346
4347 if (nextRequest == NULL) {
4348 // Don't have a repeating request already in hand, so queue
4349 // must have an entry now.
4350 RequestList::iterator firstRequest =
4351 mRequestQueue.begin();
4352 nextRequest = *firstRequest;
4353 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004354 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4355 sp<NotificationListener> listener = mListener.promote();
4356 if (listener != NULL) {
4357 listener->notifyRequestQueueEmpty();
4358 }
4359 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004360 }
4361
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004362 // In case we've been unpaused by setPaused clearing mDoPause, need to
4363 // update internal pause state (capture/setRepeatingRequest unpause
4364 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004365 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004366 if (mPaused) {
4367 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4368 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4369 if (statusTracker != 0) {
4370 statusTracker->markComponentActive(mStatusId);
4371 }
4372 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004373 mPaused = false;
4374
4375 // Check if we've reconfigured since last time, and reset the preview
4376 // request if so. Can't use 'NULL request == repeat' across configure calls.
4377 if (mReconfigured) {
4378 mPrevRequest.clear();
4379 mReconfigured = false;
4380 }
4381
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004382 if (nextRequest != NULL) {
4383 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004384 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4385 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004386
4387 // Since RequestThread::clear() removes buffers from the input stream,
4388 // get the right buffer here before unlocking mRequestLock
4389 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004390 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4391 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004392 if (res != OK) {
4393 // Can't get input buffer from gralloc queue - this could be due to
4394 // disconnected queue or other producer misbehavior, so not a fatal
4395 // error
4396 ALOGE("%s: Can't get input buffer, skipping request:"
4397 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004398
4399 sp<NotificationListener> listener = mListener.promote();
4400 if (listener != NULL) {
4401 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004402 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004403 nextRequest->mResultExtras);
4404 }
4405 return NULL;
4406 }
4407 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004408 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004409
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004410 return nextRequest;
4411}
4412
4413bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004414 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004415 status_t res;
4416 Mutex::Autolock l(mPauseLock);
4417 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004418 if (mPaused == false) {
4419 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004420 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004421 if (mNotifyPipelineDrain) {
4422 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4423 mNotifyPipelineDrain = false;
4424 mStreamIdsToBeDrained.clear();
4425 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004426 // Let the tracker know
4427 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4428 if (statusTracker != 0) {
4429 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4430 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004431 sp<Camera3Device> parent = mParent.promote();
4432 if (parent != nullptr) {
4433 parent->mRequestBufferSM.onRequestThreadPaused();
4434 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004435 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004436
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004437 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004438 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004439 return true;
4440 }
4441 }
4442 // We don't set mPaused to false here, because waitForNextRequest needs
4443 // to further manage the paused state in case of starvation.
4444 return false;
4445}
4446
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004447void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004448 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004449 // With work to do, mark thread as unpaused.
4450 // If paused by request (setPaused), don't resume, to avoid
4451 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004452 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004453 Mutex::Autolock p(mPauseLock);
4454 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004455 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4456 if (mPaused) {
4457 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4458 if (statusTracker != 0) {
4459 statusTracker->markComponentActive(mStatusId);
4460 }
4461 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004462 mPaused = false;
4463 }
4464}
4465
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004466void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4467 sp<Camera3Device> parent = mParent.promote();
4468 if (parent != NULL) {
4469 va_list args;
4470 va_start(args, fmt);
4471
4472 parent->setErrorStateV(fmt, args);
4473
4474 va_end(args);
4475 }
4476}
4477
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004478status_t Camera3Device::RequestThread::insertTriggers(
4479 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004480 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004481 Mutex::Autolock al(mTriggerMutex);
4482
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004483 sp<Camera3Device> parent = mParent.promote();
4484 if (parent == NULL) {
4485 CLOGE("RequestThread: Parent is gone");
4486 return DEAD_OBJECT;
4487 }
4488
Emilian Peevaebbe412018-01-15 13:53:24 +00004489 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004490 size_t count = mTriggerMap.size();
4491
4492 for (size_t i = 0; i < count; ++i) {
4493 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004494 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004495
4496 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4497 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4498 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004499 if (isAeTrigger) {
4500 request->mResultExtras.precaptureTriggerId = triggerId;
4501 mCurrentPreCaptureTriggerId = triggerId;
4502 } else {
4503 request->mResultExtras.afTriggerId = triggerId;
4504 mCurrentAfTriggerId = triggerId;
4505 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004506 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004507 }
4508
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004509 camera_metadata_entry entry = metadata.find(tag);
4510
4511 if (entry.count > 0) {
4512 /**
4513 * Already has an entry for this trigger in the request.
4514 * Rewrite it with our requested trigger value.
4515 */
4516 RequestTrigger oldTrigger = trigger;
4517
4518 oldTrigger.entryValue = entry.data.u8[0];
4519
4520 mTriggerReplacedMap.add(tag, oldTrigger);
4521 } else {
4522 /**
4523 * More typical, no trigger entry, so we just add it
4524 */
4525 mTriggerRemovedMap.add(tag, trigger);
4526 }
4527
4528 status_t res;
4529
4530 switch (trigger.getTagType()) {
4531 case TYPE_BYTE: {
4532 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4533 res = metadata.update(tag,
4534 &entryValue,
4535 /*count*/1);
4536 break;
4537 }
4538 case TYPE_INT32:
4539 res = metadata.update(tag,
4540 &trigger.entryValue,
4541 /*count*/1);
4542 break;
4543 default:
4544 ALOGE("%s: Type not supported: 0x%x",
4545 __FUNCTION__,
4546 trigger.getTagType());
4547 return INVALID_OPERATION;
4548 }
4549
4550 if (res != OK) {
4551 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4552 ", value %d", __FUNCTION__, trigger.getTagName(),
4553 trigger.entryValue);
4554 return res;
4555 }
4556
4557 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4558 trigger.getTagName(),
4559 trigger.entryValue);
4560 }
4561
4562 mTriggerMap.clear();
4563
4564 return count;
4565}
4566
4567status_t Camera3Device::RequestThread::removeTriggers(
4568 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004569 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004570 Mutex::Autolock al(mTriggerMutex);
4571
Emilian Peevaebbe412018-01-15 13:53:24 +00004572 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004573
4574 /**
4575 * Replace all old entries with their old values.
4576 */
4577 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4578 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4579
4580 status_t res;
4581
4582 uint32_t tag = trigger.metadataTag;
4583 switch (trigger.getTagType()) {
4584 case TYPE_BYTE: {
4585 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4586 res = metadata.update(tag,
4587 &entryValue,
4588 /*count*/1);
4589 break;
4590 }
4591 case TYPE_INT32:
4592 res = metadata.update(tag,
4593 &trigger.entryValue,
4594 /*count*/1);
4595 break;
4596 default:
4597 ALOGE("%s: Type not supported: 0x%x",
4598 __FUNCTION__,
4599 trigger.getTagType());
4600 return INVALID_OPERATION;
4601 }
4602
4603 if (res != OK) {
4604 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4605 ", trigger value %d", __FUNCTION__,
4606 trigger.getTagName(), trigger.entryValue);
4607 return res;
4608 }
4609 }
4610 mTriggerReplacedMap.clear();
4611
4612 /**
4613 * Remove all new entries.
4614 */
4615 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4616 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4617 status_t res = metadata.erase(trigger.metadataTag);
4618
4619 if (res != OK) {
4620 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4621 ", trigger value %d", __FUNCTION__,
4622 trigger.getTagName(), trigger.entryValue);
4623 return res;
4624 }
4625 }
4626 mTriggerRemovedMap.clear();
4627
4628 return OK;
4629}
4630
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004631status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004632 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004633 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004634 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004635 status_t res;
4636
Emilian Peevaebbe412018-01-15 13:53:24 +00004637 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004638
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004639 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004640 // exists
4641 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4642 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4643 if (afTrigger.count > 0 &&
4644 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4645 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004646 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004647 if (res != OK) return res;
4648 }
4649
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004650 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004651 // if none already exists
4652 camera_metadata_entry pcTrigger =
4653 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4654 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4655 if (pcTrigger.count > 0 &&
4656 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4657 pcId.count == 0) {
4658 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004659 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004660 if (res != OK) return res;
4661 }
4662
4663 return OK;
4664}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004665
Emilian Peevb9f83812023-03-17 17:13:07 -07004666bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4667 ATRACE_CALL();
4668 Mutex::Autolock l(mTriggerMutex);
4669 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4670 this->mRotateAndCropOverride);
4671}
4672
4673bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4674 bool overrideToPortrait,
4675 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004676 ATRACE_CALL();
4677
Emilian Peevb9f83812023-03-17 17:13:07 -07004678 if (overrideToPortrait) {
4679 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
Austin Borger3560b7e2022-10-27 12:20:29 -07004680 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4681 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4682 &rotateAndCrop_u8, 1);
4683 return true;
4684 }
4685
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004686 if (request->mRotateAndCropAuto) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004687 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4688
4689 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4690 if (rotateAndCropEntry.count > 0) {
Emilian Peevb9f83812023-03-17 17:13:07 -07004691 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004692 return false;
4693 } else {
Emilian Peevb9f83812023-03-17 17:13:07 -07004694 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004695 return true;
4696 }
4697 } else {
Emilian Peevb9f83812023-03-17 17:13:07 -07004698 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4699 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004700 return true;
4701 }
4702 }
Emilian Peevb9f83812023-03-17 17:13:07 -07004703
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004704 return false;
4705}
4706
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004707bool Camera3Device::RequestThread::overrideTestPattern(
4708 const sp<CaptureRequest> &request) {
4709 ATRACE_CALL();
4710
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004711 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004712
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004713 Mutex::Autolock l(mTriggerMutex);
4714
4715 bool changed = false;
4716
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004717 // For a multi-camera, the physical cameras support the same set of
4718 // test pattern modes as the logical camera.
4719 for (auto& settings : request->mSettingsList) {
4720 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004721
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004722 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4723 int32_t testPatternData[4] = {
4724 settings.mOriginalTestPatternData[0],
4725 settings.mOriginalTestPatternData[1],
4726 settings.mOriginalTestPatternData[2],
4727 settings.mOriginalTestPatternData[3]
4728 };
4729 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4730 testPatternMode = mCameraMute;
4731 testPatternData[0] = 0;
4732 testPatternData[1] = 0;
4733 testPatternData[2] = 0;
4734 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004735 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004736
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004737 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4738 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4739 if (testPatternEntry.count > 0) {
4740 if (testPatternEntry.data.i32[0] != testPatternMode) {
4741 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004742 changed = true;
4743 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004744 } else if (supportTestPatternModeKey) {
4745 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4746 &testPatternMode, 1);
4747 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004748 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004749
4750 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4751 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4752 if (testPatternColor.count >= 4) {
4753 for (size_t i = 0; i < 4; i++) {
4754 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4755 testPatternColor.data.i32[i] = testPatternData[i];
4756 changed = true;
4757 }
4758 }
4759 } else if (supportTestPatternDataKey) {
4760 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4761 testPatternData, 4);
4762 changed = true;
4763 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004764 }
4765
4766 return changed;
4767}
4768
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004769status_t Camera3Device::RequestThread::setHalInterface(
4770 sp<HalInterface> newHalInterface) {
4771 if (newHalInterface.get() == nullptr) {
4772 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4773 return DEAD_OBJECT;
4774 }
4775
4776 mInterface = newHalInterface;
4777
4778 return OK;
4779}
4780
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004781/**
4782 * PreparerThread inner class methods
4783 */
4784
4785Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004786 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004787 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004788}
4789
4790Camera3Device::PreparerThread::~PreparerThread() {
4791 Thread::requestExitAndWait();
4792 if (mCurrentStream != nullptr) {
4793 mCurrentStream->cancelPrepare();
4794 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4795 mCurrentStream.clear();
4796 }
4797 clear();
4798}
4799
Ruben Brunkc78ac262015-08-13 17:58:46 -07004800status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004801 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004802 status_t res;
4803
4804 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004805 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004806
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004807 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004808 if (res == OK) {
4809 // No preparation needed, fire listener right off
4810 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004811 if (listener != NULL) {
4812 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004813 }
4814 return OK;
4815 } else if (res != NOT_ENOUGH_DATA) {
4816 return res;
4817 }
4818
4819 // Need to prepare, start up thread if necessary
4820 if (!mActive) {
4821 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4822 // isn't running
4823 Thread::requestExitAndWait();
4824 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4825 if (res != OK) {
4826 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004827 if (listener != NULL) {
4828 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004829 }
4830 return res;
4831 }
4832 mCancelNow = false;
4833 mActive = true;
4834 ALOGV("%s: Preparer stream started", __FUNCTION__);
4835 }
4836
4837 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004838 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004839 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4840
4841 return OK;
4842}
4843
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004844void Camera3Device::PreparerThread::pause() {
4845 ATRACE_CALL();
4846
4847 Mutex::Autolock l(mLock);
4848
4849 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4850 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4851 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4852 int currentMaxCount = mCurrentMaxCount;
4853 mPendingStreams.clear();
4854 mCancelNow = true;
4855 while (mActive) {
4856 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4857 if (res == TIMED_OUT) {
4858 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4859 return;
4860 } else if (res != OK) {
4861 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4862 return;
4863 }
4864 }
4865
4866 //Check whether the prepare thread was able to complete the current
4867 //stream. In case work is still pending emplace it along with the rest
4868 //of the streams in the pending list.
4869 if (currentStream != nullptr) {
4870 if (!mCurrentPrepareComplete) {
4871 pendingStreams.emplace(currentMaxCount, currentStream);
4872 }
4873 }
4874
4875 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4876 for (const auto& it : mPendingStreams) {
4877 it.second->cancelPrepare();
4878 }
4879}
4880
4881status_t Camera3Device::PreparerThread::resume() {
4882 ATRACE_CALL();
4883 status_t res;
4884
4885 Mutex::Autolock l(mLock);
4886 sp<NotificationListener> listener = mListener.promote();
4887
4888 if (mActive) {
4889 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4890 return NO_INIT;
4891 }
4892
4893 auto it = mPendingStreams.begin();
4894 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004895 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004896 if (res == OK) {
4897 if (listener != NULL) {
4898 listener->notifyPrepared(it->second->getId());
4899 }
4900 it = mPendingStreams.erase(it);
4901 } else if (res != NOT_ENOUGH_DATA) {
4902 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4903 res, strerror(-res));
4904 it = mPendingStreams.erase(it);
4905 } else {
4906 it++;
4907 }
4908 }
4909
4910 if (mPendingStreams.empty()) {
4911 return OK;
4912 }
4913
4914 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4915 if (res != OK) {
4916 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4917 __FUNCTION__, res, strerror(-res));
4918 return res;
4919 }
4920 mCancelNow = false;
4921 mActive = true;
4922 ALOGV("%s: Preparer stream started", __FUNCTION__);
4923
4924 return OK;
4925}
4926
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004927status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004928 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004929 Mutex::Autolock l(mLock);
4930
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004931 for (const auto& it : mPendingStreams) {
4932 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004933 }
4934 mPendingStreams.clear();
4935 mCancelNow = true;
4936
4937 return OK;
4938}
4939
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004940void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004941 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004942 Mutex::Autolock l(mLock);
4943 mListener = listener;
4944}
4945
4946bool Camera3Device::PreparerThread::threadLoop() {
4947 status_t res;
4948 {
4949 Mutex::Autolock l(mLock);
4950 if (mCurrentStream == nullptr) {
4951 // End thread if done with work
4952 if (mPendingStreams.empty()) {
4953 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4954 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4955 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4956 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004957 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004958 return false;
4959 }
4960
4961 // Get next stream to prepare
4962 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004963 mCurrentStream = it->second;
4964 mCurrentMaxCount = it->first;
4965 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004966 mPendingStreams.erase(it);
4967 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4968 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4969 } else if (mCancelNow) {
4970 mCurrentStream->cancelPrepare();
4971 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4972 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4973 mCurrentStream.clear();
4974 mCancelNow = false;
4975 return true;
4976 }
4977 }
4978
4979 res = mCurrentStream->prepareNextBuffer();
4980 if (res == NOT_ENOUGH_DATA) return true;
4981 if (res != OK) {
4982 // Something bad happened; try to recover by cancelling prepare and
4983 // signalling listener anyway
4984 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4985 mCurrentStream->getId(), res, strerror(-res));
4986 mCurrentStream->cancelPrepare();
4987 }
4988
4989 // This stream has finished, notify listener
4990 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004991 sp<NotificationListener> listener = mListener.promote();
4992 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004993 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4994 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004995 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004996 }
4997
4998 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4999 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005000 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005001
5002 return true;
5003}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005004
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005005status_t Camera3Device::RequestBufferStateMachine::initialize(
5006 sp<camera3::StatusTracker> statusTracker) {
5007 if (statusTracker == nullptr) {
5008 ALOGE("%s: statusTracker is null", __FUNCTION__);
5009 return BAD_VALUE;
5010 }
5011
5012 std::lock_guard<std::mutex> lock(mLock);
5013 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005014 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005015 return OK;
5016}
5017
5018bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5019 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005020 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005021 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005022 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005023 return true;
5024 }
5025 return false;
5026}
5027
5028void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5029 std::lock_guard<std::mutex> lock(mLock);
5030 if (!mRequestBufferOngoing) {
5031 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5032 return;
5033 }
5034 mRequestBufferOngoing = false;
5035 if (mStatus == RB_STATUS_PENDING_STOP) {
5036 checkSwitchToStopLocked();
5037 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005038 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005039}
5040
5041void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5042 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005043 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005044 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005045 return;
5046}
5047
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005048void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005049 std::lock_guard<std::mutex> lock(mLock);
5050 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005051 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5052 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005053 mInflightMapEmpty = false;
5054 if (mStatus == RB_STATUS_STOPPED) {
5055 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005056 }
5057 return;
5058}
5059
5060void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5061 std::lock_guard<std::mutex> lock(mLock);
5062 mRequestThreadPaused = true;
5063 if (mStatus == RB_STATUS_PENDING_STOP) {
5064 checkSwitchToStopLocked();
5065 }
5066 return;
5067}
5068
5069void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5070 std::lock_guard<std::mutex> lock(mLock);
5071 mInflightMapEmpty = true;
5072 if (mStatus == RB_STATUS_PENDING_STOP) {
5073 checkSwitchToStopLocked();
5074 }
5075 return;
5076}
5077
5078void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5079 std::lock_guard<std::mutex> lock(mLock);
5080 if (!checkSwitchToStopLocked()) {
5081 mStatus = RB_STATUS_PENDING_STOP;
5082 }
5083 return;
5084}
5085
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005086bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5087 std::lock_guard<std::mutex> lock(mLock);
5088 if (mRequestBufferOngoing) {
5089 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5090 __FUNCTION__);
5091 return false;
5092 }
5093 mSwitchedToOffline = true;
5094 mInflightMapEmpty = true;
5095 mRequestThreadPaused = true;
5096 mStatus = RB_STATUS_STOPPED;
5097 return true;
5098}
5099
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005100void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5101 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5102 if (statusTracker != nullptr) {
5103 if (active) {
5104 statusTracker->markComponentActive(mRequestBufferStatusId);
5105 } else {
5106 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5107 }
5108 }
5109}
5110
5111bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5112 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5113 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005114 return true;
5115 }
5116 return false;
5117}
5118
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005119bool Camera3Device::startRequestBuffer() {
5120 return mRequestBufferSM.startRequestBuffer();
5121}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005122
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005123void Camera3Device::endRequestBuffer() {
5124 mRequestBufferSM.endRequestBuffer();
5125}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005126
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005127nsecs_t Camera3Device::getWaitDuration() {
5128 return kBaseGetBufferWait + getExpectedInFlightDuration();
5129}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005130
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005131void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5132 mInterface->getInflightBufferKeys(out);
5133}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005134
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005135void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5136 mInterface->getInflightRequestBufferKeys(out);
5137}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005138
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005139std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5140 std::vector<sp<Camera3StreamInterface>> ret;
5141 bool hasInputStream = mInputStream != nullptr;
5142 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5143 if (hasInputStream) {
5144 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005145 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005146 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5147 ret.push_back(mOutputStreams[i]);
5148 }
5149 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5150 ret.push_back(mDeletedStreams[i]);
5151 }
5152 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005153}
5154
Emilian Peevcc0b7952020-01-07 13:54:47 -08005155void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5156 ATRACE_CALL();
5157
5158 if (offlineStreamIds == nullptr) {
5159 return;
5160 }
5161
5162 Mutex::Autolock il(mInterfaceLock);
5163
5164 auto streamIds = mOutputStreams.getStreamIds();
5165 bool hasInputStream = mInputStream != nullptr;
5166 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5167 offlineStreamIds->push_back(mInputStream->getId());
5168 }
5169
5170 for (const auto & streamId : streamIds) {
5171 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5172 // Streams that use the camera buffer manager are currently not supported in
5173 // offline mode
5174 if (stream->getOfflineProcessingSupport() &&
5175 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5176 offlineStreamIds->push_back(streamId);
5177 }
5178 }
5179}
5180
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005181status_t Camera3Device::setRotateAndCropAutoBehavior(
5182 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5183 ATRACE_CALL();
5184 Mutex::Autolock il(mInterfaceLock);
5185 Mutex::Autolock l(mLock);
5186 if (mRequestThread == nullptr) {
5187 return INVALID_OPERATION;
5188 }
Emilian Peevb9f83812023-03-17 17:13:07 -07005189 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5190 return BAD_VALUE;
5191 }
5192 mRotateAndCropOverride = rotateAndCropValue;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005193 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5194}
5195
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005196bool Camera3Device::supportsCameraMute() {
5197 Mutex::Autolock il(mInterfaceLock);
5198 Mutex::Autolock l(mLock);
5199
5200 return mSupportCameraMute;
5201}
5202
5203status_t Camera3Device::setCameraMute(bool enabled) {
5204 ATRACE_CALL();
5205 Mutex::Autolock il(mInterfaceLock);
5206 Mutex::Autolock l(mLock);
5207
5208 if (mRequestThread == nullptr || !mSupportCameraMute) {
5209 return INVALID_OPERATION;
5210 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005211 int32_t muteMode =
5212 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5213 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5214 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5215 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005216}
5217
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005218status_t Camera3Device::injectCamera(const String8& injectedCamId,
5219 sp<CameraProviderManager> manager) {
5220 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5221 ATRACE_CALL();
5222 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005223 // When the camera device is active, injectCamera() and stopInjection() will call
5224 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5225 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5226 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5227 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5228 // waitUntilStateThenRelock().
5229 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005230
5231 status_t res = NO_ERROR;
5232 if (mInjectionMethods->isInjecting()) {
5233 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5234 return OK;
5235 } else {
5236 res = mInjectionMethods->stopInjection();
5237 if (res != OK) {
5238 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5239 __FUNCTION__, res);
5240 return res;
5241 }
5242 }
5243 }
5244
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005245 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005246 if (res != OK) {
5247 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5248 __FUNCTION__, res);
5249 return res;
5250 }
5251
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005252 // When the second display of android is cast to the remote device, and the opened camera is
5253 // also cast to the second display, in this case, because the camera has configured the streams
5254 // at this time, we can directly call injectCamera() to replace the internal camera with
5255 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005256 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5257 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5258
5259 camera3::camera_stream_configuration injectionConfig;
5260 std::vector<uint32_t> injectionBufferSizes;
5261 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5262 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5263 || injectionBufferSizes.size() <= 0) {
5264 ALOGE("Failed to inject camera due to abandoned configuration! "
5265 "mOperatingMode: %d injectionConfig.num_streams: %d "
5266 "injectionBufferSizes.size(): %zu", mOperatingMode,
5267 injectionConfig.num_streams, injectionBufferSizes.size());
5268 return DEAD_OBJECT;
5269 }
5270
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005271 res = mInjectionMethods->injectCamera(
5272 injectionConfig, injectionBufferSizes);
5273 if (res != OK) {
5274 ALOGE("Can't finish inject camera process!");
5275 return res;
5276 }
5277 }
5278
5279 return OK;
5280}
5281
5282status_t Camera3Device::stopInjection() {
5283 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5284 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005285 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005286 return mInjectionMethods->stopInjection();
5287}
5288
Shuzhen Wangabe5ea12022-12-15 22:38:07 -08005289void Camera3Device::overrideStreamUseCaseLocked() {
5290 if (mStreamUseCaseOverrides.size() == 0) {
5291 return;
5292 }
5293
5294 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5295 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5296 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5297 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5298 outputStreamsIndices[i] = i;
5299 }
5300
5301 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5302 [&](int a, int b) -> bool {
5303
5304 auto formatScore = [](int format) {
5305 switch (format) {
5306 case HAL_PIXEL_FORMAT_BLOB:
5307 return 4;
5308 case HAL_PIXEL_FORMAT_RAW16:
5309 case HAL_PIXEL_FORMAT_RAW10:
5310 case HAL_PIXEL_FORMAT_RAW12:
5311 return 3;
5312 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5313 return 2;
5314 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5315 return 1;
5316 default:
5317 return 0;
5318 }
5319 };
5320
5321 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5322 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5323 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5324 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5325 if (sizeA > sizeB ||
5326 (sizeA == sizeB && formatAScore >= formatBScore)) {
5327 return true;
5328 } else {
5329 return false;
5330 }
5331 });
5332
5333 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5334 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5335 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5336 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5337 }
5338}
5339
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005340}; // namespace android