blob: c99c9c60c3e98a23b783598039b41ebba0f4b112 [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"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080068#include <tuple>
69
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080070using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080071using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072
73namespace android {
74
Austin Borger74fca042022-05-23 12:41:21 -070075Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
76 const String8 &id, bool overrideForPerfClass, bool legacyClient):
77 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
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),
98 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800101 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102}
103
104Camera3Device::~Camera3Device()
105{
106 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800107 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700108 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109}
110
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800111const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800112 return mId;
113}
114
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115status_t Camera3Device::initializeCommonLocked() {
116
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700117 /** Start up status tracker thread */
118 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800119 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700120 if (res != OK) {
121 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
122 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700124 mStatusTracker.clear();
125 return res;
126 }
127
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700128 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700129 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700130
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700131 if (mUseHalBufManager) {
132 res = mRequestBufferSM.initialize(mStatusTracker);
133 if (res != OK) {
134 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
135 strerror(-res), res);
136 mInterface->close();
137 mStatusTracker.clear();
138 return res;
139 }
140 }
141
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800142 /** Create buffer manager */
143 mBufferManager = new Camera3BufferManager();
144
145 Vector<int32_t> sessionParamKeys;
146 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
147 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
148 if (sessionKeysEntry.count > 0) {
149 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
150 }
151
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700152 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
153 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
154 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
155 if (availableTestPatternModes.data.i32[i] ==
156 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
157 mSupportCameraMute = true;
158 mSupportTestPatternSolidColor = true;
159 break;
160 } else if (availableTestPatternModes.data.i32[i] ==
161 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
162 mSupportCameraMute = true;
163 mSupportTestPatternSolidColor = false;
164 }
165 }
166
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700167 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800168 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700169 this, mStatusTracker, mInterface, sessionParamKeys,
170 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800171 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800172 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700173 SET_ERR_L("Unable to start request queue thread: %s (%d)",
174 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800175 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800177 return res;
178 }
179
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700180 mPreparerThread = new PreparerThread();
181
Ruben Brunk183f0562015-08-12 12:55:02 -0700182 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400184 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700185 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800187 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800189 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800190 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800191 camera_metadata_entry timestampSource =
192 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
193 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
194 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800195 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800196 }
197
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700198 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100199 camera_metadata_entry partialResultsCount =
200 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
201 if (partialResultsCount.count > 0) {
202 mNumPartialResults = partialResultsCount.data.i32[0];
203 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700204 }
205
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800206 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
207 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700208 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700209 if (res != OK) {
210 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
211 return res;
212 }
213 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800214
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800215 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800216 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800217
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700218 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
219 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
220 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
221 }
222
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800223 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
224 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
225 }
226
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800227 // Hidl/AidlCamera3DeviceInjectionMethods
228 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800229
Ravneetaeb20dc2022-03-30 05:33:03 +0000230 /** Start watchdog thread */
231 mCameraServiceWatchdog = new CameraServiceWatchdog();
Austin Borger7b129542022-06-09 13:23:06 -0700232 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
233 if (res != OK) {
234 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
235 strerror(-res), res);
236 return res;
237 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000238
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 return OK;
240}
241
242status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700243 return disconnectImpl();
244}
245
246status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700248 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800249
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700251 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800253 Mutex::Autolock il(mInterfaceLock);
254 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000259 if (mRequestThread != NULL) {
260 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
261 res = mRequestThread->clear();
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800262 if (res != OK) {
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000263 SET_ERR_L("Can't stop streaming");
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800264 // Continue to close device even in case of error
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
269 maxExpectedDuration);
270 // Continue to close device even in case of error
271 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800272 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 }
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000274 // Signal to request thread that we're not expecting any
275 // more requests. This will be true since once we're in
276 // disconnect and we've cleared off the request queue, the
277 // request thread can't receive any new requests through
278 // binder calls - since disconnect holds
279 // mBinderSerialization lock.
280 mRequestThread->setRequestClearing();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800283 if (mStatus == STATUS_ERROR) {
284 CLOGE("Shutting down in an error state");
285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800287 if (mStatusTracker != NULL) {
288 mStatusTracker->requestExit();
289 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800291 if (mRequestThread != NULL) {
292 mRequestThread->requestExit();
293 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800295 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
296 for (size_t i = 0; i < mOutputStreams.size(); i++) {
297 streams.push_back(mOutputStreams[i]);
298 }
299 if (mInputStream != nullptr) {
300 streams.push_back(mInputStream);
301 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700302 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800304 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
305 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
307 // HAL may be in a bad state, so waiting for request thread
308 // (which may be stuck in the HAL processCaptureRequest call)
309 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800310 // give up mInterfaceLock here and then lock it again. Could this lead
311 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312 mRequestThread->join();
313 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800315 Mutex::Autolock il(mInterfaceLock);
316 if (mStatusTracker != NULL) {
317 mStatusTracker->join();
318 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800320 if (mInjectionMethods->isInjecting()) {
321 mInjectionMethods->stopInjection();
322 }
323
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800324 HalInterface* interface;
325 {
326 Mutex::Autolock l(mLock);
327 mRequestThread.clear();
328 Mutex::Autolock stLock(mTrackerLock);
329 mStatusTracker.clear();
330 interface = mInterface.get();
331 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700332
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800333 // Call close without internal mutex held, as the HAL close may need to
334 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000335 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700336
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800337 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800339 {
340 Mutex::Autolock l(mLock);
341 mInterface->clear();
342 mOutputStreams.clear();
343 mInputStream.clear();
344 mDeletedStreams.clear();
345 mBufferManager.clear();
346 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
347 }
348
349 for (auto& weakStream : streams) {
350 sp<Camera3StreamInterface> stream = weakStream.promote();
351 if (stream != nullptr) {
352 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
353 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
354 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700355 }
356 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700357 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000358
359 if (mCameraServiceWatchdog != NULL) {
360 mCameraServiceWatchdog->requestExit();
361 mCameraServiceWatchdog.clear();
362 }
363
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700364 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800365}
366
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700367// For dumping/debugging only -
368// try to acquire a lock a few times, eventually give up to proceed with
369// debug/dump operations
370bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
371 bool gotLock = false;
372 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
373 if (lock.tryLock() == NO_ERROR) {
374 gotLock = true;
375 break;
376 } else {
377 usleep(kDumpSleepDuration);
378 }
379 }
380 return gotLock;
381}
382
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800383nsecs_t Camera3Device::getMonoToBoottimeOffset() {
384 // try three times to get the clock offset, choose the one
385 // with the minimum gap in measurements.
386 const int tries = 3;
387 nsecs_t bestGap, measured;
388 for (int i = 0; i < tries; ++i) {
389 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
390 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
391 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
392 const nsecs_t gap = tmono2 - tmono;
393 if (i == 0 || gap < bestGap) {
394 bestGap = gap;
395 measured = tbase - ((tmono + tmono2) >> 1);
396 }
397 }
398 return measured;
399}
400
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800401ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
402 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700403 // Get max jpeg size (area-wise) for default sensor pixel mode
404 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800405 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700406 /*isUltraHighResolutionSensor*/false);
407 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
408 // not ultra high res sensor
409 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800410 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700411 /*isUltraHighResolution*/true);
412 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800413 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
414 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700415 return BAD_VALUE;
416 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700417 bool useMaxSensorPixelModeThreshold = false;
418 if (uhrMaxJpegResolution.width != 0 &&
419 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
420 // Use the ultra high res max jpeg size and max jpeg buffer size
421 useMaxSensorPixelModeThreshold = true;
422 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700423
Zhijun Hef7da0962014-04-24 13:27:56 -0700424 // Get max jpeg buffer size
425 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800426 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700427 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800428 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
429 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700430 return BAD_VALUE;
431 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700432 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700433
434 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
435 if (useMaxSensorPixelModeThreshold) {
436 maxJpegBufferSize =
437 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
438 maxDefaultJpegResolution, maxJpegBufferSize);
439 chosenMaxJpegResolution = uhrMaxJpegResolution;
440 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800441 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700442
443 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700444 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700445 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800446 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
447 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700448 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800449 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
450 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700452 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700453 return jpegBufferSize;
454}
455
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800456ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700457 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800458 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700459 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800460 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
461 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700462 return BAD_VALUE;
463 }
464 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
465 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
466 return maxBytesForPointCloud;
467}
468
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800469ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
470 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 const int PER_CONFIGURATION_SIZE = 3;
472 const int WIDTH_OFFSET = 0;
473 const int HEIGHT_OFFSET = 1;
474 const int SIZE_OFFSET = 2;
475 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800476 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800477 camera3::SessionConfigurationUtils::getAppropriateModeTag(
478 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
479 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800480 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800481 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800482 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
483 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800484 return BAD_VALUE;
485 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700486
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800487 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
488 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
489 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
490 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
491 }
492 }
493
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800494 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
495 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800496 return BAD_VALUE;
497}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700498
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800499status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
500 ATRACE_CALL();
501 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700502
503 // Try to lock, but continue in case of failure (to avoid blocking in
504 // deadlocks)
505 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
506 bool gotLock = tryLockSpinRightRound(mLock);
507
508 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800509 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
510 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700511 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800512 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
513 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700514
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800515 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700516
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800517 String16 templatesOption("-t");
518 int n = args.size();
519 for (int i = 0; i < n; i++) {
520 if (args[i] == templatesOption) {
521 dumpTemplates = true;
522 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000523 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700524 if (i + 1 < n) {
525 String8 monitorTags = String8(args[i + 1]);
526 if (monitorTags == "off") {
527 mTagMonitor.disableMonitoring();
528 } else {
529 mTagMonitor.parseTagsToMonitor(monitorTags);
530 }
531 } else {
532 mTagMonitor.disableMonitoring();
533 }
534 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800535 }
536
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800537 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800539 const char *status =
540 mStatus == STATUS_ERROR ? "ERROR" :
541 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
543 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800544 mStatus == STATUS_ACTIVE ? "ACTIVE" :
545 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700546
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800547 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700548 if (mStatus == STATUS_ERROR) {
549 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
550 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800551 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800552 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000553 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
554 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
555 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800556 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800557
558 if (mInputStream != NULL) {
559 write(fd, lines.string(), lines.size());
560 mInputStream->dump(fd, args);
561 } else {
562 lines.appendFormat(" No input stream.\n");
563 write(fd, lines.string(), lines.size());
564 }
565 for (size_t i = 0; i < mOutputStreams.size(); i++) {
566 mOutputStreams[i]->dump(fd,args);
567 }
568
Zhijun He431503c2016-03-07 17:30:16 -0800569 if (mBufferManager != NULL) {
570 lines = String8(" Camera3 Buffer Manager:\n");
571 write(fd, lines.string(), lines.size());
572 mBufferManager->dump(fd, args);
573 }
Zhijun He125684a2015-12-26 15:07:30 -0800574
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700575 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700576 if (mInFlightLock.try_lock()) {
577 if (mInFlightMap.size() == 0) {
578 lines.append(" None\n");
579 } else {
580 for (size_t i = 0; i < mInFlightMap.size(); i++) {
581 InFlightRequest r = mInFlightMap.valueAt(i);
582 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
583 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
584 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
585 r.numBuffersLeft);
586 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700587 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700588 mInFlightLock.unlock();
589 } else {
590 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 }
592 write(fd, lines.string(), lines.size());
593
Shuzhen Wang686f6442017-06-20 16:16:04 -0700594 if (mRequestThread != NULL) {
595 mRequestThread->dumpCaptureRequestLatency(fd,
596 " ProcessCaptureRequest latency histogram:");
597 }
598
Igor Murashkin1e479c02013-09-06 16:55:14 -0700599 {
600 lines = String8(" Last request sent:\n");
601 write(fd, lines.string(), lines.size());
602
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700603 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700604 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
605 }
606
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800607 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700608 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800609 "TEMPLATE_PREVIEW",
610 "TEMPLATE_STILL_CAPTURE",
611 "TEMPLATE_VIDEO_RECORD",
612 "TEMPLATE_VIDEO_SNAPSHOT",
613 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800614 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 };
616
Emilian Peevf4816702020-04-03 15:44:51 -0700617 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800618 camera_metadata_t *templateRequest = nullptr;
619 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700620 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800621 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800622 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800623 lines.append(" Not supported\n");
624 write(fd, lines.string(), lines.size());
625 } else {
626 write(fd, lines.string(), lines.size());
627 dump_indented_camera_metadata(templateRequest,
628 fd, /*verbosity*/2, /*indentation*/8);
629 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800630 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800631 }
632 }
633
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700634 mTagMonitor.dumpMonitoredMetadata(fd);
635
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800636 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800637 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800639 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800640 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800641
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700642 if (gotLock) mLock.unlock();
643 if (gotInterfaceLock) mInterfaceLock.unlock();
644
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800645 return OK;
646}
647
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700648status_t Camera3Device::startWatchingTags(const String8 &tags) {
649 mTagMonitor.parseTagsToMonitor(tags);
650 return OK;
651}
652
653status_t Camera3Device::stopWatchingTags() {
654 mTagMonitor.disableMonitoring();
655 return OK;
656}
657
658status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
659 mTagMonitor.getLatestMonitoredTagEvents(out);
660 return OK;
661}
662
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800663const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800665 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
666 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700667 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 mStatus == STATUS_ERROR ?
669 "when in error state" : "before init");
670 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700671 if (physicalId.isEmpty()) {
672 return mDeviceInfo;
673 } else {
674 std::string id(physicalId.c_str());
675 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
676 return mPhysicalDeviceInfoMap.at(id);
677 } else {
678 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
679 return mDeviceInfo;
680 }
681 }
682}
683
684const CameraMetadata& Camera3Device::info() const {
685 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800686 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800687}
688
Jianing Wei90e59c92014-03-12 18:29:36 -0700689status_t Camera3Device::checkStatusOkToCaptureLocked() {
690 switch (mStatus) {
691 case STATUS_ERROR:
692 CLOGE("Device has encountered a serious error");
693 return INVALID_OPERATION;
694 case STATUS_UNINITIALIZED:
695 CLOGE("Device not initialized");
696 return INVALID_OPERATION;
697 case STATUS_UNCONFIGURED:
698 case STATUS_CONFIGURED:
699 case STATUS_ACTIVE:
700 // OK
701 break;
702 default:
703 SET_ERR_L("Unexpected status: %d", mStatus);
704 return INVALID_OPERATION;
705 }
706 return OK;
707}
708
709status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000710 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700711 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700712 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700713 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700714 if (requestList == NULL) {
715 CLOGE("requestList cannot be NULL.");
716 return BAD_VALUE;
717 }
718
Jianing Weicb0652e2014-03-12 18:29:36 -0700719 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000720 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700721 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
722 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
723 ++metadataIt, ++surfaceMapIt) {
724 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700725 if (newRequest == 0) {
726 CLOGE("Can't create capture request");
727 return BAD_VALUE;
728 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700729
Shuzhen Wang9d066012016-09-30 11:30:20 -0700730 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700731 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700732
Jianing Weicb0652e2014-03-12 18:29:36 -0700733 // Setup burst Id and request Id
734 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800735 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
736 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700737 CLOGE("RequestID does not exist in metadata");
738 return BAD_VALUE;
739 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800740 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700741
Jianing Wei90e59c92014-03-12 18:29:36 -0700742 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700743
744 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700745 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700746 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
747 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
748 return BAD_VALUE;
749 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700750
751 // Setup batch size if this is a high speed video recording request.
752 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
753 auto firstRequest = requestList->begin();
754 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
755 if (outputStream->isVideoStream()) {
756 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800757 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700758 break;
759 }
760 }
761 }
762
Jianing Wei90e59c92014-03-12 18:29:36 -0700763 return OK;
764}
765
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800766status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800767 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800768
Emilian Peevaebbe412018-01-15 13:53:24 +0000769 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700770 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000771 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700772
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800773 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774}
775
Emilian Peevaebbe412018-01-15 13:53:24 +0000776void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700777 std::list<const SurfaceMap>& surfaceMaps,
778 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000779 PhysicalCameraSettingsList requestList;
780 requestList.push_back({std::string(getId().string()), request});
781 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700782
783 SurfaceMap surfaceMap;
784 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
785 // With no surface list passed in, stream and surface will have 1-to-1
786 // mapping. So the surface index is 0 for each stream in the surfaceMap.
787 for (size_t i = 0; i < streams.count; i++) {
788 surfaceMap[streams.data.i32[i]].push_back(0);
789 }
790 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800791}
792
Jianing Wei90e59c92014-03-12 18:29:36 -0700793status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000794 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700795 const std::list<const SurfaceMap> &surfaceMaps,
796 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700797 /*out*/
798 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700799 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700800 nsecs_t requestTimeNs = systemTime();
801
Jianing Wei90e59c92014-03-12 18:29:36 -0700802 Mutex::Autolock il(mInterfaceLock);
803 Mutex::Autolock l(mLock);
804
805 status_t res = checkStatusOkToCaptureLocked();
806 if (res != OK) {
807 // error logged by previous call
808 return res;
809 }
810
811 RequestList requestList;
812
Shuzhen Wang0129d522016-10-30 22:43:41 -0700813 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700814 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700815 if (res != OK) {
816 // error logged by previous call
817 return res;
818 }
819
820 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700821 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700822 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700823 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700824 }
825
826 if (res == OK) {
827 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
828 if (res != OK) {
829 SET_ERR_L("Can't transition to active in %f seconds!",
830 kActiveTimeout/1e9);
831 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800832 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700833 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700834 } else {
835 CLOGE("Cannot queue request. Impossible.");
836 return BAD_VALUE;
837 }
838
839 return res;
840}
841
Emilian Peevaebbe412018-01-15 13:53:24 +0000842status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700843 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700844 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700845 ATRACE_CALL();
846
Emilian Peevaebbe412018-01-15 13:53:24 +0000847 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700848}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800849
Jianing Weicb0652e2014-03-12 18:29:36 -0700850status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
851 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700855 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000856 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700857
Emilian Peevaebbe412018-01-15 13:53:24 +0000858 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700859 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860}
861
Emilian Peevaebbe412018-01-15 13:53:24 +0000862status_t Camera3Device::setStreamingRequestList(
863 const List<const PhysicalCameraSettingsList> &requestsList,
864 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700865 ATRACE_CALL();
866
Emilian Peevaebbe412018-01-15 13:53:24 +0000867 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700868}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869
870sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000871 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 status_t res;
873
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700874 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800875 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
876 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000877 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700878 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700879 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700881 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700883 } else if (mStatus == STATUS_UNCONFIGURED) {
884 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700885 CLOGE("No streams configured");
886 return NULL;
887 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 }
889
Shuzhen Wang0129d522016-10-30 22:43:41 -0700890 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800892}
893
Jianing Weicb0652e2014-03-12 18:29:36 -0700894status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800898
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 switch (mStatus) {
900 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 return INVALID_OPERATION;
903 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700904 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 case STATUS_UNCONFIGURED:
907 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800908 case STATUS_ACTIVE:
909 // OK
910 break;
911 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700912 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 return INVALID_OPERATION;
914 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800915 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700916
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700917 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918}
919
920status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800923
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700924 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800925}
926
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700927status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800928 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700929 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700931 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700932 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800933 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
934 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700935
936 status_t res;
937 bool wasActive = false;
938
939 switch (mStatus) {
940 case STATUS_ERROR:
941 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
942 return INVALID_OPERATION;
943 case STATUS_UNINITIALIZED:
944 ALOGE("%s: Device not initialized", __FUNCTION__);
945 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 case STATUS_UNCONFIGURED:
947 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700948 // OK
949 break;
950 case STATUS_ACTIVE:
951 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700952 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700953 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700955 return res;
956 }
957 wasActive = true;
958 break;
959 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700961 return INVALID_OPERATION;
962 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700964
965 if (mInputStream != 0) {
966 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
967 return INVALID_OPERATION;
968 }
969
970 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
971 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700973
974 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800975 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700976
977 *id = mNextStreamId++;
978
979 // Continue captures if active at start
980 if (wasActive) {
981 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100982 // Reuse current operating mode and session parameters for new stream config
983 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700984 if (res != OK) {
985 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
986 __FUNCTION__, mNextStreamId, strerror(-res), res);
987 return res;
988 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700989 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700990 }
991
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800992 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700993 return OK;
994}
995
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700996status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700997 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700998 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800999 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001000 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1001 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001002 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001003 int timestampBase, int mirrorMode, int32_t colorSpace) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001004 ATRACE_CALL();
1005
1006 if (consumer == nullptr) {
1007 ALOGE("%s: consumer must not be null", __FUNCTION__);
1008 return BAD_VALUE;
1009 }
1010
1011 std::vector<sp<Surface>> consumers;
1012 consumers.push_back(consumer);
1013
1014 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001015 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001016 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001017 streamUseCase, timestampBase, mirrorMode, colorSpace);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001018}
1019
1020static bool isRawFormat(int format) {
1021 switch (format) {
1022 case HAL_PIXEL_FORMAT_RAW16:
1023 case HAL_PIXEL_FORMAT_RAW12:
1024 case HAL_PIXEL_FORMAT_RAW10:
1025 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1026 return true;
1027 default:
1028 return false;
1029 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001030}
1031
1032status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1033 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001034 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001035 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001036 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001037 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001038 int timestampBase, int mirrorMode, int32_t colorSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001039 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001040
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001041 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001042 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001044 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001045 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001046 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
Austin Borger9e2b27c2022-07-15 11:27:24 -07001047 " mirrorMode %d colorSpace %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001048 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001049 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001050 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001051
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001052 status_t res;
1053 bool wasActive = false;
1054
1055 switch (mStatus) {
1056 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001057 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 return INVALID_OPERATION;
1059 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001060 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001062 case STATUS_UNCONFIGURED:
1063 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 // OK
1065 break;
1066 case STATUS_ACTIVE:
1067 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001068 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 return res;
1072 }
1073 wasActive = true;
1074 break;
1075 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001076 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 return INVALID_OPERATION;
1078 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001079 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080
1081 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001082
Shuzhen Wang0129d522016-10-30 22:43:41 -07001083 if (consumers.size() == 0 && !hasDeferredConsumer) {
1084 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1085 return BAD_VALUE;
1086 }
Zhijun He5d677d12016-05-29 16:52:39 -07001087
Shuzhen Wang0129d522016-10-30 22:43:41 -07001088 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001089 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1090 return BAD_VALUE;
1091 }
1092
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001093 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1094 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1095 // be found in only one sensor pixel mode.
1096 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1097 return BAD_VALUE;
1098 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001099 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001101 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001102 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001103 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001104 if (blobBufferSize <= 0) {
1105 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1106 return BAD_VALUE;
1107 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001108 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1109 blobBufferSize = width * height;
1110 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001111 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001112 if (blobBufferSize <= 0) {
1113 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1114 return BAD_VALUE;
1115 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001116 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001117 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001118 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001119 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001120 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001121 timestampBase, mirrorMode, colorSpace);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001122 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001123 bool maxResolution =
1124 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1125 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001126 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1127 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001128 if (rawOpaqueBufferSize <= 0) {
1129 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1130 return BAD_VALUE;
1131 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001132 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001133 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001134 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001135 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001136 timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001137 } else if (isShared) {
1138 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1139 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001140 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001141 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001142 timestampBase, mirrorMode, colorSpace);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001143 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001144 newStream = new Camera3OutputStream(mNextStreamId,
1145 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001146 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001147 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001148 timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001149 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001150 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001151 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001152 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001153 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001154 timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001155 }
Emilian Peev40ead602017-09-26 15:46:36 +01001156
1157 size_t consumerCount = consumers.size();
1158 for (size_t i = 0; i < consumerCount; i++) {
1159 int id = newStream->getSurfaceId(consumers[i]);
1160 if (id < 0) {
1161 SET_ERR_L("Invalid surface id");
1162 return BAD_VALUE;
1163 }
1164 if (surfaceIds != nullptr) {
1165 surfaceIds->push_back(id);
1166 }
1167 }
1168
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001169 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170
Emilian Peev08dd2452017-04-06 16:55:14 +01001171 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001172
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001173 newStream->setImageDumpMask(mImageDumpMask);
1174
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 res = mOutputStreams.add(mNextStreamId, newStream);
1176 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001177 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001178 return res;
1179 }
1180
Shuzhen Wang316781a2020-08-18 18:11:01 -07001181 mSessionStatsBuilder.addStream(mNextStreamId);
1182
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001184 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185
1186 // Continue captures if active at start
1187 if (wasActive) {
1188 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001189 // Reuse current operating mode and session parameters for new stream config
1190 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001192 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1193 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001194 return res;
1195 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001198 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200}
1201
Emilian Peev710c1422017-08-30 11:19:38 +01001202status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001204 if (nullptr == streamInfo) {
1205 return BAD_VALUE;
1206 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001207 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001209
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 switch (mStatus) {
1211 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001212 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001213 return INVALID_OPERATION;
1214 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 case STATUS_UNCONFIGURED:
1218 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001219 case STATUS_ACTIVE:
1220 // OK
1221 break;
1222 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001223 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001224 return INVALID_OPERATION;
1225 }
1226
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001227 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1228 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001229 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001230 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001231 }
1232
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001233 streamInfo->width = stream->getWidth();
1234 streamInfo->height = stream->getHeight();
1235 streamInfo->format = stream->getFormat();
1236 streamInfo->dataSpace = stream->getDataSpace();
1237 streamInfo->formatOverridden = stream->isFormatOverridden();
1238 streamInfo->originalFormat = stream->getOriginalFormat();
1239 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1240 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001241 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001242 streamInfo->colorSpace = stream->getColorSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001244}
1245
1246status_t Camera3Device::setStreamTransform(int id,
1247 int transform) {
1248 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001249 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 switch (mStatus) {
1253 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001254 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001255 return INVALID_OPERATION;
1256 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001257 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001259 case STATUS_UNCONFIGURED:
1260 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 case STATUS_ACTIVE:
1262 // OK
1263 break;
1264 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001265 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 return INVALID_OPERATION;
1267 }
1268
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001269 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1270 if (stream == nullptr) {
1271 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001272 return BAD_VALUE;
1273 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001274 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001275}
1276
1277status_t Camera3Device::deleteStream(int id) {
1278 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 Mutex::Autolock l(mLock);
1281 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001282
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001283 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001284
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001285 // CameraDevice semantics require device to already be idle before
1286 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001288 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001289 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290 }
1291
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001292 if (mStatus == STATUS_ERROR) {
1293 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1294 __FUNCTION__, mId.string());
1295 return -EBUSY;
1296 }
1297
Igor Murashkin2fba5842013-04-22 14:03:54 -07001298 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001299 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 if (mInputStream != NULL && id == mInputStream->getId()) {
1301 deletedStream = mInputStream;
1302 mInputStream.clear();
1303 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001304 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001305 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 return BAD_VALUE;
1307 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001308 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001309 }
1310
1311 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001312 if (stream != nullptr) {
1313 deletedStream = stream;
1314 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001315 }
1316
1317 // Free up the stream endpoint so that it can be used by some other stream
1318 res = deletedStream->disconnect();
1319 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001320 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001321 // fall through since we want to still list the stream as deleted.
1322 }
1323 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001324 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001325
1326 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001327}
1328
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001329status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001330 ATRACE_CALL();
1331 ALOGV("%s: E", __FUNCTION__);
1332
1333 Mutex::Autolock il(mInterfaceLock);
1334 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001335
Emilian Peev811d2952018-05-25 11:08:40 +01001336 // In case the client doesn't include any session parameter, try a
1337 // speculative configuration using the values from the last cached
1338 // default request.
1339 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001340 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001341 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1342 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1343 mLastTemplateId);
1344 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1345 operatingMode);
1346 }
1347
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001348 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1349}
1350
1351status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1352 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001353 //Filter out any incoming session parameters
1354 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001355 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1356 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001357 CameraMetadata filteredParams(availableSessionKeys.count);
1358 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1359 filteredParams.getAndLock());
1360 set_camera_metadata_vendor_id(meta, mVendorTagId);
1361 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001362 if (availableSessionKeys.count > 0) {
1363 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1364 camera_metadata_ro_entry entry = params.find(
1365 availableSessionKeys.data.i32[i]);
1366 if (entry.count > 0) {
1367 filteredParams.update(entry);
1368 }
1369 }
1370 }
1371
1372 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001373}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001374
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001375status_t Camera3Device::getInputBufferProducer(
1376 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001377 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001378 Mutex::Autolock il(mInterfaceLock);
1379 Mutex::Autolock l(mLock);
1380
1381 if (producer == NULL) {
1382 return BAD_VALUE;
1383 } else if (mInputStream == NULL) {
1384 return INVALID_OPERATION;
1385 }
1386
1387 return mInputStream->getInputBufferProducer(producer);
1388}
1389
Emilian Peevf4816702020-04-03 15:44:51 -07001390status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391 CameraMetadata *request) {
1392 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001393 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001394
Emilian Peevf4816702020-04-03 15:44:51 -07001395 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001396 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001397 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001398 return BAD_VALUE;
1399 }
1400
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001401 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001402
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001403 {
1404 Mutex::Autolock l(mLock);
1405 switch (mStatus) {
1406 case STATUS_ERROR:
1407 CLOGE("Device has encountered a serious error");
1408 return INVALID_OPERATION;
1409 case STATUS_UNINITIALIZED:
1410 CLOGE("Device is not initialized!");
1411 return INVALID_OPERATION;
1412 case STATUS_UNCONFIGURED:
1413 case STATUS_CONFIGURED:
1414 case STATUS_ACTIVE:
1415 // OK
1416 break;
1417 default:
1418 SET_ERR_L("Unexpected status: %d", mStatus);
1419 return INVALID_OPERATION;
1420 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001421
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001422 if (!mRequestTemplateCache[templateId].isEmpty()) {
1423 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001424 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001425 return OK;
1426 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001427 }
1428
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001429 camera_metadata_t *rawRequest;
1430 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001431 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001432
1433 {
1434 Mutex::Autolock l(mLock);
1435 if (res == BAD_VALUE) {
1436 ALOGI("%s: template %d is not supported on this camera device",
1437 __FUNCTION__, templateId);
1438 return res;
1439 } else if (res != OK) {
1440 CLOGE("Unable to construct request template %d: %s (%d)",
1441 templateId, strerror(-res), res);
1442 return res;
1443 }
1444
1445 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1446 mRequestTemplateCache[templateId].acquire(rawRequest);
1447
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001448 // Override the template request with zoomRatioMapper
1449 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1450 &mRequestTemplateCache[templateId]);
1451 if (res != OK) {
1452 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1453 templateId, strerror(-res), res);
1454 return res;
1455 }
1456
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001457 // Fill in JPEG_QUALITY if not available
1458 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1459 static const uint8_t kDefaultJpegQuality = 95;
1460 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1461 &kDefaultJpegQuality, 1);
1462 }
1463
Bharatt Kukrejad33fe9f2022-11-23 21:52:52 +00001464 // Fill in AUTOFRAMING if not available
1465 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1466 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1467 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1468 &kDefaultAutoframingMode, 1);
1469 }
1470
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001471 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001472 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001473 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001474 return OK;
1475}
1476
1477status_t Camera3Device::waitUntilDrained() {
1478 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001479 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001480 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001482
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001483 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001484}
1485
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001486status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001487 switch (mStatus) {
1488 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001489 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001490 ALOGV("%s: Already idle", __FUNCTION__);
1491 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001492 case STATUS_CONFIGURED:
1493 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494 case STATUS_ERROR:
1495 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001496 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001497 break;
1498 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001499 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001500 return INVALID_OPERATION;
1501 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001502 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1503 maxExpectedDuration);
1504 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001505 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001506 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001507 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1508 res);
1509 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510 return res;
1511}
1512
Ruben Brunk183f0562015-08-12 12:55:02 -07001513void Camera3Device::internalUpdateStatusLocked(Status status) {
1514 mStatus = status;
1515 mRecentStatusUpdates.add(mStatus);
1516 mStatusChanged.broadcast();
1517}
1518
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001519// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001520status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001521 if (mRequestThread.get() != nullptr) {
1522 mRequestThread->setPaused(true);
1523 } else {
1524 return NO_INIT;
1525 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001526
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001527 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1528 maxExpectedDuration);
1529 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001530 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001531 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001532 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001533 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001534 }
1535
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001536 return res;
1537}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001539// Resume after internalPauseAndWaitLocked
1540status_t Camera3Device::internalResumeLocked() {
1541 status_t res;
1542
1543 mRequestThread->setPaused(false);
1544
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001545 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1546 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001547 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1548 if (res != OK) {
1549 SET_ERR_L("Can't transition to active in %f seconds!",
1550 kActiveTimeout/1e9);
1551 }
1552 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001554}
1555
Ruben Brunk183f0562015-08-12 12:55:02 -07001556status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001557 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001558
1559 size_t startIndex = 0;
1560 if (mStatusWaiters == 0) {
1561 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1562 // this status list
1563 mRecentStatusUpdates.clear();
1564 } else {
1565 // If other threads are waiting on updates to this status list, set the position of the
1566 // first element that this list will check rather than clearing the list.
1567 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 }
1569
Ruben Brunk183f0562015-08-12 12:55:02 -07001570 mStatusWaiters++;
1571
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001572 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001573 if (!active && mUseHalBufManager) {
1574 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001575 if (mStatus == STATUS_ACTIVE) {
1576 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001577 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001578 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001579 mRequestBufferSM.onWaitUntilIdle();
1580 }
1581
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001582 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001583 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001584 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001585 if (active == (mStatus == STATUS_ACTIVE)) {
1586 // Desired state is current
1587 break;
1588 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001589
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001590 nsecs_t timeElapsed = systemTime() - startTime;
1591 nsecs_t timeToWait = timeout - timeElapsed;
1592 if (timeToWait <= 0) {
1593 // Thread woke up spuriously but has timed out since.
1594 // Force out of loop with TIMED_OUT result.
1595 res = TIMED_OUT;
1596 break;
1597 }
1598 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001599 if (res != OK) break;
1600
Ruben Brunk183f0562015-08-12 12:55:02 -07001601 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1602 // transitions.
1603 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1604 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1605 __FUNCTION__);
1606
1607 // Encountered desired state since we began waiting
1608 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001609 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1610 stateSeen = true;
1611 break;
1612 }
1613 }
1614 } while (!stateSeen);
1615
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001616 if (signalPipelineDrain) {
1617 mRequestThread->resetPipelineDrain();
1618 }
1619
Ruben Brunk183f0562015-08-12 12:55:02 -07001620 mStatusWaiters--;
1621
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001622 return res;
1623}
1624
1625
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001626status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001627 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001628 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001629
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001630 if (listener != NULL && mListener != NULL) {
1631 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1632 }
1633 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001634 mRequestThread->setNotificationListener(listener);
1635 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001636
1637 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638}
1639
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001640bool Camera3Device::willNotify3A() {
1641 return false;
1642}
1643
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001644status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001645 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001646 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001647
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001648 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001649 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1650 if (st == std::cv_status::timeout) {
1651 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001652 }
1653 }
1654 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001655}
1656
Jianing Weicb0652e2014-03-12 18:29:36 -07001657status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001658 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001659 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001661 if (mResultQueue.empty()) {
1662 return NOT_ENOUGH_DATA;
1663 }
1664
Jianing Weicb0652e2014-03-12 18:29:36 -07001665 if (frame == NULL) {
1666 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1667 return BAD_VALUE;
1668 }
1669
1670 CaptureResult &result = *(mResultQueue.begin());
1671 frame->mResultExtras = result.mResultExtras;
1672 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001673 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001674 mResultQueue.erase(mResultQueue.begin());
1675
1676 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001677}
1678
1679status_t Camera3Device::triggerAutofocus(uint32_t id) {
1680 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001681 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001682
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001683 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1684 // Mix-in this trigger into the next request and only the next request.
1685 RequestTrigger trigger[] = {
1686 {
1687 ANDROID_CONTROL_AF_TRIGGER,
1688 ANDROID_CONTROL_AF_TRIGGER_START
1689 },
1690 {
1691 ANDROID_CONTROL_AF_TRIGGER_ID,
1692 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001693 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001694 };
1695
1696 return mRequestThread->queueTrigger(trigger,
1697 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001698}
1699
1700status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1701 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001702 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001703
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001704 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1705 // Mix-in this trigger into the next request and only the next request.
1706 RequestTrigger trigger[] = {
1707 {
1708 ANDROID_CONTROL_AF_TRIGGER,
1709 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1710 },
1711 {
1712 ANDROID_CONTROL_AF_TRIGGER_ID,
1713 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001714 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001715 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001716
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001717 return mRequestThread->queueTrigger(trigger,
1718 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001719}
1720
1721status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1722 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001724
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001725 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1726 // Mix-in this trigger into the next request and only the next request.
1727 RequestTrigger trigger[] = {
1728 {
1729 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1730 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1731 },
1732 {
1733 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1734 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001735 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001736 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001737
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001738 return mRequestThread->queueTrigger(trigger,
1739 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001740}
1741
Jianing Weicb0652e2014-03-12 18:29:36 -07001742status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001743 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001744 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001745 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001746
Zhijun He7ef20392014-04-21 16:04:17 -07001747 {
1748 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001749
1750 // b/116514106 "disconnect()" can get called twice for the same device. The
1751 // camera device will not be initialized during the second run.
1752 if (mStatus == STATUS_UNINITIALIZED) {
1753 return OK;
1754 }
1755
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001756 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001757
1758 // Stop session and stream counter
1759 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001760 }
1761
Ravneetdbd5b242022-03-02 07:22:46 +00001762 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001763 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001764 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1765 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1766
1767 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001768}
1769
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001770status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001771 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1772}
1773
1774status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001775 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001776 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001777 Mutex::Autolock il(mInterfaceLock);
1778 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001779
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001780 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1781 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001782 CLOGE("Stream %d does not exist", streamId);
1783 return BAD_VALUE;
1784 }
1785
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001786 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001787 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001788 return BAD_VALUE;
1789 }
1790
1791 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001792 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001793 return BAD_VALUE;
1794 }
1795
Ruben Brunkc78ac262015-08-13 17:58:46 -07001796 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001797}
1798
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001799status_t Camera3Device::tearDown(int streamId) {
1800 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001801 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001802 Mutex::Autolock il(mInterfaceLock);
1803 Mutex::Autolock l(mLock);
1804
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001805 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1806 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001807 CLOGE("Stream %d does not exist", streamId);
1808 return BAD_VALUE;
1809 }
1810
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001811 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1812 CLOGE("Stream %d is a target of a in-progress request", streamId);
1813 return BAD_VALUE;
1814 }
1815
1816 return stream->tearDown();
1817}
1818
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001819status_t Camera3Device::addBufferListenerForStream(int streamId,
1820 wp<Camera3StreamBufferListener> listener) {
1821 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001822 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001823 Mutex::Autolock il(mInterfaceLock);
1824 Mutex::Autolock l(mLock);
1825
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001826 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1827 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001828 CLOGE("Stream %d does not exist", streamId);
1829 return BAD_VALUE;
1830 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001831 stream->addBufferListener(listener);
1832
1833 return OK;
1834}
1835
Austin Borger4a870a32022-02-25 01:48:41 +00001836float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1837 camera_metadata_entry minDurations =
1838 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1839 for (size_t i = 0; i < minDurations.count; i += 4) {
1840 if (minDurations.data.i64[i] == stream->getFormat()
1841 && minDurations.data.i64[i+1] == stream->getWidth()
1842 && minDurations.data.i64[i+2] == stream->getHeight()) {
1843 int64_t minFrameDuration = minDurations.data.i64[i+3];
1844 return 1e9f / minFrameDuration;
1845 }
1846 }
1847 return 0.0f;
1848}
1849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001851 * Methods called by subclasses
1852 */
1853
1854void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001855 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001856 std::vector<int> streamIds;
1857 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001858 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001859
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001860 {
1861 // Need mLock to safely update state and synchronize to current
1862 // state of methods in flight.
1863 Mutex::Autolock l(mLock);
1864 // We can get various system-idle notices from the status tracker
1865 // while starting up. Only care about them if we've actually sent
1866 // in some requests recently.
1867 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1868 return;
1869 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001870 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1871 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001872 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001873
1874 // Skip notifying listener if we're doing some user-transparent
1875 // state changes
1876 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001877
Austin Borger4a870a32022-02-25 01:48:41 +00001878 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1879 auto stream = mOutputStreams[i];
1880 if (stream.get() == nullptr) continue;
1881
1882 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1883 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1884
1885 // Populate stream statistics in case of Idle
1886 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001887 streamIds.push_back(stream->getId());
1888 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1889 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001890 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001891 if (camera3Stream != nullptr) {
1892 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001893 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001894 }
1895 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001896 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001897 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001898 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001899 stream->getDynamicRangeProfile(), streamUseCase,
1900 stream->getColorSpace());
Shuzhen Wang316781a2020-08-18 18:11:01 -07001901 }
1902 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001903 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001904
1905 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001906 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001907 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001908 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001909 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001910 status_t res = OK;
1911 if (listener != nullptr) {
1912 if (idle) {
1913 // Get session stats from the builder, and notify the listener.
1914 int64_t requestCount, resultErrorCount;
1915 bool deviceError;
1916 std::map<int, StreamStats> streamStatsMap;
1917 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1918 &deviceError, &streamStatsMap);
1919 for (size_t i = 0; i < streamIds.size(); i++) {
1920 int streamId = streamIds[i];
1921 auto stats = streamStatsMap.find(streamId);
1922 if (stats != streamStatsMap.end()) {
1923 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1924 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1925 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1926 streamStats[i].mHistogramType =
1927 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1928 streamStats[i].mHistogramBins.assign(
1929 stats->second.mCaptureLatencyBins.begin(),
1930 stats->second.mCaptureLatencyBins.end());
1931 streamStats[i].mHistogramCounts.assign(
1932 stats->second.mCaptureLatencyHistogram.begin(),
1933 stats->second.mCaptureLatencyHistogram.end());
1934 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001935 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001936 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1937 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001938 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001939 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001940 }
1941 if (res != OK) {
1942 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1943 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001944 }
1945}
1946
Shuzhen Wang758c2152017-01-10 18:26:18 -08001947status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001948 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001949 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001950 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1951 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001952
1953 if (surfaceIds == nullptr) {
1954 return BAD_VALUE;
1955 }
1956
Zhijun He5d677d12016-05-29 16:52:39 -07001957 Mutex::Autolock il(mInterfaceLock);
1958 Mutex::Autolock l(mLock);
1959
Shuzhen Wang758c2152017-01-10 18:26:18 -08001960 if (consumers.size() == 0) {
1961 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001962 return BAD_VALUE;
1963 }
1964
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001965 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1966 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001967 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001968 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001969 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001970
1971 // isConsumerConfigurationDeferred will be off after setConsumers
1972 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001973 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001974 if (res != OK) {
1975 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1976 return res;
1977 }
1978
Emilian Peev40ead602017-09-26 15:46:36 +01001979 for (auto &consumer : consumers) {
1980 int id = stream->getSurfaceId(consumer);
1981 if (id < 0) {
1982 CLOGE("Invalid surface id!");
1983 return BAD_VALUE;
1984 }
1985 surfaceIds->push_back(id);
1986 }
1987
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001988 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001989 if (!stream->isConfiguring()) {
1990 CLOGE("Stream %d was already fully configured.", streamId);
1991 return INVALID_OPERATION;
1992 }
Zhijun He5d677d12016-05-29 16:52:39 -07001993
Shuzhen Wang0129d522016-10-30 22:43:41 -07001994 res = stream->finishConfiguration();
1995 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001996 // If finishConfiguration fails due to abandoned surface, do not set
1997 // device to error state.
1998 bool isSurfaceAbandoned =
1999 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2000 if (!isSurfaceAbandoned) {
2001 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2002 stream->getId(), strerror(-res), res);
2003 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002004 return res;
2005 }
Zhijun He5d677d12016-05-29 16:52:39 -07002006 }
2007
2008 return OK;
2009}
2010
Emilian Peev40ead602017-09-26 15:46:36 +01002011status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2012 const std::vector<OutputStreamInfo> &outputInfo,
2013 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2014 Mutex::Autolock il(mInterfaceLock);
2015 Mutex::Autolock l(mLock);
2016
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002017 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2018 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002019 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002020 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002021 }
2022
2023 for (const auto &it : removedSurfaceIds) {
2024 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2025 CLOGE("Shared surface still part of a pending request!");
2026 return -EBUSY;
2027 }
2028 }
2029
Emilian Peev40ead602017-09-26 15:46:36 +01002030 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2031 if (res != OK) {
2032 CLOGE("Stream %d failed to update stream (error %d %s) ",
2033 streamId, res, strerror(-res));
2034 if (res == UNKNOWN_ERROR) {
2035 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2036 __FUNCTION__);
2037 }
2038 return res;
2039 }
2040
2041 return res;
2042}
2043
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002044status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2045 Mutex::Autolock il(mInterfaceLock);
2046 Mutex::Autolock l(mLock);
2047
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002048 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2049 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002050 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2051 return BAD_VALUE;
2052 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002053
2054 if (dropping) {
2055 mSessionStatsBuilder.stopCounter(streamId);
2056 } else {
2057 mSessionStatsBuilder.startCounter(streamId);
2058 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002059 return stream->dropBuffers(dropping);
2060}
2061
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002062/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063 * Camera3Device private methods
2064 */
2065
2066sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002067 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002068 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002070 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002071 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002072
2073 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002074 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075 if (inputStreams.count > 0) {
2076 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002077 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002078 CLOGE("Request references unknown input stream %d",
2079 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002080 return NULL;
2081 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002082
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002084 SET_ERR_L("%s: input stream %d is not configured!",
2085 __FUNCTION__, mInputStream->getId());
2086 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002088 // Check if stream prepare is blocking requests.
2089 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002090 CLOGE("Request references an input stream that's being prepared!");
2091 return NULL;
2092 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093
2094 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002095 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002096 }
2097
2098 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002099 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002100 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002101 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002102 return NULL;
2103 }
2104
2105 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002106 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2107 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002108 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002109 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110 return NULL;
2111 }
Zhijun He5d677d12016-05-29 16:52:39 -07002112 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002113 auto iter = surfaceMap.find(streams.data.i32[i]);
2114 if (iter != surfaceMap.end()) {
2115 const std::vector<size_t>& surfaces = iter->second;
2116 for (const auto& surface : surfaces) {
2117 if (stream->isConsumerConfigurationDeferred(surface)) {
2118 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2119 "due to deferred consumer", stream->getId(), surface);
2120 return NULL;
2121 }
2122 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002123 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002124 }
2125
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002126 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002127 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2128 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002130 // Check if stream prepare is blocking requests.
2131 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002132 CLOGE("Request references an output stream that's being prepared!");
2133 return NULL;
2134 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135
2136 newRequest->mOutputStreams.push(stream);
2137 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002138 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002139 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002141 auto rotateAndCropEntry =
2142 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2143 if (rotateAndCropEntry.count > 0 &&
2144 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2145 newRequest->mRotateAndCropAuto = true;
2146 } else {
2147 newRequest->mRotateAndCropAuto = false;
2148 }
2149
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002150 auto autoframingEntry =
2151 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2152 if (autoframingEntry.count > 0 &&
2153 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2154 newRequest->mAutoframingAuto = true;
2155 } else {
2156 newRequest->mAutoframingAuto = false;
2157 }
2158
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002159 auto zoomRatioEntry =
2160 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2161 if (zoomRatioEntry.count > 0 &&
2162 zoomRatioEntry.data.f[0] == 1.0f) {
2163 newRequest->mZoomRatioIs1x = true;
2164 } else {
2165 newRequest->mZoomRatioIs1x = false;
2166 }
2167
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002168 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002169 for (auto& settings : newRequest->mSettingsList) {
2170 auto testPatternModeEntry =
2171 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2172 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2173 testPatternModeEntry.data.i32[0] :
2174 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002175
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002176 auto testPatternDataEntry =
2177 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2178 if (testPatternDataEntry.count >= 4) {
2179 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2180 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2181 } else {
2182 settings.mOriginalTestPatternData[0] = 0;
2183 settings.mOriginalTestPatternData[1] = 0;
2184 settings.mOriginalTestPatternData[2] = 0;
2185 settings.mOriginalTestPatternData[3] = 0;
2186 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002187 }
2188 }
2189
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002190 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002191}
2192
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002193void Camera3Device::cancelStreamsConfigurationLocked() {
2194 int res = OK;
2195 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2196 res = mInputStream->cancelConfiguration();
2197 if (res != OK) {
2198 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2199 mInputStream->getId(), strerror(-res), res);
2200 }
2201 }
2202
2203 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002204 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002205 if (outputStream->isConfiguring()) {
2206 res = outputStream->cancelConfiguration();
2207 if (res != OK) {
2208 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2209 outputStream->getId(), strerror(-res), res);
2210 }
2211 }
2212 }
2213
2214 // Return state to that at start of call, so that future configures
2215 // properly clean things up
2216 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2217 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002218
2219 res = mPreparerThread->resume();
2220 if (res != OK) {
2221 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2222 }
2223}
2224
Emilian Peev0d0191e2020-04-21 17:01:18 -07002225bool Camera3Device::checkAbandonedStreamsLocked() {
2226 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2227 return true;
2228 }
2229
2230 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2231 auto stream = mOutputStreams[i];
2232 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2233 return true;
2234 }
2235 }
2236
2237 return false;
2238}
2239
Emilian Peev3bead5f2020-05-28 17:29:08 -07002240bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002241 ATRACE_CALL();
2242 bool ret = false;
2243
Shuzhen Wang316781a2020-08-18 18:11:01 -07002244 nsecs_t startTime = systemTime();
2245
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002246 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002247 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2248
2249 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002250 if (checkAbandonedStreamsLocked()) {
2251 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2252 __FUNCTION__);
2253 return true;
2254 }
2255
Emilian Peev3bead5f2020-05-28 17:29:08 -07002256 status_t rc = NO_ERROR;
2257 bool markClientActive = false;
2258 if (mStatus == STATUS_ACTIVE) {
2259 markClientActive = true;
2260 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002261
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
Austin Borger74fca042022-05-23 12:41:21 -07002292 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002293 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
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002366 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002368 mPreparerThread->pause();
2369
Emilian Peevf4816702020-04-03 15:44:51 -07002370 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002371 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002372 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002373 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002374
Emilian Peevf4816702020-04-03 15:44:51 -07002375 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002377 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002378
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002379
2380 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002381 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382 inputStream = mInputStream->startConfiguration();
2383 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002384 CLOGE("Can't start input stream configuration");
2385 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002386 return INVALID_OPERATION;
2387 }
2388 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002389
2390 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002391 }
2392
Shuzhen Wang99080502021-03-07 21:08:20 -08002393 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002394 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002395 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002396
2397 // Don't configure bidi streams twice, nor add them twice to the list
2398 if (mOutputStreams[i].get() ==
2399 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2400
2401 config.num_streams--;
2402 continue;
2403 }
2404
Emilian Peevf4816702020-04-03 15:44:51 -07002405 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002406 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002407 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002408 CLOGE("Can't start output stream configuration");
2409 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002410 return INVALID_OPERATION;
2411 }
2412 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002413
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002414 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002415 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2416 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002417 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2418 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002419 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2420 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002421 } else if (outputStream->data_space ==
2422 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2423 bufferSizes[k] = outputStream->width * outputStream->height;
2424 } else {
2425 ALOGW("%s: Blob dataSpace %d not supported",
2426 __FUNCTION__, outputStream->data_space);
2427 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002428 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002429
2430 if (mOutputStreams[i]->isMultiResolution()) {
2431 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2432 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2433 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2434 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002435
2436 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2437 composerSurfacePresent = true;
2438 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002439 }
2440
2441 config.streams = streams.editArray();
2442
2443 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002444 // max_buffers, usage, and priv fields, as well as data_space and format
2445 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002446
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002447 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002448 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002449 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002450
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002451 if (res == BAD_VALUE) {
2452 // HAL rejected this set of streams as unsupported, clean up config
2453 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002454 CLOGE("Set of requested inputs/outputs not supported by HAL");
2455 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002456 return BAD_VALUE;
2457 } else if (res != OK) {
2458 // Some other kind of error from configure_streams - this is not
2459 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002460 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2461 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002462 return res;
2463 }
2464
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002465 // Finish all stream configuration immediately.
2466 // TODO: Try to relax this later back to lazy completion, which should be
2467 // faster
2468
Igor Murashkin073f8572013-05-02 14:59:28 -07002469 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002470 bool streamReConfigured = false;
2471 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002472 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002473 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002474 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002475 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002476 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2477 return DEAD_OBJECT;
2478 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002479 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002480 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002481 if (streamReConfigured) {
2482 mInterface->onStreamReConfigured(mInputStream->getId());
2483 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002484 }
2485
2486 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002487 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002488 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002489 bool streamReConfigured = false;
2490 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002491 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002492 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002493 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002494 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002495 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2496 return DEAD_OBJECT;
2497 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002498 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002499 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002500 if (streamReConfigured) {
2501 mInterface->onStreamReConfigured(outputStream->getId());
2502 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002503 }
2504 }
2505
Emilian Peeve23f1d92021-09-20 14:56:01 -07002506 mRequestThread->setComposerSurface(composerSurfacePresent);
2507
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002508 // Request thread needs to know to avoid using repeat-last-settings protocol
2509 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002510 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002511 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2512 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002513 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002514
Zhijun He90f7c372016-08-16 16:19:43 -07002515 char value[PROPERTY_VALUE_MAX];
2516 property_get("camera.fifo.disable", value, "0");
2517 int32_t disableFifo = atoi(value);
2518 if (disableFifo != 1) {
2519 // Boost priority of request thread to SCHED_FIFO.
2520 pid_t requestThreadTid = mRequestThread->getTid();
2521 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002522 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002523 if (res != OK) {
2524 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2525 strerror(-res), res);
2526 } else {
2527 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2528 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002529 }
2530
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002531 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002532 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2533 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2534 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2535 sessionParams.unlock(newSessionParams);
2536 mSessionParams.unlock(currentSessionParams);
2537 if (updateSessionParams) {
2538 mSessionParams = sessionParams;
2539 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002540
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002541 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002542
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002543 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002544 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002545
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002546 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002547
Zhijun He0a210512014-07-24 13:45:15 -07002548 // tear down the deleted streams after configure streams.
2549 mDeletedStreams.clear();
2550
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002551 auto rc = mPreparerThread->resume();
2552 if (rc != OK) {
2553 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2554 return rc;
2555 }
2556
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002557 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002558 mRequestBufferSM.onStreamsConfigured();
2559 }
2560
Cliff Wu3b268182021-07-06 15:44:43 +08002561 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002562 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002563 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002564 // configure the injection streams.
2565 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002566 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002567 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2568 res = mInjectionMethods->injectCamera(config, bufferSizes);
2569 if (res != OK) {
2570 ALOGE("Can't finish inject camera process!");
2571 return res;
2572 }
Cliff Wu3b268182021-07-06 15:44:43 +08002573 } else {
2574 // First run configureStreamsLocked() and then call injectCamera() case:
2575 // If the stream configuration has been completed and camera deive is active, but the
2576 // injection camera has not been injected yet, we need to store the stream configuration of
2577 // the internal camera (because the stream configuration of the injection camera is based
2578 // on the internal camera). When injecting occurs later, this configuration can be used by
2579 // the injection camera.
2580 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2581 " injection camera has not been injected yet.", __FUNCTION__);
2582 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002583 }
2584
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002585 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002586}
2587
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002588status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002589 ATRACE_CALL();
2590 status_t res;
2591
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002592 if (mFakeStreamId != NO_STREAM) {
2593 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002594 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002595 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002596 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002597 return INVALID_OPERATION;
2598 }
2599
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002600 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 sp<Camera3OutputStreamInterface> fakeStream =
2603 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002604
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002605 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002607 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002608 return res;
2609 }
2610
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002611 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002612 mNextStreamId++;
2613
2614 return OK;
2615}
2616
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002617status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002618 ATRACE_CALL();
2619 status_t res;
2620
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002621 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002622 if (mOutputStreams.size() == 1) return OK;
2623
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002624 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002625
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002626 // Ok, have a fake stream and there's at least one other output stream,
2627 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002628
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002629 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002630 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002631 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002632 return INVALID_OPERATION;
2633 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002634 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002635
2636 // Free up the stream endpoint so that it can be used by some other stream
2637 res = deletedStream->disconnect();
2638 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002639 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002640 // fall through since we want to still list the stream as deleted.
2641 }
2642 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002643 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002644
2645 return res;
2646}
2647
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002648void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002649 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002650 Mutex::Autolock l(mLock);
2651 va_list args;
2652 va_start(args, fmt);
2653
2654 setErrorStateLockedV(fmt, args);
2655
2656 va_end(args);
2657}
2658
2659void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002660 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002661 Mutex::Autolock l(mLock);
2662 setErrorStateLockedV(fmt, args);
2663}
2664
2665void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2666 va_list args;
2667 va_start(args, fmt);
2668
2669 setErrorStateLockedV(fmt, args);
2670
2671 va_end(args);
2672}
2673
2674void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002675 // Print out all error messages to log
2676 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002677 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002678
2679 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002680 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002681
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002682 mErrorCause = errorCause;
2683
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002684 if (mRequestThread != nullptr) {
2685 mRequestThread->setPaused(true);
2686 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002687 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002688
2689 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002690 sp<NotificationListener> listener = mListener.promote();
2691 if (listener != NULL) {
2692 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002693 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002694 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002695 }
2696
2697 // Save stack trace. View by dumping it later.
2698 CameraTraces::saveTrace();
2699 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002700}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002701
2702/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002703 * In-flight request management
2704 */
2705
Jianing Weicb0652e2014-03-12 18:29:36 -07002706status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002707 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002708 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002709 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002710 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002711 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002712 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002713 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002714 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002715
2716 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002717 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002718 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002719 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2720 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002721 if (res < 0) return res;
2722
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002723 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002724 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2725 // avoid a deadlock during reprocess requests.
2726 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002727 if (mStatusTracker != nullptr) {
2728 mStatusTracker->markComponentActive(mInFlightStatusId);
2729 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002730 }
2731
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002732 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002733 return OK;
2734}
2735
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002736void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002737 // Indicate idle inFlightMap to the status tracker
2738 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002739 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002740 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2741 // avoid a deadlock during reprocess requests.
2742 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002743 if (mStatusTracker != nullptr) {
2744 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2745 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002746 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002747 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002748}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002749
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002750void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002751 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002752 // something has likely gone wrong. This might still be legit only if application send in
2753 // a long burst of long exposure requests.
2754 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2755 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2756 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2757 mInFlightMap.size(), mExpectedInflightDuration);
2758 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2759 kInFlightWarnLimitHighSpeed) {
2760 CLOGW("In-flight list too large for high speed configuration: %zu,"
2761 "total inflight duration %" PRIu64,
2762 mInFlightMap.size(), mExpectedInflightDuration);
2763 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002764 }
2765}
2766
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002767void Camera3Device::onInflightMapFlushedLocked() {
2768 mExpectedInflightDuration = 0;
2769}
2770
2771void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002772 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002773 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2774 mInFlightMap.removeItemsAt(idx, 1);
2775
2776 onInflightEntryRemovedLocked(duration);
2777}
2778
2779
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002780void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002781 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002782 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002783 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002784 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002785 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002786 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002787
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002788 FlushInflightReqStates states {
2789 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002790 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002791
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002792 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002793}
2794
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002795CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002796 ALOGV("%s", __FUNCTION__);
2797
Igor Murashkin1e479c02013-09-06 16:55:14 -07002798 CameraMetadata retVal;
2799
2800 if (mRequestThread != NULL) {
2801 retVal = mRequestThread->getLatestRequest();
2802 }
2803
Igor Murashkin1e479c02013-09-06 16:55:14 -07002804 return retVal;
2805}
2806
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002807void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002808 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002809 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002810 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2811 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002812
2813 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002814 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002815}
2816
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002817void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002818 std::vector<native_handle_t*> *handles, bool closeFd) {
2819 if (handles == nullptr) {
2820 return;
2821 }
2822 if (closeFd) {
2823 for (auto& handle : *handles) {
2824 native_handle_close(handle);
2825 }
2826 }
2827 for (auto& handle : *handles) {
2828 native_handle_delete(handle);
2829 }
2830 handles->clear();
2831 return;
2832}
2833
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002834/**
2835 * HalInterface inner class methods
2836 */
2837
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002838void Camera3Device::HalInterface::getInflightBufferKeys(
2839 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002840 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002841 return;
2842}
2843
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002844void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2845 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002846 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002847 return;
2848}
2849
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002850bool Camera3Device::HalInterface::verifyBufferIds(
2851 int32_t streamId, std::vector<uint64_t>& bufIds) {
2852 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002853}
2854
2855status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002856 int32_t frameNumber, int32_t streamId,
2857 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002858 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002859}
2860
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002861status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002862 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002863 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002864}
2865
2866// Find and pop a buffer_handle_t based on bufferId
2867status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002868 uint64_t bufferId,
2869 /*out*/ buffer_handle_t** buffer,
2870 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002871 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002872}
2873
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002874std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2875 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002876 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002877}
2878
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002879uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2880 const native_handle_t* handle) {
2881 return mBufferRecords.removeOneBufferCache(streamId, handle);
2882}
2883
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002884void Camera3Device::HalInterface::onBufferFreed(
2885 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002886 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2887 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2888 if (bufferId != BUFFER_ID_NO_BUFFER) {
2889 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002890 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002891}
2892
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002893void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002894 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2895 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2896 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002897 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2898 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002899}
2900
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002901/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002902 * RequestThread inner class methods
2903 */
2904
2905Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002906 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002907 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002908 bool useHalBufManager,
2909 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002910 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002911 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002912 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002913 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002914 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002915 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002916 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002917 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002918 mReconfigured(false),
2919 mDoPause(false),
2920 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002921 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002922 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002923 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002924 mCurrentAfTriggerId(0),
2925 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002926 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00002927 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002928 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002929 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002930 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002931 mRepeatingLastFrameNumber(
2932 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002933 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002934 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002935 mRequestLatency(kRequestLatencyBinSize),
2936 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002937 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002938 mUseHalBufManager(useHalBufManager),
2939 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002940 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002941}
2942
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002943Camera3Device::RequestThread::~RequestThread() {}
2944
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002945void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002946 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002947 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002948 Mutex::Autolock l(mRequestLock);
2949 mListener = listener;
2950}
2951
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002952void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002953 const CameraMetadata& sessionParams,
2954 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002955 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002956 Mutex::Autolock l(mRequestLock);
2957 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002958 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002959 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002960 // Prepare video stream for high speed recording.
2961 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002962 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002963}
2964
Jianing Wei90e59c92014-03-12 18:29:36 -07002965status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002966 List<sp<CaptureRequest> > &requests,
2967 /*out*/
2968 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002969 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002970 Mutex::Autolock l(mRequestLock);
2971 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2972 ++it) {
2973 mRequestQueue.push_back(*it);
2974 }
2975
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002976 if (lastFrameNumber != NULL) {
2977 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2978 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2979 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2980 *lastFrameNumber);
2981 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002982
Jianing Wei90e59c92014-03-12 18:29:36 -07002983 unpauseForNewRequests();
2984
2985 return OK;
2986}
2987
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002988
2989status_t Camera3Device::RequestThread::queueTrigger(
2990 RequestTrigger trigger[],
2991 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002992 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002993 Mutex::Autolock l(mTriggerMutex);
2994 status_t ret;
2995
2996 for (size_t i = 0; i < count; ++i) {
2997 ret = queueTriggerLocked(trigger[i]);
2998
2999 if (ret != OK) {
3000 return ret;
3001 }
3002 }
3003
3004 return OK;
3005}
3006
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003007const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3008 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003009 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003010 if (d != nullptr) return d->mId;
3011 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003012}
3013
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003014status_t Camera3Device::RequestThread::queueTriggerLocked(
3015 RequestTrigger trigger) {
3016
3017 uint32_t tag = trigger.metadataTag;
3018 ssize_t index = mTriggerMap.indexOfKey(tag);
3019
3020 switch (trigger.getTagType()) {
3021 case TYPE_BYTE:
3022 // fall-through
3023 case TYPE_INT32:
3024 break;
3025 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003026 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3027 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003028 return INVALID_OPERATION;
3029 }
3030
3031 /**
3032 * Collect only the latest trigger, since we only have 1 field
3033 * in the request settings per trigger tag, and can't send more than 1
3034 * trigger per request.
3035 */
3036 if (index != NAME_NOT_FOUND) {
3037 mTriggerMap.editValueAt(index) = trigger;
3038 } else {
3039 mTriggerMap.add(tag, trigger);
3040 }
3041
3042 return OK;
3043}
3044
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003045status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003046 const RequestList &requests,
3047 /*out*/
3048 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003049 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003050 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003051 if (lastFrameNumber != NULL) {
3052 *lastFrameNumber = mRepeatingLastFrameNumber;
3053 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003054 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003055 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003056 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3057 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003058
3059 unpauseForNewRequests();
3060
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003061 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003062 return OK;
3063}
3064
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003065bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003066 if (mRepeatingRequests.empty()) {
3067 return false;
3068 }
3069 int32_t requestId = requestIn->mResultExtras.requestId;
3070 const RequestList &repeatRequests = mRepeatingRequests;
3071 // All repeating requests are guaranteed to have same id so only check first quest
3072 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3073 return (firstRequest->mResultExtras.requestId == requestId);
3074}
3075
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003076status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003077 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003078 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003079 return clearRepeatingRequestsLocked(lastFrameNumber);
3080
3081}
3082
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003083status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3084 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003085 std::vector<int32_t> streamIds;
3086 for (const auto& request : mRepeatingRequests) {
3087 for (const auto& stream : request->mOutputStreams) {
3088 streamIds.push_back(stream->getId());
3089 }
3090 }
3091
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003092 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003093 if (lastFrameNumber != NULL) {
3094 *lastFrameNumber = mRepeatingLastFrameNumber;
3095 }
Emilian Peev2295df72021-11-12 18:14:10 -08003096
3097 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3098
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003099 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003100 return OK;
3101}
3102
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003103status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003104 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003105 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003106 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003107 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003108
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003109 // Send errors for all requests pending in the request queue, including
3110 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003111 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003112 if (listener != NULL) {
3113 for (RequestList::iterator it = mRequestQueue.begin();
3114 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003115 // Abort the input buffers for reprocess requests.
3116 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003117 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003118 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003119 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003120 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003121 if (res != OK) {
3122 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3123 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3124 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003125 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003126 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3127 if (res != OK) {
3128 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3129 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3130 }
3131 }
3132 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003133 // Set the frame number this request would have had, if it
3134 // had been submitted; this frame number will not be reused.
3135 // The requestId and burstId fields were set when the request was
3136 // submitted originally (in convertMetadataListToRequestListLocked)
3137 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003138 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003139 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003140 }
3141 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003142 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003143
3144 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003145 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003146 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003147 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003148 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003149 return OK;
3150}
3151
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003152status_t Camera3Device::RequestThread::flush() {
3153 ATRACE_CALL();
3154 Mutex::Autolock l(mFlushLock);
3155
Emilian Peev08dd2452017-04-06 16:55:14 +01003156 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003157}
3158
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003159void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003160 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003161 Mutex::Autolock l(mPauseLock);
3162 mDoPause = paused;
3163 mDoPauseSignal.signal();
3164}
3165
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003166status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3167 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003168 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003169 Mutex::Autolock l(mLatestRequestMutex);
3170 status_t res;
3171 while (mLatestRequestId != requestId) {
3172 nsecs_t startTime = systemTime();
3173
3174 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3175 if (res != OK) return res;
3176
3177 timeout -= (systemTime() - startTime);
3178 }
3179
3180 return OK;
3181}
3182
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003183void Camera3Device::RequestThread::requestExit() {
3184 // Call parent to set up shutdown
3185 Thread::requestExit();
3186 // The exit from any possible waits
3187 mDoPauseSignal.signal();
3188 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003189
3190 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3191 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003192}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003193
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003194void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003195 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003196 bool surfaceAbandoned = false;
3197 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003198 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003199 {
3200 Mutex::Autolock l(mRequestLock);
3201 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3202 // repeating requests.
3203 for (const auto& request : mRepeatingRequests) {
3204 for (const auto& s : request->mOutputStreams) {
3205 if (s->isAbandoned()) {
3206 surfaceAbandoned = true;
3207 clearRepeatingRequestsLocked(&lastFrameNumber);
3208 break;
3209 }
3210 }
3211 if (surfaceAbandoned) {
3212 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003213 }
3214 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003215 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003216 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003217
3218 if (listener != NULL && surfaceAbandoned) {
3219 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003220 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003221}
3222
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003223bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003224 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003225 status_t res;
3226 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003227 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003228 uint32_t numRequestProcessed = 0;
3229 for (size_t i = 0; i < batchSize; i++) {
3230 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003231 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003232 }
3233
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003234 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3235
3236 bool triggerRemoveFailed = false;
3237 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3238 for (size_t i = 0; i < numRequestProcessed; i++) {
3239 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3240 nextRequest.submitted = true;
3241
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003242 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003243
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003244 if (!triggerRemoveFailed) {
3245 // Remove any previously queued triggers (after unlock)
3246 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3247 if (removeTriggerRes != OK) {
3248 triggerRemoveFailed = true;
3249 triggerFailedRequest = nextRequest;
3250 }
3251 }
3252 }
3253
3254 if (triggerRemoveFailed) {
3255 SET_ERR("RequestThread: Unable to remove triggers "
3256 "(capture request %d, HAL device: %s (%d)",
3257 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3258 cleanUpFailedRequests(/*sendRequestError*/ false);
3259 return false;
3260 }
3261
3262 if (res != OK) {
3263 // Should only get a failure here for malformed requests or device-level
3264 // errors, so consider all errors fatal. Bad metadata failures should
3265 // come through notify.
3266 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3267 mNextRequests[numRequestProcessed].halRequest.frame_number,
3268 strerror(-res), res);
3269 cleanUpFailedRequests(/*sendRequestError*/ false);
3270 return false;
3271 }
3272 return true;
3273}
3274
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003275Camera3Device::RequestThread::ExpectedDurationInfo
3276 Camera3Device::RequestThread::calculateExpectedDurationRange(
3277 const camera_metadata_t *request) {
3278 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003279 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003280 InFlightRequest::kDefaultMaxExpectedDuration,
3281 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003282 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3283 find_camera_metadata_ro_entry(request,
3284 ANDROID_CONTROL_AE_MODE,
3285 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003286 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003287
3288 switch (e.data.u8[0]) {
3289 case ANDROID_CONTROL_AE_MODE_OFF:
3290 find_camera_metadata_ro_entry(request,
3291 ANDROID_SENSOR_EXPOSURE_TIME,
3292 &e);
3293 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003294 expectedDurationInfo.minDuration = e.data.i64[0];
3295 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003296 }
3297 find_camera_metadata_ro_entry(request,
3298 ANDROID_SENSOR_FRAME_DURATION,
3299 &e);
3300 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003301 expectedDurationInfo.minDuration =
3302 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3303 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003304 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003305 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003306 break;
3307 default:
3308 find_camera_metadata_ro_entry(request,
3309 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3310 &e);
3311 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003312 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3313 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003314 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003315 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003316 break;
3317 }
3318
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003319 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003320}
3321
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003322bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3323 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3324 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3325 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3326 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3327 return true;
3328 }
3329
3330 return false;
3331}
3332
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003333void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3334 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003335 camera_capture_request_t& halRequest = nextRequest.halRequest;
3336 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003337 Mutex::Autolock al(mLatestRequestMutex);
3338
Shuzhen Wang83bff122020-11-20 15:51:39 -08003339 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003340 mLatestRequest.acquire(cloned);
3341
3342 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003343 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3344 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3345 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003346 CameraMetadata(cloned));
3347 }
3348
3349 sp<Camera3Device> parent = mParent.promote();
3350 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003351 int32_t inputStreamId = -1;
3352 if (halRequest.input_buffer != nullptr) {
3353 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3354 }
3355
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003356 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003357 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003358 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3359 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003360 }
3361 }
3362
Shuzhen Wang83bff122020-11-20 15:51:39 -08003363 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003364 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003365 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003366 }
3367
Shuzhen Wang83bff122020-11-20 15:51:39 -08003368 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003369}
3370
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003371bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3372 ATRACE_CALL();
3373 bool updatesDetected = false;
3374
Emilian Peev4ec17882019-01-24 17:16:58 -08003375 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003376 for (auto tag : mSessionParamKeys) {
3377 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003378 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003379
3380 if (entry.count > 0) {
3381 bool isDifferent = false;
3382 if (lastEntry.count > 0) {
3383 // Have a last value, compare to see if changed
3384 if (lastEntry.type == entry.type &&
3385 lastEntry.count == entry.count) {
3386 // Same type and count, compare values
3387 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3388 size_t entryBytes = bytesPerValue * lastEntry.count;
3389 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3390 if (cmp != 0) {
3391 isDifferent = true;
3392 }
3393 } else {
3394 // Count or type has changed
3395 isDifferent = true;
3396 }
3397 } else {
3398 // No last entry, so always consider to be different
3399 isDifferent = true;
3400 }
3401
3402 if (isDifferent) {
3403 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003404 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3405 updatesDetected = true;
3406 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003407 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003408 }
3409 } else if (lastEntry.count > 0) {
3410 // Value has been removed
3411 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003412 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003413 updatesDetected = true;
3414 }
3415 }
3416
Emilian Peev4ec17882019-01-24 17:16:58 -08003417 bool reconfigureRequired;
3418 if (updatesDetected) {
3419 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3420 updatedParams);
3421 mLatestSessionParams = updatedParams;
3422 } else {
3423 reconfigureRequired = false;
3424 }
3425
3426 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003427}
3428
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003430 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003431 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003432 // Any function called from threadLoop() must not hold mInterfaceLock since
3433 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3434 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003435
3436 // Handle paused state.
3437 if (waitIfPaused()) {
3438 return true;
3439 }
3440
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003441 // Wait for the next batch of requests.
3442 waitForNextRequestBatch();
3443 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003444 return true;
3445 }
3446
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003447 // Get the latest request ID, if any
3448 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003449 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003450 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003451 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003452 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003453 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003454 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3455 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003456 }
3457
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003458 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3459 // or a single request from streaming or burst. In either case the first element
3460 // should contain the latest camera settings that we need to check for any session
3461 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003462 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003463 res = OK;
3464
3465 //Input stream buffers are already acquired at this point so an input stream
3466 //will not be able to move to idle state unless we force it.
3467 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3468 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3469 if (res != OK) {
3470 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3471 cleanUpFailedRequests(/*sendRequestError*/ false);
3472 return false;
3473 }
3474 }
3475
3476 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003477 sp<Camera3Device> parent = mParent.promote();
3478 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003479 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3480 if (statusTracker != nullptr) {
3481 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3482 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003483 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003484 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003485 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003486
3487 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3488 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3489 if (res != OK) {
3490 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3491 cleanUpFailedRequests(/*sendRequestError*/ false);
3492 return false;
3493 }
3494 }
3495 }
3496 }
3497
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003498 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003499 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003500 if (res == TIMED_OUT) {
3501 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003502 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003503 // Check if any stream is abandoned.
3504 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003505 return true;
3506 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003507 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003508 return false;
3509 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003510
Zhijun Hecc27e112013-10-03 16:12:43 -07003511 // Inform waitUntilRequestProcessed thread of a new request ID
3512 {
3513 Mutex::Autolock al(mLatestRequestMutex);
3514
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003515 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003516 mLatestRequestSignal.signal();
3517 }
3518
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003519 // Submit a batch of requests to HAL.
3520 // Use flush lock only when submitting multilple requests in a batch.
3521 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3522 // which may take a long time to finish so synchronizing flush() and
3523 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3524 // For now, only synchronize for high speed recording and we should figure something out for
3525 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003526 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003527
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003528 if (useFlushLock) {
3529 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003530 }
3531
Zhijun Hef0645c12016-08-02 00:58:11 -07003532 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003533 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003534
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003535 sp<Camera3Device> parent = mParent.promote();
3536 if (parent != nullptr) {
3537 parent->mRequestBufferSM.onSubmittingRequest();
3538 }
3539
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003540 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003541 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003542 submitRequestSuccess = sendRequestsBatch();
3543
Shuzhen Wang686f6442017-06-20 16:16:04 -07003544 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3545 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003546
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003547 if (useFlushLock) {
3548 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003549 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003550
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003551 // Unset as current request
3552 {
3553 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003554 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003555 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003556 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003557
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003558 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003559}
3560
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003561status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3562 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3563 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3564 ANDROID_SCALER_CROP_REGION_SET};
3565 if (request == nullptr) {
3566 ALOGE("%s request metadata nullptr", __FUNCTION__);
3567 return BAD_VALUE;
3568 }
3569 status_t res = OK;
3570 for (const auto &key : kFwkOnlyRegionKeys) {
3571 if (request->exists(key)) {
3572 res = request->erase(key);
3573 if (res != OK) {
3574 return res;
3575 }
3576 }
3577 }
3578 return OK;
3579}
3580
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003581status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003582 ATRACE_CALL();
3583
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003584 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003585 for (size_t i = 0; i < mNextRequests.size(); i++) {
3586 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003587 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003588 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3589 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003590
3591 // Prepare a request to HAL
3592 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3593
3594 // Insert any queued triggers (before metadata is locked)
3595 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003596 if (res < 0) {
3597 SET_ERR("RequestThread: Unable to insert triggers "
3598 "(capture request %d, HAL device: %s (%d)",
3599 halRequest->frame_number, strerror(-res), res);
3600 return INVALID_OPERATION;
3601 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003602
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003603 int triggerCount = res;
3604 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3605 mPrevTriggers = triggerCount;
3606
Emilian Peeve23f1d92021-09-20 14:56:01 -07003607 // Do not override rotate&crop for stream configurations that include
3608 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3609 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3610 bool rotateAndCropChanged = mComposerOutput ? false :
3611 overrideAutoRotateAndCrop(captureRequest);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003612 bool autoframingChanged = overrideAutoframing(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003613 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003614
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003615 // If the request is the same as last, or we had triggers now or last time or
3616 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003617 bool newRequest =
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003618 (mPrevRequest != captureRequest || triggersMixedIn || rotateAndCropChanged ||
3619 autoframingChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003620 // Request settings are all the same within one batch, so only treat the first
3621 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003622 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003623 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003624 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003625 /**
3626 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003627 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003628 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003629 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003630 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003631 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003632 "(capture request %d, HAL device: %s (%d)",
3633 halRequest->frame_number, strerror(-res), res);
3634 return INVALID_OPERATION;
3635 }
3636
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003637 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003638 sp<Camera3Device> parent = mParent.promote();
3639 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003640 List<PhysicalCameraSettings>::iterator it;
3641 for (it = captureRequest->mSettingsList.begin();
3642 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003643 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3644 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003645 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3646 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3647 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3648 res);
3649 return INVALID_OPERATION;
3650 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003651 continue;
3652 }
3653
3654 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3655 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3656 updateCaptureRequest(&(it->metadata));
3657 if (res != OK) {
3658 SET_ERR("RequestThread: Unable to correct capture requests "
3659 "for scaler crop region and metering regions for request "
3660 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3661 res);
3662 return INVALID_OPERATION;
3663 }
3664 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003665 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3666 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3667 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3668 res);
3669 return INVALID_OPERATION;
3670 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003671 }
3672 }
3673
3674 // Correct metadata regions for distortion correction if enabled
3675 for (it = captureRequest->mSettingsList.begin();
3676 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003677 if (parent->mDistortionMappers.find(it->cameraId) ==
3678 parent->mDistortionMappers.end()) {
3679 continue;
3680 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003681
3682 if (!captureRequest->mDistortionCorrectionUpdated) {
3683 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3684 &(it->metadata));
3685 if (res != OK) {
3686 SET_ERR("RequestThread: Unable to correct capture requests "
3687 "for lens distortion for request %d: %s (%d)",
3688 halRequest->frame_number, strerror(-res), res);
3689 return INVALID_OPERATION;
3690 }
3691 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003692 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003693 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003694
3695 for (it = captureRequest->mSettingsList.begin();
3696 it != captureRequest->mSettingsList.end(); it++) {
3697 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3698 parent->mZoomRatioMappers.end()) {
3699 continue;
3700 }
3701
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003702 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003703 cameraIdsWithZoom.insert(it->cameraId);
3704 }
3705
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003706 if (!captureRequest->mZoomRatioUpdated) {
3707 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3708 &(it->metadata));
3709 if (res != OK) {
3710 SET_ERR("RequestThread: Unable to correct capture requests "
3711 "for zoom ratio for request %d: %s (%d)",
3712 halRequest->frame_number, strerror(-res), res);
3713 return INVALID_OPERATION;
3714 }
3715 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003716 }
3717 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003718 if (captureRequest->mRotateAndCropAuto &&
3719 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003720 for (it = captureRequest->mSettingsList.begin();
3721 it != captureRequest->mSettingsList.end(); it++) {
3722 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3723 if (mapper != parent->mRotateAndCropMappers.end()) {
3724 res = mapper->second.updateCaptureRequest(&(it->metadata));
3725 if (res != OK) {
3726 SET_ERR("RequestThread: Unable to correct capture requests "
3727 "for rotate-and-crop for request %d: %s (%d)",
3728 halRequest->frame_number, strerror(-res), res);
3729 return INVALID_OPERATION;
3730 }
3731 }
3732 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003733 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003734 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003735 }
3736 }
3737
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003738 /**
3739 * The request should be presorted so accesses in HAL
3740 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3741 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003742 captureRequest->mSettingsList.begin()->metadata.sort();
3743 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003744 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003745 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003746 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3747
3748 IF_ALOGV() {
3749 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3750 find_camera_metadata_ro_entry(
3751 halRequest->settings,
3752 ANDROID_CONTROL_AF_TRIGGER,
3753 &e
3754 );
3755 if (e.count > 0) {
3756 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3757 __FUNCTION__,
3758 halRequest->frame_number,
3759 e.data.u8[0]);
3760 }
3761 }
3762 } else {
3763 // leave request.settings NULL to indicate 'reuse latest given'
3764 ALOGVV("%s: Request settings are REUSED",
3765 __FUNCTION__);
3766 }
3767
Emilian Peevaebbe412018-01-15 13:53:24 +00003768 if (captureRequest->mSettingsList.size() > 1) {
3769 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3770 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003771 if (newRequest) {
3772 halRequest->physcam_settings =
3773 new const camera_metadata* [halRequest->num_physcam_settings];
3774 } else {
3775 halRequest->physcam_settings = nullptr;
3776 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003777 auto it = ++captureRequest->mSettingsList.begin();
3778 size_t i = 0;
3779 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3780 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003781 if (newRequest) {
3782 it->metadata.sort();
3783 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3784 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003785 }
3786 }
3787
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003788 uint32_t totalNumBuffers = 0;
3789
3790 // Fill in buffers
3791 if (captureRequest->mInputStream != NULL) {
3792 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003793
3794 halRequest->input_width = captureRequest->mInputBufferSize.width;
3795 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003796 totalNumBuffers += 1;
3797 } else {
3798 halRequest->input_buffer = NULL;
3799 }
3800
Emilian Peevf4816702020-04-03 15:44:51 -07003801 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003802 captureRequest->mOutputStreams.size());
3803 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003804 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003805
3806 sp<Camera3Device> parent = mParent.promote();
3807 if (parent == NULL) {
3808 // Should not happen, and nowhere to send errors to, so just log it
3809 CLOGE("RequestThread: Parent is gone");
3810 return INVALID_OPERATION;
3811 }
3812 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3813
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003814 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003815 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003816 sp<Camera3OutputStreamInterface> outputStream =
3817 captureRequest->mOutputStreams.editItemAt(j);
3818 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003819
3820 // Prepare video buffers for high speed recording on the first video request.
3821 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3822 // Only try to prepare video stream on the first video request.
3823 mPrepareVideoStream = false;
3824
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003825 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3826 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003827 while (res == NOT_ENOUGH_DATA) {
3828 res = outputStream->prepareNextBuffer();
3829 }
3830 if (res != OK) {
3831 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3832 __FUNCTION__, strerror(-res), res);
3833 outputStream->cancelPrepare();
3834 }
3835 }
3836
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003837 std::vector<size_t> uniqueSurfaceIds;
3838 res = outputStream->getUniqueSurfaceIds(
3839 captureRequest->mOutputSurfaces[streamId],
3840 &uniqueSurfaceIds);
3841 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3842 if (res != OK && res != INVALID_OPERATION) {
3843 ALOGE("%s: failed to query stream %d unique surface IDs",
3844 __FUNCTION__, streamId);
3845 return res;
3846 }
3847 if (res == OK) {
3848 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3849 }
3850
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003851 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003852 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003853 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003854 return TIMED_OUT;
3855 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003856 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003857 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003858 buffer.stream = outputStream->asHalStream();
3859 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003860 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003861 buffer.acquire_fence = -1;
3862 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003863 // Mark the output stream as unpreparable to block clients from calling
3864 // 'prepare' after this request reaches CameraHal and before the respective
3865 // buffers are requested.
3866 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003867 } else {
3868 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3869 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003870 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003871 if (res != OK) {
3872 // Can't get output buffer from gralloc queue - this could be due to
3873 // abandoned queue or other consumer misbehavior, so not a fatal
3874 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003875 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003876 " %s (%d)", strerror(-res), res);
3877
3878 return TIMED_OUT;
3879 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003880 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003881
3882 {
3883 sp<Camera3Device> parent = mParent.promote();
3884 if (parent != nullptr) {
3885 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3886 for (const auto& settings : captureRequest->mSettingsList) {
3887 if ((streamCameraId.isEmpty() &&
3888 parent->getId() == settings.cameraId.c_str()) ||
3889 streamCameraId == settings.cameraId.c_str()) {
3890 outputStream->fireBufferRequestForFrameNumber(
3891 captureRequest->mResultExtras.frameNumber,
3892 settings.metadata);
3893 }
3894 }
3895 }
3896 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003897
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003898 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003899 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3900 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3901 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3902 } else if (!physicalCameraId.isEmpty()) {
3903 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003904 }
3905 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003906 }
3907 totalNumBuffers += halRequest->num_output_buffers;
3908
3909 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003910 // If this request list is for constrained high speed recording (not
3911 // preview), and the current request is not the last one in the batch,
3912 // do not send callback to the app.
3913 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003914 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003915 hasCallback = false;
3916 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003917 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003918 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003919 const camera_metadata_t* settings = halRequest->settings;
3920 bool shouldUnlockSettings = false;
3921 if (settings == nullptr) {
3922 shouldUnlockSettings = true;
3923 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3924 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003925 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3926 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003927 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003928 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3929 isStillCapture = true;
3930 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3931 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003932
Emilian Peevaf8416e2021-02-04 17:52:43 -08003933 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003934 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003935 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3936 isZslCapture = true;
3937 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003938 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003939 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003940 res = parent->registerInFlight(halRequest->frame_number,
3941 totalNumBuffers, captureRequest->mResultExtras,
3942 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003943 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003944 expectedDurationInfo.minDuration,
3945 expectedDurationInfo.maxDuration,
3946 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003947 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003948 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
3949 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003950 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003951 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003952 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3953 ", burstId = %" PRId32 ".",
3954 __FUNCTION__,
3955 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3956 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003957
3958 if (shouldUnlockSettings) {
3959 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3960 }
3961
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003962 if (res != OK) {
3963 SET_ERR("RequestThread: Unable to register new in-flight request:"
3964 " %s (%d)", strerror(-res), res);
3965 return INVALID_OPERATION;
3966 }
3967 }
3968
3969 return OK;
3970}
3971
Igor Murashkin1e479c02013-09-06 16:55:14 -07003972CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003973 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003974 Mutex::Autolock al(mLatestRequestMutex);
3975
3976 ALOGV("RequestThread::%s", __FUNCTION__);
3977
3978 return mLatestRequest;
3979}
3980
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003981bool Camera3Device::RequestThread::isStreamPending(
3982 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003983 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003984 Mutex::Autolock l(mRequestLock);
3985
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003986 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003987 if (!nextRequest.submitted) {
3988 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3989 if (stream == s) return true;
3990 }
3991 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003992 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003993 }
3994
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003995 for (const auto& request : mRequestQueue) {
3996 for (const auto& s : request->mOutputStreams) {
3997 if (stream == s) return true;
3998 }
3999 if (stream == request->mInputStream) return true;
4000 }
4001
4002 for (const auto& request : mRepeatingRequests) {
4003 for (const auto& s : request->mOutputStreams) {
4004 if (stream == s) return true;
4005 }
4006 if (stream == request->mInputStream) return true;
4007 }
4008
4009 return false;
4010}
Jianing Weicb0652e2014-03-12 18:29:36 -07004011
Emilian Peev40ead602017-09-26 15:46:36 +01004012bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4013 ATRACE_CALL();
4014 Mutex::Autolock l(mRequestLock);
4015
4016 for (const auto& nextRequest : mNextRequests) {
4017 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4018 if (s.first == streamId) {
4019 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4020 if (it != s.second.end()) {
4021 return true;
4022 }
4023 }
4024 }
4025 }
4026
4027 for (const auto& request : mRequestQueue) {
4028 for (const auto& s : request->mOutputSurfaces) {
4029 if (s.first == streamId) {
4030 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4031 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004032 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004033 }
4034 }
4035 }
4036 }
4037
4038 for (const auto& request : mRepeatingRequests) {
4039 for (const auto& s : request->mOutputSurfaces) {
4040 if (s.first == streamId) {
4041 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4042 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004043 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004044 }
4045 }
4046 }
4047 }
4048
4049 return false;
4050}
4051
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004052void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4053 if (!mUseHalBufManager) {
4054 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4055 return;
4056 }
4057
4058 Mutex::Autolock pl(mPauseLock);
4059 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004060 mInterface->signalPipelineDrain(streamIds);
4061 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004062 }
4063 // If request thread is still busy, wait until paused then notify HAL
4064 mNotifyPipelineDrain = true;
4065 mStreamIdsToBeDrained = streamIds;
4066}
4067
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004068void Camera3Device::RequestThread::resetPipelineDrain() {
4069 Mutex::Autolock pl(mPauseLock);
4070 mNotifyPipelineDrain = false;
4071 mStreamIdsToBeDrained.clear();
4072}
4073
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004074void Camera3Device::RequestThread::clearPreviousRequest() {
4075 Mutex::Autolock l(mRequestLock);
4076 mPrevRequest.clear();
4077}
4078
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004079status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4080 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4081 ATRACE_CALL();
4082 Mutex::Autolock l(mTriggerMutex);
4083 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4084 return BAD_VALUE;
4085 }
4086 mRotateAndCropOverride = rotateAndCropValue;
4087 return OK;
4088}
4089
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004090status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4091 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4092 ATRACE_CALL();
4093 Mutex::Autolock l(mTriggerMutex);
4094 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
4095 return BAD_VALUE;
4096 }
4097 mAutoframingOverride = autoframingValue;
4098 return OK;
4099}
4100
Emilian Peeve23f1d92021-09-20 14:56:01 -07004101status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4102 ATRACE_CALL();
4103 Mutex::Autolock l(mTriggerMutex);
4104 mComposerOutput = composerSurfacePresent;
4105 return OK;
4106}
4107
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004108status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004109 ATRACE_CALL();
4110 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004111 if (muteMode != mCameraMute) {
4112 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004113 mCameraMuteChanged = true;
4114 }
4115 return OK;
4116}
4117
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004118nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004119 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004120 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004121 return mExpectedInflightDuration > kMinInflightDuration ?
4122 mExpectedInflightDuration : kMinInflightDuration;
4123}
4124
Emilian Peevaebbe412018-01-15 13:53:24 +00004125void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004126 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004127 if ((request == nullptr) || (halRequest == nullptr)) {
4128 ALOGE("%s: Invalid request!", __FUNCTION__);
4129 return;
4130 }
4131
4132 if (halRequest->num_physcam_settings > 0) {
4133 if (halRequest->physcam_id != nullptr) {
4134 delete [] halRequest->physcam_id;
4135 halRequest->physcam_id = nullptr;
4136 }
4137 if (halRequest->physcam_settings != nullptr) {
4138 auto it = ++(request->mSettingsList.begin());
4139 size_t i = 0;
4140 for (; it != request->mSettingsList.end(); it++, i++) {
4141 it->metadata.unlock(halRequest->physcam_settings[i]);
4142 }
4143 delete [] halRequest->physcam_settings;
4144 halRequest->physcam_settings = nullptr;
4145 }
4146 }
4147}
4148
Ravneetaeb20dc2022-03-30 05:33:03 +00004149status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4150 Mutex::Autolock il(mInterfaceLock);
4151 Mutex::Autolock l(mLock);
4152
4153 if (mCameraServiceWatchdog != NULL) {
4154 mCameraServiceWatchdog->setEnabled(enabled);
4155 }
4156
4157 return OK;
4158}
4159
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004160void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4161 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004162 return;
4163 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004164
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004165 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004166 // Skip the ones that have been submitted successfully.
4167 if (nextRequest.submitted) {
4168 continue;
4169 }
4170
4171 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004172 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4173 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004174
4175 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004176 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004177 }
4178
Emilian Peevaebbe412018-01-15 13:53:24 +00004179 cleanupPhysicalSettings(captureRequest, halRequest);
4180
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004181 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004182 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004183 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4184 }
4185
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004186 // No output buffer can be returned when using HAL buffer manager
4187 if (!mUseHalBufManager) {
4188 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4189 //Buffers that failed processing could still have
4190 //valid acquire fence.
4191 int acquireFence = (*outputBuffers)[i].acquire_fence;
4192 if (0 <= acquireFence) {
4193 close(acquireFence);
4194 outputBuffers->editItemAt(i).acquire_fence = -1;
4195 }
Emilian Peevf4816702020-04-03 15:44:51 -07004196 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004197 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4198 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004199 /*timestampIncreasing*/true, std::vector<size_t> (),
4200 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004201 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004202 }
4203
4204 if (sendRequestError) {
4205 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004206 sp<NotificationListener> listener = mListener.promote();
4207 if (listener != NULL) {
4208 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004209 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004210 captureRequest->mResultExtras);
4211 }
4212 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004213
4214 // Remove yet-to-be submitted inflight request from inflightMap
4215 {
4216 sp<Camera3Device> parent = mParent.promote();
4217 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004218 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004219 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4220 if (idx >= 0) {
4221 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4222 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4223 parent->removeInFlightMapEntryLocked(idx);
4224 }
4225 }
4226 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004227 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004228
4229 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004230 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004231}
4232
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004233void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004234 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004235 // Optimized a bit for the simple steady-state case (single repeating
4236 // request), to avoid putting that request in the queue temporarily.
4237 Mutex::Autolock l(mRequestLock);
4238
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004239 assert(mNextRequests.empty());
4240
4241 NextRequest nextRequest;
4242 nextRequest.captureRequest = waitForNextRequestLocked();
4243 if (nextRequest.captureRequest == nullptr) {
4244 return;
4245 }
4246
Emilian Peevf4816702020-04-03 15:44:51 -07004247 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004248 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004249 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004250
4251 // Wait for additional requests
4252 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4253
4254 for (size_t i = 1; i < batchSize; i++) {
4255 NextRequest additionalRequest;
4256 additionalRequest.captureRequest = waitForNextRequestLocked();
4257 if (additionalRequest.captureRequest == nullptr) {
4258 break;
4259 }
4260
Emilian Peevf4816702020-04-03 15:44:51 -07004261 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004262 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004263 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004264 }
4265
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004266 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004267 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004268 mNextRequests.size(), batchSize);
4269 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004270 }
4271
4272 return;
4273}
4274
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004275void Camera3Device::RequestThread::setRequestClearing() {
4276 Mutex::Autolock l(mRequestLock);
4277 mRequestClearing = true;
4278}
4279
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004280sp<Camera3Device::CaptureRequest>
4281 Camera3Device::RequestThread::waitForNextRequestLocked() {
4282 status_t res;
4283 sp<CaptureRequest> nextRequest;
4284
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004285 while (mRequestQueue.empty()) {
4286 if (!mRepeatingRequests.empty()) {
4287 // Always atomically enqueue all requests in a repeating request
4288 // list. Guarantees a complete in-sequence set of captures to
4289 // application.
4290 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004291 if (mFirstRepeating) {
4292 mFirstRepeating = false;
4293 } else {
4294 for (auto& request : requests) {
4295 // For repeating requests, override timestamp request using
4296 // the time a request is inserted into the request queue,
4297 // because the original repeating request will have an old
4298 // fixed timestamp.
4299 request->mRequestTimeNs = systemTime();
4300 }
4301 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004302 RequestList::const_iterator firstRequest =
4303 requests.begin();
4304 nextRequest = *firstRequest;
4305 mRequestQueue.insert(mRequestQueue.end(),
4306 ++firstRequest,
4307 requests.end());
4308 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004309
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004310 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004311
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004312 break;
4313 }
4314
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004315 if (!mRequestClearing) {
4316 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004318
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004319 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4320 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004321 Mutex::Autolock pl(mPauseLock);
4322 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004323 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004324 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004325 if (mNotifyPipelineDrain) {
4326 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4327 mNotifyPipelineDrain = false;
4328 mStreamIdsToBeDrained.clear();
4329 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004330 // Let the tracker know
4331 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4332 if (statusTracker != 0) {
4333 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4334 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004335 sp<Camera3Device> parent = mParent.promote();
4336 if (parent != nullptr) {
4337 parent->mRequestBufferSM.onRequestThreadPaused();
4338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004339 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004340 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004341 // Stop waiting for now and let thread management happen
4342 return NULL;
4343 }
4344 }
4345
4346 if (nextRequest == NULL) {
4347 // Don't have a repeating request already in hand, so queue
4348 // must have an entry now.
4349 RequestList::iterator firstRequest =
4350 mRequestQueue.begin();
4351 nextRequest = *firstRequest;
4352 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004353 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4354 sp<NotificationListener> listener = mListener.promote();
4355 if (listener != NULL) {
4356 listener->notifyRequestQueueEmpty();
4357 }
4358 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004359 }
4360
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004361 // In case we've been unpaused by setPaused clearing mDoPause, need to
4362 // update internal pause state (capture/setRepeatingRequest unpause
4363 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004364 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004365 if (mPaused) {
4366 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4367 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4368 if (statusTracker != 0) {
4369 statusTracker->markComponentActive(mStatusId);
4370 }
4371 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004372 mPaused = false;
4373
4374 // Check if we've reconfigured since last time, and reset the preview
4375 // request if so. Can't use 'NULL request == repeat' across configure calls.
4376 if (mReconfigured) {
4377 mPrevRequest.clear();
4378 mReconfigured = false;
4379 }
4380
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004381 if (nextRequest != NULL) {
4382 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004383 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4384 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004385
4386 // Since RequestThread::clear() removes buffers from the input stream,
4387 // get the right buffer here before unlocking mRequestLock
4388 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004389 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4390 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004391 if (res != OK) {
4392 // Can't get input buffer from gralloc queue - this could be due to
4393 // disconnected queue or other producer misbehavior, so not a fatal
4394 // error
4395 ALOGE("%s: Can't get input buffer, skipping request:"
4396 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004397
4398 sp<NotificationListener> listener = mListener.promote();
4399 if (listener != NULL) {
4400 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004401 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004402 nextRequest->mResultExtras);
4403 }
4404 return NULL;
4405 }
4406 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004407 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004409 return nextRequest;
4410}
4411
4412bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004413 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004414 status_t res;
4415 Mutex::Autolock l(mPauseLock);
4416 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004417 if (mPaused == false) {
4418 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004419 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004420 if (mNotifyPipelineDrain) {
4421 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4422 mNotifyPipelineDrain = false;
4423 mStreamIdsToBeDrained.clear();
4424 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004425 // Let the tracker know
4426 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4427 if (statusTracker != 0) {
4428 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4429 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004430 sp<Camera3Device> parent = mParent.promote();
4431 if (parent != nullptr) {
4432 parent->mRequestBufferSM.onRequestThreadPaused();
4433 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004434 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004435
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004436 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004437 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004438 return true;
4439 }
4440 }
4441 // We don't set mPaused to false here, because waitForNextRequest needs
4442 // to further manage the paused state in case of starvation.
4443 return false;
4444}
4445
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004446void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004447 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004448 // With work to do, mark thread as unpaused.
4449 // If paused by request (setPaused), don't resume, to avoid
4450 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004451 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004452 Mutex::Autolock p(mPauseLock);
4453 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004454 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4455 if (mPaused) {
4456 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4457 if (statusTracker != 0) {
4458 statusTracker->markComponentActive(mStatusId);
4459 }
4460 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004461 mPaused = false;
4462 }
4463}
4464
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004465void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4466 sp<Camera3Device> parent = mParent.promote();
4467 if (parent != NULL) {
4468 va_list args;
4469 va_start(args, fmt);
4470
4471 parent->setErrorStateV(fmt, args);
4472
4473 va_end(args);
4474 }
4475}
4476
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004477status_t Camera3Device::RequestThread::insertTriggers(
4478 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004479 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004480 Mutex::Autolock al(mTriggerMutex);
4481
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004482 sp<Camera3Device> parent = mParent.promote();
4483 if (parent == NULL) {
4484 CLOGE("RequestThread: Parent is gone");
4485 return DEAD_OBJECT;
4486 }
4487
Emilian Peevaebbe412018-01-15 13:53:24 +00004488 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004489 size_t count = mTriggerMap.size();
4490
4491 for (size_t i = 0; i < count; ++i) {
4492 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004493 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004494
4495 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4496 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4497 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004498 if (isAeTrigger) {
4499 request->mResultExtras.precaptureTriggerId = triggerId;
4500 mCurrentPreCaptureTriggerId = triggerId;
4501 } else {
4502 request->mResultExtras.afTriggerId = triggerId;
4503 mCurrentAfTriggerId = triggerId;
4504 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004505 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004506 }
4507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004508 camera_metadata_entry entry = metadata.find(tag);
4509
4510 if (entry.count > 0) {
4511 /**
4512 * Already has an entry for this trigger in the request.
4513 * Rewrite it with our requested trigger value.
4514 */
4515 RequestTrigger oldTrigger = trigger;
4516
4517 oldTrigger.entryValue = entry.data.u8[0];
4518
4519 mTriggerReplacedMap.add(tag, oldTrigger);
4520 } else {
4521 /**
4522 * More typical, no trigger entry, so we just add it
4523 */
4524 mTriggerRemovedMap.add(tag, trigger);
4525 }
4526
4527 status_t res;
4528
4529 switch (trigger.getTagType()) {
4530 case TYPE_BYTE: {
4531 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4532 res = metadata.update(tag,
4533 &entryValue,
4534 /*count*/1);
4535 break;
4536 }
4537 case TYPE_INT32:
4538 res = metadata.update(tag,
4539 &trigger.entryValue,
4540 /*count*/1);
4541 break;
4542 default:
4543 ALOGE("%s: Type not supported: 0x%x",
4544 __FUNCTION__,
4545 trigger.getTagType());
4546 return INVALID_OPERATION;
4547 }
4548
4549 if (res != OK) {
4550 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4551 ", value %d", __FUNCTION__, trigger.getTagName(),
4552 trigger.entryValue);
4553 return res;
4554 }
4555
4556 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4557 trigger.getTagName(),
4558 trigger.entryValue);
4559 }
4560
4561 mTriggerMap.clear();
4562
4563 return count;
4564}
4565
4566status_t Camera3Device::RequestThread::removeTriggers(
4567 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004568 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004569 Mutex::Autolock al(mTriggerMutex);
4570
Emilian Peevaebbe412018-01-15 13:53:24 +00004571 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004572
4573 /**
4574 * Replace all old entries with their old values.
4575 */
4576 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4577 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4578
4579 status_t res;
4580
4581 uint32_t tag = trigger.metadataTag;
4582 switch (trigger.getTagType()) {
4583 case TYPE_BYTE: {
4584 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4585 res = metadata.update(tag,
4586 &entryValue,
4587 /*count*/1);
4588 break;
4589 }
4590 case TYPE_INT32:
4591 res = metadata.update(tag,
4592 &trigger.entryValue,
4593 /*count*/1);
4594 break;
4595 default:
4596 ALOGE("%s: Type not supported: 0x%x",
4597 __FUNCTION__,
4598 trigger.getTagType());
4599 return INVALID_OPERATION;
4600 }
4601
4602 if (res != OK) {
4603 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4604 ", trigger value %d", __FUNCTION__,
4605 trigger.getTagName(), trigger.entryValue);
4606 return res;
4607 }
4608 }
4609 mTriggerReplacedMap.clear();
4610
4611 /**
4612 * Remove all new entries.
4613 */
4614 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4615 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4616 status_t res = metadata.erase(trigger.metadataTag);
4617
4618 if (res != OK) {
4619 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4620 ", trigger value %d", __FUNCTION__,
4621 trigger.getTagName(), trigger.entryValue);
4622 return res;
4623 }
4624 }
4625 mTriggerRemovedMap.clear();
4626
4627 return OK;
4628}
4629
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004630status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004631 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004632 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004633 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004634 status_t res;
4635
Emilian Peevaebbe412018-01-15 13:53:24 +00004636 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004637
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004638 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004639 // exists
4640 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4641 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4642 if (afTrigger.count > 0 &&
4643 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4644 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004645 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004646 if (res != OK) return res;
4647 }
4648
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004649 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004650 // if none already exists
4651 camera_metadata_entry pcTrigger =
4652 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4653 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4654 if (pcTrigger.count > 0 &&
4655 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4656 pcId.count == 0) {
4657 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004658 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004659 if (res != OK) return res;
4660 }
4661
4662 return OK;
4663}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004664
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004665bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4666 const sp<CaptureRequest> &request) {
4667 ATRACE_CALL();
4668
4669 if (request->mRotateAndCropAuto) {
4670 Mutex::Autolock l(mTriggerMutex);
4671 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4672
4673 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4674 if (rotateAndCropEntry.count > 0) {
4675 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4676 return false;
4677 } else {
4678 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4679 return true;
4680 }
4681 } else {
4682 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4683 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4684 &rotateAndCrop_u8, 1);
4685 return true;
4686 }
4687 }
4688 return false;
4689}
4690
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004691bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4692 ATRACE_CALL();
4693
4694 if (request->mAutoframingAuto) {
4695 Mutex::Autolock l(mTriggerMutex);
4696 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4697
4698 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4699 if (autoframingEntry.count > 0) {
4700 if (autoframingEntry.data.u8[0] == mAutoframingOverride) {
4701 return false;
4702 } else {
4703 autoframingEntry.data.u8[0] = mAutoframingOverride;
4704 return true;
4705 }
4706 } else {
4707 uint8_t autoframing_u8 = mAutoframingOverride;
4708 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4709 &autoframing_u8, 1);
4710 return true;
4711 }
4712 }
4713 return false;
4714}
4715
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004716bool Camera3Device::RequestThread::overrideTestPattern(
4717 const sp<CaptureRequest> &request) {
4718 ATRACE_CALL();
4719
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004720 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004721
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004722 Mutex::Autolock l(mTriggerMutex);
4723
4724 bool changed = false;
4725
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004726 // For a multi-camera, the physical cameras support the same set of
4727 // test pattern modes as the logical camera.
4728 for (auto& settings : request->mSettingsList) {
4729 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004730
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004731 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4732 int32_t testPatternData[4] = {
4733 settings.mOriginalTestPatternData[0],
4734 settings.mOriginalTestPatternData[1],
4735 settings.mOriginalTestPatternData[2],
4736 settings.mOriginalTestPatternData[3]
4737 };
4738 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4739 testPatternMode = mCameraMute;
4740 testPatternData[0] = 0;
4741 testPatternData[1] = 0;
4742 testPatternData[2] = 0;
4743 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004744 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004745
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004746 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4747 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4748 if (testPatternEntry.count > 0) {
4749 if (testPatternEntry.data.i32[0] != testPatternMode) {
4750 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004751 changed = true;
4752 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004753 } else if (supportTestPatternModeKey) {
4754 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4755 &testPatternMode, 1);
4756 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004757 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004758
4759 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4760 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4761 if (testPatternColor.count >= 4) {
4762 for (size_t i = 0; i < 4; i++) {
4763 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4764 testPatternColor.data.i32[i] = testPatternData[i];
4765 changed = true;
4766 }
4767 }
4768 } else if (supportTestPatternDataKey) {
4769 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4770 testPatternData, 4);
4771 changed = true;
4772 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004773 }
4774
4775 return changed;
4776}
4777
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004778status_t Camera3Device::RequestThread::setHalInterface(
4779 sp<HalInterface> newHalInterface) {
4780 if (newHalInterface.get() == nullptr) {
4781 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4782 return DEAD_OBJECT;
4783 }
4784
4785 mInterface = newHalInterface;
4786
4787 return OK;
4788}
4789
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004790/**
4791 * PreparerThread inner class methods
4792 */
4793
4794Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004795 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004796 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004797}
4798
4799Camera3Device::PreparerThread::~PreparerThread() {
4800 Thread::requestExitAndWait();
4801 if (mCurrentStream != nullptr) {
4802 mCurrentStream->cancelPrepare();
4803 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4804 mCurrentStream.clear();
4805 }
4806 clear();
4807}
4808
Ruben Brunkc78ac262015-08-13 17:58:46 -07004809status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004810 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004811 status_t res;
4812
4813 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004814 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004815
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004816 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004817 if (res == OK) {
4818 // No preparation needed, fire listener right off
4819 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004820 if (listener != NULL) {
4821 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004822 }
4823 return OK;
4824 } else if (res != NOT_ENOUGH_DATA) {
4825 return res;
4826 }
4827
4828 // Need to prepare, start up thread if necessary
4829 if (!mActive) {
4830 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4831 // isn't running
4832 Thread::requestExitAndWait();
4833 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4834 if (res != OK) {
4835 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004836 if (listener != NULL) {
4837 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004838 }
4839 return res;
4840 }
4841 mCancelNow = false;
4842 mActive = true;
4843 ALOGV("%s: Preparer stream started", __FUNCTION__);
4844 }
4845
4846 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004847 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004848 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4849
4850 return OK;
4851}
4852
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004853void Camera3Device::PreparerThread::pause() {
4854 ATRACE_CALL();
4855
4856 Mutex::Autolock l(mLock);
4857
4858 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4859 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4860 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4861 int currentMaxCount = mCurrentMaxCount;
4862 mPendingStreams.clear();
4863 mCancelNow = true;
4864 while (mActive) {
4865 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4866 if (res == TIMED_OUT) {
4867 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4868 return;
4869 } else if (res != OK) {
4870 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4871 return;
4872 }
4873 }
4874
4875 //Check whether the prepare thread was able to complete the current
4876 //stream. In case work is still pending emplace it along with the rest
4877 //of the streams in the pending list.
4878 if (currentStream != nullptr) {
4879 if (!mCurrentPrepareComplete) {
4880 pendingStreams.emplace(currentMaxCount, currentStream);
4881 }
4882 }
4883
4884 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4885 for (const auto& it : mPendingStreams) {
4886 it.second->cancelPrepare();
4887 }
4888}
4889
4890status_t Camera3Device::PreparerThread::resume() {
4891 ATRACE_CALL();
4892 status_t res;
4893
4894 Mutex::Autolock l(mLock);
4895 sp<NotificationListener> listener = mListener.promote();
4896
4897 if (mActive) {
4898 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4899 return NO_INIT;
4900 }
4901
4902 auto it = mPendingStreams.begin();
4903 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004904 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004905 if (res == OK) {
4906 if (listener != NULL) {
4907 listener->notifyPrepared(it->second->getId());
4908 }
4909 it = mPendingStreams.erase(it);
4910 } else if (res != NOT_ENOUGH_DATA) {
4911 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4912 res, strerror(-res));
4913 it = mPendingStreams.erase(it);
4914 } else {
4915 it++;
4916 }
4917 }
4918
4919 if (mPendingStreams.empty()) {
4920 return OK;
4921 }
4922
4923 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4924 if (res != OK) {
4925 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4926 __FUNCTION__, res, strerror(-res));
4927 return res;
4928 }
4929 mCancelNow = false;
4930 mActive = true;
4931 ALOGV("%s: Preparer stream started", __FUNCTION__);
4932
4933 return OK;
4934}
4935
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004936status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004937 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004938 Mutex::Autolock l(mLock);
4939
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004940 for (const auto& it : mPendingStreams) {
4941 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004942 }
4943 mPendingStreams.clear();
4944 mCancelNow = true;
4945
4946 return OK;
4947}
4948
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004949void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004950 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004951 Mutex::Autolock l(mLock);
4952 mListener = listener;
4953}
4954
4955bool Camera3Device::PreparerThread::threadLoop() {
4956 status_t res;
4957 {
4958 Mutex::Autolock l(mLock);
4959 if (mCurrentStream == nullptr) {
4960 // End thread if done with work
4961 if (mPendingStreams.empty()) {
4962 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4963 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4964 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4965 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004966 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004967 return false;
4968 }
4969
4970 // Get next stream to prepare
4971 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004972 mCurrentStream = it->second;
4973 mCurrentMaxCount = it->first;
4974 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004975 mPendingStreams.erase(it);
4976 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4977 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4978 } else if (mCancelNow) {
4979 mCurrentStream->cancelPrepare();
4980 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4981 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4982 mCurrentStream.clear();
4983 mCancelNow = false;
4984 return true;
4985 }
4986 }
4987
4988 res = mCurrentStream->prepareNextBuffer();
4989 if (res == NOT_ENOUGH_DATA) return true;
4990 if (res != OK) {
4991 // Something bad happened; try to recover by cancelling prepare and
4992 // signalling listener anyway
4993 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4994 mCurrentStream->getId(), res, strerror(-res));
4995 mCurrentStream->cancelPrepare();
4996 }
4997
4998 // This stream has finished, notify listener
4999 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005000 sp<NotificationListener> listener = mListener.promote();
5001 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005002 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5003 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005004 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005005 }
5006
5007 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5008 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005009 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005010
5011 return true;
5012}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005013
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005014status_t Camera3Device::RequestBufferStateMachine::initialize(
5015 sp<camera3::StatusTracker> statusTracker) {
5016 if (statusTracker == nullptr) {
5017 ALOGE("%s: statusTracker is null", __FUNCTION__);
5018 return BAD_VALUE;
5019 }
5020
5021 std::lock_guard<std::mutex> lock(mLock);
5022 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005023 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005024 return OK;
5025}
5026
5027bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5028 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005029 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005030 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005031 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005032 return true;
5033 }
5034 return false;
5035}
5036
5037void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5038 std::lock_guard<std::mutex> lock(mLock);
5039 if (!mRequestBufferOngoing) {
5040 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5041 return;
5042 }
5043 mRequestBufferOngoing = false;
5044 if (mStatus == RB_STATUS_PENDING_STOP) {
5045 checkSwitchToStopLocked();
5046 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005047 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005048}
5049
5050void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5051 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005052 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005053 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005054 return;
5055}
5056
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005057void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005058 std::lock_guard<std::mutex> lock(mLock);
5059 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005060 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5061 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005062 mInflightMapEmpty = false;
5063 if (mStatus == RB_STATUS_STOPPED) {
5064 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005065 }
5066 return;
5067}
5068
5069void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5070 std::lock_guard<std::mutex> lock(mLock);
5071 mRequestThreadPaused = true;
5072 if (mStatus == RB_STATUS_PENDING_STOP) {
5073 checkSwitchToStopLocked();
5074 }
5075 return;
5076}
5077
5078void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5079 std::lock_guard<std::mutex> lock(mLock);
5080 mInflightMapEmpty = true;
5081 if (mStatus == RB_STATUS_PENDING_STOP) {
5082 checkSwitchToStopLocked();
5083 }
5084 return;
5085}
5086
5087void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5088 std::lock_guard<std::mutex> lock(mLock);
5089 if (!checkSwitchToStopLocked()) {
5090 mStatus = RB_STATUS_PENDING_STOP;
5091 }
5092 return;
5093}
5094
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005095bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5096 std::lock_guard<std::mutex> lock(mLock);
5097 if (mRequestBufferOngoing) {
5098 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5099 __FUNCTION__);
5100 return false;
5101 }
5102 mSwitchedToOffline = true;
5103 mInflightMapEmpty = true;
5104 mRequestThreadPaused = true;
5105 mStatus = RB_STATUS_STOPPED;
5106 return true;
5107}
5108
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005109void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5110 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5111 if (statusTracker != nullptr) {
5112 if (active) {
5113 statusTracker->markComponentActive(mRequestBufferStatusId);
5114 } else {
5115 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5116 }
5117 }
5118}
5119
5120bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5121 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5122 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005123 return true;
5124 }
5125 return false;
5126}
5127
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005128bool Camera3Device::startRequestBuffer() {
5129 return mRequestBufferSM.startRequestBuffer();
5130}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005131
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005132void Camera3Device::endRequestBuffer() {
5133 mRequestBufferSM.endRequestBuffer();
5134}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005135
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005136nsecs_t Camera3Device::getWaitDuration() {
5137 return kBaseGetBufferWait + getExpectedInFlightDuration();
5138}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005139
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005140void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5141 mInterface->getInflightBufferKeys(out);
5142}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005143
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005144void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5145 mInterface->getInflightRequestBufferKeys(out);
5146}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005147
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005148std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5149 std::vector<sp<Camera3StreamInterface>> ret;
5150 bool hasInputStream = mInputStream != nullptr;
5151 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5152 if (hasInputStream) {
5153 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005154 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005155 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5156 ret.push_back(mOutputStreams[i]);
5157 }
5158 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5159 ret.push_back(mDeletedStreams[i]);
5160 }
5161 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005162}
5163
Emilian Peevcc0b7952020-01-07 13:54:47 -08005164void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5165 ATRACE_CALL();
5166
5167 if (offlineStreamIds == nullptr) {
5168 return;
5169 }
5170
5171 Mutex::Autolock il(mInterfaceLock);
5172
5173 auto streamIds = mOutputStreams.getStreamIds();
5174 bool hasInputStream = mInputStream != nullptr;
5175 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5176 offlineStreamIds->push_back(mInputStream->getId());
5177 }
5178
5179 for (const auto & streamId : streamIds) {
5180 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5181 // Streams that use the camera buffer manager are currently not supported in
5182 // offline mode
5183 if (stream->getOfflineProcessingSupport() &&
5184 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5185 offlineStreamIds->push_back(streamId);
5186 }
5187 }
5188}
5189
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005190status_t Camera3Device::setRotateAndCropAutoBehavior(
5191 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5192 ATRACE_CALL();
5193 Mutex::Autolock il(mInterfaceLock);
5194 Mutex::Autolock l(mLock);
5195 if (mRequestThread == nullptr) {
5196 return INVALID_OPERATION;
5197 }
5198 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5199}
5200
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005201status_t Camera3Device::setAutoframingAutoBehavior(
5202 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5203 ATRACE_CALL();
5204 Mutex::Autolock il(mInterfaceLock);
5205 Mutex::Autolock l(mLock);
5206 if (mRequestThread == nullptr) {
5207 return INVALID_OPERATION;
5208 }
5209 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5210}
5211
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005212bool Camera3Device::supportsCameraMute() {
5213 Mutex::Autolock il(mInterfaceLock);
5214 Mutex::Autolock l(mLock);
5215
5216 return mSupportCameraMute;
5217}
5218
5219status_t Camera3Device::setCameraMute(bool enabled) {
5220 ATRACE_CALL();
5221 Mutex::Autolock il(mInterfaceLock);
5222 Mutex::Autolock l(mLock);
5223
5224 if (mRequestThread == nullptr || !mSupportCameraMute) {
5225 return INVALID_OPERATION;
5226 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005227 int32_t muteMode =
5228 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5229 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5230 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5231 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005232}
5233
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005234status_t Camera3Device::injectCamera(const String8& injectedCamId,
5235 sp<CameraProviderManager> manager) {
5236 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5237 ATRACE_CALL();
5238 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005239 // When the camera device is active, injectCamera() and stopInjection() will call
5240 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5241 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5242 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5243 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5244 // waitUntilStateThenRelock().
5245 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005246
5247 status_t res = NO_ERROR;
5248 if (mInjectionMethods->isInjecting()) {
5249 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5250 return OK;
5251 } else {
5252 res = mInjectionMethods->stopInjection();
5253 if (res != OK) {
5254 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5255 __FUNCTION__, res);
5256 return res;
5257 }
5258 }
5259 }
5260
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005261 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005262 if (res != OK) {
5263 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5264 __FUNCTION__, res);
5265 return res;
5266 }
5267
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005268 // When the second display of android is cast to the remote device, and the opened camera is
5269 // also cast to the second display, in this case, because the camera has configured the streams
5270 // at this time, we can directly call injectCamera() to replace the internal camera with
5271 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005272 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5273 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5274
5275 camera3::camera_stream_configuration injectionConfig;
5276 std::vector<uint32_t> injectionBufferSizes;
5277 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5278 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5279 || injectionBufferSizes.size() <= 0) {
5280 ALOGE("Failed to inject camera due to abandoned configuration! "
5281 "mOperatingMode: %d injectionConfig.num_streams: %d "
5282 "injectionBufferSizes.size(): %zu", mOperatingMode,
5283 injectionConfig.num_streams, injectionBufferSizes.size());
5284 return DEAD_OBJECT;
5285 }
5286
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005287 res = mInjectionMethods->injectCamera(
5288 injectionConfig, injectionBufferSizes);
5289 if (res != OK) {
5290 ALOGE("Can't finish inject camera process!");
5291 return res;
5292 }
5293 }
5294
5295 return OK;
5296}
5297
5298status_t Camera3Device::stopInjection() {
5299 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5300 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005301 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005302 return mInjectionMethods->stopInjection();
5303}
5304
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005305}; // namespace android