blob: 251522fa20d2f6d01ef8695cd791aa0e6b8d5a4d [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) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001515 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001517 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001518 }
1519
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520 return res;
1521}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001523// Resume after internalPauseAndWaitLocked
1524status_t Camera3Device::internalResumeLocked() {
1525 status_t res;
1526
1527 mRequestThread->setPaused(false);
1528
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001529 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1530 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001531 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1532 if (res != OK) {
1533 SET_ERR_L("Can't transition to active in %f seconds!",
1534 kActiveTimeout/1e9);
1535 }
1536 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538}
1539
Ruben Brunk183f0562015-08-12 12:55:02 -07001540status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001541 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001542
1543 size_t startIndex = 0;
1544 if (mStatusWaiters == 0) {
1545 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1546 // this status list
1547 mRecentStatusUpdates.clear();
1548 } else {
1549 // If other threads are waiting on updates to this status list, set the position of the
1550 // first element that this list will check rather than clearing the list.
1551 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001552 }
1553
Ruben Brunk183f0562015-08-12 12:55:02 -07001554 mStatusWaiters++;
1555
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001556 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001557 if (!active && mUseHalBufManager) {
1558 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001559 if (mStatus == STATUS_ACTIVE) {
1560 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001561 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001562 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001563 mRequestBufferSM.onWaitUntilIdle();
1564 }
1565
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001567 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001569 if (active == (mStatus == STATUS_ACTIVE)) {
1570 // Desired state is current
1571 break;
1572 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001573
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001574 nsecs_t timeElapsed = systemTime() - startTime;
1575 nsecs_t timeToWait = timeout - timeElapsed;
1576 if (timeToWait <= 0) {
1577 // Thread woke up spuriously but has timed out since.
1578 // Force out of loop with TIMED_OUT result.
1579 res = TIMED_OUT;
1580 break;
1581 }
1582 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 if (res != OK) break;
1584
Ruben Brunk183f0562015-08-12 12:55:02 -07001585 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1586 // transitions.
1587 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1588 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1589 __FUNCTION__);
1590
1591 // Encountered desired state since we began waiting
1592 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1594 stateSeen = true;
1595 break;
1596 }
1597 }
1598 } while (!stateSeen);
1599
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001600 if (signalPipelineDrain) {
1601 mRequestThread->resetPipelineDrain();
1602 }
1603
Ruben Brunk183f0562015-08-12 12:55:02 -07001604 mStatusWaiters--;
1605
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001606 return res;
1607}
1608
1609
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001610status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001612 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001614 if (listener != NULL && mListener != NULL) {
1615 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1616 }
1617 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001618 mRequestThread->setNotificationListener(listener);
1619 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001620
1621 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001622}
1623
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001624bool Camera3Device::willNotify3A() {
1625 return false;
1626}
1627
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001628status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001629 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001630 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001631
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001632 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001633 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1634 if (st == std::cv_status::timeout) {
1635 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001636 }
1637 }
1638 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001639}
1640
Jianing Weicb0652e2014-03-12 18:29:36 -07001641status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001642 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001643 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001644
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001645 if (mResultQueue.empty()) {
1646 return NOT_ENOUGH_DATA;
1647 }
1648
Jianing Weicb0652e2014-03-12 18:29:36 -07001649 if (frame == NULL) {
1650 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1651 return BAD_VALUE;
1652 }
1653
1654 CaptureResult &result = *(mResultQueue.begin());
1655 frame->mResultExtras = result.mResultExtras;
1656 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001657 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001658 mResultQueue.erase(mResultQueue.begin());
1659
1660 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001661}
1662
1663status_t Camera3Device::triggerAutofocus(uint32_t id) {
1664 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001665 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001666
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001667 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1668 // Mix-in this trigger into the next request and only the next request.
1669 RequestTrigger trigger[] = {
1670 {
1671 ANDROID_CONTROL_AF_TRIGGER,
1672 ANDROID_CONTROL_AF_TRIGGER_START
1673 },
1674 {
1675 ANDROID_CONTROL_AF_TRIGGER_ID,
1676 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001677 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001678 };
1679
1680 return mRequestThread->queueTrigger(trigger,
1681 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001682}
1683
1684status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1685 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001686 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001687
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001688 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1689 // Mix-in this trigger into the next request and only the next request.
1690 RequestTrigger trigger[] = {
1691 {
1692 ANDROID_CONTROL_AF_TRIGGER,
1693 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1694 },
1695 {
1696 ANDROID_CONTROL_AF_TRIGGER_ID,
1697 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001698 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001699 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001701 return mRequestThread->queueTrigger(trigger,
1702 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001703}
1704
1705status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1706 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001707 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001708
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001709 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1710 // Mix-in this trigger into the next request and only the next request.
1711 RequestTrigger trigger[] = {
1712 {
1713 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1714 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1715 },
1716 {
1717 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1718 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001719 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001720 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001722 return mRequestThread->queueTrigger(trigger,
1723 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001724}
1725
Jianing Weicb0652e2014-03-12 18:29:36 -07001726status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001727 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001728 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001730
Zhijun He7ef20392014-04-21 16:04:17 -07001731 {
1732 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001733
1734 // b/116514106 "disconnect()" can get called twice for the same device. The
1735 // camera device will not be initialized during the second run.
1736 if (mStatus == STATUS_UNINITIALIZED) {
1737 return OK;
1738 }
1739
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001740 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001741
1742 // Stop session and stream counter
1743 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001744 }
1745
Ravneetdbd5b242022-03-02 07:22:46 +00001746 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1747 uint64_t maxExpectedDuration = (getExpectedInFlightDuration() + kBaseGetBufferWait) / 1e6;
1748 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1749 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1750
1751 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001752}
1753
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001754status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001755 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1756}
1757
1758status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001759 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001760 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001761 Mutex::Autolock il(mInterfaceLock);
1762 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001763
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001764 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1765 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001766 CLOGE("Stream %d does not exist", streamId);
1767 return BAD_VALUE;
1768 }
1769
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001770 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001771 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001772 return BAD_VALUE;
1773 }
1774
1775 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001776 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001777 return BAD_VALUE;
1778 }
1779
Ruben Brunkc78ac262015-08-13 17:58:46 -07001780 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001781}
1782
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001783status_t Camera3Device::tearDown(int streamId) {
1784 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001785 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001786 Mutex::Autolock il(mInterfaceLock);
1787 Mutex::Autolock l(mLock);
1788
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001789 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1790 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001791 CLOGE("Stream %d does not exist", streamId);
1792 return BAD_VALUE;
1793 }
1794
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001795 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1796 CLOGE("Stream %d is a target of a in-progress request", streamId);
1797 return BAD_VALUE;
1798 }
1799
1800 return stream->tearDown();
1801}
1802
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001803status_t Camera3Device::addBufferListenerForStream(int streamId,
1804 wp<Camera3StreamBufferListener> listener) {
1805 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001806 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001807 Mutex::Autolock il(mInterfaceLock);
1808 Mutex::Autolock l(mLock);
1809
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001810 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1811 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001812 CLOGE("Stream %d does not exist", streamId);
1813 return BAD_VALUE;
1814 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001815 stream->addBufferListener(listener);
1816
1817 return OK;
1818}
1819
Austin Borger4a870a32022-02-25 01:48:41 +00001820float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1821 camera_metadata_entry minDurations =
1822 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1823 for (size_t i = 0; i < minDurations.count; i += 4) {
1824 if (minDurations.data.i64[i] == stream->getFormat()
1825 && minDurations.data.i64[i+1] == stream->getWidth()
1826 && minDurations.data.i64[i+2] == stream->getHeight()) {
1827 int64_t minFrameDuration = minDurations.data.i64[i+3];
1828 return 1e9f / minFrameDuration;
1829 }
1830 }
1831 return 0.0f;
1832}
1833
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001834/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001835 * Methods called by subclasses
1836 */
1837
1838void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001839 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001840 std::vector<int> streamIds;
1841 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001842 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001843
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001844 {
1845 // Need mLock to safely update state and synchronize to current
1846 // state of methods in flight.
1847 Mutex::Autolock l(mLock);
1848 // We can get various system-idle notices from the status tracker
1849 // while starting up. Only care about them if we've actually sent
1850 // in some requests recently.
1851 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1852 return;
1853 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001854 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1855 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001856 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001857
1858 // Skip notifying listener if we're doing some user-transparent
1859 // state changes
1860 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001861
Austin Borger4a870a32022-02-25 01:48:41 +00001862 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1863 auto stream = mOutputStreams[i];
1864 if (stream.get() == nullptr) continue;
1865
1866 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1867 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1868
1869 // Populate stream statistics in case of Idle
1870 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001871 streamIds.push_back(stream->getId());
1872 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1873 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001874 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001875 if (camera3Stream != nullptr) {
1876 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001877 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001878 }
1879 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001880 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001881 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001882 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001883 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001884 }
1885 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001887
1888 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001889 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001890 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001891 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001892 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001893 status_t res = OK;
1894 if (listener != nullptr) {
1895 if (idle) {
1896 // Get session stats from the builder, and notify the listener.
1897 int64_t requestCount, resultErrorCount;
1898 bool deviceError;
1899 std::map<int, StreamStats> streamStatsMap;
1900 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1901 &deviceError, &streamStatsMap);
1902 for (size_t i = 0; i < streamIds.size(); i++) {
1903 int streamId = streamIds[i];
1904 auto stats = streamStatsMap.find(streamId);
1905 if (stats != streamStatsMap.end()) {
1906 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1907 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1908 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1909 streamStats[i].mHistogramType =
1910 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1911 streamStats[i].mHistogramBins.assign(
1912 stats->second.mCaptureLatencyBins.begin(),
1913 stats->second.mCaptureLatencyBins.end());
1914 streamStats[i].mHistogramCounts.assign(
1915 stats->second.mCaptureLatencyHistogram.begin(),
1916 stats->second.mCaptureLatencyHistogram.end());
1917 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001918 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001919 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1920 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001921 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001922 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001923 }
1924 if (res != OK) {
1925 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1926 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001927 }
1928}
1929
Shuzhen Wang758c2152017-01-10 18:26:18 -08001930status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001931 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001932 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001933 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1934 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001935
1936 if (surfaceIds == nullptr) {
1937 return BAD_VALUE;
1938 }
1939
Zhijun He5d677d12016-05-29 16:52:39 -07001940 Mutex::Autolock il(mInterfaceLock);
1941 Mutex::Autolock l(mLock);
1942
Shuzhen Wang758c2152017-01-10 18:26:18 -08001943 if (consumers.size() == 0) {
1944 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001945 return BAD_VALUE;
1946 }
1947
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001948 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1949 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001950 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001951 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001952 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001953
1954 // isConsumerConfigurationDeferred will be off after setConsumers
1955 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001956 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001957 if (res != OK) {
1958 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1959 return res;
1960 }
1961
Emilian Peev40ead602017-09-26 15:46:36 +01001962 for (auto &consumer : consumers) {
1963 int id = stream->getSurfaceId(consumer);
1964 if (id < 0) {
1965 CLOGE("Invalid surface id!");
1966 return BAD_VALUE;
1967 }
1968 surfaceIds->push_back(id);
1969 }
1970
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001971 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001972 if (!stream->isConfiguring()) {
1973 CLOGE("Stream %d was already fully configured.", streamId);
1974 return INVALID_OPERATION;
1975 }
Zhijun He5d677d12016-05-29 16:52:39 -07001976
Shuzhen Wang0129d522016-10-30 22:43:41 -07001977 res = stream->finishConfiguration();
1978 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001979 // If finishConfiguration fails due to abandoned surface, do not set
1980 // device to error state.
1981 bool isSurfaceAbandoned =
1982 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1983 if (!isSurfaceAbandoned) {
1984 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1985 stream->getId(), strerror(-res), res);
1986 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001987 return res;
1988 }
Zhijun He5d677d12016-05-29 16:52:39 -07001989 }
1990
1991 return OK;
1992}
1993
Emilian Peev40ead602017-09-26 15:46:36 +01001994status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1995 const std::vector<OutputStreamInfo> &outputInfo,
1996 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1997 Mutex::Autolock il(mInterfaceLock);
1998 Mutex::Autolock l(mLock);
1999
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002000 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2001 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002002 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002003 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002004 }
2005
2006 for (const auto &it : removedSurfaceIds) {
2007 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2008 CLOGE("Shared surface still part of a pending request!");
2009 return -EBUSY;
2010 }
2011 }
2012
Emilian Peev40ead602017-09-26 15:46:36 +01002013 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2014 if (res != OK) {
2015 CLOGE("Stream %d failed to update stream (error %d %s) ",
2016 streamId, res, strerror(-res));
2017 if (res == UNKNOWN_ERROR) {
2018 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2019 __FUNCTION__);
2020 }
2021 return res;
2022 }
2023
2024 return res;
2025}
2026
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002027status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2028 Mutex::Autolock il(mInterfaceLock);
2029 Mutex::Autolock l(mLock);
2030
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002031 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2032 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002033 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2034 return BAD_VALUE;
2035 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002036
2037 if (dropping) {
2038 mSessionStatsBuilder.stopCounter(streamId);
2039 } else {
2040 mSessionStatsBuilder.startCounter(streamId);
2041 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002042 return stream->dropBuffers(dropping);
2043}
2044
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002045/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002046 * Camera3Device private methods
2047 */
2048
2049sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002050 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002051 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002053 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002054 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002055
2056 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002057 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058 if (inputStreams.count > 0) {
2059 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002060 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002061 CLOGE("Request references unknown input stream %d",
2062 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063 return NULL;
2064 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002065
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002067 SET_ERR_L("%s: input stream %d is not configured!",
2068 __FUNCTION__, mInputStream->getId());
2069 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002071 // Check if stream prepare is blocking requests.
2072 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002073 CLOGE("Request references an input stream that's being prepared!");
2074 return NULL;
2075 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076
2077 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002078 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002079 }
2080
2081 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002082 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002084 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 return NULL;
2086 }
2087
2088 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002089 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2090 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002091 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002092 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093 return NULL;
2094 }
Zhijun He5d677d12016-05-29 16:52:39 -07002095 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002096 auto iter = surfaceMap.find(streams.data.i32[i]);
2097 if (iter != surfaceMap.end()) {
2098 const std::vector<size_t>& surfaces = iter->second;
2099 for (const auto& surface : surfaces) {
2100 if (stream->isConsumerConfigurationDeferred(surface)) {
2101 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2102 "due to deferred consumer", stream->getId(), surface);
2103 return NULL;
2104 }
2105 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002106 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002107 }
2108
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002109 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002110 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2111 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002113 // Check if stream prepare is blocking requests.
2114 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002115 CLOGE("Request references an output stream that's being prepared!");
2116 return NULL;
2117 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118
2119 newRequest->mOutputStreams.push(stream);
2120 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002121 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002122 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002124 auto rotateAndCropEntry =
2125 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2126 if (rotateAndCropEntry.count > 0 &&
2127 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2128 newRequest->mRotateAndCropAuto = true;
2129 } else {
2130 newRequest->mRotateAndCropAuto = false;
2131 }
2132
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002133 auto zoomRatioEntry =
2134 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2135 if (zoomRatioEntry.count > 0 &&
2136 zoomRatioEntry.data.f[0] == 1.0f) {
2137 newRequest->mZoomRatioIs1x = true;
2138 } else {
2139 newRequest->mZoomRatioIs1x = false;
2140 }
2141
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002142 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002143 for (auto& settings : newRequest->mSettingsList) {
2144 auto testPatternModeEntry =
2145 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2146 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2147 testPatternModeEntry.data.i32[0] :
2148 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002149
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002150 auto testPatternDataEntry =
2151 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2152 if (testPatternDataEntry.count >= 4) {
2153 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2154 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2155 } else {
2156 settings.mOriginalTestPatternData[0] = 0;
2157 settings.mOriginalTestPatternData[1] = 0;
2158 settings.mOriginalTestPatternData[2] = 0;
2159 settings.mOriginalTestPatternData[3] = 0;
2160 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002161 }
2162 }
2163
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002164 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002165}
2166
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002167void Camera3Device::cancelStreamsConfigurationLocked() {
2168 int res = OK;
2169 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2170 res = mInputStream->cancelConfiguration();
2171 if (res != OK) {
2172 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2173 mInputStream->getId(), strerror(-res), res);
2174 }
2175 }
2176
2177 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002178 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002179 if (outputStream->isConfiguring()) {
2180 res = outputStream->cancelConfiguration();
2181 if (res != OK) {
2182 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2183 outputStream->getId(), strerror(-res), res);
2184 }
2185 }
2186 }
2187
2188 // Return state to that at start of call, so that future configures
2189 // properly clean things up
2190 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2191 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002192
2193 res = mPreparerThread->resume();
2194 if (res != OK) {
2195 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2196 }
2197}
2198
Emilian Peev0d0191e2020-04-21 17:01:18 -07002199bool Camera3Device::checkAbandonedStreamsLocked() {
2200 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2201 return true;
2202 }
2203
2204 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2205 auto stream = mOutputStreams[i];
2206 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2207 return true;
2208 }
2209 }
2210
2211 return false;
2212}
2213
Emilian Peev3bead5f2020-05-28 17:29:08 -07002214bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002215 ATRACE_CALL();
2216 bool ret = false;
2217
Shuzhen Wang316781a2020-08-18 18:11:01 -07002218 nsecs_t startTime = systemTime();
2219
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002220 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002221 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2222
2223 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002224 if (checkAbandonedStreamsLocked()) {
2225 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2226 __FUNCTION__);
2227 return true;
2228 }
2229
Emilian Peev3bead5f2020-05-28 17:29:08 -07002230 status_t rc = NO_ERROR;
2231 bool markClientActive = false;
2232 if (mStatus == STATUS_ACTIVE) {
2233 markClientActive = true;
2234 mPauseStateNotify = true;
2235 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2236
2237 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2238 }
2239
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002240 if (rc == NO_ERROR) {
2241 mNeedConfig = true;
2242 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2243 if (rc == NO_ERROR) {
2244 ret = true;
2245 mPauseStateNotify = false;
2246 //Moving to active state while holding 'mLock' is important.
2247 //There could be pending calls to 'create-/deleteStream' which
2248 //will trigger another stream configuration while the already
2249 //present streams end up with outstanding buffers that will
2250 //not get drained.
2251 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002252 } else if (rc == DEAD_OBJECT) {
2253 // DEAD_OBJECT can be returned if either the consumer surface is
2254 // abandoned, or the HAL has died.
2255 // - If the HAL has died, configureStreamsLocked call will set
2256 // device to error state,
2257 // - If surface is abandoned, we should not set device to error
2258 // state.
2259 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002260 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002261 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002262 }
2263 } else {
2264 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2265 }
2266
Austin Borger74fca042022-05-23 12:41:21 -07002267 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002268 ns2ms(systemTime() - startTime));
2269
Emilian Peev3bead5f2020-05-28 17:29:08 -07002270 if (markClientActive) {
2271 mStatusTracker->markComponentActive(clientStatusId);
2272 }
2273
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002274 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002275}
2276
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002277status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002278 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002279 ATRACE_CALL();
2280 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002282 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002283 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002284 return INVALID_OPERATION;
2285 }
2286
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002287 if (operatingMode < 0) {
2288 CLOGE("Invalid operating mode: %d", operatingMode);
2289 return BAD_VALUE;
2290 }
2291
2292 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002293 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002294
2295 if (mOperatingMode != operatingMode) {
2296 mNeedConfig = true;
2297 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2298 mOperatingMode = operatingMode;
2299 }
2300
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002301 // Reset min expected duration when session is reconfigured.
2302 mMinExpectedDuration = 0;
2303
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002304 // In case called from configureStreams, abort queued input buffers not belonging to
2305 // any pending requests.
2306 if (mInputStream != NULL && notifyRequestThread) {
2307 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002308 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002309 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002310 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002311 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002312 if (res != OK) {
2313 // Exhausted acquiring all input buffers.
2314 break;
2315 }
2316
Emilian Peevf4816702020-04-03 15:44:51 -07002317 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002318 res = mInputStream->returnInputBuffer(inputBuffer);
2319 if (res != OK) {
2320 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2321 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2322 }
2323 }
2324 }
2325
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002326 if (!mNeedConfig) {
2327 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2328 return OK;
2329 }
2330
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002331 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002332 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002333 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2334 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002335 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002336 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002337 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002338 }
2339
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002341 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002342
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002343 mPreparerThread->pause();
2344
Emilian Peevf4816702020-04-03 15:44:51 -07002345 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002346 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002347 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002348 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349
Emilian Peevf4816702020-04-03 15:44:51 -07002350 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002351 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002352 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002353
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002354
2355 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002356 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002357 inputStream = mInputStream->startConfiguration();
2358 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002359 CLOGE("Can't start input stream configuration");
2360 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002361 return INVALID_OPERATION;
2362 }
2363 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002364
2365 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002366 }
2367
Shuzhen Wang99080502021-03-07 21:08:20 -08002368 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002369 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002370 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002371
2372 // Don't configure bidi streams twice, nor add them twice to the list
2373 if (mOutputStreams[i].get() ==
2374 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2375
2376 config.num_streams--;
2377 continue;
2378 }
2379
Emilian Peevf4816702020-04-03 15:44:51 -07002380 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002381 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002383 CLOGE("Can't start output stream configuration");
2384 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002385 return INVALID_OPERATION;
2386 }
2387 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002388
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002389 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002390 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2391 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002392 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2393 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002394 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2395 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002396 } else if (outputStream->data_space ==
2397 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2398 bufferSizes[k] = outputStream->width * outputStream->height;
2399 } else {
2400 ALOGW("%s: Blob dataSpace %d not supported",
2401 __FUNCTION__, outputStream->data_space);
2402 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002403 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002404
2405 if (mOutputStreams[i]->isMultiResolution()) {
2406 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2407 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2408 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2409 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002410
2411 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2412 composerSurfacePresent = true;
2413 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002414 }
2415
2416 config.streams = streams.editArray();
2417
2418 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002419 // max_buffers, usage, and priv fields, as well as data_space and format
2420 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002421
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002422 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002423 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002424 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002425
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002426 if (res == BAD_VALUE) {
2427 // HAL rejected this set of streams as unsupported, clean up config
2428 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002429 CLOGE("Set of requested inputs/outputs not supported by HAL");
2430 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002431 return BAD_VALUE;
2432 } else if (res != OK) {
2433 // Some other kind of error from configure_streams - this is not
2434 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002435 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2436 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002437 return res;
2438 }
2439
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002440 // Finish all stream configuration immediately.
2441 // TODO: Try to relax this later back to lazy completion, which should be
2442 // faster
2443
Igor Murashkin073f8572013-05-02 14:59:28 -07002444 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002445 bool streamReConfigured = false;
2446 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002447 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002448 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002449 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002450 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002451 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2452 return DEAD_OBJECT;
2453 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002454 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002455 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002456 if (streamReConfigured) {
2457 mInterface->onStreamReConfigured(mInputStream->getId());
2458 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002459 }
2460
2461 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002462 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002463 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002464 bool streamReConfigured = false;
2465 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002466 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002467 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002468 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002469 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002470 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2471 return DEAD_OBJECT;
2472 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002473 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002474 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002475 if (streamReConfigured) {
2476 mInterface->onStreamReConfigured(outputStream->getId());
2477 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002478 }
2479 }
2480
Emilian Peeve23f1d92021-09-20 14:56:01 -07002481 mRequestThread->setComposerSurface(composerSurfacePresent);
2482
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002483 // Request thread needs to know to avoid using repeat-last-settings protocol
2484 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002485 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002486 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2487 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002488 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002489
Zhijun He90f7c372016-08-16 16:19:43 -07002490 char value[PROPERTY_VALUE_MAX];
2491 property_get("camera.fifo.disable", value, "0");
2492 int32_t disableFifo = atoi(value);
2493 if (disableFifo != 1) {
2494 // Boost priority of request thread to SCHED_FIFO.
2495 pid_t requestThreadTid = mRequestThread->getTid();
2496 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002497 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002498 if (res != OK) {
2499 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2500 strerror(-res), res);
2501 } else {
2502 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2503 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002504 }
2505
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002506 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002507 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2508 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2509 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2510 sessionParams.unlock(newSessionParams);
2511 mSessionParams.unlock(currentSessionParams);
2512 if (updateSessionParams) {
2513 mSessionParams = sessionParams;
2514 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002515
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002516 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002517
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002518 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002519 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002520
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002521 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002522
Zhijun He0a210512014-07-24 13:45:15 -07002523 // tear down the deleted streams after configure streams.
2524 mDeletedStreams.clear();
2525
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002526 auto rc = mPreparerThread->resume();
2527 if (rc != OK) {
2528 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2529 return rc;
2530 }
2531
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002532 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002533 mRequestBufferSM.onStreamsConfigured();
2534 }
2535
Cliff Wu3b268182021-07-06 15:44:43 +08002536 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002537 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002538 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002539 // configure the injection streams.
2540 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002541 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002542 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2543 res = mInjectionMethods->injectCamera(config, bufferSizes);
2544 if (res != OK) {
2545 ALOGE("Can't finish inject camera process!");
2546 return res;
2547 }
Cliff Wu3b268182021-07-06 15:44:43 +08002548 } else {
2549 // First run configureStreamsLocked() and then call injectCamera() case:
2550 // If the stream configuration has been completed and camera deive is active, but the
2551 // injection camera has not been injected yet, we need to store the stream configuration of
2552 // the internal camera (because the stream configuration of the injection camera is based
2553 // on the internal camera). When injecting occurs later, this configuration can be used by
2554 // the injection camera.
2555 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2556 " injection camera has not been injected yet.", __FUNCTION__);
2557 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002558 }
2559
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002560 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002561}
2562
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002563status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002564 ATRACE_CALL();
2565 status_t res;
2566
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002567 if (mFakeStreamId != NO_STREAM) {
2568 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002569 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002570 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002571 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002572 return INVALID_OPERATION;
2573 }
2574
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002575 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002576
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002577 sp<Camera3OutputStreamInterface> fakeStream =
2578 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002579
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002580 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002581 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002582 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002583 return res;
2584 }
2585
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002586 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002587 mNextStreamId++;
2588
2589 return OK;
2590}
2591
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002592status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002593 ATRACE_CALL();
2594 status_t res;
2595
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002596 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002597 if (mOutputStreams.size() == 1) return OK;
2598
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002599 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002600
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002601 // Ok, have a fake stream and there's at least one other output stream,
2602 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002603
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002604 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002605 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002606 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002607 return INVALID_OPERATION;
2608 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002609 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002610
2611 // Free up the stream endpoint so that it can be used by some other stream
2612 res = deletedStream->disconnect();
2613 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002614 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002615 // fall through since we want to still list the stream as deleted.
2616 }
2617 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002618 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002619
2620 return res;
2621}
2622
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002623void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002624 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002625 Mutex::Autolock l(mLock);
2626 va_list args;
2627 va_start(args, fmt);
2628
2629 setErrorStateLockedV(fmt, args);
2630
2631 va_end(args);
2632}
2633
2634void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002635 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002636 Mutex::Autolock l(mLock);
2637 setErrorStateLockedV(fmt, args);
2638}
2639
2640void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2641 va_list args;
2642 va_start(args, fmt);
2643
2644 setErrorStateLockedV(fmt, args);
2645
2646 va_end(args);
2647}
2648
2649void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002650 // Print out all error messages to log
2651 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002652 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002653
2654 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002655 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002656
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002657 mErrorCause = errorCause;
2658
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002659 if (mRequestThread != nullptr) {
2660 mRequestThread->setPaused(true);
2661 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002662 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002663
2664 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002665 sp<NotificationListener> listener = mListener.promote();
2666 if (listener != NULL) {
2667 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002668 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002669 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002670 }
2671
2672 // Save stack trace. View by dumping it later.
2673 CameraTraces::saveTrace();
2674 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002675}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676
2677/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002678 * In-flight request management
2679 */
2680
Jianing Weicb0652e2014-03-12 18:29:36 -07002681status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002682 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002683 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08002684 const std::set<std::set<String8>>& physicalCameraIds,
2685 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2686 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002687 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002688 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002689 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002690
2691 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002692 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002693 hasAppCallback, minExpectedDuration, maxExpectedDuration, physicalCameraIds,
2694 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2695 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002696 if (res < 0) return res;
2697
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002698 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002699 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2700 // avoid a deadlock during reprocess requests.
2701 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002702 if (mStatusTracker != nullptr) {
2703 mStatusTracker->markComponentActive(mInFlightStatusId);
2704 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002705 }
2706
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002707 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002708 return OK;
2709}
2710
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002711void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002712 // Indicate idle inFlightMap to the status tracker
2713 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002714 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002715 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2716 // avoid a deadlock during reprocess requests.
2717 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002718 if (mStatusTracker != nullptr) {
2719 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2720 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002721 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002722 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002723}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002724
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002725void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002726 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002727 // something has likely gone wrong. This might still be legit only if application send in
2728 // a long burst of long exposure requests.
2729 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2730 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2731 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2732 mInFlightMap.size(), mExpectedInflightDuration);
2733 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2734 kInFlightWarnLimitHighSpeed) {
2735 CLOGW("In-flight list too large for high speed configuration: %zu,"
2736 "total inflight duration %" PRIu64,
2737 mInFlightMap.size(), mExpectedInflightDuration);
2738 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002739 }
2740}
2741
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002742void Camera3Device::onInflightMapFlushedLocked() {
2743 mExpectedInflightDuration = 0;
2744}
2745
2746void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002747 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002748 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2749 mInFlightMap.removeItemsAt(idx, 1);
2750
2751 onInflightEntryRemovedLocked(duration);
2752}
2753
2754
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002755void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002756 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002757 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002758 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002759 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002760 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002761 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002762
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002763 FlushInflightReqStates states {
2764 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002765 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002766
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002767 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002768}
2769
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002770CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002771 ALOGV("%s", __FUNCTION__);
2772
Igor Murashkin1e479c02013-09-06 16:55:14 -07002773 CameraMetadata retVal;
2774
2775 if (mRequestThread != NULL) {
2776 retVal = mRequestThread->getLatestRequest();
2777 }
2778
Igor Murashkin1e479c02013-09-06 16:55:14 -07002779 return retVal;
2780}
2781
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002782void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002783 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002784 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002785 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2786 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002787
2788 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002789 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002790}
2791
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002792void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002793 std::vector<native_handle_t*> *handles, bool closeFd) {
2794 if (handles == nullptr) {
2795 return;
2796 }
2797 if (closeFd) {
2798 for (auto& handle : *handles) {
2799 native_handle_close(handle);
2800 }
2801 }
2802 for (auto& handle : *handles) {
2803 native_handle_delete(handle);
2804 }
2805 handles->clear();
2806 return;
2807}
2808
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002809/**
2810 * HalInterface inner class methods
2811 */
2812
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002813void Camera3Device::HalInterface::getInflightBufferKeys(
2814 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002815 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002816 return;
2817}
2818
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002819void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2820 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002821 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002822 return;
2823}
2824
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002825bool Camera3Device::HalInterface::verifyBufferIds(
2826 int32_t streamId, std::vector<uint64_t>& bufIds) {
2827 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002828}
2829
2830status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002831 int32_t frameNumber, int32_t streamId,
2832 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002833 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002834}
2835
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002836status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002837 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002838 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002839}
2840
2841// Find and pop a buffer_handle_t based on bufferId
2842status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002843 uint64_t bufferId,
2844 /*out*/ buffer_handle_t** buffer,
2845 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002846 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002847}
2848
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002849std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2850 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002851 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002852}
2853
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002854uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2855 const native_handle_t* handle) {
2856 return mBufferRecords.removeOneBufferCache(streamId, handle);
2857}
2858
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002859void Camera3Device::HalInterface::onBufferFreed(
2860 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002861 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2862 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2863 if (bufferId != BUFFER_ID_NO_BUFFER) {
2864 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002865 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002866}
2867
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002868void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002869 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2870 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2871 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002872 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2873 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002874}
2875
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002876/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877 * RequestThread inner class methods
2878 */
2879
2880Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002881 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002882 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002883 bool useHalBufManager,
2884 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002885 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002886 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002887 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002888 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002889 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002890 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002891 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002892 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002893 mReconfigured(false),
2894 mDoPause(false),
2895 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002896 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002897 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002898 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002899 mCurrentAfTriggerId(0),
2900 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002901 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002902 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002903 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002904 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002905 mRepeatingLastFrameNumber(
2906 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002907 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002908 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002909 mRequestLatency(kRequestLatencyBinSize),
2910 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002911 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002912 mUseHalBufManager(useHalBufManager),
2913 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002914 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002915}
2916
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002917Camera3Device::RequestThread::~RequestThread() {}
2918
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002919void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002920 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002921 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002922 Mutex::Autolock l(mRequestLock);
2923 mListener = listener;
2924}
2925
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002926void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002927 const CameraMetadata& sessionParams,
2928 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002929 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002930 Mutex::Autolock l(mRequestLock);
2931 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002932 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002933 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002934 // Prepare video stream for high speed recording.
2935 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002936 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002937}
2938
Jianing Wei90e59c92014-03-12 18:29:36 -07002939status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002940 List<sp<CaptureRequest> > &requests,
2941 /*out*/
2942 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002943 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002944 Mutex::Autolock l(mRequestLock);
2945 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2946 ++it) {
2947 mRequestQueue.push_back(*it);
2948 }
2949
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002950 if (lastFrameNumber != NULL) {
2951 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2952 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2953 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2954 *lastFrameNumber);
2955 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002956
Jianing Wei90e59c92014-03-12 18:29:36 -07002957 unpauseForNewRequests();
2958
2959 return OK;
2960}
2961
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002962
2963status_t Camera3Device::RequestThread::queueTrigger(
2964 RequestTrigger trigger[],
2965 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002966 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002967 Mutex::Autolock l(mTriggerMutex);
2968 status_t ret;
2969
2970 for (size_t i = 0; i < count; ++i) {
2971 ret = queueTriggerLocked(trigger[i]);
2972
2973 if (ret != OK) {
2974 return ret;
2975 }
2976 }
2977
2978 return OK;
2979}
2980
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002981const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2982 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002983 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002984 if (d != nullptr) return d->mId;
2985 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002986}
2987
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002988status_t Camera3Device::RequestThread::queueTriggerLocked(
2989 RequestTrigger trigger) {
2990
2991 uint32_t tag = trigger.metadataTag;
2992 ssize_t index = mTriggerMap.indexOfKey(tag);
2993
2994 switch (trigger.getTagType()) {
2995 case TYPE_BYTE:
2996 // fall-through
2997 case TYPE_INT32:
2998 break;
2999 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003000 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3001 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003002 return INVALID_OPERATION;
3003 }
3004
3005 /**
3006 * Collect only the latest trigger, since we only have 1 field
3007 * in the request settings per trigger tag, and can't send more than 1
3008 * trigger per request.
3009 */
3010 if (index != NAME_NOT_FOUND) {
3011 mTriggerMap.editValueAt(index) = trigger;
3012 } else {
3013 mTriggerMap.add(tag, trigger);
3014 }
3015
3016 return OK;
3017}
3018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003019status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003020 const RequestList &requests,
3021 /*out*/
3022 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003023 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003024 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003025 if (lastFrameNumber != NULL) {
3026 *lastFrameNumber = mRepeatingLastFrameNumber;
3027 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003028 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003029 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3031 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003032
3033 unpauseForNewRequests();
3034
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003035 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003036 return OK;
3037}
3038
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003039bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003040 if (mRepeatingRequests.empty()) {
3041 return false;
3042 }
3043 int32_t requestId = requestIn->mResultExtras.requestId;
3044 const RequestList &repeatRequests = mRepeatingRequests;
3045 // All repeating requests are guaranteed to have same id so only check first quest
3046 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3047 return (firstRequest->mResultExtras.requestId == requestId);
3048}
3049
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003050status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003051 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003052 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003053 return clearRepeatingRequestsLocked(lastFrameNumber);
3054
3055}
3056
3057status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003058 std::vector<int32_t> streamIds;
3059 for (const auto& request : mRepeatingRequests) {
3060 for (const auto& stream : request->mOutputStreams) {
3061 streamIds.push_back(stream->getId());
3062 }
3063 }
3064
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003065 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003066 if (lastFrameNumber != NULL) {
3067 *lastFrameNumber = mRepeatingLastFrameNumber;
3068 }
Emilian Peev2295df72021-11-12 18:14:10 -08003069
3070 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3071
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003072 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003073 return OK;
3074}
3075
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003076status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003077 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003078 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003079 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003080 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003081
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003082 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003083
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003084 // Send errors for all requests pending in the request queue, including
3085 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003086 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003087 if (listener != NULL) {
3088 for (RequestList::iterator it = mRequestQueue.begin();
3089 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003090 // Abort the input buffers for reprocess requests.
3091 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003092 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003093 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003094 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003095 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003096 if (res != OK) {
3097 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3098 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3099 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003100 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003101 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3102 if (res != OK) {
3103 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3104 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3105 }
3106 }
3107 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003108 // Set the frame number this request would have had, if it
3109 // had been submitted; this frame number will not be reused.
3110 // The requestId and burstId fields were set when the request was
3111 // submitted originally (in convertMetadataListToRequestListLocked)
3112 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003113 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003114 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003115 }
3116 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003117 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003118
3119 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003120 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003121 if (lastFrameNumber != NULL) {
3122 *lastFrameNumber = mRepeatingLastFrameNumber;
3123 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003124 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003125 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003126 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003127 return OK;
3128}
3129
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003130status_t Camera3Device::RequestThread::flush() {
3131 ATRACE_CALL();
3132 Mutex::Autolock l(mFlushLock);
3133
Emilian Peev08dd2452017-04-06 16:55:14 +01003134 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003135}
3136
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003137void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003138 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003139 Mutex::Autolock l(mPauseLock);
3140 mDoPause = paused;
3141 mDoPauseSignal.signal();
3142}
3143
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003144status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3145 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003146 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003147 Mutex::Autolock l(mLatestRequestMutex);
3148 status_t res;
3149 while (mLatestRequestId != requestId) {
3150 nsecs_t startTime = systemTime();
3151
3152 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3153 if (res != OK) return res;
3154
3155 timeout -= (systemTime() - startTime);
3156 }
3157
3158 return OK;
3159}
3160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003161void Camera3Device::RequestThread::requestExit() {
3162 // Call parent to set up shutdown
3163 Thread::requestExit();
3164 // The exit from any possible waits
3165 mDoPauseSignal.signal();
3166 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003167
3168 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3169 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003170}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003171
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003172void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003173 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003174 bool surfaceAbandoned = false;
3175 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003176 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003177 {
3178 Mutex::Autolock l(mRequestLock);
3179 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3180 // repeating requests.
3181 for (const auto& request : mRepeatingRequests) {
3182 for (const auto& s : request->mOutputStreams) {
3183 if (s->isAbandoned()) {
3184 surfaceAbandoned = true;
3185 clearRepeatingRequestsLocked(&lastFrameNumber);
3186 break;
3187 }
3188 }
3189 if (surfaceAbandoned) {
3190 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003191 }
3192 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003193 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003194 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003195
3196 if (listener != NULL && surfaceAbandoned) {
3197 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003198 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003199}
3200
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003201bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003202 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003203 status_t res;
3204 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003205 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003206 uint32_t numRequestProcessed = 0;
3207 for (size_t i = 0; i < batchSize; i++) {
3208 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003209 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003210 }
3211
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003212 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3213
3214 bool triggerRemoveFailed = false;
3215 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3216 for (size_t i = 0; i < numRequestProcessed; i++) {
3217 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3218 nextRequest.submitted = true;
3219
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003220 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003221
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003222 if (!triggerRemoveFailed) {
3223 // Remove any previously queued triggers (after unlock)
3224 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3225 if (removeTriggerRes != OK) {
3226 triggerRemoveFailed = true;
3227 triggerFailedRequest = nextRequest;
3228 }
3229 }
3230 }
3231
3232 if (triggerRemoveFailed) {
3233 SET_ERR("RequestThread: Unable to remove triggers "
3234 "(capture request %d, HAL device: %s (%d)",
3235 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3236 cleanUpFailedRequests(/*sendRequestError*/ false);
3237 return false;
3238 }
3239
3240 if (res != OK) {
3241 // Should only get a failure here for malformed requests or device-level
3242 // errors, so consider all errors fatal. Bad metadata failures should
3243 // come through notify.
3244 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3245 mNextRequests[numRequestProcessed].halRequest.frame_number,
3246 strerror(-res), res);
3247 cleanUpFailedRequests(/*sendRequestError*/ false);
3248 return false;
3249 }
3250 return true;
3251}
3252
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003253std::pair<nsecs_t, nsecs_t> Camera3Device::RequestThread::calculateExpectedDurationRange(
3254 const camera_metadata_t *request) {
3255 std::pair<nsecs_t, nsecs_t> expectedRange(
3256 InFlightRequest::kDefaultMinExpectedDuration,
3257 InFlightRequest::kDefaultMaxExpectedDuration);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003258 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3259 find_camera_metadata_ro_entry(request,
3260 ANDROID_CONTROL_AE_MODE,
3261 &e);
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003262 if (e.count == 0) return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003263
3264 switch (e.data.u8[0]) {
3265 case ANDROID_CONTROL_AE_MODE_OFF:
3266 find_camera_metadata_ro_entry(request,
3267 ANDROID_SENSOR_EXPOSURE_TIME,
3268 &e);
3269 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003270 expectedRange.first = e.data.i64[0];
3271 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003272 }
3273 find_camera_metadata_ro_entry(request,
3274 ANDROID_SENSOR_FRAME_DURATION,
3275 &e);
3276 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003277 expectedRange.first = std::max(e.data.i64[0], expectedRange.first);
3278 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003279 }
3280 break;
3281 default:
3282 find_camera_metadata_ro_entry(request,
3283 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3284 &e);
3285 if (e.count > 1) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003286 expectedRange.first = 1e9 / e.data.i32[1];
3287 expectedRange.second = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003288 }
3289 break;
3290 }
3291
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003292 return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003293}
3294
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003295bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3296 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3297 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3298 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3299 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3300 return true;
3301 }
3302
3303 return false;
3304}
3305
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003306void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3307 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003308 camera_capture_request_t& halRequest = nextRequest.halRequest;
3309 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003310 Mutex::Autolock al(mLatestRequestMutex);
3311
Shuzhen Wang83bff122020-11-20 15:51:39 -08003312 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003313 mLatestRequest.acquire(cloned);
3314
3315 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003316 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3317 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3318 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003319 CameraMetadata(cloned));
3320 }
3321
3322 sp<Camera3Device> parent = mParent.promote();
3323 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003324 int32_t inputStreamId = -1;
3325 if (halRequest.input_buffer != nullptr) {
3326 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3327 }
3328
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003329 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003330 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003331 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3332 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003333 }
3334 }
3335
Shuzhen Wang83bff122020-11-20 15:51:39 -08003336 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003337 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003338 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003339 }
3340
Shuzhen Wang83bff122020-11-20 15:51:39 -08003341 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003342}
3343
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003344bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3345 ATRACE_CALL();
3346 bool updatesDetected = false;
3347
Emilian Peev4ec17882019-01-24 17:16:58 -08003348 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003349 for (auto tag : mSessionParamKeys) {
3350 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003351 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003352
3353 if (entry.count > 0) {
3354 bool isDifferent = false;
3355 if (lastEntry.count > 0) {
3356 // Have a last value, compare to see if changed
3357 if (lastEntry.type == entry.type &&
3358 lastEntry.count == entry.count) {
3359 // Same type and count, compare values
3360 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3361 size_t entryBytes = bytesPerValue * lastEntry.count;
3362 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3363 if (cmp != 0) {
3364 isDifferent = true;
3365 }
3366 } else {
3367 // Count or type has changed
3368 isDifferent = true;
3369 }
3370 } else {
3371 // No last entry, so always consider to be different
3372 isDifferent = true;
3373 }
3374
3375 if (isDifferent) {
3376 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003377 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3378 updatesDetected = true;
3379 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003380 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003381 }
3382 } else if (lastEntry.count > 0) {
3383 // Value has been removed
3384 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003385 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003386 updatesDetected = true;
3387 }
3388 }
3389
Emilian Peev4ec17882019-01-24 17:16:58 -08003390 bool reconfigureRequired;
3391 if (updatesDetected) {
3392 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3393 updatedParams);
3394 mLatestSessionParams = updatedParams;
3395 } else {
3396 reconfigureRequired = false;
3397 }
3398
3399 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003400}
3401
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003402bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003403 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003404 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003405 // Any function called from threadLoop() must not hold mInterfaceLock since
3406 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3407 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003408
3409 // Handle paused state.
3410 if (waitIfPaused()) {
3411 return true;
3412 }
3413
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003414 // Wait for the next batch of requests.
3415 waitForNextRequestBatch();
3416 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003417 return true;
3418 }
3419
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003420 // Get the latest request ID, if any
3421 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003422 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003423 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003424 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003425 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003426 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003427 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3428 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003429 }
3430
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003431 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3432 // or a single request from streaming or burst. In either case the first element
3433 // should contain the latest camera settings that we need to check for any session
3434 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003435 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003436 res = OK;
3437
3438 //Input stream buffers are already acquired at this point so an input stream
3439 //will not be able to move to idle state unless we force it.
3440 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3441 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3442 if (res != OK) {
3443 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3444 cleanUpFailedRequests(/*sendRequestError*/ false);
3445 return false;
3446 }
3447 }
3448
3449 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003450 sp<Camera3Device> parent = mParent.promote();
3451 if (parent != nullptr) {
3452 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003453 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003454 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003455
3456 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3457 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3458 if (res != OK) {
3459 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3460 cleanUpFailedRequests(/*sendRequestError*/ false);
3461 return false;
3462 }
3463 }
3464 }
3465 }
3466
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003467 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003468 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003469 if (res == TIMED_OUT) {
3470 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003471 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003472 // Check if any stream is abandoned.
3473 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003474 return true;
3475 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003476 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003477 return false;
3478 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003479
Zhijun Hecc27e112013-10-03 16:12:43 -07003480 // Inform waitUntilRequestProcessed thread of a new request ID
3481 {
3482 Mutex::Autolock al(mLatestRequestMutex);
3483
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003484 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003485 mLatestRequestSignal.signal();
3486 }
3487
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003488 // Submit a batch of requests to HAL.
3489 // Use flush lock only when submitting multilple requests in a batch.
3490 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3491 // which may take a long time to finish so synchronizing flush() and
3492 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3493 // For now, only synchronize for high speed recording and we should figure something out for
3494 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003495 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003496
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003497 if (useFlushLock) {
3498 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003499 }
3500
Zhijun Hef0645c12016-08-02 00:58:11 -07003501 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003502 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003503
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003504 sp<Camera3Device> parent = mParent.promote();
3505 if (parent != nullptr) {
3506 parent->mRequestBufferSM.onSubmittingRequest();
3507 }
3508
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003509 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003510 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003511 submitRequestSuccess = sendRequestsBatch();
3512
Shuzhen Wang686f6442017-06-20 16:16:04 -07003513 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3514 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003515
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003516 if (useFlushLock) {
3517 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003518 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003519
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003520 // Unset as current request
3521 {
3522 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003523 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003524 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003525 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003526
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003527 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003528}
3529
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003530status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3531 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3532 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3533 ANDROID_SCALER_CROP_REGION_SET};
3534 if (request == nullptr) {
3535 ALOGE("%s request metadata nullptr", __FUNCTION__);
3536 return BAD_VALUE;
3537 }
3538 status_t res = OK;
3539 for (const auto &key : kFwkOnlyRegionKeys) {
3540 if (request->exists(key)) {
3541 res = request->erase(key);
3542 if (res != OK) {
3543 return res;
3544 }
3545 }
3546 }
3547 return OK;
3548}
3549
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003550status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003551 ATRACE_CALL();
3552
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003553 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003554 for (size_t i = 0; i < mNextRequests.size(); i++) {
3555 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003556 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003557 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3558 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003559
3560 // Prepare a request to HAL
3561 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3562
3563 // Insert any queued triggers (before metadata is locked)
3564 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003565 if (res < 0) {
3566 SET_ERR("RequestThread: Unable to insert triggers "
3567 "(capture request %d, HAL device: %s (%d)",
3568 halRequest->frame_number, strerror(-res), res);
3569 return INVALID_OPERATION;
3570 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003571
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003572 int triggerCount = res;
3573 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3574 mPrevTriggers = triggerCount;
3575
Emilian Peeve23f1d92021-09-20 14:56:01 -07003576 // Do not override rotate&crop for stream configurations that include
3577 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3578 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3579 bool rotateAndCropChanged = mComposerOutput ? false :
3580 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003581 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003582
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003583 // If the request is the same as last, or we had triggers now or last time or
3584 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003585 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003586 (mPrevRequest != captureRequest || triggersMixedIn ||
3587 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003588 // Request settings are all the same within one batch, so only treat the first
3589 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003590 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003591 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003592 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003593 /**
3594 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003595 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003596 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003597 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003598 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003599 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003600 "(capture request %d, HAL device: %s (%d)",
3601 halRequest->frame_number, strerror(-res), res);
3602 return INVALID_OPERATION;
3603 }
3604
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003605 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003606 sp<Camera3Device> parent = mParent.promote();
3607 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003608 List<PhysicalCameraSettings>::iterator it;
3609 for (it = captureRequest->mSettingsList.begin();
3610 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003611 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3612 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003613 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3614 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3615 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3616 res);
3617 return INVALID_OPERATION;
3618 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003619 continue;
3620 }
3621
3622 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3623 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3624 updateCaptureRequest(&(it->metadata));
3625 if (res != OK) {
3626 SET_ERR("RequestThread: Unable to correct capture requests "
3627 "for scaler crop region and metering regions for request "
3628 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3629 res);
3630 return INVALID_OPERATION;
3631 }
3632 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003633 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3634 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3635 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3636 res);
3637 return INVALID_OPERATION;
3638 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003639 }
3640 }
3641
3642 // Correct metadata regions for distortion correction if enabled
3643 for (it = captureRequest->mSettingsList.begin();
3644 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003645 if (parent->mDistortionMappers.find(it->cameraId) ==
3646 parent->mDistortionMappers.end()) {
3647 continue;
3648 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003649
3650 if (!captureRequest->mDistortionCorrectionUpdated) {
3651 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3652 &(it->metadata));
3653 if (res != OK) {
3654 SET_ERR("RequestThread: Unable to correct capture requests "
3655 "for lens distortion for request %d: %s (%d)",
3656 halRequest->frame_number, strerror(-res), res);
3657 return INVALID_OPERATION;
3658 }
3659 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003660 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003661 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003662
3663 for (it = captureRequest->mSettingsList.begin();
3664 it != captureRequest->mSettingsList.end(); it++) {
3665 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3666 parent->mZoomRatioMappers.end()) {
3667 continue;
3668 }
3669
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003670 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003671 cameraIdsWithZoom.insert(it->cameraId);
3672 }
3673
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003674 if (!captureRequest->mZoomRatioUpdated) {
3675 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3676 &(it->metadata));
3677 if (res != OK) {
3678 SET_ERR("RequestThread: Unable to correct capture requests "
3679 "for zoom ratio for request %d: %s (%d)",
3680 halRequest->frame_number, strerror(-res), res);
3681 return INVALID_OPERATION;
3682 }
3683 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003684 }
3685 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003686 if (captureRequest->mRotateAndCropAuto &&
3687 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003688 for (it = captureRequest->mSettingsList.begin();
3689 it != captureRequest->mSettingsList.end(); it++) {
3690 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3691 if (mapper != parent->mRotateAndCropMappers.end()) {
3692 res = mapper->second.updateCaptureRequest(&(it->metadata));
3693 if (res != OK) {
3694 SET_ERR("RequestThread: Unable to correct capture requests "
3695 "for rotate-and-crop for request %d: %s (%d)",
3696 halRequest->frame_number, strerror(-res), res);
3697 return INVALID_OPERATION;
3698 }
3699 }
3700 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003701 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003702 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003703 }
3704 }
3705
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003706 /**
3707 * The request should be presorted so accesses in HAL
3708 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3709 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003710 captureRequest->mSettingsList.begin()->metadata.sort();
3711 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003712 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003713 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003714 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3715
3716 IF_ALOGV() {
3717 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3718 find_camera_metadata_ro_entry(
3719 halRequest->settings,
3720 ANDROID_CONTROL_AF_TRIGGER,
3721 &e
3722 );
3723 if (e.count > 0) {
3724 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3725 __FUNCTION__,
3726 halRequest->frame_number,
3727 e.data.u8[0]);
3728 }
3729 }
3730 } else {
3731 // leave request.settings NULL to indicate 'reuse latest given'
3732 ALOGVV("%s: Request settings are REUSED",
3733 __FUNCTION__);
3734 }
3735
Emilian Peevaebbe412018-01-15 13:53:24 +00003736 if (captureRequest->mSettingsList.size() > 1) {
3737 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3738 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003739 if (newRequest) {
3740 halRequest->physcam_settings =
3741 new const camera_metadata* [halRequest->num_physcam_settings];
3742 } else {
3743 halRequest->physcam_settings = nullptr;
3744 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003745 auto it = ++captureRequest->mSettingsList.begin();
3746 size_t i = 0;
3747 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3748 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003749 if (newRequest) {
3750 it->metadata.sort();
3751 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3752 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003753 }
3754 }
3755
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003756 uint32_t totalNumBuffers = 0;
3757
3758 // Fill in buffers
3759 if (captureRequest->mInputStream != NULL) {
3760 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003761
3762 halRequest->input_width = captureRequest->mInputBufferSize.width;
3763 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003764 totalNumBuffers += 1;
3765 } else {
3766 halRequest->input_buffer = NULL;
3767 }
3768
Emilian Peevf4816702020-04-03 15:44:51 -07003769 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003770 captureRequest->mOutputStreams.size());
3771 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003772 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003773
3774 sp<Camera3Device> parent = mParent.promote();
3775 if (parent == NULL) {
3776 // Should not happen, and nowhere to send errors to, so just log it
3777 CLOGE("RequestThread: Parent is gone");
3778 return INVALID_OPERATION;
3779 }
3780 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3781
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003782 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003783 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003784 sp<Camera3OutputStreamInterface> outputStream =
3785 captureRequest->mOutputStreams.editItemAt(j);
3786 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003787
3788 // Prepare video buffers for high speed recording on the first video request.
3789 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3790 // Only try to prepare video stream on the first video request.
3791 mPrepareVideoStream = false;
3792
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003793 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3794 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003795 while (res == NOT_ENOUGH_DATA) {
3796 res = outputStream->prepareNextBuffer();
3797 }
3798 if (res != OK) {
3799 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3800 __FUNCTION__, strerror(-res), res);
3801 outputStream->cancelPrepare();
3802 }
3803 }
3804
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003805 std::vector<size_t> uniqueSurfaceIds;
3806 res = outputStream->getUniqueSurfaceIds(
3807 captureRequest->mOutputSurfaces[streamId],
3808 &uniqueSurfaceIds);
3809 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3810 if (res != OK && res != INVALID_OPERATION) {
3811 ALOGE("%s: failed to query stream %d unique surface IDs",
3812 __FUNCTION__, streamId);
3813 return res;
3814 }
3815 if (res == OK) {
3816 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3817 }
3818
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003819 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003820 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003821 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003822 return TIMED_OUT;
3823 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003824 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003825 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003826 buffer.stream = outputStream->asHalStream();
3827 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003828 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003829 buffer.acquire_fence = -1;
3830 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003831 // Mark the output stream as unpreparable to block clients from calling
3832 // 'prepare' after this request reaches CameraHal and before the respective
3833 // buffers are requested.
3834 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003835 } else {
3836 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3837 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003838 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003839 if (res != OK) {
3840 // Can't get output buffer from gralloc queue - this could be due to
3841 // abandoned queue or other consumer misbehavior, so not a fatal
3842 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003843 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003844 " %s (%d)", strerror(-res), res);
3845
3846 return TIMED_OUT;
3847 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003848 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003849
3850 {
3851 sp<Camera3Device> parent = mParent.promote();
3852 if (parent != nullptr) {
3853 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3854 for (const auto& settings : captureRequest->mSettingsList) {
3855 if ((streamCameraId.isEmpty() &&
3856 parent->getId() == settings.cameraId.c_str()) ||
3857 streamCameraId == settings.cameraId.c_str()) {
3858 outputStream->fireBufferRequestForFrameNumber(
3859 captureRequest->mResultExtras.frameNumber,
3860 settings.metadata);
3861 }
3862 }
3863 }
3864 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003865
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003866 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003867 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3868 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3869 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3870 } else if (!physicalCameraId.isEmpty()) {
3871 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003872 }
3873 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003874 }
3875 totalNumBuffers += halRequest->num_output_buffers;
3876
3877 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003878 // If this request list is for constrained high speed recording (not
3879 // preview), and the current request is not the last one in the batch,
3880 // do not send callback to the app.
3881 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003882 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003883 hasCallback = false;
3884 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003885 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003886 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003887 const camera_metadata_t* settings = halRequest->settings;
3888 bool shouldUnlockSettings = false;
3889 if (settings == nullptr) {
3890 shouldUnlockSettings = true;
3891 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3892 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003893 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3894 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003895 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003896 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3897 isStillCapture = true;
3898 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3899 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003900
Emilian Peevaf8416e2021-02-04 17:52:43 -08003901 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003902 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003903 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3904 isZslCapture = true;
3905 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003906 }
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003907 auto expectedDurationRange = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003908 res = parent->registerInFlight(halRequest->frame_number,
3909 totalNumBuffers, captureRequest->mResultExtras,
3910 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003911 hasCallback,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003912 /*min*/expectedDurationRange.first,
3913 /*max*/expectedDurationRange.second,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003914 requestedPhysicalCameras, isStillCapture, isZslCapture,
3915 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003916 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003917 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003918 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3919 ", burstId = %" PRId32 ".",
3920 __FUNCTION__,
3921 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3922 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003923
3924 if (shouldUnlockSettings) {
3925 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3926 }
3927
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003928 if (res != OK) {
3929 SET_ERR("RequestThread: Unable to register new in-flight request:"
3930 " %s (%d)", strerror(-res), res);
3931 return INVALID_OPERATION;
3932 }
3933 }
3934
3935 return OK;
3936}
3937
Igor Murashkin1e479c02013-09-06 16:55:14 -07003938CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003939 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003940 Mutex::Autolock al(mLatestRequestMutex);
3941
3942 ALOGV("RequestThread::%s", __FUNCTION__);
3943
3944 return mLatestRequest;
3945}
3946
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003947bool Camera3Device::RequestThread::isStreamPending(
3948 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003949 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003950 Mutex::Autolock l(mRequestLock);
3951
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003952 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003953 if (!nextRequest.submitted) {
3954 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3955 if (stream == s) return true;
3956 }
3957 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003958 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003959 }
3960
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003961 for (const auto& request : mRequestQueue) {
3962 for (const auto& s : request->mOutputStreams) {
3963 if (stream == s) return true;
3964 }
3965 if (stream == request->mInputStream) return true;
3966 }
3967
3968 for (const auto& request : mRepeatingRequests) {
3969 for (const auto& s : request->mOutputStreams) {
3970 if (stream == s) return true;
3971 }
3972 if (stream == request->mInputStream) return true;
3973 }
3974
3975 return false;
3976}
Jianing Weicb0652e2014-03-12 18:29:36 -07003977
Emilian Peev40ead602017-09-26 15:46:36 +01003978bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3979 ATRACE_CALL();
3980 Mutex::Autolock l(mRequestLock);
3981
3982 for (const auto& nextRequest : mNextRequests) {
3983 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3984 if (s.first == streamId) {
3985 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3986 if (it != s.second.end()) {
3987 return true;
3988 }
3989 }
3990 }
3991 }
3992
3993 for (const auto& request : mRequestQueue) {
3994 for (const auto& s : request->mOutputSurfaces) {
3995 if (s.first == streamId) {
3996 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3997 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003998 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01003999 }
4000 }
4001 }
4002 }
4003
4004 for (const auto& request : mRepeatingRequests) {
4005 for (const auto& s : request->mOutputSurfaces) {
4006 if (s.first == streamId) {
4007 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4008 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004009 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004010 }
4011 }
4012 }
4013 }
4014
4015 return false;
4016}
4017
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004018void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4019 if (!mUseHalBufManager) {
4020 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4021 return;
4022 }
4023
4024 Mutex::Autolock pl(mPauseLock);
4025 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004026 mInterface->signalPipelineDrain(streamIds);
4027 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004028 }
4029 // If request thread is still busy, wait until paused then notify HAL
4030 mNotifyPipelineDrain = true;
4031 mStreamIdsToBeDrained = streamIds;
4032}
4033
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004034void Camera3Device::RequestThread::resetPipelineDrain() {
4035 Mutex::Autolock pl(mPauseLock);
4036 mNotifyPipelineDrain = false;
4037 mStreamIdsToBeDrained.clear();
4038}
4039
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004040void Camera3Device::RequestThread::clearPreviousRequest() {
4041 Mutex::Autolock l(mRequestLock);
4042 mPrevRequest.clear();
4043}
4044
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004045status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4046 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4047 ATRACE_CALL();
4048 Mutex::Autolock l(mTriggerMutex);
4049 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4050 return BAD_VALUE;
4051 }
4052 mRotateAndCropOverride = rotateAndCropValue;
4053 return OK;
4054}
4055
Emilian Peeve23f1d92021-09-20 14:56:01 -07004056status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4057 ATRACE_CALL();
4058 Mutex::Autolock l(mTriggerMutex);
4059 mComposerOutput = composerSurfacePresent;
4060 return OK;
4061}
4062
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004063status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004064 ATRACE_CALL();
4065 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004066 if (muteMode != mCameraMute) {
4067 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004068 mCameraMuteChanged = true;
4069 }
4070 return OK;
4071}
4072
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004073nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004074 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004075 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004076 return mExpectedInflightDuration > kMinInflightDuration ?
4077 mExpectedInflightDuration : kMinInflightDuration;
4078}
4079
Emilian Peevaebbe412018-01-15 13:53:24 +00004080void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004081 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004082 if ((request == nullptr) || (halRequest == nullptr)) {
4083 ALOGE("%s: Invalid request!", __FUNCTION__);
4084 return;
4085 }
4086
4087 if (halRequest->num_physcam_settings > 0) {
4088 if (halRequest->physcam_id != nullptr) {
4089 delete [] halRequest->physcam_id;
4090 halRequest->physcam_id = nullptr;
4091 }
4092 if (halRequest->physcam_settings != nullptr) {
4093 auto it = ++(request->mSettingsList.begin());
4094 size_t i = 0;
4095 for (; it != request->mSettingsList.end(); it++, i++) {
4096 it->metadata.unlock(halRequest->physcam_settings[i]);
4097 }
4098 delete [] halRequest->physcam_settings;
4099 halRequest->physcam_settings = nullptr;
4100 }
4101 }
4102}
4103
Ravneetaeb20dc2022-03-30 05:33:03 +00004104status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4105 Mutex::Autolock il(mInterfaceLock);
4106 Mutex::Autolock l(mLock);
4107
4108 if (mCameraServiceWatchdog != NULL) {
4109 mCameraServiceWatchdog->setEnabled(enabled);
4110 }
4111
4112 return OK;
4113}
4114
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004115void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4116 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004117 return;
4118 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004119
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004120 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004121 // Skip the ones that have been submitted successfully.
4122 if (nextRequest.submitted) {
4123 continue;
4124 }
4125
4126 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004127 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4128 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004129
4130 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004131 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004132 }
4133
Emilian Peevaebbe412018-01-15 13:53:24 +00004134 cleanupPhysicalSettings(captureRequest, halRequest);
4135
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004136 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004137 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004138 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4139 }
4140
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004141 // No output buffer can be returned when using HAL buffer manager
4142 if (!mUseHalBufManager) {
4143 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4144 //Buffers that failed processing could still have
4145 //valid acquire fence.
4146 int acquireFence = (*outputBuffers)[i].acquire_fence;
4147 if (0 <= acquireFence) {
4148 close(acquireFence);
4149 outputBuffers->editItemAt(i).acquire_fence = -1;
4150 }
Emilian Peevf4816702020-04-03 15:44:51 -07004151 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004152 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4153 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004154 /*timestampIncreasing*/true, std::vector<size_t> (),
4155 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004156 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004157 }
4158
4159 if (sendRequestError) {
4160 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004161 sp<NotificationListener> listener = mListener.promote();
4162 if (listener != NULL) {
4163 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004164 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004165 captureRequest->mResultExtras);
4166 }
4167 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004168
4169 // Remove yet-to-be submitted inflight request from inflightMap
4170 {
4171 sp<Camera3Device> parent = mParent.promote();
4172 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004173 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004174 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4175 if (idx >= 0) {
4176 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4177 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4178 parent->removeInFlightMapEntryLocked(idx);
4179 }
4180 }
4181 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004182 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004183
4184 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004185 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004186}
4187
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004188void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004189 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004190 // Optimized a bit for the simple steady-state case (single repeating
4191 // request), to avoid putting that request in the queue temporarily.
4192 Mutex::Autolock l(mRequestLock);
4193
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004194 assert(mNextRequests.empty());
4195
4196 NextRequest nextRequest;
4197 nextRequest.captureRequest = waitForNextRequestLocked();
4198 if (nextRequest.captureRequest == nullptr) {
4199 return;
4200 }
4201
Emilian Peevf4816702020-04-03 15:44:51 -07004202 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004203 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004204 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004205
4206 // Wait for additional requests
4207 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4208
4209 for (size_t i = 1; i < batchSize; i++) {
4210 NextRequest additionalRequest;
4211 additionalRequest.captureRequest = waitForNextRequestLocked();
4212 if (additionalRequest.captureRequest == nullptr) {
4213 break;
4214 }
4215
Emilian Peevf4816702020-04-03 15:44:51 -07004216 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004217 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004218 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004219 }
4220
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004221 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004222 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004223 mNextRequests.size(), batchSize);
4224 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004225 }
4226
4227 return;
4228}
4229
4230sp<Camera3Device::CaptureRequest>
4231 Camera3Device::RequestThread::waitForNextRequestLocked() {
4232 status_t res;
4233 sp<CaptureRequest> nextRequest;
4234
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004235 while (mRequestQueue.empty()) {
4236 if (!mRepeatingRequests.empty()) {
4237 // Always atomically enqueue all requests in a repeating request
4238 // list. Guarantees a complete in-sequence set of captures to
4239 // application.
4240 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004241 if (mFirstRepeating) {
4242 mFirstRepeating = false;
4243 } else {
4244 for (auto& request : requests) {
4245 // For repeating requests, override timestamp request using
4246 // the time a request is inserted into the request queue,
4247 // because the original repeating request will have an old
4248 // fixed timestamp.
4249 request->mRequestTimeNs = systemTime();
4250 }
4251 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004252 RequestList::const_iterator firstRequest =
4253 requests.begin();
4254 nextRequest = *firstRequest;
4255 mRequestQueue.insert(mRequestQueue.end(),
4256 ++firstRequest,
4257 requests.end());
4258 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004259
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004260 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004261
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004262 break;
4263 }
4264
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004265 if (!mRequestClearing) {
4266 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4267 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004268
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004269 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4270 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004271 Mutex::Autolock pl(mPauseLock);
4272 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004273 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004274 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004275 if (mNotifyPipelineDrain) {
4276 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4277 mNotifyPipelineDrain = false;
4278 mStreamIdsToBeDrained.clear();
4279 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004280 // Let the tracker know
4281 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4282 if (statusTracker != 0) {
4283 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4284 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004285 sp<Camera3Device> parent = mParent.promote();
4286 if (parent != nullptr) {
4287 parent->mRequestBufferSM.onRequestThreadPaused();
4288 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004289 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004290 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004291 // Stop waiting for now and let thread management happen
4292 return NULL;
4293 }
4294 }
4295
4296 if (nextRequest == NULL) {
4297 // Don't have a repeating request already in hand, so queue
4298 // must have an entry now.
4299 RequestList::iterator firstRequest =
4300 mRequestQueue.begin();
4301 nextRequest = *firstRequest;
4302 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004303 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4304 sp<NotificationListener> listener = mListener.promote();
4305 if (listener != NULL) {
4306 listener->notifyRequestQueueEmpty();
4307 }
4308 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004309 }
4310
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004311 // In case we've been unpaused by setPaused clearing mDoPause, need to
4312 // update internal pause state (capture/setRepeatingRequest unpause
4313 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004314 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004315 if (mPaused) {
4316 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4317 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4318 if (statusTracker != 0) {
4319 statusTracker->markComponentActive(mStatusId);
4320 }
4321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004322 mPaused = false;
4323
4324 // Check if we've reconfigured since last time, and reset the preview
4325 // request if so. Can't use 'NULL request == repeat' across configure calls.
4326 if (mReconfigured) {
4327 mPrevRequest.clear();
4328 mReconfigured = false;
4329 }
4330
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004331 if (nextRequest != NULL) {
4332 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004333 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4334 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004335
4336 // Since RequestThread::clear() removes buffers from the input stream,
4337 // get the right buffer here before unlocking mRequestLock
4338 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004339 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4340 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004341 if (res != OK) {
4342 // Can't get input buffer from gralloc queue - this could be due to
4343 // disconnected queue or other producer misbehavior, so not a fatal
4344 // error
4345 ALOGE("%s: Can't get input buffer, skipping request:"
4346 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004347
4348 sp<NotificationListener> listener = mListener.promote();
4349 if (listener != NULL) {
4350 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004351 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004352 nextRequest->mResultExtras);
4353 }
4354 return NULL;
4355 }
4356 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004357 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004358
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004359 return nextRequest;
4360}
4361
4362bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004363 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004364 status_t res;
4365 Mutex::Autolock l(mPauseLock);
4366 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004367 if (mPaused == false) {
4368 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004369 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004370 if (mNotifyPipelineDrain) {
4371 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4372 mNotifyPipelineDrain = false;
4373 mStreamIdsToBeDrained.clear();
4374 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004375 // Let the tracker know
4376 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4377 if (statusTracker != 0) {
4378 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4379 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004380 sp<Camera3Device> parent = mParent.promote();
4381 if (parent != nullptr) {
4382 parent->mRequestBufferSM.onRequestThreadPaused();
4383 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004384 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004385
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004386 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004387 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004388 return true;
4389 }
4390 }
4391 // We don't set mPaused to false here, because waitForNextRequest needs
4392 // to further manage the paused state in case of starvation.
4393 return false;
4394}
4395
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004396void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004397 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004398 // With work to do, mark thread as unpaused.
4399 // If paused by request (setPaused), don't resume, to avoid
4400 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004401 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004402 Mutex::Autolock p(mPauseLock);
4403 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004404 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4405 if (mPaused) {
4406 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4407 if (statusTracker != 0) {
4408 statusTracker->markComponentActive(mStatusId);
4409 }
4410 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004411 mPaused = false;
4412 }
4413}
4414
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004415void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4416 sp<Camera3Device> parent = mParent.promote();
4417 if (parent != NULL) {
4418 va_list args;
4419 va_start(args, fmt);
4420
4421 parent->setErrorStateV(fmt, args);
4422
4423 va_end(args);
4424 }
4425}
4426
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004427status_t Camera3Device::RequestThread::insertTriggers(
4428 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004429 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004430 Mutex::Autolock al(mTriggerMutex);
4431
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004432 sp<Camera3Device> parent = mParent.promote();
4433 if (parent == NULL) {
4434 CLOGE("RequestThread: Parent is gone");
4435 return DEAD_OBJECT;
4436 }
4437
Emilian Peevaebbe412018-01-15 13:53:24 +00004438 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004439 size_t count = mTriggerMap.size();
4440
4441 for (size_t i = 0; i < count; ++i) {
4442 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004443 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004444
4445 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4446 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4447 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004448 if (isAeTrigger) {
4449 request->mResultExtras.precaptureTriggerId = triggerId;
4450 mCurrentPreCaptureTriggerId = triggerId;
4451 } else {
4452 request->mResultExtras.afTriggerId = triggerId;
4453 mCurrentAfTriggerId = triggerId;
4454 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004455 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004456 }
4457
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004458 camera_metadata_entry entry = metadata.find(tag);
4459
4460 if (entry.count > 0) {
4461 /**
4462 * Already has an entry for this trigger in the request.
4463 * Rewrite it with our requested trigger value.
4464 */
4465 RequestTrigger oldTrigger = trigger;
4466
4467 oldTrigger.entryValue = entry.data.u8[0];
4468
4469 mTriggerReplacedMap.add(tag, oldTrigger);
4470 } else {
4471 /**
4472 * More typical, no trigger entry, so we just add it
4473 */
4474 mTriggerRemovedMap.add(tag, trigger);
4475 }
4476
4477 status_t res;
4478
4479 switch (trigger.getTagType()) {
4480 case TYPE_BYTE: {
4481 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4482 res = metadata.update(tag,
4483 &entryValue,
4484 /*count*/1);
4485 break;
4486 }
4487 case TYPE_INT32:
4488 res = metadata.update(tag,
4489 &trigger.entryValue,
4490 /*count*/1);
4491 break;
4492 default:
4493 ALOGE("%s: Type not supported: 0x%x",
4494 __FUNCTION__,
4495 trigger.getTagType());
4496 return INVALID_OPERATION;
4497 }
4498
4499 if (res != OK) {
4500 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4501 ", value %d", __FUNCTION__, trigger.getTagName(),
4502 trigger.entryValue);
4503 return res;
4504 }
4505
4506 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4507 trigger.getTagName(),
4508 trigger.entryValue);
4509 }
4510
4511 mTriggerMap.clear();
4512
4513 return count;
4514}
4515
4516status_t Camera3Device::RequestThread::removeTriggers(
4517 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004518 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004519 Mutex::Autolock al(mTriggerMutex);
4520
Emilian Peevaebbe412018-01-15 13:53:24 +00004521 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004522
4523 /**
4524 * Replace all old entries with their old values.
4525 */
4526 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4527 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4528
4529 status_t res;
4530
4531 uint32_t tag = trigger.metadataTag;
4532 switch (trigger.getTagType()) {
4533 case TYPE_BYTE: {
4534 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4535 res = metadata.update(tag,
4536 &entryValue,
4537 /*count*/1);
4538 break;
4539 }
4540 case TYPE_INT32:
4541 res = metadata.update(tag,
4542 &trigger.entryValue,
4543 /*count*/1);
4544 break;
4545 default:
4546 ALOGE("%s: Type not supported: 0x%x",
4547 __FUNCTION__,
4548 trigger.getTagType());
4549 return INVALID_OPERATION;
4550 }
4551
4552 if (res != OK) {
4553 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4554 ", trigger value %d", __FUNCTION__,
4555 trigger.getTagName(), trigger.entryValue);
4556 return res;
4557 }
4558 }
4559 mTriggerReplacedMap.clear();
4560
4561 /**
4562 * Remove all new entries.
4563 */
4564 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4565 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4566 status_t res = metadata.erase(trigger.metadataTag);
4567
4568 if (res != OK) {
4569 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4570 ", trigger value %d", __FUNCTION__,
4571 trigger.getTagName(), trigger.entryValue);
4572 return res;
4573 }
4574 }
4575 mTriggerRemovedMap.clear();
4576
4577 return OK;
4578}
4579
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004580status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004581 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004582 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004583 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004584 status_t res;
4585
Emilian Peevaebbe412018-01-15 13:53:24 +00004586 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004587
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004588 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004589 // exists
4590 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4591 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4592 if (afTrigger.count > 0 &&
4593 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4594 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004595 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004596 if (res != OK) return res;
4597 }
4598
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004599 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004600 // if none already exists
4601 camera_metadata_entry pcTrigger =
4602 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4603 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4604 if (pcTrigger.count > 0 &&
4605 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4606 pcId.count == 0) {
4607 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004608 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004609 if (res != OK) return res;
4610 }
4611
4612 return OK;
4613}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004614
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004615bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4616 const sp<CaptureRequest> &request) {
4617 ATRACE_CALL();
4618
4619 if (request->mRotateAndCropAuto) {
4620 Mutex::Autolock l(mTriggerMutex);
4621 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4622
4623 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4624 if (rotateAndCropEntry.count > 0) {
4625 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4626 return false;
4627 } else {
4628 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4629 return true;
4630 }
4631 } else {
4632 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4633 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4634 &rotateAndCrop_u8, 1);
4635 return true;
4636 }
4637 }
4638 return false;
4639}
4640
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004641bool Camera3Device::RequestThread::overrideTestPattern(
4642 const sp<CaptureRequest> &request) {
4643 ATRACE_CALL();
4644
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004645 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004646
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004647 Mutex::Autolock l(mTriggerMutex);
4648
4649 bool changed = false;
4650
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004651 // For a multi-camera, the physical cameras support the same set of
4652 // test pattern modes as the logical camera.
4653 for (auto& settings : request->mSettingsList) {
4654 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004655
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004656 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4657 int32_t testPatternData[4] = {
4658 settings.mOriginalTestPatternData[0],
4659 settings.mOriginalTestPatternData[1],
4660 settings.mOriginalTestPatternData[2],
4661 settings.mOriginalTestPatternData[3]
4662 };
4663 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4664 testPatternMode = mCameraMute;
4665 testPatternData[0] = 0;
4666 testPatternData[1] = 0;
4667 testPatternData[2] = 0;
4668 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004669 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004670
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004671 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4672 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4673 if (testPatternEntry.count > 0) {
4674 if (testPatternEntry.data.i32[0] != testPatternMode) {
4675 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004676 changed = true;
4677 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004678 } else if (supportTestPatternModeKey) {
4679 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4680 &testPatternMode, 1);
4681 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004682 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004683
4684 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4685 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4686 if (testPatternColor.count >= 4) {
4687 for (size_t i = 0; i < 4; i++) {
4688 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4689 testPatternColor.data.i32[i] = testPatternData[i];
4690 changed = true;
4691 }
4692 }
4693 } else if (supportTestPatternDataKey) {
4694 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4695 testPatternData, 4);
4696 changed = true;
4697 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004698 }
4699
4700 return changed;
4701}
4702
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004703status_t Camera3Device::RequestThread::setHalInterface(
4704 sp<HalInterface> newHalInterface) {
4705 if (newHalInterface.get() == nullptr) {
4706 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4707 return DEAD_OBJECT;
4708 }
4709
4710 mInterface = newHalInterface;
4711
4712 return OK;
4713}
4714
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004715/**
4716 * PreparerThread inner class methods
4717 */
4718
4719Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004720 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004721 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004722}
4723
4724Camera3Device::PreparerThread::~PreparerThread() {
4725 Thread::requestExitAndWait();
4726 if (mCurrentStream != nullptr) {
4727 mCurrentStream->cancelPrepare();
4728 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4729 mCurrentStream.clear();
4730 }
4731 clear();
4732}
4733
Ruben Brunkc78ac262015-08-13 17:58:46 -07004734status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004735 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004736 status_t res;
4737
4738 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004739 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004740
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004741 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004742 if (res == OK) {
4743 // No preparation needed, fire listener right off
4744 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004745 if (listener != NULL) {
4746 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004747 }
4748 return OK;
4749 } else if (res != NOT_ENOUGH_DATA) {
4750 return res;
4751 }
4752
4753 // Need to prepare, start up thread if necessary
4754 if (!mActive) {
4755 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4756 // isn't running
4757 Thread::requestExitAndWait();
4758 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4759 if (res != OK) {
4760 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004761 if (listener != NULL) {
4762 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004763 }
4764 return res;
4765 }
4766 mCancelNow = false;
4767 mActive = true;
4768 ALOGV("%s: Preparer stream started", __FUNCTION__);
4769 }
4770
4771 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004772 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004773 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4774
4775 return OK;
4776}
4777
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004778void Camera3Device::PreparerThread::pause() {
4779 ATRACE_CALL();
4780
4781 Mutex::Autolock l(mLock);
4782
4783 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4784 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4785 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4786 int currentMaxCount = mCurrentMaxCount;
4787 mPendingStreams.clear();
4788 mCancelNow = true;
4789 while (mActive) {
4790 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4791 if (res == TIMED_OUT) {
4792 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4793 return;
4794 } else if (res != OK) {
4795 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4796 return;
4797 }
4798 }
4799
4800 //Check whether the prepare thread was able to complete the current
4801 //stream. In case work is still pending emplace it along with the rest
4802 //of the streams in the pending list.
4803 if (currentStream != nullptr) {
4804 if (!mCurrentPrepareComplete) {
4805 pendingStreams.emplace(currentMaxCount, currentStream);
4806 }
4807 }
4808
4809 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4810 for (const auto& it : mPendingStreams) {
4811 it.second->cancelPrepare();
4812 }
4813}
4814
4815status_t Camera3Device::PreparerThread::resume() {
4816 ATRACE_CALL();
4817 status_t res;
4818
4819 Mutex::Autolock l(mLock);
4820 sp<NotificationListener> listener = mListener.promote();
4821
4822 if (mActive) {
4823 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4824 return NO_INIT;
4825 }
4826
4827 auto it = mPendingStreams.begin();
4828 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004829 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004830 if (res == OK) {
4831 if (listener != NULL) {
4832 listener->notifyPrepared(it->second->getId());
4833 }
4834 it = mPendingStreams.erase(it);
4835 } else if (res != NOT_ENOUGH_DATA) {
4836 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4837 res, strerror(-res));
4838 it = mPendingStreams.erase(it);
4839 } else {
4840 it++;
4841 }
4842 }
4843
4844 if (mPendingStreams.empty()) {
4845 return OK;
4846 }
4847
4848 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4849 if (res != OK) {
4850 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4851 __FUNCTION__, res, strerror(-res));
4852 return res;
4853 }
4854 mCancelNow = false;
4855 mActive = true;
4856 ALOGV("%s: Preparer stream started", __FUNCTION__);
4857
4858 return OK;
4859}
4860
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004861status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004862 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004863 Mutex::Autolock l(mLock);
4864
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004865 for (const auto& it : mPendingStreams) {
4866 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004867 }
4868 mPendingStreams.clear();
4869 mCancelNow = true;
4870
4871 return OK;
4872}
4873
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004874void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004875 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004876 Mutex::Autolock l(mLock);
4877 mListener = listener;
4878}
4879
4880bool Camera3Device::PreparerThread::threadLoop() {
4881 status_t res;
4882 {
4883 Mutex::Autolock l(mLock);
4884 if (mCurrentStream == nullptr) {
4885 // End thread if done with work
4886 if (mPendingStreams.empty()) {
4887 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4888 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4889 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4890 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004891 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004892 return false;
4893 }
4894
4895 // Get next stream to prepare
4896 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004897 mCurrentStream = it->second;
4898 mCurrentMaxCount = it->first;
4899 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004900 mPendingStreams.erase(it);
4901 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4902 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4903 } else if (mCancelNow) {
4904 mCurrentStream->cancelPrepare();
4905 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4906 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4907 mCurrentStream.clear();
4908 mCancelNow = false;
4909 return true;
4910 }
4911 }
4912
4913 res = mCurrentStream->prepareNextBuffer();
4914 if (res == NOT_ENOUGH_DATA) return true;
4915 if (res != OK) {
4916 // Something bad happened; try to recover by cancelling prepare and
4917 // signalling listener anyway
4918 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4919 mCurrentStream->getId(), res, strerror(-res));
4920 mCurrentStream->cancelPrepare();
4921 }
4922
4923 // This stream has finished, notify listener
4924 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004925 sp<NotificationListener> listener = mListener.promote();
4926 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004927 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4928 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004929 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004930 }
4931
4932 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4933 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004934 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004935
4936 return true;
4937}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004938
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004939status_t Camera3Device::RequestBufferStateMachine::initialize(
4940 sp<camera3::StatusTracker> statusTracker) {
4941 if (statusTracker == nullptr) {
4942 ALOGE("%s: statusTracker is null", __FUNCTION__);
4943 return BAD_VALUE;
4944 }
4945
4946 std::lock_guard<std::mutex> lock(mLock);
4947 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004948 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004949 return OK;
4950}
4951
4952bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4953 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004954 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004955 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004956 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004957 return true;
4958 }
4959 return false;
4960}
4961
4962void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4963 std::lock_guard<std::mutex> lock(mLock);
4964 if (!mRequestBufferOngoing) {
4965 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4966 return;
4967 }
4968 mRequestBufferOngoing = false;
4969 if (mStatus == RB_STATUS_PENDING_STOP) {
4970 checkSwitchToStopLocked();
4971 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004972 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004973}
4974
4975void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4976 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004977 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004978 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004979 return;
4980}
4981
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004982void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004983 std::lock_guard<std::mutex> lock(mLock);
4984 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004985 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4986 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004987 mInflightMapEmpty = false;
4988 if (mStatus == RB_STATUS_STOPPED) {
4989 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004990 }
4991 return;
4992}
4993
4994void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4995 std::lock_guard<std::mutex> lock(mLock);
4996 mRequestThreadPaused = true;
4997 if (mStatus == RB_STATUS_PENDING_STOP) {
4998 checkSwitchToStopLocked();
4999 }
5000 return;
5001}
5002
5003void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5004 std::lock_guard<std::mutex> lock(mLock);
5005 mInflightMapEmpty = true;
5006 if (mStatus == RB_STATUS_PENDING_STOP) {
5007 checkSwitchToStopLocked();
5008 }
5009 return;
5010}
5011
5012void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5013 std::lock_guard<std::mutex> lock(mLock);
5014 if (!checkSwitchToStopLocked()) {
5015 mStatus = RB_STATUS_PENDING_STOP;
5016 }
5017 return;
5018}
5019
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005020bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5021 std::lock_guard<std::mutex> lock(mLock);
5022 if (mRequestBufferOngoing) {
5023 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5024 __FUNCTION__);
5025 return false;
5026 }
5027 mSwitchedToOffline = true;
5028 mInflightMapEmpty = true;
5029 mRequestThreadPaused = true;
5030 mStatus = RB_STATUS_STOPPED;
5031 return true;
5032}
5033
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005034void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5035 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5036 if (statusTracker != nullptr) {
5037 if (active) {
5038 statusTracker->markComponentActive(mRequestBufferStatusId);
5039 } else {
5040 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5041 }
5042 }
5043}
5044
5045bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5046 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5047 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005048 return true;
5049 }
5050 return false;
5051}
5052
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005053bool Camera3Device::startRequestBuffer() {
5054 return mRequestBufferSM.startRequestBuffer();
5055}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005056
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005057void Camera3Device::endRequestBuffer() {
5058 mRequestBufferSM.endRequestBuffer();
5059}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005060
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005061nsecs_t Camera3Device::getWaitDuration() {
5062 return kBaseGetBufferWait + getExpectedInFlightDuration();
5063}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005064
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005065void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5066 mInterface->getInflightBufferKeys(out);
5067}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005068
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005069void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5070 mInterface->getInflightRequestBufferKeys(out);
5071}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005072
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005073std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5074 std::vector<sp<Camera3StreamInterface>> ret;
5075 bool hasInputStream = mInputStream != nullptr;
5076 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5077 if (hasInputStream) {
5078 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005079 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005080 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5081 ret.push_back(mOutputStreams[i]);
5082 }
5083 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5084 ret.push_back(mDeletedStreams[i]);
5085 }
5086 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005087}
5088
Emilian Peevcc0b7952020-01-07 13:54:47 -08005089void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5090 ATRACE_CALL();
5091
5092 if (offlineStreamIds == nullptr) {
5093 return;
5094 }
5095
5096 Mutex::Autolock il(mInterfaceLock);
5097
5098 auto streamIds = mOutputStreams.getStreamIds();
5099 bool hasInputStream = mInputStream != nullptr;
5100 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5101 offlineStreamIds->push_back(mInputStream->getId());
5102 }
5103
5104 for (const auto & streamId : streamIds) {
5105 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5106 // Streams that use the camera buffer manager are currently not supported in
5107 // offline mode
5108 if (stream->getOfflineProcessingSupport() &&
5109 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5110 offlineStreamIds->push_back(streamId);
5111 }
5112 }
5113}
5114
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005115status_t Camera3Device::setRotateAndCropAutoBehavior(
5116 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5117 ATRACE_CALL();
5118 Mutex::Autolock il(mInterfaceLock);
5119 Mutex::Autolock l(mLock);
5120 if (mRequestThread == nullptr) {
5121 return INVALID_OPERATION;
5122 }
5123 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5124}
5125
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005126bool Camera3Device::supportsCameraMute() {
5127 Mutex::Autolock il(mInterfaceLock);
5128 Mutex::Autolock l(mLock);
5129
5130 return mSupportCameraMute;
5131}
5132
5133status_t Camera3Device::setCameraMute(bool enabled) {
5134 ATRACE_CALL();
5135 Mutex::Autolock il(mInterfaceLock);
5136 Mutex::Autolock l(mLock);
5137
5138 if (mRequestThread == nullptr || !mSupportCameraMute) {
5139 return INVALID_OPERATION;
5140 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005141 int32_t muteMode =
5142 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5143 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5144 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5145 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005146}
5147
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005148status_t Camera3Device::injectCamera(const String8& injectedCamId,
5149 sp<CameraProviderManager> manager) {
5150 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5151 ATRACE_CALL();
5152 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005153 // When the camera device is active, injectCamera() and stopInjection() will call
5154 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5155 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5156 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5157 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5158 // waitUntilStateThenRelock().
5159 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005160
5161 status_t res = NO_ERROR;
5162 if (mInjectionMethods->isInjecting()) {
5163 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5164 return OK;
5165 } else {
5166 res = mInjectionMethods->stopInjection();
5167 if (res != OK) {
5168 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5169 __FUNCTION__, res);
5170 return res;
5171 }
5172 }
5173 }
5174
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005175 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005176 if (res != OK) {
5177 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5178 __FUNCTION__, res);
5179 return res;
5180 }
5181
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005182 // When the second display of android is cast to the remote device, and the opened camera is
5183 // also cast to the second display, in this case, because the camera has configured the streams
5184 // at this time, we can directly call injectCamera() to replace the internal camera with
5185 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005186 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5187 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5188
5189 camera3::camera_stream_configuration injectionConfig;
5190 std::vector<uint32_t> injectionBufferSizes;
5191 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5192 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5193 || injectionBufferSizes.size() <= 0) {
5194 ALOGE("Failed to inject camera due to abandoned configuration! "
5195 "mOperatingMode: %d injectionConfig.num_streams: %d "
5196 "injectionBufferSizes.size(): %zu", mOperatingMode,
5197 injectionConfig.num_streams, injectionBufferSizes.size());
5198 return DEAD_OBJECT;
5199 }
5200
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005201 res = mInjectionMethods->injectCamera(
5202 injectionConfig, injectionBufferSizes);
5203 if (res != OK) {
5204 ALOGE("Can't finish inject camera process!");
5205 return res;
5206 }
5207 }
5208
5209 return OK;
5210}
5211
5212status_t Camera3Device::stopInjection() {
5213 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5214 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005215 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005216 return mInjectionMethods->stopInjection();
5217}
5218
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005219}; // namespace android