blob: acee2ab2816093f6b6359b2507f700e68c060618 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080068#include <tuple>
69
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080070using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080071using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072
73namespace android {
74
Austin Borger74fca042022-05-23 12:41:21 -070075Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
76 const String8 &id, bool overrideForPerfClass, bool legacyClient):
77 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080078 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070079 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080080 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070081 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070082 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070083 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070084 mUsePartialResult(false),
85 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080086 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080087 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070088 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070089 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070090 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070092 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000094 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010095 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070096 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070097 mNeedFixupMonochromeTags(false),
98 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800101 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102}
103
104Camera3Device::~Camera3Device()
105{
106 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800107 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700108 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109}
110
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800111const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800112 return mId;
113}
114
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115status_t Camera3Device::initializeCommonLocked() {
116
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700117 /** Start up status tracker thread */
118 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800119 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700120 if (res != OK) {
121 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
122 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700124 mStatusTracker.clear();
125 return res;
126 }
127
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700128 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700129 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700130
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700131 if (mUseHalBufManager) {
132 res = mRequestBufferSM.initialize(mStatusTracker);
133 if (res != OK) {
134 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
135 strerror(-res), res);
136 mInterface->close();
137 mStatusTracker.clear();
138 return res;
139 }
140 }
141
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800142 /** Create buffer manager */
143 mBufferManager = new Camera3BufferManager();
144
145 Vector<int32_t> sessionParamKeys;
146 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
147 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
148 if (sessionKeysEntry.count > 0) {
149 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
150 }
151
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700152 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
153 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
154 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
155 if (availableTestPatternModes.data.i32[i] ==
156 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
157 mSupportCameraMute = true;
158 mSupportTestPatternSolidColor = true;
159 break;
160 } else if (availableTestPatternModes.data.i32[i] ==
161 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
162 mSupportCameraMute = true;
163 mSupportTestPatternSolidColor = false;
164 }
165 }
166
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700167 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800168 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700169 this, mStatusTracker, mInterface, sessionParamKeys,
170 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800171 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800172 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700173 SET_ERR_L("Unable to start request queue thread: %s (%d)",
174 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800175 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800177 return res;
178 }
179
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700180 mPreparerThread = new PreparerThread();
181
Ruben Brunk183f0562015-08-12 12:55:02 -0700182 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400184 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700185 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800187 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800189 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800190 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800191 camera_metadata_entry timestampSource =
192 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
193 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
194 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800195 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800196 }
197
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700198 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100199 camera_metadata_entry partialResultsCount =
200 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
201 if (partialResultsCount.count > 0) {
202 mNumPartialResults = partialResultsCount.data.i32[0];
203 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700204 }
205
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800206 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
207 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700208 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700209 if (res != OK) {
210 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
211 return res;
212 }
213 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800214
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800215 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800216 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800217
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700218 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
219 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
220 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
221 }
222
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800223 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
224 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
225 }
226
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800227 // Hidl/AidlCamera3DeviceInjectionMethods
228 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800229
Ravneetaeb20dc2022-03-30 05:33:03 +0000230 /** Start watchdog thread */
231 mCameraServiceWatchdog = new CameraServiceWatchdog();
Austin Borger7b129542022-06-09 13:23:06 -0700232 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
233 if (res != OK) {
234 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
235 strerror(-res), res);
236 return res;
237 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000238
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 return OK;
240}
241
242status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700243 return disconnectImpl();
244}
245
246status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700248 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800249
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700251 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800253 Mutex::Autolock il(mInterfaceLock);
254 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
269 maxExpectedDuration);
270 // Continue to close device even in case of error
271 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700273 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800274
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800275 if (mStatus == STATUS_ERROR) {
276 CLOGE("Shutting down in an error state");
277 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800279 if (mStatusTracker != NULL) {
280 mStatusTracker->requestExit();
281 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800283 if (mRequestThread != NULL) {
284 mRequestThread->requestExit();
285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800287 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
288 for (size_t i = 0; i < mOutputStreams.size(); i++) {
289 streams.push_back(mOutputStreams[i]);
290 }
291 if (mInputStream != nullptr) {
292 streams.push_back(mInputStream);
293 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800296 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
297 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
299 // HAL may be in a bad state, so waiting for request thread
300 // (which may be stuck in the HAL processCaptureRequest call)
301 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800302 // give up mInterfaceLock here and then lock it again. Could this lead
303 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 mRequestThread->join();
305 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800307 Mutex::Autolock il(mInterfaceLock);
308 if (mStatusTracker != NULL) {
309 mStatusTracker->join();
310 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800312 if (mInjectionMethods->isInjecting()) {
313 mInjectionMethods->stopInjection();
314 }
315
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800316 HalInterface* interface;
317 {
318 Mutex::Autolock l(mLock);
319 mRequestThread.clear();
320 Mutex::Autolock stLock(mTrackerLock);
321 mStatusTracker.clear();
322 interface = mInterface.get();
323 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700324
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800325 // Call close without internal mutex held, as the HAL close may need to
326 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000327 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700328
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800329 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800330
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800331 {
332 Mutex::Autolock l(mLock);
333 mInterface->clear();
334 mOutputStreams.clear();
335 mInputStream.clear();
336 mDeletedStreams.clear();
337 mBufferManager.clear();
338 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
339 }
340
341 for (auto& weakStream : streams) {
342 sp<Camera3StreamInterface> stream = weakStream.promote();
343 if (stream != nullptr) {
344 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
345 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
346 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700347 }
348 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700349 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000350
351 if (mCameraServiceWatchdog != NULL) {
352 mCameraServiceWatchdog->requestExit();
353 mCameraServiceWatchdog.clear();
354 }
355
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700356 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800357}
358
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359// For dumping/debugging only -
360// try to acquire a lock a few times, eventually give up to proceed with
361// debug/dump operations
362bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
363 bool gotLock = false;
364 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
365 if (lock.tryLock() == NO_ERROR) {
366 gotLock = true;
367 break;
368 } else {
369 usleep(kDumpSleepDuration);
370 }
371 }
372 return gotLock;
373}
374
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800375nsecs_t Camera3Device::getMonoToBoottimeOffset() {
376 // try three times to get the clock offset, choose the one
377 // with the minimum gap in measurements.
378 const int tries = 3;
379 nsecs_t bestGap, measured;
380 for (int i = 0; i < tries; ++i) {
381 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
382 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
383 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
384 const nsecs_t gap = tmono2 - tmono;
385 if (i == 0 || gap < bestGap) {
386 bestGap = gap;
387 measured = tbase - ((tmono + tmono2) >> 1);
388 }
389 }
390 return measured;
391}
392
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800393ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
394 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700395 // Get max jpeg size (area-wise) for default sensor pixel mode
396 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800397 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700398 /*isUltraHighResolutionSensor*/false);
399 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
400 // not ultra high res sensor
401 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800402 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700403 /*isUltraHighResolution*/true);
404 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800405 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
406 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700407 return BAD_VALUE;
408 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700409 bool useMaxSensorPixelModeThreshold = false;
410 if (uhrMaxJpegResolution.width != 0 &&
411 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
412 // Use the ultra high res max jpeg size and max jpeg buffer size
413 useMaxSensorPixelModeThreshold = true;
414 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700415
Zhijun Hef7da0962014-04-24 13:27:56 -0700416 // Get max jpeg buffer size
417 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800418 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700419 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800420 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
421 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 return BAD_VALUE;
423 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700425
426 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
427 if (useMaxSensorPixelModeThreshold) {
428 maxJpegBufferSize =
429 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
430 maxDefaultJpegResolution, maxJpegBufferSize);
431 chosenMaxJpegResolution = uhrMaxJpegResolution;
432 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800433 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700434
435 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700436 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700437 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800438 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
439 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700440 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800441 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
442 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700445 return jpegBufferSize;
446}
447
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800448ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700449 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800450 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700451 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800452 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
453 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 return BAD_VALUE;
455 }
456 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
457 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
458 return maxBytesForPointCloud;
459}
460
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800461ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
462 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800463 const int PER_CONFIGURATION_SIZE = 3;
464 const int WIDTH_OFFSET = 0;
465 const int HEIGHT_OFFSET = 1;
466 const int SIZE_OFFSET = 2;
467 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800468 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800469 camera3::SessionConfigurationUtils::getAppropriateModeTag(
470 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
471 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800472 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800473 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800474 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
475 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800476 return BAD_VALUE;
477 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700478
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800479 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
480 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
481 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
482 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
483 }
484 }
485
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800486 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
487 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800488 return BAD_VALUE;
489}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700490
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800491status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
492 ATRACE_CALL();
493 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700494
495 // Try to lock, but continue in case of failure (to avoid blocking in
496 // deadlocks)
497 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
498 bool gotLock = tryLockSpinRightRound(mLock);
499
500 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800501 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
502 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700503 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800504 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
505 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700506
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800507 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700508
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800509 String16 templatesOption("-t");
510 int n = args.size();
511 for (int i = 0; i < n; i++) {
512 if (args[i] == templatesOption) {
513 dumpTemplates = true;
514 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000515 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700516 if (i + 1 < n) {
517 String8 monitorTags = String8(args[i + 1]);
518 if (monitorTags == "off") {
519 mTagMonitor.disableMonitoring();
520 } else {
521 mTagMonitor.parseTagsToMonitor(monitorTags);
522 }
523 } else {
524 mTagMonitor.disableMonitoring();
525 }
526 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800527 }
528
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800529 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800531 const char *status =
532 mStatus == STATUS_ERROR ? "ERROR" :
533 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700534 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
535 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800536 mStatus == STATUS_ACTIVE ? "ACTIVE" :
537 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800539 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700540 if (mStatus == STATUS_ERROR) {
541 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
542 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800544 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000545 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
546 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
547 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800548 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800549
550 if (mInputStream != NULL) {
551 write(fd, lines.string(), lines.size());
552 mInputStream->dump(fd, args);
553 } else {
554 lines.appendFormat(" No input stream.\n");
555 write(fd, lines.string(), lines.size());
556 }
557 for (size_t i = 0; i < mOutputStreams.size(); i++) {
558 mOutputStreams[i]->dump(fd,args);
559 }
560
Zhijun He431503c2016-03-07 17:30:16 -0800561 if (mBufferManager != NULL) {
562 lines = String8(" Camera3 Buffer Manager:\n");
563 write(fd, lines.string(), lines.size());
564 mBufferManager->dump(fd, args);
565 }
Zhijun He125684a2015-12-26 15:07:30 -0800566
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700567 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700568 if (mInFlightLock.try_lock()) {
569 if (mInFlightMap.size() == 0) {
570 lines.append(" None\n");
571 } else {
572 for (size_t i = 0; i < mInFlightMap.size(); i++) {
573 InFlightRequest r = mInFlightMap.valueAt(i);
574 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
575 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
576 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
577 r.numBuffersLeft);
578 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700579 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700580 mInFlightLock.unlock();
581 } else {
582 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 }
584 write(fd, lines.string(), lines.size());
585
Shuzhen Wang686f6442017-06-20 16:16:04 -0700586 if (mRequestThread != NULL) {
587 mRequestThread->dumpCaptureRequestLatency(fd,
588 " ProcessCaptureRequest latency histogram:");
589 }
590
Igor Murashkin1e479c02013-09-06 16:55:14 -0700591 {
592 lines = String8(" Last request sent:\n");
593 write(fd, lines.string(), lines.size());
594
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700596 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
597 }
598
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800599 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700600 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800601 "TEMPLATE_PREVIEW",
602 "TEMPLATE_STILL_CAPTURE",
603 "TEMPLATE_VIDEO_RECORD",
604 "TEMPLATE_VIDEO_SNAPSHOT",
605 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800606 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800607 };
608
Emilian Peevf4816702020-04-03 15:44:51 -0700609 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800610 camera_metadata_t *templateRequest = nullptr;
611 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700612 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800614 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 lines.append(" Not supported\n");
616 write(fd, lines.string(), lines.size());
617 } else {
618 write(fd, lines.string(), lines.size());
619 dump_indented_camera_metadata(templateRequest,
620 fd, /*verbosity*/2, /*indentation*/8);
621 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800622 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800623 }
624 }
625
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700626 mTagMonitor.dumpMonitoredMetadata(fd);
627
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800628 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800629 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800631 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800633
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700634 if (gotLock) mLock.unlock();
635 if (gotInterfaceLock) mInterfaceLock.unlock();
636
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 return OK;
638}
639
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700640status_t Camera3Device::startWatchingTags(const String8 &tags) {
641 mTagMonitor.parseTagsToMonitor(tags);
642 return OK;
643}
644
645status_t Camera3Device::stopWatchingTags() {
646 mTagMonitor.disableMonitoring();
647 return OK;
648}
649
650status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
651 mTagMonitor.getLatestMonitoredTagEvents(out);
652 return OK;
653}
654
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800655const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800656 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
658 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 mStatus == STATUS_ERROR ?
661 "when in error state" : "before init");
662 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700663 if (physicalId.isEmpty()) {
664 return mDeviceInfo;
665 } else {
666 std::string id(physicalId.c_str());
667 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
668 return mPhysicalDeviceInfoMap.at(id);
669 } else {
670 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
671 return mDeviceInfo;
672 }
673 }
674}
675
676const CameraMetadata& Camera3Device::info() const {
677 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800678 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800679}
680
Jianing Wei90e59c92014-03-12 18:29:36 -0700681status_t Camera3Device::checkStatusOkToCaptureLocked() {
682 switch (mStatus) {
683 case STATUS_ERROR:
684 CLOGE("Device has encountered a serious error");
685 return INVALID_OPERATION;
686 case STATUS_UNINITIALIZED:
687 CLOGE("Device not initialized");
688 return INVALID_OPERATION;
689 case STATUS_UNCONFIGURED:
690 case STATUS_CONFIGURED:
691 case STATUS_ACTIVE:
692 // OK
693 break;
694 default:
695 SET_ERR_L("Unexpected status: %d", mStatus);
696 return INVALID_OPERATION;
697 }
698 return OK;
699}
700
701status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000702 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700703 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700704 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700706 if (requestList == NULL) {
707 CLOGE("requestList cannot be NULL.");
708 return BAD_VALUE;
709 }
710
Jianing Weicb0652e2014-03-12 18:29:36 -0700711 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000712 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700713 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
714 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
715 ++metadataIt, ++surfaceMapIt) {
716 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 if (newRequest == 0) {
718 CLOGE("Can't create capture request");
719 return BAD_VALUE;
720 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700721
Shuzhen Wang9d066012016-09-30 11:30:20 -0700722 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700723 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700724
Jianing Weicb0652e2014-03-12 18:29:36 -0700725 // Setup burst Id and request Id
726 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800727 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
728 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700729 CLOGE("RequestID does not exist in metadata");
730 return BAD_VALUE;
731 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800732 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700733
Jianing Wei90e59c92014-03-12 18:29:36 -0700734 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700735
736 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700737 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700738 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
739 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
740 return BAD_VALUE;
741 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700742
743 // Setup batch size if this is a high speed video recording request.
744 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
745 auto firstRequest = requestList->begin();
746 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
747 if (outputStream->isVideoStream()) {
748 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800749 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700750 break;
751 }
752 }
753 }
754
Jianing Wei90e59c92014-03-12 18:29:36 -0700755 return OK;
756}
757
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800758status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Emilian Peevaebbe412018-01-15 13:53:24 +0000761 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000763 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800765 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766}
767
Emilian Peevaebbe412018-01-15 13:53:24 +0000768void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 std::list<const SurfaceMap>& surfaceMaps,
770 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000771 PhysicalCameraSettingsList requestList;
772 requestList.push_back({std::string(getId().string()), request});
773 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774
775 SurfaceMap surfaceMap;
776 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
777 // With no surface list passed in, stream and surface will have 1-to-1
778 // mapping. So the surface index is 0 for each stream in the surfaceMap.
779 for (size_t i = 0; i < streams.count; i++) {
780 surfaceMap[streams.data.i32[i]].push_back(0);
781 }
782 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000786 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 const std::list<const SurfaceMap> &surfaceMaps,
788 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700789 /*out*/
790 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700791 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700792 nsecs_t requestTimeNs = systemTime();
793
Jianing Wei90e59c92014-03-12 18:29:36 -0700794 Mutex::Autolock il(mInterfaceLock);
795 Mutex::Autolock l(mLock);
796
797 status_t res = checkStatusOkToCaptureLocked();
798 if (res != OK) {
799 // error logged by previous call
800 return res;
801 }
802
803 RequestList requestList;
804
Shuzhen Wang0129d522016-10-30 22:43:41 -0700805 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700806 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700807 if (res != OK) {
808 // error logged by previous call
809 return res;
810 }
811
812 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700813 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700814 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700815 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700816 }
817
818 if (res == OK) {
819 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
820 if (res != OK) {
821 SET_ERR_L("Can't transition to active in %f seconds!",
822 kActiveTimeout/1e9);
823 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800824 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700825 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700826 } else {
827 CLOGE("Cannot queue request. Impossible.");
828 return BAD_VALUE;
829 }
830
831 return res;
832}
833
Emilian Peevaebbe412018-01-15 13:53:24 +0000834status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700835 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700836 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700837 ATRACE_CALL();
838
Emilian Peevaebbe412018-01-15 13:53:24 +0000839 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700840}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800841
Jianing Weicb0652e2014-03-12 18:29:36 -0700842status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
843 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845
Emilian Peevaebbe412018-01-15 13:53:24 +0000846 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700847 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000848 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700849
Emilian Peevaebbe412018-01-15 13:53:24 +0000850 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700851 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852}
853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854status_t Camera3Device::setStreamingRequestList(
855 const List<const PhysicalCameraSettingsList> &requestsList,
856 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 ATRACE_CALL();
858
Emilian Peevaebbe412018-01-15 13:53:24 +0000859 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700860}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861
862sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000863 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 status_t res;
865
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800867 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
868 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000869 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700870 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700871 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700873 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700875 } else if (mStatus == STATUS_UNCONFIGURED) {
876 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 CLOGE("No streams configured");
878 return NULL;
879 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 }
881
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800884}
885
Jianing Weicb0652e2014-03-12 18:29:36 -0700886status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800890
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 switch (mStatus) {
892 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700893 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 return INVALID_OPERATION;
895 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700898 case STATUS_UNCONFIGURED:
899 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 case STATUS_ACTIVE:
901 // OK
902 break;
903 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700904 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 return INVALID_OPERATION;
906 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800907 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700908
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700909 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910}
911
912status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
913 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917}
918
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700919status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800920 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700923 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700924 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800925 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
926 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700927
928 status_t res;
929 bool wasActive = false;
930
931 switch (mStatus) {
932 case STATUS_ERROR:
933 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
934 return INVALID_OPERATION;
935 case STATUS_UNINITIALIZED:
936 ALOGE("%s: Device not initialized", __FUNCTION__);
937 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 case STATUS_UNCONFIGURED:
939 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700940 // OK
941 break;
942 case STATUS_ACTIVE:
943 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700944 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700945 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700947 return res;
948 }
949 wasActive = true;
950 break;
951 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700953 return INVALID_OPERATION;
954 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700956
957 if (mInputStream != 0) {
958 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
959 return INVALID_OPERATION;
960 }
961
962 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
963 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700965
966 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800967 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700968
969 *id = mNextStreamId++;
970
971 // Continue captures if active at start
972 if (wasActive) {
973 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100974 // Reuse current operating mode and session parameters for new stream config
975 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700976 if (res != OK) {
977 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
978 __FUNCTION__, mNextStreamId, strerror(-res), res);
979 return res;
980 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700982 }
983
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800984 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700985 return OK;
986}
987
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700988status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700989 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700990 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800991 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800992 const std::unordered_set<int32_t> &sensorPixelModesUsed,
993 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800994 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800995 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700996 ATRACE_CALL();
997
998 if (consumer == nullptr) {
999 ALOGE("%s: consumer must not be null", __FUNCTION__);
1000 return BAD_VALUE;
1001 }
1002
1003 std::vector<sp<Surface>> consumers;
1004 consumers.push_back(consumer);
1005
1006 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001007 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001008 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001009 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001010}
1011
1012static bool isRawFormat(int format) {
1013 switch (format) {
1014 case HAL_PIXEL_FORMAT_RAW16:
1015 case HAL_PIXEL_FORMAT_RAW12:
1016 case HAL_PIXEL_FORMAT_RAW10:
1017 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1018 return true;
1019 default:
1020 return false;
1021 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001022}
1023
1024status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1025 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001026 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001027 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001028 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001029 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1030 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001031 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001032
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001034 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001036 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001037 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001038 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1039 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001040 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001041 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001042 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 status_t res;
1045 bool wasActive = false;
1046
1047 switch (mStatus) {
1048 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 case STATUS_UNCONFIGURED:
1055 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 // OK
1057 break;
1058 case STATUS_ACTIVE:
1059 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001060 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001062 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return res;
1064 }
1065 wasActive = true;
1066 break;
1067 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072
1073 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001074
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 if (consumers.size() == 0 && !hasDeferredConsumer) {
1076 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1077 return BAD_VALUE;
1078 }
Zhijun He5d677d12016-05-29 16:52:39 -07001079
Shuzhen Wang0129d522016-10-30 22:43:41 -07001080 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001081 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1082 return BAD_VALUE;
1083 }
1084
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001085 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1086 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1087 // be found in only one sensor pixel mode.
1088 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1089 return BAD_VALUE;
1090 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001091 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001093 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001094 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001095 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001096 if (blobBufferSize <= 0) {
1097 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1098 return BAD_VALUE;
1099 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001100 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1101 blobBufferSize = width * height;
1102 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001103 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001104 if (blobBufferSize <= 0) {
1105 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1106 return BAD_VALUE;
1107 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001108 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001109 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001110 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001111 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001112 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001113 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001114 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001115 bool maxResolution =
1116 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1117 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001118 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1119 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001120 if (rawOpaqueBufferSize <= 0) {
1121 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1122 return BAD_VALUE;
1123 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001124 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001125 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001126 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001127 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001128 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001129 } else if (isShared) {
1130 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1131 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001132 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001133 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001134 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001135 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001136 newStream = new Camera3OutputStream(mNextStreamId,
1137 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001138 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001139 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001140 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001142 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001143 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001144 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001145 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001146 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 }
Emilian Peev40ead602017-09-26 15:46:36 +01001148
1149 size_t consumerCount = consumers.size();
1150 for (size_t i = 0; i < consumerCount; i++) {
1151 int id = newStream->getSurfaceId(consumers[i]);
1152 if (id < 0) {
1153 SET_ERR_L("Invalid surface id");
1154 return BAD_VALUE;
1155 }
1156 if (surfaceIds != nullptr) {
1157 surfaceIds->push_back(id);
1158 }
1159 }
1160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162
Emilian Peev08dd2452017-04-06 16:55:14 +01001163 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001164
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001165 newStream->setImageDumpMask(mImageDumpMask);
1166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 res = mOutputStreams.add(mNextStreamId, newStream);
1168 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001169 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 return res;
1171 }
1172
Shuzhen Wang316781a2020-08-18 18:11:01 -07001173 mSessionStatsBuilder.addStream(mNextStreamId);
1174
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001176 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177
1178 // Continue captures if active at start
1179 if (wasActive) {
1180 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001181 // Reuse current operating mode and session parameters for new stream config
1182 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1185 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 return res;
1187 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001189 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001190 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
Emilian Peev710c1422017-08-30 11:19:38 +01001194status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001196 if (nullptr == streamInfo) {
1197 return BAD_VALUE;
1198 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001201
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 switch (mStatus) {
1203 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001204 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return INVALID_OPERATION;
1206 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 case STATUS_UNCONFIGURED:
1210 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 case STATUS_ACTIVE:
1212 // OK
1213 break;
1214 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 return INVALID_OPERATION;
1217 }
1218
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001219 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1220 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001222 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 }
1224
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001225 streamInfo->width = stream->getWidth();
1226 streamInfo->height = stream->getHeight();
1227 streamInfo->format = stream->getFormat();
1228 streamInfo->dataSpace = stream->getDataSpace();
1229 streamInfo->formatOverridden = stream->isFormatOverridden();
1230 streamInfo->originalFormat = stream->getOriginalFormat();
1231 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1232 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001233 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235}
1236
1237status_t Camera3Device::setStreamTransform(int id,
1238 int transform) {
1239 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001240 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 switch (mStatus) {
1244 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
1247 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 case STATUS_UNCONFIGURED:
1251 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 case STATUS_ACTIVE:
1253 // OK
1254 break;
1255 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return INVALID_OPERATION;
1258 }
1259
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001260 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1261 if (stream == nullptr) {
1262 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return BAD_VALUE;
1264 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001265 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
1268status_t Camera3Device::deleteStream(int id) {
1269 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 Mutex::Autolock l(mLock);
1272 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001274 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001276 // CameraDevice semantics require device to already be idle before
1277 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001279 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001280 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 }
1282
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001283 if (mStatus == STATUS_ERROR) {
1284 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1285 __FUNCTION__, mId.string());
1286 return -EBUSY;
1287 }
1288
Igor Murashkin2fba5842013-04-22 14:03:54 -07001289 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001290 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 if (mInputStream != NULL && id == mInputStream->getId()) {
1292 deletedStream = mInputStream;
1293 mInputStream.clear();
1294 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001295 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001296 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 return BAD_VALUE;
1298 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001299 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001300 }
1301
1302 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001303 if (stream != nullptr) {
1304 deletedStream = stream;
1305 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 }
1307
1308 // Free up the stream endpoint so that it can be used by some other stream
1309 res = deletedStream->disconnect();
1310 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 // fall through since we want to still list the stream as deleted.
1313 }
1314 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001315 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316
1317 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318}
1319
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001320status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001321 ATRACE_CALL();
1322 ALOGV("%s: E", __FUNCTION__);
1323
1324 Mutex::Autolock il(mInterfaceLock);
1325 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001326
Emilian Peev811d2952018-05-25 11:08:40 +01001327 // In case the client doesn't include any session parameter, try a
1328 // speculative configuration using the values from the last cached
1329 // default request.
1330 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001331 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001332 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1333 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1334 mLastTemplateId);
1335 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1336 operatingMode);
1337 }
1338
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001339 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1340}
1341
1342status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1343 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001344 //Filter out any incoming session parameters
1345 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001346 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1347 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001348 CameraMetadata filteredParams(availableSessionKeys.count);
1349 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1350 filteredParams.getAndLock());
1351 set_camera_metadata_vendor_id(meta, mVendorTagId);
1352 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001353 if (availableSessionKeys.count > 0) {
1354 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1355 camera_metadata_ro_entry entry = params.find(
1356 availableSessionKeys.data.i32[i]);
1357 if (entry.count > 0) {
1358 filteredParams.update(entry);
1359 }
1360 }
1361 }
1362
1363 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001364}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001366status_t Camera3Device::getInputBufferProducer(
1367 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001368 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001369 Mutex::Autolock il(mInterfaceLock);
1370 Mutex::Autolock l(mLock);
1371
1372 if (producer == NULL) {
1373 return BAD_VALUE;
1374 } else if (mInputStream == NULL) {
1375 return INVALID_OPERATION;
1376 }
1377
1378 return mInputStream->getInputBufferProducer(producer);
1379}
1380
Emilian Peevf4816702020-04-03 15:44:51 -07001381status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382 CameraMetadata *request) {
1383 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001384 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001385
Emilian Peevf4816702020-04-03 15:44:51 -07001386 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001387 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001388 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001389 return BAD_VALUE;
1390 }
1391
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001394 {
1395 Mutex::Autolock l(mLock);
1396 switch (mStatus) {
1397 case STATUS_ERROR:
1398 CLOGE("Device has encountered a serious error");
1399 return INVALID_OPERATION;
1400 case STATUS_UNINITIALIZED:
1401 CLOGE("Device is not initialized!");
1402 return INVALID_OPERATION;
1403 case STATUS_UNCONFIGURED:
1404 case STATUS_CONFIGURED:
1405 case STATUS_ACTIVE:
1406 // OK
1407 break;
1408 default:
1409 SET_ERR_L("Unexpected status: %d", mStatus);
1410 return INVALID_OPERATION;
1411 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001412
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001413 if (!mRequestTemplateCache[templateId].isEmpty()) {
1414 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001415 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001416 return OK;
1417 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001418 }
1419
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001420 camera_metadata_t *rawRequest;
1421 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001422 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001423
1424 {
1425 Mutex::Autolock l(mLock);
1426 if (res == BAD_VALUE) {
1427 ALOGI("%s: template %d is not supported on this camera device",
1428 __FUNCTION__, templateId);
1429 return res;
1430 } else if (res != OK) {
1431 CLOGE("Unable to construct request template %d: %s (%d)",
1432 templateId, strerror(-res), res);
1433 return res;
1434 }
1435
1436 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1437 mRequestTemplateCache[templateId].acquire(rawRequest);
1438
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001439 // Override the template request with zoomRatioMapper
1440 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1441 &mRequestTemplateCache[templateId]);
1442 if (res != OK) {
1443 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1444 templateId, strerror(-res), res);
1445 return res;
1446 }
1447
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001448 // Fill in JPEG_QUALITY if not available
1449 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1450 static const uint8_t kDefaultJpegQuality = 95;
1451 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1452 &kDefaultJpegQuality, 1);
1453 }
1454
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001455 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001456 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001457 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458 return OK;
1459}
1460
1461status_t Camera3Device::waitUntilDrained() {
1462 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001463 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001464 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001467 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001468}
1469
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001470status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 switch (mStatus) {
1472 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001473 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 ALOGV("%s: Already idle", __FUNCTION__);
1475 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 case STATUS_CONFIGURED:
1477 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 case STATUS_ERROR:
1479 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 break;
1482 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484 return INVALID_OPERATION;
1485 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001486 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1487 maxExpectedDuration);
1488 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001489 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001490 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001491 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1492 res);
1493 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001494 return res;
1495}
1496
Ruben Brunk183f0562015-08-12 12:55:02 -07001497void Camera3Device::internalUpdateStatusLocked(Status status) {
1498 mStatus = status;
1499 mRecentStatusUpdates.add(mStatus);
1500 mStatusChanged.broadcast();
1501}
1502
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001503// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001504status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001505 if (mRequestThread.get() != nullptr) {
1506 mRequestThread->setPaused(true);
1507 } else {
1508 return NO_INIT;
1509 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001511 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1512 maxExpectedDuration);
1513 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001514 if (res != OK) {
1515 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001516 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517 }
1518
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001519 return res;
1520}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001521
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001522// Resume after internalPauseAndWaitLocked
1523status_t Camera3Device::internalResumeLocked() {
1524 status_t res;
1525
1526 mRequestThread->setPaused(false);
1527
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001528 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1529 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001530 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1531 if (res != OK) {
1532 SET_ERR_L("Can't transition to active in %f seconds!",
1533 kActiveTimeout/1e9);
1534 }
1535 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001537}
1538
Ruben Brunk183f0562015-08-12 12:55:02 -07001539status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001540 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001541
1542 size_t startIndex = 0;
1543 if (mStatusWaiters == 0) {
1544 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1545 // this status list
1546 mRecentStatusUpdates.clear();
1547 } else {
1548 // If other threads are waiting on updates to this status list, set the position of the
1549 // first element that this list will check rather than clearing the list.
1550 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001551 }
1552
Ruben Brunk183f0562015-08-12 12:55:02 -07001553 mStatusWaiters++;
1554
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001555 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001556 if (!active && mUseHalBufManager) {
1557 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001558 if (mStatus == STATUS_ACTIVE) {
1559 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001560 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001561 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001562 mRequestBufferSM.onWaitUntilIdle();
1563 }
1564
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001566 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001568 if (active == (mStatus == STATUS_ACTIVE)) {
1569 // Desired state is current
1570 break;
1571 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001572
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001573 nsecs_t timeElapsed = systemTime() - startTime;
1574 nsecs_t timeToWait = timeout - timeElapsed;
1575 if (timeToWait <= 0) {
1576 // Thread woke up spuriously but has timed out since.
1577 // Force out of loop with TIMED_OUT result.
1578 res = TIMED_OUT;
1579 break;
1580 }
1581 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001582 if (res != OK) break;
1583
Ruben Brunk183f0562015-08-12 12:55:02 -07001584 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1585 // transitions.
1586 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1587 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1588 __FUNCTION__);
1589
1590 // Encountered desired state since we began waiting
1591 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1593 stateSeen = true;
1594 break;
1595 }
1596 }
1597 } while (!stateSeen);
1598
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001599 if (signalPipelineDrain) {
1600 mRequestThread->resetPipelineDrain();
1601 }
1602
Ruben Brunk183f0562015-08-12 12:55:02 -07001603 mStatusWaiters--;
1604
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001605 return res;
1606}
1607
1608
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001609status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001610 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001611 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001612
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001613 if (listener != NULL && mListener != NULL) {
1614 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1615 }
1616 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001617 mRequestThread->setNotificationListener(listener);
1618 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001619
1620 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001621}
1622
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001623bool Camera3Device::willNotify3A() {
1624 return false;
1625}
1626
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001627status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001628 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001629 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001630
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001631 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001632 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1633 if (st == std::cv_status::timeout) {
1634 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001635 }
1636 }
1637 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638}
1639
Jianing Weicb0652e2014-03-12 18:29:36 -07001640status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001641 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001642 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001643
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001644 if (mResultQueue.empty()) {
1645 return NOT_ENOUGH_DATA;
1646 }
1647
Jianing Weicb0652e2014-03-12 18:29:36 -07001648 if (frame == NULL) {
1649 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1650 return BAD_VALUE;
1651 }
1652
1653 CaptureResult &result = *(mResultQueue.begin());
1654 frame->mResultExtras = result.mResultExtras;
1655 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001656 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001657 mResultQueue.erase(mResultQueue.begin());
1658
1659 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660}
1661
1662status_t Camera3Device::triggerAutofocus(uint32_t id) {
1663 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001664 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001665
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001666 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1667 // Mix-in this trigger into the next request and only the next request.
1668 RequestTrigger trigger[] = {
1669 {
1670 ANDROID_CONTROL_AF_TRIGGER,
1671 ANDROID_CONTROL_AF_TRIGGER_START
1672 },
1673 {
1674 ANDROID_CONTROL_AF_TRIGGER_ID,
1675 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001676 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001677 };
1678
1679 return mRequestThread->queueTrigger(trigger,
1680 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001681}
1682
1683status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1684 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001685 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001686
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001687 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1688 // Mix-in this trigger into the next request and only the next request.
1689 RequestTrigger trigger[] = {
1690 {
1691 ANDROID_CONTROL_AF_TRIGGER,
1692 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1693 },
1694 {
1695 ANDROID_CONTROL_AF_TRIGGER_ID,
1696 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001697 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001698 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001699
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001700 return mRequestThread->queueTrigger(trigger,
1701 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001702}
1703
1704status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1705 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001706 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001708 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1709 // Mix-in this trigger into the next request and only the next request.
1710 RequestTrigger trigger[] = {
1711 {
1712 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1713 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1714 },
1715 {
1716 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1717 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001718 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001719 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001720
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001721 return mRequestThread->queueTrigger(trigger,
1722 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001723}
1724
Jianing Weicb0652e2014-03-12 18:29:36 -07001725status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001726 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001727 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001728 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001729
Zhijun He7ef20392014-04-21 16:04:17 -07001730 {
1731 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001732
1733 // b/116514106 "disconnect()" can get called twice for the same device. The
1734 // camera device will not be initialized during the second run.
1735 if (mStatus == STATUS_UNINITIALIZED) {
1736 return OK;
1737 }
1738
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001739 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001740
1741 // Stop session and stream counter
1742 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001743 }
1744
Ravneetdbd5b242022-03-02 07:22:46 +00001745 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1746 uint64_t maxExpectedDuration = (getExpectedInFlightDuration() + kBaseGetBufferWait) / 1e6;
1747 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1748 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1749
1750 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001751}
1752
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001753status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001754 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1755}
1756
1757status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001758 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001759 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001760 Mutex::Autolock il(mInterfaceLock);
1761 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001762
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001763 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1764 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001765 CLOGE("Stream %d does not exist", streamId);
1766 return BAD_VALUE;
1767 }
1768
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001769 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001770 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001771 return BAD_VALUE;
1772 }
1773
1774 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001775 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001776 return BAD_VALUE;
1777 }
1778
Ruben Brunkc78ac262015-08-13 17:58:46 -07001779 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001780}
1781
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001782status_t Camera3Device::tearDown(int streamId) {
1783 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001784 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001785 Mutex::Autolock il(mInterfaceLock);
1786 Mutex::Autolock l(mLock);
1787
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001788 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1789 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001790 CLOGE("Stream %d does not exist", streamId);
1791 return BAD_VALUE;
1792 }
1793
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001794 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1795 CLOGE("Stream %d is a target of a in-progress request", streamId);
1796 return BAD_VALUE;
1797 }
1798
1799 return stream->tearDown();
1800}
1801
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001802status_t Camera3Device::addBufferListenerForStream(int streamId,
1803 wp<Camera3StreamBufferListener> listener) {
1804 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001805 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001806 Mutex::Autolock il(mInterfaceLock);
1807 Mutex::Autolock l(mLock);
1808
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001809 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1810 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001811 CLOGE("Stream %d does not exist", streamId);
1812 return BAD_VALUE;
1813 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001814 stream->addBufferListener(listener);
1815
1816 return OK;
1817}
1818
Austin Borger4a870a32022-02-25 01:48:41 +00001819float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1820 camera_metadata_entry minDurations =
1821 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1822 for (size_t i = 0; i < minDurations.count; i += 4) {
1823 if (minDurations.data.i64[i] == stream->getFormat()
1824 && minDurations.data.i64[i+1] == stream->getWidth()
1825 && minDurations.data.i64[i+2] == stream->getHeight()) {
1826 int64_t minFrameDuration = minDurations.data.i64[i+3];
1827 return 1e9f / minFrameDuration;
1828 }
1829 }
1830 return 0.0f;
1831}
1832
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001834 * Methods called by subclasses
1835 */
1836
1837void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001838 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001839 std::vector<int> streamIds;
1840 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001841 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001842
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001843 {
1844 // Need mLock to safely update state and synchronize to current
1845 // state of methods in flight.
1846 Mutex::Autolock l(mLock);
1847 // We can get various system-idle notices from the status tracker
1848 // while starting up. Only care about them if we've actually sent
1849 // in some requests recently.
1850 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1851 return;
1852 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001853 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1854 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001855 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001856
1857 // Skip notifying listener if we're doing some user-transparent
1858 // state changes
1859 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001860
Austin Borger4a870a32022-02-25 01:48:41 +00001861 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1862 auto stream = mOutputStreams[i];
1863 if (stream.get() == nullptr) continue;
1864
1865 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1866 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1867
1868 // Populate stream statistics in case of Idle
1869 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001870 streamIds.push_back(stream->getId());
1871 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1872 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001873 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001874 if (camera3Stream != nullptr) {
1875 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001876 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001877 }
1878 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001879 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001880 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001881 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001882 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001883 }
1884 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001885 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001886
1887 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001888 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001889 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001890 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001891 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001892 status_t res = OK;
1893 if (listener != nullptr) {
1894 if (idle) {
1895 // Get session stats from the builder, and notify the listener.
1896 int64_t requestCount, resultErrorCount;
1897 bool deviceError;
1898 std::map<int, StreamStats> streamStatsMap;
1899 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1900 &deviceError, &streamStatsMap);
1901 for (size_t i = 0; i < streamIds.size(); i++) {
1902 int streamId = streamIds[i];
1903 auto stats = streamStatsMap.find(streamId);
1904 if (stats != streamStatsMap.end()) {
1905 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1906 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1907 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1908 streamStats[i].mHistogramType =
1909 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1910 streamStats[i].mHistogramBins.assign(
1911 stats->second.mCaptureLatencyBins.begin(),
1912 stats->second.mCaptureLatencyBins.end());
1913 streamStats[i].mHistogramCounts.assign(
1914 stats->second.mCaptureLatencyHistogram.begin(),
1915 stats->second.mCaptureLatencyHistogram.end());
1916 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001917 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001918 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1919 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001920 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001921 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001922 }
1923 if (res != OK) {
1924 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1925 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001926 }
1927}
1928
Shuzhen Wang758c2152017-01-10 18:26:18 -08001929status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001930 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001931 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001932 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1933 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001934
1935 if (surfaceIds == nullptr) {
1936 return BAD_VALUE;
1937 }
1938
Zhijun He5d677d12016-05-29 16:52:39 -07001939 Mutex::Autolock il(mInterfaceLock);
1940 Mutex::Autolock l(mLock);
1941
Shuzhen Wang758c2152017-01-10 18:26:18 -08001942 if (consumers.size() == 0) {
1943 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001944 return BAD_VALUE;
1945 }
1946
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001947 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1948 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001949 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001950 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001951 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001952
1953 // isConsumerConfigurationDeferred will be off after setConsumers
1954 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001955 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001956 if (res != OK) {
1957 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1958 return res;
1959 }
1960
Emilian Peev40ead602017-09-26 15:46:36 +01001961 for (auto &consumer : consumers) {
1962 int id = stream->getSurfaceId(consumer);
1963 if (id < 0) {
1964 CLOGE("Invalid surface id!");
1965 return BAD_VALUE;
1966 }
1967 surfaceIds->push_back(id);
1968 }
1969
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001970 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001971 if (!stream->isConfiguring()) {
1972 CLOGE("Stream %d was already fully configured.", streamId);
1973 return INVALID_OPERATION;
1974 }
Zhijun He5d677d12016-05-29 16:52:39 -07001975
Shuzhen Wang0129d522016-10-30 22:43:41 -07001976 res = stream->finishConfiguration();
1977 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001978 // If finishConfiguration fails due to abandoned surface, do not set
1979 // device to error state.
1980 bool isSurfaceAbandoned =
1981 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1982 if (!isSurfaceAbandoned) {
1983 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1984 stream->getId(), strerror(-res), res);
1985 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001986 return res;
1987 }
Zhijun He5d677d12016-05-29 16:52:39 -07001988 }
1989
1990 return OK;
1991}
1992
Emilian Peev40ead602017-09-26 15:46:36 +01001993status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1994 const std::vector<OutputStreamInfo> &outputInfo,
1995 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1996 Mutex::Autolock il(mInterfaceLock);
1997 Mutex::Autolock l(mLock);
1998
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001999 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2000 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002001 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002002 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002003 }
2004
2005 for (const auto &it : removedSurfaceIds) {
2006 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2007 CLOGE("Shared surface still part of a pending request!");
2008 return -EBUSY;
2009 }
2010 }
2011
Emilian Peev40ead602017-09-26 15:46:36 +01002012 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2013 if (res != OK) {
2014 CLOGE("Stream %d failed to update stream (error %d %s) ",
2015 streamId, res, strerror(-res));
2016 if (res == UNKNOWN_ERROR) {
2017 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2018 __FUNCTION__);
2019 }
2020 return res;
2021 }
2022
2023 return res;
2024}
2025
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002026status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2027 Mutex::Autolock il(mInterfaceLock);
2028 Mutex::Autolock l(mLock);
2029
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002030 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2031 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002032 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2033 return BAD_VALUE;
2034 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002035
2036 if (dropping) {
2037 mSessionStatsBuilder.stopCounter(streamId);
2038 } else {
2039 mSessionStatsBuilder.startCounter(streamId);
2040 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002041 return stream->dropBuffers(dropping);
2042}
2043
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002044/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 * Camera3Device private methods
2046 */
2047
2048sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002049 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002051
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002052 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002053 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002054
2055 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002056 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057 if (inputStreams.count > 0) {
2058 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002059 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002060 CLOGE("Request references unknown input stream %d",
2061 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002062 return NULL;
2063 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002064
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002066 SET_ERR_L("%s: input stream %d is not configured!",
2067 __FUNCTION__, mInputStream->getId());
2068 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002070 // Check if stream prepare is blocking requests.
2071 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002072 CLOGE("Request references an input stream that's being prepared!");
2073 return NULL;
2074 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075
2076 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002077 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002078 }
2079
2080 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002081 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002083 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002084 return NULL;
2085 }
2086
2087 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002088 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2089 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002090 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002091 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 return NULL;
2093 }
Zhijun He5d677d12016-05-29 16:52:39 -07002094 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002095 auto iter = surfaceMap.find(streams.data.i32[i]);
2096 if (iter != surfaceMap.end()) {
2097 const std::vector<size_t>& surfaces = iter->second;
2098 for (const auto& surface : surfaces) {
2099 if (stream->isConsumerConfigurationDeferred(surface)) {
2100 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2101 "due to deferred consumer", stream->getId(), surface);
2102 return NULL;
2103 }
2104 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002105 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002106 }
2107
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002108 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002109 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2110 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002111 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002112 // Check if stream prepare is blocking requests.
2113 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002114 CLOGE("Request references an output stream that's being prepared!");
2115 return NULL;
2116 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002117
2118 newRequest->mOutputStreams.push(stream);
2119 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002120 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002121 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002122
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002123 auto rotateAndCropEntry =
2124 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2125 if (rotateAndCropEntry.count > 0 &&
2126 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2127 newRequest->mRotateAndCropAuto = true;
2128 } else {
2129 newRequest->mRotateAndCropAuto = false;
2130 }
2131
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002132 auto zoomRatioEntry =
2133 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2134 if (zoomRatioEntry.count > 0 &&
2135 zoomRatioEntry.data.f[0] == 1.0f) {
2136 newRequest->mZoomRatioIs1x = true;
2137 } else {
2138 newRequest->mZoomRatioIs1x = false;
2139 }
2140
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002141 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002142 for (auto& settings : newRequest->mSettingsList) {
2143 auto testPatternModeEntry =
2144 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2145 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2146 testPatternModeEntry.data.i32[0] :
2147 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002148
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002149 auto testPatternDataEntry =
2150 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2151 if (testPatternDataEntry.count >= 4) {
2152 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2153 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2154 } else {
2155 settings.mOriginalTestPatternData[0] = 0;
2156 settings.mOriginalTestPatternData[1] = 0;
2157 settings.mOriginalTestPatternData[2] = 0;
2158 settings.mOriginalTestPatternData[3] = 0;
2159 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002160 }
2161 }
2162
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002163 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002164}
2165
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002166void Camera3Device::cancelStreamsConfigurationLocked() {
2167 int res = OK;
2168 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2169 res = mInputStream->cancelConfiguration();
2170 if (res != OK) {
2171 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2172 mInputStream->getId(), strerror(-res), res);
2173 }
2174 }
2175
2176 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002177 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002178 if (outputStream->isConfiguring()) {
2179 res = outputStream->cancelConfiguration();
2180 if (res != OK) {
2181 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2182 outputStream->getId(), strerror(-res), res);
2183 }
2184 }
2185 }
2186
2187 // Return state to that at start of call, so that future configures
2188 // properly clean things up
2189 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2190 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002191
2192 res = mPreparerThread->resume();
2193 if (res != OK) {
2194 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2195 }
2196}
2197
Emilian Peev0d0191e2020-04-21 17:01:18 -07002198bool Camera3Device::checkAbandonedStreamsLocked() {
2199 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2200 return true;
2201 }
2202
2203 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2204 auto stream = mOutputStreams[i];
2205 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2206 return true;
2207 }
2208 }
2209
2210 return false;
2211}
2212
Emilian Peev3bead5f2020-05-28 17:29:08 -07002213bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002214 ATRACE_CALL();
2215 bool ret = false;
2216
Shuzhen Wang316781a2020-08-18 18:11:01 -07002217 nsecs_t startTime = systemTime();
2218
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002219 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002220 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2221
2222 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002223 if (checkAbandonedStreamsLocked()) {
2224 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2225 __FUNCTION__);
2226 return true;
2227 }
2228
Emilian Peev3bead5f2020-05-28 17:29:08 -07002229 status_t rc = NO_ERROR;
2230 bool markClientActive = false;
2231 if (mStatus == STATUS_ACTIVE) {
2232 markClientActive = true;
2233 mPauseStateNotify = true;
2234 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2235
2236 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2237 }
2238
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002239 if (rc == NO_ERROR) {
2240 mNeedConfig = true;
2241 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2242 if (rc == NO_ERROR) {
2243 ret = true;
2244 mPauseStateNotify = false;
2245 //Moving to active state while holding 'mLock' is important.
2246 //There could be pending calls to 'create-/deleteStream' which
2247 //will trigger another stream configuration while the already
2248 //present streams end up with outstanding buffers that will
2249 //not get drained.
2250 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002251 } else if (rc == DEAD_OBJECT) {
2252 // DEAD_OBJECT can be returned if either the consumer surface is
2253 // abandoned, or the HAL has died.
2254 // - If the HAL has died, configureStreamsLocked call will set
2255 // device to error state,
2256 // - If surface is abandoned, we should not set device to error
2257 // state.
2258 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002259 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002260 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002261 }
2262 } else {
2263 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2264 }
2265
Austin Borger74fca042022-05-23 12:41:21 -07002266 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002267 ns2ms(systemTime() - startTime));
2268
Emilian Peev3bead5f2020-05-28 17:29:08 -07002269 if (markClientActive) {
2270 mStatusTracker->markComponentActive(clientStatusId);
2271 }
2272
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002273 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002274}
2275
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002276status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002277 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278 ATRACE_CALL();
2279 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002281 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002282 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002283 return INVALID_OPERATION;
2284 }
2285
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002286 if (operatingMode < 0) {
2287 CLOGE("Invalid operating mode: %d", operatingMode);
2288 return BAD_VALUE;
2289 }
2290
2291 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002292 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002293
2294 if (mOperatingMode != operatingMode) {
2295 mNeedConfig = true;
2296 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2297 mOperatingMode = operatingMode;
2298 }
2299
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002300 // Reset min expected duration when session is reconfigured.
2301 mMinExpectedDuration = 0;
2302
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002303 // In case called from configureStreams, abort queued input buffers not belonging to
2304 // any pending requests.
2305 if (mInputStream != NULL && notifyRequestThread) {
2306 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002307 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002308 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002309 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002310 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002311 if (res != OK) {
2312 // Exhausted acquiring all input buffers.
2313 break;
2314 }
2315
Emilian Peevf4816702020-04-03 15:44:51 -07002316 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002317 res = mInputStream->returnInputBuffer(inputBuffer);
2318 if (res != OK) {
2319 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2320 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2321 }
2322 }
2323 }
2324
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002325 if (!mNeedConfig) {
2326 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2327 return OK;
2328 }
2329
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002330 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002331 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002332 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2333 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002334 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002335 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002336 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002337 }
2338
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002339 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002340 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002341
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002342 mPreparerThread->pause();
2343
Emilian Peevf4816702020-04-03 15:44:51 -07002344 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002345 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002346 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002347 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002348
Emilian Peevf4816702020-04-03 15:44:51 -07002349 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002350 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002351 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002352
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002353
2354 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002355 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002356 inputStream = mInputStream->startConfiguration();
2357 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002358 CLOGE("Can't start input stream configuration");
2359 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002360 return INVALID_OPERATION;
2361 }
2362 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002363
2364 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365 }
2366
Shuzhen Wang99080502021-03-07 21:08:20 -08002367 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002368 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002369 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002370
2371 // Don't configure bidi streams twice, nor add them twice to the list
2372 if (mOutputStreams[i].get() ==
2373 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2374
2375 config.num_streams--;
2376 continue;
2377 }
2378
Emilian Peevf4816702020-04-03 15:44:51 -07002379 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002380 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002381 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002382 CLOGE("Can't start output stream configuration");
2383 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002384 return INVALID_OPERATION;
2385 }
2386 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002387
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002388 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002389 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2390 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002391 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2392 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002393 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2394 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002395 } else if (outputStream->data_space ==
2396 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2397 bufferSizes[k] = outputStream->width * outputStream->height;
2398 } else {
2399 ALOGW("%s: Blob dataSpace %d not supported",
2400 __FUNCTION__, outputStream->data_space);
2401 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002402 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002403
2404 if (mOutputStreams[i]->isMultiResolution()) {
2405 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2406 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2407 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2408 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002409
2410 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2411 composerSurfacePresent = true;
2412 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413 }
2414
2415 config.streams = streams.editArray();
2416
2417 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002418 // max_buffers, usage, and priv fields, as well as data_space and format
2419 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002420
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002421 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002422 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002423 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002424
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002425 if (res == BAD_VALUE) {
2426 // HAL rejected this set of streams as unsupported, clean up config
2427 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002428 CLOGE("Set of requested inputs/outputs not supported by HAL");
2429 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002430 return BAD_VALUE;
2431 } else if (res != OK) {
2432 // Some other kind of error from configure_streams - this is not
2433 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002434 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2435 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002436 return res;
2437 }
2438
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002439 // Finish all stream configuration immediately.
2440 // TODO: Try to relax this later back to lazy completion, which should be
2441 // faster
2442
Igor Murashkin073f8572013-05-02 14:59:28 -07002443 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002444 bool streamReConfigured = false;
2445 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002446 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002447 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002448 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002449 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002450 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2451 return DEAD_OBJECT;
2452 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002453 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002454 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002455 if (streamReConfigured) {
2456 mInterface->onStreamReConfigured(mInputStream->getId());
2457 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002458 }
2459
2460 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002461 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002462 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002463 bool streamReConfigured = false;
2464 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002465 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002466 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002467 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002468 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002469 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2470 return DEAD_OBJECT;
2471 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002472 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002473 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002474 if (streamReConfigured) {
2475 mInterface->onStreamReConfigured(outputStream->getId());
2476 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002477 }
2478 }
2479
Emilian Peeve23f1d92021-09-20 14:56:01 -07002480 mRequestThread->setComposerSurface(composerSurfacePresent);
2481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482 // Request thread needs to know to avoid using repeat-last-settings protocol
2483 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002484 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002485 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2486 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002487 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002488
Zhijun He90f7c372016-08-16 16:19:43 -07002489 char value[PROPERTY_VALUE_MAX];
2490 property_get("camera.fifo.disable", value, "0");
2491 int32_t disableFifo = atoi(value);
2492 if (disableFifo != 1) {
2493 // Boost priority of request thread to SCHED_FIFO.
2494 pid_t requestThreadTid = mRequestThread->getTid();
2495 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002496 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002497 if (res != OK) {
2498 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2499 strerror(-res), res);
2500 } else {
2501 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2502 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002503 }
2504
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002505 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002506 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2507 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2508 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2509 sessionParams.unlock(newSessionParams);
2510 mSessionParams.unlock(currentSessionParams);
2511 if (updateSessionParams) {
2512 mSessionParams = sessionParams;
2513 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002514
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002515 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002516
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002517 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002518 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002519
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002520 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002521
Zhijun He0a210512014-07-24 13:45:15 -07002522 // tear down the deleted streams after configure streams.
2523 mDeletedStreams.clear();
2524
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002525 auto rc = mPreparerThread->resume();
2526 if (rc != OK) {
2527 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2528 return rc;
2529 }
2530
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002531 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002532 mRequestBufferSM.onStreamsConfigured();
2533 }
2534
Cliff Wu3b268182021-07-06 15:44:43 +08002535 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002536 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002537 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002538 // configure the injection streams.
2539 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002540 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002541 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2542 res = mInjectionMethods->injectCamera(config, bufferSizes);
2543 if (res != OK) {
2544 ALOGE("Can't finish inject camera process!");
2545 return res;
2546 }
Cliff Wu3b268182021-07-06 15:44:43 +08002547 } else {
2548 // First run configureStreamsLocked() and then call injectCamera() case:
2549 // If the stream configuration has been completed and camera deive is active, but the
2550 // injection camera has not been injected yet, we need to store the stream configuration of
2551 // the internal camera (because the stream configuration of the injection camera is based
2552 // on the internal camera). When injecting occurs later, this configuration can be used by
2553 // the injection camera.
2554 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2555 " injection camera has not been injected yet.", __FUNCTION__);
2556 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002557 }
2558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002559 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002560}
2561
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002562status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002563 ATRACE_CALL();
2564 status_t res;
2565
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002566 if (mFakeStreamId != NO_STREAM) {
2567 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002568 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002569 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002570 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002571 return INVALID_OPERATION;
2572 }
2573
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002574 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002575
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002576 sp<Camera3OutputStreamInterface> fakeStream =
2577 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002578
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002579 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002580 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002581 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002582 return res;
2583 }
2584
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002585 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002586 mNextStreamId++;
2587
2588 return OK;
2589}
2590
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002591status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002592 ATRACE_CALL();
2593 status_t res;
2594
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002595 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002596 if (mOutputStreams.size() == 1) return OK;
2597
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002598 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002599
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002600 // Ok, have a fake stream and there's at least one other output stream,
2601 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002602
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002603 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002604 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002605 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606 return INVALID_OPERATION;
2607 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002608 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002609
2610 // Free up the stream endpoint so that it can be used by some other stream
2611 res = deletedStream->disconnect();
2612 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002613 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002614 // fall through since we want to still list the stream as deleted.
2615 }
2616 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002617 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002618
2619 return res;
2620}
2621
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002622void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002623 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002624 Mutex::Autolock l(mLock);
2625 va_list args;
2626 va_start(args, fmt);
2627
2628 setErrorStateLockedV(fmt, args);
2629
2630 va_end(args);
2631}
2632
2633void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002634 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002635 Mutex::Autolock l(mLock);
2636 setErrorStateLockedV(fmt, args);
2637}
2638
2639void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2640 va_list args;
2641 va_start(args, fmt);
2642
2643 setErrorStateLockedV(fmt, args);
2644
2645 va_end(args);
2646}
2647
2648void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002649 // Print out all error messages to log
2650 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002651 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002652
2653 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002654 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002655
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002656 mErrorCause = errorCause;
2657
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002658 if (mRequestThread != nullptr) {
2659 mRequestThread->setPaused(true);
2660 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002661 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002662
2663 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002664 sp<NotificationListener> listener = mListener.promote();
2665 if (listener != NULL) {
2666 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002667 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002668 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002669 }
2670
2671 // Save stack trace. View by dumping it later.
2672 CameraTraces::saveTrace();
2673 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002674}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002675
2676/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002677 * In-flight request management
2678 */
2679
Jianing Weicb0652e2014-03-12 18:29:36 -07002680status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002681 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002682 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08002683 const std::set<std::set<String8>>& physicalCameraIds,
2684 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2685 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002686 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002687 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002688 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002689
2690 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002691 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002692 hasAppCallback, minExpectedDuration, maxExpectedDuration, physicalCameraIds,
2693 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2694 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002695 if (res < 0) return res;
2696
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002697 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002698 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2699 // avoid a deadlock during reprocess requests.
2700 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002701 if (mStatusTracker != nullptr) {
2702 mStatusTracker->markComponentActive(mInFlightStatusId);
2703 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002704 }
2705
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002706 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002707 return OK;
2708}
2709
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002710void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002711 // Indicate idle inFlightMap to the status tracker
2712 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002713 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002714 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2715 // avoid a deadlock during reprocess requests.
2716 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002717 if (mStatusTracker != nullptr) {
2718 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2719 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002720 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002721 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002722}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002723
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002724void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002725 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002726 // something has likely gone wrong. This might still be legit only if application send in
2727 // a long burst of long exposure requests.
2728 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2729 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2730 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2731 mInFlightMap.size(), mExpectedInflightDuration);
2732 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2733 kInFlightWarnLimitHighSpeed) {
2734 CLOGW("In-flight list too large for high speed configuration: %zu,"
2735 "total inflight duration %" PRIu64,
2736 mInFlightMap.size(), mExpectedInflightDuration);
2737 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002738 }
2739}
2740
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002741void Camera3Device::onInflightMapFlushedLocked() {
2742 mExpectedInflightDuration = 0;
2743}
2744
2745void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002746 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002747 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2748 mInFlightMap.removeItemsAt(idx, 1);
2749
2750 onInflightEntryRemovedLocked(duration);
2751}
2752
2753
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002754void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002755 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002756 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002757 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002758 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002759 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002760 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002761
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002762 FlushInflightReqStates states {
2763 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002764 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002765
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002766 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002767}
2768
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002769CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002770 ALOGV("%s", __FUNCTION__);
2771
Igor Murashkin1e479c02013-09-06 16:55:14 -07002772 CameraMetadata retVal;
2773
2774 if (mRequestThread != NULL) {
2775 retVal = mRequestThread->getLatestRequest();
2776 }
2777
Igor Murashkin1e479c02013-09-06 16:55:14 -07002778 return retVal;
2779}
2780
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002781void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002782 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002783 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002784 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2785 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002786
2787 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002788 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002789}
2790
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002791void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002792 std::vector<native_handle_t*> *handles, bool closeFd) {
2793 if (handles == nullptr) {
2794 return;
2795 }
2796 if (closeFd) {
2797 for (auto& handle : *handles) {
2798 native_handle_close(handle);
2799 }
2800 }
2801 for (auto& handle : *handles) {
2802 native_handle_delete(handle);
2803 }
2804 handles->clear();
2805 return;
2806}
2807
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002808/**
2809 * HalInterface inner class methods
2810 */
2811
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002812void Camera3Device::HalInterface::getInflightBufferKeys(
2813 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002814 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002815 return;
2816}
2817
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002818void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2819 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002820 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002821 return;
2822}
2823
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002824bool Camera3Device::HalInterface::verifyBufferIds(
2825 int32_t streamId, std::vector<uint64_t>& bufIds) {
2826 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002827}
2828
2829status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002830 int32_t frameNumber, int32_t streamId,
2831 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002832 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002833}
2834
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002835status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002836 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002837 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002838}
2839
2840// Find and pop a buffer_handle_t based on bufferId
2841status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002842 uint64_t bufferId,
2843 /*out*/ buffer_handle_t** buffer,
2844 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002845 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002846}
2847
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002848std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2849 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002850 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002851}
2852
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002853uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2854 const native_handle_t* handle) {
2855 return mBufferRecords.removeOneBufferCache(streamId, handle);
2856}
2857
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002858void Camera3Device::HalInterface::onBufferFreed(
2859 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002860 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2861 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2862 if (bufferId != BUFFER_ID_NO_BUFFER) {
2863 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002864 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002865}
2866
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002867void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002868 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2869 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2870 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002871 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2872 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002873}
2874
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002875/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002876 * RequestThread inner class methods
2877 */
2878
2879Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002880 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002881 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002882 bool useHalBufManager,
2883 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002884 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002886 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002887 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002888 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002889 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002890 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002891 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002892 mReconfigured(false),
2893 mDoPause(false),
2894 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002895 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002896 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002897 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002898 mCurrentAfTriggerId(0),
2899 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002900 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002901 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002902 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002903 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002904 mRepeatingLastFrameNumber(
2905 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002906 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002907 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002908 mRequestLatency(kRequestLatencyBinSize),
2909 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002910 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002911 mUseHalBufManager(useHalBufManager),
2912 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002913 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002914}
2915
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002916Camera3Device::RequestThread::~RequestThread() {}
2917
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002918void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002919 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002920 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002921 Mutex::Autolock l(mRequestLock);
2922 mListener = listener;
2923}
2924
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002925void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002926 const CameraMetadata& sessionParams,
2927 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002928 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002929 Mutex::Autolock l(mRequestLock);
2930 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002931 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002932 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002933 // Prepare video stream for high speed recording.
2934 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002935 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002936}
2937
Jianing Wei90e59c92014-03-12 18:29:36 -07002938status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002939 List<sp<CaptureRequest> > &requests,
2940 /*out*/
2941 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002942 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002943 Mutex::Autolock l(mRequestLock);
2944 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2945 ++it) {
2946 mRequestQueue.push_back(*it);
2947 }
2948
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002949 if (lastFrameNumber != NULL) {
2950 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2951 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2952 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2953 *lastFrameNumber);
2954 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002955
Jianing Wei90e59c92014-03-12 18:29:36 -07002956 unpauseForNewRequests();
2957
2958 return OK;
2959}
2960
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002961
2962status_t Camera3Device::RequestThread::queueTrigger(
2963 RequestTrigger trigger[],
2964 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002965 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002966 Mutex::Autolock l(mTriggerMutex);
2967 status_t ret;
2968
2969 for (size_t i = 0; i < count; ++i) {
2970 ret = queueTriggerLocked(trigger[i]);
2971
2972 if (ret != OK) {
2973 return ret;
2974 }
2975 }
2976
2977 return OK;
2978}
2979
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002980const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2981 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002982 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002983 if (d != nullptr) return d->mId;
2984 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002985}
2986
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002987status_t Camera3Device::RequestThread::queueTriggerLocked(
2988 RequestTrigger trigger) {
2989
2990 uint32_t tag = trigger.metadataTag;
2991 ssize_t index = mTriggerMap.indexOfKey(tag);
2992
2993 switch (trigger.getTagType()) {
2994 case TYPE_BYTE:
2995 // fall-through
2996 case TYPE_INT32:
2997 break;
2998 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002999 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3000 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003001 return INVALID_OPERATION;
3002 }
3003
3004 /**
3005 * Collect only the latest trigger, since we only have 1 field
3006 * in the request settings per trigger tag, and can't send more than 1
3007 * trigger per request.
3008 */
3009 if (index != NAME_NOT_FOUND) {
3010 mTriggerMap.editValueAt(index) = trigger;
3011 } else {
3012 mTriggerMap.add(tag, trigger);
3013 }
3014
3015 return OK;
3016}
3017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003018status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003019 const RequestList &requests,
3020 /*out*/
3021 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003022 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003023 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003024 if (lastFrameNumber != NULL) {
3025 *lastFrameNumber = mRepeatingLastFrameNumber;
3026 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003027 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003028 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003029 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3030 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003031
3032 unpauseForNewRequests();
3033
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003034 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003035 return OK;
3036}
3037
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003038bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003039 if (mRepeatingRequests.empty()) {
3040 return false;
3041 }
3042 int32_t requestId = requestIn->mResultExtras.requestId;
3043 const RequestList &repeatRequests = mRepeatingRequests;
3044 // All repeating requests are guaranteed to have same id so only check first quest
3045 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3046 return (firstRequest->mResultExtras.requestId == requestId);
3047}
3048
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003049status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003050 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003051 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003052 return clearRepeatingRequestsLocked(lastFrameNumber);
3053
3054}
3055
3056status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003057 std::vector<int32_t> streamIds;
3058 for (const auto& request : mRepeatingRequests) {
3059 for (const auto& stream : request->mOutputStreams) {
3060 streamIds.push_back(stream->getId());
3061 }
3062 }
3063
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003064 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003065 if (lastFrameNumber != NULL) {
3066 *lastFrameNumber = mRepeatingLastFrameNumber;
3067 }
Emilian Peev2295df72021-11-12 18:14:10 -08003068
3069 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3070
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003071 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003072 return OK;
3073}
3074
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003075status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003076 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003077 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003078 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003079 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003080
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003081 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003082
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003083 // Send errors for all requests pending in the request queue, including
3084 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003085 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003086 if (listener != NULL) {
3087 for (RequestList::iterator it = mRequestQueue.begin();
3088 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003089 // Abort the input buffers for reprocess requests.
3090 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003091 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003092 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003093 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003094 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003095 if (res != OK) {
3096 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3097 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3098 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003099 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003100 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3101 if (res != OK) {
3102 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3103 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3104 }
3105 }
3106 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003107 // Set the frame number this request would have had, if it
3108 // had been submitted; this frame number will not be reused.
3109 // The requestId and burstId fields were set when the request was
3110 // submitted originally (in convertMetadataListToRequestListLocked)
3111 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003112 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003113 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003114 }
3115 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003116 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003117
3118 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003119 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003120 if (lastFrameNumber != NULL) {
3121 *lastFrameNumber = mRepeatingLastFrameNumber;
3122 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003123 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003124 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003125 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003126 return OK;
3127}
3128
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003129status_t Camera3Device::RequestThread::flush() {
3130 ATRACE_CALL();
3131 Mutex::Autolock l(mFlushLock);
3132
Emilian Peev08dd2452017-04-06 16:55:14 +01003133 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003134}
3135
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003136void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003137 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003138 Mutex::Autolock l(mPauseLock);
3139 mDoPause = paused;
3140 mDoPauseSignal.signal();
3141}
3142
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003143status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3144 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003145 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003146 Mutex::Autolock l(mLatestRequestMutex);
3147 status_t res;
3148 while (mLatestRequestId != requestId) {
3149 nsecs_t startTime = systemTime();
3150
3151 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3152 if (res != OK) return res;
3153
3154 timeout -= (systemTime() - startTime);
3155 }
3156
3157 return OK;
3158}
3159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003160void Camera3Device::RequestThread::requestExit() {
3161 // Call parent to set up shutdown
3162 Thread::requestExit();
3163 // The exit from any possible waits
3164 mDoPauseSignal.signal();
3165 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003166
3167 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3168 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003169}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003170
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003171void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003172 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003173 bool surfaceAbandoned = false;
3174 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003175 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003176 {
3177 Mutex::Autolock l(mRequestLock);
3178 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3179 // repeating requests.
3180 for (const auto& request : mRepeatingRequests) {
3181 for (const auto& s : request->mOutputStreams) {
3182 if (s->isAbandoned()) {
3183 surfaceAbandoned = true;
3184 clearRepeatingRequestsLocked(&lastFrameNumber);
3185 break;
3186 }
3187 }
3188 if (surfaceAbandoned) {
3189 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003190 }
3191 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003192 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003193 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003194
3195 if (listener != NULL && surfaceAbandoned) {
3196 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003197 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003198}
3199
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003200bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003201 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003202 status_t res;
3203 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003204 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003205 uint32_t numRequestProcessed = 0;
3206 for (size_t i = 0; i < batchSize; i++) {
3207 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003208 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003209 }
3210
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003211 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3212
3213 bool triggerRemoveFailed = false;
3214 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3215 for (size_t i = 0; i < numRequestProcessed; i++) {
3216 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3217 nextRequest.submitted = true;
3218
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003219 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003220
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003221 if (!triggerRemoveFailed) {
3222 // Remove any previously queued triggers (after unlock)
3223 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3224 if (removeTriggerRes != OK) {
3225 triggerRemoveFailed = true;
3226 triggerFailedRequest = nextRequest;
3227 }
3228 }
3229 }
3230
3231 if (triggerRemoveFailed) {
3232 SET_ERR("RequestThread: Unable to remove triggers "
3233 "(capture request %d, HAL device: %s (%d)",
3234 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3235 cleanUpFailedRequests(/*sendRequestError*/ false);
3236 return false;
3237 }
3238
3239 if (res != OK) {
3240 // Should only get a failure here for malformed requests or device-level
3241 // errors, so consider all errors fatal. Bad metadata failures should
3242 // come through notify.
3243 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3244 mNextRequests[numRequestProcessed].halRequest.frame_number,
3245 strerror(-res), res);
3246 cleanUpFailedRequests(/*sendRequestError*/ false);
3247 return false;
3248 }
3249 return true;
3250}
3251
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003252std::pair<nsecs_t, nsecs_t> Camera3Device::RequestThread::calculateExpectedDurationRange(
3253 const camera_metadata_t *request) {
3254 std::pair<nsecs_t, nsecs_t> expectedRange(
3255 InFlightRequest::kDefaultMinExpectedDuration,
3256 InFlightRequest::kDefaultMaxExpectedDuration);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003257 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3258 find_camera_metadata_ro_entry(request,
3259 ANDROID_CONTROL_AE_MODE,
3260 &e);
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003261 if (e.count == 0) return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003262
3263 switch (e.data.u8[0]) {
3264 case ANDROID_CONTROL_AE_MODE_OFF:
3265 find_camera_metadata_ro_entry(request,
3266 ANDROID_SENSOR_EXPOSURE_TIME,
3267 &e);
3268 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003269 expectedRange.first = e.data.i64[0];
3270 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003271 }
3272 find_camera_metadata_ro_entry(request,
3273 ANDROID_SENSOR_FRAME_DURATION,
3274 &e);
3275 if (e.count > 0) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003276 expectedRange.first = std::max(e.data.i64[0], expectedRange.first);
3277 expectedRange.second = expectedRange.first;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003278 }
3279 break;
3280 default:
3281 find_camera_metadata_ro_entry(request,
3282 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3283 &e);
3284 if (e.count > 1) {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003285 expectedRange.first = 1e9 / e.data.i32[1];
3286 expectedRange.second = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003287 }
3288 break;
3289 }
3290
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003291 return expectedRange;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003292}
3293
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003294bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3295 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3296 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3297 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3298 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3299 return true;
3300 }
3301
3302 return false;
3303}
3304
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003305void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3306 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003307 camera_capture_request_t& halRequest = nextRequest.halRequest;
3308 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003309 Mutex::Autolock al(mLatestRequestMutex);
3310
Shuzhen Wang83bff122020-11-20 15:51:39 -08003311 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003312 mLatestRequest.acquire(cloned);
3313
3314 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003315 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3316 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3317 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003318 CameraMetadata(cloned));
3319 }
3320
3321 sp<Camera3Device> parent = mParent.promote();
3322 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003323 int32_t inputStreamId = -1;
3324 if (halRequest.input_buffer != nullptr) {
3325 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3326 }
3327
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003328 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003329 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003330 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3331 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003332 }
3333 }
3334
Shuzhen Wang83bff122020-11-20 15:51:39 -08003335 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003336 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003337 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003338 }
3339
Shuzhen Wang83bff122020-11-20 15:51:39 -08003340 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003341}
3342
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003343bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3344 ATRACE_CALL();
3345 bool updatesDetected = false;
3346
Emilian Peev4ec17882019-01-24 17:16:58 -08003347 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003348 for (auto tag : mSessionParamKeys) {
3349 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003350 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003351
3352 if (entry.count > 0) {
3353 bool isDifferent = false;
3354 if (lastEntry.count > 0) {
3355 // Have a last value, compare to see if changed
3356 if (lastEntry.type == entry.type &&
3357 lastEntry.count == entry.count) {
3358 // Same type and count, compare values
3359 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3360 size_t entryBytes = bytesPerValue * lastEntry.count;
3361 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3362 if (cmp != 0) {
3363 isDifferent = true;
3364 }
3365 } else {
3366 // Count or type has changed
3367 isDifferent = true;
3368 }
3369 } else {
3370 // No last entry, so always consider to be different
3371 isDifferent = true;
3372 }
3373
3374 if (isDifferent) {
3375 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003376 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3377 updatesDetected = true;
3378 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003379 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003380 }
3381 } else if (lastEntry.count > 0) {
3382 // Value has been removed
3383 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003384 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003385 updatesDetected = true;
3386 }
3387 }
3388
Emilian Peev4ec17882019-01-24 17:16:58 -08003389 bool reconfigureRequired;
3390 if (updatesDetected) {
3391 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3392 updatedParams);
3393 mLatestSessionParams = updatedParams;
3394 } else {
3395 reconfigureRequired = false;
3396 }
3397
3398 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003399}
3400
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003401bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003402 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003403 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003404 // Any function called from threadLoop() must not hold mInterfaceLock since
3405 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3406 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003407
3408 // Handle paused state.
3409 if (waitIfPaused()) {
3410 return true;
3411 }
3412
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003413 // Wait for the next batch of requests.
3414 waitForNextRequestBatch();
3415 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003416 return true;
3417 }
3418
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003419 // Get the latest request ID, if any
3420 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003421 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003422 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003423 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003424 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003425 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003426 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3427 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003428 }
3429
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003430 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3431 // or a single request from streaming or burst. In either case the first element
3432 // should contain the latest camera settings that we need to check for any session
3433 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003434 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003435 res = OK;
3436
3437 //Input stream buffers are already acquired at this point so an input stream
3438 //will not be able to move to idle state unless we force it.
3439 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3440 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3441 if (res != OK) {
3442 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3443 cleanUpFailedRequests(/*sendRequestError*/ false);
3444 return false;
3445 }
3446 }
3447
3448 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003449 sp<Camera3Device> parent = mParent.promote();
3450 if (parent != nullptr) {
3451 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003452 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003453 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003454
3455 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3456 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3457 if (res != OK) {
3458 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3459 cleanUpFailedRequests(/*sendRequestError*/ false);
3460 return false;
3461 }
3462 }
3463 }
3464 }
3465
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003466 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003467 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003468 if (res == TIMED_OUT) {
3469 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003470 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003471 // Check if any stream is abandoned.
3472 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003473 return true;
3474 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003475 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003476 return false;
3477 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003478
Zhijun Hecc27e112013-10-03 16:12:43 -07003479 // Inform waitUntilRequestProcessed thread of a new request ID
3480 {
3481 Mutex::Autolock al(mLatestRequestMutex);
3482
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003483 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003484 mLatestRequestSignal.signal();
3485 }
3486
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003487 // Submit a batch of requests to HAL.
3488 // Use flush lock only when submitting multilple requests in a batch.
3489 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3490 // which may take a long time to finish so synchronizing flush() and
3491 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3492 // For now, only synchronize for high speed recording and we should figure something out for
3493 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003494 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003495
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003496 if (useFlushLock) {
3497 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003498 }
3499
Zhijun Hef0645c12016-08-02 00:58:11 -07003500 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003501 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003502
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003503 sp<Camera3Device> parent = mParent.promote();
3504 if (parent != nullptr) {
3505 parent->mRequestBufferSM.onSubmittingRequest();
3506 }
3507
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003508 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003509 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003510 submitRequestSuccess = sendRequestsBatch();
3511
Shuzhen Wang686f6442017-06-20 16:16:04 -07003512 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3513 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003514
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003515 if (useFlushLock) {
3516 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003517 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003518
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003519 // Unset as current request
3520 {
3521 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003522 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003523 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003524 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003525
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003526 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003527}
3528
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003529status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3530 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3531 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3532 ANDROID_SCALER_CROP_REGION_SET};
3533 if (request == nullptr) {
3534 ALOGE("%s request metadata nullptr", __FUNCTION__);
3535 return BAD_VALUE;
3536 }
3537 status_t res = OK;
3538 for (const auto &key : kFwkOnlyRegionKeys) {
3539 if (request->exists(key)) {
3540 res = request->erase(key);
3541 if (res != OK) {
3542 return res;
3543 }
3544 }
3545 }
3546 return OK;
3547}
3548
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003549status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003550 ATRACE_CALL();
3551
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003552 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003553 for (size_t i = 0; i < mNextRequests.size(); i++) {
3554 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003555 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003556 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3557 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003558
3559 // Prepare a request to HAL
3560 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3561
3562 // Insert any queued triggers (before metadata is locked)
3563 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003564 if (res < 0) {
3565 SET_ERR("RequestThread: Unable to insert triggers "
3566 "(capture request %d, HAL device: %s (%d)",
3567 halRequest->frame_number, strerror(-res), res);
3568 return INVALID_OPERATION;
3569 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003570
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003571 int triggerCount = res;
3572 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3573 mPrevTriggers = triggerCount;
3574
Emilian Peeve23f1d92021-09-20 14:56:01 -07003575 // Do not override rotate&crop for stream configurations that include
3576 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3577 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3578 bool rotateAndCropChanged = mComposerOutput ? false :
3579 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003580 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003581
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003582 // If the request is the same as last, or we had triggers now or last time or
3583 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003584 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003585 (mPrevRequest != captureRequest || triggersMixedIn ||
3586 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003587 // Request settings are all the same within one batch, so only treat the first
3588 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003589 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003590 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003591 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003592 /**
3593 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003594 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003595 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003596 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003597 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003598 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003599 "(capture request %d, HAL device: %s (%d)",
3600 halRequest->frame_number, strerror(-res), res);
3601 return INVALID_OPERATION;
3602 }
3603
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003604 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003605 sp<Camera3Device> parent = mParent.promote();
3606 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003607 List<PhysicalCameraSettings>::iterator it;
3608 for (it = captureRequest->mSettingsList.begin();
3609 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003610 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3611 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003612 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3613 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3614 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3615 res);
3616 return INVALID_OPERATION;
3617 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003618 continue;
3619 }
3620
3621 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3622 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3623 updateCaptureRequest(&(it->metadata));
3624 if (res != OK) {
3625 SET_ERR("RequestThread: Unable to correct capture requests "
3626 "for scaler crop region and metering regions for request "
3627 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3628 res);
3629 return INVALID_OPERATION;
3630 }
3631 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003632 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3633 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3634 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3635 res);
3636 return INVALID_OPERATION;
3637 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003638 }
3639 }
3640
3641 // Correct metadata regions for distortion correction if enabled
3642 for (it = captureRequest->mSettingsList.begin();
3643 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003644 if (parent->mDistortionMappers.find(it->cameraId) ==
3645 parent->mDistortionMappers.end()) {
3646 continue;
3647 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003648
3649 if (!captureRequest->mDistortionCorrectionUpdated) {
3650 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3651 &(it->metadata));
3652 if (res != OK) {
3653 SET_ERR("RequestThread: Unable to correct capture requests "
3654 "for lens distortion for request %d: %s (%d)",
3655 halRequest->frame_number, strerror(-res), res);
3656 return INVALID_OPERATION;
3657 }
3658 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003659 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003660 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003661
3662 for (it = captureRequest->mSettingsList.begin();
3663 it != captureRequest->mSettingsList.end(); it++) {
3664 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3665 parent->mZoomRatioMappers.end()) {
3666 continue;
3667 }
3668
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003669 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003670 cameraIdsWithZoom.insert(it->cameraId);
3671 }
3672
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003673 if (!captureRequest->mZoomRatioUpdated) {
3674 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3675 &(it->metadata));
3676 if (res != OK) {
3677 SET_ERR("RequestThread: Unable to correct capture requests "
3678 "for zoom ratio for request %d: %s (%d)",
3679 halRequest->frame_number, strerror(-res), res);
3680 return INVALID_OPERATION;
3681 }
3682 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003683 }
3684 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003685 if (captureRequest->mRotateAndCropAuto &&
3686 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003687 for (it = captureRequest->mSettingsList.begin();
3688 it != captureRequest->mSettingsList.end(); it++) {
3689 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3690 if (mapper != parent->mRotateAndCropMappers.end()) {
3691 res = mapper->second.updateCaptureRequest(&(it->metadata));
3692 if (res != OK) {
3693 SET_ERR("RequestThread: Unable to correct capture requests "
3694 "for rotate-and-crop for request %d: %s (%d)",
3695 halRequest->frame_number, strerror(-res), res);
3696 return INVALID_OPERATION;
3697 }
3698 }
3699 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003700 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003701 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003702 }
3703 }
3704
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003705 /**
3706 * The request should be presorted so accesses in HAL
3707 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3708 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003709 captureRequest->mSettingsList.begin()->metadata.sort();
3710 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003711 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003712 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003713 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3714
3715 IF_ALOGV() {
3716 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3717 find_camera_metadata_ro_entry(
3718 halRequest->settings,
3719 ANDROID_CONTROL_AF_TRIGGER,
3720 &e
3721 );
3722 if (e.count > 0) {
3723 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3724 __FUNCTION__,
3725 halRequest->frame_number,
3726 e.data.u8[0]);
3727 }
3728 }
3729 } else {
3730 // leave request.settings NULL to indicate 'reuse latest given'
3731 ALOGVV("%s: Request settings are REUSED",
3732 __FUNCTION__);
3733 }
3734
Emilian Peevaebbe412018-01-15 13:53:24 +00003735 if (captureRequest->mSettingsList.size() > 1) {
3736 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3737 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003738 if (newRequest) {
3739 halRequest->physcam_settings =
3740 new const camera_metadata* [halRequest->num_physcam_settings];
3741 } else {
3742 halRequest->physcam_settings = nullptr;
3743 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003744 auto it = ++captureRequest->mSettingsList.begin();
3745 size_t i = 0;
3746 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3747 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003748 if (newRequest) {
3749 it->metadata.sort();
3750 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3751 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003752 }
3753 }
3754
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003755 uint32_t totalNumBuffers = 0;
3756
3757 // Fill in buffers
3758 if (captureRequest->mInputStream != NULL) {
3759 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003760
3761 halRequest->input_width = captureRequest->mInputBufferSize.width;
3762 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003763 totalNumBuffers += 1;
3764 } else {
3765 halRequest->input_buffer = NULL;
3766 }
3767
Emilian Peevf4816702020-04-03 15:44:51 -07003768 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003769 captureRequest->mOutputStreams.size());
3770 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003771 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003772
3773 sp<Camera3Device> parent = mParent.promote();
3774 if (parent == NULL) {
3775 // Should not happen, and nowhere to send errors to, so just log it
3776 CLOGE("RequestThread: Parent is gone");
3777 return INVALID_OPERATION;
3778 }
3779 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3780
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003781 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003782 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003783 sp<Camera3OutputStreamInterface> outputStream =
3784 captureRequest->mOutputStreams.editItemAt(j);
3785 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003786
3787 // Prepare video buffers for high speed recording on the first video request.
3788 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3789 // Only try to prepare video stream on the first video request.
3790 mPrepareVideoStream = false;
3791
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003792 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3793 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003794 while (res == NOT_ENOUGH_DATA) {
3795 res = outputStream->prepareNextBuffer();
3796 }
3797 if (res != OK) {
3798 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3799 __FUNCTION__, strerror(-res), res);
3800 outputStream->cancelPrepare();
3801 }
3802 }
3803
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003804 std::vector<size_t> uniqueSurfaceIds;
3805 res = outputStream->getUniqueSurfaceIds(
3806 captureRequest->mOutputSurfaces[streamId],
3807 &uniqueSurfaceIds);
3808 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3809 if (res != OK && res != INVALID_OPERATION) {
3810 ALOGE("%s: failed to query stream %d unique surface IDs",
3811 __FUNCTION__, streamId);
3812 return res;
3813 }
3814 if (res == OK) {
3815 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3816 }
3817
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003818 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003819 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003820 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003821 return TIMED_OUT;
3822 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003823 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003824 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003825 buffer.stream = outputStream->asHalStream();
3826 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003827 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003828 buffer.acquire_fence = -1;
3829 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003830 // Mark the output stream as unpreparable to block clients from calling
3831 // 'prepare' after this request reaches CameraHal and before the respective
3832 // buffers are requested.
3833 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003834 } else {
3835 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3836 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003837 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003838 if (res != OK) {
3839 // Can't get output buffer from gralloc queue - this could be due to
3840 // abandoned queue or other consumer misbehavior, so not a fatal
3841 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003842 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003843 " %s (%d)", strerror(-res), res);
3844
3845 return TIMED_OUT;
3846 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003847 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003848
3849 {
3850 sp<Camera3Device> parent = mParent.promote();
3851 if (parent != nullptr) {
3852 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3853 for (const auto& settings : captureRequest->mSettingsList) {
3854 if ((streamCameraId.isEmpty() &&
3855 parent->getId() == settings.cameraId.c_str()) ||
3856 streamCameraId == settings.cameraId.c_str()) {
3857 outputStream->fireBufferRequestForFrameNumber(
3858 captureRequest->mResultExtras.frameNumber,
3859 settings.metadata);
3860 }
3861 }
3862 }
3863 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003864
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003865 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003866 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3867 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3868 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3869 } else if (!physicalCameraId.isEmpty()) {
3870 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003871 }
3872 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003873 }
3874 totalNumBuffers += halRequest->num_output_buffers;
3875
3876 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003877 // If this request list is for constrained high speed recording (not
3878 // preview), and the current request is not the last one in the batch,
3879 // do not send callback to the app.
3880 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003881 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003882 hasCallback = false;
3883 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003884 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003885 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003886 const camera_metadata_t* settings = halRequest->settings;
3887 bool shouldUnlockSettings = false;
3888 if (settings == nullptr) {
3889 shouldUnlockSettings = true;
3890 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3891 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003892 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3893 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003894 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003895 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3896 isStillCapture = true;
3897 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3898 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003899
Emilian Peevaf8416e2021-02-04 17:52:43 -08003900 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003901 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003902 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3903 isZslCapture = true;
3904 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003905 }
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003906 auto expectedDurationRange = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003907 res = parent->registerInFlight(halRequest->frame_number,
3908 totalNumBuffers, captureRequest->mResultExtras,
3909 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003910 hasCallback,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003911 /*min*/expectedDurationRange.first,
3912 /*max*/expectedDurationRange.second,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003913 requestedPhysicalCameras, isStillCapture, isZslCapture,
3914 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003915 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003916 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003917 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3918 ", burstId = %" PRId32 ".",
3919 __FUNCTION__,
3920 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3921 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003922
3923 if (shouldUnlockSettings) {
3924 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3925 }
3926
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003927 if (res != OK) {
3928 SET_ERR("RequestThread: Unable to register new in-flight request:"
3929 " %s (%d)", strerror(-res), res);
3930 return INVALID_OPERATION;
3931 }
3932 }
3933
3934 return OK;
3935}
3936
Igor Murashkin1e479c02013-09-06 16:55:14 -07003937CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003938 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003939 Mutex::Autolock al(mLatestRequestMutex);
3940
3941 ALOGV("RequestThread::%s", __FUNCTION__);
3942
3943 return mLatestRequest;
3944}
3945
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003946bool Camera3Device::RequestThread::isStreamPending(
3947 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003948 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003949 Mutex::Autolock l(mRequestLock);
3950
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003951 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003952 if (!nextRequest.submitted) {
3953 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3954 if (stream == s) return true;
3955 }
3956 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003957 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003958 }
3959
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003960 for (const auto& request : mRequestQueue) {
3961 for (const auto& s : request->mOutputStreams) {
3962 if (stream == s) return true;
3963 }
3964 if (stream == request->mInputStream) return true;
3965 }
3966
3967 for (const auto& request : mRepeatingRequests) {
3968 for (const auto& s : request->mOutputStreams) {
3969 if (stream == s) return true;
3970 }
3971 if (stream == request->mInputStream) return true;
3972 }
3973
3974 return false;
3975}
Jianing Weicb0652e2014-03-12 18:29:36 -07003976
Emilian Peev40ead602017-09-26 15:46:36 +01003977bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3978 ATRACE_CALL();
3979 Mutex::Autolock l(mRequestLock);
3980
3981 for (const auto& nextRequest : mNextRequests) {
3982 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3983 if (s.first == streamId) {
3984 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3985 if (it != s.second.end()) {
3986 return true;
3987 }
3988 }
3989 }
3990 }
3991
3992 for (const auto& request : mRequestQueue) {
3993 for (const auto& s : request->mOutputSurfaces) {
3994 if (s.first == streamId) {
3995 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3996 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003997 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01003998 }
3999 }
4000 }
4001 }
4002
4003 for (const auto& request : mRepeatingRequests) {
4004 for (const auto& s : request->mOutputSurfaces) {
4005 if (s.first == streamId) {
4006 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4007 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004008 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004009 }
4010 }
4011 }
4012 }
4013
4014 return false;
4015}
4016
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004017void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4018 if (!mUseHalBufManager) {
4019 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4020 return;
4021 }
4022
4023 Mutex::Autolock pl(mPauseLock);
4024 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004025 mInterface->signalPipelineDrain(streamIds);
4026 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004027 }
4028 // If request thread is still busy, wait until paused then notify HAL
4029 mNotifyPipelineDrain = true;
4030 mStreamIdsToBeDrained = streamIds;
4031}
4032
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004033void Camera3Device::RequestThread::resetPipelineDrain() {
4034 Mutex::Autolock pl(mPauseLock);
4035 mNotifyPipelineDrain = false;
4036 mStreamIdsToBeDrained.clear();
4037}
4038
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004039void Camera3Device::RequestThread::clearPreviousRequest() {
4040 Mutex::Autolock l(mRequestLock);
4041 mPrevRequest.clear();
4042}
4043
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004044status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4045 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4046 ATRACE_CALL();
4047 Mutex::Autolock l(mTriggerMutex);
4048 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4049 return BAD_VALUE;
4050 }
4051 mRotateAndCropOverride = rotateAndCropValue;
4052 return OK;
4053}
4054
Emilian Peeve23f1d92021-09-20 14:56:01 -07004055status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4056 ATRACE_CALL();
4057 Mutex::Autolock l(mTriggerMutex);
4058 mComposerOutput = composerSurfacePresent;
4059 return OK;
4060}
4061
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004062status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004063 ATRACE_CALL();
4064 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004065 if (muteMode != mCameraMute) {
4066 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004067 mCameraMuteChanged = true;
4068 }
4069 return OK;
4070}
4071
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004072nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004073 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004074 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004075 return mExpectedInflightDuration > kMinInflightDuration ?
4076 mExpectedInflightDuration : kMinInflightDuration;
4077}
4078
Emilian Peevaebbe412018-01-15 13:53:24 +00004079void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004080 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004081 if ((request == nullptr) || (halRequest == nullptr)) {
4082 ALOGE("%s: Invalid request!", __FUNCTION__);
4083 return;
4084 }
4085
4086 if (halRequest->num_physcam_settings > 0) {
4087 if (halRequest->physcam_id != nullptr) {
4088 delete [] halRequest->physcam_id;
4089 halRequest->physcam_id = nullptr;
4090 }
4091 if (halRequest->physcam_settings != nullptr) {
4092 auto it = ++(request->mSettingsList.begin());
4093 size_t i = 0;
4094 for (; it != request->mSettingsList.end(); it++, i++) {
4095 it->metadata.unlock(halRequest->physcam_settings[i]);
4096 }
4097 delete [] halRequest->physcam_settings;
4098 halRequest->physcam_settings = nullptr;
4099 }
4100 }
4101}
4102
Ravneetaeb20dc2022-03-30 05:33:03 +00004103status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4104 Mutex::Autolock il(mInterfaceLock);
4105 Mutex::Autolock l(mLock);
4106
4107 if (mCameraServiceWatchdog != NULL) {
4108 mCameraServiceWatchdog->setEnabled(enabled);
4109 }
4110
4111 return OK;
4112}
4113
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004114void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4115 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004116 return;
4117 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004118
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004119 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004120 // Skip the ones that have been submitted successfully.
4121 if (nextRequest.submitted) {
4122 continue;
4123 }
4124
4125 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004126 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4127 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004128
4129 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004130 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004131 }
4132
Emilian Peevaebbe412018-01-15 13:53:24 +00004133 cleanupPhysicalSettings(captureRequest, halRequest);
4134
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004135 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004136 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004137 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4138 }
4139
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004140 // No output buffer can be returned when using HAL buffer manager
4141 if (!mUseHalBufManager) {
4142 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4143 //Buffers that failed processing could still have
4144 //valid acquire fence.
4145 int acquireFence = (*outputBuffers)[i].acquire_fence;
4146 if (0 <= acquireFence) {
4147 close(acquireFence);
4148 outputBuffers->editItemAt(i).acquire_fence = -1;
4149 }
Emilian Peevf4816702020-04-03 15:44:51 -07004150 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004151 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4152 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004153 /*timestampIncreasing*/true, std::vector<size_t> (),
4154 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004155 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004156 }
4157
4158 if (sendRequestError) {
4159 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004160 sp<NotificationListener> listener = mListener.promote();
4161 if (listener != NULL) {
4162 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004163 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004164 captureRequest->mResultExtras);
4165 }
4166 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004167
4168 // Remove yet-to-be submitted inflight request from inflightMap
4169 {
4170 sp<Camera3Device> parent = mParent.promote();
4171 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004172 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004173 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4174 if (idx >= 0) {
4175 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4176 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4177 parent->removeInFlightMapEntryLocked(idx);
4178 }
4179 }
4180 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004181 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004182
4183 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004184 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004185}
4186
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004187void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004188 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004189 // Optimized a bit for the simple steady-state case (single repeating
4190 // request), to avoid putting that request in the queue temporarily.
4191 Mutex::Autolock l(mRequestLock);
4192
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004193 assert(mNextRequests.empty());
4194
4195 NextRequest nextRequest;
4196 nextRequest.captureRequest = waitForNextRequestLocked();
4197 if (nextRequest.captureRequest == nullptr) {
4198 return;
4199 }
4200
Emilian Peevf4816702020-04-03 15:44:51 -07004201 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004202 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004203 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004204
4205 // Wait for additional requests
4206 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4207
4208 for (size_t i = 1; i < batchSize; i++) {
4209 NextRequest additionalRequest;
4210 additionalRequest.captureRequest = waitForNextRequestLocked();
4211 if (additionalRequest.captureRequest == nullptr) {
4212 break;
4213 }
4214
Emilian Peevf4816702020-04-03 15:44:51 -07004215 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004216 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004217 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004218 }
4219
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004220 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004221 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004222 mNextRequests.size(), batchSize);
4223 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004224 }
4225
4226 return;
4227}
4228
4229sp<Camera3Device::CaptureRequest>
4230 Camera3Device::RequestThread::waitForNextRequestLocked() {
4231 status_t res;
4232 sp<CaptureRequest> nextRequest;
4233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004234 while (mRequestQueue.empty()) {
4235 if (!mRepeatingRequests.empty()) {
4236 // Always atomically enqueue all requests in a repeating request
4237 // list. Guarantees a complete in-sequence set of captures to
4238 // application.
4239 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004240 if (mFirstRepeating) {
4241 mFirstRepeating = false;
4242 } else {
4243 for (auto& request : requests) {
4244 // For repeating requests, override timestamp request using
4245 // the time a request is inserted into the request queue,
4246 // because the original repeating request will have an old
4247 // fixed timestamp.
4248 request->mRequestTimeNs = systemTime();
4249 }
4250 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004251 RequestList::const_iterator firstRequest =
4252 requests.begin();
4253 nextRequest = *firstRequest;
4254 mRequestQueue.insert(mRequestQueue.end(),
4255 ++firstRequest,
4256 requests.end());
4257 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004258
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004259 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004260
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004261 break;
4262 }
4263
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004264 if (!mRequestClearing) {
4265 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4266 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004267
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004268 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4269 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004270 Mutex::Autolock pl(mPauseLock);
4271 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004272 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004273 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004274 if (mNotifyPipelineDrain) {
4275 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4276 mNotifyPipelineDrain = false;
4277 mStreamIdsToBeDrained.clear();
4278 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004279 // Let the tracker know
4280 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4281 if (statusTracker != 0) {
4282 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4283 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004284 sp<Camera3Device> parent = mParent.promote();
4285 if (parent != nullptr) {
4286 parent->mRequestBufferSM.onRequestThreadPaused();
4287 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004288 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004289 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004290 // Stop waiting for now and let thread management happen
4291 return NULL;
4292 }
4293 }
4294
4295 if (nextRequest == NULL) {
4296 // Don't have a repeating request already in hand, so queue
4297 // must have an entry now.
4298 RequestList::iterator firstRequest =
4299 mRequestQueue.begin();
4300 nextRequest = *firstRequest;
4301 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004302 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4303 sp<NotificationListener> listener = mListener.promote();
4304 if (listener != NULL) {
4305 listener->notifyRequestQueueEmpty();
4306 }
4307 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004308 }
4309
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004310 // In case we've been unpaused by setPaused clearing mDoPause, need to
4311 // update internal pause state (capture/setRepeatingRequest unpause
4312 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004313 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004314 if (mPaused) {
4315 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4316 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4317 if (statusTracker != 0) {
4318 statusTracker->markComponentActive(mStatusId);
4319 }
4320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004321 mPaused = false;
4322
4323 // Check if we've reconfigured since last time, and reset the preview
4324 // request if so. Can't use 'NULL request == repeat' across configure calls.
4325 if (mReconfigured) {
4326 mPrevRequest.clear();
4327 mReconfigured = false;
4328 }
4329
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004330 if (nextRequest != NULL) {
4331 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004332 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4333 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004334
4335 // Since RequestThread::clear() removes buffers from the input stream,
4336 // get the right buffer here before unlocking mRequestLock
4337 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004338 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4339 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004340 if (res != OK) {
4341 // Can't get input buffer from gralloc queue - this could be due to
4342 // disconnected queue or other producer misbehavior, so not a fatal
4343 // error
4344 ALOGE("%s: Can't get input buffer, skipping request:"
4345 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004346
4347 sp<NotificationListener> listener = mListener.promote();
4348 if (listener != NULL) {
4349 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004350 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004351 nextRequest->mResultExtras);
4352 }
4353 return NULL;
4354 }
4355 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004356 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004357
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004358 return nextRequest;
4359}
4360
4361bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004362 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004363 status_t res;
4364 Mutex::Autolock l(mPauseLock);
4365 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004366 if (mPaused == false) {
4367 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004368 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004369 if (mNotifyPipelineDrain) {
4370 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4371 mNotifyPipelineDrain = false;
4372 mStreamIdsToBeDrained.clear();
4373 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004374 // Let the tracker know
4375 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4376 if (statusTracker != 0) {
4377 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4378 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004379 sp<Camera3Device> parent = mParent.promote();
4380 if (parent != nullptr) {
4381 parent->mRequestBufferSM.onRequestThreadPaused();
4382 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004383 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004385 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004386 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004387 return true;
4388 }
4389 }
4390 // We don't set mPaused to false here, because waitForNextRequest needs
4391 // to further manage the paused state in case of starvation.
4392 return false;
4393}
4394
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004395void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004396 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004397 // With work to do, mark thread as unpaused.
4398 // If paused by request (setPaused), don't resume, to avoid
4399 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004400 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004401 Mutex::Autolock p(mPauseLock);
4402 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004403 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4404 if (mPaused) {
4405 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4406 if (statusTracker != 0) {
4407 statusTracker->markComponentActive(mStatusId);
4408 }
4409 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004410 mPaused = false;
4411 }
4412}
4413
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004414void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4415 sp<Camera3Device> parent = mParent.promote();
4416 if (parent != NULL) {
4417 va_list args;
4418 va_start(args, fmt);
4419
4420 parent->setErrorStateV(fmt, args);
4421
4422 va_end(args);
4423 }
4424}
4425
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004426status_t Camera3Device::RequestThread::insertTriggers(
4427 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004428 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004429 Mutex::Autolock al(mTriggerMutex);
4430
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004431 sp<Camera3Device> parent = mParent.promote();
4432 if (parent == NULL) {
4433 CLOGE("RequestThread: Parent is gone");
4434 return DEAD_OBJECT;
4435 }
4436
Emilian Peevaebbe412018-01-15 13:53:24 +00004437 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004438 size_t count = mTriggerMap.size();
4439
4440 for (size_t i = 0; i < count; ++i) {
4441 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004442 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004443
4444 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4445 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4446 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004447 if (isAeTrigger) {
4448 request->mResultExtras.precaptureTriggerId = triggerId;
4449 mCurrentPreCaptureTriggerId = triggerId;
4450 } else {
4451 request->mResultExtras.afTriggerId = triggerId;
4452 mCurrentAfTriggerId = triggerId;
4453 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004454 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004455 }
4456
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004457 camera_metadata_entry entry = metadata.find(tag);
4458
4459 if (entry.count > 0) {
4460 /**
4461 * Already has an entry for this trigger in the request.
4462 * Rewrite it with our requested trigger value.
4463 */
4464 RequestTrigger oldTrigger = trigger;
4465
4466 oldTrigger.entryValue = entry.data.u8[0];
4467
4468 mTriggerReplacedMap.add(tag, oldTrigger);
4469 } else {
4470 /**
4471 * More typical, no trigger entry, so we just add it
4472 */
4473 mTriggerRemovedMap.add(tag, trigger);
4474 }
4475
4476 status_t res;
4477
4478 switch (trigger.getTagType()) {
4479 case TYPE_BYTE: {
4480 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4481 res = metadata.update(tag,
4482 &entryValue,
4483 /*count*/1);
4484 break;
4485 }
4486 case TYPE_INT32:
4487 res = metadata.update(tag,
4488 &trigger.entryValue,
4489 /*count*/1);
4490 break;
4491 default:
4492 ALOGE("%s: Type not supported: 0x%x",
4493 __FUNCTION__,
4494 trigger.getTagType());
4495 return INVALID_OPERATION;
4496 }
4497
4498 if (res != OK) {
4499 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4500 ", value %d", __FUNCTION__, trigger.getTagName(),
4501 trigger.entryValue);
4502 return res;
4503 }
4504
4505 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4506 trigger.getTagName(),
4507 trigger.entryValue);
4508 }
4509
4510 mTriggerMap.clear();
4511
4512 return count;
4513}
4514
4515status_t Camera3Device::RequestThread::removeTriggers(
4516 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004517 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004518 Mutex::Autolock al(mTriggerMutex);
4519
Emilian Peevaebbe412018-01-15 13:53:24 +00004520 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004521
4522 /**
4523 * Replace all old entries with their old values.
4524 */
4525 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4526 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4527
4528 status_t res;
4529
4530 uint32_t tag = trigger.metadataTag;
4531 switch (trigger.getTagType()) {
4532 case TYPE_BYTE: {
4533 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4534 res = metadata.update(tag,
4535 &entryValue,
4536 /*count*/1);
4537 break;
4538 }
4539 case TYPE_INT32:
4540 res = metadata.update(tag,
4541 &trigger.entryValue,
4542 /*count*/1);
4543 break;
4544 default:
4545 ALOGE("%s: Type not supported: 0x%x",
4546 __FUNCTION__,
4547 trigger.getTagType());
4548 return INVALID_OPERATION;
4549 }
4550
4551 if (res != OK) {
4552 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4553 ", trigger value %d", __FUNCTION__,
4554 trigger.getTagName(), trigger.entryValue);
4555 return res;
4556 }
4557 }
4558 mTriggerReplacedMap.clear();
4559
4560 /**
4561 * Remove all new entries.
4562 */
4563 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4564 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4565 status_t res = metadata.erase(trigger.metadataTag);
4566
4567 if (res != OK) {
4568 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4569 ", trigger value %d", __FUNCTION__,
4570 trigger.getTagName(), trigger.entryValue);
4571 return res;
4572 }
4573 }
4574 mTriggerRemovedMap.clear();
4575
4576 return OK;
4577}
4578
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004579status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004580 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004581 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004582 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004583 status_t res;
4584
Emilian Peevaebbe412018-01-15 13:53:24 +00004585 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004586
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004587 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004588 // exists
4589 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4590 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4591 if (afTrigger.count > 0 &&
4592 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4593 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004594 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004595 if (res != OK) return res;
4596 }
4597
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004598 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004599 // if none already exists
4600 camera_metadata_entry pcTrigger =
4601 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4602 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4603 if (pcTrigger.count > 0 &&
4604 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4605 pcId.count == 0) {
4606 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004607 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004608 if (res != OK) return res;
4609 }
4610
4611 return OK;
4612}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004613
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004614bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4615 const sp<CaptureRequest> &request) {
4616 ATRACE_CALL();
4617
4618 if (request->mRotateAndCropAuto) {
4619 Mutex::Autolock l(mTriggerMutex);
4620 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4621
4622 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4623 if (rotateAndCropEntry.count > 0) {
4624 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4625 return false;
4626 } else {
4627 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4628 return true;
4629 }
4630 } else {
4631 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4632 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4633 &rotateAndCrop_u8, 1);
4634 return true;
4635 }
4636 }
4637 return false;
4638}
4639
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004640bool Camera3Device::RequestThread::overrideTestPattern(
4641 const sp<CaptureRequest> &request) {
4642 ATRACE_CALL();
4643
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004644 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004645
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004646 Mutex::Autolock l(mTriggerMutex);
4647
4648 bool changed = false;
4649
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004650 // For a multi-camera, the physical cameras support the same set of
4651 // test pattern modes as the logical camera.
4652 for (auto& settings : request->mSettingsList) {
4653 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004654
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004655 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4656 int32_t testPatternData[4] = {
4657 settings.mOriginalTestPatternData[0],
4658 settings.mOriginalTestPatternData[1],
4659 settings.mOriginalTestPatternData[2],
4660 settings.mOriginalTestPatternData[3]
4661 };
4662 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4663 testPatternMode = mCameraMute;
4664 testPatternData[0] = 0;
4665 testPatternData[1] = 0;
4666 testPatternData[2] = 0;
4667 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004668 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004669
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004670 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4671 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4672 if (testPatternEntry.count > 0) {
4673 if (testPatternEntry.data.i32[0] != testPatternMode) {
4674 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004675 changed = true;
4676 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004677 } else if (supportTestPatternModeKey) {
4678 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4679 &testPatternMode, 1);
4680 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004681 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004682
4683 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4684 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4685 if (testPatternColor.count >= 4) {
4686 for (size_t i = 0; i < 4; i++) {
4687 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4688 testPatternColor.data.i32[i] = testPatternData[i];
4689 changed = true;
4690 }
4691 }
4692 } else if (supportTestPatternDataKey) {
4693 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4694 testPatternData, 4);
4695 changed = true;
4696 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004697 }
4698
4699 return changed;
4700}
4701
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004702status_t Camera3Device::RequestThread::setHalInterface(
4703 sp<HalInterface> newHalInterface) {
4704 if (newHalInterface.get() == nullptr) {
4705 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4706 return DEAD_OBJECT;
4707 }
4708
4709 mInterface = newHalInterface;
4710
4711 return OK;
4712}
4713
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004714/**
4715 * PreparerThread inner class methods
4716 */
4717
4718Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004719 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004720 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004721}
4722
4723Camera3Device::PreparerThread::~PreparerThread() {
4724 Thread::requestExitAndWait();
4725 if (mCurrentStream != nullptr) {
4726 mCurrentStream->cancelPrepare();
4727 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4728 mCurrentStream.clear();
4729 }
4730 clear();
4731}
4732
Ruben Brunkc78ac262015-08-13 17:58:46 -07004733status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004734 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004735 status_t res;
4736
4737 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004738 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004739
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004740 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004741 if (res == OK) {
4742 // No preparation needed, fire listener right off
4743 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004744 if (listener != NULL) {
4745 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004746 }
4747 return OK;
4748 } else if (res != NOT_ENOUGH_DATA) {
4749 return res;
4750 }
4751
4752 // Need to prepare, start up thread if necessary
4753 if (!mActive) {
4754 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4755 // isn't running
4756 Thread::requestExitAndWait();
4757 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4758 if (res != OK) {
4759 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004760 if (listener != NULL) {
4761 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004762 }
4763 return res;
4764 }
4765 mCancelNow = false;
4766 mActive = true;
4767 ALOGV("%s: Preparer stream started", __FUNCTION__);
4768 }
4769
4770 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004771 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004772 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4773
4774 return OK;
4775}
4776
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004777void Camera3Device::PreparerThread::pause() {
4778 ATRACE_CALL();
4779
4780 Mutex::Autolock l(mLock);
4781
4782 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4783 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4784 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4785 int currentMaxCount = mCurrentMaxCount;
4786 mPendingStreams.clear();
4787 mCancelNow = true;
4788 while (mActive) {
4789 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4790 if (res == TIMED_OUT) {
4791 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4792 return;
4793 } else if (res != OK) {
4794 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4795 return;
4796 }
4797 }
4798
4799 //Check whether the prepare thread was able to complete the current
4800 //stream. In case work is still pending emplace it along with the rest
4801 //of the streams in the pending list.
4802 if (currentStream != nullptr) {
4803 if (!mCurrentPrepareComplete) {
4804 pendingStreams.emplace(currentMaxCount, currentStream);
4805 }
4806 }
4807
4808 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4809 for (const auto& it : mPendingStreams) {
4810 it.second->cancelPrepare();
4811 }
4812}
4813
4814status_t Camera3Device::PreparerThread::resume() {
4815 ATRACE_CALL();
4816 status_t res;
4817
4818 Mutex::Autolock l(mLock);
4819 sp<NotificationListener> listener = mListener.promote();
4820
4821 if (mActive) {
4822 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4823 return NO_INIT;
4824 }
4825
4826 auto it = mPendingStreams.begin();
4827 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004828 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004829 if (res == OK) {
4830 if (listener != NULL) {
4831 listener->notifyPrepared(it->second->getId());
4832 }
4833 it = mPendingStreams.erase(it);
4834 } else if (res != NOT_ENOUGH_DATA) {
4835 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4836 res, strerror(-res));
4837 it = mPendingStreams.erase(it);
4838 } else {
4839 it++;
4840 }
4841 }
4842
4843 if (mPendingStreams.empty()) {
4844 return OK;
4845 }
4846
4847 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4848 if (res != OK) {
4849 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4850 __FUNCTION__, res, strerror(-res));
4851 return res;
4852 }
4853 mCancelNow = false;
4854 mActive = true;
4855 ALOGV("%s: Preparer stream started", __FUNCTION__);
4856
4857 return OK;
4858}
4859
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004860status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004861 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004862 Mutex::Autolock l(mLock);
4863
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004864 for (const auto& it : mPendingStreams) {
4865 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004866 }
4867 mPendingStreams.clear();
4868 mCancelNow = true;
4869
4870 return OK;
4871}
4872
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004873void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004874 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004875 Mutex::Autolock l(mLock);
4876 mListener = listener;
4877}
4878
4879bool Camera3Device::PreparerThread::threadLoop() {
4880 status_t res;
4881 {
4882 Mutex::Autolock l(mLock);
4883 if (mCurrentStream == nullptr) {
4884 // End thread if done with work
4885 if (mPendingStreams.empty()) {
4886 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4887 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4888 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4889 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004890 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004891 return false;
4892 }
4893
4894 // Get next stream to prepare
4895 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004896 mCurrentStream = it->second;
4897 mCurrentMaxCount = it->first;
4898 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004899 mPendingStreams.erase(it);
4900 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4901 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4902 } else if (mCancelNow) {
4903 mCurrentStream->cancelPrepare();
4904 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4905 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4906 mCurrentStream.clear();
4907 mCancelNow = false;
4908 return true;
4909 }
4910 }
4911
4912 res = mCurrentStream->prepareNextBuffer();
4913 if (res == NOT_ENOUGH_DATA) return true;
4914 if (res != OK) {
4915 // Something bad happened; try to recover by cancelling prepare and
4916 // signalling listener anyway
4917 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4918 mCurrentStream->getId(), res, strerror(-res));
4919 mCurrentStream->cancelPrepare();
4920 }
4921
4922 // This stream has finished, notify listener
4923 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004924 sp<NotificationListener> listener = mListener.promote();
4925 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004926 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4927 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004928 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004929 }
4930
4931 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4932 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004933 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004934
4935 return true;
4936}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004937
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004938status_t Camera3Device::RequestBufferStateMachine::initialize(
4939 sp<camera3::StatusTracker> statusTracker) {
4940 if (statusTracker == nullptr) {
4941 ALOGE("%s: statusTracker is null", __FUNCTION__);
4942 return BAD_VALUE;
4943 }
4944
4945 std::lock_guard<std::mutex> lock(mLock);
4946 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004947 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004948 return OK;
4949}
4950
4951bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4952 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004953 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004954 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004955 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004956 return true;
4957 }
4958 return false;
4959}
4960
4961void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4962 std::lock_guard<std::mutex> lock(mLock);
4963 if (!mRequestBufferOngoing) {
4964 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4965 return;
4966 }
4967 mRequestBufferOngoing = false;
4968 if (mStatus == RB_STATUS_PENDING_STOP) {
4969 checkSwitchToStopLocked();
4970 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004971 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004972}
4973
4974void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4975 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004976 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004977 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004978 return;
4979}
4980
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004981void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004982 std::lock_guard<std::mutex> lock(mLock);
4983 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004984 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4985 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004986 mInflightMapEmpty = false;
4987 if (mStatus == RB_STATUS_STOPPED) {
4988 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004989 }
4990 return;
4991}
4992
4993void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4994 std::lock_guard<std::mutex> lock(mLock);
4995 mRequestThreadPaused = true;
4996 if (mStatus == RB_STATUS_PENDING_STOP) {
4997 checkSwitchToStopLocked();
4998 }
4999 return;
5000}
5001
5002void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5003 std::lock_guard<std::mutex> lock(mLock);
5004 mInflightMapEmpty = true;
5005 if (mStatus == RB_STATUS_PENDING_STOP) {
5006 checkSwitchToStopLocked();
5007 }
5008 return;
5009}
5010
5011void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5012 std::lock_guard<std::mutex> lock(mLock);
5013 if (!checkSwitchToStopLocked()) {
5014 mStatus = RB_STATUS_PENDING_STOP;
5015 }
5016 return;
5017}
5018
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005019bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5020 std::lock_guard<std::mutex> lock(mLock);
5021 if (mRequestBufferOngoing) {
5022 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5023 __FUNCTION__);
5024 return false;
5025 }
5026 mSwitchedToOffline = true;
5027 mInflightMapEmpty = true;
5028 mRequestThreadPaused = true;
5029 mStatus = RB_STATUS_STOPPED;
5030 return true;
5031}
5032
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005033void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5034 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5035 if (statusTracker != nullptr) {
5036 if (active) {
5037 statusTracker->markComponentActive(mRequestBufferStatusId);
5038 } else {
5039 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5040 }
5041 }
5042}
5043
5044bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5045 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5046 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005047 return true;
5048 }
5049 return false;
5050}
5051
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005052bool Camera3Device::startRequestBuffer() {
5053 return mRequestBufferSM.startRequestBuffer();
5054}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005055
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005056void Camera3Device::endRequestBuffer() {
5057 mRequestBufferSM.endRequestBuffer();
5058}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005059
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005060nsecs_t Camera3Device::getWaitDuration() {
5061 return kBaseGetBufferWait + getExpectedInFlightDuration();
5062}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005063
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005064void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5065 mInterface->getInflightBufferKeys(out);
5066}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005068void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5069 mInterface->getInflightRequestBufferKeys(out);
5070}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005071
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005072std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5073 std::vector<sp<Camera3StreamInterface>> ret;
5074 bool hasInputStream = mInputStream != nullptr;
5075 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5076 if (hasInputStream) {
5077 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005078 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005079 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5080 ret.push_back(mOutputStreams[i]);
5081 }
5082 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5083 ret.push_back(mDeletedStreams[i]);
5084 }
5085 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005086}
5087
Emilian Peevcc0b7952020-01-07 13:54:47 -08005088void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5089 ATRACE_CALL();
5090
5091 if (offlineStreamIds == nullptr) {
5092 return;
5093 }
5094
5095 Mutex::Autolock il(mInterfaceLock);
5096
5097 auto streamIds = mOutputStreams.getStreamIds();
5098 bool hasInputStream = mInputStream != nullptr;
5099 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5100 offlineStreamIds->push_back(mInputStream->getId());
5101 }
5102
5103 for (const auto & streamId : streamIds) {
5104 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5105 // Streams that use the camera buffer manager are currently not supported in
5106 // offline mode
5107 if (stream->getOfflineProcessingSupport() &&
5108 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5109 offlineStreamIds->push_back(streamId);
5110 }
5111 }
5112}
5113
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005114status_t Camera3Device::setRotateAndCropAutoBehavior(
5115 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5116 ATRACE_CALL();
5117 Mutex::Autolock il(mInterfaceLock);
5118 Mutex::Autolock l(mLock);
5119 if (mRequestThread == nullptr) {
5120 return INVALID_OPERATION;
5121 }
5122 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5123}
5124
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005125bool Camera3Device::supportsCameraMute() {
5126 Mutex::Autolock il(mInterfaceLock);
5127 Mutex::Autolock l(mLock);
5128
5129 return mSupportCameraMute;
5130}
5131
5132status_t Camera3Device::setCameraMute(bool enabled) {
5133 ATRACE_CALL();
5134 Mutex::Autolock il(mInterfaceLock);
5135 Mutex::Autolock l(mLock);
5136
5137 if (mRequestThread == nullptr || !mSupportCameraMute) {
5138 return INVALID_OPERATION;
5139 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005140 int32_t muteMode =
5141 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5142 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5143 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5144 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005145}
5146
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005147status_t Camera3Device::injectCamera(const String8& injectedCamId,
5148 sp<CameraProviderManager> manager) {
5149 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5150 ATRACE_CALL();
5151 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005152 // When the camera device is active, injectCamera() and stopInjection() will call
5153 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5154 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5155 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5156 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5157 // waitUntilStateThenRelock().
5158 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005159
5160 status_t res = NO_ERROR;
5161 if (mInjectionMethods->isInjecting()) {
5162 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5163 return OK;
5164 } else {
5165 res = mInjectionMethods->stopInjection();
5166 if (res != OK) {
5167 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5168 __FUNCTION__, res);
5169 return res;
5170 }
5171 }
5172 }
5173
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005174 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005175 if (res != OK) {
5176 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5177 __FUNCTION__, res);
5178 return res;
5179 }
5180
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005181 // When the second display of android is cast to the remote device, and the opened camera is
5182 // also cast to the second display, in this case, because the camera has configured the streams
5183 // at this time, we can directly call injectCamera() to replace the internal camera with
5184 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005185 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5186 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5187
5188 camera3::camera_stream_configuration injectionConfig;
5189 std::vector<uint32_t> injectionBufferSizes;
5190 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5191 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5192 || injectionBufferSizes.size() <= 0) {
5193 ALOGE("Failed to inject camera due to abandoned configuration! "
5194 "mOperatingMode: %d injectionConfig.num_streams: %d "
5195 "injectionBufferSizes.size(): %zu", mOperatingMode,
5196 injectionConfig.num_streams, injectionBufferSizes.size());
5197 return DEAD_OBJECT;
5198 }
5199
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005200 res = mInjectionMethods->injectCamera(
5201 injectionConfig, injectionBufferSizes);
5202 if (res != OK) {
5203 ALOGE("Can't finish inject camera process!");
5204 return res;
5205 }
5206 }
5207
5208 return OK;
5209}
5210
5211status_t Camera3Device::stopInjection() {
5212 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5213 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005214 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005215 return mInjectionMethods->stopInjection();
5216}
5217
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005218}; // namespace android