blob: ebfa1d6a9bb3606093395d596ba36249de97c08a [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();
232 mCameraServiceWatchdog->run("CameraServiceWatchdog");
233
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800234 return OK;
235}
236
237status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700238 return disconnectImpl();
239}
240
241status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800242 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700243 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800244
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700245 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700246 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700247 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800248 Mutex::Autolock il(mInterfaceLock);
249 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
250 {
251 Mutex::Autolock l(mLock);
252 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800254 if (mStatus == STATUS_ACTIVE ||
255 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
256 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800258 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800260 } else {
261 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
262 if (res != OK) {
263 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
264 maxExpectedDuration);
265 // Continue to close device even in case of error
266 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700268 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800270 if (mStatus == STATUS_ERROR) {
271 CLOGE("Shutting down in an error state");
272 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800274 if (mStatusTracker != NULL) {
275 mStatusTracker->requestExit();
276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800278 if (mRequestThread != NULL) {
279 mRequestThread->requestExit();
280 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800282 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
283 for (size_t i = 0; i < mOutputStreams.size(); i++) {
284 streams.push_back(mOutputStreams[i]);
285 }
286 if (mInputStream != nullptr) {
287 streams.push_back(mInputStream);
288 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700289 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800291 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
292 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
294 // HAL may be in a bad state, so waiting for request thread
295 // (which may be stuck in the HAL processCaptureRequest call)
296 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800297 // give up mInterfaceLock here and then lock it again. Could this lead
298 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 mRequestThread->join();
300 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700301 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800302 Mutex::Autolock il(mInterfaceLock);
303 if (mStatusTracker != NULL) {
304 mStatusTracker->join();
305 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800307 if (mInjectionMethods->isInjecting()) {
308 mInjectionMethods->stopInjection();
309 }
310
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800311 HalInterface* interface;
312 {
313 Mutex::Autolock l(mLock);
314 mRequestThread.clear();
315 Mutex::Autolock stLock(mTrackerLock);
316 mStatusTracker.clear();
317 interface = mInterface.get();
318 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700319
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800320 // Call close without internal mutex held, as the HAL close may need to
321 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000322 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700323
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800324 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800326 {
327 Mutex::Autolock l(mLock);
328 mInterface->clear();
329 mOutputStreams.clear();
330 mInputStream.clear();
331 mDeletedStreams.clear();
332 mBufferManager.clear();
333 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
334 }
335
336 for (auto& weakStream : streams) {
337 sp<Camera3StreamInterface> stream = weakStream.promote();
338 if (stream != nullptr) {
339 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
340 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
341 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700342 }
343 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700344 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000345
346 if (mCameraServiceWatchdog != NULL) {
347 mCameraServiceWatchdog->requestExit();
348 mCameraServiceWatchdog.clear();
349 }
350
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700351 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800352}
353
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700354// For dumping/debugging only -
355// try to acquire a lock a few times, eventually give up to proceed with
356// debug/dump operations
357bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
358 bool gotLock = false;
359 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
360 if (lock.tryLock() == NO_ERROR) {
361 gotLock = true;
362 break;
363 } else {
364 usleep(kDumpSleepDuration);
365 }
366 }
367 return gotLock;
368}
369
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800370nsecs_t Camera3Device::getMonoToBoottimeOffset() {
371 // try three times to get the clock offset, choose the one
372 // with the minimum gap in measurements.
373 const int tries = 3;
374 nsecs_t bestGap, measured;
375 for (int i = 0; i < tries; ++i) {
376 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
377 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
378 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
379 const nsecs_t gap = tmono2 - tmono;
380 if (i == 0 || gap < bestGap) {
381 bestGap = gap;
382 measured = tbase - ((tmono + tmono2) >> 1);
383 }
384 }
385 return measured;
386}
387
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800388ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
389 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700390 // Get max jpeg size (area-wise) for default sensor pixel mode
391 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800392 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700393 /*isUltraHighResolutionSensor*/false);
394 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
395 // not ultra high res sensor
396 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800397 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700398 /*isUltraHighResolution*/true);
399 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800400 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
401 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700402 return BAD_VALUE;
403 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700404 bool useMaxSensorPixelModeThreshold = false;
405 if (uhrMaxJpegResolution.width != 0 &&
406 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
407 // Use the ultra high res max jpeg size and max jpeg buffer size
408 useMaxSensorPixelModeThreshold = true;
409 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700410
Zhijun Hef7da0962014-04-24 13:27:56 -0700411 // Get max jpeg buffer size
412 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800413 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700414 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800415 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
416 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700417 return BAD_VALUE;
418 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700419 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700420
421 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
422 if (useMaxSensorPixelModeThreshold) {
423 maxJpegBufferSize =
424 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
425 maxDefaultJpegResolution, maxJpegBufferSize);
426 chosenMaxJpegResolution = uhrMaxJpegResolution;
427 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800428 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700429
430 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700431 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700432 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800433 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
434 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700435 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800436 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
437 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700438 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700440 return jpegBufferSize;
441}
442
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800443ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700444 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800445 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700446 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800447 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
448 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700449 return BAD_VALUE;
450 }
451 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
452 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
453 return maxBytesForPointCloud;
454}
455
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800456ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
457 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800458 const int PER_CONFIGURATION_SIZE = 3;
459 const int WIDTH_OFFSET = 0;
460 const int HEIGHT_OFFSET = 1;
461 const int SIZE_OFFSET = 2;
462 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800463 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800464 camera3::SessionConfigurationUtils::getAppropriateModeTag(
465 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
466 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800467 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800468 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800469 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
470 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800471 return BAD_VALUE;
472 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700473
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800474 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
475 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
476 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
477 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
478 }
479 }
480
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800481 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
482 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800483 return BAD_VALUE;
484}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700485
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800486status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
487 ATRACE_CALL();
488 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700489
490 // Try to lock, but continue in case of failure (to avoid blocking in
491 // deadlocks)
492 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
493 bool gotLock = tryLockSpinRightRound(mLock);
494
495 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800496 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
497 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700498 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800499 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
500 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700501
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800502 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700503
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800504 String16 templatesOption("-t");
505 int n = args.size();
506 for (int i = 0; i < n; i++) {
507 if (args[i] == templatesOption) {
508 dumpTemplates = true;
509 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000510 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700511 if (i + 1 < n) {
512 String8 monitorTags = String8(args[i + 1]);
513 if (monitorTags == "off") {
514 mTagMonitor.disableMonitoring();
515 } else {
516 mTagMonitor.parseTagsToMonitor(monitorTags);
517 }
518 } else {
519 mTagMonitor.disableMonitoring();
520 }
521 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800522 }
523
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800524 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800526 const char *status =
527 mStatus == STATUS_ERROR ? "ERROR" :
528 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700529 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
530 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800531 mStatus == STATUS_ACTIVE ? "ACTIVE" :
532 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700533
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800534 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700535 if (mStatus == STATUS_ERROR) {
536 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
537 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800539 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000540 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
541 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
542 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800543 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800544
545 if (mInputStream != NULL) {
546 write(fd, lines.string(), lines.size());
547 mInputStream->dump(fd, args);
548 } else {
549 lines.appendFormat(" No input stream.\n");
550 write(fd, lines.string(), lines.size());
551 }
552 for (size_t i = 0; i < mOutputStreams.size(); i++) {
553 mOutputStreams[i]->dump(fd,args);
554 }
555
Zhijun He431503c2016-03-07 17:30:16 -0800556 if (mBufferManager != NULL) {
557 lines = String8(" Camera3 Buffer Manager:\n");
558 write(fd, lines.string(), lines.size());
559 mBufferManager->dump(fd, args);
560 }
Zhijun He125684a2015-12-26 15:07:30 -0800561
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700562 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700563 if (mInFlightLock.try_lock()) {
564 if (mInFlightMap.size() == 0) {
565 lines.append(" None\n");
566 } else {
567 for (size_t i = 0; i < mInFlightMap.size(); i++) {
568 InFlightRequest r = mInFlightMap.valueAt(i);
569 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
570 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
571 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
572 r.numBuffersLeft);
573 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700574 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700575 mInFlightLock.unlock();
576 } else {
577 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700578 }
579 write(fd, lines.string(), lines.size());
580
Shuzhen Wang686f6442017-06-20 16:16:04 -0700581 if (mRequestThread != NULL) {
582 mRequestThread->dumpCaptureRequestLatency(fd,
583 " ProcessCaptureRequest latency histogram:");
584 }
585
Igor Murashkin1e479c02013-09-06 16:55:14 -0700586 {
587 lines = String8(" Last request sent:\n");
588 write(fd, lines.string(), lines.size());
589
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700590 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700591 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
592 }
593
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800594 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700595 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800596 "TEMPLATE_PREVIEW",
597 "TEMPLATE_STILL_CAPTURE",
598 "TEMPLATE_VIDEO_RECORD",
599 "TEMPLATE_VIDEO_SNAPSHOT",
600 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800601 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800602 };
603
Emilian Peevf4816702020-04-03 15:44:51 -0700604 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800605 camera_metadata_t *templateRequest = nullptr;
606 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700607 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800608 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800610 lines.append(" Not supported\n");
611 write(fd, lines.string(), lines.size());
612 } else {
613 write(fd, lines.string(), lines.size());
614 dump_indented_camera_metadata(templateRequest,
615 fd, /*verbosity*/2, /*indentation*/8);
616 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800617 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800618 }
619 }
620
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700621 mTagMonitor.dumpMonitoredMetadata(fd);
622
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800623 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800624 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800626 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800628
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629 if (gotLock) mLock.unlock();
630 if (gotInterfaceLock) mInterfaceLock.unlock();
631
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800632 return OK;
633}
634
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700635status_t Camera3Device::startWatchingTags(const String8 &tags) {
636 mTagMonitor.parseTagsToMonitor(tags);
637 return OK;
638}
639
640status_t Camera3Device::stopWatchingTags() {
641 mTagMonitor.disableMonitoring();
642 return OK;
643}
644
645status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
646 mTagMonitor.getLatestMonitoredTagEvents(out);
647 return OK;
648}
649
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800650const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800651 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
653 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700654 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 mStatus == STATUS_ERROR ?
656 "when in error state" : "before init");
657 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700658 if (physicalId.isEmpty()) {
659 return mDeviceInfo;
660 } else {
661 std::string id(physicalId.c_str());
662 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
663 return mPhysicalDeviceInfoMap.at(id);
664 } else {
665 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
666 return mDeviceInfo;
667 }
668 }
669}
670
671const CameraMetadata& Camera3Device::info() const {
672 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800673 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800674}
675
Jianing Wei90e59c92014-03-12 18:29:36 -0700676status_t Camera3Device::checkStatusOkToCaptureLocked() {
677 switch (mStatus) {
678 case STATUS_ERROR:
679 CLOGE("Device has encountered a serious error");
680 return INVALID_OPERATION;
681 case STATUS_UNINITIALIZED:
682 CLOGE("Device not initialized");
683 return INVALID_OPERATION;
684 case STATUS_UNCONFIGURED:
685 case STATUS_CONFIGURED:
686 case STATUS_ACTIVE:
687 // OK
688 break;
689 default:
690 SET_ERR_L("Unexpected status: %d", mStatus);
691 return INVALID_OPERATION;
692 }
693 return OK;
694}
695
696status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000697 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700698 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700699 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700700 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700701 if (requestList == NULL) {
702 CLOGE("requestList cannot be NULL.");
703 return BAD_VALUE;
704 }
705
Jianing Weicb0652e2014-03-12 18:29:36 -0700706 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000707 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700708 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
709 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
710 ++metadataIt, ++surfaceMapIt) {
711 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700712 if (newRequest == 0) {
713 CLOGE("Can't create capture request");
714 return BAD_VALUE;
715 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700716
Shuzhen Wang9d066012016-09-30 11:30:20 -0700717 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700718 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700719
Jianing Weicb0652e2014-03-12 18:29:36 -0700720 // Setup burst Id and request Id
721 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800722 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
723 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700724 CLOGE("RequestID does not exist in metadata");
725 return BAD_VALUE;
726 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800727 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700728
Jianing Wei90e59c92014-03-12 18:29:36 -0700729 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700730
731 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700732 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700733 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
734 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
735 return BAD_VALUE;
736 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700737
738 // Setup batch size if this is a high speed video recording request.
739 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
740 auto firstRequest = requestList->begin();
741 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
742 if (outputStream->isVideoStream()) {
743 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800744 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700745 break;
746 }
747 }
748 }
749
Jianing Wei90e59c92014-03-12 18:29:36 -0700750 return OK;
751}
752
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800753status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800754 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800755
Emilian Peevaebbe412018-01-15 13:53:24 +0000756 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700757 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000758 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700759
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800760 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700761}
762
Emilian Peevaebbe412018-01-15 13:53:24 +0000763void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764 std::list<const SurfaceMap>& surfaceMaps,
765 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000766 PhysicalCameraSettingsList requestList;
767 requestList.push_back({std::string(getId().string()), request});
768 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769
770 SurfaceMap surfaceMap;
771 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
772 // With no surface list passed in, stream and surface will have 1-to-1
773 // mapping. So the surface index is 0 for each stream in the surfaceMap.
774 for (size_t i = 0; i < streams.count; i++) {
775 surfaceMap[streams.data.i32[i]].push_back(0);
776 }
777 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800778}
779
Jianing Wei90e59c92014-03-12 18:29:36 -0700780status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000781 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700782 const std::list<const SurfaceMap> &surfaceMaps,
783 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700784 /*out*/
785 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700786 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700787 nsecs_t requestTimeNs = systemTime();
788
Jianing Wei90e59c92014-03-12 18:29:36 -0700789 Mutex::Autolock il(mInterfaceLock);
790 Mutex::Autolock l(mLock);
791
792 status_t res = checkStatusOkToCaptureLocked();
793 if (res != OK) {
794 // error logged by previous call
795 return res;
796 }
797
798 RequestList requestList;
799
Shuzhen Wang0129d522016-10-30 22:43:41 -0700800 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700801 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700802 if (res != OK) {
803 // error logged by previous call
804 return res;
805 }
806
807 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700808 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700809 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700810 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700811 }
812
813 if (res == OK) {
814 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
815 if (res != OK) {
816 SET_ERR_L("Can't transition to active in %f seconds!",
817 kActiveTimeout/1e9);
818 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800819 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700820 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700821 } else {
822 CLOGE("Cannot queue request. Impossible.");
823 return BAD_VALUE;
824 }
825
826 return res;
827}
828
Emilian Peevaebbe412018-01-15 13:53:24 +0000829status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700830 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700831 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700832 ATRACE_CALL();
833
Emilian Peevaebbe412018-01-15 13:53:24 +0000834 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700835}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800836
Jianing Weicb0652e2014-03-12 18:29:36 -0700837status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
838 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800839 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Emilian Peevaebbe412018-01-15 13:53:24 +0000841 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700842 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000843 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700844
Emilian Peevaebbe412018-01-15 13:53:24 +0000845 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700846 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847}
848
Emilian Peevaebbe412018-01-15 13:53:24 +0000849status_t Camera3Device::setStreamingRequestList(
850 const List<const PhysicalCameraSettingsList> &requestsList,
851 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700852 ATRACE_CALL();
853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700855}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856
857sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000858 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800859 status_t res;
860
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700861 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800862 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
863 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000864 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700865 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700866 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700868 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700870 } else if (mStatus == STATUS_UNCONFIGURED) {
871 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 CLOGE("No streams configured");
873 return NULL;
874 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 }
876
Shuzhen Wang0129d522016-10-30 22:43:41 -0700877 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800879}
880
Jianing Weicb0652e2014-03-12 18:29:36 -0700881status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800882 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700883 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800885
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 switch (mStatus) {
887 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 return INVALID_OPERATION;
890 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700891 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 case STATUS_UNCONFIGURED:
894 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 case STATUS_ACTIVE:
896 // OK
897 break;
898 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700899 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 return INVALID_OPERATION;
901 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800902 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700903
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700904 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800905}
906
907status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
908 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700909 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700911 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800912}
913
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700914status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800915 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700916 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700917 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700918 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700919 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800920 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
921 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700922
923 status_t res;
924 bool wasActive = false;
925
926 switch (mStatus) {
927 case STATUS_ERROR:
928 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
929 return INVALID_OPERATION;
930 case STATUS_UNINITIALIZED:
931 ALOGE("%s: Device not initialized", __FUNCTION__);
932 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700933 case STATUS_UNCONFIGURED:
934 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700935 // OK
936 break;
937 case STATUS_ACTIVE:
938 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700939 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700940 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700942 return res;
943 }
944 wasActive = true;
945 break;
946 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700948 return INVALID_OPERATION;
949 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700950 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700951
952 if (mInputStream != 0) {
953 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
954 return INVALID_OPERATION;
955 }
956
957 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
958 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700960
961 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800962 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700963
964 *id = mNextStreamId++;
965
966 // Continue captures if active at start
967 if (wasActive) {
968 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100969 // Reuse current operating mode and session parameters for new stream config
970 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700971 if (res != OK) {
972 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
973 __FUNCTION__, mNextStreamId, strerror(-res), res);
974 return res;
975 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700976 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700977 }
978
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800979 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700980 return OK;
981}
982
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700983status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700984 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700985 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800986 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800987 const std::unordered_set<int32_t> &sensorPixelModesUsed,
988 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800989 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800990 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700991 ATRACE_CALL();
992
993 if (consumer == nullptr) {
994 ALOGE("%s: consumer must not be null", __FUNCTION__);
995 return BAD_VALUE;
996 }
997
998 std::vector<sp<Surface>> consumers;
999 consumers.push_back(consumer);
1000
1001 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001002 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001003 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001004 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001005}
1006
1007static bool isRawFormat(int format) {
1008 switch (format) {
1009 case HAL_PIXEL_FORMAT_RAW16:
1010 case HAL_PIXEL_FORMAT_RAW12:
1011 case HAL_PIXEL_FORMAT_RAW10:
1012 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1013 return true;
1014 default:
1015 return false;
1016 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001017}
1018
1019status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1020 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001021 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001022 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001023 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001024 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1025 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001026 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001027
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001028 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001029 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001030 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001031 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001032 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001033 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1034 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001035 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001036 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001037 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001038
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 status_t res;
1040 bool wasActive = false;
1041
1042 switch (mStatus) {
1043 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001044 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001045 return INVALID_OPERATION;
1046 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001047 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001049 case STATUS_UNCONFIGURED:
1050 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 // OK
1052 break;
1053 case STATUS_ACTIVE:
1054 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001055 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001057 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 return res;
1059 }
1060 wasActive = true;
1061 break;
1062 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001063 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 return INVALID_OPERATION;
1065 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001066 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067
1068 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001069
Shuzhen Wang0129d522016-10-30 22:43:41 -07001070 if (consumers.size() == 0 && !hasDeferredConsumer) {
1071 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1072 return BAD_VALUE;
1073 }
Zhijun He5d677d12016-05-29 16:52:39 -07001074
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001076 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1077 return BAD_VALUE;
1078 }
1079
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001080 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1081 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1082 // be found in only one sensor pixel mode.
1083 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1084 return BAD_VALUE;
1085 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001086 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001088 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001089 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001090 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001091 if (blobBufferSize <= 0) {
1092 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1093 return BAD_VALUE;
1094 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001095 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1096 blobBufferSize = width * height;
1097 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001098 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001099 if (blobBufferSize <= 0) {
1100 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1101 return BAD_VALUE;
1102 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001103 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001104 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001105 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001106 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001107 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001108 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001109 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001110 bool maxResolution =
1111 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1112 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001113 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1114 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001115 if (rawOpaqueBufferSize <= 0) {
1116 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1117 return BAD_VALUE;
1118 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001119 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001120 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001121 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001122 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001123 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001124 } else if (isShared) {
1125 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1126 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001127 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001128 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001129 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001130 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001131 newStream = new Camera3OutputStream(mNextStreamId,
1132 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001133 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001134 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001135 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001137 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001138 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001139 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001140 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001141 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001142 }
Emilian Peev40ead602017-09-26 15:46:36 +01001143
1144 size_t consumerCount = consumers.size();
1145 for (size_t i = 0; i < consumerCount; i++) {
1146 int id = newStream->getSurfaceId(consumers[i]);
1147 if (id < 0) {
1148 SET_ERR_L("Invalid surface id");
1149 return BAD_VALUE;
1150 }
1151 if (surfaceIds != nullptr) {
1152 surfaceIds->push_back(id);
1153 }
1154 }
1155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157
Emilian Peev08dd2452017-04-06 16:55:14 +01001158 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001159
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001160 newStream->setImageDumpMask(mImageDumpMask);
1161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 res = mOutputStreams.add(mNextStreamId, newStream);
1163 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001164 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 return res;
1166 }
1167
Shuzhen Wang316781a2020-08-18 18:11:01 -07001168 mSessionStatsBuilder.addStream(mNextStreamId);
1169
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001171 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001172
1173 // Continue captures if active at start
1174 if (wasActive) {
1175 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001176 // Reuse current operating mode and session parameters for new stream config
1177 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001178 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001179 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1180 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001181 return res;
1182 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001183 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001185 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001187}
1188
Emilian Peev710c1422017-08-30 11:19:38 +01001189status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001191 if (nullptr == streamInfo) {
1192 return BAD_VALUE;
1193 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001194 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 switch (mStatus) {
1198 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001199 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return INVALID_OPERATION;
1201 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001204 case STATUS_UNCONFIGURED:
1205 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001206 case STATUS_ACTIVE:
1207 // OK
1208 break;
1209 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001210 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 return INVALID_OPERATION;
1212 }
1213
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001214 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1215 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001217 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 }
1219
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001220 streamInfo->width = stream->getWidth();
1221 streamInfo->height = stream->getHeight();
1222 streamInfo->format = stream->getFormat();
1223 streamInfo->dataSpace = stream->getDataSpace();
1224 streamInfo->formatOverridden = stream->isFormatOverridden();
1225 streamInfo->originalFormat = stream->getOriginalFormat();
1226 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1227 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001228 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001230}
1231
1232status_t Camera3Device::setStreamTransform(int id,
1233 int transform) {
1234 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001235 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 switch (mStatus) {
1239 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001240 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 return INVALID_OPERATION;
1242 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001243 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001244 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 case STATUS_UNCONFIGURED:
1246 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 case STATUS_ACTIVE:
1248 // OK
1249 break;
1250 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001251 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 return INVALID_OPERATION;
1253 }
1254
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001255 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1256 if (stream == nullptr) {
1257 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 return BAD_VALUE;
1259 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001260 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001261}
1262
1263status_t Camera3Device::deleteStream(int id) {
1264 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001265 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 Mutex::Autolock l(mLock);
1267 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001269 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001270
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 // CameraDevice semantics require device to already be idle before
1272 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001273 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001274 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001275 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001276 }
1277
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001278 if (mStatus == STATUS_ERROR) {
1279 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1280 __FUNCTION__, mId.string());
1281 return -EBUSY;
1282 }
1283
Igor Murashkin2fba5842013-04-22 14:03:54 -07001284 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001285 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 if (mInputStream != NULL && id == mInputStream->getId()) {
1287 deletedStream = mInputStream;
1288 mInputStream.clear();
1289 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001290 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001291 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 return BAD_VALUE;
1293 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001294 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001295 }
1296
1297 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001298 if (stream != nullptr) {
1299 deletedStream = stream;
1300 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301 }
1302
1303 // Free up the stream endpoint so that it can be used by some other stream
1304 res = deletedStream->disconnect();
1305 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001306 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001307 // fall through since we want to still list the stream as deleted.
1308 }
1309 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001310 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001311
1312 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313}
1314
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001315status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001316 ATRACE_CALL();
1317 ALOGV("%s: E", __FUNCTION__);
1318
1319 Mutex::Autolock il(mInterfaceLock);
1320 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001321
Emilian Peev811d2952018-05-25 11:08:40 +01001322 // In case the client doesn't include any session parameter, try a
1323 // speculative configuration using the values from the last cached
1324 // default request.
1325 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001326 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001327 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1328 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1329 mLastTemplateId);
1330 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1331 operatingMode);
1332 }
1333
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001334 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1335}
1336
1337status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1338 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001339 //Filter out any incoming session parameters
1340 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001341 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1342 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001343 CameraMetadata filteredParams(availableSessionKeys.count);
1344 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1345 filteredParams.getAndLock());
1346 set_camera_metadata_vendor_id(meta, mVendorTagId);
1347 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001348 if (availableSessionKeys.count > 0) {
1349 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1350 camera_metadata_ro_entry entry = params.find(
1351 availableSessionKeys.data.i32[i]);
1352 if (entry.count > 0) {
1353 filteredParams.update(entry);
1354 }
1355 }
1356 }
1357
1358 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001359}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001360
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001361status_t Camera3Device::getInputBufferProducer(
1362 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001363 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001364 Mutex::Autolock il(mInterfaceLock);
1365 Mutex::Autolock l(mLock);
1366
1367 if (producer == NULL) {
1368 return BAD_VALUE;
1369 } else if (mInputStream == NULL) {
1370 return INVALID_OPERATION;
1371 }
1372
1373 return mInputStream->getInputBufferProducer(producer);
1374}
1375
Emilian Peevf4816702020-04-03 15:44:51 -07001376status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001377 CameraMetadata *request) {
1378 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001379 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001380
Emilian Peevf4816702020-04-03 15:44:51 -07001381 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001382 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001383 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001384 return BAD_VALUE;
1385 }
1386
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001387 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001389 {
1390 Mutex::Autolock l(mLock);
1391 switch (mStatus) {
1392 case STATUS_ERROR:
1393 CLOGE("Device has encountered a serious error");
1394 return INVALID_OPERATION;
1395 case STATUS_UNINITIALIZED:
1396 CLOGE("Device is not initialized!");
1397 return INVALID_OPERATION;
1398 case STATUS_UNCONFIGURED:
1399 case STATUS_CONFIGURED:
1400 case STATUS_ACTIVE:
1401 // OK
1402 break;
1403 default:
1404 SET_ERR_L("Unexpected status: %d", mStatus);
1405 return INVALID_OPERATION;
1406 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001407
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001408 if (!mRequestTemplateCache[templateId].isEmpty()) {
1409 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001410 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001411 return OK;
1412 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001413 }
1414
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001415 camera_metadata_t *rawRequest;
1416 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001417 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001418
1419 {
1420 Mutex::Autolock l(mLock);
1421 if (res == BAD_VALUE) {
1422 ALOGI("%s: template %d is not supported on this camera device",
1423 __FUNCTION__, templateId);
1424 return res;
1425 } else if (res != OK) {
1426 CLOGE("Unable to construct request template %d: %s (%d)",
1427 templateId, strerror(-res), res);
1428 return res;
1429 }
1430
1431 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1432 mRequestTemplateCache[templateId].acquire(rawRequest);
1433
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001434 // Override the template request with zoomRatioMapper
1435 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1436 &mRequestTemplateCache[templateId]);
1437 if (res != OK) {
1438 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1439 templateId, strerror(-res), res);
1440 return res;
1441 }
1442
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001443 // Fill in JPEG_QUALITY if not available
1444 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1445 static const uint8_t kDefaultJpegQuality = 95;
1446 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1447 &kDefaultJpegQuality, 1);
1448 }
1449
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001450 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001451 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001452 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001453 return OK;
1454}
1455
1456status_t Camera3Device::waitUntilDrained() {
1457 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001458 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001459 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001461
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001462 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001463}
1464
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001465status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 switch (mStatus) {
1467 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001468 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469 ALOGV("%s: Already idle", __FUNCTION__);
1470 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001471 case STATUS_CONFIGURED:
1472 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473 case STATUS_ERROR:
1474 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001475 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001476 break;
1477 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001478 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479 return INVALID_OPERATION;
1480 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001481 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1482 maxExpectedDuration);
1483 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001484 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001485 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001486 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1487 res);
1488 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001489 return res;
1490}
1491
Ruben Brunk183f0562015-08-12 12:55:02 -07001492void Camera3Device::internalUpdateStatusLocked(Status status) {
1493 mStatus = status;
1494 mRecentStatusUpdates.add(mStatus);
1495 mStatusChanged.broadcast();
1496}
1497
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001498// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001499status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001500 if (mRequestThread.get() != nullptr) {
1501 mRequestThread->setPaused(true);
1502 } else {
1503 return NO_INIT;
1504 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001505
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001506 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1507 maxExpectedDuration);
1508 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001509 if (res != OK) {
1510 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001511 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001512 }
1513
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001514 return res;
1515}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001516
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517// Resume after internalPauseAndWaitLocked
1518status_t Camera3Device::internalResumeLocked() {
1519 status_t res;
1520
1521 mRequestThread->setPaused(false);
1522
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001523 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1524 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001525 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1526 if (res != OK) {
1527 SET_ERR_L("Can't transition to active in %f seconds!",
1528 kActiveTimeout/1e9);
1529 }
1530 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001531 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001532}
1533
Ruben Brunk183f0562015-08-12 12:55:02 -07001534status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001535 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001536
1537 size_t startIndex = 0;
1538 if (mStatusWaiters == 0) {
1539 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1540 // this status list
1541 mRecentStatusUpdates.clear();
1542 } else {
1543 // If other threads are waiting on updates to this status list, set the position of the
1544 // first element that this list will check rather than clearing the list.
1545 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001546 }
1547
Ruben Brunk183f0562015-08-12 12:55:02 -07001548 mStatusWaiters++;
1549
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001550 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001551 if (!active && mUseHalBufManager) {
1552 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001553 if (mStatus == STATUS_ACTIVE) {
1554 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001555 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001556 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001557 mRequestBufferSM.onWaitUntilIdle();
1558 }
1559
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001560 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001561 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001562 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001563 if (active == (mStatus == STATUS_ACTIVE)) {
1564 // Desired state is current
1565 break;
1566 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001568 nsecs_t timeElapsed = systemTime() - startTime;
1569 nsecs_t timeToWait = timeout - timeElapsed;
1570 if (timeToWait <= 0) {
1571 // Thread woke up spuriously but has timed out since.
1572 // Force out of loop with TIMED_OUT result.
1573 res = TIMED_OUT;
1574 break;
1575 }
1576 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577 if (res != OK) break;
1578
Ruben Brunk183f0562015-08-12 12:55:02 -07001579 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1580 // transitions.
1581 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1582 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1583 __FUNCTION__);
1584
1585 // Encountered desired state since we began waiting
1586 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1588 stateSeen = true;
1589 break;
1590 }
1591 }
1592 } while (!stateSeen);
1593
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001594 if (signalPipelineDrain) {
1595 mRequestThread->resetPipelineDrain();
1596 }
1597
Ruben Brunk183f0562015-08-12 12:55:02 -07001598 mStatusWaiters--;
1599
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001600 return res;
1601}
1602
1603
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001604status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001605 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001606 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001607
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001608 if (listener != NULL && mListener != NULL) {
1609 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1610 }
1611 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001612 mRequestThread->setNotificationListener(listener);
1613 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001614
1615 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001616}
1617
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001618bool Camera3Device::willNotify3A() {
1619 return false;
1620}
1621
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001622status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001623 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001624 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001625
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001626 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001627 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1628 if (st == std::cv_status::timeout) {
1629 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001630 }
1631 }
1632 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001633}
1634
Jianing Weicb0652e2014-03-12 18:29:36 -07001635status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001636 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001637 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001639 if (mResultQueue.empty()) {
1640 return NOT_ENOUGH_DATA;
1641 }
1642
Jianing Weicb0652e2014-03-12 18:29:36 -07001643 if (frame == NULL) {
1644 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1645 return BAD_VALUE;
1646 }
1647
1648 CaptureResult &result = *(mResultQueue.begin());
1649 frame->mResultExtras = result.mResultExtras;
1650 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001651 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001652 mResultQueue.erase(mResultQueue.begin());
1653
1654 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001655}
1656
1657status_t Camera3Device::triggerAutofocus(uint32_t id) {
1658 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001659 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001661 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1662 // Mix-in this trigger into the next request and only the next request.
1663 RequestTrigger trigger[] = {
1664 {
1665 ANDROID_CONTROL_AF_TRIGGER,
1666 ANDROID_CONTROL_AF_TRIGGER_START
1667 },
1668 {
1669 ANDROID_CONTROL_AF_TRIGGER_ID,
1670 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001671 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001672 };
1673
1674 return mRequestThread->queueTrigger(trigger,
1675 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001676}
1677
1678status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1679 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001680 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001681
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001682 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1683 // Mix-in this trigger into the next request and only the next request.
1684 RequestTrigger trigger[] = {
1685 {
1686 ANDROID_CONTROL_AF_TRIGGER,
1687 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1688 },
1689 {
1690 ANDROID_CONTROL_AF_TRIGGER_ID,
1691 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001692 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001693 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001694
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001695 return mRequestThread->queueTrigger(trigger,
1696 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001697}
1698
1699status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1700 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001701 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001702
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001703 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1704 // Mix-in this trigger into the next request and only the next request.
1705 RequestTrigger trigger[] = {
1706 {
1707 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1708 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1709 },
1710 {
1711 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1712 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001713 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001714 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001715
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001716 return mRequestThread->queueTrigger(trigger,
1717 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001718}
1719
Jianing Weicb0652e2014-03-12 18:29:36 -07001720status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001721 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001722 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001724
Zhijun He7ef20392014-04-21 16:04:17 -07001725 {
1726 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001727
1728 // b/116514106 "disconnect()" can get called twice for the same device. The
1729 // camera device will not be initialized during the second run.
1730 if (mStatus == STATUS_UNINITIALIZED) {
1731 return OK;
1732 }
1733
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001734 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001735
1736 // Stop session and stream counter
1737 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001738 }
1739
Ravneetdbd5b242022-03-02 07:22:46 +00001740 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1741 uint64_t maxExpectedDuration = (getExpectedInFlightDuration() + kBaseGetBufferWait) / 1e6;
1742 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1743 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1744
1745 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001746}
1747
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001748status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001749 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1750}
1751
1752status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001753 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001754 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001755 Mutex::Autolock il(mInterfaceLock);
1756 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001757
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001758 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1759 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001760 CLOGE("Stream %d does not exist", streamId);
1761 return BAD_VALUE;
1762 }
1763
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001764 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001765 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001766 return BAD_VALUE;
1767 }
1768
1769 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001770 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001771 return BAD_VALUE;
1772 }
1773
Ruben Brunkc78ac262015-08-13 17:58:46 -07001774 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001775}
1776
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001777status_t Camera3Device::tearDown(int streamId) {
1778 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001779 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001780 Mutex::Autolock il(mInterfaceLock);
1781 Mutex::Autolock l(mLock);
1782
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001783 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1784 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001785 CLOGE("Stream %d does not exist", streamId);
1786 return BAD_VALUE;
1787 }
1788
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001789 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1790 CLOGE("Stream %d is a target of a in-progress request", streamId);
1791 return BAD_VALUE;
1792 }
1793
1794 return stream->tearDown();
1795}
1796
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001797status_t Camera3Device::addBufferListenerForStream(int streamId,
1798 wp<Camera3StreamBufferListener> listener) {
1799 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001800 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001801 Mutex::Autolock il(mInterfaceLock);
1802 Mutex::Autolock l(mLock);
1803
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001804 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1805 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001806 CLOGE("Stream %d does not exist", streamId);
1807 return BAD_VALUE;
1808 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001809 stream->addBufferListener(listener);
1810
1811 return OK;
1812}
1813
Austin Borger4a870a32022-02-25 01:48:41 +00001814float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1815 camera_metadata_entry minDurations =
1816 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1817 for (size_t i = 0; i < minDurations.count; i += 4) {
1818 if (minDurations.data.i64[i] == stream->getFormat()
1819 && minDurations.data.i64[i+1] == stream->getWidth()
1820 && minDurations.data.i64[i+2] == stream->getHeight()) {
1821 int64_t minFrameDuration = minDurations.data.i64[i+3];
1822 return 1e9f / minFrameDuration;
1823 }
1824 }
1825 return 0.0f;
1826}
1827
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001829 * Methods called by subclasses
1830 */
1831
1832void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001833 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001834 std::vector<int> streamIds;
1835 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001836 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001837
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001838 {
1839 // Need mLock to safely update state and synchronize to current
1840 // state of methods in flight.
1841 Mutex::Autolock l(mLock);
1842 // We can get various system-idle notices from the status tracker
1843 // while starting up. Only care about them if we've actually sent
1844 // in some requests recently.
1845 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1846 return;
1847 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001848 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1849 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001850 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001851
1852 // Skip notifying listener if we're doing some user-transparent
1853 // state changes
1854 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001855
Austin Borger4a870a32022-02-25 01:48:41 +00001856 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1857 auto stream = mOutputStreams[i];
1858 if (stream.get() == nullptr) continue;
1859
1860 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1861 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1862
1863 // Populate stream statistics in case of Idle
1864 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001865 streamIds.push_back(stream->getId());
1866 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1867 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001868 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001869 if (camera3Stream != nullptr) {
1870 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001871 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001872 }
1873 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001874 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001875 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001876 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001877 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001878 }
1879 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001880 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001881
1882 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001883 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001884 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001885 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001887 status_t res = OK;
1888 if (listener != nullptr) {
1889 if (idle) {
1890 // Get session stats from the builder, and notify the listener.
1891 int64_t requestCount, resultErrorCount;
1892 bool deviceError;
1893 std::map<int, StreamStats> streamStatsMap;
1894 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1895 &deviceError, &streamStatsMap);
1896 for (size_t i = 0; i < streamIds.size(); i++) {
1897 int streamId = streamIds[i];
1898 auto stats = streamStatsMap.find(streamId);
1899 if (stats != streamStatsMap.end()) {
1900 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1901 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1902 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1903 streamStats[i].mHistogramType =
1904 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1905 streamStats[i].mHistogramBins.assign(
1906 stats->second.mCaptureLatencyBins.begin(),
1907 stats->second.mCaptureLatencyBins.end());
1908 streamStats[i].mHistogramCounts.assign(
1909 stats->second.mCaptureLatencyHistogram.begin(),
1910 stats->second.mCaptureLatencyHistogram.end());
1911 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001912 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001913 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1914 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001915 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001916 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001917 }
1918 if (res != OK) {
1919 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1920 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001921 }
1922}
1923
Shuzhen Wang758c2152017-01-10 18:26:18 -08001924status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001925 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001926 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001927 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1928 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001929
1930 if (surfaceIds == nullptr) {
1931 return BAD_VALUE;
1932 }
1933
Zhijun He5d677d12016-05-29 16:52:39 -07001934 Mutex::Autolock il(mInterfaceLock);
1935 Mutex::Autolock l(mLock);
1936
Shuzhen Wang758c2152017-01-10 18:26:18 -08001937 if (consumers.size() == 0) {
1938 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001939 return BAD_VALUE;
1940 }
1941
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001942 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1943 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001944 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001945 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001946 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001947
1948 // isConsumerConfigurationDeferred will be off after setConsumers
1949 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001950 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001951 if (res != OK) {
1952 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1953 return res;
1954 }
1955
Emilian Peev40ead602017-09-26 15:46:36 +01001956 for (auto &consumer : consumers) {
1957 int id = stream->getSurfaceId(consumer);
1958 if (id < 0) {
1959 CLOGE("Invalid surface id!");
1960 return BAD_VALUE;
1961 }
1962 surfaceIds->push_back(id);
1963 }
1964
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001965 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001966 if (!stream->isConfiguring()) {
1967 CLOGE("Stream %d was already fully configured.", streamId);
1968 return INVALID_OPERATION;
1969 }
Zhijun He5d677d12016-05-29 16:52:39 -07001970
Shuzhen Wang0129d522016-10-30 22:43:41 -07001971 res = stream->finishConfiguration();
1972 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001973 // If finishConfiguration fails due to abandoned surface, do not set
1974 // device to error state.
1975 bool isSurfaceAbandoned =
1976 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1977 if (!isSurfaceAbandoned) {
1978 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1979 stream->getId(), strerror(-res), res);
1980 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001981 return res;
1982 }
Zhijun He5d677d12016-05-29 16:52:39 -07001983 }
1984
1985 return OK;
1986}
1987
Emilian Peev40ead602017-09-26 15:46:36 +01001988status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1989 const std::vector<OutputStreamInfo> &outputInfo,
1990 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1991 Mutex::Autolock il(mInterfaceLock);
1992 Mutex::Autolock l(mLock);
1993
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001994 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1995 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01001996 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001997 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01001998 }
1999
2000 for (const auto &it : removedSurfaceIds) {
2001 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2002 CLOGE("Shared surface still part of a pending request!");
2003 return -EBUSY;
2004 }
2005 }
2006
Emilian Peev40ead602017-09-26 15:46:36 +01002007 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2008 if (res != OK) {
2009 CLOGE("Stream %d failed to update stream (error %d %s) ",
2010 streamId, res, strerror(-res));
2011 if (res == UNKNOWN_ERROR) {
2012 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2013 __FUNCTION__);
2014 }
2015 return res;
2016 }
2017
2018 return res;
2019}
2020
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002021status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2022 Mutex::Autolock il(mInterfaceLock);
2023 Mutex::Autolock l(mLock);
2024
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002025 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2026 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002027 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2028 return BAD_VALUE;
2029 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002030
2031 if (dropping) {
2032 mSessionStatsBuilder.stopCounter(streamId);
2033 } else {
2034 mSessionStatsBuilder.startCounter(streamId);
2035 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002036 return stream->dropBuffers(dropping);
2037}
2038
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002039/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 * Camera3Device private methods
2041 */
2042
2043sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002044 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002046
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002047 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002048 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002049
2050 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002051 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052 if (inputStreams.count > 0) {
2053 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002054 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002055 CLOGE("Request references unknown input stream %d",
2056 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057 return NULL;
2058 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002059
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002060 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002061 SET_ERR_L("%s: input stream %d is not configured!",
2062 __FUNCTION__, mInputStream->getId());
2063 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002065 // Check if stream prepare is blocking requests.
2066 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002067 CLOGE("Request references an input stream that's being prepared!");
2068 return NULL;
2069 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070
2071 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002072 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073 }
2074
2075 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002076 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002078 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002079 return NULL;
2080 }
2081
2082 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002083 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2084 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002085 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002086 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087 return NULL;
2088 }
Zhijun He5d677d12016-05-29 16:52:39 -07002089 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002090 auto iter = surfaceMap.find(streams.data.i32[i]);
2091 if (iter != surfaceMap.end()) {
2092 const std::vector<size_t>& surfaces = iter->second;
2093 for (const auto& surface : surfaces) {
2094 if (stream->isConsumerConfigurationDeferred(surface)) {
2095 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2096 "due to deferred consumer", stream->getId(), surface);
2097 return NULL;
2098 }
2099 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002100 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002101 }
2102
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002103 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002104 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2105 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002106 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002107 // Check if stream prepare is blocking requests.
2108 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002109 CLOGE("Request references an output stream that's being prepared!");
2110 return NULL;
2111 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112
2113 newRequest->mOutputStreams.push(stream);
2114 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002115 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002116 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002117
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002118 auto rotateAndCropEntry =
2119 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2120 if (rotateAndCropEntry.count > 0 &&
2121 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2122 newRequest->mRotateAndCropAuto = true;
2123 } else {
2124 newRequest->mRotateAndCropAuto = false;
2125 }
2126
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002127 auto zoomRatioEntry =
2128 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2129 if (zoomRatioEntry.count > 0 &&
2130 zoomRatioEntry.data.f[0] == 1.0f) {
2131 newRequest->mZoomRatioIs1x = true;
2132 } else {
2133 newRequest->mZoomRatioIs1x = false;
2134 }
2135
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002136 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002137 for (auto& settings : newRequest->mSettingsList) {
2138 auto testPatternModeEntry =
2139 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2140 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2141 testPatternModeEntry.data.i32[0] :
2142 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002143
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002144 auto testPatternDataEntry =
2145 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2146 if (testPatternDataEntry.count >= 4) {
2147 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2148 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2149 } else {
2150 settings.mOriginalTestPatternData[0] = 0;
2151 settings.mOriginalTestPatternData[1] = 0;
2152 settings.mOriginalTestPatternData[2] = 0;
2153 settings.mOriginalTestPatternData[3] = 0;
2154 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002155 }
2156 }
2157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002158 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002159}
2160
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002161void Camera3Device::cancelStreamsConfigurationLocked() {
2162 int res = OK;
2163 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2164 res = mInputStream->cancelConfiguration();
2165 if (res != OK) {
2166 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2167 mInputStream->getId(), strerror(-res), res);
2168 }
2169 }
2170
2171 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002172 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002173 if (outputStream->isConfiguring()) {
2174 res = outputStream->cancelConfiguration();
2175 if (res != OK) {
2176 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2177 outputStream->getId(), strerror(-res), res);
2178 }
2179 }
2180 }
2181
2182 // Return state to that at start of call, so that future configures
2183 // properly clean things up
2184 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2185 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002186
2187 res = mPreparerThread->resume();
2188 if (res != OK) {
2189 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2190 }
2191}
2192
Emilian Peev0d0191e2020-04-21 17:01:18 -07002193bool Camera3Device::checkAbandonedStreamsLocked() {
2194 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2195 return true;
2196 }
2197
2198 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2199 auto stream = mOutputStreams[i];
2200 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2201 return true;
2202 }
2203 }
2204
2205 return false;
2206}
2207
Emilian Peev3bead5f2020-05-28 17:29:08 -07002208bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002209 ATRACE_CALL();
2210 bool ret = false;
2211
Shuzhen Wang316781a2020-08-18 18:11:01 -07002212 nsecs_t startTime = systemTime();
2213
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002214 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002215 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2216
2217 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002218 if (checkAbandonedStreamsLocked()) {
2219 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2220 __FUNCTION__);
2221 return true;
2222 }
2223
Emilian Peev3bead5f2020-05-28 17:29:08 -07002224 status_t rc = NO_ERROR;
2225 bool markClientActive = false;
2226 if (mStatus == STATUS_ACTIVE) {
2227 markClientActive = true;
2228 mPauseStateNotify = true;
2229 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2230
2231 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2232 }
2233
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002234 if (rc == NO_ERROR) {
2235 mNeedConfig = true;
2236 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2237 if (rc == NO_ERROR) {
2238 ret = true;
2239 mPauseStateNotify = false;
2240 //Moving to active state while holding 'mLock' is important.
2241 //There could be pending calls to 'create-/deleteStream' which
2242 //will trigger another stream configuration while the already
2243 //present streams end up with outstanding buffers that will
2244 //not get drained.
2245 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002246 } else if (rc == DEAD_OBJECT) {
2247 // DEAD_OBJECT can be returned if either the consumer surface is
2248 // abandoned, or the HAL has died.
2249 // - If the HAL has died, configureStreamsLocked call will set
2250 // device to error state,
2251 // - If surface is abandoned, we should not set device to error
2252 // state.
2253 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002254 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002255 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002256 }
2257 } else {
2258 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2259 }
2260
Austin Borger74fca042022-05-23 12:41:21 -07002261 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002262 ns2ms(systemTime() - startTime));
2263
Emilian Peev3bead5f2020-05-28 17:29:08 -07002264 if (markClientActive) {
2265 mStatusTracker->markComponentActive(clientStatusId);
2266 }
2267
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002268 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002269}
2270
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002271status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002272 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002273 ATRACE_CALL();
2274 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002275
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002276 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002277 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278 return INVALID_OPERATION;
2279 }
2280
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002281 if (operatingMode < 0) {
2282 CLOGE("Invalid operating mode: %d", operatingMode);
2283 return BAD_VALUE;
2284 }
2285
2286 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002287 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002288
2289 if (mOperatingMode != operatingMode) {
2290 mNeedConfig = true;
2291 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2292 mOperatingMode = operatingMode;
2293 }
2294
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002295 // In case called from configureStreams, abort queued input buffers not belonging to
2296 // any pending requests.
2297 if (mInputStream != NULL && notifyRequestThread) {
2298 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002299 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002300 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002301 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002302 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002303 if (res != OK) {
2304 // Exhausted acquiring all input buffers.
2305 break;
2306 }
2307
Emilian Peevf4816702020-04-03 15:44:51 -07002308 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002309 res = mInputStream->returnInputBuffer(inputBuffer);
2310 if (res != OK) {
2311 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2312 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2313 }
2314 }
2315 }
2316
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002317 if (!mNeedConfig) {
2318 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2319 return OK;
2320 }
2321
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002322 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002323 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002324 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2325 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002326 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002327 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002328 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002329 }
2330
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002331 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002332 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002334 mPreparerThread->pause();
2335
Emilian Peevf4816702020-04-03 15:44:51 -07002336 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002337 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002338 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002339 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340
Emilian Peevf4816702020-04-03 15:44:51 -07002341 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002342 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002343 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002344
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345
2346 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002347 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002348 inputStream = mInputStream->startConfiguration();
2349 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002350 CLOGE("Can't start input stream configuration");
2351 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002352 return INVALID_OPERATION;
2353 }
2354 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002355
2356 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002357 }
2358
Shuzhen Wang99080502021-03-07 21:08:20 -08002359 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002360 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002361 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002362
2363 // Don't configure bidi streams twice, nor add them twice to the list
2364 if (mOutputStreams[i].get() ==
2365 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2366
2367 config.num_streams--;
2368 continue;
2369 }
2370
Emilian Peevf4816702020-04-03 15:44:51 -07002371 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002372 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002374 CLOGE("Can't start output stream configuration");
2375 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 return INVALID_OPERATION;
2377 }
2378 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002379
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002380 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002381 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2382 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002383 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2384 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002385 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2386 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002387 } else if (outputStream->data_space ==
2388 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2389 bufferSizes[k] = outputStream->width * outputStream->height;
2390 } else {
2391 ALOGW("%s: Blob dataSpace %d not supported",
2392 __FUNCTION__, outputStream->data_space);
2393 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002394 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002395
2396 if (mOutputStreams[i]->isMultiResolution()) {
2397 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2398 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2399 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2400 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002401
2402 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2403 composerSurfacePresent = true;
2404 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002405 }
2406
2407 config.streams = streams.editArray();
2408
2409 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002410 // max_buffers, usage, and priv fields, as well as data_space and format
2411 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002412
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002413 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002414 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002415 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002416
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002417 if (res == BAD_VALUE) {
2418 // HAL rejected this set of streams as unsupported, clean up config
2419 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002420 CLOGE("Set of requested inputs/outputs not supported by HAL");
2421 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002422 return BAD_VALUE;
2423 } else if (res != OK) {
2424 // Some other kind of error from configure_streams - this is not
2425 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002426 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2427 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002428 return res;
2429 }
2430
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002431 // Finish all stream configuration immediately.
2432 // TODO: Try to relax this later back to lazy completion, which should be
2433 // faster
2434
Igor Murashkin073f8572013-05-02 14:59:28 -07002435 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002436 bool streamReConfigured = false;
2437 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002438 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002439 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002440 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002441 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002442 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2443 return DEAD_OBJECT;
2444 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002445 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002446 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002447 if (streamReConfigured) {
2448 mInterface->onStreamReConfigured(mInputStream->getId());
2449 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002450 }
2451
2452 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002453 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002454 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002455 bool streamReConfigured = false;
2456 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002457 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002458 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002459 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002460 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002461 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2462 return DEAD_OBJECT;
2463 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002464 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002465 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002466 if (streamReConfigured) {
2467 mInterface->onStreamReConfigured(outputStream->getId());
2468 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002469 }
2470 }
2471
Emilian Peeve23f1d92021-09-20 14:56:01 -07002472 mRequestThread->setComposerSurface(composerSurfacePresent);
2473
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002474 // Request thread needs to know to avoid using repeat-last-settings protocol
2475 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002476 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002477 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2478 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002479 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480
Zhijun He90f7c372016-08-16 16:19:43 -07002481 char value[PROPERTY_VALUE_MAX];
2482 property_get("camera.fifo.disable", value, "0");
2483 int32_t disableFifo = atoi(value);
2484 if (disableFifo != 1) {
2485 // Boost priority of request thread to SCHED_FIFO.
2486 pid_t requestThreadTid = mRequestThread->getTid();
2487 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002488 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002489 if (res != OK) {
2490 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2491 strerror(-res), res);
2492 } else {
2493 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2494 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002495 }
2496
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002497 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002498 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2499 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2500 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2501 sessionParams.unlock(newSessionParams);
2502 mSessionParams.unlock(currentSessionParams);
2503 if (updateSessionParams) {
2504 mSessionParams = sessionParams;
2505 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002506
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002507 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002508
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002509 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002510 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002511
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002512 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002513
Zhijun He0a210512014-07-24 13:45:15 -07002514 // tear down the deleted streams after configure streams.
2515 mDeletedStreams.clear();
2516
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002517 auto rc = mPreparerThread->resume();
2518 if (rc != OK) {
2519 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2520 return rc;
2521 }
2522
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002523 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002524 mRequestBufferSM.onStreamsConfigured();
2525 }
2526
Cliff Wu3b268182021-07-06 15:44:43 +08002527 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002528 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002529 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002530 // configure the injection streams.
2531 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002532 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002533 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2534 res = mInjectionMethods->injectCamera(config, bufferSizes);
2535 if (res != OK) {
2536 ALOGE("Can't finish inject camera process!");
2537 return res;
2538 }
Cliff Wu3b268182021-07-06 15:44:43 +08002539 } else {
2540 // First run configureStreamsLocked() and then call injectCamera() case:
2541 // If the stream configuration has been completed and camera deive is active, but the
2542 // injection camera has not been injected yet, we need to store the stream configuration of
2543 // the internal camera (because the stream configuration of the injection camera is based
2544 // on the internal camera). When injecting occurs later, this configuration can be used by
2545 // the injection camera.
2546 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2547 " injection camera has not been injected yet.", __FUNCTION__);
2548 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002549 }
2550
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002551 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002552}
2553
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002554status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002555 ATRACE_CALL();
2556 status_t res;
2557
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002558 if (mFakeStreamId != NO_STREAM) {
2559 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002560 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002561 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002562 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002563 return INVALID_OPERATION;
2564 }
2565
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002566 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002567
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002568 sp<Camera3OutputStreamInterface> fakeStream =
2569 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002570
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002571 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002572 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002573 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002574 return res;
2575 }
2576
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002577 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002578 mNextStreamId++;
2579
2580 return OK;
2581}
2582
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002583status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002584 ATRACE_CALL();
2585 status_t res;
2586
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002587 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002588 if (mOutputStreams.size() == 1) return OK;
2589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002590 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002591
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002592 // Ok, have a fake stream and there's at least one other output stream,
2593 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002594
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002595 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002596 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002597 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002598 return INVALID_OPERATION;
2599 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002600 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002601
2602 // Free up the stream endpoint so that it can be used by some other stream
2603 res = deletedStream->disconnect();
2604 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002605 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606 // fall through since we want to still list the stream as deleted.
2607 }
2608 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002609 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002610
2611 return res;
2612}
2613
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002614void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002615 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002616 Mutex::Autolock l(mLock);
2617 va_list args;
2618 va_start(args, fmt);
2619
2620 setErrorStateLockedV(fmt, args);
2621
2622 va_end(args);
2623}
2624
2625void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002626 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002627 Mutex::Autolock l(mLock);
2628 setErrorStateLockedV(fmt, args);
2629}
2630
2631void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2632 va_list args;
2633 va_start(args, fmt);
2634
2635 setErrorStateLockedV(fmt, args);
2636
2637 va_end(args);
2638}
2639
2640void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002641 // Print out all error messages to log
2642 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002643 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002644
2645 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002646 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002647
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002648 mErrorCause = errorCause;
2649
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002650 if (mRequestThread != nullptr) {
2651 mRequestThread->setPaused(true);
2652 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002653 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002654
2655 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002656 sp<NotificationListener> listener = mListener.promote();
2657 if (listener != NULL) {
2658 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002659 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002660 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661 }
2662
2663 // Save stack trace. View by dumping it later.
2664 CameraTraces::saveTrace();
2665 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002666}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002667
2668/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002669 * In-flight request management
2670 */
2671
Jianing Weicb0652e2014-03-12 18:29:36 -07002672status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002673 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002674 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08002675 const std::set<std::set<String8>>& physicalCameraIds,
2676 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2677 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002678 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002679 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002680 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002681
2682 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002683 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002684 hasAppCallback, minExpectedDuration, maxExpectedDuration, physicalCameraIds,
2685 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2686 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002687 if (res < 0) return res;
2688
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002689 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002690 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2691 // avoid a deadlock during reprocess requests.
2692 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002693 if (mStatusTracker != nullptr) {
2694 mStatusTracker->markComponentActive(mInFlightStatusId);
2695 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002696 }
2697
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002698 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002699 return OK;
2700}
2701
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002702void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002703 // Indicate idle inFlightMap to the status tracker
2704 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002705 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002706 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2707 // avoid a deadlock during reprocess requests.
2708 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002709 if (mStatusTracker != nullptr) {
2710 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2711 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002712 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002713 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002714}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002715
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002716void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002717 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002718 // something has likely gone wrong. This might still be legit only if application send in
2719 // a long burst of long exposure requests.
2720 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2721 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2722 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2723 mInFlightMap.size(), mExpectedInflightDuration);
2724 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2725 kInFlightWarnLimitHighSpeed) {
2726 CLOGW("In-flight list too large for high speed configuration: %zu,"
2727 "total inflight duration %" PRIu64,
2728 mInFlightMap.size(), mExpectedInflightDuration);
2729 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002730 }
2731}
2732
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002733void Camera3Device::onInflightMapFlushedLocked() {
2734 mExpectedInflightDuration = 0;
2735}
2736
2737void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002738 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002739 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2740 mInFlightMap.removeItemsAt(idx, 1);
2741
2742 onInflightEntryRemovedLocked(duration);
2743}
2744
2745
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002746void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002747 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002748 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002749 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002750 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002751 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002752 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002753
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002754 FlushInflightReqStates states {
2755 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002756 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002757
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002758 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002759}
2760
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002761CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002762 ALOGV("%s", __FUNCTION__);
2763
Igor Murashkin1e479c02013-09-06 16:55:14 -07002764 CameraMetadata retVal;
2765
2766 if (mRequestThread != NULL) {
2767 retVal = mRequestThread->getLatestRequest();
2768 }
2769
Igor Murashkin1e479c02013-09-06 16:55:14 -07002770 return retVal;
2771}
2772
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002773void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002774 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002775 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002776 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2777 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002778
2779 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002780 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002781}
2782
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002783void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002784 std::vector<native_handle_t*> *handles, bool closeFd) {
2785 if (handles == nullptr) {
2786 return;
2787 }
2788 if (closeFd) {
2789 for (auto& handle : *handles) {
2790 native_handle_close(handle);
2791 }
2792 }
2793 for (auto& handle : *handles) {
2794 native_handle_delete(handle);
2795 }
2796 handles->clear();
2797 return;
2798}
2799
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002800/**
2801 * HalInterface inner class methods
2802 */
2803
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002804void Camera3Device::HalInterface::getInflightBufferKeys(
2805 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002806 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002807 return;
2808}
2809
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002810void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2811 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002812 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002813 return;
2814}
2815
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002816bool Camera3Device::HalInterface::verifyBufferIds(
2817 int32_t streamId, std::vector<uint64_t>& bufIds) {
2818 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002819}
2820
2821status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002822 int32_t frameNumber, int32_t streamId,
2823 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002824 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002825}
2826
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002827status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002828 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002829 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002830}
2831
2832// Find and pop a buffer_handle_t based on bufferId
2833status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002834 uint64_t bufferId,
2835 /*out*/ buffer_handle_t** buffer,
2836 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002837 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002838}
2839
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002840std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2841 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002842 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002843}
2844
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002845uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2846 const native_handle_t* handle) {
2847 return mBufferRecords.removeOneBufferCache(streamId, handle);
2848}
2849
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002850void Camera3Device::HalInterface::onBufferFreed(
2851 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002852 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2853 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2854 if (bufferId != BUFFER_ID_NO_BUFFER) {
2855 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002856 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002857}
2858
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002859void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002860 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2861 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2862 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002863 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2864 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002865}
2866
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002867/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002868 * RequestThread inner class methods
2869 */
2870
2871Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002872 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002873 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002874 bool useHalBufManager,
2875 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002876 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002878 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002879 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002880 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002881 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002882 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002883 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 mReconfigured(false),
2885 mDoPause(false),
2886 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002887 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002888 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002889 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002890 mCurrentAfTriggerId(0),
2891 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002892 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002893 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002894 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002895 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002896 mRepeatingLastFrameNumber(
2897 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002898 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002899 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002900 mRequestLatency(kRequestLatencyBinSize),
2901 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002902 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002903 mUseHalBufManager(useHalBufManager),
2904 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002905 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002906}
2907
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002908Camera3Device::RequestThread::~RequestThread() {}
2909
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002910void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002911 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002912 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002913 Mutex::Autolock l(mRequestLock);
2914 mListener = listener;
2915}
2916
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002917void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002918 const CameraMetadata& sessionParams,
2919 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002920 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 Mutex::Autolock l(mRequestLock);
2922 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002923 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002924 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002925 // Prepare video stream for high speed recording.
2926 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002927 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928}
2929
Jianing Wei90e59c92014-03-12 18:29:36 -07002930status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002931 List<sp<CaptureRequest> > &requests,
2932 /*out*/
2933 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002934 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002935 Mutex::Autolock l(mRequestLock);
2936 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2937 ++it) {
2938 mRequestQueue.push_back(*it);
2939 }
2940
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002941 if (lastFrameNumber != NULL) {
2942 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2943 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2944 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2945 *lastFrameNumber);
2946 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002947
Jianing Wei90e59c92014-03-12 18:29:36 -07002948 unpauseForNewRequests();
2949
2950 return OK;
2951}
2952
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002953
2954status_t Camera3Device::RequestThread::queueTrigger(
2955 RequestTrigger trigger[],
2956 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002957 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002958 Mutex::Autolock l(mTriggerMutex);
2959 status_t ret;
2960
2961 for (size_t i = 0; i < count; ++i) {
2962 ret = queueTriggerLocked(trigger[i]);
2963
2964 if (ret != OK) {
2965 return ret;
2966 }
2967 }
2968
2969 return OK;
2970}
2971
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002972const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2973 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002974 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002975 if (d != nullptr) return d->mId;
2976 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002977}
2978
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002979status_t Camera3Device::RequestThread::queueTriggerLocked(
2980 RequestTrigger trigger) {
2981
2982 uint32_t tag = trigger.metadataTag;
2983 ssize_t index = mTriggerMap.indexOfKey(tag);
2984
2985 switch (trigger.getTagType()) {
2986 case TYPE_BYTE:
2987 // fall-through
2988 case TYPE_INT32:
2989 break;
2990 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002991 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2992 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002993 return INVALID_OPERATION;
2994 }
2995
2996 /**
2997 * Collect only the latest trigger, since we only have 1 field
2998 * in the request settings per trigger tag, and can't send more than 1
2999 * trigger per request.
3000 */
3001 if (index != NAME_NOT_FOUND) {
3002 mTriggerMap.editValueAt(index) = trigger;
3003 } else {
3004 mTriggerMap.add(tag, trigger);
3005 }
3006
3007 return OK;
3008}
3009
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003010status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003011 const RequestList &requests,
3012 /*out*/
3013 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003014 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003015 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003016 if (lastFrameNumber != NULL) {
3017 *lastFrameNumber = mRepeatingLastFrameNumber;
3018 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003019 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003020 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003021 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3022 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003023
3024 unpauseForNewRequests();
3025
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003026 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003027 return OK;
3028}
3029
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003030bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003031 if (mRepeatingRequests.empty()) {
3032 return false;
3033 }
3034 int32_t requestId = requestIn->mResultExtras.requestId;
3035 const RequestList &repeatRequests = mRepeatingRequests;
3036 // All repeating requests are guaranteed to have same id so only check first quest
3037 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3038 return (firstRequest->mResultExtras.requestId == requestId);
3039}
3040
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003041status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003042 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003043 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003044 return clearRepeatingRequestsLocked(lastFrameNumber);
3045
3046}
3047
3048status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003049 std::vector<int32_t> streamIds;
3050 for (const auto& request : mRepeatingRequests) {
3051 for (const auto& stream : request->mOutputStreams) {
3052 streamIds.push_back(stream->getId());
3053 }
3054 }
3055
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003056 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003057 if (lastFrameNumber != NULL) {
3058 *lastFrameNumber = mRepeatingLastFrameNumber;
3059 }
Emilian Peev2295df72021-11-12 18:14:10 -08003060
3061 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3062
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003063 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003064 return OK;
3065}
3066
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003067status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003068 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003069 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003070 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003071 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003072
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003073 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003074
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003075 // Send errors for all requests pending in the request queue, including
3076 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003077 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003078 if (listener != NULL) {
3079 for (RequestList::iterator it = mRequestQueue.begin();
3080 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003081 // Abort the input buffers for reprocess requests.
3082 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003083 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003084 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003085 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003086 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003087 if (res != OK) {
3088 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3089 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3090 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003091 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003092 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3093 if (res != OK) {
3094 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3095 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3096 }
3097 }
3098 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003099 // Set the frame number this request would have had, if it
3100 // had been submitted; this frame number will not be reused.
3101 // The requestId and burstId fields were set when the request was
3102 // submitted originally (in convertMetadataListToRequestListLocked)
3103 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003104 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003105 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003106 }
3107 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003108 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003109
3110 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003111 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003112 if (lastFrameNumber != NULL) {
3113 *lastFrameNumber = mRepeatingLastFrameNumber;
3114 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003115 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003116 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003117 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003118 return OK;
3119}
3120
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003121status_t Camera3Device::RequestThread::flush() {
3122 ATRACE_CALL();
3123 Mutex::Autolock l(mFlushLock);
3124
Emilian Peev08dd2452017-04-06 16:55:14 +01003125 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003126}
3127
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003128void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003129 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003130 Mutex::Autolock l(mPauseLock);
3131 mDoPause = paused;
3132 mDoPauseSignal.signal();
3133}
3134
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003135status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3136 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003137 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003138 Mutex::Autolock l(mLatestRequestMutex);
3139 status_t res;
3140 while (mLatestRequestId != requestId) {
3141 nsecs_t startTime = systemTime();
3142
3143 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3144 if (res != OK) return res;
3145
3146 timeout -= (systemTime() - startTime);
3147 }
3148
3149 return OK;
3150}
3151
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003152void Camera3Device::RequestThread::requestExit() {
3153 // Call parent to set up shutdown
3154 Thread::requestExit();
3155 // The exit from any possible waits
3156 mDoPauseSignal.signal();
3157 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003158
3159 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3160 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003161}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003162
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003163void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003164 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003165 bool surfaceAbandoned = false;
3166 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003167 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003168 {
3169 Mutex::Autolock l(mRequestLock);
3170 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3171 // repeating requests.
3172 for (const auto& request : mRepeatingRequests) {
3173 for (const auto& s : request->mOutputStreams) {
3174 if (s->isAbandoned()) {
3175 surfaceAbandoned = true;
3176 clearRepeatingRequestsLocked(&lastFrameNumber);
3177 break;
3178 }
3179 }
3180 if (surfaceAbandoned) {
3181 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003182 }
3183 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003184 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003185 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003186
3187 if (listener != NULL && surfaceAbandoned) {
3188 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003189 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003190}
3191
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003192bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003193 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003194 status_t res;
3195 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003196 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003197 uint32_t numRequestProcessed = 0;
3198 for (size_t i = 0; i < batchSize; i++) {
3199 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003200 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003201 }
3202
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003203 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3204
3205 bool triggerRemoveFailed = false;
3206 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3207 for (size_t i = 0; i < numRequestProcessed; i++) {
3208 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3209 nextRequest.submitted = true;
3210
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003211 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003212
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003213 if (!triggerRemoveFailed) {
3214 // Remove any previously queued triggers (after unlock)
3215 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3216 if (removeTriggerRes != OK) {
3217 triggerRemoveFailed = true;
3218 triggerFailedRequest = nextRequest;
3219 }
3220 }
3221 }
3222
3223 if (triggerRemoveFailed) {
3224 SET_ERR("RequestThread: Unable to remove triggers "
3225 "(capture request %d, HAL device: %s (%d)",
3226 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3227 cleanUpFailedRequests(/*sendRequestError*/ false);
3228 return false;
3229 }
3230
3231 if (res != OK) {
3232 // Should only get a failure here for malformed requests or device-level
3233 // errors, so consider all errors fatal. Bad metadata failures should
3234 // come through notify.
3235 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3236 mNextRequests[numRequestProcessed].halRequest.frame_number,
3237 strerror(-res), res);
3238 cleanUpFailedRequests(/*sendRequestError*/ false);
3239 return false;
3240 }
3241 return true;
3242}
3243
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003244std::pair<nsecs_t, nsecs_t> Camera3Device::RequestThread::calculateExpectedDurationRange(
3245 const camera_metadata_t *request) {
3246 std::pair<nsecs_t, nsecs_t> expectedRange(
3247 InFlightRequest::kDefaultMinExpectedDuration,
3248 InFlightRequest::kDefaultMaxExpectedDuration);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003249 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3250 find_camera_metadata_ro_entry(request,
3251 ANDROID_CONTROL_AE_MODE,
3252 &e);
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003253 if (e.count == 0) return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003254
3255 switch (e.data.u8[0]) {
3256 case ANDROID_CONTROL_AE_MODE_OFF:
3257 find_camera_metadata_ro_entry(request,
3258 ANDROID_SENSOR_EXPOSURE_TIME,
3259 &e);
3260 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003261 expectedRange.first = e.data.i64[0];
3262 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003263 }
3264 find_camera_metadata_ro_entry(request,
3265 ANDROID_SENSOR_FRAME_DURATION,
3266 &e);
3267 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003268 expectedRange.first = std::max(e.data.i64[0], expectedRange.first);
3269 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003270 }
3271 break;
3272 default:
3273 find_camera_metadata_ro_entry(request,
3274 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3275 &e);
3276 if (e.count > 1) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003277 expectedRange.first = 1e9 / e.data.i32[1];
3278 expectedRange.second = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003279 }
3280 break;
3281 }
3282
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003283 return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003284}
3285
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003286bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3287 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3288 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3289 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3290 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3291 return true;
3292 }
3293
3294 return false;
3295}
3296
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003297void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3298 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003299 camera_capture_request_t& halRequest = nextRequest.halRequest;
3300 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003301 Mutex::Autolock al(mLatestRequestMutex);
3302
Shuzhen Wang83bff122020-11-20 15:51:39 -08003303 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003304 mLatestRequest.acquire(cloned);
3305
3306 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003307 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3308 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3309 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003310 CameraMetadata(cloned));
3311 }
3312
3313 sp<Camera3Device> parent = mParent.promote();
3314 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003315 int32_t inputStreamId = -1;
3316 if (halRequest.input_buffer != nullptr) {
3317 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3318 }
3319
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003320 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003321 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003322 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3323 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003324 }
3325 }
3326
Shuzhen Wang83bff122020-11-20 15:51:39 -08003327 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003328 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003329 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003330 }
3331
Shuzhen Wang83bff122020-11-20 15:51:39 -08003332 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003333}
3334
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003335bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3336 ATRACE_CALL();
3337 bool updatesDetected = false;
3338
Emilian Peev4ec17882019-01-24 17:16:58 -08003339 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003340 for (auto tag : mSessionParamKeys) {
3341 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003342 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003343
3344 if (entry.count > 0) {
3345 bool isDifferent = false;
3346 if (lastEntry.count > 0) {
3347 // Have a last value, compare to see if changed
3348 if (lastEntry.type == entry.type &&
3349 lastEntry.count == entry.count) {
3350 // Same type and count, compare values
3351 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3352 size_t entryBytes = bytesPerValue * lastEntry.count;
3353 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3354 if (cmp != 0) {
3355 isDifferent = true;
3356 }
3357 } else {
3358 // Count or type has changed
3359 isDifferent = true;
3360 }
3361 } else {
3362 // No last entry, so always consider to be different
3363 isDifferent = true;
3364 }
3365
3366 if (isDifferent) {
3367 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003368 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3369 updatesDetected = true;
3370 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003371 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003372 }
3373 } else if (lastEntry.count > 0) {
3374 // Value has been removed
3375 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003376 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003377 updatesDetected = true;
3378 }
3379 }
3380
Emilian Peev4ec17882019-01-24 17:16:58 -08003381 bool reconfigureRequired;
3382 if (updatesDetected) {
3383 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3384 updatedParams);
3385 mLatestSessionParams = updatedParams;
3386 } else {
3387 reconfigureRequired = false;
3388 }
3389
3390 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003391}
3392
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003393bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003394 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003395 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003396 // Any function called from threadLoop() must not hold mInterfaceLock since
3397 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3398 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003399
3400 // Handle paused state.
3401 if (waitIfPaused()) {
3402 return true;
3403 }
3404
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003405 // Wait for the next batch of requests.
3406 waitForNextRequestBatch();
3407 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003408 return true;
3409 }
3410
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003411 // Get the latest request ID, if any
3412 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003413 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003414 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003415 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003416 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003417 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003418 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3419 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003420 }
3421
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003422 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3423 // or a single request from streaming or burst. In either case the first element
3424 // should contain the latest camera settings that we need to check for any session
3425 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003426 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003427 res = OK;
3428
3429 //Input stream buffers are already acquired at this point so an input stream
3430 //will not be able to move to idle state unless we force it.
3431 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3432 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3433 if (res != OK) {
3434 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3435 cleanUpFailedRequests(/*sendRequestError*/ false);
3436 return false;
3437 }
3438 }
3439
3440 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003441 sp<Camera3Device> parent = mParent.promote();
3442 if (parent != nullptr) {
3443 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003444 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003445 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003446
3447 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3448 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3449 if (res != OK) {
3450 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3451 cleanUpFailedRequests(/*sendRequestError*/ false);
3452 return false;
3453 }
3454 }
3455 }
3456 }
3457
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003458 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003459 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003460 if (res == TIMED_OUT) {
3461 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003462 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003463 // Check if any stream is abandoned.
3464 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003465 return true;
3466 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003467 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003468 return false;
3469 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003470
Zhijun Hecc27e112013-10-03 16:12:43 -07003471 // Inform waitUntilRequestProcessed thread of a new request ID
3472 {
3473 Mutex::Autolock al(mLatestRequestMutex);
3474
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003475 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003476 mLatestRequestSignal.signal();
3477 }
3478
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003479 // Submit a batch of requests to HAL.
3480 // Use flush lock only when submitting multilple requests in a batch.
3481 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3482 // which may take a long time to finish so synchronizing flush() and
3483 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3484 // For now, only synchronize for high speed recording and we should figure something out for
3485 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003486 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003487
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003488 if (useFlushLock) {
3489 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003490 }
3491
Zhijun Hef0645c12016-08-02 00:58:11 -07003492 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003493 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003494
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003495 sp<Camera3Device> parent = mParent.promote();
3496 if (parent != nullptr) {
3497 parent->mRequestBufferSM.onSubmittingRequest();
3498 }
3499
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003500 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003501 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003502 submitRequestSuccess = sendRequestsBatch();
3503
Shuzhen Wang686f6442017-06-20 16:16:04 -07003504 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3505 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003506
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003507 if (useFlushLock) {
3508 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003509 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003510
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003511 // Unset as current request
3512 {
3513 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003514 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003515 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003516 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003517
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003518 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003519}
3520
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003521status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3522 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3523 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3524 ANDROID_SCALER_CROP_REGION_SET};
3525 if (request == nullptr) {
3526 ALOGE("%s request metadata nullptr", __FUNCTION__);
3527 return BAD_VALUE;
3528 }
3529 status_t res = OK;
3530 for (const auto &key : kFwkOnlyRegionKeys) {
3531 if (request->exists(key)) {
3532 res = request->erase(key);
3533 if (res != OK) {
3534 return res;
3535 }
3536 }
3537 }
3538 return OK;
3539}
3540
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003541status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003542 ATRACE_CALL();
3543
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003544 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003545 for (size_t i = 0; i < mNextRequests.size(); i++) {
3546 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003547 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003548 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3549 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003550
3551 // Prepare a request to HAL
3552 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3553
3554 // Insert any queued triggers (before metadata is locked)
3555 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003556 if (res < 0) {
3557 SET_ERR("RequestThread: Unable to insert triggers "
3558 "(capture request %d, HAL device: %s (%d)",
3559 halRequest->frame_number, strerror(-res), res);
3560 return INVALID_OPERATION;
3561 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003562
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003563 int triggerCount = res;
3564 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3565 mPrevTriggers = triggerCount;
3566
Emilian Peeve23f1d92021-09-20 14:56:01 -07003567 // Do not override rotate&crop for stream configurations that include
3568 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3569 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3570 bool rotateAndCropChanged = mComposerOutput ? false :
3571 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003572 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003573
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003574 // If the request is the same as last, or we had triggers now or last time or
3575 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003576 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003577 (mPrevRequest != captureRequest || triggersMixedIn ||
3578 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003579 // Request settings are all the same within one batch, so only treat the first
3580 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003581 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003582 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003583 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003584 /**
3585 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003586 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003587 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003588 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003589 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003590 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003591 "(capture request %d, HAL device: %s (%d)",
3592 halRequest->frame_number, strerror(-res), res);
3593 return INVALID_OPERATION;
3594 }
3595
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003596 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003597 sp<Camera3Device> parent = mParent.promote();
3598 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003599 List<PhysicalCameraSettings>::iterator it;
3600 for (it = captureRequest->mSettingsList.begin();
3601 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003602 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3603 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003604 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3605 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3606 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3607 res);
3608 return INVALID_OPERATION;
3609 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003610 continue;
3611 }
3612
3613 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3614 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3615 updateCaptureRequest(&(it->metadata));
3616 if (res != OK) {
3617 SET_ERR("RequestThread: Unable to correct capture requests "
3618 "for scaler crop region and metering regions for request "
3619 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3620 res);
3621 return INVALID_OPERATION;
3622 }
3623 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003624 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3625 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3626 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3627 res);
3628 return INVALID_OPERATION;
3629 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003630 }
3631 }
3632
3633 // Correct metadata regions for distortion correction if enabled
3634 for (it = captureRequest->mSettingsList.begin();
3635 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003636 if (parent->mDistortionMappers.find(it->cameraId) ==
3637 parent->mDistortionMappers.end()) {
3638 continue;
3639 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003640
3641 if (!captureRequest->mDistortionCorrectionUpdated) {
3642 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3643 &(it->metadata));
3644 if (res != OK) {
3645 SET_ERR("RequestThread: Unable to correct capture requests "
3646 "for lens distortion for request %d: %s (%d)",
3647 halRequest->frame_number, strerror(-res), res);
3648 return INVALID_OPERATION;
3649 }
3650 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003651 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003652 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003653
3654 for (it = captureRequest->mSettingsList.begin();
3655 it != captureRequest->mSettingsList.end(); it++) {
3656 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3657 parent->mZoomRatioMappers.end()) {
3658 continue;
3659 }
3660
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003661 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003662 cameraIdsWithZoom.insert(it->cameraId);
3663 }
3664
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003665 if (!captureRequest->mZoomRatioUpdated) {
3666 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3667 &(it->metadata));
3668 if (res != OK) {
3669 SET_ERR("RequestThread: Unable to correct capture requests "
3670 "for zoom ratio for request %d: %s (%d)",
3671 halRequest->frame_number, strerror(-res), res);
3672 return INVALID_OPERATION;
3673 }
3674 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003675 }
3676 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003677 if (captureRequest->mRotateAndCropAuto &&
3678 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003679 for (it = captureRequest->mSettingsList.begin();
3680 it != captureRequest->mSettingsList.end(); it++) {
3681 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3682 if (mapper != parent->mRotateAndCropMappers.end()) {
3683 res = mapper->second.updateCaptureRequest(&(it->metadata));
3684 if (res != OK) {
3685 SET_ERR("RequestThread: Unable to correct capture requests "
3686 "for rotate-and-crop for request %d: %s (%d)",
3687 halRequest->frame_number, strerror(-res), res);
3688 return INVALID_OPERATION;
3689 }
3690 }
3691 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003692 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003693 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003694 }
3695 }
3696
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003697 /**
3698 * The request should be presorted so accesses in HAL
3699 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3700 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003701 captureRequest->mSettingsList.begin()->metadata.sort();
3702 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003703 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003704 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003705 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3706
3707 IF_ALOGV() {
3708 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3709 find_camera_metadata_ro_entry(
3710 halRequest->settings,
3711 ANDROID_CONTROL_AF_TRIGGER,
3712 &e
3713 );
3714 if (e.count > 0) {
3715 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3716 __FUNCTION__,
3717 halRequest->frame_number,
3718 e.data.u8[0]);
3719 }
3720 }
3721 } else {
3722 // leave request.settings NULL to indicate 'reuse latest given'
3723 ALOGVV("%s: Request settings are REUSED",
3724 __FUNCTION__);
3725 }
3726
Emilian Peevaebbe412018-01-15 13:53:24 +00003727 if (captureRequest->mSettingsList.size() > 1) {
3728 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3729 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003730 if (newRequest) {
3731 halRequest->physcam_settings =
3732 new const camera_metadata* [halRequest->num_physcam_settings];
3733 } else {
3734 halRequest->physcam_settings = nullptr;
3735 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003736 auto it = ++captureRequest->mSettingsList.begin();
3737 size_t i = 0;
3738 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3739 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003740 if (newRequest) {
3741 it->metadata.sort();
3742 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3743 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003744 }
3745 }
3746
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003747 uint32_t totalNumBuffers = 0;
3748
3749 // Fill in buffers
3750 if (captureRequest->mInputStream != NULL) {
3751 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003752
3753 halRequest->input_width = captureRequest->mInputBufferSize.width;
3754 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003755 totalNumBuffers += 1;
3756 } else {
3757 halRequest->input_buffer = NULL;
3758 }
3759
Emilian Peevf4816702020-04-03 15:44:51 -07003760 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003761 captureRequest->mOutputStreams.size());
3762 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003763 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003764
3765 sp<Camera3Device> parent = mParent.promote();
3766 if (parent == NULL) {
3767 // Should not happen, and nowhere to send errors to, so just log it
3768 CLOGE("RequestThread: Parent is gone");
3769 return INVALID_OPERATION;
3770 }
3771 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3772
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003773 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003774 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003775 sp<Camera3OutputStreamInterface> outputStream =
3776 captureRequest->mOutputStreams.editItemAt(j);
3777 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003778
3779 // Prepare video buffers for high speed recording on the first video request.
3780 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3781 // Only try to prepare video stream on the first video request.
3782 mPrepareVideoStream = false;
3783
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003784 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3785 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003786 while (res == NOT_ENOUGH_DATA) {
3787 res = outputStream->prepareNextBuffer();
3788 }
3789 if (res != OK) {
3790 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3791 __FUNCTION__, strerror(-res), res);
3792 outputStream->cancelPrepare();
3793 }
3794 }
3795
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003796 std::vector<size_t> uniqueSurfaceIds;
3797 res = outputStream->getUniqueSurfaceIds(
3798 captureRequest->mOutputSurfaces[streamId],
3799 &uniqueSurfaceIds);
3800 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3801 if (res != OK && res != INVALID_OPERATION) {
3802 ALOGE("%s: failed to query stream %d unique surface IDs",
3803 __FUNCTION__, streamId);
3804 return res;
3805 }
3806 if (res == OK) {
3807 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3808 }
3809
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003810 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003811 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003812 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003813 return TIMED_OUT;
3814 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003815 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003816 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003817 buffer.stream = outputStream->asHalStream();
3818 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003819 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003820 buffer.acquire_fence = -1;
3821 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003822 // Mark the output stream as unpreparable to block clients from calling
3823 // 'prepare' after this request reaches CameraHal and before the respective
3824 // buffers are requested.
3825 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003826 } else {
3827 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3828 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003829 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003830 if (res != OK) {
3831 // Can't get output buffer from gralloc queue - this could be due to
3832 // abandoned queue or other consumer misbehavior, so not a fatal
3833 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003834 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003835 " %s (%d)", strerror(-res), res);
3836
3837 return TIMED_OUT;
3838 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003839 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003840
3841 {
3842 sp<Camera3Device> parent = mParent.promote();
3843 if (parent != nullptr) {
3844 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3845 for (const auto& settings : captureRequest->mSettingsList) {
3846 if ((streamCameraId.isEmpty() &&
3847 parent->getId() == settings.cameraId.c_str()) ||
3848 streamCameraId == settings.cameraId.c_str()) {
3849 outputStream->fireBufferRequestForFrameNumber(
3850 captureRequest->mResultExtras.frameNumber,
3851 settings.metadata);
3852 }
3853 }
3854 }
3855 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003856
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003857 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003858 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3859 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3860 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3861 } else if (!physicalCameraId.isEmpty()) {
3862 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003863 }
3864 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003865 }
3866 totalNumBuffers += halRequest->num_output_buffers;
3867
3868 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003869 // If this request list is for constrained high speed recording (not
3870 // preview), and the current request is not the last one in the batch,
3871 // do not send callback to the app.
3872 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003873 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003874 hasCallback = false;
3875 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003876 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003877 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003878 const camera_metadata_t* settings = halRequest->settings;
3879 bool shouldUnlockSettings = false;
3880 if (settings == nullptr) {
3881 shouldUnlockSettings = true;
3882 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3883 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003884 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3885 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003886 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003887 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3888 isStillCapture = true;
3889 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3890 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003891
Emilian Peevaf8416e2021-02-04 17:52:43 -08003892 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003893 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003894 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3895 isZslCapture = true;
3896 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003897 }
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003898 auto expectedDurationRange = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003899 res = parent->registerInFlight(halRequest->frame_number,
3900 totalNumBuffers, captureRequest->mResultExtras,
3901 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003902 hasCallback,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003903 /*min*/expectedDurationRange.first,
3904 /*max*/expectedDurationRange.second,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003905 requestedPhysicalCameras, isStillCapture, isZslCapture,
3906 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003907 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003908 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003909 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3910 ", burstId = %" PRId32 ".",
3911 __FUNCTION__,
3912 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3913 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003914
3915 if (shouldUnlockSettings) {
3916 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3917 }
3918
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003919 if (res != OK) {
3920 SET_ERR("RequestThread: Unable to register new in-flight request:"
3921 " %s (%d)", strerror(-res), res);
3922 return INVALID_OPERATION;
3923 }
3924 }
3925
3926 return OK;
3927}
3928
Igor Murashkin1e479c02013-09-06 16:55:14 -07003929CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003930 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003931 Mutex::Autolock al(mLatestRequestMutex);
3932
3933 ALOGV("RequestThread::%s", __FUNCTION__);
3934
3935 return mLatestRequest;
3936}
3937
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003938bool Camera3Device::RequestThread::isStreamPending(
3939 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003940 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003941 Mutex::Autolock l(mRequestLock);
3942
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003943 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003944 if (!nextRequest.submitted) {
3945 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3946 if (stream == s) return true;
3947 }
3948 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003949 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003950 }
3951
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003952 for (const auto& request : mRequestQueue) {
3953 for (const auto& s : request->mOutputStreams) {
3954 if (stream == s) return true;
3955 }
3956 if (stream == request->mInputStream) return true;
3957 }
3958
3959 for (const auto& request : mRepeatingRequests) {
3960 for (const auto& s : request->mOutputStreams) {
3961 if (stream == s) return true;
3962 }
3963 if (stream == request->mInputStream) return true;
3964 }
3965
3966 return false;
3967}
Jianing Weicb0652e2014-03-12 18:29:36 -07003968
Emilian Peev40ead602017-09-26 15:46:36 +01003969bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3970 ATRACE_CALL();
3971 Mutex::Autolock l(mRequestLock);
3972
3973 for (const auto& nextRequest : mNextRequests) {
3974 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3975 if (s.first == streamId) {
3976 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3977 if (it != s.second.end()) {
3978 return true;
3979 }
3980 }
3981 }
3982 }
3983
3984 for (const auto& request : mRequestQueue) {
3985 for (const auto& s : request->mOutputSurfaces) {
3986 if (s.first == streamId) {
3987 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3988 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003989 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01003990 }
3991 }
3992 }
3993 }
3994
3995 for (const auto& request : mRepeatingRequests) {
3996 for (const auto& s : request->mOutputSurfaces) {
3997 if (s.first == streamId) {
3998 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3999 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004000 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004001 }
4002 }
4003 }
4004 }
4005
4006 return false;
4007}
4008
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004009void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4010 if (!mUseHalBufManager) {
4011 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4012 return;
4013 }
4014
4015 Mutex::Autolock pl(mPauseLock);
4016 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004017 mInterface->signalPipelineDrain(streamIds);
4018 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004019 }
4020 // If request thread is still busy, wait until paused then notify HAL
4021 mNotifyPipelineDrain = true;
4022 mStreamIdsToBeDrained = streamIds;
4023}
4024
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004025void Camera3Device::RequestThread::resetPipelineDrain() {
4026 Mutex::Autolock pl(mPauseLock);
4027 mNotifyPipelineDrain = false;
4028 mStreamIdsToBeDrained.clear();
4029}
4030
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004031void Camera3Device::RequestThread::clearPreviousRequest() {
4032 Mutex::Autolock l(mRequestLock);
4033 mPrevRequest.clear();
4034}
4035
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004036status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4037 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4038 ATRACE_CALL();
4039 Mutex::Autolock l(mTriggerMutex);
4040 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4041 return BAD_VALUE;
4042 }
4043 mRotateAndCropOverride = rotateAndCropValue;
4044 return OK;
4045}
4046
Emilian Peeve23f1d92021-09-20 14:56:01 -07004047status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4048 ATRACE_CALL();
4049 Mutex::Autolock l(mTriggerMutex);
4050 mComposerOutput = composerSurfacePresent;
4051 return OK;
4052}
4053
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004054status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004055 ATRACE_CALL();
4056 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004057 if (muteMode != mCameraMute) {
4058 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004059 mCameraMuteChanged = true;
4060 }
4061 return OK;
4062}
4063
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004064nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004065 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004066 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004067 return mExpectedInflightDuration > kMinInflightDuration ?
4068 mExpectedInflightDuration : kMinInflightDuration;
4069}
4070
Emilian Peevaebbe412018-01-15 13:53:24 +00004071void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004072 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004073 if ((request == nullptr) || (halRequest == nullptr)) {
4074 ALOGE("%s: Invalid request!", __FUNCTION__);
4075 return;
4076 }
4077
4078 if (halRequest->num_physcam_settings > 0) {
4079 if (halRequest->physcam_id != nullptr) {
4080 delete [] halRequest->physcam_id;
4081 halRequest->physcam_id = nullptr;
4082 }
4083 if (halRequest->physcam_settings != nullptr) {
4084 auto it = ++(request->mSettingsList.begin());
4085 size_t i = 0;
4086 for (; it != request->mSettingsList.end(); it++, i++) {
4087 it->metadata.unlock(halRequest->physcam_settings[i]);
4088 }
4089 delete [] halRequest->physcam_settings;
4090 halRequest->physcam_settings = nullptr;
4091 }
4092 }
4093}
4094
Ravneetaeb20dc2022-03-30 05:33:03 +00004095status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4096 Mutex::Autolock il(mInterfaceLock);
4097 Mutex::Autolock l(mLock);
4098
4099 if (mCameraServiceWatchdog != NULL) {
4100 mCameraServiceWatchdog->setEnabled(enabled);
4101 }
4102
4103 return OK;
4104}
4105
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004106void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4107 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004108 return;
4109 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004110
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004111 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004112 // Skip the ones that have been submitted successfully.
4113 if (nextRequest.submitted) {
4114 continue;
4115 }
4116
4117 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004118 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4119 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004120
4121 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004122 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004123 }
4124
Emilian Peevaebbe412018-01-15 13:53:24 +00004125 cleanupPhysicalSettings(captureRequest, halRequest);
4126
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004127 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004128 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004129 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4130 }
4131
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004132 // No output buffer can be returned when using HAL buffer manager
4133 if (!mUseHalBufManager) {
4134 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4135 //Buffers that failed processing could still have
4136 //valid acquire fence.
4137 int acquireFence = (*outputBuffers)[i].acquire_fence;
4138 if (0 <= acquireFence) {
4139 close(acquireFence);
4140 outputBuffers->editItemAt(i).acquire_fence = -1;
4141 }
Emilian Peevf4816702020-04-03 15:44:51 -07004142 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004143 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4144 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004145 /*timestampIncreasing*/true, std::vector<size_t> (),
4146 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004147 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004148 }
4149
4150 if (sendRequestError) {
4151 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004152 sp<NotificationListener> listener = mListener.promote();
4153 if (listener != NULL) {
4154 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004155 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004156 captureRequest->mResultExtras);
4157 }
4158 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004159
4160 // Remove yet-to-be submitted inflight request from inflightMap
4161 {
4162 sp<Camera3Device> parent = mParent.promote();
4163 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004164 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004165 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4166 if (idx >= 0) {
4167 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4168 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4169 parent->removeInFlightMapEntryLocked(idx);
4170 }
4171 }
4172 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004173 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004174
4175 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004176 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004177}
4178
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004179void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004180 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004181 // Optimized a bit for the simple steady-state case (single repeating
4182 // request), to avoid putting that request in the queue temporarily.
4183 Mutex::Autolock l(mRequestLock);
4184
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004185 assert(mNextRequests.empty());
4186
4187 NextRequest nextRequest;
4188 nextRequest.captureRequest = waitForNextRequestLocked();
4189 if (nextRequest.captureRequest == nullptr) {
4190 return;
4191 }
4192
Emilian Peevf4816702020-04-03 15:44:51 -07004193 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004194 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004195 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004196
4197 // Wait for additional requests
4198 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4199
4200 for (size_t i = 1; i < batchSize; i++) {
4201 NextRequest additionalRequest;
4202 additionalRequest.captureRequest = waitForNextRequestLocked();
4203 if (additionalRequest.captureRequest == nullptr) {
4204 break;
4205 }
4206
Emilian Peevf4816702020-04-03 15:44:51 -07004207 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004208 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004209 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004210 }
4211
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004212 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004213 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004214 mNextRequests.size(), batchSize);
4215 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004216 }
4217
4218 return;
4219}
4220
4221sp<Camera3Device::CaptureRequest>
4222 Camera3Device::RequestThread::waitForNextRequestLocked() {
4223 status_t res;
4224 sp<CaptureRequest> nextRequest;
4225
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004226 while (mRequestQueue.empty()) {
4227 if (!mRepeatingRequests.empty()) {
4228 // Always atomically enqueue all requests in a repeating request
4229 // list. Guarantees a complete in-sequence set of captures to
4230 // application.
4231 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004232 if (mFirstRepeating) {
4233 mFirstRepeating = false;
4234 } else {
4235 for (auto& request : requests) {
4236 // For repeating requests, override timestamp request using
4237 // the time a request is inserted into the request queue,
4238 // because the original repeating request will have an old
4239 // fixed timestamp.
4240 request->mRequestTimeNs = systemTime();
4241 }
4242 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004243 RequestList::const_iterator firstRequest =
4244 requests.begin();
4245 nextRequest = *firstRequest;
4246 mRequestQueue.insert(mRequestQueue.end(),
4247 ++firstRequest,
4248 requests.end());
4249 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004250
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004251 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004252
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004253 break;
4254 }
4255
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004256 if (!mRequestClearing) {
4257 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004260 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4261 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004262 Mutex::Autolock pl(mPauseLock);
4263 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004264 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004265 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004266 if (mNotifyPipelineDrain) {
4267 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4268 mNotifyPipelineDrain = false;
4269 mStreamIdsToBeDrained.clear();
4270 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004271 // Let the tracker know
4272 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4273 if (statusTracker != 0) {
4274 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4275 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004276 sp<Camera3Device> parent = mParent.promote();
4277 if (parent != nullptr) {
4278 parent->mRequestBufferSM.onRequestThreadPaused();
4279 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004280 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004281 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004282 // Stop waiting for now and let thread management happen
4283 return NULL;
4284 }
4285 }
4286
4287 if (nextRequest == NULL) {
4288 // Don't have a repeating request already in hand, so queue
4289 // must have an entry now.
4290 RequestList::iterator firstRequest =
4291 mRequestQueue.begin();
4292 nextRequest = *firstRequest;
4293 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004294 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4295 sp<NotificationListener> listener = mListener.promote();
4296 if (listener != NULL) {
4297 listener->notifyRequestQueueEmpty();
4298 }
4299 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004300 }
4301
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004302 // In case we've been unpaused by setPaused clearing mDoPause, need to
4303 // update internal pause state (capture/setRepeatingRequest unpause
4304 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004305 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004306 if (mPaused) {
4307 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4308 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4309 if (statusTracker != 0) {
4310 statusTracker->markComponentActive(mStatusId);
4311 }
4312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004313 mPaused = false;
4314
4315 // Check if we've reconfigured since last time, and reset the preview
4316 // request if so. Can't use 'NULL request == repeat' across configure calls.
4317 if (mReconfigured) {
4318 mPrevRequest.clear();
4319 mReconfigured = false;
4320 }
4321
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004322 if (nextRequest != NULL) {
4323 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004324 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4325 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004326
4327 // Since RequestThread::clear() removes buffers from the input stream,
4328 // get the right buffer here before unlocking mRequestLock
4329 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004330 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4331 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004332 if (res != OK) {
4333 // Can't get input buffer from gralloc queue - this could be due to
4334 // disconnected queue or other producer misbehavior, so not a fatal
4335 // error
4336 ALOGE("%s: Can't get input buffer, skipping request:"
4337 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004338
4339 sp<NotificationListener> listener = mListener.promote();
4340 if (listener != NULL) {
4341 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004342 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004343 nextRequest->mResultExtras);
4344 }
4345 return NULL;
4346 }
4347 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004348 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004350 return nextRequest;
4351}
4352
4353bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004354 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004355 status_t res;
4356 Mutex::Autolock l(mPauseLock);
4357 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004358 if (mPaused == false) {
4359 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004360 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004361 if (mNotifyPipelineDrain) {
4362 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4363 mNotifyPipelineDrain = false;
4364 mStreamIdsToBeDrained.clear();
4365 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004366 // Let the tracker know
4367 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4368 if (statusTracker != 0) {
4369 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4370 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004371 sp<Camera3Device> parent = mParent.promote();
4372 if (parent != nullptr) {
4373 parent->mRequestBufferSM.onRequestThreadPaused();
4374 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004375 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004376
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004377 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004378 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004379 return true;
4380 }
4381 }
4382 // We don't set mPaused to false here, because waitForNextRequest needs
4383 // to further manage the paused state in case of starvation.
4384 return false;
4385}
4386
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004387void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004388 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004389 // With work to do, mark thread as unpaused.
4390 // If paused by request (setPaused), don't resume, to avoid
4391 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004392 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004393 Mutex::Autolock p(mPauseLock);
4394 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004395 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4396 if (mPaused) {
4397 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4398 if (statusTracker != 0) {
4399 statusTracker->markComponentActive(mStatusId);
4400 }
4401 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004402 mPaused = false;
4403 }
4404}
4405
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004406void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4407 sp<Camera3Device> parent = mParent.promote();
4408 if (parent != NULL) {
4409 va_list args;
4410 va_start(args, fmt);
4411
4412 parent->setErrorStateV(fmt, args);
4413
4414 va_end(args);
4415 }
4416}
4417
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004418status_t Camera3Device::RequestThread::insertTriggers(
4419 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004420 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004421 Mutex::Autolock al(mTriggerMutex);
4422
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004423 sp<Camera3Device> parent = mParent.promote();
4424 if (parent == NULL) {
4425 CLOGE("RequestThread: Parent is gone");
4426 return DEAD_OBJECT;
4427 }
4428
Emilian Peevaebbe412018-01-15 13:53:24 +00004429 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004430 size_t count = mTriggerMap.size();
4431
4432 for (size_t i = 0; i < count; ++i) {
4433 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004434 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004435
4436 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4437 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4438 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004439 if (isAeTrigger) {
4440 request->mResultExtras.precaptureTriggerId = triggerId;
4441 mCurrentPreCaptureTriggerId = triggerId;
4442 } else {
4443 request->mResultExtras.afTriggerId = triggerId;
4444 mCurrentAfTriggerId = triggerId;
4445 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004446 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004447 }
4448
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004449 camera_metadata_entry entry = metadata.find(tag);
4450
4451 if (entry.count > 0) {
4452 /**
4453 * Already has an entry for this trigger in the request.
4454 * Rewrite it with our requested trigger value.
4455 */
4456 RequestTrigger oldTrigger = trigger;
4457
4458 oldTrigger.entryValue = entry.data.u8[0];
4459
4460 mTriggerReplacedMap.add(tag, oldTrigger);
4461 } else {
4462 /**
4463 * More typical, no trigger entry, so we just add it
4464 */
4465 mTriggerRemovedMap.add(tag, trigger);
4466 }
4467
4468 status_t res;
4469
4470 switch (trigger.getTagType()) {
4471 case TYPE_BYTE: {
4472 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4473 res = metadata.update(tag,
4474 &entryValue,
4475 /*count*/1);
4476 break;
4477 }
4478 case TYPE_INT32:
4479 res = metadata.update(tag,
4480 &trigger.entryValue,
4481 /*count*/1);
4482 break;
4483 default:
4484 ALOGE("%s: Type not supported: 0x%x",
4485 __FUNCTION__,
4486 trigger.getTagType());
4487 return INVALID_OPERATION;
4488 }
4489
4490 if (res != OK) {
4491 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4492 ", value %d", __FUNCTION__, trigger.getTagName(),
4493 trigger.entryValue);
4494 return res;
4495 }
4496
4497 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4498 trigger.getTagName(),
4499 trigger.entryValue);
4500 }
4501
4502 mTriggerMap.clear();
4503
4504 return count;
4505}
4506
4507status_t Camera3Device::RequestThread::removeTriggers(
4508 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004509 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004510 Mutex::Autolock al(mTriggerMutex);
4511
Emilian Peevaebbe412018-01-15 13:53:24 +00004512 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004513
4514 /**
4515 * Replace all old entries with their old values.
4516 */
4517 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4518 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4519
4520 status_t res;
4521
4522 uint32_t tag = trigger.metadataTag;
4523 switch (trigger.getTagType()) {
4524 case TYPE_BYTE: {
4525 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4526 res = metadata.update(tag,
4527 &entryValue,
4528 /*count*/1);
4529 break;
4530 }
4531 case TYPE_INT32:
4532 res = metadata.update(tag,
4533 &trigger.entryValue,
4534 /*count*/1);
4535 break;
4536 default:
4537 ALOGE("%s: Type not supported: 0x%x",
4538 __FUNCTION__,
4539 trigger.getTagType());
4540 return INVALID_OPERATION;
4541 }
4542
4543 if (res != OK) {
4544 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4545 ", trigger value %d", __FUNCTION__,
4546 trigger.getTagName(), trigger.entryValue);
4547 return res;
4548 }
4549 }
4550 mTriggerReplacedMap.clear();
4551
4552 /**
4553 * Remove all new entries.
4554 */
4555 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4556 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4557 status_t res = metadata.erase(trigger.metadataTag);
4558
4559 if (res != OK) {
4560 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4561 ", trigger value %d", __FUNCTION__,
4562 trigger.getTagName(), trigger.entryValue);
4563 return res;
4564 }
4565 }
4566 mTriggerRemovedMap.clear();
4567
4568 return OK;
4569}
4570
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004571status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004572 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004573 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004574 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004575 status_t res;
4576
Emilian Peevaebbe412018-01-15 13:53:24 +00004577 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004578
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004579 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004580 // exists
4581 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4582 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4583 if (afTrigger.count > 0 &&
4584 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4585 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004586 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004587 if (res != OK) return res;
4588 }
4589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004590 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004591 // if none already exists
4592 camera_metadata_entry pcTrigger =
4593 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4594 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4595 if (pcTrigger.count > 0 &&
4596 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4597 pcId.count == 0) {
4598 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004599 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004600 if (res != OK) return res;
4601 }
4602
4603 return OK;
4604}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004605
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004606bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4607 const sp<CaptureRequest> &request) {
4608 ATRACE_CALL();
4609
4610 if (request->mRotateAndCropAuto) {
4611 Mutex::Autolock l(mTriggerMutex);
4612 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4613
4614 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4615 if (rotateAndCropEntry.count > 0) {
4616 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4617 return false;
4618 } else {
4619 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4620 return true;
4621 }
4622 } else {
4623 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4624 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4625 &rotateAndCrop_u8, 1);
4626 return true;
4627 }
4628 }
4629 return false;
4630}
4631
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004632bool Camera3Device::RequestThread::overrideTestPattern(
4633 const sp<CaptureRequest> &request) {
4634 ATRACE_CALL();
4635
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004636 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004637
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004638 Mutex::Autolock l(mTriggerMutex);
4639
4640 bool changed = false;
4641
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004642 // For a multi-camera, the physical cameras support the same set of
4643 // test pattern modes as the logical camera.
4644 for (auto& settings : request->mSettingsList) {
4645 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004646
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004647 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4648 int32_t testPatternData[4] = {
4649 settings.mOriginalTestPatternData[0],
4650 settings.mOriginalTestPatternData[1],
4651 settings.mOriginalTestPatternData[2],
4652 settings.mOriginalTestPatternData[3]
4653 };
4654 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4655 testPatternMode = mCameraMute;
4656 testPatternData[0] = 0;
4657 testPatternData[1] = 0;
4658 testPatternData[2] = 0;
4659 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004660 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004661
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004662 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4663 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4664 if (testPatternEntry.count > 0) {
4665 if (testPatternEntry.data.i32[0] != testPatternMode) {
4666 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004667 changed = true;
4668 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004669 } else if (supportTestPatternModeKey) {
4670 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4671 &testPatternMode, 1);
4672 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004673 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004674
4675 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4676 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4677 if (testPatternColor.count >= 4) {
4678 for (size_t i = 0; i < 4; i++) {
4679 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4680 testPatternColor.data.i32[i] = testPatternData[i];
4681 changed = true;
4682 }
4683 }
4684 } else if (supportTestPatternDataKey) {
4685 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4686 testPatternData, 4);
4687 changed = true;
4688 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004689 }
4690
4691 return changed;
4692}
4693
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004694status_t Camera3Device::RequestThread::setHalInterface(
4695 sp<HalInterface> newHalInterface) {
4696 if (newHalInterface.get() == nullptr) {
4697 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4698 return DEAD_OBJECT;
4699 }
4700
4701 mInterface = newHalInterface;
4702
4703 return OK;
4704}
4705
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004706/**
4707 * PreparerThread inner class methods
4708 */
4709
4710Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004711 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004712 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004713}
4714
4715Camera3Device::PreparerThread::~PreparerThread() {
4716 Thread::requestExitAndWait();
4717 if (mCurrentStream != nullptr) {
4718 mCurrentStream->cancelPrepare();
4719 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4720 mCurrentStream.clear();
4721 }
4722 clear();
4723}
4724
Ruben Brunkc78ac262015-08-13 17:58:46 -07004725status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004726 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004727 status_t res;
4728
4729 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004730 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004731
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004732 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004733 if (res == OK) {
4734 // No preparation needed, fire listener right off
4735 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004736 if (listener != NULL) {
4737 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004738 }
4739 return OK;
4740 } else if (res != NOT_ENOUGH_DATA) {
4741 return res;
4742 }
4743
4744 // Need to prepare, start up thread if necessary
4745 if (!mActive) {
4746 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4747 // isn't running
4748 Thread::requestExitAndWait();
4749 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4750 if (res != OK) {
4751 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004752 if (listener != NULL) {
4753 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004754 }
4755 return res;
4756 }
4757 mCancelNow = false;
4758 mActive = true;
4759 ALOGV("%s: Preparer stream started", __FUNCTION__);
4760 }
4761
4762 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004763 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004764 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4765
4766 return OK;
4767}
4768
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004769void Camera3Device::PreparerThread::pause() {
4770 ATRACE_CALL();
4771
4772 Mutex::Autolock l(mLock);
4773
4774 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4775 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4776 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4777 int currentMaxCount = mCurrentMaxCount;
4778 mPendingStreams.clear();
4779 mCancelNow = true;
4780 while (mActive) {
4781 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4782 if (res == TIMED_OUT) {
4783 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4784 return;
4785 } else if (res != OK) {
4786 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4787 return;
4788 }
4789 }
4790
4791 //Check whether the prepare thread was able to complete the current
4792 //stream. In case work is still pending emplace it along with the rest
4793 //of the streams in the pending list.
4794 if (currentStream != nullptr) {
4795 if (!mCurrentPrepareComplete) {
4796 pendingStreams.emplace(currentMaxCount, currentStream);
4797 }
4798 }
4799
4800 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4801 for (const auto& it : mPendingStreams) {
4802 it.second->cancelPrepare();
4803 }
4804}
4805
4806status_t Camera3Device::PreparerThread::resume() {
4807 ATRACE_CALL();
4808 status_t res;
4809
4810 Mutex::Autolock l(mLock);
4811 sp<NotificationListener> listener = mListener.promote();
4812
4813 if (mActive) {
4814 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4815 return NO_INIT;
4816 }
4817
4818 auto it = mPendingStreams.begin();
4819 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004820 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004821 if (res == OK) {
4822 if (listener != NULL) {
4823 listener->notifyPrepared(it->second->getId());
4824 }
4825 it = mPendingStreams.erase(it);
4826 } else if (res != NOT_ENOUGH_DATA) {
4827 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4828 res, strerror(-res));
4829 it = mPendingStreams.erase(it);
4830 } else {
4831 it++;
4832 }
4833 }
4834
4835 if (mPendingStreams.empty()) {
4836 return OK;
4837 }
4838
4839 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4840 if (res != OK) {
4841 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4842 __FUNCTION__, res, strerror(-res));
4843 return res;
4844 }
4845 mCancelNow = false;
4846 mActive = true;
4847 ALOGV("%s: Preparer stream started", __FUNCTION__);
4848
4849 return OK;
4850}
4851
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004852status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004853 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004854 Mutex::Autolock l(mLock);
4855
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004856 for (const auto& it : mPendingStreams) {
4857 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004858 }
4859 mPendingStreams.clear();
4860 mCancelNow = true;
4861
4862 return OK;
4863}
4864
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004865void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004866 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004867 Mutex::Autolock l(mLock);
4868 mListener = listener;
4869}
4870
4871bool Camera3Device::PreparerThread::threadLoop() {
4872 status_t res;
4873 {
4874 Mutex::Autolock l(mLock);
4875 if (mCurrentStream == nullptr) {
4876 // End thread if done with work
4877 if (mPendingStreams.empty()) {
4878 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4879 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4880 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4881 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004882 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004883 return false;
4884 }
4885
4886 // Get next stream to prepare
4887 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004888 mCurrentStream = it->second;
4889 mCurrentMaxCount = it->first;
4890 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004891 mPendingStreams.erase(it);
4892 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4893 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4894 } else if (mCancelNow) {
4895 mCurrentStream->cancelPrepare();
4896 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4897 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4898 mCurrentStream.clear();
4899 mCancelNow = false;
4900 return true;
4901 }
4902 }
4903
4904 res = mCurrentStream->prepareNextBuffer();
4905 if (res == NOT_ENOUGH_DATA) return true;
4906 if (res != OK) {
4907 // Something bad happened; try to recover by cancelling prepare and
4908 // signalling listener anyway
4909 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4910 mCurrentStream->getId(), res, strerror(-res));
4911 mCurrentStream->cancelPrepare();
4912 }
4913
4914 // This stream has finished, notify listener
4915 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004916 sp<NotificationListener> listener = mListener.promote();
4917 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004918 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4919 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004920 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004921 }
4922
4923 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4924 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004925 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004926
4927 return true;
4928}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004929
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004930status_t Camera3Device::RequestBufferStateMachine::initialize(
4931 sp<camera3::StatusTracker> statusTracker) {
4932 if (statusTracker == nullptr) {
4933 ALOGE("%s: statusTracker is null", __FUNCTION__);
4934 return BAD_VALUE;
4935 }
4936
4937 std::lock_guard<std::mutex> lock(mLock);
4938 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004939 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004940 return OK;
4941}
4942
4943bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4944 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004945 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004946 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004947 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004948 return true;
4949 }
4950 return false;
4951}
4952
4953void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4954 std::lock_guard<std::mutex> lock(mLock);
4955 if (!mRequestBufferOngoing) {
4956 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4957 return;
4958 }
4959 mRequestBufferOngoing = false;
4960 if (mStatus == RB_STATUS_PENDING_STOP) {
4961 checkSwitchToStopLocked();
4962 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004963 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004964}
4965
4966void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4967 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004968 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004969 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004970 return;
4971}
4972
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004973void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004974 std::lock_guard<std::mutex> lock(mLock);
4975 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004976 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4977 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004978 mInflightMapEmpty = false;
4979 if (mStatus == RB_STATUS_STOPPED) {
4980 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004981 }
4982 return;
4983}
4984
4985void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4986 std::lock_guard<std::mutex> lock(mLock);
4987 mRequestThreadPaused = true;
4988 if (mStatus == RB_STATUS_PENDING_STOP) {
4989 checkSwitchToStopLocked();
4990 }
4991 return;
4992}
4993
4994void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
4995 std::lock_guard<std::mutex> lock(mLock);
4996 mInflightMapEmpty = true;
4997 if (mStatus == RB_STATUS_PENDING_STOP) {
4998 checkSwitchToStopLocked();
4999 }
5000 return;
5001}
5002
5003void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5004 std::lock_guard<std::mutex> lock(mLock);
5005 if (!checkSwitchToStopLocked()) {
5006 mStatus = RB_STATUS_PENDING_STOP;
5007 }
5008 return;
5009}
5010
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005011bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5012 std::lock_guard<std::mutex> lock(mLock);
5013 if (mRequestBufferOngoing) {
5014 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5015 __FUNCTION__);
5016 return false;
5017 }
5018 mSwitchedToOffline = true;
5019 mInflightMapEmpty = true;
5020 mRequestThreadPaused = true;
5021 mStatus = RB_STATUS_STOPPED;
5022 return true;
5023}
5024
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005025void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5026 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5027 if (statusTracker != nullptr) {
5028 if (active) {
5029 statusTracker->markComponentActive(mRequestBufferStatusId);
5030 } else {
5031 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5032 }
5033 }
5034}
5035
5036bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5037 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5038 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005039 return true;
5040 }
5041 return false;
5042}
5043
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005044bool Camera3Device::startRequestBuffer() {
5045 return mRequestBufferSM.startRequestBuffer();
5046}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005047
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005048void Camera3Device::endRequestBuffer() {
5049 mRequestBufferSM.endRequestBuffer();
5050}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005051
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005052nsecs_t Camera3Device::getWaitDuration() {
5053 return kBaseGetBufferWait + getExpectedInFlightDuration();
5054}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005055
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005056void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5057 mInterface->getInflightBufferKeys(out);
5058}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005059
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005060void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5061 mInterface->getInflightRequestBufferKeys(out);
5062}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005063
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005064std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5065 std::vector<sp<Camera3StreamInterface>> ret;
5066 bool hasInputStream = mInputStream != nullptr;
5067 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5068 if (hasInputStream) {
5069 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005070 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005071 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5072 ret.push_back(mOutputStreams[i]);
5073 }
5074 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5075 ret.push_back(mDeletedStreams[i]);
5076 }
5077 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005078}
5079
Emilian Peevcc0b7952020-01-07 13:54:47 -08005080void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5081 ATRACE_CALL();
5082
5083 if (offlineStreamIds == nullptr) {
5084 return;
5085 }
5086
5087 Mutex::Autolock il(mInterfaceLock);
5088
5089 auto streamIds = mOutputStreams.getStreamIds();
5090 bool hasInputStream = mInputStream != nullptr;
5091 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5092 offlineStreamIds->push_back(mInputStream->getId());
5093 }
5094
5095 for (const auto & streamId : streamIds) {
5096 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5097 // Streams that use the camera buffer manager are currently not supported in
5098 // offline mode
5099 if (stream->getOfflineProcessingSupport() &&
5100 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5101 offlineStreamIds->push_back(streamId);
5102 }
5103 }
5104}
5105
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005106status_t Camera3Device::setRotateAndCropAutoBehavior(
5107 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5108 ATRACE_CALL();
5109 Mutex::Autolock il(mInterfaceLock);
5110 Mutex::Autolock l(mLock);
5111 if (mRequestThread == nullptr) {
5112 return INVALID_OPERATION;
5113 }
5114 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5115}
5116
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005117bool Camera3Device::supportsCameraMute() {
5118 Mutex::Autolock il(mInterfaceLock);
5119 Mutex::Autolock l(mLock);
5120
5121 return mSupportCameraMute;
5122}
5123
5124status_t Camera3Device::setCameraMute(bool enabled) {
5125 ATRACE_CALL();
5126 Mutex::Autolock il(mInterfaceLock);
5127 Mutex::Autolock l(mLock);
5128
5129 if (mRequestThread == nullptr || !mSupportCameraMute) {
5130 return INVALID_OPERATION;
5131 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005132 int32_t muteMode =
5133 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5134 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5135 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5136 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005137}
5138
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005139status_t Camera3Device::injectCamera(const String8& injectedCamId,
5140 sp<CameraProviderManager> manager) {
5141 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5142 ATRACE_CALL();
5143 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005144 // When the camera device is active, injectCamera() and stopInjection() will call
5145 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5146 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5147 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5148 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5149 // waitUntilStateThenRelock().
5150 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005151
5152 status_t res = NO_ERROR;
5153 if (mInjectionMethods->isInjecting()) {
5154 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5155 return OK;
5156 } else {
5157 res = mInjectionMethods->stopInjection();
5158 if (res != OK) {
5159 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5160 __FUNCTION__, res);
5161 return res;
5162 }
5163 }
5164 }
5165
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005166 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005167 if (res != OK) {
5168 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5169 __FUNCTION__, res);
5170 return res;
5171 }
5172
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005173 // When the second display of android is cast to the remote device, and the opened camera is
5174 // also cast to the second display, in this case, because the camera has configured the streams
5175 // at this time, we can directly call injectCamera() to replace the internal camera with
5176 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005177 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5178 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5179
5180 camera3::camera_stream_configuration injectionConfig;
5181 std::vector<uint32_t> injectionBufferSizes;
5182 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5183 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5184 || injectionBufferSizes.size() <= 0) {
5185 ALOGE("Failed to inject camera due to abandoned configuration! "
5186 "mOperatingMode: %d injectionConfig.num_streams: %d "
5187 "injectionBufferSizes.size(): %zu", mOperatingMode,
5188 injectionConfig.num_streams, injectionBufferSizes.size());
5189 return DEAD_OBJECT;
5190 }
5191
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005192 res = mInjectionMethods->injectCamera(
5193 injectionConfig, injectionBufferSizes);
5194 if (res != OK) {
5195 ALOGE("Can't finish inject camera process!");
5196 return res;
5197 }
5198 }
5199
5200 return OK;
5201}
5202
5203status_t Camera3Device::stopInjection() {
5204 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5205 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005206 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005207 return mInjectionMethods->stopInjection();
5208}
5209
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005210}; // namespace android