blob: 1d54b17bafaaaf6bf0e433ccfd1860f5b643756a [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
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002150 auto zoomRatioEntry =
2151 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2152 if (zoomRatioEntry.count > 0 &&
2153 zoomRatioEntry.data.f[0] == 1.0f) {
2154 newRequest->mZoomRatioIs1x = true;
2155 } else {
2156 newRequest->mZoomRatioIs1x = false;
2157 }
2158
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002159 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002160 for (auto& settings : newRequest->mSettingsList) {
2161 auto testPatternModeEntry =
2162 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2163 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2164 testPatternModeEntry.data.i32[0] :
2165 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002166
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002167 auto testPatternDataEntry =
2168 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2169 if (testPatternDataEntry.count >= 4) {
2170 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2171 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2172 } else {
2173 settings.mOriginalTestPatternData[0] = 0;
2174 settings.mOriginalTestPatternData[1] = 0;
2175 settings.mOriginalTestPatternData[2] = 0;
2176 settings.mOriginalTestPatternData[3] = 0;
2177 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002178 }
2179 }
2180
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002181 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002182}
2183
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002184void Camera3Device::cancelStreamsConfigurationLocked() {
2185 int res = OK;
2186 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2187 res = mInputStream->cancelConfiguration();
2188 if (res != OK) {
2189 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2190 mInputStream->getId(), strerror(-res), res);
2191 }
2192 }
2193
2194 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002195 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002196 if (outputStream->isConfiguring()) {
2197 res = outputStream->cancelConfiguration();
2198 if (res != OK) {
2199 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2200 outputStream->getId(), strerror(-res), res);
2201 }
2202 }
2203 }
2204
2205 // Return state to that at start of call, so that future configures
2206 // properly clean things up
2207 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2208 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002209
2210 res = mPreparerThread->resume();
2211 if (res != OK) {
2212 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2213 }
2214}
2215
Emilian Peev0d0191e2020-04-21 17:01:18 -07002216bool Camera3Device::checkAbandonedStreamsLocked() {
2217 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2218 return true;
2219 }
2220
2221 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2222 auto stream = mOutputStreams[i];
2223 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2224 return true;
2225 }
2226 }
2227
2228 return false;
2229}
2230
Emilian Peev3bead5f2020-05-28 17:29:08 -07002231bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002232 ATRACE_CALL();
2233 bool ret = false;
2234
Shuzhen Wang316781a2020-08-18 18:11:01 -07002235 nsecs_t startTime = systemTime();
2236
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002237 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002238 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2239
2240 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002241 if (checkAbandonedStreamsLocked()) {
2242 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2243 __FUNCTION__);
2244 return true;
2245 }
2246
Emilian Peev3bead5f2020-05-28 17:29:08 -07002247 status_t rc = NO_ERROR;
2248 bool markClientActive = false;
2249 if (mStatus == STATUS_ACTIVE) {
2250 markClientActive = true;
2251 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002252
2253 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2254 }
2255
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002256 if (rc == NO_ERROR) {
2257 mNeedConfig = true;
2258 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2259 if (rc == NO_ERROR) {
2260 ret = true;
2261 mPauseStateNotify = false;
2262 //Moving to active state while holding 'mLock' is important.
2263 //There could be pending calls to 'create-/deleteStream' which
2264 //will trigger another stream configuration while the already
2265 //present streams end up with outstanding buffers that will
2266 //not get drained.
2267 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002268 } else if (rc == DEAD_OBJECT) {
2269 // DEAD_OBJECT can be returned if either the consumer surface is
2270 // abandoned, or the HAL has died.
2271 // - If the HAL has died, configureStreamsLocked call will set
2272 // device to error state,
2273 // - If surface is abandoned, we should not set device to error
2274 // state.
2275 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002276 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002277 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002278 }
2279 } else {
2280 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2281 }
2282
Austin Borger74fca042022-05-23 12:41:21 -07002283 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002284 ns2ms(systemTime() - startTime));
2285
Emilian Peev3bead5f2020-05-28 17:29:08 -07002286 if (markClientActive) {
2287 mStatusTracker->markComponentActive(clientStatusId);
2288 }
2289
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002290 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002291}
2292
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002293status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002294 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295 ATRACE_CALL();
2296 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002297
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002298 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002299 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002300 return INVALID_OPERATION;
2301 }
2302
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002303 if (operatingMode < 0) {
2304 CLOGE("Invalid operating mode: %d", operatingMode);
2305 return BAD_VALUE;
2306 }
2307
2308 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002309 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002310
2311 if (mOperatingMode != operatingMode) {
2312 mNeedConfig = true;
2313 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2314 mOperatingMode = operatingMode;
2315 }
2316
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002317 // Reset min expected duration when session is reconfigured.
2318 mMinExpectedDuration = 0;
2319
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002320 // In case called from configureStreams, abort queued input buffers not belonging to
2321 // any pending requests.
2322 if (mInputStream != NULL && notifyRequestThread) {
2323 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002324 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002325 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002326 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002327 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002328 if (res != OK) {
2329 // Exhausted acquiring all input buffers.
2330 break;
2331 }
2332
Emilian Peevf4816702020-04-03 15:44:51 -07002333 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002334 res = mInputStream->returnInputBuffer(inputBuffer);
2335 if (res != OK) {
2336 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2337 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2338 }
2339 }
2340 }
2341
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002342 if (!mNeedConfig) {
2343 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2344 return OK;
2345 }
2346
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002347 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002348 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002349 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2350 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002351 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002352 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002353 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002354 }
2355
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002356 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002357 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002358
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002359 mPreparerThread->pause();
2360
Emilian Peevf4816702020-04-03 15:44:51 -07002361 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002362 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002363 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002364 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365
Emilian Peevf4816702020-04-03 15:44:51 -07002366 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002368 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002369
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002370
2371 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002372 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373 inputStream = mInputStream->startConfiguration();
2374 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002375 CLOGE("Can't start input stream configuration");
2376 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 return INVALID_OPERATION;
2378 }
2379 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002380
2381 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382 }
2383
Shuzhen Wang99080502021-03-07 21:08:20 -08002384 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002385 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002386 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002387
2388 // Don't configure bidi streams twice, nor add them twice to the list
2389 if (mOutputStreams[i].get() ==
2390 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2391
2392 config.num_streams--;
2393 continue;
2394 }
2395
Emilian Peevf4816702020-04-03 15:44:51 -07002396 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002397 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002398 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002399 CLOGE("Can't start output stream configuration");
2400 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002401 return INVALID_OPERATION;
2402 }
2403 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002404
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002405 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002406 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2407 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002408 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2409 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002410 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2411 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002412 } else if (outputStream->data_space ==
2413 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2414 bufferSizes[k] = outputStream->width * outputStream->height;
2415 } else {
2416 ALOGW("%s: Blob dataSpace %d not supported",
2417 __FUNCTION__, outputStream->data_space);
2418 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002419 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002420
2421 if (mOutputStreams[i]->isMultiResolution()) {
2422 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2423 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2424 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2425 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002426
2427 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2428 composerSurfacePresent = true;
2429 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002430 }
2431
2432 config.streams = streams.editArray();
2433
2434 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002435 // max_buffers, usage, and priv fields, as well as data_space and format
2436 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002437
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002438 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002439 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002440 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002441
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002442 if (res == BAD_VALUE) {
2443 // HAL rejected this set of streams as unsupported, clean up config
2444 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002445 CLOGE("Set of requested inputs/outputs not supported by HAL");
2446 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447 return BAD_VALUE;
2448 } else if (res != OK) {
2449 // Some other kind of error from configure_streams - this is not
2450 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002451 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2452 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453 return res;
2454 }
2455
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002456 // Finish all stream configuration immediately.
2457 // TODO: Try to relax this later back to lazy completion, which should be
2458 // faster
2459
Igor Murashkin073f8572013-05-02 14:59:28 -07002460 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002461 bool streamReConfigured = false;
2462 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002463 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002464 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002465 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002466 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002467 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2468 return DEAD_OBJECT;
2469 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002470 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002471 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002472 if (streamReConfigured) {
2473 mInterface->onStreamReConfigured(mInputStream->getId());
2474 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002475 }
2476
2477 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002478 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002479 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002480 bool streamReConfigured = false;
2481 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002482 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002483 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002484 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002485 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002486 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2487 return DEAD_OBJECT;
2488 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002489 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002490 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002491 if (streamReConfigured) {
2492 mInterface->onStreamReConfigured(outputStream->getId());
2493 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002494 }
2495 }
2496
Emilian Peeve23f1d92021-09-20 14:56:01 -07002497 mRequestThread->setComposerSurface(composerSurfacePresent);
2498
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002499 // Request thread needs to know to avoid using repeat-last-settings protocol
2500 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002501 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002502 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2503 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002504 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002505
Zhijun He90f7c372016-08-16 16:19:43 -07002506 char value[PROPERTY_VALUE_MAX];
2507 property_get("camera.fifo.disable", value, "0");
2508 int32_t disableFifo = atoi(value);
2509 if (disableFifo != 1) {
2510 // Boost priority of request thread to SCHED_FIFO.
2511 pid_t requestThreadTid = mRequestThread->getTid();
2512 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002513 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002514 if (res != OK) {
2515 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2516 strerror(-res), res);
2517 } else {
2518 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2519 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002520 }
2521
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002522 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002523 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2524 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2525 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2526 sessionParams.unlock(newSessionParams);
2527 mSessionParams.unlock(currentSessionParams);
2528 if (updateSessionParams) {
2529 mSessionParams = sessionParams;
2530 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002531
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002532 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002533
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002534 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002535 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002536
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002537 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002538
Zhijun He0a210512014-07-24 13:45:15 -07002539 // tear down the deleted streams after configure streams.
2540 mDeletedStreams.clear();
2541
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002542 auto rc = mPreparerThread->resume();
2543 if (rc != OK) {
2544 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2545 return rc;
2546 }
2547
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002548 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002549 mRequestBufferSM.onStreamsConfigured();
2550 }
2551
Cliff Wu3b268182021-07-06 15:44:43 +08002552 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002553 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002554 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002555 // configure the injection streams.
2556 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002557 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002558 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2559 res = mInjectionMethods->injectCamera(config, bufferSizes);
2560 if (res != OK) {
2561 ALOGE("Can't finish inject camera process!");
2562 return res;
2563 }
Cliff Wu3b268182021-07-06 15:44:43 +08002564 } else {
2565 // First run configureStreamsLocked() and then call injectCamera() case:
2566 // If the stream configuration has been completed and camera deive is active, but the
2567 // injection camera has not been injected yet, we need to store the stream configuration of
2568 // the internal camera (because the stream configuration of the injection camera is based
2569 // on the internal camera). When injecting occurs later, this configuration can be used by
2570 // the injection camera.
2571 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2572 " injection camera has not been injected yet.", __FUNCTION__);
2573 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002574 }
2575
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002576 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002577}
2578
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002579status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002580 ATRACE_CALL();
2581 status_t res;
2582
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002583 if (mFakeStreamId != NO_STREAM) {
2584 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002585 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002586 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002587 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002588 return INVALID_OPERATION;
2589 }
2590
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002591 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002592
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002593 sp<Camera3OutputStreamInterface> fakeStream =
2594 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002595
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002596 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002597 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002598 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002599 return res;
2600 }
2601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002603 mNextStreamId++;
2604
2605 return OK;
2606}
2607
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002608status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002609 ATRACE_CALL();
2610 status_t res;
2611
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002612 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002613 if (mOutputStreams.size() == 1) return OK;
2614
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002615 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002616
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002617 // Ok, have a fake stream and there's at least one other output stream,
2618 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002619
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002620 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002621 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002622 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002623 return INVALID_OPERATION;
2624 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002625 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002626
2627 // Free up the stream endpoint so that it can be used by some other stream
2628 res = deletedStream->disconnect();
2629 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002630 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002631 // fall through since we want to still list the stream as deleted.
2632 }
2633 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002634 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002635
2636 return res;
2637}
2638
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002639void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002640 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002641 Mutex::Autolock l(mLock);
2642 va_list args;
2643 va_start(args, fmt);
2644
2645 setErrorStateLockedV(fmt, args);
2646
2647 va_end(args);
2648}
2649
2650void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002651 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002652 Mutex::Autolock l(mLock);
2653 setErrorStateLockedV(fmt, args);
2654}
2655
2656void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2657 va_list args;
2658 va_start(args, fmt);
2659
2660 setErrorStateLockedV(fmt, args);
2661
2662 va_end(args);
2663}
2664
2665void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002666 // Print out all error messages to log
2667 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002668 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002669
2670 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002671 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002672
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002673 mErrorCause = errorCause;
2674
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002675 if (mRequestThread != nullptr) {
2676 mRequestThread->setPaused(true);
2677 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002678 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002679
2680 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002681 sp<NotificationListener> listener = mListener.promote();
2682 if (listener != NULL) {
2683 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002684 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002685 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002686 }
2687
2688 // Save stack trace. View by dumping it later.
2689 CameraTraces::saveTrace();
2690 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002691}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002692
2693/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002694 * In-flight request management
2695 */
2696
Jianing Weicb0652e2014-03-12 18:29:36 -07002697status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002698 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002699 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002700 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Shuzhen Wang99080502021-03-07 21:08:20 -08002701 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2702 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002703 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002704 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002705 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002706
2707 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002708 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002709 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002710 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2711 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002712 if (res < 0) return res;
2713
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002714 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002715 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2716 // avoid a deadlock during reprocess requests.
2717 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002718 if (mStatusTracker != nullptr) {
2719 mStatusTracker->markComponentActive(mInFlightStatusId);
2720 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002721 }
2722
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002723 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002724 return OK;
2725}
2726
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002727void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002728 // Indicate idle inFlightMap to the status tracker
2729 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002730 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002731 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2732 // avoid a deadlock during reprocess requests.
2733 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002734 if (mStatusTracker != nullptr) {
2735 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2736 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002737 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002738 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002739}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002740
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002741void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002742 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002743 // something has likely gone wrong. This might still be legit only if application send in
2744 // a long burst of long exposure requests.
2745 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2746 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2747 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2748 mInFlightMap.size(), mExpectedInflightDuration);
2749 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2750 kInFlightWarnLimitHighSpeed) {
2751 CLOGW("In-flight list too large for high speed configuration: %zu,"
2752 "total inflight duration %" PRIu64,
2753 mInFlightMap.size(), mExpectedInflightDuration);
2754 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002755 }
2756}
2757
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002758void Camera3Device::onInflightMapFlushedLocked() {
2759 mExpectedInflightDuration = 0;
2760}
2761
2762void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002763 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002764 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2765 mInFlightMap.removeItemsAt(idx, 1);
2766
2767 onInflightEntryRemovedLocked(duration);
2768}
2769
2770
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002771void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002772 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002773 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002774 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002775 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002776 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002777 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002778
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002779 FlushInflightReqStates states {
2780 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002781 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002782
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002783 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002784}
2785
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002786CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002787 ALOGV("%s", __FUNCTION__);
2788
Igor Murashkin1e479c02013-09-06 16:55:14 -07002789 CameraMetadata retVal;
2790
2791 if (mRequestThread != NULL) {
2792 retVal = mRequestThread->getLatestRequest();
2793 }
2794
Igor Murashkin1e479c02013-09-06 16:55:14 -07002795 return retVal;
2796}
2797
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002798void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002799 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002800 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002801 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2802 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002803
2804 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002805 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002806}
2807
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002808void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002809 std::vector<native_handle_t*> *handles, bool closeFd) {
2810 if (handles == nullptr) {
2811 return;
2812 }
2813 if (closeFd) {
2814 for (auto& handle : *handles) {
2815 native_handle_close(handle);
2816 }
2817 }
2818 for (auto& handle : *handles) {
2819 native_handle_delete(handle);
2820 }
2821 handles->clear();
2822 return;
2823}
2824
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002825/**
2826 * HalInterface inner class methods
2827 */
2828
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002829void Camera3Device::HalInterface::getInflightBufferKeys(
2830 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002831 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002832 return;
2833}
2834
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002835void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2836 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002837 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002838 return;
2839}
2840
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002841bool Camera3Device::HalInterface::verifyBufferIds(
2842 int32_t streamId, std::vector<uint64_t>& bufIds) {
2843 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002844}
2845
2846status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002847 int32_t frameNumber, int32_t streamId,
2848 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002849 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002850}
2851
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002852status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002853 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002854 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002855}
2856
2857// Find and pop a buffer_handle_t based on bufferId
2858status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002859 uint64_t bufferId,
2860 /*out*/ buffer_handle_t** buffer,
2861 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002862 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002863}
2864
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002865std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2866 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002867 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002868}
2869
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002870uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2871 const native_handle_t* handle) {
2872 return mBufferRecords.removeOneBufferCache(streamId, handle);
2873}
2874
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002875void Camera3Device::HalInterface::onBufferFreed(
2876 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002877 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2878 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2879 if (bufferId != BUFFER_ID_NO_BUFFER) {
2880 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002881 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002882}
2883
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002884void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002885 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2886 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2887 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002888 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2889 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002890}
2891
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002892/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002893 * RequestThread inner class methods
2894 */
2895
2896Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002897 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002898 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002899 bool useHalBufManager,
2900 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002901 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002902 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002903 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002904 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002905 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002906 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002907 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002908 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909 mReconfigured(false),
2910 mDoPause(false),
2911 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002912 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002913 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002914 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002915 mCurrentAfTriggerId(0),
2916 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002917 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002918 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002919 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002920 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002921 mRepeatingLastFrameNumber(
2922 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002923 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002924 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002925 mRequestLatency(kRequestLatencyBinSize),
2926 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002927 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002928 mUseHalBufManager(useHalBufManager),
2929 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002930 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002931}
2932
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002933Camera3Device::RequestThread::~RequestThread() {}
2934
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002935void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002936 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002937 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002938 Mutex::Autolock l(mRequestLock);
2939 mListener = listener;
2940}
2941
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002942void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002943 const CameraMetadata& sessionParams,
2944 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002945 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002946 Mutex::Autolock l(mRequestLock);
2947 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002948 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002949 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002950 // Prepare video stream for high speed recording.
2951 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002952 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002953}
2954
Jianing Wei90e59c92014-03-12 18:29:36 -07002955status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002956 List<sp<CaptureRequest> > &requests,
2957 /*out*/
2958 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002959 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002960 Mutex::Autolock l(mRequestLock);
2961 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2962 ++it) {
2963 mRequestQueue.push_back(*it);
2964 }
2965
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002966 if (lastFrameNumber != NULL) {
2967 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2968 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2969 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2970 *lastFrameNumber);
2971 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002972
Jianing Wei90e59c92014-03-12 18:29:36 -07002973 unpauseForNewRequests();
2974
2975 return OK;
2976}
2977
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002978
2979status_t Camera3Device::RequestThread::queueTrigger(
2980 RequestTrigger trigger[],
2981 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002982 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002983 Mutex::Autolock l(mTriggerMutex);
2984 status_t ret;
2985
2986 for (size_t i = 0; i < count; ++i) {
2987 ret = queueTriggerLocked(trigger[i]);
2988
2989 if (ret != OK) {
2990 return ret;
2991 }
2992 }
2993
2994 return OK;
2995}
2996
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002997const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2998 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002999 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003000 if (d != nullptr) return d->mId;
3001 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003002}
3003
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003004status_t Camera3Device::RequestThread::queueTriggerLocked(
3005 RequestTrigger trigger) {
3006
3007 uint32_t tag = trigger.metadataTag;
3008 ssize_t index = mTriggerMap.indexOfKey(tag);
3009
3010 switch (trigger.getTagType()) {
3011 case TYPE_BYTE:
3012 // fall-through
3013 case TYPE_INT32:
3014 break;
3015 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003016 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3017 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003018 return INVALID_OPERATION;
3019 }
3020
3021 /**
3022 * Collect only the latest trigger, since we only have 1 field
3023 * in the request settings per trigger tag, and can't send more than 1
3024 * trigger per request.
3025 */
3026 if (index != NAME_NOT_FOUND) {
3027 mTriggerMap.editValueAt(index) = trigger;
3028 } else {
3029 mTriggerMap.add(tag, trigger);
3030 }
3031
3032 return OK;
3033}
3034
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003035status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003036 const RequestList &requests,
3037 /*out*/
3038 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003039 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003040 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003041 if (lastFrameNumber != NULL) {
3042 *lastFrameNumber = mRepeatingLastFrameNumber;
3043 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003044 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003045 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003046 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3047 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003048
3049 unpauseForNewRequests();
3050
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003051 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003052 return OK;
3053}
3054
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003055bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003056 if (mRepeatingRequests.empty()) {
3057 return false;
3058 }
3059 int32_t requestId = requestIn->mResultExtras.requestId;
3060 const RequestList &repeatRequests = mRepeatingRequests;
3061 // All repeating requests are guaranteed to have same id so only check first quest
3062 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3063 return (firstRequest->mResultExtras.requestId == requestId);
3064}
3065
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003066status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003067 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003068 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003069 return clearRepeatingRequestsLocked(lastFrameNumber);
3070
3071}
3072
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003073status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3074 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003075 std::vector<int32_t> streamIds;
3076 for (const auto& request : mRepeatingRequests) {
3077 for (const auto& stream : request->mOutputStreams) {
3078 streamIds.push_back(stream->getId());
3079 }
3080 }
3081
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003082 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003083 if (lastFrameNumber != NULL) {
3084 *lastFrameNumber = mRepeatingLastFrameNumber;
3085 }
Emilian Peev2295df72021-11-12 18:14:10 -08003086
3087 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3088
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003089 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003090 return OK;
3091}
3092
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003093status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003094 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003095 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003096 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003097 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003098
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003099 // Send errors for all requests pending in the request queue, including
3100 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003101 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003102 if (listener != NULL) {
3103 for (RequestList::iterator it = mRequestQueue.begin();
3104 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003105 // Abort the input buffers for reprocess requests.
3106 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003107 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003108 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003109 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003110 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003111 if (res != OK) {
3112 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3113 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3114 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003115 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003116 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3117 if (res != OK) {
3118 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3119 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3120 }
3121 }
3122 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003123 // Set the frame number this request would have had, if it
3124 // had been submitted; this frame number will not be reused.
3125 // The requestId and burstId fields were set when the request was
3126 // submitted originally (in convertMetadataListToRequestListLocked)
3127 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003128 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003129 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003130 }
3131 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003132 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003133
3134 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003135 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003136 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003137 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003138 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003139 return OK;
3140}
3141
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003142status_t Camera3Device::RequestThread::flush() {
3143 ATRACE_CALL();
3144 Mutex::Autolock l(mFlushLock);
3145
Emilian Peev08dd2452017-04-06 16:55:14 +01003146 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003147}
3148
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003149void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003150 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003151 Mutex::Autolock l(mPauseLock);
3152 mDoPause = paused;
3153 mDoPauseSignal.signal();
3154}
3155
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003156status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3157 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003158 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003159 Mutex::Autolock l(mLatestRequestMutex);
3160 status_t res;
3161 while (mLatestRequestId != requestId) {
3162 nsecs_t startTime = systemTime();
3163
3164 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3165 if (res != OK) return res;
3166
3167 timeout -= (systemTime() - startTime);
3168 }
3169
3170 return OK;
3171}
3172
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003173void Camera3Device::RequestThread::requestExit() {
3174 // Call parent to set up shutdown
3175 Thread::requestExit();
3176 // The exit from any possible waits
3177 mDoPauseSignal.signal();
3178 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003179
3180 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3181 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003182}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003183
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003184void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003185 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003186 bool surfaceAbandoned = false;
3187 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003188 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003189 {
3190 Mutex::Autolock l(mRequestLock);
3191 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3192 // repeating requests.
3193 for (const auto& request : mRepeatingRequests) {
3194 for (const auto& s : request->mOutputStreams) {
3195 if (s->isAbandoned()) {
3196 surfaceAbandoned = true;
3197 clearRepeatingRequestsLocked(&lastFrameNumber);
3198 break;
3199 }
3200 }
3201 if (surfaceAbandoned) {
3202 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003203 }
3204 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003205 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003206 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003207
3208 if (listener != NULL && surfaceAbandoned) {
3209 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003210 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003211}
3212
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003213bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003214 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003215 status_t res;
3216 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003217 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003218 uint32_t numRequestProcessed = 0;
3219 for (size_t i = 0; i < batchSize; i++) {
3220 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003221 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003222 }
3223
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003224 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3225
3226 bool triggerRemoveFailed = false;
3227 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3228 for (size_t i = 0; i < numRequestProcessed; i++) {
3229 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3230 nextRequest.submitted = true;
3231
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003232 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003233
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003234 if (!triggerRemoveFailed) {
3235 // Remove any previously queued triggers (after unlock)
3236 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3237 if (removeTriggerRes != OK) {
3238 triggerRemoveFailed = true;
3239 triggerFailedRequest = nextRequest;
3240 }
3241 }
3242 }
3243
3244 if (triggerRemoveFailed) {
3245 SET_ERR("RequestThread: Unable to remove triggers "
3246 "(capture request %d, HAL device: %s (%d)",
3247 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3248 cleanUpFailedRequests(/*sendRequestError*/ false);
3249 return false;
3250 }
3251
3252 if (res != OK) {
3253 // Should only get a failure here for malformed requests or device-level
3254 // errors, so consider all errors fatal. Bad metadata failures should
3255 // come through notify.
3256 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3257 mNextRequests[numRequestProcessed].halRequest.frame_number,
3258 strerror(-res), res);
3259 cleanUpFailedRequests(/*sendRequestError*/ false);
3260 return false;
3261 }
3262 return true;
3263}
3264
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003265Camera3Device::RequestThread::ExpectedDurationInfo
3266 Camera3Device::RequestThread::calculateExpectedDurationRange(
3267 const camera_metadata_t *request) {
3268 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003269 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003270 InFlightRequest::kDefaultMaxExpectedDuration,
3271 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003272 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3273 find_camera_metadata_ro_entry(request,
3274 ANDROID_CONTROL_AE_MODE,
3275 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003276 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003277
3278 switch (e.data.u8[0]) {
3279 case ANDROID_CONTROL_AE_MODE_OFF:
3280 find_camera_metadata_ro_entry(request,
3281 ANDROID_SENSOR_EXPOSURE_TIME,
3282 &e);
3283 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003284 expectedDurationInfo.minDuration = e.data.i64[0];
3285 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003286 }
3287 find_camera_metadata_ro_entry(request,
3288 ANDROID_SENSOR_FRAME_DURATION,
3289 &e);
3290 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003291 expectedDurationInfo.minDuration =
3292 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3293 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003294 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003295 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003296 break;
3297 default:
3298 find_camera_metadata_ro_entry(request,
3299 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3300 &e);
3301 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003302 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3303 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003304 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003305 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003306 break;
3307 }
3308
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003309 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003310}
3311
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003312bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3313 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3314 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3315 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3316 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3317 return true;
3318 }
3319
3320 return false;
3321}
3322
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003323void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3324 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003325 camera_capture_request_t& halRequest = nextRequest.halRequest;
3326 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003327 Mutex::Autolock al(mLatestRequestMutex);
3328
Shuzhen Wang83bff122020-11-20 15:51:39 -08003329 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003330 mLatestRequest.acquire(cloned);
3331
3332 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003333 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3334 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3335 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003336 CameraMetadata(cloned));
3337 }
3338
3339 sp<Camera3Device> parent = mParent.promote();
3340 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003341 int32_t inputStreamId = -1;
3342 if (halRequest.input_buffer != nullptr) {
3343 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3344 }
3345
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003346 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003347 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003348 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3349 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003350 }
3351 }
3352
Shuzhen Wang83bff122020-11-20 15:51:39 -08003353 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003354 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003355 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003356 }
3357
Shuzhen Wang83bff122020-11-20 15:51:39 -08003358 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003359}
3360
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003361bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3362 ATRACE_CALL();
3363 bool updatesDetected = false;
3364
Emilian Peev4ec17882019-01-24 17:16:58 -08003365 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003366 for (auto tag : mSessionParamKeys) {
3367 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003368 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003369
3370 if (entry.count > 0) {
3371 bool isDifferent = false;
3372 if (lastEntry.count > 0) {
3373 // Have a last value, compare to see if changed
3374 if (lastEntry.type == entry.type &&
3375 lastEntry.count == entry.count) {
3376 // Same type and count, compare values
3377 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3378 size_t entryBytes = bytesPerValue * lastEntry.count;
3379 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3380 if (cmp != 0) {
3381 isDifferent = true;
3382 }
3383 } else {
3384 // Count or type has changed
3385 isDifferent = true;
3386 }
3387 } else {
3388 // No last entry, so always consider to be different
3389 isDifferent = true;
3390 }
3391
3392 if (isDifferent) {
3393 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003394 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3395 updatesDetected = true;
3396 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003397 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003398 }
3399 } else if (lastEntry.count > 0) {
3400 // Value has been removed
3401 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003402 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003403 updatesDetected = true;
3404 }
3405 }
3406
Emilian Peev4ec17882019-01-24 17:16:58 -08003407 bool reconfigureRequired;
3408 if (updatesDetected) {
3409 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3410 updatedParams);
3411 mLatestSessionParams = updatedParams;
3412 } else {
3413 reconfigureRequired = false;
3414 }
3415
3416 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003417}
3418
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003419bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003420 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003421 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003422 // Any function called from threadLoop() must not hold mInterfaceLock since
3423 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3424 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003425
3426 // Handle paused state.
3427 if (waitIfPaused()) {
3428 return true;
3429 }
3430
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003431 // Wait for the next batch of requests.
3432 waitForNextRequestBatch();
3433 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003434 return true;
3435 }
3436
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003437 // Get the latest request ID, if any
3438 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003439 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003440 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003441 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003442 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003443 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003444 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3445 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003446 }
3447
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003448 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3449 // or a single request from streaming or burst. In either case the first element
3450 // should contain the latest camera settings that we need to check for any session
3451 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003452 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003453 res = OK;
3454
3455 //Input stream buffers are already acquired at this point so an input stream
3456 //will not be able to move to idle state unless we force it.
3457 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3458 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3459 if (res != OK) {
3460 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3461 cleanUpFailedRequests(/*sendRequestError*/ false);
3462 return false;
3463 }
3464 }
3465
3466 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003467 sp<Camera3Device> parent = mParent.promote();
3468 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003469 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3470 if (statusTracker != nullptr) {
3471 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3472 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003473 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003474 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003475 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003476
3477 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3478 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3479 if (res != OK) {
3480 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3481 cleanUpFailedRequests(/*sendRequestError*/ false);
3482 return false;
3483 }
3484 }
3485 }
3486 }
3487
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003488 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003489 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003490 if (res == TIMED_OUT) {
3491 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003492 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003493 // Check if any stream is abandoned.
3494 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003495 return true;
3496 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003497 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003498 return false;
3499 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003500
Zhijun Hecc27e112013-10-03 16:12:43 -07003501 // Inform waitUntilRequestProcessed thread of a new request ID
3502 {
3503 Mutex::Autolock al(mLatestRequestMutex);
3504
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003505 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003506 mLatestRequestSignal.signal();
3507 }
3508
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003509 // Submit a batch of requests to HAL.
3510 // Use flush lock only when submitting multilple requests in a batch.
3511 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3512 // which may take a long time to finish so synchronizing flush() and
3513 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3514 // For now, only synchronize for high speed recording and we should figure something out for
3515 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003516 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003517
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003518 if (useFlushLock) {
3519 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003520 }
3521
Zhijun Hef0645c12016-08-02 00:58:11 -07003522 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003523 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003524
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003525 sp<Camera3Device> parent = mParent.promote();
3526 if (parent != nullptr) {
3527 parent->mRequestBufferSM.onSubmittingRequest();
3528 }
3529
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003530 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003531 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003532 submitRequestSuccess = sendRequestsBatch();
3533
Shuzhen Wang686f6442017-06-20 16:16:04 -07003534 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3535 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003536
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003537 if (useFlushLock) {
3538 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003539 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003540
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003541 // Unset as current request
3542 {
3543 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003544 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003545 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003546 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003547
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003548 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003549}
3550
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003551status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3552 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3553 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3554 ANDROID_SCALER_CROP_REGION_SET};
3555 if (request == nullptr) {
3556 ALOGE("%s request metadata nullptr", __FUNCTION__);
3557 return BAD_VALUE;
3558 }
3559 status_t res = OK;
3560 for (const auto &key : kFwkOnlyRegionKeys) {
3561 if (request->exists(key)) {
3562 res = request->erase(key);
3563 if (res != OK) {
3564 return res;
3565 }
3566 }
3567 }
3568 return OK;
3569}
3570
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003571status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003572 ATRACE_CALL();
3573
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003574 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003575 for (size_t i = 0; i < mNextRequests.size(); i++) {
3576 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003577 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003578 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3579 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003580
3581 // Prepare a request to HAL
3582 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3583
3584 // Insert any queued triggers (before metadata is locked)
3585 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003586 if (res < 0) {
3587 SET_ERR("RequestThread: Unable to insert triggers "
3588 "(capture request %d, HAL device: %s (%d)",
3589 halRequest->frame_number, strerror(-res), res);
3590 return INVALID_OPERATION;
3591 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003592
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003593 int triggerCount = res;
3594 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3595 mPrevTriggers = triggerCount;
3596
Emilian Peeve23f1d92021-09-20 14:56:01 -07003597 // Do not override rotate&crop for stream configurations that include
3598 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3599 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3600 bool rotateAndCropChanged = mComposerOutput ? false :
3601 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003602 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003603
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003604 // If the request is the same as last, or we had triggers now or last time or
3605 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003606 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003607 (mPrevRequest != captureRequest || triggersMixedIn ||
3608 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003609 // Request settings are all the same within one batch, so only treat the first
3610 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003611 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003612 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003613 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003614 /**
3615 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003616 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003617 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003618 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003619 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003620 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003621 "(capture request %d, HAL device: %s (%d)",
3622 halRequest->frame_number, strerror(-res), res);
3623 return INVALID_OPERATION;
3624 }
3625
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003626 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003627 sp<Camera3Device> parent = mParent.promote();
3628 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003629 List<PhysicalCameraSettings>::iterator it;
3630 for (it = captureRequest->mSettingsList.begin();
3631 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003632 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3633 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003634 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3635 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3636 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3637 res);
3638 return INVALID_OPERATION;
3639 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003640 continue;
3641 }
3642
3643 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3644 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3645 updateCaptureRequest(&(it->metadata));
3646 if (res != OK) {
3647 SET_ERR("RequestThread: Unable to correct capture requests "
3648 "for scaler crop region and metering regions for request "
3649 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3650 res);
3651 return INVALID_OPERATION;
3652 }
3653 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003654 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3655 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3656 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3657 res);
3658 return INVALID_OPERATION;
3659 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003660 }
3661 }
3662
3663 // Correct metadata regions for distortion correction if enabled
3664 for (it = captureRequest->mSettingsList.begin();
3665 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003666 if (parent->mDistortionMappers.find(it->cameraId) ==
3667 parent->mDistortionMappers.end()) {
3668 continue;
3669 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003670
3671 if (!captureRequest->mDistortionCorrectionUpdated) {
3672 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3673 &(it->metadata));
3674 if (res != OK) {
3675 SET_ERR("RequestThread: Unable to correct capture requests "
3676 "for lens distortion for request %d: %s (%d)",
3677 halRequest->frame_number, strerror(-res), res);
3678 return INVALID_OPERATION;
3679 }
3680 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003681 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003682 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003683
3684 for (it = captureRequest->mSettingsList.begin();
3685 it != captureRequest->mSettingsList.end(); it++) {
3686 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3687 parent->mZoomRatioMappers.end()) {
3688 continue;
3689 }
3690
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003691 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003692 cameraIdsWithZoom.insert(it->cameraId);
3693 }
3694
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003695 if (!captureRequest->mZoomRatioUpdated) {
3696 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3697 &(it->metadata));
3698 if (res != OK) {
3699 SET_ERR("RequestThread: Unable to correct capture requests "
3700 "for zoom ratio for request %d: %s (%d)",
3701 halRequest->frame_number, strerror(-res), res);
3702 return INVALID_OPERATION;
3703 }
3704 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003705 }
3706 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003707 if (captureRequest->mRotateAndCropAuto &&
3708 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003709 for (it = captureRequest->mSettingsList.begin();
3710 it != captureRequest->mSettingsList.end(); it++) {
3711 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3712 if (mapper != parent->mRotateAndCropMappers.end()) {
3713 res = mapper->second.updateCaptureRequest(&(it->metadata));
3714 if (res != OK) {
3715 SET_ERR("RequestThread: Unable to correct capture requests "
3716 "for rotate-and-crop for request %d: %s (%d)",
3717 halRequest->frame_number, strerror(-res), res);
3718 return INVALID_OPERATION;
3719 }
3720 }
3721 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003722 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003723 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003724 }
3725 }
3726
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003727 /**
3728 * The request should be presorted so accesses in HAL
3729 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3730 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003731 captureRequest->mSettingsList.begin()->metadata.sort();
3732 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003733 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003734 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003735 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3736
3737 IF_ALOGV() {
3738 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3739 find_camera_metadata_ro_entry(
3740 halRequest->settings,
3741 ANDROID_CONTROL_AF_TRIGGER,
3742 &e
3743 );
3744 if (e.count > 0) {
3745 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3746 __FUNCTION__,
3747 halRequest->frame_number,
3748 e.data.u8[0]);
3749 }
3750 }
3751 } else {
3752 // leave request.settings NULL to indicate 'reuse latest given'
3753 ALOGVV("%s: Request settings are REUSED",
3754 __FUNCTION__);
3755 }
3756
Emilian Peevaebbe412018-01-15 13:53:24 +00003757 if (captureRequest->mSettingsList.size() > 1) {
3758 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3759 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003760 if (newRequest) {
3761 halRequest->physcam_settings =
3762 new const camera_metadata* [halRequest->num_physcam_settings];
3763 } else {
3764 halRequest->physcam_settings = nullptr;
3765 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003766 auto it = ++captureRequest->mSettingsList.begin();
3767 size_t i = 0;
3768 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3769 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003770 if (newRequest) {
3771 it->metadata.sort();
3772 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3773 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003774 }
3775 }
3776
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003777 uint32_t totalNumBuffers = 0;
3778
3779 // Fill in buffers
3780 if (captureRequest->mInputStream != NULL) {
3781 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003782
3783 halRequest->input_width = captureRequest->mInputBufferSize.width;
3784 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003785 totalNumBuffers += 1;
3786 } else {
3787 halRequest->input_buffer = NULL;
3788 }
3789
Emilian Peevf4816702020-04-03 15:44:51 -07003790 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003791 captureRequest->mOutputStreams.size());
3792 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003793 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003794
3795 sp<Camera3Device> parent = mParent.promote();
3796 if (parent == NULL) {
3797 // Should not happen, and nowhere to send errors to, so just log it
3798 CLOGE("RequestThread: Parent is gone");
3799 return INVALID_OPERATION;
3800 }
3801 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3802
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003803 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003804 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003805 sp<Camera3OutputStreamInterface> outputStream =
3806 captureRequest->mOutputStreams.editItemAt(j);
3807 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003808
3809 // Prepare video buffers for high speed recording on the first video request.
3810 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3811 // Only try to prepare video stream on the first video request.
3812 mPrepareVideoStream = false;
3813
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003814 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3815 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003816 while (res == NOT_ENOUGH_DATA) {
3817 res = outputStream->prepareNextBuffer();
3818 }
3819 if (res != OK) {
3820 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3821 __FUNCTION__, strerror(-res), res);
3822 outputStream->cancelPrepare();
3823 }
3824 }
3825
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003826 std::vector<size_t> uniqueSurfaceIds;
3827 res = outputStream->getUniqueSurfaceIds(
3828 captureRequest->mOutputSurfaces[streamId],
3829 &uniqueSurfaceIds);
3830 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3831 if (res != OK && res != INVALID_OPERATION) {
3832 ALOGE("%s: failed to query stream %d unique surface IDs",
3833 __FUNCTION__, streamId);
3834 return res;
3835 }
3836 if (res == OK) {
3837 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3838 }
3839
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003840 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003841 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003842 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003843 return TIMED_OUT;
3844 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003845 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003846 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003847 buffer.stream = outputStream->asHalStream();
3848 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003849 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003850 buffer.acquire_fence = -1;
3851 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003852 // Mark the output stream as unpreparable to block clients from calling
3853 // 'prepare' after this request reaches CameraHal and before the respective
3854 // buffers are requested.
3855 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003856 } else {
3857 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3858 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003859 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003860 if (res != OK) {
3861 // Can't get output buffer from gralloc queue - this could be due to
3862 // abandoned queue or other consumer misbehavior, so not a fatal
3863 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003864 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003865 " %s (%d)", strerror(-res), res);
3866
3867 return TIMED_OUT;
3868 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003869 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003870
3871 {
3872 sp<Camera3Device> parent = mParent.promote();
3873 if (parent != nullptr) {
3874 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3875 for (const auto& settings : captureRequest->mSettingsList) {
3876 if ((streamCameraId.isEmpty() &&
3877 parent->getId() == settings.cameraId.c_str()) ||
3878 streamCameraId == settings.cameraId.c_str()) {
3879 outputStream->fireBufferRequestForFrameNumber(
3880 captureRequest->mResultExtras.frameNumber,
3881 settings.metadata);
3882 }
3883 }
3884 }
3885 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003886
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003887 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003888 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3889 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3890 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3891 } else if (!physicalCameraId.isEmpty()) {
3892 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003893 }
3894 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003895 }
3896 totalNumBuffers += halRequest->num_output_buffers;
3897
3898 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003899 // If this request list is for constrained high speed recording (not
3900 // preview), and the current request is not the last one in the batch,
3901 // do not send callback to the app.
3902 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003903 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003904 hasCallback = false;
3905 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003906 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003907 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003908 const camera_metadata_t* settings = halRequest->settings;
3909 bool shouldUnlockSettings = false;
3910 if (settings == nullptr) {
3911 shouldUnlockSettings = true;
3912 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3913 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003914 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3915 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003916 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003917 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3918 isStillCapture = true;
3919 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3920 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003921
Emilian Peevaf8416e2021-02-04 17:52:43 -08003922 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003923 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003924 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3925 isZslCapture = true;
3926 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003927 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003928 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003929 res = parent->registerInFlight(halRequest->frame_number,
3930 totalNumBuffers, captureRequest->mResultExtras,
3931 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003932 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003933 expectedDurationInfo.minDuration,
3934 expectedDurationInfo.maxDuration,
3935 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003936 requestedPhysicalCameras, isStillCapture, isZslCapture,
3937 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003938 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003939 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003940 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3941 ", burstId = %" PRId32 ".",
3942 __FUNCTION__,
3943 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3944 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003945
3946 if (shouldUnlockSettings) {
3947 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3948 }
3949
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003950 if (res != OK) {
3951 SET_ERR("RequestThread: Unable to register new in-flight request:"
3952 " %s (%d)", strerror(-res), res);
3953 return INVALID_OPERATION;
3954 }
3955 }
3956
3957 return OK;
3958}
3959
Igor Murashkin1e479c02013-09-06 16:55:14 -07003960CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003961 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003962 Mutex::Autolock al(mLatestRequestMutex);
3963
3964 ALOGV("RequestThread::%s", __FUNCTION__);
3965
3966 return mLatestRequest;
3967}
3968
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003969bool Camera3Device::RequestThread::isStreamPending(
3970 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003971 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003972 Mutex::Autolock l(mRequestLock);
3973
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003974 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003975 if (!nextRequest.submitted) {
3976 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3977 if (stream == s) return true;
3978 }
3979 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003980 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003981 }
3982
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003983 for (const auto& request : mRequestQueue) {
3984 for (const auto& s : request->mOutputStreams) {
3985 if (stream == s) return true;
3986 }
3987 if (stream == request->mInputStream) return true;
3988 }
3989
3990 for (const auto& request : mRepeatingRequests) {
3991 for (const auto& s : request->mOutputStreams) {
3992 if (stream == s) return true;
3993 }
3994 if (stream == request->mInputStream) return true;
3995 }
3996
3997 return false;
3998}
Jianing Weicb0652e2014-03-12 18:29:36 -07003999
Emilian Peev40ead602017-09-26 15:46:36 +01004000bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4001 ATRACE_CALL();
4002 Mutex::Autolock l(mRequestLock);
4003
4004 for (const auto& nextRequest : mNextRequests) {
4005 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4006 if (s.first == streamId) {
4007 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4008 if (it != s.second.end()) {
4009 return true;
4010 }
4011 }
4012 }
4013 }
4014
4015 for (const auto& request : mRequestQueue) {
4016 for (const auto& s : request->mOutputSurfaces) {
4017 if (s.first == streamId) {
4018 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4019 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004020 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004021 }
4022 }
4023 }
4024 }
4025
4026 for (const auto& request : mRepeatingRequests) {
4027 for (const auto& s : request->mOutputSurfaces) {
4028 if (s.first == streamId) {
4029 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4030 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004031 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004032 }
4033 }
4034 }
4035 }
4036
4037 return false;
4038}
4039
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004040void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4041 if (!mUseHalBufManager) {
4042 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4043 return;
4044 }
4045
4046 Mutex::Autolock pl(mPauseLock);
4047 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004048 mInterface->signalPipelineDrain(streamIds);
4049 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004050 }
4051 // If request thread is still busy, wait until paused then notify HAL
4052 mNotifyPipelineDrain = true;
4053 mStreamIdsToBeDrained = streamIds;
4054}
4055
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004056void Camera3Device::RequestThread::resetPipelineDrain() {
4057 Mutex::Autolock pl(mPauseLock);
4058 mNotifyPipelineDrain = false;
4059 mStreamIdsToBeDrained.clear();
4060}
4061
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004062void Camera3Device::RequestThread::clearPreviousRequest() {
4063 Mutex::Autolock l(mRequestLock);
4064 mPrevRequest.clear();
4065}
4066
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004067status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4068 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4069 ATRACE_CALL();
4070 Mutex::Autolock l(mTriggerMutex);
4071 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4072 return BAD_VALUE;
4073 }
4074 mRotateAndCropOverride = rotateAndCropValue;
4075 return OK;
4076}
4077
Emilian Peeve23f1d92021-09-20 14:56:01 -07004078status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4079 ATRACE_CALL();
4080 Mutex::Autolock l(mTriggerMutex);
4081 mComposerOutput = composerSurfacePresent;
4082 return OK;
4083}
4084
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004085status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004086 ATRACE_CALL();
4087 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004088 if (muteMode != mCameraMute) {
4089 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004090 mCameraMuteChanged = true;
4091 }
4092 return OK;
4093}
4094
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004095nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004096 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004097 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004098 return mExpectedInflightDuration > kMinInflightDuration ?
4099 mExpectedInflightDuration : kMinInflightDuration;
4100}
4101
Emilian Peevaebbe412018-01-15 13:53:24 +00004102void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004103 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004104 if ((request == nullptr) || (halRequest == nullptr)) {
4105 ALOGE("%s: Invalid request!", __FUNCTION__);
4106 return;
4107 }
4108
4109 if (halRequest->num_physcam_settings > 0) {
4110 if (halRequest->physcam_id != nullptr) {
4111 delete [] halRequest->physcam_id;
4112 halRequest->physcam_id = nullptr;
4113 }
4114 if (halRequest->physcam_settings != nullptr) {
4115 auto it = ++(request->mSettingsList.begin());
4116 size_t i = 0;
4117 for (; it != request->mSettingsList.end(); it++, i++) {
4118 it->metadata.unlock(halRequest->physcam_settings[i]);
4119 }
4120 delete [] halRequest->physcam_settings;
4121 halRequest->physcam_settings = nullptr;
4122 }
4123 }
4124}
4125
Ravneetaeb20dc2022-03-30 05:33:03 +00004126status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4127 Mutex::Autolock il(mInterfaceLock);
4128 Mutex::Autolock l(mLock);
4129
4130 if (mCameraServiceWatchdog != NULL) {
4131 mCameraServiceWatchdog->setEnabled(enabled);
4132 }
4133
4134 return OK;
4135}
4136
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004137void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4138 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004139 return;
4140 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004141
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004142 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004143 // Skip the ones that have been submitted successfully.
4144 if (nextRequest.submitted) {
4145 continue;
4146 }
4147
4148 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004149 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4150 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004151
4152 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004153 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004154 }
4155
Emilian Peevaebbe412018-01-15 13:53:24 +00004156 cleanupPhysicalSettings(captureRequest, halRequest);
4157
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004158 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004159 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004160 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4161 }
4162
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004163 // No output buffer can be returned when using HAL buffer manager
4164 if (!mUseHalBufManager) {
4165 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4166 //Buffers that failed processing could still have
4167 //valid acquire fence.
4168 int acquireFence = (*outputBuffers)[i].acquire_fence;
4169 if (0 <= acquireFence) {
4170 close(acquireFence);
4171 outputBuffers->editItemAt(i).acquire_fence = -1;
4172 }
Emilian Peevf4816702020-04-03 15:44:51 -07004173 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004174 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4175 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004176 /*timestampIncreasing*/true, std::vector<size_t> (),
4177 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004178 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004179 }
4180
4181 if (sendRequestError) {
4182 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004183 sp<NotificationListener> listener = mListener.promote();
4184 if (listener != NULL) {
4185 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004186 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004187 captureRequest->mResultExtras);
4188 }
4189 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004190
4191 // Remove yet-to-be submitted inflight request from inflightMap
4192 {
4193 sp<Camera3Device> parent = mParent.promote();
4194 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004195 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004196 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4197 if (idx >= 0) {
4198 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4199 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4200 parent->removeInFlightMapEntryLocked(idx);
4201 }
4202 }
4203 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004204 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004205
4206 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004207 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004208}
4209
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004210void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004211 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004212 // Optimized a bit for the simple steady-state case (single repeating
4213 // request), to avoid putting that request in the queue temporarily.
4214 Mutex::Autolock l(mRequestLock);
4215
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004216 assert(mNextRequests.empty());
4217
4218 NextRequest nextRequest;
4219 nextRequest.captureRequest = waitForNextRequestLocked();
4220 if (nextRequest.captureRequest == nullptr) {
4221 return;
4222 }
4223
Emilian Peevf4816702020-04-03 15:44:51 -07004224 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004225 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004226 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004227
4228 // Wait for additional requests
4229 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4230
4231 for (size_t i = 1; i < batchSize; i++) {
4232 NextRequest additionalRequest;
4233 additionalRequest.captureRequest = waitForNextRequestLocked();
4234 if (additionalRequest.captureRequest == nullptr) {
4235 break;
4236 }
4237
Emilian Peevf4816702020-04-03 15:44:51 -07004238 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004239 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004240 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004241 }
4242
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004243 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004244 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004245 mNextRequests.size(), batchSize);
4246 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004247 }
4248
4249 return;
4250}
4251
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004252void Camera3Device::RequestThread::setRequestClearing() {
4253 Mutex::Autolock l(mRequestLock);
4254 mRequestClearing = true;
4255}
4256
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004257sp<Camera3Device::CaptureRequest>
4258 Camera3Device::RequestThread::waitForNextRequestLocked() {
4259 status_t res;
4260 sp<CaptureRequest> nextRequest;
4261
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004262 while (mRequestQueue.empty()) {
4263 if (!mRepeatingRequests.empty()) {
4264 // Always atomically enqueue all requests in a repeating request
4265 // list. Guarantees a complete in-sequence set of captures to
4266 // application.
4267 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004268 if (mFirstRepeating) {
4269 mFirstRepeating = false;
4270 } else {
4271 for (auto& request : requests) {
4272 // For repeating requests, override timestamp request using
4273 // the time a request is inserted into the request queue,
4274 // because the original repeating request will have an old
4275 // fixed timestamp.
4276 request->mRequestTimeNs = systemTime();
4277 }
4278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004279 RequestList::const_iterator firstRequest =
4280 requests.begin();
4281 nextRequest = *firstRequest;
4282 mRequestQueue.insert(mRequestQueue.end(),
4283 ++firstRequest,
4284 requests.end());
4285 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004286
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004287 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004289 break;
4290 }
4291
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004292 if (!mRequestClearing) {
4293 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4294 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004295
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004296 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4297 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004298 Mutex::Autolock pl(mPauseLock);
4299 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004300 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004301 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004302 if (mNotifyPipelineDrain) {
4303 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4304 mNotifyPipelineDrain = false;
4305 mStreamIdsToBeDrained.clear();
4306 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004307 // Let the tracker know
4308 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4309 if (statusTracker != 0) {
4310 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4311 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004312 sp<Camera3Device> parent = mParent.promote();
4313 if (parent != nullptr) {
4314 parent->mRequestBufferSM.onRequestThreadPaused();
4315 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004316 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004317 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004318 // Stop waiting for now and let thread management happen
4319 return NULL;
4320 }
4321 }
4322
4323 if (nextRequest == NULL) {
4324 // Don't have a repeating request already in hand, so queue
4325 // must have an entry now.
4326 RequestList::iterator firstRequest =
4327 mRequestQueue.begin();
4328 nextRequest = *firstRequest;
4329 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004330 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4331 sp<NotificationListener> listener = mListener.promote();
4332 if (listener != NULL) {
4333 listener->notifyRequestQueueEmpty();
4334 }
4335 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004336 }
4337
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004338 // In case we've been unpaused by setPaused clearing mDoPause, need to
4339 // update internal pause state (capture/setRepeatingRequest unpause
4340 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004341 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004342 if (mPaused) {
4343 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4344 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4345 if (statusTracker != 0) {
4346 statusTracker->markComponentActive(mStatusId);
4347 }
4348 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004349 mPaused = false;
4350
4351 // Check if we've reconfigured since last time, and reset the preview
4352 // request if so. Can't use 'NULL request == repeat' across configure calls.
4353 if (mReconfigured) {
4354 mPrevRequest.clear();
4355 mReconfigured = false;
4356 }
4357
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004358 if (nextRequest != NULL) {
4359 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004360 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4361 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004362
4363 // Since RequestThread::clear() removes buffers from the input stream,
4364 // get the right buffer here before unlocking mRequestLock
4365 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004366 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4367 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004368 if (res != OK) {
4369 // Can't get input buffer from gralloc queue - this could be due to
4370 // disconnected queue or other producer misbehavior, so not a fatal
4371 // error
4372 ALOGE("%s: Can't get input buffer, skipping request:"
4373 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004374
4375 sp<NotificationListener> listener = mListener.promote();
4376 if (listener != NULL) {
4377 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004378 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004379 nextRequest->mResultExtras);
4380 }
4381 return NULL;
4382 }
4383 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004384 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004385
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004386 return nextRequest;
4387}
4388
4389bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004390 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004391 status_t res;
4392 Mutex::Autolock l(mPauseLock);
4393 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004394 if (mPaused == false) {
4395 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004396 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004397 if (mNotifyPipelineDrain) {
4398 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4399 mNotifyPipelineDrain = false;
4400 mStreamIdsToBeDrained.clear();
4401 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004402 // Let the tracker know
4403 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4404 if (statusTracker != 0) {
4405 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4406 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004407 sp<Camera3Device> parent = mParent.promote();
4408 if (parent != nullptr) {
4409 parent->mRequestBufferSM.onRequestThreadPaused();
4410 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004411 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004412
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004413 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004414 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004415 return true;
4416 }
4417 }
4418 // We don't set mPaused to false here, because waitForNextRequest needs
4419 // to further manage the paused state in case of starvation.
4420 return false;
4421}
4422
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004423void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004424 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004425 // With work to do, mark thread as unpaused.
4426 // If paused by request (setPaused), don't resume, to avoid
4427 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004428 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004429 Mutex::Autolock p(mPauseLock);
4430 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004431 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4432 if (mPaused) {
4433 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4434 if (statusTracker != 0) {
4435 statusTracker->markComponentActive(mStatusId);
4436 }
4437 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004438 mPaused = false;
4439 }
4440}
4441
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004442void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4443 sp<Camera3Device> parent = mParent.promote();
4444 if (parent != NULL) {
4445 va_list args;
4446 va_start(args, fmt);
4447
4448 parent->setErrorStateV(fmt, args);
4449
4450 va_end(args);
4451 }
4452}
4453
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004454status_t Camera3Device::RequestThread::insertTriggers(
4455 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004456 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004457 Mutex::Autolock al(mTriggerMutex);
4458
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004459 sp<Camera3Device> parent = mParent.promote();
4460 if (parent == NULL) {
4461 CLOGE("RequestThread: Parent is gone");
4462 return DEAD_OBJECT;
4463 }
4464
Emilian Peevaebbe412018-01-15 13:53:24 +00004465 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004466 size_t count = mTriggerMap.size();
4467
4468 for (size_t i = 0; i < count; ++i) {
4469 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004470 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004471
4472 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4473 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4474 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004475 if (isAeTrigger) {
4476 request->mResultExtras.precaptureTriggerId = triggerId;
4477 mCurrentPreCaptureTriggerId = triggerId;
4478 } else {
4479 request->mResultExtras.afTriggerId = triggerId;
4480 mCurrentAfTriggerId = triggerId;
4481 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004482 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004483 }
4484
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004485 camera_metadata_entry entry = metadata.find(tag);
4486
4487 if (entry.count > 0) {
4488 /**
4489 * Already has an entry for this trigger in the request.
4490 * Rewrite it with our requested trigger value.
4491 */
4492 RequestTrigger oldTrigger = trigger;
4493
4494 oldTrigger.entryValue = entry.data.u8[0];
4495
4496 mTriggerReplacedMap.add(tag, oldTrigger);
4497 } else {
4498 /**
4499 * More typical, no trigger entry, so we just add it
4500 */
4501 mTriggerRemovedMap.add(tag, trigger);
4502 }
4503
4504 status_t res;
4505
4506 switch (trigger.getTagType()) {
4507 case TYPE_BYTE: {
4508 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4509 res = metadata.update(tag,
4510 &entryValue,
4511 /*count*/1);
4512 break;
4513 }
4514 case TYPE_INT32:
4515 res = metadata.update(tag,
4516 &trigger.entryValue,
4517 /*count*/1);
4518 break;
4519 default:
4520 ALOGE("%s: Type not supported: 0x%x",
4521 __FUNCTION__,
4522 trigger.getTagType());
4523 return INVALID_OPERATION;
4524 }
4525
4526 if (res != OK) {
4527 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4528 ", value %d", __FUNCTION__, trigger.getTagName(),
4529 trigger.entryValue);
4530 return res;
4531 }
4532
4533 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4534 trigger.getTagName(),
4535 trigger.entryValue);
4536 }
4537
4538 mTriggerMap.clear();
4539
4540 return count;
4541}
4542
4543status_t Camera3Device::RequestThread::removeTriggers(
4544 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004545 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004546 Mutex::Autolock al(mTriggerMutex);
4547
Emilian Peevaebbe412018-01-15 13:53:24 +00004548 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004549
4550 /**
4551 * Replace all old entries with their old values.
4552 */
4553 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4554 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4555
4556 status_t res;
4557
4558 uint32_t tag = trigger.metadataTag;
4559 switch (trigger.getTagType()) {
4560 case TYPE_BYTE: {
4561 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4562 res = metadata.update(tag,
4563 &entryValue,
4564 /*count*/1);
4565 break;
4566 }
4567 case TYPE_INT32:
4568 res = metadata.update(tag,
4569 &trigger.entryValue,
4570 /*count*/1);
4571 break;
4572 default:
4573 ALOGE("%s: Type not supported: 0x%x",
4574 __FUNCTION__,
4575 trigger.getTagType());
4576 return INVALID_OPERATION;
4577 }
4578
4579 if (res != OK) {
4580 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4581 ", trigger value %d", __FUNCTION__,
4582 trigger.getTagName(), trigger.entryValue);
4583 return res;
4584 }
4585 }
4586 mTriggerReplacedMap.clear();
4587
4588 /**
4589 * Remove all new entries.
4590 */
4591 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4592 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4593 status_t res = metadata.erase(trigger.metadataTag);
4594
4595 if (res != OK) {
4596 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4597 ", trigger value %d", __FUNCTION__,
4598 trigger.getTagName(), trigger.entryValue);
4599 return res;
4600 }
4601 }
4602 mTriggerRemovedMap.clear();
4603
4604 return OK;
4605}
4606
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004607status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004608 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004609 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004610 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004611 status_t res;
4612
Emilian Peevaebbe412018-01-15 13:53:24 +00004613 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004614
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004615 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004616 // exists
4617 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4618 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4619 if (afTrigger.count > 0 &&
4620 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4621 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004622 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004623 if (res != OK) return res;
4624 }
4625
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004626 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004627 // if none already exists
4628 camera_metadata_entry pcTrigger =
4629 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4630 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4631 if (pcTrigger.count > 0 &&
4632 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4633 pcId.count == 0) {
4634 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004635 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004636 if (res != OK) return res;
4637 }
4638
4639 return OK;
4640}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004641
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004642bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4643 const sp<CaptureRequest> &request) {
4644 ATRACE_CALL();
4645
4646 if (request->mRotateAndCropAuto) {
4647 Mutex::Autolock l(mTriggerMutex);
4648 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4649
4650 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4651 if (rotateAndCropEntry.count > 0) {
4652 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4653 return false;
4654 } else {
4655 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4656 return true;
4657 }
4658 } else {
4659 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4660 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4661 &rotateAndCrop_u8, 1);
4662 return true;
4663 }
4664 }
4665 return false;
4666}
4667
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004668bool Camera3Device::RequestThread::overrideTestPattern(
4669 const sp<CaptureRequest> &request) {
4670 ATRACE_CALL();
4671
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004672 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004673
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004674 Mutex::Autolock l(mTriggerMutex);
4675
4676 bool changed = false;
4677
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004678 // For a multi-camera, the physical cameras support the same set of
4679 // test pattern modes as the logical camera.
4680 for (auto& settings : request->mSettingsList) {
4681 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004682
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004683 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4684 int32_t testPatternData[4] = {
4685 settings.mOriginalTestPatternData[0],
4686 settings.mOriginalTestPatternData[1],
4687 settings.mOriginalTestPatternData[2],
4688 settings.mOriginalTestPatternData[3]
4689 };
4690 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4691 testPatternMode = mCameraMute;
4692 testPatternData[0] = 0;
4693 testPatternData[1] = 0;
4694 testPatternData[2] = 0;
4695 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004696 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004697
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004698 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4699 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4700 if (testPatternEntry.count > 0) {
4701 if (testPatternEntry.data.i32[0] != testPatternMode) {
4702 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004703 changed = true;
4704 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004705 } else if (supportTestPatternModeKey) {
4706 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4707 &testPatternMode, 1);
4708 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004709 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004710
4711 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4712 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4713 if (testPatternColor.count >= 4) {
4714 for (size_t i = 0; i < 4; i++) {
4715 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4716 testPatternColor.data.i32[i] = testPatternData[i];
4717 changed = true;
4718 }
4719 }
4720 } else if (supportTestPatternDataKey) {
4721 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4722 testPatternData, 4);
4723 changed = true;
4724 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004725 }
4726
4727 return changed;
4728}
4729
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004730status_t Camera3Device::RequestThread::setHalInterface(
4731 sp<HalInterface> newHalInterface) {
4732 if (newHalInterface.get() == nullptr) {
4733 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4734 return DEAD_OBJECT;
4735 }
4736
4737 mInterface = newHalInterface;
4738
4739 return OK;
4740}
4741
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004742/**
4743 * PreparerThread inner class methods
4744 */
4745
4746Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004747 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004748 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004749}
4750
4751Camera3Device::PreparerThread::~PreparerThread() {
4752 Thread::requestExitAndWait();
4753 if (mCurrentStream != nullptr) {
4754 mCurrentStream->cancelPrepare();
4755 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4756 mCurrentStream.clear();
4757 }
4758 clear();
4759}
4760
Ruben Brunkc78ac262015-08-13 17:58:46 -07004761status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004762 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004763 status_t res;
4764
4765 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004766 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004767
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004768 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004769 if (res == OK) {
4770 // No preparation needed, fire listener right off
4771 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004772 if (listener != NULL) {
4773 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004774 }
4775 return OK;
4776 } else if (res != NOT_ENOUGH_DATA) {
4777 return res;
4778 }
4779
4780 // Need to prepare, start up thread if necessary
4781 if (!mActive) {
4782 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4783 // isn't running
4784 Thread::requestExitAndWait();
4785 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4786 if (res != OK) {
4787 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004788 if (listener != NULL) {
4789 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004790 }
4791 return res;
4792 }
4793 mCancelNow = false;
4794 mActive = true;
4795 ALOGV("%s: Preparer stream started", __FUNCTION__);
4796 }
4797
4798 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004799 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004800 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4801
4802 return OK;
4803}
4804
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004805void Camera3Device::PreparerThread::pause() {
4806 ATRACE_CALL();
4807
4808 Mutex::Autolock l(mLock);
4809
4810 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4811 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4812 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4813 int currentMaxCount = mCurrentMaxCount;
4814 mPendingStreams.clear();
4815 mCancelNow = true;
4816 while (mActive) {
4817 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4818 if (res == TIMED_OUT) {
4819 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4820 return;
4821 } else if (res != OK) {
4822 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4823 return;
4824 }
4825 }
4826
4827 //Check whether the prepare thread was able to complete the current
4828 //stream. In case work is still pending emplace it along with the rest
4829 //of the streams in the pending list.
4830 if (currentStream != nullptr) {
4831 if (!mCurrentPrepareComplete) {
4832 pendingStreams.emplace(currentMaxCount, currentStream);
4833 }
4834 }
4835
4836 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4837 for (const auto& it : mPendingStreams) {
4838 it.second->cancelPrepare();
4839 }
4840}
4841
4842status_t Camera3Device::PreparerThread::resume() {
4843 ATRACE_CALL();
4844 status_t res;
4845
4846 Mutex::Autolock l(mLock);
4847 sp<NotificationListener> listener = mListener.promote();
4848
4849 if (mActive) {
4850 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4851 return NO_INIT;
4852 }
4853
4854 auto it = mPendingStreams.begin();
4855 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004856 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004857 if (res == OK) {
4858 if (listener != NULL) {
4859 listener->notifyPrepared(it->second->getId());
4860 }
4861 it = mPendingStreams.erase(it);
4862 } else if (res != NOT_ENOUGH_DATA) {
4863 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4864 res, strerror(-res));
4865 it = mPendingStreams.erase(it);
4866 } else {
4867 it++;
4868 }
4869 }
4870
4871 if (mPendingStreams.empty()) {
4872 return OK;
4873 }
4874
4875 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4876 if (res != OK) {
4877 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4878 __FUNCTION__, res, strerror(-res));
4879 return res;
4880 }
4881 mCancelNow = false;
4882 mActive = true;
4883 ALOGV("%s: Preparer stream started", __FUNCTION__);
4884
4885 return OK;
4886}
4887
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004888status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004889 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004890 Mutex::Autolock l(mLock);
4891
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004892 for (const auto& it : mPendingStreams) {
4893 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004894 }
4895 mPendingStreams.clear();
4896 mCancelNow = true;
4897
4898 return OK;
4899}
4900
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004901void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004902 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004903 Mutex::Autolock l(mLock);
4904 mListener = listener;
4905}
4906
4907bool Camera3Device::PreparerThread::threadLoop() {
4908 status_t res;
4909 {
4910 Mutex::Autolock l(mLock);
4911 if (mCurrentStream == nullptr) {
4912 // End thread if done with work
4913 if (mPendingStreams.empty()) {
4914 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4915 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4916 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4917 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004918 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004919 return false;
4920 }
4921
4922 // Get next stream to prepare
4923 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004924 mCurrentStream = it->second;
4925 mCurrentMaxCount = it->first;
4926 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004927 mPendingStreams.erase(it);
4928 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4929 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4930 } else if (mCancelNow) {
4931 mCurrentStream->cancelPrepare();
4932 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4933 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4934 mCurrentStream.clear();
4935 mCancelNow = false;
4936 return true;
4937 }
4938 }
4939
4940 res = mCurrentStream->prepareNextBuffer();
4941 if (res == NOT_ENOUGH_DATA) return true;
4942 if (res != OK) {
4943 // Something bad happened; try to recover by cancelling prepare and
4944 // signalling listener anyway
4945 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4946 mCurrentStream->getId(), res, strerror(-res));
4947 mCurrentStream->cancelPrepare();
4948 }
4949
4950 // This stream has finished, notify listener
4951 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004952 sp<NotificationListener> listener = mListener.promote();
4953 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004954 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4955 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004956 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004957 }
4958
4959 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4960 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004961 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004962
4963 return true;
4964}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004965
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004966status_t Camera3Device::RequestBufferStateMachine::initialize(
4967 sp<camera3::StatusTracker> statusTracker) {
4968 if (statusTracker == nullptr) {
4969 ALOGE("%s: statusTracker is null", __FUNCTION__);
4970 return BAD_VALUE;
4971 }
4972
4973 std::lock_guard<std::mutex> lock(mLock);
4974 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004975 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004976 return OK;
4977}
4978
4979bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4980 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004981 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004982 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004983 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004984 return true;
4985 }
4986 return false;
4987}
4988
4989void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4990 std::lock_guard<std::mutex> lock(mLock);
4991 if (!mRequestBufferOngoing) {
4992 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4993 return;
4994 }
4995 mRequestBufferOngoing = false;
4996 if (mStatus == RB_STATUS_PENDING_STOP) {
4997 checkSwitchToStopLocked();
4998 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004999 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005000}
5001
5002void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5003 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005004 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005005 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005006 return;
5007}
5008
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005009void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005010 std::lock_guard<std::mutex> lock(mLock);
5011 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005012 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5013 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005014 mInflightMapEmpty = false;
5015 if (mStatus == RB_STATUS_STOPPED) {
5016 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005017 }
5018 return;
5019}
5020
5021void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5022 std::lock_guard<std::mutex> lock(mLock);
5023 mRequestThreadPaused = true;
5024 if (mStatus == RB_STATUS_PENDING_STOP) {
5025 checkSwitchToStopLocked();
5026 }
5027 return;
5028}
5029
5030void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5031 std::lock_guard<std::mutex> lock(mLock);
5032 mInflightMapEmpty = true;
5033 if (mStatus == RB_STATUS_PENDING_STOP) {
5034 checkSwitchToStopLocked();
5035 }
5036 return;
5037}
5038
5039void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5040 std::lock_guard<std::mutex> lock(mLock);
5041 if (!checkSwitchToStopLocked()) {
5042 mStatus = RB_STATUS_PENDING_STOP;
5043 }
5044 return;
5045}
5046
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005047bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5048 std::lock_guard<std::mutex> lock(mLock);
5049 if (mRequestBufferOngoing) {
5050 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5051 __FUNCTION__);
5052 return false;
5053 }
5054 mSwitchedToOffline = true;
5055 mInflightMapEmpty = true;
5056 mRequestThreadPaused = true;
5057 mStatus = RB_STATUS_STOPPED;
5058 return true;
5059}
5060
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005061void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5062 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5063 if (statusTracker != nullptr) {
5064 if (active) {
5065 statusTracker->markComponentActive(mRequestBufferStatusId);
5066 } else {
5067 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5068 }
5069 }
5070}
5071
5072bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5073 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5074 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005075 return true;
5076 }
5077 return false;
5078}
5079
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005080bool Camera3Device::startRequestBuffer() {
5081 return mRequestBufferSM.startRequestBuffer();
5082}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005083
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005084void Camera3Device::endRequestBuffer() {
5085 mRequestBufferSM.endRequestBuffer();
5086}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005087
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005088nsecs_t Camera3Device::getWaitDuration() {
5089 return kBaseGetBufferWait + getExpectedInFlightDuration();
5090}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005091
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005092void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5093 mInterface->getInflightBufferKeys(out);
5094}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005095
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005096void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5097 mInterface->getInflightRequestBufferKeys(out);
5098}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005099
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005100std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5101 std::vector<sp<Camera3StreamInterface>> ret;
5102 bool hasInputStream = mInputStream != nullptr;
5103 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5104 if (hasInputStream) {
5105 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005106 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005107 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5108 ret.push_back(mOutputStreams[i]);
5109 }
5110 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5111 ret.push_back(mDeletedStreams[i]);
5112 }
5113 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005114}
5115
Emilian Peevcc0b7952020-01-07 13:54:47 -08005116void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5117 ATRACE_CALL();
5118
5119 if (offlineStreamIds == nullptr) {
5120 return;
5121 }
5122
5123 Mutex::Autolock il(mInterfaceLock);
5124
5125 auto streamIds = mOutputStreams.getStreamIds();
5126 bool hasInputStream = mInputStream != nullptr;
5127 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5128 offlineStreamIds->push_back(mInputStream->getId());
5129 }
5130
5131 for (const auto & streamId : streamIds) {
5132 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5133 // Streams that use the camera buffer manager are currently not supported in
5134 // offline mode
5135 if (stream->getOfflineProcessingSupport() &&
5136 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5137 offlineStreamIds->push_back(streamId);
5138 }
5139 }
5140}
5141
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005142status_t Camera3Device::setRotateAndCropAutoBehavior(
5143 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5144 ATRACE_CALL();
5145 Mutex::Autolock il(mInterfaceLock);
5146 Mutex::Autolock l(mLock);
5147 if (mRequestThread == nullptr) {
5148 return INVALID_OPERATION;
5149 }
5150 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5151}
5152
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005153bool Camera3Device::supportsCameraMute() {
5154 Mutex::Autolock il(mInterfaceLock);
5155 Mutex::Autolock l(mLock);
5156
5157 return mSupportCameraMute;
5158}
5159
5160status_t Camera3Device::setCameraMute(bool enabled) {
5161 ATRACE_CALL();
5162 Mutex::Autolock il(mInterfaceLock);
5163 Mutex::Autolock l(mLock);
5164
5165 if (mRequestThread == nullptr || !mSupportCameraMute) {
5166 return INVALID_OPERATION;
5167 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005168 int32_t muteMode =
5169 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5170 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5171 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5172 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005173}
5174
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005175status_t Camera3Device::injectCamera(const String8& injectedCamId,
5176 sp<CameraProviderManager> manager) {
5177 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5178 ATRACE_CALL();
5179 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005180 // When the camera device is active, injectCamera() and stopInjection() will call
5181 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5182 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5183 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5184 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5185 // waitUntilStateThenRelock().
5186 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005187
5188 status_t res = NO_ERROR;
5189 if (mInjectionMethods->isInjecting()) {
5190 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5191 return OK;
5192 } else {
5193 res = mInjectionMethods->stopInjection();
5194 if (res != OK) {
5195 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5196 __FUNCTION__, res);
5197 return res;
5198 }
5199 }
5200 }
5201
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005202 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005203 if (res != OK) {
5204 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5205 __FUNCTION__, res);
5206 return res;
5207 }
5208
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005209 // When the second display of android is cast to the remote device, and the opened camera is
5210 // also cast to the second display, in this case, because the camera has configured the streams
5211 // at this time, we can directly call injectCamera() to replace the internal camera with
5212 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005213 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5214 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5215
5216 camera3::camera_stream_configuration injectionConfig;
5217 std::vector<uint32_t> injectionBufferSizes;
5218 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5219 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5220 || injectionBufferSizes.size() <= 0) {
5221 ALOGE("Failed to inject camera due to abandoned configuration! "
5222 "mOperatingMode: %d injectionConfig.num_streams: %d "
5223 "injectionBufferSizes.size(): %zu", mOperatingMode,
5224 injectionConfig.num_streams, injectionBufferSizes.size());
5225 return DEAD_OBJECT;
5226 }
5227
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005228 res = mInjectionMethods->injectCamera(
5229 injectionConfig, injectionBufferSizes);
5230 if (res != OK) {
5231 ALOGE("Can't finish inject camera process!");
5232 return res;
5233 }
5234 }
5235
5236 return OK;
5237}
5238
5239status_t Camera3Device::stopInjection() {
5240 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5241 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005242 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005243 return mInjectionMethods->stopInjection();
5244}
5245
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005246}; // namespace android