blob: 22a9663ff47f0865375cdf3c61f68965c719d75b [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 Chowdhary646c31b2020-01-30 13:09:59 -0800259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800265 } 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 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700273 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800274
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800275 if (mStatus == STATUS_ERROR) {
276 CLOGE("Shutting down in an error state");
277 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800279 if (mStatusTracker != NULL) {
280 mStatusTracker->requestExit();
281 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800283 if (mRequestThread != NULL) {
284 mRequestThread->requestExit();
285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800287 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
288 for (size_t i = 0; i < mOutputStreams.size(); i++) {
289 streams.push_back(mOutputStreams[i]);
290 }
291 if (mInputStream != nullptr) {
292 streams.push_back(mInputStream);
293 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800296 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
297 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
299 // HAL may be in a bad state, so waiting for request thread
300 // (which may be stuck in the HAL processCaptureRequest call)
301 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800302 // give up mInterfaceLock here and then lock it again. Could this lead
303 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 mRequestThread->join();
305 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800307 Mutex::Autolock il(mInterfaceLock);
308 if (mStatusTracker != NULL) {
309 mStatusTracker->join();
310 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800312 if (mInjectionMethods->isInjecting()) {
313 mInjectionMethods->stopInjection();
314 }
315
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800316 HalInterface* interface;
317 {
318 Mutex::Autolock l(mLock);
319 mRequestThread.clear();
320 Mutex::Autolock stLock(mTrackerLock);
321 mStatusTracker.clear();
322 interface = mInterface.get();
323 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700324
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800325 // Call close without internal mutex held, as the HAL close may need to
326 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000327 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700328
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800329 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800330
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800331 {
332 Mutex::Autolock l(mLock);
333 mInterface->clear();
334 mOutputStreams.clear();
335 mInputStream.clear();
336 mDeletedStreams.clear();
337 mBufferManager.clear();
338 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
339 }
340
341 for (auto& weakStream : streams) {
342 sp<Camera3StreamInterface> stream = weakStream.promote();
343 if (stream != nullptr) {
344 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
345 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
346 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700347 }
348 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700349 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000350
351 if (mCameraServiceWatchdog != NULL) {
352 mCameraServiceWatchdog->requestExit();
353 mCameraServiceWatchdog.clear();
354 }
355
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700356 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800357}
358
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359// For dumping/debugging only -
360// try to acquire a lock a few times, eventually give up to proceed with
361// debug/dump operations
362bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
363 bool gotLock = false;
364 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
365 if (lock.tryLock() == NO_ERROR) {
366 gotLock = true;
367 break;
368 } else {
369 usleep(kDumpSleepDuration);
370 }
371 }
372 return gotLock;
373}
374
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800375nsecs_t Camera3Device::getMonoToBoottimeOffset() {
376 // try three times to get the clock offset, choose the one
377 // with the minimum gap in measurements.
378 const int tries = 3;
379 nsecs_t bestGap, measured;
380 for (int i = 0; i < tries; ++i) {
381 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
382 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
383 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
384 const nsecs_t gap = tmono2 - tmono;
385 if (i == 0 || gap < bestGap) {
386 bestGap = gap;
387 measured = tbase - ((tmono + tmono2) >> 1);
388 }
389 }
390 return measured;
391}
392
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800393ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
394 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700395 // Get max jpeg size (area-wise) for default sensor pixel mode
396 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800397 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700398 /*isUltraHighResolutionSensor*/false);
399 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
400 // not ultra high res sensor
401 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800402 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700403 /*isUltraHighResolution*/true);
404 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800405 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
406 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700407 return BAD_VALUE;
408 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700409 bool useMaxSensorPixelModeThreshold = false;
410 if (uhrMaxJpegResolution.width != 0 &&
411 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
412 // Use the ultra high res max jpeg size and max jpeg buffer size
413 useMaxSensorPixelModeThreshold = true;
414 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700415
Zhijun Hef7da0962014-04-24 13:27:56 -0700416 // Get max jpeg buffer size
417 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800418 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700419 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800420 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
421 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 return BAD_VALUE;
423 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700425
426 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
427 if (useMaxSensorPixelModeThreshold) {
428 maxJpegBufferSize =
429 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
430 maxDefaultJpegResolution, maxJpegBufferSize);
431 chosenMaxJpegResolution = uhrMaxJpegResolution;
432 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800433 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700434
435 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700436 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700437 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800438 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
439 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700440 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800441 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
442 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700445 return jpegBufferSize;
446}
447
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800448ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700449 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800450 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700451 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800452 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
453 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 return BAD_VALUE;
455 }
456 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
457 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
458 return maxBytesForPointCloud;
459}
460
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800461ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
462 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800463 const int PER_CONFIGURATION_SIZE = 3;
464 const int WIDTH_OFFSET = 0;
465 const int HEIGHT_OFFSET = 1;
466 const int SIZE_OFFSET = 2;
467 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800468 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800469 camera3::SessionConfigurationUtils::getAppropriateModeTag(
470 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
471 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800472 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800473 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800474 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
475 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800476 return BAD_VALUE;
477 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700478
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800479 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
480 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
481 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
482 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
483 }
484 }
485
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800486 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
487 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800488 return BAD_VALUE;
489}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700490
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800491status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
492 ATRACE_CALL();
493 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700494
495 // Try to lock, but continue in case of failure (to avoid blocking in
496 // deadlocks)
497 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
498 bool gotLock = tryLockSpinRightRound(mLock);
499
500 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800501 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
502 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700503 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800504 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
505 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700506
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800507 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700508
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800509 String16 templatesOption("-t");
510 int n = args.size();
511 for (int i = 0; i < n; i++) {
512 if (args[i] == templatesOption) {
513 dumpTemplates = true;
514 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000515 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700516 if (i + 1 < n) {
517 String8 monitorTags = String8(args[i + 1]);
518 if (monitorTags == "off") {
519 mTagMonitor.disableMonitoring();
520 } else {
521 mTagMonitor.parseTagsToMonitor(monitorTags);
522 }
523 } else {
524 mTagMonitor.disableMonitoring();
525 }
526 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800527 }
528
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800529 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800531 const char *status =
532 mStatus == STATUS_ERROR ? "ERROR" :
533 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700534 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
535 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800536 mStatus == STATUS_ACTIVE ? "ACTIVE" :
537 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800539 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700540 if (mStatus == STATUS_ERROR) {
541 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
542 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800544 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000545 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
546 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
547 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800548 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800549
550 if (mInputStream != NULL) {
551 write(fd, lines.string(), lines.size());
552 mInputStream->dump(fd, args);
553 } else {
554 lines.appendFormat(" No input stream.\n");
555 write(fd, lines.string(), lines.size());
556 }
557 for (size_t i = 0; i < mOutputStreams.size(); i++) {
558 mOutputStreams[i]->dump(fd,args);
559 }
560
Zhijun He431503c2016-03-07 17:30:16 -0800561 if (mBufferManager != NULL) {
562 lines = String8(" Camera3 Buffer Manager:\n");
563 write(fd, lines.string(), lines.size());
564 mBufferManager->dump(fd, args);
565 }
Zhijun He125684a2015-12-26 15:07:30 -0800566
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700567 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700568 if (mInFlightLock.try_lock()) {
569 if (mInFlightMap.size() == 0) {
570 lines.append(" None\n");
571 } else {
572 for (size_t i = 0; i < mInFlightMap.size(); i++) {
573 InFlightRequest r = mInFlightMap.valueAt(i);
574 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
575 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
576 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
577 r.numBuffersLeft);
578 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700579 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700580 mInFlightLock.unlock();
581 } else {
582 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 }
584 write(fd, lines.string(), lines.size());
585
Shuzhen Wang686f6442017-06-20 16:16:04 -0700586 if (mRequestThread != NULL) {
587 mRequestThread->dumpCaptureRequestLatency(fd,
588 " ProcessCaptureRequest latency histogram:");
589 }
590
Igor Murashkin1e479c02013-09-06 16:55:14 -0700591 {
592 lines = String8(" Last request sent:\n");
593 write(fd, lines.string(), lines.size());
594
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700596 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
597 }
598
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800599 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700600 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800601 "TEMPLATE_PREVIEW",
602 "TEMPLATE_STILL_CAPTURE",
603 "TEMPLATE_VIDEO_RECORD",
604 "TEMPLATE_VIDEO_SNAPSHOT",
605 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800606 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800607 };
608
Emilian Peevf4816702020-04-03 15:44:51 -0700609 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800610 camera_metadata_t *templateRequest = nullptr;
611 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700612 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800614 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 lines.append(" Not supported\n");
616 write(fd, lines.string(), lines.size());
617 } else {
618 write(fd, lines.string(), lines.size());
619 dump_indented_camera_metadata(templateRequest,
620 fd, /*verbosity*/2, /*indentation*/8);
621 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800622 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800623 }
624 }
625
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700626 mTagMonitor.dumpMonitoredMetadata(fd);
627
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800628 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800629 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800631 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800633
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700634 if (gotLock) mLock.unlock();
635 if (gotInterfaceLock) mInterfaceLock.unlock();
636
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 return OK;
638}
639
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700640status_t Camera3Device::startWatchingTags(const String8 &tags) {
641 mTagMonitor.parseTagsToMonitor(tags);
642 return OK;
643}
644
645status_t Camera3Device::stopWatchingTags() {
646 mTagMonitor.disableMonitoring();
647 return OK;
648}
649
650status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
651 mTagMonitor.getLatestMonitoredTagEvents(out);
652 return OK;
653}
654
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800655const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800656 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
658 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 mStatus == STATUS_ERROR ?
661 "when in error state" : "before init");
662 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700663 if (physicalId.isEmpty()) {
664 return mDeviceInfo;
665 } else {
666 std::string id(physicalId.c_str());
667 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
668 return mPhysicalDeviceInfoMap.at(id);
669 } else {
670 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
671 return mDeviceInfo;
672 }
673 }
674}
675
676const CameraMetadata& Camera3Device::info() const {
677 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800678 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800679}
680
Jianing Wei90e59c92014-03-12 18:29:36 -0700681status_t Camera3Device::checkStatusOkToCaptureLocked() {
682 switch (mStatus) {
683 case STATUS_ERROR:
684 CLOGE("Device has encountered a serious error");
685 return INVALID_OPERATION;
686 case STATUS_UNINITIALIZED:
687 CLOGE("Device not initialized");
688 return INVALID_OPERATION;
689 case STATUS_UNCONFIGURED:
690 case STATUS_CONFIGURED:
691 case STATUS_ACTIVE:
692 // OK
693 break;
694 default:
695 SET_ERR_L("Unexpected status: %d", mStatus);
696 return INVALID_OPERATION;
697 }
698 return OK;
699}
700
701status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000702 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700703 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700704 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700706 if (requestList == NULL) {
707 CLOGE("requestList cannot be NULL.");
708 return BAD_VALUE;
709 }
710
Jianing Weicb0652e2014-03-12 18:29:36 -0700711 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000712 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700713 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
714 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
715 ++metadataIt, ++surfaceMapIt) {
716 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 if (newRequest == 0) {
718 CLOGE("Can't create capture request");
719 return BAD_VALUE;
720 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700721
Shuzhen Wang9d066012016-09-30 11:30:20 -0700722 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700723 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700724
Jianing Weicb0652e2014-03-12 18:29:36 -0700725 // Setup burst Id and request Id
726 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800727 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
728 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700729 CLOGE("RequestID does not exist in metadata");
730 return BAD_VALUE;
731 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800732 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700733
Jianing Wei90e59c92014-03-12 18:29:36 -0700734 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700735
736 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700737 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700738 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
739 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
740 return BAD_VALUE;
741 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700742
743 // Setup batch size if this is a high speed video recording request.
744 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
745 auto firstRequest = requestList->begin();
746 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
747 if (outputStream->isVideoStream()) {
748 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800749 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700750 break;
751 }
752 }
753 }
754
Jianing Wei90e59c92014-03-12 18:29:36 -0700755 return OK;
756}
757
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800758status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Emilian Peevaebbe412018-01-15 13:53:24 +0000761 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000763 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800765 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766}
767
Emilian Peevaebbe412018-01-15 13:53:24 +0000768void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 std::list<const SurfaceMap>& surfaceMaps,
770 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000771 PhysicalCameraSettingsList requestList;
772 requestList.push_back({std::string(getId().string()), request});
773 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774
775 SurfaceMap surfaceMap;
776 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
777 // With no surface list passed in, stream and surface will have 1-to-1
778 // mapping. So the surface index is 0 for each stream in the surfaceMap.
779 for (size_t i = 0; i < streams.count; i++) {
780 surfaceMap[streams.data.i32[i]].push_back(0);
781 }
782 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000786 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 const std::list<const SurfaceMap> &surfaceMaps,
788 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700789 /*out*/
790 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700791 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700792 nsecs_t requestTimeNs = systemTime();
793
Jianing Wei90e59c92014-03-12 18:29:36 -0700794 Mutex::Autolock il(mInterfaceLock);
795 Mutex::Autolock l(mLock);
796
797 status_t res = checkStatusOkToCaptureLocked();
798 if (res != OK) {
799 // error logged by previous call
800 return res;
801 }
802
803 RequestList requestList;
804
Shuzhen Wang0129d522016-10-30 22:43:41 -0700805 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700806 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700807 if (res != OK) {
808 // error logged by previous call
809 return res;
810 }
811
812 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700813 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700814 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700815 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700816 }
817
818 if (res == OK) {
819 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
820 if (res != OK) {
821 SET_ERR_L("Can't transition to active in %f seconds!",
822 kActiveTimeout/1e9);
823 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800824 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700825 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700826 } else {
827 CLOGE("Cannot queue request. Impossible.");
828 return BAD_VALUE;
829 }
830
831 return res;
832}
833
Emilian Peevaebbe412018-01-15 13:53:24 +0000834status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700835 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700836 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700837 ATRACE_CALL();
838
Emilian Peevaebbe412018-01-15 13:53:24 +0000839 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700840}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800841
Jianing Weicb0652e2014-03-12 18:29:36 -0700842status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
843 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845
Emilian Peevaebbe412018-01-15 13:53:24 +0000846 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700847 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000848 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700849
Emilian Peevaebbe412018-01-15 13:53:24 +0000850 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700851 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852}
853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854status_t Camera3Device::setStreamingRequestList(
855 const List<const PhysicalCameraSettingsList> &requestsList,
856 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 ATRACE_CALL();
858
Emilian Peevaebbe412018-01-15 13:53:24 +0000859 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700860}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861
862sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000863 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 status_t res;
865
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800867 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
868 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000869 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700870 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700871 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700873 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700875 } else if (mStatus == STATUS_UNCONFIGURED) {
876 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 CLOGE("No streams configured");
878 return NULL;
879 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 }
881
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800884}
885
Jianing Weicb0652e2014-03-12 18:29:36 -0700886status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800890
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 switch (mStatus) {
892 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700893 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 return INVALID_OPERATION;
895 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700898 case STATUS_UNCONFIGURED:
899 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 case STATUS_ACTIVE:
901 // OK
902 break;
903 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700904 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 return INVALID_OPERATION;
906 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800907 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700908
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700909 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910}
911
912status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
913 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917}
918
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700919status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800920 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700923 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700924 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800925 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
926 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700927
928 status_t res;
929 bool wasActive = false;
930
931 switch (mStatus) {
932 case STATUS_ERROR:
933 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
934 return INVALID_OPERATION;
935 case STATUS_UNINITIALIZED:
936 ALOGE("%s: Device not initialized", __FUNCTION__);
937 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 case STATUS_UNCONFIGURED:
939 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700940 // OK
941 break;
942 case STATUS_ACTIVE:
943 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700944 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700945 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700947 return res;
948 }
949 wasActive = true;
950 break;
951 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700953 return INVALID_OPERATION;
954 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700956
957 if (mInputStream != 0) {
958 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
959 return INVALID_OPERATION;
960 }
961
962 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
963 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700965
966 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800967 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700968
969 *id = mNextStreamId++;
970
971 // Continue captures if active at start
972 if (wasActive) {
973 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100974 // Reuse current operating mode and session parameters for new stream config
975 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700976 if (res != OK) {
977 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
978 __FUNCTION__, mNextStreamId, strerror(-res), res);
979 return res;
980 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700982 }
983
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800984 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700985 return OK;
986}
987
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700988status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700989 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700990 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800991 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800992 const std::unordered_set<int32_t> &sensorPixelModesUsed,
993 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800994 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800995 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700996 ATRACE_CALL();
997
998 if (consumer == nullptr) {
999 ALOGE("%s: consumer must not be null", __FUNCTION__);
1000 return BAD_VALUE;
1001 }
1002
1003 std::vector<sp<Surface>> consumers;
1004 consumers.push_back(consumer);
1005
1006 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001007 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001008 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001009 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001010}
1011
1012static bool isRawFormat(int format) {
1013 switch (format) {
1014 case HAL_PIXEL_FORMAT_RAW16:
1015 case HAL_PIXEL_FORMAT_RAW12:
1016 case HAL_PIXEL_FORMAT_RAW10:
1017 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1018 return true;
1019 default:
1020 return false;
1021 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001022}
1023
1024status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1025 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001026 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001027 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001028 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001029 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1030 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001031 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001032
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001034 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001036 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001037 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001038 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1039 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001040 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001041 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001042 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 status_t res;
1045 bool wasActive = false;
1046
1047 switch (mStatus) {
1048 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 case STATUS_UNCONFIGURED:
1055 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 // OK
1057 break;
1058 case STATUS_ACTIVE:
1059 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001060 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001062 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return res;
1064 }
1065 wasActive = true;
1066 break;
1067 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072
1073 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001074
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 if (consumers.size() == 0 && !hasDeferredConsumer) {
1076 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1077 return BAD_VALUE;
1078 }
Zhijun He5d677d12016-05-29 16:52:39 -07001079
Shuzhen Wang0129d522016-10-30 22:43:41 -07001080 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001081 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1082 return BAD_VALUE;
1083 }
1084
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001085 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1086 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1087 // be found in only one sensor pixel mode.
1088 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1089 return BAD_VALUE;
1090 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001091 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001093 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001094 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001095 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001096 if (blobBufferSize <= 0) {
1097 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1098 return BAD_VALUE;
1099 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001100 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1101 blobBufferSize = width * height;
1102 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001103 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001104 if (blobBufferSize <= 0) {
1105 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1106 return BAD_VALUE;
1107 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001108 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001109 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001110 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001111 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001112 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001113 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001114 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001115 bool maxResolution =
1116 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1117 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001118 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1119 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001120 if (rawOpaqueBufferSize <= 0) {
1121 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1122 return BAD_VALUE;
1123 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001124 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001125 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001126 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001127 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001128 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001129 } else if (isShared) {
1130 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1131 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001132 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001133 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001134 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001135 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001136 newStream = new Camera3OutputStream(mNextStreamId,
1137 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001138 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001139 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001140 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001142 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001143 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001144 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001145 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001146 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 }
Emilian Peev40ead602017-09-26 15:46:36 +01001148
1149 size_t consumerCount = consumers.size();
1150 for (size_t i = 0; i < consumerCount; i++) {
1151 int id = newStream->getSurfaceId(consumers[i]);
1152 if (id < 0) {
1153 SET_ERR_L("Invalid surface id");
1154 return BAD_VALUE;
1155 }
1156 if (surfaceIds != nullptr) {
1157 surfaceIds->push_back(id);
1158 }
1159 }
1160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162
Emilian Peev08dd2452017-04-06 16:55:14 +01001163 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001164
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001165 newStream->setImageDumpMask(mImageDumpMask);
1166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 res = mOutputStreams.add(mNextStreamId, newStream);
1168 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001169 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 return res;
1171 }
1172
Shuzhen Wang316781a2020-08-18 18:11:01 -07001173 mSessionStatsBuilder.addStream(mNextStreamId);
1174
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001176 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177
1178 // Continue captures if active at start
1179 if (wasActive) {
1180 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001181 // Reuse current operating mode and session parameters for new stream config
1182 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1185 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 return res;
1187 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001189 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001190 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
Emilian Peev710c1422017-08-30 11:19:38 +01001194status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001196 if (nullptr == streamInfo) {
1197 return BAD_VALUE;
1198 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001201
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 switch (mStatus) {
1203 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001204 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return INVALID_OPERATION;
1206 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 case STATUS_UNCONFIGURED:
1210 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 case STATUS_ACTIVE:
1212 // OK
1213 break;
1214 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 return INVALID_OPERATION;
1217 }
1218
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001219 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1220 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001222 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 }
1224
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001225 streamInfo->width = stream->getWidth();
1226 streamInfo->height = stream->getHeight();
1227 streamInfo->format = stream->getFormat();
1228 streamInfo->dataSpace = stream->getDataSpace();
1229 streamInfo->formatOverridden = stream->isFormatOverridden();
1230 streamInfo->originalFormat = stream->getOriginalFormat();
1231 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1232 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001233 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235}
1236
1237status_t Camera3Device::setStreamTransform(int id,
1238 int transform) {
1239 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001240 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 switch (mStatus) {
1244 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
1247 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 case STATUS_UNCONFIGURED:
1251 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 case STATUS_ACTIVE:
1253 // OK
1254 break;
1255 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return INVALID_OPERATION;
1258 }
1259
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001260 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1261 if (stream == nullptr) {
1262 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return BAD_VALUE;
1264 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001265 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
1268status_t Camera3Device::deleteStream(int id) {
1269 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 Mutex::Autolock l(mLock);
1272 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001274 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001276 // CameraDevice semantics require device to already be idle before
1277 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001279 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001280 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 }
1282
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001283 if (mStatus == STATUS_ERROR) {
1284 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1285 __FUNCTION__, mId.string());
1286 return -EBUSY;
1287 }
1288
Igor Murashkin2fba5842013-04-22 14:03:54 -07001289 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001290 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 if (mInputStream != NULL && id == mInputStream->getId()) {
1292 deletedStream = mInputStream;
1293 mInputStream.clear();
1294 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001295 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001296 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 return BAD_VALUE;
1298 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001299 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001300 }
1301
1302 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001303 if (stream != nullptr) {
1304 deletedStream = stream;
1305 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 }
1307
1308 // Free up the stream endpoint so that it can be used by some other stream
1309 res = deletedStream->disconnect();
1310 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 // fall through since we want to still list the stream as deleted.
1313 }
1314 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001315 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316
1317 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318}
1319
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001320status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001321 ATRACE_CALL();
1322 ALOGV("%s: E", __FUNCTION__);
1323
1324 Mutex::Autolock il(mInterfaceLock);
1325 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001326
Emilian Peev811d2952018-05-25 11:08:40 +01001327 // In case the client doesn't include any session parameter, try a
1328 // speculative configuration using the values from the last cached
1329 // default request.
1330 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001331 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001332 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1333 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1334 mLastTemplateId);
1335 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1336 operatingMode);
1337 }
1338
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001339 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1340}
1341
1342status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1343 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001344 //Filter out any incoming session parameters
1345 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001346 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1347 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001348 CameraMetadata filteredParams(availableSessionKeys.count);
1349 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1350 filteredParams.getAndLock());
1351 set_camera_metadata_vendor_id(meta, mVendorTagId);
1352 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001353 if (availableSessionKeys.count > 0) {
1354 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1355 camera_metadata_ro_entry entry = params.find(
1356 availableSessionKeys.data.i32[i]);
1357 if (entry.count > 0) {
1358 filteredParams.update(entry);
1359 }
1360 }
1361 }
1362
1363 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001364}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001366status_t Camera3Device::getInputBufferProducer(
1367 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001368 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001369 Mutex::Autolock il(mInterfaceLock);
1370 Mutex::Autolock l(mLock);
1371
1372 if (producer == NULL) {
1373 return BAD_VALUE;
1374 } else if (mInputStream == NULL) {
1375 return INVALID_OPERATION;
1376 }
1377
1378 return mInputStream->getInputBufferProducer(producer);
1379}
1380
Emilian Peevf4816702020-04-03 15:44:51 -07001381status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382 CameraMetadata *request) {
1383 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001384 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001385
Emilian Peevf4816702020-04-03 15:44:51 -07001386 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001387 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001388 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001389 return BAD_VALUE;
1390 }
1391
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001394 {
1395 Mutex::Autolock l(mLock);
1396 switch (mStatus) {
1397 case STATUS_ERROR:
1398 CLOGE("Device has encountered a serious error");
1399 return INVALID_OPERATION;
1400 case STATUS_UNINITIALIZED:
1401 CLOGE("Device is not initialized!");
1402 return INVALID_OPERATION;
1403 case STATUS_UNCONFIGURED:
1404 case STATUS_CONFIGURED:
1405 case STATUS_ACTIVE:
1406 // OK
1407 break;
1408 default:
1409 SET_ERR_L("Unexpected status: %d", mStatus);
1410 return INVALID_OPERATION;
1411 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001412
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001413 if (!mRequestTemplateCache[templateId].isEmpty()) {
1414 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001415 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001416 return OK;
1417 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001418 }
1419
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001420 camera_metadata_t *rawRequest;
1421 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001422 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001423
1424 {
1425 Mutex::Autolock l(mLock);
1426 if (res == BAD_VALUE) {
1427 ALOGI("%s: template %d is not supported on this camera device",
1428 __FUNCTION__, templateId);
1429 return res;
1430 } else if (res != OK) {
1431 CLOGE("Unable to construct request template %d: %s (%d)",
1432 templateId, strerror(-res), res);
1433 return res;
1434 }
1435
1436 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1437 mRequestTemplateCache[templateId].acquire(rawRequest);
1438
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001439 // Override the template request with zoomRatioMapper
1440 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1441 &mRequestTemplateCache[templateId]);
1442 if (res != OK) {
1443 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1444 templateId, strerror(-res), res);
1445 return res;
1446 }
1447
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001448 // Fill in JPEG_QUALITY if not available
1449 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1450 static const uint8_t kDefaultJpegQuality = 95;
1451 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1452 &kDefaultJpegQuality, 1);
1453 }
1454
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001455 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001456 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001457 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458 return OK;
1459}
1460
1461status_t Camera3Device::waitUntilDrained() {
1462 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001463 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001464 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001467 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001468}
1469
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001470status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 switch (mStatus) {
1472 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001473 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 ALOGV("%s: Already idle", __FUNCTION__);
1475 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 case STATUS_CONFIGURED:
1477 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 case STATUS_ERROR:
1479 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 break;
1482 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484 return INVALID_OPERATION;
1485 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001486 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1487 maxExpectedDuration);
1488 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001489 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001490 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001491 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1492 res);
1493 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001494 return res;
1495}
1496
Ruben Brunk183f0562015-08-12 12:55:02 -07001497void Camera3Device::internalUpdateStatusLocked(Status status) {
1498 mStatus = status;
1499 mRecentStatusUpdates.add(mStatus);
1500 mStatusChanged.broadcast();
1501}
1502
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001503// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001504status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001505 if (mRequestThread.get() != nullptr) {
1506 mRequestThread->setPaused(true);
1507 } else {
1508 return NO_INIT;
1509 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001511 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1512 maxExpectedDuration);
1513 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001514 if (res != OK) {
1515 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001516 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517 }
1518
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001519 return res;
1520}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001521
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001522// Resume after internalPauseAndWaitLocked
1523status_t Camera3Device::internalResumeLocked() {
1524 status_t res;
1525
1526 mRequestThread->setPaused(false);
1527
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001528 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1529 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001530 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1531 if (res != OK) {
1532 SET_ERR_L("Can't transition to active in %f seconds!",
1533 kActiveTimeout/1e9);
1534 }
1535 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001537}
1538
Ruben Brunk183f0562015-08-12 12:55:02 -07001539status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001540 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001541
1542 size_t startIndex = 0;
1543 if (mStatusWaiters == 0) {
1544 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1545 // this status list
1546 mRecentStatusUpdates.clear();
1547 } else {
1548 // If other threads are waiting on updates to this status list, set the position of the
1549 // first element that this list will check rather than clearing the list.
1550 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001551 }
1552
Ruben Brunk183f0562015-08-12 12:55:02 -07001553 mStatusWaiters++;
1554
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001555 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001556 if (!active && mUseHalBufManager) {
1557 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001558 if (mStatus == STATUS_ACTIVE) {
1559 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001560 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001561 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001562 mRequestBufferSM.onWaitUntilIdle();
1563 }
1564
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001566 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001568 if (active == (mStatus == STATUS_ACTIVE)) {
1569 // Desired state is current
1570 break;
1571 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001572
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001573 nsecs_t timeElapsed = systemTime() - startTime;
1574 nsecs_t timeToWait = timeout - timeElapsed;
1575 if (timeToWait <= 0) {
1576 // Thread woke up spuriously but has timed out since.
1577 // Force out of loop with TIMED_OUT result.
1578 res = TIMED_OUT;
1579 break;
1580 }
1581 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001582 if (res != OK) break;
1583
Ruben Brunk183f0562015-08-12 12:55:02 -07001584 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1585 // transitions.
1586 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1587 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1588 __FUNCTION__);
1589
1590 // Encountered desired state since we began waiting
1591 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1593 stateSeen = true;
1594 break;
1595 }
1596 }
1597 } while (!stateSeen);
1598
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001599 if (signalPipelineDrain) {
1600 mRequestThread->resetPipelineDrain();
1601 }
1602
Ruben Brunk183f0562015-08-12 12:55:02 -07001603 mStatusWaiters--;
1604
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001605 return res;
1606}
1607
1608
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001609status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001610 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001611 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001612
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001613 if (listener != NULL && mListener != NULL) {
1614 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1615 }
1616 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001617 mRequestThread->setNotificationListener(listener);
1618 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001619
1620 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001621}
1622
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001623bool Camera3Device::willNotify3A() {
1624 return false;
1625}
1626
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001627status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001628 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001629 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001630
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001631 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001632 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1633 if (st == std::cv_status::timeout) {
1634 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001635 }
1636 }
1637 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638}
1639
Jianing Weicb0652e2014-03-12 18:29:36 -07001640status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001641 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001642 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001643
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001644 if (mResultQueue.empty()) {
1645 return NOT_ENOUGH_DATA;
1646 }
1647
Jianing Weicb0652e2014-03-12 18:29:36 -07001648 if (frame == NULL) {
1649 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1650 return BAD_VALUE;
1651 }
1652
1653 CaptureResult &result = *(mResultQueue.begin());
1654 frame->mResultExtras = result.mResultExtras;
1655 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001656 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001657 mResultQueue.erase(mResultQueue.begin());
1658
1659 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660}
1661
1662status_t Camera3Device::triggerAutofocus(uint32_t id) {
1663 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001664 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001665
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001666 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1667 // Mix-in this trigger into the next request and only the next request.
1668 RequestTrigger trigger[] = {
1669 {
1670 ANDROID_CONTROL_AF_TRIGGER,
1671 ANDROID_CONTROL_AF_TRIGGER_START
1672 },
1673 {
1674 ANDROID_CONTROL_AF_TRIGGER_ID,
1675 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001676 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001677 };
1678
1679 return mRequestThread->queueTrigger(trigger,
1680 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001681}
1682
1683status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1684 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001685 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001686
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001687 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1688 // Mix-in this trigger into the next request and only the next request.
1689 RequestTrigger trigger[] = {
1690 {
1691 ANDROID_CONTROL_AF_TRIGGER,
1692 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1693 },
1694 {
1695 ANDROID_CONTROL_AF_TRIGGER_ID,
1696 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001697 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001698 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001699
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001700 return mRequestThread->queueTrigger(trigger,
1701 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001702}
1703
1704status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1705 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001706 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001708 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1709 // Mix-in this trigger into the next request and only the next request.
1710 RequestTrigger trigger[] = {
1711 {
1712 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1713 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1714 },
1715 {
1716 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1717 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001718 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001719 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001720
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001721 return mRequestThread->queueTrigger(trigger,
1722 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001723}
1724
Jianing Weicb0652e2014-03-12 18:29:36 -07001725status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001726 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001727 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001728 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001729
Zhijun He7ef20392014-04-21 16:04:17 -07001730 {
1731 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001732
1733 // b/116514106 "disconnect()" can get called twice for the same device. The
1734 // camera device will not be initialized during the second run.
1735 if (mStatus == STATUS_UNINITIALIZED) {
1736 return OK;
1737 }
1738
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001739 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001740
1741 // Stop session and stream counter
1742 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001743 }
1744
Ravneetdbd5b242022-03-02 07:22:46 +00001745 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1746 uint64_t maxExpectedDuration = (getExpectedInFlightDuration() + kBaseGetBufferWait) / 1e6;
1747 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1748 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1749
1750 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001751}
1752
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001753status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001754 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1755}
1756
1757status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001758 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001759 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001760 Mutex::Autolock il(mInterfaceLock);
1761 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001762
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001763 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1764 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001765 CLOGE("Stream %d does not exist", streamId);
1766 return BAD_VALUE;
1767 }
1768
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001769 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001770 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001771 return BAD_VALUE;
1772 }
1773
1774 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001775 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001776 return BAD_VALUE;
1777 }
1778
Ruben Brunkc78ac262015-08-13 17:58:46 -07001779 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001780}
1781
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001782status_t Camera3Device::tearDown(int streamId) {
1783 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001784 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001785 Mutex::Autolock il(mInterfaceLock);
1786 Mutex::Autolock l(mLock);
1787
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001788 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1789 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001790 CLOGE("Stream %d does not exist", streamId);
1791 return BAD_VALUE;
1792 }
1793
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001794 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1795 CLOGE("Stream %d is a target of a in-progress request", streamId);
1796 return BAD_VALUE;
1797 }
1798
1799 return stream->tearDown();
1800}
1801
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001802status_t Camera3Device::addBufferListenerForStream(int streamId,
1803 wp<Camera3StreamBufferListener> listener) {
1804 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001805 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001806 Mutex::Autolock il(mInterfaceLock);
1807 Mutex::Autolock l(mLock);
1808
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001809 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1810 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001811 CLOGE("Stream %d does not exist", streamId);
1812 return BAD_VALUE;
1813 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001814 stream->addBufferListener(listener);
1815
1816 return OK;
1817}
1818
Austin Borger4a870a32022-02-25 01:48:41 +00001819float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1820 camera_metadata_entry minDurations =
1821 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1822 for (size_t i = 0; i < minDurations.count; i += 4) {
1823 if (minDurations.data.i64[i] == stream->getFormat()
1824 && minDurations.data.i64[i+1] == stream->getWidth()
1825 && minDurations.data.i64[i+2] == stream->getHeight()) {
1826 int64_t minFrameDuration = minDurations.data.i64[i+3];
1827 return 1e9f / minFrameDuration;
1828 }
1829 }
1830 return 0.0f;
1831}
1832
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001834 * Methods called by subclasses
1835 */
1836
1837void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001838 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001839 std::vector<int> streamIds;
1840 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001841 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001842
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001843 {
1844 // Need mLock to safely update state and synchronize to current
1845 // state of methods in flight.
1846 Mutex::Autolock l(mLock);
1847 // We can get various system-idle notices from the status tracker
1848 // while starting up. Only care about them if we've actually sent
1849 // in some requests recently.
1850 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1851 return;
1852 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001853 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1854 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001855 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001856
1857 // Skip notifying listener if we're doing some user-transparent
1858 // state changes
1859 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001860
Austin Borger4a870a32022-02-25 01:48:41 +00001861 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1862 auto stream = mOutputStreams[i];
1863 if (stream.get() == nullptr) continue;
1864
1865 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1866 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1867
1868 // Populate stream statistics in case of Idle
1869 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001870 streamIds.push_back(stream->getId());
1871 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1872 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001873 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001874 if (camera3Stream != nullptr) {
1875 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001876 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001877 }
1878 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001879 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001880 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001881 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001882 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001883 }
1884 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001885 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001886
1887 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001888 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001889 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001890 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001891 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001892 status_t res = OK;
1893 if (listener != nullptr) {
1894 if (idle) {
1895 // Get session stats from the builder, and notify the listener.
1896 int64_t requestCount, resultErrorCount;
1897 bool deviceError;
1898 std::map<int, StreamStats> streamStatsMap;
1899 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1900 &deviceError, &streamStatsMap);
1901 for (size_t i = 0; i < streamIds.size(); i++) {
1902 int streamId = streamIds[i];
1903 auto stats = streamStatsMap.find(streamId);
1904 if (stats != streamStatsMap.end()) {
1905 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1906 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1907 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1908 streamStats[i].mHistogramType =
1909 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1910 streamStats[i].mHistogramBins.assign(
1911 stats->second.mCaptureLatencyBins.begin(),
1912 stats->second.mCaptureLatencyBins.end());
1913 streamStats[i].mHistogramCounts.assign(
1914 stats->second.mCaptureLatencyHistogram.begin(),
1915 stats->second.mCaptureLatencyHistogram.end());
1916 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001917 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001918 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1919 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001920 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001921 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001922 }
1923 if (res != OK) {
1924 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1925 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001926 }
1927}
1928
Shuzhen Wang758c2152017-01-10 18:26:18 -08001929status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001930 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001931 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001932 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1933 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001934
1935 if (surfaceIds == nullptr) {
1936 return BAD_VALUE;
1937 }
1938
Zhijun He5d677d12016-05-29 16:52:39 -07001939 Mutex::Autolock il(mInterfaceLock);
1940 Mutex::Autolock l(mLock);
1941
Shuzhen Wang758c2152017-01-10 18:26:18 -08001942 if (consumers.size() == 0) {
1943 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001944 return BAD_VALUE;
1945 }
1946
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001947 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1948 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001949 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001950 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001951 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001952
1953 // isConsumerConfigurationDeferred will be off after setConsumers
1954 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001955 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001956 if (res != OK) {
1957 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1958 return res;
1959 }
1960
Emilian Peev40ead602017-09-26 15:46:36 +01001961 for (auto &consumer : consumers) {
1962 int id = stream->getSurfaceId(consumer);
1963 if (id < 0) {
1964 CLOGE("Invalid surface id!");
1965 return BAD_VALUE;
1966 }
1967 surfaceIds->push_back(id);
1968 }
1969
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001970 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001971 if (!stream->isConfiguring()) {
1972 CLOGE("Stream %d was already fully configured.", streamId);
1973 return INVALID_OPERATION;
1974 }
Zhijun He5d677d12016-05-29 16:52:39 -07001975
Shuzhen Wang0129d522016-10-30 22:43:41 -07001976 res = stream->finishConfiguration();
1977 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001978 // If finishConfiguration fails due to abandoned surface, do not set
1979 // device to error state.
1980 bool isSurfaceAbandoned =
1981 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1982 if (!isSurfaceAbandoned) {
1983 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1984 stream->getId(), strerror(-res), res);
1985 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001986 return res;
1987 }
Zhijun He5d677d12016-05-29 16:52:39 -07001988 }
1989
1990 return OK;
1991}
1992
Emilian Peev40ead602017-09-26 15:46:36 +01001993status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1994 const std::vector<OutputStreamInfo> &outputInfo,
1995 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1996 Mutex::Autolock il(mInterfaceLock);
1997 Mutex::Autolock l(mLock);
1998
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001999 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2000 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002001 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002002 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002003 }
2004
2005 for (const auto &it : removedSurfaceIds) {
2006 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2007 CLOGE("Shared surface still part of a pending request!");
2008 return -EBUSY;
2009 }
2010 }
2011
Emilian Peev40ead602017-09-26 15:46:36 +01002012 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2013 if (res != OK) {
2014 CLOGE("Stream %d failed to update stream (error %d %s) ",
2015 streamId, res, strerror(-res));
2016 if (res == UNKNOWN_ERROR) {
2017 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2018 __FUNCTION__);
2019 }
2020 return res;
2021 }
2022
2023 return res;
2024}
2025
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002026status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2027 Mutex::Autolock il(mInterfaceLock);
2028 Mutex::Autolock l(mLock);
2029
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002030 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2031 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002032 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2033 return BAD_VALUE;
2034 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002035
2036 if (dropping) {
2037 mSessionStatsBuilder.stopCounter(streamId);
2038 } else {
2039 mSessionStatsBuilder.startCounter(streamId);
2040 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002041 return stream->dropBuffers(dropping);
2042}
2043
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002044/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 * Camera3Device private methods
2046 */
2047
2048sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002049 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002051
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002052 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002053 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002054
2055 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002056 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057 if (inputStreams.count > 0) {
2058 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002059 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002060 CLOGE("Request references unknown input stream %d",
2061 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002062 return NULL;
2063 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002064
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002066 SET_ERR_L("%s: input stream %d is not configured!",
2067 __FUNCTION__, mInputStream->getId());
2068 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002070 // Check if stream prepare is blocking requests.
2071 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002072 CLOGE("Request references an input stream that's being prepared!");
2073 return NULL;
2074 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075
2076 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002077 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002078 }
2079
2080 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002081 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002083 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002084 return NULL;
2085 }
2086
2087 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002088 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2089 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002090 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002091 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 return NULL;
2093 }
Zhijun He5d677d12016-05-29 16:52:39 -07002094 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002095 auto iter = surfaceMap.find(streams.data.i32[i]);
2096 if (iter != surfaceMap.end()) {
2097 const std::vector<size_t>& surfaces = iter->second;
2098 for (const auto& surface : surfaces) {
2099 if (stream->isConsumerConfigurationDeferred(surface)) {
2100 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2101 "due to deferred consumer", stream->getId(), surface);
2102 return NULL;
2103 }
2104 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002105 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002106 }
2107
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002108 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002109 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2110 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002111 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002112 // Check if stream prepare is blocking requests.
2113 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002114 CLOGE("Request references an output stream that's being prepared!");
2115 return NULL;
2116 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002117
2118 newRequest->mOutputStreams.push(stream);
2119 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002120 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002121 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002122
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002123 auto rotateAndCropEntry =
2124 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2125 if (rotateAndCropEntry.count > 0 &&
2126 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2127 newRequest->mRotateAndCropAuto = true;
2128 } else {
2129 newRequest->mRotateAndCropAuto = false;
2130 }
2131
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002132 auto zoomRatioEntry =
2133 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2134 if (zoomRatioEntry.count > 0 &&
2135 zoomRatioEntry.data.f[0] == 1.0f) {
2136 newRequest->mZoomRatioIs1x = true;
2137 } else {
2138 newRequest->mZoomRatioIs1x = false;
2139 }
2140
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002141 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002142 for (auto& settings : newRequest->mSettingsList) {
2143 auto testPatternModeEntry =
2144 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2145 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2146 testPatternModeEntry.data.i32[0] :
2147 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002148
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002149 auto testPatternDataEntry =
2150 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2151 if (testPatternDataEntry.count >= 4) {
2152 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2153 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2154 } else {
2155 settings.mOriginalTestPatternData[0] = 0;
2156 settings.mOriginalTestPatternData[1] = 0;
2157 settings.mOriginalTestPatternData[2] = 0;
2158 settings.mOriginalTestPatternData[3] = 0;
2159 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002160 }
2161 }
2162
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002163 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002164}
2165
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002166void Camera3Device::cancelStreamsConfigurationLocked() {
2167 int res = OK;
2168 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2169 res = mInputStream->cancelConfiguration();
2170 if (res != OK) {
2171 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2172 mInputStream->getId(), strerror(-res), res);
2173 }
2174 }
2175
2176 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002177 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002178 if (outputStream->isConfiguring()) {
2179 res = outputStream->cancelConfiguration();
2180 if (res != OK) {
2181 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2182 outputStream->getId(), strerror(-res), res);
2183 }
2184 }
2185 }
2186
2187 // Return state to that at start of call, so that future configures
2188 // properly clean things up
2189 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2190 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002191
2192 res = mPreparerThread->resume();
2193 if (res != OK) {
2194 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2195 }
2196}
2197
Emilian Peev0d0191e2020-04-21 17:01:18 -07002198bool Camera3Device::checkAbandonedStreamsLocked() {
2199 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2200 return true;
2201 }
2202
2203 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2204 auto stream = mOutputStreams[i];
2205 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2206 return true;
2207 }
2208 }
2209
2210 return false;
2211}
2212
Emilian Peev3bead5f2020-05-28 17:29:08 -07002213bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002214 ATRACE_CALL();
2215 bool ret = false;
2216
Shuzhen Wang316781a2020-08-18 18:11:01 -07002217 nsecs_t startTime = systemTime();
2218
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002219 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002220 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2221
2222 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002223 if (checkAbandonedStreamsLocked()) {
2224 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2225 __FUNCTION__);
2226 return true;
2227 }
2228
Emilian Peev3bead5f2020-05-28 17:29:08 -07002229 status_t rc = NO_ERROR;
2230 bool markClientActive = false;
2231 if (mStatus == STATUS_ACTIVE) {
2232 markClientActive = true;
2233 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002234
2235 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2236 }
2237
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002238 if (rc == NO_ERROR) {
2239 mNeedConfig = true;
2240 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2241 if (rc == NO_ERROR) {
2242 ret = true;
2243 mPauseStateNotify = false;
2244 //Moving to active state while holding 'mLock' is important.
2245 //There could be pending calls to 'create-/deleteStream' which
2246 //will trigger another stream configuration while the already
2247 //present streams end up with outstanding buffers that will
2248 //not get drained.
2249 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002250 } else if (rc == DEAD_OBJECT) {
2251 // DEAD_OBJECT can be returned if either the consumer surface is
2252 // abandoned, or the HAL has died.
2253 // - If the HAL has died, configureStreamsLocked call will set
2254 // device to error state,
2255 // - If surface is abandoned, we should not set device to error
2256 // state.
2257 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002258 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002259 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002260 }
2261 } else {
2262 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2263 }
2264
Austin Borger74fca042022-05-23 12:41:21 -07002265 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002266 ns2ms(systemTime() - startTime));
2267
Emilian Peev3bead5f2020-05-28 17:29:08 -07002268 if (markClientActive) {
2269 mStatusTracker->markComponentActive(clientStatusId);
2270 }
2271
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002272 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002273}
2274
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002275status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002276 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002277 ATRACE_CALL();
2278 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002279
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002280 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002281 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002282 return INVALID_OPERATION;
2283 }
2284
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002285 if (operatingMode < 0) {
2286 CLOGE("Invalid operating mode: %d", operatingMode);
2287 return BAD_VALUE;
2288 }
2289
2290 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002291 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002292
2293 if (mOperatingMode != operatingMode) {
2294 mNeedConfig = true;
2295 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2296 mOperatingMode = operatingMode;
2297 }
2298
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002299 // Reset min expected duration when session is reconfigured.
2300 mMinExpectedDuration = 0;
2301
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002302 // In case called from configureStreams, abort queued input buffers not belonging to
2303 // any pending requests.
2304 if (mInputStream != NULL && notifyRequestThread) {
2305 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002306 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002307 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002308 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002309 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002310 if (res != OK) {
2311 // Exhausted acquiring all input buffers.
2312 break;
2313 }
2314
Emilian Peevf4816702020-04-03 15:44:51 -07002315 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002316 res = mInputStream->returnInputBuffer(inputBuffer);
2317 if (res != OK) {
2318 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2319 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2320 }
2321 }
2322 }
2323
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002324 if (!mNeedConfig) {
2325 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2326 return OK;
2327 }
2328
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002329 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002330 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002331 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2332 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002333 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002334 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002335 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002336 }
2337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002338 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002339 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002341 mPreparerThread->pause();
2342
Emilian Peevf4816702020-04-03 15:44:51 -07002343 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002344 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002346 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002347
Emilian Peevf4816702020-04-03 15:44:51 -07002348 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002350 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002351
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002352
2353 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002354 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002355 inputStream = mInputStream->startConfiguration();
2356 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002357 CLOGE("Can't start input stream configuration");
2358 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002359 return INVALID_OPERATION;
2360 }
2361 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002362
2363 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002364 }
2365
Shuzhen Wang99080502021-03-07 21:08:20 -08002366 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002367 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002369
2370 // Don't configure bidi streams twice, nor add them twice to the list
2371 if (mOutputStreams[i].get() ==
2372 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2373
2374 config.num_streams--;
2375 continue;
2376 }
2377
Emilian Peevf4816702020-04-03 15:44:51 -07002378 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002379 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002380 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002381 CLOGE("Can't start output stream configuration");
2382 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 return INVALID_OPERATION;
2384 }
2385 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002386
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002387 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002388 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2389 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002390 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2391 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002392 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2393 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002394 } else if (outputStream->data_space ==
2395 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2396 bufferSizes[k] = outputStream->width * outputStream->height;
2397 } else {
2398 ALOGW("%s: Blob dataSpace %d not supported",
2399 __FUNCTION__, outputStream->data_space);
2400 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002401 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002402
2403 if (mOutputStreams[i]->isMultiResolution()) {
2404 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2405 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2406 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2407 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002408
2409 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2410 composerSurfacePresent = true;
2411 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412 }
2413
2414 config.streams = streams.editArray();
2415
2416 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002417 // max_buffers, usage, and priv fields, as well as data_space and format
2418 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002419
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002420 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002421 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002422 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002423
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002424 if (res == BAD_VALUE) {
2425 // HAL rejected this set of streams as unsupported, clean up config
2426 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002427 CLOGE("Set of requested inputs/outputs not supported by HAL");
2428 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002429 return BAD_VALUE;
2430 } else if (res != OK) {
2431 // Some other kind of error from configure_streams - this is not
2432 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002433 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2434 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002435 return res;
2436 }
2437
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002438 // Finish all stream configuration immediately.
2439 // TODO: Try to relax this later back to lazy completion, which should be
2440 // faster
2441
Igor Murashkin073f8572013-05-02 14:59:28 -07002442 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002443 bool streamReConfigured = false;
2444 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002445 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002446 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002447 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002448 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002449 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2450 return DEAD_OBJECT;
2451 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002452 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002453 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002454 if (streamReConfigured) {
2455 mInterface->onStreamReConfigured(mInputStream->getId());
2456 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002457 }
2458
2459 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002460 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002461 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002462 bool streamReConfigured = false;
2463 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002464 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002465 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002466 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002467 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002468 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2469 return DEAD_OBJECT;
2470 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002471 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002472 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002473 if (streamReConfigured) {
2474 mInterface->onStreamReConfigured(outputStream->getId());
2475 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002476 }
2477 }
2478
Emilian Peeve23f1d92021-09-20 14:56:01 -07002479 mRequestThread->setComposerSurface(composerSurfacePresent);
2480
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002481 // Request thread needs to know to avoid using repeat-last-settings protocol
2482 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002483 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002484 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2485 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002486 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487
Zhijun He90f7c372016-08-16 16:19:43 -07002488 char value[PROPERTY_VALUE_MAX];
2489 property_get("camera.fifo.disable", value, "0");
2490 int32_t disableFifo = atoi(value);
2491 if (disableFifo != 1) {
2492 // Boost priority of request thread to SCHED_FIFO.
2493 pid_t requestThreadTid = mRequestThread->getTid();
2494 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002495 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002496 if (res != OK) {
2497 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2498 strerror(-res), res);
2499 } else {
2500 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2501 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002502 }
2503
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002504 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002505 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2506 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2507 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2508 sessionParams.unlock(newSessionParams);
2509 mSessionParams.unlock(currentSessionParams);
2510 if (updateSessionParams) {
2511 mSessionParams = sessionParams;
2512 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002514 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002515
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002516 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002517 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002518
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002519 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002520
Zhijun He0a210512014-07-24 13:45:15 -07002521 // tear down the deleted streams after configure streams.
2522 mDeletedStreams.clear();
2523
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002524 auto rc = mPreparerThread->resume();
2525 if (rc != OK) {
2526 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2527 return rc;
2528 }
2529
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002530 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002531 mRequestBufferSM.onStreamsConfigured();
2532 }
2533
Cliff Wu3b268182021-07-06 15:44:43 +08002534 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002535 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002536 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002537 // configure the injection streams.
2538 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002539 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002540 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2541 res = mInjectionMethods->injectCamera(config, bufferSizes);
2542 if (res != OK) {
2543 ALOGE("Can't finish inject camera process!");
2544 return res;
2545 }
Cliff Wu3b268182021-07-06 15:44:43 +08002546 } else {
2547 // First run configureStreamsLocked() and then call injectCamera() case:
2548 // If the stream configuration has been completed and camera deive is active, but the
2549 // injection camera has not been injected yet, we need to store the stream configuration of
2550 // the internal camera (because the stream configuration of the injection camera is based
2551 // on the internal camera). When injecting occurs later, this configuration can be used by
2552 // the injection camera.
2553 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2554 " injection camera has not been injected yet.", __FUNCTION__);
2555 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002556 }
2557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002558 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002559}
2560
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002561status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002562 ATRACE_CALL();
2563 status_t res;
2564
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002565 if (mFakeStreamId != NO_STREAM) {
2566 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002567 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002568 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002569 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002570 return INVALID_OPERATION;
2571 }
2572
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002573 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002574
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002575 sp<Camera3OutputStreamInterface> fakeStream =
2576 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002577
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002578 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002579 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002580 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002581 return res;
2582 }
2583
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002584 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002585 mNextStreamId++;
2586
2587 return OK;
2588}
2589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002590status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002591 ATRACE_CALL();
2592 status_t res;
2593
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002594 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002595 if (mOutputStreams.size() == 1) return OK;
2596
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002597 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002598
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002599 // Ok, have a fake stream and there's at least one other output stream,
2600 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002603 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002604 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002605 return INVALID_OPERATION;
2606 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002607 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002608
2609 // Free up the stream endpoint so that it can be used by some other stream
2610 res = deletedStream->disconnect();
2611 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002612 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002613 // fall through since we want to still list the stream as deleted.
2614 }
2615 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002616 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002617
2618 return res;
2619}
2620
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002621void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002622 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002623 Mutex::Autolock l(mLock);
2624 va_list args;
2625 va_start(args, fmt);
2626
2627 setErrorStateLockedV(fmt, args);
2628
2629 va_end(args);
2630}
2631
2632void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002633 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002634 Mutex::Autolock l(mLock);
2635 setErrorStateLockedV(fmt, args);
2636}
2637
2638void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2639 va_list args;
2640 va_start(args, fmt);
2641
2642 setErrorStateLockedV(fmt, args);
2643
2644 va_end(args);
2645}
2646
2647void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002648 // Print out all error messages to log
2649 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002650 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002651
2652 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002653 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002654
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002655 mErrorCause = errorCause;
2656
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002657 if (mRequestThread != nullptr) {
2658 mRequestThread->setPaused(true);
2659 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002660 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661
2662 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002663 sp<NotificationListener> listener = mListener.promote();
2664 if (listener != NULL) {
2665 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002667 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002668 }
2669
2670 // Save stack trace. View by dumping it later.
2671 CameraTraces::saveTrace();
2672 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002673}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002674
2675/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002676 * In-flight request management
2677 */
2678
Jianing Weicb0652e2014-03-12 18:29:36 -07002679status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002680 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002681 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08002682 const std::set<std::set<String8>>& physicalCameraIds,
2683 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2684 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002685 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002686 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002687 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002688
2689 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002690 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002691 hasAppCallback, minExpectedDuration, maxExpectedDuration, physicalCameraIds,
2692 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2693 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002694 if (res < 0) return res;
2695
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002696 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002697 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2698 // avoid a deadlock during reprocess requests.
2699 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002700 if (mStatusTracker != nullptr) {
2701 mStatusTracker->markComponentActive(mInFlightStatusId);
2702 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002703 }
2704
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002705 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002706 return OK;
2707}
2708
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002709void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002710 // Indicate idle inFlightMap to the status tracker
2711 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002712 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002713 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2714 // avoid a deadlock during reprocess requests.
2715 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002716 if (mStatusTracker != nullptr) {
2717 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2718 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002719 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002720 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002721}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002722
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002723void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002724 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002725 // something has likely gone wrong. This might still be legit only if application send in
2726 // a long burst of long exposure requests.
2727 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2728 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2729 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2730 mInFlightMap.size(), mExpectedInflightDuration);
2731 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2732 kInFlightWarnLimitHighSpeed) {
2733 CLOGW("In-flight list too large for high speed configuration: %zu,"
2734 "total inflight duration %" PRIu64,
2735 mInFlightMap.size(), mExpectedInflightDuration);
2736 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002737 }
2738}
2739
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002740void Camera3Device::onInflightMapFlushedLocked() {
2741 mExpectedInflightDuration = 0;
2742}
2743
2744void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002745 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002746 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2747 mInFlightMap.removeItemsAt(idx, 1);
2748
2749 onInflightEntryRemovedLocked(duration);
2750}
2751
2752
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002753void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002754 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002755 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002756 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002757 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002758 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002759 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002760
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002761 FlushInflightReqStates states {
2762 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002763 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002764
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002765 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002766}
2767
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002768CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002769 ALOGV("%s", __FUNCTION__);
2770
Igor Murashkin1e479c02013-09-06 16:55:14 -07002771 CameraMetadata retVal;
2772
2773 if (mRequestThread != NULL) {
2774 retVal = mRequestThread->getLatestRequest();
2775 }
2776
Igor Murashkin1e479c02013-09-06 16:55:14 -07002777 return retVal;
2778}
2779
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002780void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002781 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002782 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002783 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2784 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002785
2786 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002787 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002788}
2789
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002790void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002791 std::vector<native_handle_t*> *handles, bool closeFd) {
2792 if (handles == nullptr) {
2793 return;
2794 }
2795 if (closeFd) {
2796 for (auto& handle : *handles) {
2797 native_handle_close(handle);
2798 }
2799 }
2800 for (auto& handle : *handles) {
2801 native_handle_delete(handle);
2802 }
2803 handles->clear();
2804 return;
2805}
2806
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002807/**
2808 * HalInterface inner class methods
2809 */
2810
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002811void Camera3Device::HalInterface::getInflightBufferKeys(
2812 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002813 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002814 return;
2815}
2816
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002817void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2818 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002819 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002820 return;
2821}
2822
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002823bool Camera3Device::HalInterface::verifyBufferIds(
2824 int32_t streamId, std::vector<uint64_t>& bufIds) {
2825 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002826}
2827
2828status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002829 int32_t frameNumber, int32_t streamId,
2830 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002831 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002832}
2833
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002834status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002835 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002836 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002837}
2838
2839// Find and pop a buffer_handle_t based on bufferId
2840status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002841 uint64_t bufferId,
2842 /*out*/ buffer_handle_t** buffer,
2843 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002844 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002845}
2846
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002847std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2848 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002849 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002850}
2851
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002852uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2853 const native_handle_t* handle) {
2854 return mBufferRecords.removeOneBufferCache(streamId, handle);
2855}
2856
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002857void Camera3Device::HalInterface::onBufferFreed(
2858 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002859 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2860 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2861 if (bufferId != BUFFER_ID_NO_BUFFER) {
2862 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002863 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002864}
2865
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002866void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002867 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2868 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2869 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002870 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2871 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002872}
2873
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002874/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002875 * RequestThread inner class methods
2876 */
2877
2878Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002879 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002880 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002881 bool useHalBufManager,
2882 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002883 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002885 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002886 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002887 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002888 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002889 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002890 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002891 mReconfigured(false),
2892 mDoPause(false),
2893 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002894 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002895 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002896 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002897 mCurrentAfTriggerId(0),
2898 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002899 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002900 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002901 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002902 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002903 mRepeatingLastFrameNumber(
2904 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002905 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002906 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002907 mRequestLatency(kRequestLatencyBinSize),
2908 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002909 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002910 mUseHalBufManager(useHalBufManager),
2911 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002912 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913}
2914
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002915Camera3Device::RequestThread::~RequestThread() {}
2916
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002917void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002918 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002919 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 Mutex::Autolock l(mRequestLock);
2921 mListener = listener;
2922}
2923
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002924void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002925 const CameraMetadata& sessionParams,
2926 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002927 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 Mutex::Autolock l(mRequestLock);
2929 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002930 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002931 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002932 // Prepare video stream for high speed recording.
2933 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002934 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002935}
2936
Jianing Wei90e59c92014-03-12 18:29:36 -07002937status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002938 List<sp<CaptureRequest> > &requests,
2939 /*out*/
2940 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002941 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002942 Mutex::Autolock l(mRequestLock);
2943 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2944 ++it) {
2945 mRequestQueue.push_back(*it);
2946 }
2947
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002948 if (lastFrameNumber != NULL) {
2949 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2950 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2951 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2952 *lastFrameNumber);
2953 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002954
Jianing Wei90e59c92014-03-12 18:29:36 -07002955 unpauseForNewRequests();
2956
2957 return OK;
2958}
2959
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002960
2961status_t Camera3Device::RequestThread::queueTrigger(
2962 RequestTrigger trigger[],
2963 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002964 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002965 Mutex::Autolock l(mTriggerMutex);
2966 status_t ret;
2967
2968 for (size_t i = 0; i < count; ++i) {
2969 ret = queueTriggerLocked(trigger[i]);
2970
2971 if (ret != OK) {
2972 return ret;
2973 }
2974 }
2975
2976 return OK;
2977}
2978
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002979const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2980 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002981 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002982 if (d != nullptr) return d->mId;
2983 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002984}
2985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002986status_t Camera3Device::RequestThread::queueTriggerLocked(
2987 RequestTrigger trigger) {
2988
2989 uint32_t tag = trigger.metadataTag;
2990 ssize_t index = mTriggerMap.indexOfKey(tag);
2991
2992 switch (trigger.getTagType()) {
2993 case TYPE_BYTE:
2994 // fall-through
2995 case TYPE_INT32:
2996 break;
2997 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002998 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2999 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003000 return INVALID_OPERATION;
3001 }
3002
3003 /**
3004 * Collect only the latest trigger, since we only have 1 field
3005 * in the request settings per trigger tag, and can't send more than 1
3006 * trigger per request.
3007 */
3008 if (index != NAME_NOT_FOUND) {
3009 mTriggerMap.editValueAt(index) = trigger;
3010 } else {
3011 mTriggerMap.add(tag, trigger);
3012 }
3013
3014 return OK;
3015}
3016
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003017status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003018 const RequestList &requests,
3019 /*out*/
3020 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003021 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003022 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003023 if (lastFrameNumber != NULL) {
3024 *lastFrameNumber = mRepeatingLastFrameNumber;
3025 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003026 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003027 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003028 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3029 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003030
3031 unpauseForNewRequests();
3032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003033 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034 return OK;
3035}
3036
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003037bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003038 if (mRepeatingRequests.empty()) {
3039 return false;
3040 }
3041 int32_t requestId = requestIn->mResultExtras.requestId;
3042 const RequestList &repeatRequests = mRepeatingRequests;
3043 // All repeating requests are guaranteed to have same id so only check first quest
3044 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3045 return (firstRequest->mResultExtras.requestId == requestId);
3046}
3047
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003048status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003049 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003050 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003051 return clearRepeatingRequestsLocked(lastFrameNumber);
3052
3053}
3054
3055status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003056 std::vector<int32_t> streamIds;
3057 for (const auto& request : mRepeatingRequests) {
3058 for (const auto& stream : request->mOutputStreams) {
3059 streamIds.push_back(stream->getId());
3060 }
3061 }
3062
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003063 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003064 if (lastFrameNumber != NULL) {
3065 *lastFrameNumber = mRepeatingLastFrameNumber;
3066 }
Emilian Peev2295df72021-11-12 18:14:10 -08003067
3068 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3069
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003070 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003071 return OK;
3072}
3073
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003074status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003075 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003076 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003077 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003078 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003079
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003080 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003081
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003082 // Send errors for all requests pending in the request queue, including
3083 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003084 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003085 if (listener != NULL) {
3086 for (RequestList::iterator it = mRequestQueue.begin();
3087 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003088 // Abort the input buffers for reprocess requests.
3089 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003090 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003091 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003092 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003093 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003094 if (res != OK) {
3095 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3096 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3097 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003098 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003099 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3100 if (res != OK) {
3101 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3102 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3103 }
3104 }
3105 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003106 // Set the frame number this request would have had, if it
3107 // had been submitted; this frame number will not be reused.
3108 // The requestId and burstId fields were set when the request was
3109 // submitted originally (in convertMetadataListToRequestListLocked)
3110 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003111 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003112 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003113 }
3114 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003115 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003116
3117 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003118 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003119 if (lastFrameNumber != NULL) {
3120 *lastFrameNumber = mRepeatingLastFrameNumber;
3121 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003122 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003123 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003124 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003125 return OK;
3126}
3127
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003128status_t Camera3Device::RequestThread::flush() {
3129 ATRACE_CALL();
3130 Mutex::Autolock l(mFlushLock);
3131
Emilian Peev08dd2452017-04-06 16:55:14 +01003132 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003133}
3134
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003135void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003136 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003137 Mutex::Autolock l(mPauseLock);
3138 mDoPause = paused;
3139 mDoPauseSignal.signal();
3140}
3141
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003142status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3143 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003144 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003145 Mutex::Autolock l(mLatestRequestMutex);
3146 status_t res;
3147 while (mLatestRequestId != requestId) {
3148 nsecs_t startTime = systemTime();
3149
3150 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3151 if (res != OK) return res;
3152
3153 timeout -= (systemTime() - startTime);
3154 }
3155
3156 return OK;
3157}
3158
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003159void Camera3Device::RequestThread::requestExit() {
3160 // Call parent to set up shutdown
3161 Thread::requestExit();
3162 // The exit from any possible waits
3163 mDoPauseSignal.signal();
3164 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003165
3166 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3167 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003168}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003169
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003170void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003171 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003172 bool surfaceAbandoned = false;
3173 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003174 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003175 {
3176 Mutex::Autolock l(mRequestLock);
3177 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3178 // repeating requests.
3179 for (const auto& request : mRepeatingRequests) {
3180 for (const auto& s : request->mOutputStreams) {
3181 if (s->isAbandoned()) {
3182 surfaceAbandoned = true;
3183 clearRepeatingRequestsLocked(&lastFrameNumber);
3184 break;
3185 }
3186 }
3187 if (surfaceAbandoned) {
3188 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003189 }
3190 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003191 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003192 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003193
3194 if (listener != NULL && surfaceAbandoned) {
3195 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003196 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003197}
3198
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003199bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003200 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003201 status_t res;
3202 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003203 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003204 uint32_t numRequestProcessed = 0;
3205 for (size_t i = 0; i < batchSize; i++) {
3206 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003207 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003208 }
3209
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003210 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3211
3212 bool triggerRemoveFailed = false;
3213 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3214 for (size_t i = 0; i < numRequestProcessed; i++) {
3215 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3216 nextRequest.submitted = true;
3217
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003218 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003219
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003220 if (!triggerRemoveFailed) {
3221 // Remove any previously queued triggers (after unlock)
3222 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3223 if (removeTriggerRes != OK) {
3224 triggerRemoveFailed = true;
3225 triggerFailedRequest = nextRequest;
3226 }
3227 }
3228 }
3229
3230 if (triggerRemoveFailed) {
3231 SET_ERR("RequestThread: Unable to remove triggers "
3232 "(capture request %d, HAL device: %s (%d)",
3233 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3234 cleanUpFailedRequests(/*sendRequestError*/ false);
3235 return false;
3236 }
3237
3238 if (res != OK) {
3239 // Should only get a failure here for malformed requests or device-level
3240 // errors, so consider all errors fatal. Bad metadata failures should
3241 // come through notify.
3242 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3243 mNextRequests[numRequestProcessed].halRequest.frame_number,
3244 strerror(-res), res);
3245 cleanUpFailedRequests(/*sendRequestError*/ false);
3246 return false;
3247 }
3248 return true;
3249}
3250
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003251std::pair<nsecs_t, nsecs_t> Camera3Device::RequestThread::calculateExpectedDurationRange(
3252 const camera_metadata_t *request) {
3253 std::pair<nsecs_t, nsecs_t> expectedRange(
3254 InFlightRequest::kDefaultMinExpectedDuration,
3255 InFlightRequest::kDefaultMaxExpectedDuration);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003256 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3257 find_camera_metadata_ro_entry(request,
3258 ANDROID_CONTROL_AE_MODE,
3259 &e);
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003260 if (e.count == 0) return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003261
3262 switch (e.data.u8[0]) {
3263 case ANDROID_CONTROL_AE_MODE_OFF:
3264 find_camera_metadata_ro_entry(request,
3265 ANDROID_SENSOR_EXPOSURE_TIME,
3266 &e);
3267 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003268 expectedRange.first = e.data.i64[0];
3269 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003270 }
3271 find_camera_metadata_ro_entry(request,
3272 ANDROID_SENSOR_FRAME_DURATION,
3273 &e);
3274 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003275 expectedRange.first = std::max(e.data.i64[0], expectedRange.first);
3276 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003277 }
3278 break;
3279 default:
3280 find_camera_metadata_ro_entry(request,
3281 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3282 &e);
3283 if (e.count > 1) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003284 expectedRange.first = 1e9 / e.data.i32[1];
3285 expectedRange.second = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003286 }
3287 break;
3288 }
3289
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003290 return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003291}
3292
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003293bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3294 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3295 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3296 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3297 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3298 return true;
3299 }
3300
3301 return false;
3302}
3303
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003304void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3305 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003306 camera_capture_request_t& halRequest = nextRequest.halRequest;
3307 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003308 Mutex::Autolock al(mLatestRequestMutex);
3309
Shuzhen Wang83bff122020-11-20 15:51:39 -08003310 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003311 mLatestRequest.acquire(cloned);
3312
3313 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003314 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3315 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3316 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003317 CameraMetadata(cloned));
3318 }
3319
3320 sp<Camera3Device> parent = mParent.promote();
3321 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003322 int32_t inputStreamId = -1;
3323 if (halRequest.input_buffer != nullptr) {
3324 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3325 }
3326
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003327 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003328 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003329 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3330 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003331 }
3332 }
3333
Shuzhen Wang83bff122020-11-20 15:51:39 -08003334 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003335 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003336 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003337 }
3338
Shuzhen Wang83bff122020-11-20 15:51:39 -08003339 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003340}
3341
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003342bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3343 ATRACE_CALL();
3344 bool updatesDetected = false;
3345
Emilian Peev4ec17882019-01-24 17:16:58 -08003346 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003347 for (auto tag : mSessionParamKeys) {
3348 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003349 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003350
3351 if (entry.count > 0) {
3352 bool isDifferent = false;
3353 if (lastEntry.count > 0) {
3354 // Have a last value, compare to see if changed
3355 if (lastEntry.type == entry.type &&
3356 lastEntry.count == entry.count) {
3357 // Same type and count, compare values
3358 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3359 size_t entryBytes = bytesPerValue * lastEntry.count;
3360 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3361 if (cmp != 0) {
3362 isDifferent = true;
3363 }
3364 } else {
3365 // Count or type has changed
3366 isDifferent = true;
3367 }
3368 } else {
3369 // No last entry, so always consider to be different
3370 isDifferent = true;
3371 }
3372
3373 if (isDifferent) {
3374 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003375 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3376 updatesDetected = true;
3377 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003378 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003379 }
3380 } else if (lastEntry.count > 0) {
3381 // Value has been removed
3382 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003383 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003384 updatesDetected = true;
3385 }
3386 }
3387
Emilian Peev4ec17882019-01-24 17:16:58 -08003388 bool reconfigureRequired;
3389 if (updatesDetected) {
3390 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3391 updatedParams);
3392 mLatestSessionParams = updatedParams;
3393 } else {
3394 reconfigureRequired = false;
3395 }
3396
3397 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003398}
3399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003400bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003401 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003402 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003403 // Any function called from threadLoop() must not hold mInterfaceLock since
3404 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3405 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003406
3407 // Handle paused state.
3408 if (waitIfPaused()) {
3409 return true;
3410 }
3411
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003412 // Wait for the next batch of requests.
3413 waitForNextRequestBatch();
3414 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003415 return true;
3416 }
3417
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003418 // Get the latest request ID, if any
3419 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003420 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003421 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003422 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003423 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003424 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003425 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3426 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003427 }
3428
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003429 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3430 // or a single request from streaming or burst. In either case the first element
3431 // should contain the latest camera settings that we need to check for any session
3432 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003433 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003434 res = OK;
3435
3436 //Input stream buffers are already acquired at this point so an input stream
3437 //will not be able to move to idle state unless we force it.
3438 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3439 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3440 if (res != OK) {
3441 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3442 cleanUpFailedRequests(/*sendRequestError*/ false);
3443 return false;
3444 }
3445 }
3446
3447 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003448 sp<Camera3Device> parent = mParent.promote();
3449 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003450 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3451 if (statusTracker != nullptr) {
3452 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3453 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003454 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003455 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003456 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003457
3458 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3459 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3460 if (res != OK) {
3461 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3462 cleanUpFailedRequests(/*sendRequestError*/ false);
3463 return false;
3464 }
3465 }
3466 }
3467 }
3468
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003469 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003470 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003471 if (res == TIMED_OUT) {
3472 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003473 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003474 // Check if any stream is abandoned.
3475 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003476 return true;
3477 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003478 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003479 return false;
3480 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003481
Zhijun Hecc27e112013-10-03 16:12:43 -07003482 // Inform waitUntilRequestProcessed thread of a new request ID
3483 {
3484 Mutex::Autolock al(mLatestRequestMutex);
3485
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003486 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003487 mLatestRequestSignal.signal();
3488 }
3489
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003490 // Submit a batch of requests to HAL.
3491 // Use flush lock only when submitting multilple requests in a batch.
3492 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3493 // which may take a long time to finish so synchronizing flush() and
3494 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3495 // For now, only synchronize for high speed recording and we should figure something out for
3496 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003497 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003498
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003499 if (useFlushLock) {
3500 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003501 }
3502
Zhijun Hef0645c12016-08-02 00:58:11 -07003503 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003504 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003505
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003506 sp<Camera3Device> parent = mParent.promote();
3507 if (parent != nullptr) {
3508 parent->mRequestBufferSM.onSubmittingRequest();
3509 }
3510
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003511 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003512 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003513 submitRequestSuccess = sendRequestsBatch();
3514
Shuzhen Wang686f6442017-06-20 16:16:04 -07003515 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3516 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003517
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003518 if (useFlushLock) {
3519 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003520 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003521
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003522 // Unset as current request
3523 {
3524 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003525 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003526 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003527 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003528
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003529 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003530}
3531
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003532status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3533 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3534 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3535 ANDROID_SCALER_CROP_REGION_SET};
3536 if (request == nullptr) {
3537 ALOGE("%s request metadata nullptr", __FUNCTION__);
3538 return BAD_VALUE;
3539 }
3540 status_t res = OK;
3541 for (const auto &key : kFwkOnlyRegionKeys) {
3542 if (request->exists(key)) {
3543 res = request->erase(key);
3544 if (res != OK) {
3545 return res;
3546 }
3547 }
3548 }
3549 return OK;
3550}
3551
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003552status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003553 ATRACE_CALL();
3554
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003555 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003556 for (size_t i = 0; i < mNextRequests.size(); i++) {
3557 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003558 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003559 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3560 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003561
3562 // Prepare a request to HAL
3563 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3564
3565 // Insert any queued triggers (before metadata is locked)
3566 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003567 if (res < 0) {
3568 SET_ERR("RequestThread: Unable to insert triggers "
3569 "(capture request %d, HAL device: %s (%d)",
3570 halRequest->frame_number, strerror(-res), res);
3571 return INVALID_OPERATION;
3572 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003573
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003574 int triggerCount = res;
3575 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3576 mPrevTriggers = triggerCount;
3577
Emilian Peeve23f1d92021-09-20 14:56:01 -07003578 // Do not override rotate&crop for stream configurations that include
3579 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3580 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3581 bool rotateAndCropChanged = mComposerOutput ? false :
3582 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003583 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003584
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003585 // If the request is the same as last, or we had triggers now or last time or
3586 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003587 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003588 (mPrevRequest != captureRequest || triggersMixedIn ||
3589 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003590 // Request settings are all the same within one batch, so only treat the first
3591 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003592 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003593 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003594 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003595 /**
3596 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003597 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003598 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003599 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003600 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003601 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003602 "(capture request %d, HAL device: %s (%d)",
3603 halRequest->frame_number, strerror(-res), res);
3604 return INVALID_OPERATION;
3605 }
3606
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003607 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003608 sp<Camera3Device> parent = mParent.promote();
3609 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003610 List<PhysicalCameraSettings>::iterator it;
3611 for (it = captureRequest->mSettingsList.begin();
3612 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003613 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3614 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003615 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3616 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3617 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3618 res);
3619 return INVALID_OPERATION;
3620 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003621 continue;
3622 }
3623
3624 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3625 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3626 updateCaptureRequest(&(it->metadata));
3627 if (res != OK) {
3628 SET_ERR("RequestThread: Unable to correct capture requests "
3629 "for scaler crop region and metering regions for request "
3630 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3631 res);
3632 return INVALID_OPERATION;
3633 }
3634 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003635 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3636 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3637 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3638 res);
3639 return INVALID_OPERATION;
3640 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003641 }
3642 }
3643
3644 // Correct metadata regions for distortion correction if enabled
3645 for (it = captureRequest->mSettingsList.begin();
3646 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003647 if (parent->mDistortionMappers.find(it->cameraId) ==
3648 parent->mDistortionMappers.end()) {
3649 continue;
3650 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003651
3652 if (!captureRequest->mDistortionCorrectionUpdated) {
3653 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3654 &(it->metadata));
3655 if (res != OK) {
3656 SET_ERR("RequestThread: Unable to correct capture requests "
3657 "for lens distortion for request %d: %s (%d)",
3658 halRequest->frame_number, strerror(-res), res);
3659 return INVALID_OPERATION;
3660 }
3661 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003662 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003663 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003664
3665 for (it = captureRequest->mSettingsList.begin();
3666 it != captureRequest->mSettingsList.end(); it++) {
3667 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3668 parent->mZoomRatioMappers.end()) {
3669 continue;
3670 }
3671
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003672 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003673 cameraIdsWithZoom.insert(it->cameraId);
3674 }
3675
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003676 if (!captureRequest->mZoomRatioUpdated) {
3677 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3678 &(it->metadata));
3679 if (res != OK) {
3680 SET_ERR("RequestThread: Unable to correct capture requests "
3681 "for zoom ratio for request %d: %s (%d)",
3682 halRequest->frame_number, strerror(-res), res);
3683 return INVALID_OPERATION;
3684 }
3685 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003686 }
3687 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003688 if (captureRequest->mRotateAndCropAuto &&
3689 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003690 for (it = captureRequest->mSettingsList.begin();
3691 it != captureRequest->mSettingsList.end(); it++) {
3692 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3693 if (mapper != parent->mRotateAndCropMappers.end()) {
3694 res = mapper->second.updateCaptureRequest(&(it->metadata));
3695 if (res != OK) {
3696 SET_ERR("RequestThread: Unable to correct capture requests "
3697 "for rotate-and-crop for request %d: %s (%d)",
3698 halRequest->frame_number, strerror(-res), res);
3699 return INVALID_OPERATION;
3700 }
3701 }
3702 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003703 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003704 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003705 }
3706 }
3707
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003708 /**
3709 * The request should be presorted so accesses in HAL
3710 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3711 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003712 captureRequest->mSettingsList.begin()->metadata.sort();
3713 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003714 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003715 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003716 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3717
3718 IF_ALOGV() {
3719 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3720 find_camera_metadata_ro_entry(
3721 halRequest->settings,
3722 ANDROID_CONTROL_AF_TRIGGER,
3723 &e
3724 );
3725 if (e.count > 0) {
3726 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3727 __FUNCTION__,
3728 halRequest->frame_number,
3729 e.data.u8[0]);
3730 }
3731 }
3732 } else {
3733 // leave request.settings NULL to indicate 'reuse latest given'
3734 ALOGVV("%s: Request settings are REUSED",
3735 __FUNCTION__);
3736 }
3737
Emilian Peevaebbe412018-01-15 13:53:24 +00003738 if (captureRequest->mSettingsList.size() > 1) {
3739 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3740 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003741 if (newRequest) {
3742 halRequest->physcam_settings =
3743 new const camera_metadata* [halRequest->num_physcam_settings];
3744 } else {
3745 halRequest->physcam_settings = nullptr;
3746 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003747 auto it = ++captureRequest->mSettingsList.begin();
3748 size_t i = 0;
3749 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3750 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003751 if (newRequest) {
3752 it->metadata.sort();
3753 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3754 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003755 }
3756 }
3757
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003758 uint32_t totalNumBuffers = 0;
3759
3760 // Fill in buffers
3761 if (captureRequest->mInputStream != NULL) {
3762 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003763
3764 halRequest->input_width = captureRequest->mInputBufferSize.width;
3765 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003766 totalNumBuffers += 1;
3767 } else {
3768 halRequest->input_buffer = NULL;
3769 }
3770
Emilian Peevf4816702020-04-03 15:44:51 -07003771 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003772 captureRequest->mOutputStreams.size());
3773 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003774 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003775
3776 sp<Camera3Device> parent = mParent.promote();
3777 if (parent == NULL) {
3778 // Should not happen, and nowhere to send errors to, so just log it
3779 CLOGE("RequestThread: Parent is gone");
3780 return INVALID_OPERATION;
3781 }
3782 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3783
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003784 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003785 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003786 sp<Camera3OutputStreamInterface> outputStream =
3787 captureRequest->mOutputStreams.editItemAt(j);
3788 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003789
3790 // Prepare video buffers for high speed recording on the first video request.
3791 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3792 // Only try to prepare video stream on the first video request.
3793 mPrepareVideoStream = false;
3794
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003795 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3796 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003797 while (res == NOT_ENOUGH_DATA) {
3798 res = outputStream->prepareNextBuffer();
3799 }
3800 if (res != OK) {
3801 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3802 __FUNCTION__, strerror(-res), res);
3803 outputStream->cancelPrepare();
3804 }
3805 }
3806
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003807 std::vector<size_t> uniqueSurfaceIds;
3808 res = outputStream->getUniqueSurfaceIds(
3809 captureRequest->mOutputSurfaces[streamId],
3810 &uniqueSurfaceIds);
3811 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3812 if (res != OK && res != INVALID_OPERATION) {
3813 ALOGE("%s: failed to query stream %d unique surface IDs",
3814 __FUNCTION__, streamId);
3815 return res;
3816 }
3817 if (res == OK) {
3818 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3819 }
3820
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003821 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003822 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003823 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003824 return TIMED_OUT;
3825 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003826 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003827 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003828 buffer.stream = outputStream->asHalStream();
3829 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003830 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003831 buffer.acquire_fence = -1;
3832 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003833 // Mark the output stream as unpreparable to block clients from calling
3834 // 'prepare' after this request reaches CameraHal and before the respective
3835 // buffers are requested.
3836 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003837 } else {
3838 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3839 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003840 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003841 if (res != OK) {
3842 // Can't get output buffer from gralloc queue - this could be due to
3843 // abandoned queue or other consumer misbehavior, so not a fatal
3844 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003845 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003846 " %s (%d)", strerror(-res), res);
3847
3848 return TIMED_OUT;
3849 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003850 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003851
3852 {
3853 sp<Camera3Device> parent = mParent.promote();
3854 if (parent != nullptr) {
3855 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3856 for (const auto& settings : captureRequest->mSettingsList) {
3857 if ((streamCameraId.isEmpty() &&
3858 parent->getId() == settings.cameraId.c_str()) ||
3859 streamCameraId == settings.cameraId.c_str()) {
3860 outputStream->fireBufferRequestForFrameNumber(
3861 captureRequest->mResultExtras.frameNumber,
3862 settings.metadata);
3863 }
3864 }
3865 }
3866 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003867
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003868 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003869 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3870 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3871 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3872 } else if (!physicalCameraId.isEmpty()) {
3873 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003874 }
3875 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003876 }
3877 totalNumBuffers += halRequest->num_output_buffers;
3878
3879 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003880 // If this request list is for constrained high speed recording (not
3881 // preview), and the current request is not the last one in the batch,
3882 // do not send callback to the app.
3883 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003884 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003885 hasCallback = false;
3886 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003887 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003888 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003889 const camera_metadata_t* settings = halRequest->settings;
3890 bool shouldUnlockSettings = false;
3891 if (settings == nullptr) {
3892 shouldUnlockSettings = true;
3893 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3894 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003895 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3896 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003897 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003898 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3899 isStillCapture = true;
3900 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3901 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003902
Emilian Peevaf8416e2021-02-04 17:52:43 -08003903 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003904 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003905 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3906 isZslCapture = true;
3907 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003908 }
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003909 auto expectedDurationRange = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003910 res = parent->registerInFlight(halRequest->frame_number,
3911 totalNumBuffers, captureRequest->mResultExtras,
3912 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003913 hasCallback,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003914 /*min*/expectedDurationRange.first,
3915 /*max*/expectedDurationRange.second,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003916 requestedPhysicalCameras, isStillCapture, isZslCapture,
3917 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003918 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003919 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003920 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3921 ", burstId = %" PRId32 ".",
3922 __FUNCTION__,
3923 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3924 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003925
3926 if (shouldUnlockSettings) {
3927 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3928 }
3929
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003930 if (res != OK) {
3931 SET_ERR("RequestThread: Unable to register new in-flight request:"
3932 " %s (%d)", strerror(-res), res);
3933 return INVALID_OPERATION;
3934 }
3935 }
3936
3937 return OK;
3938}
3939
Igor Murashkin1e479c02013-09-06 16:55:14 -07003940CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003941 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003942 Mutex::Autolock al(mLatestRequestMutex);
3943
3944 ALOGV("RequestThread::%s", __FUNCTION__);
3945
3946 return mLatestRequest;
3947}
3948
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003949bool Camera3Device::RequestThread::isStreamPending(
3950 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003951 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003952 Mutex::Autolock l(mRequestLock);
3953
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003954 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003955 if (!nextRequest.submitted) {
3956 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3957 if (stream == s) return true;
3958 }
3959 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003960 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003961 }
3962
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003963 for (const auto& request : mRequestQueue) {
3964 for (const auto& s : request->mOutputStreams) {
3965 if (stream == s) return true;
3966 }
3967 if (stream == request->mInputStream) return true;
3968 }
3969
3970 for (const auto& request : mRepeatingRequests) {
3971 for (const auto& s : request->mOutputStreams) {
3972 if (stream == s) return true;
3973 }
3974 if (stream == request->mInputStream) return true;
3975 }
3976
3977 return false;
3978}
Jianing Weicb0652e2014-03-12 18:29:36 -07003979
Emilian Peev40ead602017-09-26 15:46:36 +01003980bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3981 ATRACE_CALL();
3982 Mutex::Autolock l(mRequestLock);
3983
3984 for (const auto& nextRequest : mNextRequests) {
3985 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3986 if (s.first == streamId) {
3987 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3988 if (it != s.second.end()) {
3989 return true;
3990 }
3991 }
3992 }
3993 }
3994
3995 for (const auto& request : mRequestQueue) {
3996 for (const auto& s : request->mOutputSurfaces) {
3997 if (s.first == streamId) {
3998 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3999 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004000 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004001 }
4002 }
4003 }
4004 }
4005
4006 for (const auto& request : mRepeatingRequests) {
4007 for (const auto& s : request->mOutputSurfaces) {
4008 if (s.first == streamId) {
4009 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4010 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004011 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004012 }
4013 }
4014 }
4015 }
4016
4017 return false;
4018}
4019
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004020void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4021 if (!mUseHalBufManager) {
4022 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4023 return;
4024 }
4025
4026 Mutex::Autolock pl(mPauseLock);
4027 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004028 mInterface->signalPipelineDrain(streamIds);
4029 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004030 }
4031 // If request thread is still busy, wait until paused then notify HAL
4032 mNotifyPipelineDrain = true;
4033 mStreamIdsToBeDrained = streamIds;
4034}
4035
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004036void Camera3Device::RequestThread::resetPipelineDrain() {
4037 Mutex::Autolock pl(mPauseLock);
4038 mNotifyPipelineDrain = false;
4039 mStreamIdsToBeDrained.clear();
4040}
4041
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004042void Camera3Device::RequestThread::clearPreviousRequest() {
4043 Mutex::Autolock l(mRequestLock);
4044 mPrevRequest.clear();
4045}
4046
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004047status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4048 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4049 ATRACE_CALL();
4050 Mutex::Autolock l(mTriggerMutex);
4051 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4052 return BAD_VALUE;
4053 }
4054 mRotateAndCropOverride = rotateAndCropValue;
4055 return OK;
4056}
4057
Emilian Peeve23f1d92021-09-20 14:56:01 -07004058status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4059 ATRACE_CALL();
4060 Mutex::Autolock l(mTriggerMutex);
4061 mComposerOutput = composerSurfacePresent;
4062 return OK;
4063}
4064
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004065status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004066 ATRACE_CALL();
4067 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004068 if (muteMode != mCameraMute) {
4069 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004070 mCameraMuteChanged = true;
4071 }
4072 return OK;
4073}
4074
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004075nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004076 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004077 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004078 return mExpectedInflightDuration > kMinInflightDuration ?
4079 mExpectedInflightDuration : kMinInflightDuration;
4080}
4081
Emilian Peevaebbe412018-01-15 13:53:24 +00004082void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004083 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004084 if ((request == nullptr) || (halRequest == nullptr)) {
4085 ALOGE("%s: Invalid request!", __FUNCTION__);
4086 return;
4087 }
4088
4089 if (halRequest->num_physcam_settings > 0) {
4090 if (halRequest->physcam_id != nullptr) {
4091 delete [] halRequest->physcam_id;
4092 halRequest->physcam_id = nullptr;
4093 }
4094 if (halRequest->physcam_settings != nullptr) {
4095 auto it = ++(request->mSettingsList.begin());
4096 size_t i = 0;
4097 for (; it != request->mSettingsList.end(); it++, i++) {
4098 it->metadata.unlock(halRequest->physcam_settings[i]);
4099 }
4100 delete [] halRequest->physcam_settings;
4101 halRequest->physcam_settings = nullptr;
4102 }
4103 }
4104}
4105
Ravneetaeb20dc2022-03-30 05:33:03 +00004106status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4107 Mutex::Autolock il(mInterfaceLock);
4108 Mutex::Autolock l(mLock);
4109
4110 if (mCameraServiceWatchdog != NULL) {
4111 mCameraServiceWatchdog->setEnabled(enabled);
4112 }
4113
4114 return OK;
4115}
4116
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004117void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4118 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004119 return;
4120 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004121
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004122 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004123 // Skip the ones that have been submitted successfully.
4124 if (nextRequest.submitted) {
4125 continue;
4126 }
4127
4128 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004129 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4130 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004131
4132 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004133 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004134 }
4135
Emilian Peevaebbe412018-01-15 13:53:24 +00004136 cleanupPhysicalSettings(captureRequest, halRequest);
4137
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004138 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004139 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004140 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4141 }
4142
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004143 // No output buffer can be returned when using HAL buffer manager
4144 if (!mUseHalBufManager) {
4145 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4146 //Buffers that failed processing could still have
4147 //valid acquire fence.
4148 int acquireFence = (*outputBuffers)[i].acquire_fence;
4149 if (0 <= acquireFence) {
4150 close(acquireFence);
4151 outputBuffers->editItemAt(i).acquire_fence = -1;
4152 }
Emilian Peevf4816702020-04-03 15:44:51 -07004153 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004154 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4155 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004156 /*timestampIncreasing*/true, std::vector<size_t> (),
4157 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004158 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004159 }
4160
4161 if (sendRequestError) {
4162 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004163 sp<NotificationListener> listener = mListener.promote();
4164 if (listener != NULL) {
4165 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004166 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004167 captureRequest->mResultExtras);
4168 }
4169 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004170
4171 // Remove yet-to-be submitted inflight request from inflightMap
4172 {
4173 sp<Camera3Device> parent = mParent.promote();
4174 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004175 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004176 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4177 if (idx >= 0) {
4178 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4179 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4180 parent->removeInFlightMapEntryLocked(idx);
4181 }
4182 }
4183 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004184 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004185
4186 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004187 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004188}
4189
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004190void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004191 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004192 // Optimized a bit for the simple steady-state case (single repeating
4193 // request), to avoid putting that request in the queue temporarily.
4194 Mutex::Autolock l(mRequestLock);
4195
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004196 assert(mNextRequests.empty());
4197
4198 NextRequest nextRequest;
4199 nextRequest.captureRequest = waitForNextRequestLocked();
4200 if (nextRequest.captureRequest == nullptr) {
4201 return;
4202 }
4203
Emilian Peevf4816702020-04-03 15:44:51 -07004204 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004205 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004206 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004207
4208 // Wait for additional requests
4209 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4210
4211 for (size_t i = 1; i < batchSize; i++) {
4212 NextRequest additionalRequest;
4213 additionalRequest.captureRequest = waitForNextRequestLocked();
4214 if (additionalRequest.captureRequest == nullptr) {
4215 break;
4216 }
4217
Emilian Peevf4816702020-04-03 15:44:51 -07004218 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004219 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004220 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004221 }
4222
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004223 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004224 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004225 mNextRequests.size(), batchSize);
4226 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004227 }
4228
4229 return;
4230}
4231
4232sp<Camera3Device::CaptureRequest>
4233 Camera3Device::RequestThread::waitForNextRequestLocked() {
4234 status_t res;
4235 sp<CaptureRequest> nextRequest;
4236
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004237 while (mRequestQueue.empty()) {
4238 if (!mRepeatingRequests.empty()) {
4239 // Always atomically enqueue all requests in a repeating request
4240 // list. Guarantees a complete in-sequence set of captures to
4241 // application.
4242 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004243 if (mFirstRepeating) {
4244 mFirstRepeating = false;
4245 } else {
4246 for (auto& request : requests) {
4247 // For repeating requests, override timestamp request using
4248 // the time a request is inserted into the request queue,
4249 // because the original repeating request will have an old
4250 // fixed timestamp.
4251 request->mRequestTimeNs = systemTime();
4252 }
4253 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004254 RequestList::const_iterator firstRequest =
4255 requests.begin();
4256 nextRequest = *firstRequest;
4257 mRequestQueue.insert(mRequestQueue.end(),
4258 ++firstRequest,
4259 requests.end());
4260 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004261
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004262 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004263
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004264 break;
4265 }
4266
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004267 if (!mRequestClearing) {
4268 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4269 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004270
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004271 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4272 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004273 Mutex::Autolock pl(mPauseLock);
4274 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004275 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004276 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004277 if (mNotifyPipelineDrain) {
4278 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4279 mNotifyPipelineDrain = false;
4280 mStreamIdsToBeDrained.clear();
4281 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004282 // Let the tracker know
4283 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4284 if (statusTracker != 0) {
4285 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4286 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004287 sp<Camera3Device> parent = mParent.promote();
4288 if (parent != nullptr) {
4289 parent->mRequestBufferSM.onRequestThreadPaused();
4290 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004291 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004292 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004293 // Stop waiting for now and let thread management happen
4294 return NULL;
4295 }
4296 }
4297
4298 if (nextRequest == NULL) {
4299 // Don't have a repeating request already in hand, so queue
4300 // must have an entry now.
4301 RequestList::iterator firstRequest =
4302 mRequestQueue.begin();
4303 nextRequest = *firstRequest;
4304 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004305 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4306 sp<NotificationListener> listener = mListener.promote();
4307 if (listener != NULL) {
4308 listener->notifyRequestQueueEmpty();
4309 }
4310 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004311 }
4312
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004313 // In case we've been unpaused by setPaused clearing mDoPause, need to
4314 // update internal pause state (capture/setRepeatingRequest unpause
4315 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004316 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004317 if (mPaused) {
4318 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4319 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4320 if (statusTracker != 0) {
4321 statusTracker->markComponentActive(mStatusId);
4322 }
4323 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004324 mPaused = false;
4325
4326 // Check if we've reconfigured since last time, and reset the preview
4327 // request if so. Can't use 'NULL request == repeat' across configure calls.
4328 if (mReconfigured) {
4329 mPrevRequest.clear();
4330 mReconfigured = false;
4331 }
4332
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004333 if (nextRequest != NULL) {
4334 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004335 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4336 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004337
4338 // Since RequestThread::clear() removes buffers from the input stream,
4339 // get the right buffer here before unlocking mRequestLock
4340 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004341 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4342 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004343 if (res != OK) {
4344 // Can't get input buffer from gralloc queue - this could be due to
4345 // disconnected queue or other producer misbehavior, so not a fatal
4346 // error
4347 ALOGE("%s: Can't get input buffer, skipping request:"
4348 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004349
4350 sp<NotificationListener> listener = mListener.promote();
4351 if (listener != NULL) {
4352 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004353 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004354 nextRequest->mResultExtras);
4355 }
4356 return NULL;
4357 }
4358 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004359 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004360
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004361 return nextRequest;
4362}
4363
4364bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004365 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004366 status_t res;
4367 Mutex::Autolock l(mPauseLock);
4368 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004369 if (mPaused == false) {
4370 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004371 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004372 if (mNotifyPipelineDrain) {
4373 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4374 mNotifyPipelineDrain = false;
4375 mStreamIdsToBeDrained.clear();
4376 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004377 // Let the tracker know
4378 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4379 if (statusTracker != 0) {
4380 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4381 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004382 sp<Camera3Device> parent = mParent.promote();
4383 if (parent != nullptr) {
4384 parent->mRequestBufferSM.onRequestThreadPaused();
4385 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004386 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004387
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004388 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004389 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004390 return true;
4391 }
4392 }
4393 // We don't set mPaused to false here, because waitForNextRequest needs
4394 // to further manage the paused state in case of starvation.
4395 return false;
4396}
4397
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004398void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004399 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004400 // With work to do, mark thread as unpaused.
4401 // If paused by request (setPaused), don't resume, to avoid
4402 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004403 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004404 Mutex::Autolock p(mPauseLock);
4405 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004406 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4407 if (mPaused) {
4408 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4409 if (statusTracker != 0) {
4410 statusTracker->markComponentActive(mStatusId);
4411 }
4412 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004413 mPaused = false;
4414 }
4415}
4416
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004417void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4418 sp<Camera3Device> parent = mParent.promote();
4419 if (parent != NULL) {
4420 va_list args;
4421 va_start(args, fmt);
4422
4423 parent->setErrorStateV(fmt, args);
4424
4425 va_end(args);
4426 }
4427}
4428
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004429status_t Camera3Device::RequestThread::insertTriggers(
4430 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004431 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004432 Mutex::Autolock al(mTriggerMutex);
4433
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004434 sp<Camera3Device> parent = mParent.promote();
4435 if (parent == NULL) {
4436 CLOGE("RequestThread: Parent is gone");
4437 return DEAD_OBJECT;
4438 }
4439
Emilian Peevaebbe412018-01-15 13:53:24 +00004440 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004441 size_t count = mTriggerMap.size();
4442
4443 for (size_t i = 0; i < count; ++i) {
4444 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004445 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004446
4447 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4448 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4449 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004450 if (isAeTrigger) {
4451 request->mResultExtras.precaptureTriggerId = triggerId;
4452 mCurrentPreCaptureTriggerId = triggerId;
4453 } else {
4454 request->mResultExtras.afTriggerId = triggerId;
4455 mCurrentAfTriggerId = triggerId;
4456 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004457 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004458 }
4459
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004460 camera_metadata_entry entry = metadata.find(tag);
4461
4462 if (entry.count > 0) {
4463 /**
4464 * Already has an entry for this trigger in the request.
4465 * Rewrite it with our requested trigger value.
4466 */
4467 RequestTrigger oldTrigger = trigger;
4468
4469 oldTrigger.entryValue = entry.data.u8[0];
4470
4471 mTriggerReplacedMap.add(tag, oldTrigger);
4472 } else {
4473 /**
4474 * More typical, no trigger entry, so we just add it
4475 */
4476 mTriggerRemovedMap.add(tag, trigger);
4477 }
4478
4479 status_t res;
4480
4481 switch (trigger.getTagType()) {
4482 case TYPE_BYTE: {
4483 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4484 res = metadata.update(tag,
4485 &entryValue,
4486 /*count*/1);
4487 break;
4488 }
4489 case TYPE_INT32:
4490 res = metadata.update(tag,
4491 &trigger.entryValue,
4492 /*count*/1);
4493 break;
4494 default:
4495 ALOGE("%s: Type not supported: 0x%x",
4496 __FUNCTION__,
4497 trigger.getTagType());
4498 return INVALID_OPERATION;
4499 }
4500
4501 if (res != OK) {
4502 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4503 ", value %d", __FUNCTION__, trigger.getTagName(),
4504 trigger.entryValue);
4505 return res;
4506 }
4507
4508 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4509 trigger.getTagName(),
4510 trigger.entryValue);
4511 }
4512
4513 mTriggerMap.clear();
4514
4515 return count;
4516}
4517
4518status_t Camera3Device::RequestThread::removeTriggers(
4519 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004520 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004521 Mutex::Autolock al(mTriggerMutex);
4522
Emilian Peevaebbe412018-01-15 13:53:24 +00004523 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004524
4525 /**
4526 * Replace all old entries with their old values.
4527 */
4528 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4529 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4530
4531 status_t res;
4532
4533 uint32_t tag = trigger.metadataTag;
4534 switch (trigger.getTagType()) {
4535 case TYPE_BYTE: {
4536 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4537 res = metadata.update(tag,
4538 &entryValue,
4539 /*count*/1);
4540 break;
4541 }
4542 case TYPE_INT32:
4543 res = metadata.update(tag,
4544 &trigger.entryValue,
4545 /*count*/1);
4546 break;
4547 default:
4548 ALOGE("%s: Type not supported: 0x%x",
4549 __FUNCTION__,
4550 trigger.getTagType());
4551 return INVALID_OPERATION;
4552 }
4553
4554 if (res != OK) {
4555 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4556 ", trigger value %d", __FUNCTION__,
4557 trigger.getTagName(), trigger.entryValue);
4558 return res;
4559 }
4560 }
4561 mTriggerReplacedMap.clear();
4562
4563 /**
4564 * Remove all new entries.
4565 */
4566 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4567 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4568 status_t res = metadata.erase(trigger.metadataTag);
4569
4570 if (res != OK) {
4571 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4572 ", trigger value %d", __FUNCTION__,
4573 trigger.getTagName(), trigger.entryValue);
4574 return res;
4575 }
4576 }
4577 mTriggerRemovedMap.clear();
4578
4579 return OK;
4580}
4581
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004582status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004583 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004584 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004585 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004586 status_t res;
4587
Emilian Peevaebbe412018-01-15 13:53:24 +00004588 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004590 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004591 // exists
4592 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4593 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4594 if (afTrigger.count > 0 &&
4595 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4596 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004597 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004598 if (res != OK) return res;
4599 }
4600
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004601 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004602 // if none already exists
4603 camera_metadata_entry pcTrigger =
4604 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4605 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4606 if (pcTrigger.count > 0 &&
4607 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4608 pcId.count == 0) {
4609 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004610 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004611 if (res != OK) return res;
4612 }
4613
4614 return OK;
4615}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004616
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004617bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4618 const sp<CaptureRequest> &request) {
4619 ATRACE_CALL();
4620
4621 if (request->mRotateAndCropAuto) {
4622 Mutex::Autolock l(mTriggerMutex);
4623 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4624
4625 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4626 if (rotateAndCropEntry.count > 0) {
4627 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4628 return false;
4629 } else {
4630 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4631 return true;
4632 }
4633 } else {
4634 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4635 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4636 &rotateAndCrop_u8, 1);
4637 return true;
4638 }
4639 }
4640 return false;
4641}
4642
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004643bool Camera3Device::RequestThread::overrideTestPattern(
4644 const sp<CaptureRequest> &request) {
4645 ATRACE_CALL();
4646
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004647 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004648
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004649 Mutex::Autolock l(mTriggerMutex);
4650
4651 bool changed = false;
4652
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004653 // For a multi-camera, the physical cameras support the same set of
4654 // test pattern modes as the logical camera.
4655 for (auto& settings : request->mSettingsList) {
4656 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004657
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004658 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4659 int32_t testPatternData[4] = {
4660 settings.mOriginalTestPatternData[0],
4661 settings.mOriginalTestPatternData[1],
4662 settings.mOriginalTestPatternData[2],
4663 settings.mOriginalTestPatternData[3]
4664 };
4665 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4666 testPatternMode = mCameraMute;
4667 testPatternData[0] = 0;
4668 testPatternData[1] = 0;
4669 testPatternData[2] = 0;
4670 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004671 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004672
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004673 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4674 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4675 if (testPatternEntry.count > 0) {
4676 if (testPatternEntry.data.i32[0] != testPatternMode) {
4677 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004678 changed = true;
4679 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004680 } else if (supportTestPatternModeKey) {
4681 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4682 &testPatternMode, 1);
4683 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004684 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004685
4686 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4687 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4688 if (testPatternColor.count >= 4) {
4689 for (size_t i = 0; i < 4; i++) {
4690 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4691 testPatternColor.data.i32[i] = testPatternData[i];
4692 changed = true;
4693 }
4694 }
4695 } else if (supportTestPatternDataKey) {
4696 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4697 testPatternData, 4);
4698 changed = true;
4699 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004700 }
4701
4702 return changed;
4703}
4704
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004705status_t Camera3Device::RequestThread::setHalInterface(
4706 sp<HalInterface> newHalInterface) {
4707 if (newHalInterface.get() == nullptr) {
4708 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4709 return DEAD_OBJECT;
4710 }
4711
4712 mInterface = newHalInterface;
4713
4714 return OK;
4715}
4716
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004717/**
4718 * PreparerThread inner class methods
4719 */
4720
4721Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004722 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004723 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004724}
4725
4726Camera3Device::PreparerThread::~PreparerThread() {
4727 Thread::requestExitAndWait();
4728 if (mCurrentStream != nullptr) {
4729 mCurrentStream->cancelPrepare();
4730 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4731 mCurrentStream.clear();
4732 }
4733 clear();
4734}
4735
Ruben Brunkc78ac262015-08-13 17:58:46 -07004736status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004737 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004738 status_t res;
4739
4740 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004741 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004742
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004743 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004744 if (res == OK) {
4745 // No preparation needed, fire listener right off
4746 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004747 if (listener != NULL) {
4748 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004749 }
4750 return OK;
4751 } else if (res != NOT_ENOUGH_DATA) {
4752 return res;
4753 }
4754
4755 // Need to prepare, start up thread if necessary
4756 if (!mActive) {
4757 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4758 // isn't running
4759 Thread::requestExitAndWait();
4760 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4761 if (res != OK) {
4762 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004763 if (listener != NULL) {
4764 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004765 }
4766 return res;
4767 }
4768 mCancelNow = false;
4769 mActive = true;
4770 ALOGV("%s: Preparer stream started", __FUNCTION__);
4771 }
4772
4773 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004774 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004775 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4776
4777 return OK;
4778}
4779
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004780void Camera3Device::PreparerThread::pause() {
4781 ATRACE_CALL();
4782
4783 Mutex::Autolock l(mLock);
4784
4785 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4786 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4787 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4788 int currentMaxCount = mCurrentMaxCount;
4789 mPendingStreams.clear();
4790 mCancelNow = true;
4791 while (mActive) {
4792 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4793 if (res == TIMED_OUT) {
4794 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4795 return;
4796 } else if (res != OK) {
4797 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4798 return;
4799 }
4800 }
4801
4802 //Check whether the prepare thread was able to complete the current
4803 //stream. In case work is still pending emplace it along with the rest
4804 //of the streams in the pending list.
4805 if (currentStream != nullptr) {
4806 if (!mCurrentPrepareComplete) {
4807 pendingStreams.emplace(currentMaxCount, currentStream);
4808 }
4809 }
4810
4811 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4812 for (const auto& it : mPendingStreams) {
4813 it.second->cancelPrepare();
4814 }
4815}
4816
4817status_t Camera3Device::PreparerThread::resume() {
4818 ATRACE_CALL();
4819 status_t res;
4820
4821 Mutex::Autolock l(mLock);
4822 sp<NotificationListener> listener = mListener.promote();
4823
4824 if (mActive) {
4825 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4826 return NO_INIT;
4827 }
4828
4829 auto it = mPendingStreams.begin();
4830 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004831 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004832 if (res == OK) {
4833 if (listener != NULL) {
4834 listener->notifyPrepared(it->second->getId());
4835 }
4836 it = mPendingStreams.erase(it);
4837 } else if (res != NOT_ENOUGH_DATA) {
4838 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4839 res, strerror(-res));
4840 it = mPendingStreams.erase(it);
4841 } else {
4842 it++;
4843 }
4844 }
4845
4846 if (mPendingStreams.empty()) {
4847 return OK;
4848 }
4849
4850 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4851 if (res != OK) {
4852 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4853 __FUNCTION__, res, strerror(-res));
4854 return res;
4855 }
4856 mCancelNow = false;
4857 mActive = true;
4858 ALOGV("%s: Preparer stream started", __FUNCTION__);
4859
4860 return OK;
4861}
4862
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004863status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004864 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004865 Mutex::Autolock l(mLock);
4866
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004867 for (const auto& it : mPendingStreams) {
4868 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004869 }
4870 mPendingStreams.clear();
4871 mCancelNow = true;
4872
4873 return OK;
4874}
4875
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004876void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004877 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004878 Mutex::Autolock l(mLock);
4879 mListener = listener;
4880}
4881
4882bool Camera3Device::PreparerThread::threadLoop() {
4883 status_t res;
4884 {
4885 Mutex::Autolock l(mLock);
4886 if (mCurrentStream == nullptr) {
4887 // End thread if done with work
4888 if (mPendingStreams.empty()) {
4889 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4890 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4891 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4892 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004893 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004894 return false;
4895 }
4896
4897 // Get next stream to prepare
4898 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004899 mCurrentStream = it->second;
4900 mCurrentMaxCount = it->first;
4901 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004902 mPendingStreams.erase(it);
4903 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4904 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4905 } else if (mCancelNow) {
4906 mCurrentStream->cancelPrepare();
4907 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4908 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4909 mCurrentStream.clear();
4910 mCancelNow = false;
4911 return true;
4912 }
4913 }
4914
4915 res = mCurrentStream->prepareNextBuffer();
4916 if (res == NOT_ENOUGH_DATA) return true;
4917 if (res != OK) {
4918 // Something bad happened; try to recover by cancelling prepare and
4919 // signalling listener anyway
4920 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4921 mCurrentStream->getId(), res, strerror(-res));
4922 mCurrentStream->cancelPrepare();
4923 }
4924
4925 // This stream has finished, notify listener
4926 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004927 sp<NotificationListener> listener = mListener.promote();
4928 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004929 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4930 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004931 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004932 }
4933
4934 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4935 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004936 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004937
4938 return true;
4939}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004940
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004941status_t Camera3Device::RequestBufferStateMachine::initialize(
4942 sp<camera3::StatusTracker> statusTracker) {
4943 if (statusTracker == nullptr) {
4944 ALOGE("%s: statusTracker is null", __FUNCTION__);
4945 return BAD_VALUE;
4946 }
4947
4948 std::lock_guard<std::mutex> lock(mLock);
4949 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004950 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004951 return OK;
4952}
4953
4954bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4955 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004956 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004957 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004958 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004959 return true;
4960 }
4961 return false;
4962}
4963
4964void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4965 std::lock_guard<std::mutex> lock(mLock);
4966 if (!mRequestBufferOngoing) {
4967 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4968 return;
4969 }
4970 mRequestBufferOngoing = false;
4971 if (mStatus == RB_STATUS_PENDING_STOP) {
4972 checkSwitchToStopLocked();
4973 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004974 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004975}
4976
4977void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4978 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004979 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004980 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004981 return;
4982}
4983
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004984void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004985 std::lock_guard<std::mutex> lock(mLock);
4986 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004987 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4988 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004989 mInflightMapEmpty = false;
4990 if (mStatus == RB_STATUS_STOPPED) {
4991 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004992 }
4993 return;
4994}
4995
4996void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4997 std::lock_guard<std::mutex> lock(mLock);
4998 mRequestThreadPaused = true;
4999 if (mStatus == RB_STATUS_PENDING_STOP) {
5000 checkSwitchToStopLocked();
5001 }
5002 return;
5003}
5004
5005void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5006 std::lock_guard<std::mutex> lock(mLock);
5007 mInflightMapEmpty = true;
5008 if (mStatus == RB_STATUS_PENDING_STOP) {
5009 checkSwitchToStopLocked();
5010 }
5011 return;
5012}
5013
5014void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5015 std::lock_guard<std::mutex> lock(mLock);
5016 if (!checkSwitchToStopLocked()) {
5017 mStatus = RB_STATUS_PENDING_STOP;
5018 }
5019 return;
5020}
5021
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005022bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5023 std::lock_guard<std::mutex> lock(mLock);
5024 if (mRequestBufferOngoing) {
5025 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5026 __FUNCTION__);
5027 return false;
5028 }
5029 mSwitchedToOffline = true;
5030 mInflightMapEmpty = true;
5031 mRequestThreadPaused = true;
5032 mStatus = RB_STATUS_STOPPED;
5033 return true;
5034}
5035
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005036void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5037 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5038 if (statusTracker != nullptr) {
5039 if (active) {
5040 statusTracker->markComponentActive(mRequestBufferStatusId);
5041 } else {
5042 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5043 }
5044 }
5045}
5046
5047bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5048 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5049 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005050 return true;
5051 }
5052 return false;
5053}
5054
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005055bool Camera3Device::startRequestBuffer() {
5056 return mRequestBufferSM.startRequestBuffer();
5057}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005058
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005059void Camera3Device::endRequestBuffer() {
5060 mRequestBufferSM.endRequestBuffer();
5061}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005062
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005063nsecs_t Camera3Device::getWaitDuration() {
5064 return kBaseGetBufferWait + getExpectedInFlightDuration();
5065}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005067void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5068 mInterface->getInflightBufferKeys(out);
5069}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005070
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005071void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5072 mInterface->getInflightRequestBufferKeys(out);
5073}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005074
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005075std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5076 std::vector<sp<Camera3StreamInterface>> ret;
5077 bool hasInputStream = mInputStream != nullptr;
5078 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5079 if (hasInputStream) {
5080 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005081 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005082 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5083 ret.push_back(mOutputStreams[i]);
5084 }
5085 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5086 ret.push_back(mDeletedStreams[i]);
5087 }
5088 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005089}
5090
Emilian Peevcc0b7952020-01-07 13:54:47 -08005091void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5092 ATRACE_CALL();
5093
5094 if (offlineStreamIds == nullptr) {
5095 return;
5096 }
5097
5098 Mutex::Autolock il(mInterfaceLock);
5099
5100 auto streamIds = mOutputStreams.getStreamIds();
5101 bool hasInputStream = mInputStream != nullptr;
5102 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5103 offlineStreamIds->push_back(mInputStream->getId());
5104 }
5105
5106 for (const auto & streamId : streamIds) {
5107 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5108 // Streams that use the camera buffer manager are currently not supported in
5109 // offline mode
5110 if (stream->getOfflineProcessingSupport() &&
5111 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5112 offlineStreamIds->push_back(streamId);
5113 }
5114 }
5115}
5116
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005117status_t Camera3Device::setRotateAndCropAutoBehavior(
5118 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5119 ATRACE_CALL();
5120 Mutex::Autolock il(mInterfaceLock);
5121 Mutex::Autolock l(mLock);
5122 if (mRequestThread == nullptr) {
5123 return INVALID_OPERATION;
5124 }
5125 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5126}
5127
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005128bool Camera3Device::supportsCameraMute() {
5129 Mutex::Autolock il(mInterfaceLock);
5130 Mutex::Autolock l(mLock);
5131
5132 return mSupportCameraMute;
5133}
5134
5135status_t Camera3Device::setCameraMute(bool enabled) {
5136 ATRACE_CALL();
5137 Mutex::Autolock il(mInterfaceLock);
5138 Mutex::Autolock l(mLock);
5139
5140 if (mRequestThread == nullptr || !mSupportCameraMute) {
5141 return INVALID_OPERATION;
5142 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005143 int32_t muteMode =
5144 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5145 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5146 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5147 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005148}
5149
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005150status_t Camera3Device::injectCamera(const String8& injectedCamId,
5151 sp<CameraProviderManager> manager) {
5152 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5153 ATRACE_CALL();
5154 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005155 // When the camera device is active, injectCamera() and stopInjection() will call
5156 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5157 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5158 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5159 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5160 // waitUntilStateThenRelock().
5161 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005162
5163 status_t res = NO_ERROR;
5164 if (mInjectionMethods->isInjecting()) {
5165 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5166 return OK;
5167 } else {
5168 res = mInjectionMethods->stopInjection();
5169 if (res != OK) {
5170 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5171 __FUNCTION__, res);
5172 return res;
5173 }
5174 }
5175 }
5176
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005177 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005178 if (res != OK) {
5179 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5180 __FUNCTION__, res);
5181 return res;
5182 }
5183
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005184 // When the second display of android is cast to the remote device, and the opened camera is
5185 // also cast to the second display, in this case, because the camera has configured the streams
5186 // at this time, we can directly call injectCamera() to replace the internal camera with
5187 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005188 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5189 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5190
5191 camera3::camera_stream_configuration injectionConfig;
5192 std::vector<uint32_t> injectionBufferSizes;
5193 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5194 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5195 || injectionBufferSizes.size() <= 0) {
5196 ALOGE("Failed to inject camera due to abandoned configuration! "
5197 "mOperatingMode: %d injectionConfig.num_streams: %d "
5198 "injectionBufferSizes.size(): %zu", mOperatingMode,
5199 injectionConfig.num_streams, injectionBufferSizes.size());
5200 return DEAD_OBJECT;
5201 }
5202
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005203 res = mInjectionMethods->injectCamera(
5204 injectionConfig, injectionBufferSizes);
5205 if (res != OK) {
5206 ALOGE("Can't finish inject camera process!");
5207 return res;
5208 }
5209 }
5210
5211 return OK;
5212}
5213
5214status_t Camera3Device::stopInjection() {
5215 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5216 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005217 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005218 return mInjectionMethods->stopInjection();
5219}
5220
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005221}; // namespace android