blob: efd967bcb8523ffad568959b29d247241f6a07fd [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080068#include <tuple>
69
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080070using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080071using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072
73namespace android {
74
Austin Borger74fca042022-05-23 12:41:21 -070075Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
76 const String8 &id, bool overrideForPerfClass, bool legacyClient):
77 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080078 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070079 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080080 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070081 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070082 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070083 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070084 mUsePartialResult(false),
85 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080086 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080087 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070088 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070089 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070090 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070092 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000094 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010095 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070096 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070097 mNeedFixupMonochromeTags(false),
98 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099{
100 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800101 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102}
103
104Camera3Device::~Camera3Device()
105{
106 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800107 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700108 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109}
110
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800111const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800112 return mId;
113}
114
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115status_t Camera3Device::initializeCommonLocked() {
116
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700117 /** Start up status tracker thread */
118 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800119 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700120 if (res != OK) {
121 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
122 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700124 mStatusTracker.clear();
125 return res;
126 }
127
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700128 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700129 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700130
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700131 if (mUseHalBufManager) {
132 res = mRequestBufferSM.initialize(mStatusTracker);
133 if (res != OK) {
134 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
135 strerror(-res), res);
136 mInterface->close();
137 mStatusTracker.clear();
138 return res;
139 }
140 }
141
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800142 /** Create buffer manager */
143 mBufferManager = new Camera3BufferManager();
144
145 Vector<int32_t> sessionParamKeys;
146 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
147 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
148 if (sessionKeysEntry.count > 0) {
149 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
150 }
151
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700152 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
153 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
154 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
155 if (availableTestPatternModes.data.i32[i] ==
156 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
157 mSupportCameraMute = true;
158 mSupportTestPatternSolidColor = true;
159 break;
160 } else if (availableTestPatternModes.data.i32[i] ==
161 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
162 mSupportCameraMute = true;
163 mSupportTestPatternSolidColor = false;
164 }
165 }
166
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700167 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800168 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700169 this, mStatusTracker, mInterface, sessionParamKeys,
170 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800171 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800172 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700173 SET_ERR_L("Unable to start request queue thread: %s (%d)",
174 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800175 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800177 return res;
178 }
179
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700180 mPreparerThread = new PreparerThread();
181
Ruben Brunk183f0562015-08-12 12:55:02 -0700182 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400184 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700185 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800187 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800189 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800190 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800191 camera_metadata_entry timestampSource =
192 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
193 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
194 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800195 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800196 }
197
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700198 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100199 camera_metadata_entry partialResultsCount =
200 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
201 if (partialResultsCount.count > 0) {
202 mNumPartialResults = partialResultsCount.data.i32[0];
203 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700204 }
205
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800206 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
207 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700208 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700209 if (res != OK) {
210 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
211 return res;
212 }
213 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800214
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800215 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800216 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800217
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700218 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
219 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
220 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
221 }
222
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800223 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
224 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
225 }
226
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800227 // Hidl/AidlCamera3DeviceInjectionMethods
228 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800229
Ravneetaeb20dc2022-03-30 05:33:03 +0000230 /** Start watchdog thread */
231 mCameraServiceWatchdog = new CameraServiceWatchdog();
Austin Borger7b129542022-06-09 13:23:06 -0700232 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
233 if (res != OK) {
234 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
235 strerror(-res), res);
236 return res;
237 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000238
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 return OK;
240}
241
242status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700243 return disconnectImpl();
244}
245
246status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700248 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800249
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700251 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800253 Mutex::Autolock il(mInterfaceLock);
254 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
255 {
256 Mutex::Autolock l(mLock);
257 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800259 if (mStatus == STATUS_ACTIVE ||
260 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
261 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800265 } else {
266 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
267 if (res != OK) {
268 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
269 maxExpectedDuration);
270 // Continue to close device even in case of error
271 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700273 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800274
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800275 if (mStatus == STATUS_ERROR) {
276 CLOGE("Shutting down in an error state");
277 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800279 if (mStatusTracker != NULL) {
280 mStatusTracker->requestExit();
281 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800283 if (mRequestThread != NULL) {
284 mRequestThread->requestExit();
285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700286
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800287 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
288 for (size_t i = 0; i < mOutputStreams.size(); i++) {
289 streams.push_back(mOutputStreams[i]);
290 }
291 if (mInputStream != nullptr) {
292 streams.push_back(mInputStream);
293 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800296 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
297 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
299 // HAL may be in a bad state, so waiting for request thread
300 // (which may be stuck in the HAL processCaptureRequest call)
301 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800302 // give up mInterfaceLock here and then lock it again. Could this lead
303 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 mRequestThread->join();
305 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800307 Mutex::Autolock il(mInterfaceLock);
308 if (mStatusTracker != NULL) {
309 mStatusTracker->join();
310 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800312 if (mInjectionMethods->isInjecting()) {
313 mInjectionMethods->stopInjection();
314 }
315
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800316 HalInterface* interface;
317 {
318 Mutex::Autolock l(mLock);
319 mRequestThread.clear();
320 Mutex::Autolock stLock(mTrackerLock);
321 mStatusTracker.clear();
322 interface = mInterface.get();
323 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700324
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800325 // Call close without internal mutex held, as the HAL close may need to
326 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000327 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700328
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800329 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800330
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800331 {
332 Mutex::Autolock l(mLock);
333 mInterface->clear();
334 mOutputStreams.clear();
335 mInputStream.clear();
336 mDeletedStreams.clear();
337 mBufferManager.clear();
338 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
339 }
340
341 for (auto& weakStream : streams) {
342 sp<Camera3StreamInterface> stream = weakStream.promote();
343 if (stream != nullptr) {
344 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
345 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
346 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700347 }
348 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700349 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000350
351 if (mCameraServiceWatchdog != NULL) {
352 mCameraServiceWatchdog->requestExit();
353 mCameraServiceWatchdog.clear();
354 }
355
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700356 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800357}
358
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359// For dumping/debugging only -
360// try to acquire a lock a few times, eventually give up to proceed with
361// debug/dump operations
362bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
363 bool gotLock = false;
364 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
365 if (lock.tryLock() == NO_ERROR) {
366 gotLock = true;
367 break;
368 } else {
369 usleep(kDumpSleepDuration);
370 }
371 }
372 return gotLock;
373}
374
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800375nsecs_t Camera3Device::getMonoToBoottimeOffset() {
376 // try three times to get the clock offset, choose the one
377 // with the minimum gap in measurements.
378 const int tries = 3;
379 nsecs_t bestGap, measured;
380 for (int i = 0; i < tries; ++i) {
381 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
382 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
383 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
384 const nsecs_t gap = tmono2 - tmono;
385 if (i == 0 || gap < bestGap) {
386 bestGap = gap;
387 measured = tbase - ((tmono + tmono2) >> 1);
388 }
389 }
390 return measured;
391}
392
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800393ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
394 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700395 // Get max jpeg size (area-wise) for default sensor pixel mode
396 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800397 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700398 /*isUltraHighResolutionSensor*/false);
399 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
400 // not ultra high res sensor
401 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800402 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700403 /*isUltraHighResolution*/true);
404 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800405 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
406 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700407 return BAD_VALUE;
408 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700409 bool useMaxSensorPixelModeThreshold = false;
410 if (uhrMaxJpegResolution.width != 0 &&
411 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
412 // Use the ultra high res max jpeg size and max jpeg buffer size
413 useMaxSensorPixelModeThreshold = true;
414 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700415
Zhijun Hef7da0962014-04-24 13:27:56 -0700416 // Get max jpeg buffer size
417 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800418 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700419 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800420 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
421 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700422 return BAD_VALUE;
423 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700424 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700425
426 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
427 if (useMaxSensorPixelModeThreshold) {
428 maxJpegBufferSize =
429 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
430 maxDefaultJpegResolution, maxJpegBufferSize);
431 chosenMaxJpegResolution = uhrMaxJpegResolution;
432 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800433 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700434
435 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700436 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700437 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800438 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
439 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700440 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800441 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
442 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700445 return jpegBufferSize;
446}
447
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800448ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700449 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800450 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700451 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800452 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
453 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700454 return BAD_VALUE;
455 }
456 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
457 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
458 return maxBytesForPointCloud;
459}
460
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800461ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
462 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800463 const int PER_CONFIGURATION_SIZE = 3;
464 const int WIDTH_OFFSET = 0;
465 const int HEIGHT_OFFSET = 1;
466 const int SIZE_OFFSET = 2;
467 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800468 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800469 camera3::SessionConfigurationUtils::getAppropriateModeTag(
470 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
471 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800472 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800473 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800474 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
475 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800476 return BAD_VALUE;
477 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700478
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800479 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
480 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
481 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
482 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
483 }
484 }
485
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800486 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
487 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800488 return BAD_VALUE;
489}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700490
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800491status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
492 ATRACE_CALL();
493 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700494
495 // Try to lock, but continue in case of failure (to avoid blocking in
496 // deadlocks)
497 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
498 bool gotLock = tryLockSpinRightRound(mLock);
499
500 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800501 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
502 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700503 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800504 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
505 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700506
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800507 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700508
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800509 String16 templatesOption("-t");
510 int n = args.size();
511 for (int i = 0; i < n; i++) {
512 if (args[i] == templatesOption) {
513 dumpTemplates = true;
514 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000515 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700516 if (i + 1 < n) {
517 String8 monitorTags = String8(args[i + 1]);
518 if (monitorTags == "off") {
519 mTagMonitor.disableMonitoring();
520 } else {
521 mTagMonitor.parseTagsToMonitor(monitorTags);
522 }
523 } else {
524 mTagMonitor.disableMonitoring();
525 }
526 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800527 }
528
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800529 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800531 const char *status =
532 mStatus == STATUS_ERROR ? "ERROR" :
533 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700534 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
535 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800536 mStatus == STATUS_ACTIVE ? "ACTIVE" :
537 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800539 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700540 if (mStatus == STATUS_ERROR) {
541 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
542 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800544 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000545 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
546 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
547 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800548 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800549
550 if (mInputStream != NULL) {
551 write(fd, lines.string(), lines.size());
552 mInputStream->dump(fd, args);
553 } else {
554 lines.appendFormat(" No input stream.\n");
555 write(fd, lines.string(), lines.size());
556 }
557 for (size_t i = 0; i < mOutputStreams.size(); i++) {
558 mOutputStreams[i]->dump(fd,args);
559 }
560
Zhijun He431503c2016-03-07 17:30:16 -0800561 if (mBufferManager != NULL) {
562 lines = String8(" Camera3 Buffer Manager:\n");
563 write(fd, lines.string(), lines.size());
564 mBufferManager->dump(fd, args);
565 }
Zhijun He125684a2015-12-26 15:07:30 -0800566
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700567 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700568 if (mInFlightLock.try_lock()) {
569 if (mInFlightMap.size() == 0) {
570 lines.append(" None\n");
571 } else {
572 for (size_t i = 0; i < mInFlightMap.size(); i++) {
573 InFlightRequest r = mInFlightMap.valueAt(i);
574 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
575 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
576 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
577 r.numBuffersLeft);
578 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700579 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700580 mInFlightLock.unlock();
581 } else {
582 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700583 }
584 write(fd, lines.string(), lines.size());
585
Shuzhen Wang686f6442017-06-20 16:16:04 -0700586 if (mRequestThread != NULL) {
587 mRequestThread->dumpCaptureRequestLatency(fd,
588 " ProcessCaptureRequest latency histogram:");
589 }
590
Igor Murashkin1e479c02013-09-06 16:55:14 -0700591 {
592 lines = String8(" Last request sent:\n");
593 write(fd, lines.string(), lines.size());
594
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700596 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
597 }
598
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800599 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700600 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800601 "TEMPLATE_PREVIEW",
602 "TEMPLATE_STILL_CAPTURE",
603 "TEMPLATE_VIDEO_RECORD",
604 "TEMPLATE_VIDEO_SNAPSHOT",
605 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800606 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800607 };
608
Emilian Peevf4816702020-04-03 15:44:51 -0700609 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800610 camera_metadata_t *templateRequest = nullptr;
611 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700612 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800614 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 lines.append(" Not supported\n");
616 write(fd, lines.string(), lines.size());
617 } else {
618 write(fd, lines.string(), lines.size());
619 dump_indented_camera_metadata(templateRequest,
620 fd, /*verbosity*/2, /*indentation*/8);
621 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800622 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800623 }
624 }
625
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700626 mTagMonitor.dumpMonitoredMetadata(fd);
627
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800628 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800629 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800631 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800633
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700634 if (gotLock) mLock.unlock();
635 if (gotInterfaceLock) mInterfaceLock.unlock();
636
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800637 return OK;
638}
639
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700640status_t Camera3Device::startWatchingTags(const String8 &tags) {
641 mTagMonitor.parseTagsToMonitor(tags);
642 return OK;
643}
644
645status_t Camera3Device::stopWatchingTags() {
646 mTagMonitor.disableMonitoring();
647 return OK;
648}
649
650status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
651 mTagMonitor.getLatestMonitoredTagEvents(out);
652 return OK;
653}
654
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800655const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800656 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
658 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700659 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 mStatus == STATUS_ERROR ?
661 "when in error state" : "before init");
662 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700663 if (physicalId.isEmpty()) {
664 return mDeviceInfo;
665 } else {
666 std::string id(physicalId.c_str());
667 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
668 return mPhysicalDeviceInfoMap.at(id);
669 } else {
670 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
671 return mDeviceInfo;
672 }
673 }
674}
675
676const CameraMetadata& Camera3Device::info() const {
677 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800678 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800679}
680
Jianing Wei90e59c92014-03-12 18:29:36 -0700681status_t Camera3Device::checkStatusOkToCaptureLocked() {
682 switch (mStatus) {
683 case STATUS_ERROR:
684 CLOGE("Device has encountered a serious error");
685 return INVALID_OPERATION;
686 case STATUS_UNINITIALIZED:
687 CLOGE("Device not initialized");
688 return INVALID_OPERATION;
689 case STATUS_UNCONFIGURED:
690 case STATUS_CONFIGURED:
691 case STATUS_ACTIVE:
692 // OK
693 break;
694 default:
695 SET_ERR_L("Unexpected status: %d", mStatus);
696 return INVALID_OPERATION;
697 }
698 return OK;
699}
700
701status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000702 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700703 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700704 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700706 if (requestList == NULL) {
707 CLOGE("requestList cannot be NULL.");
708 return BAD_VALUE;
709 }
710
Jianing Weicb0652e2014-03-12 18:29:36 -0700711 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000712 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700713 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
714 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
715 ++metadataIt, ++surfaceMapIt) {
716 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 if (newRequest == 0) {
718 CLOGE("Can't create capture request");
719 return BAD_VALUE;
720 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700721
Shuzhen Wang9d066012016-09-30 11:30:20 -0700722 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700723 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700724
Jianing Weicb0652e2014-03-12 18:29:36 -0700725 // Setup burst Id and request Id
726 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800727 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
728 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700729 CLOGE("RequestID does not exist in metadata");
730 return BAD_VALUE;
731 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800732 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700733
Jianing Wei90e59c92014-03-12 18:29:36 -0700734 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700735
736 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700737 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700738 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
739 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
740 return BAD_VALUE;
741 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700742
743 // Setup batch size if this is a high speed video recording request.
744 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
745 auto firstRequest = requestList->begin();
746 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
747 if (outputStream->isVideoStream()) {
748 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800749 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700750 break;
751 }
752 }
753 }
754
Jianing Wei90e59c92014-03-12 18:29:36 -0700755 return OK;
756}
757
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800758status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Emilian Peevaebbe412018-01-15 13:53:24 +0000761 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000763 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800765 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700766}
767
Emilian Peevaebbe412018-01-15 13:53:24 +0000768void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 std::list<const SurfaceMap>& surfaceMaps,
770 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000771 PhysicalCameraSettingsList requestList;
772 requestList.push_back({std::string(getId().string()), request});
773 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774
775 SurfaceMap surfaceMap;
776 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
777 // With no surface list passed in, stream and surface will have 1-to-1
778 // mapping. So the surface index is 0 for each stream in the surfaceMap.
779 for (size_t i = 0; i < streams.count; i++) {
780 surfaceMap[streams.data.i32[i]].push_back(0);
781 }
782 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000786 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700787 const std::list<const SurfaceMap> &surfaceMaps,
788 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700789 /*out*/
790 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700791 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700792 nsecs_t requestTimeNs = systemTime();
793
Jianing Wei90e59c92014-03-12 18:29:36 -0700794 Mutex::Autolock il(mInterfaceLock);
795 Mutex::Autolock l(mLock);
796
797 status_t res = checkStatusOkToCaptureLocked();
798 if (res != OK) {
799 // error logged by previous call
800 return res;
801 }
802
803 RequestList requestList;
804
Shuzhen Wang0129d522016-10-30 22:43:41 -0700805 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700806 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700807 if (res != OK) {
808 // error logged by previous call
809 return res;
810 }
811
812 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700813 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700814 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700815 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700816 }
817
818 if (res == OK) {
819 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
820 if (res != OK) {
821 SET_ERR_L("Can't transition to active in %f seconds!",
822 kActiveTimeout/1e9);
823 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800824 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700825 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700826 } else {
827 CLOGE("Cannot queue request. Impossible.");
828 return BAD_VALUE;
829 }
830
831 return res;
832}
833
Emilian Peevaebbe412018-01-15 13:53:24 +0000834status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700835 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700836 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700837 ATRACE_CALL();
838
Emilian Peevaebbe412018-01-15 13:53:24 +0000839 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700840}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800841
Jianing Weicb0652e2014-03-12 18:29:36 -0700842status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
843 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845
Emilian Peevaebbe412018-01-15 13:53:24 +0000846 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700847 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000848 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700849
Emilian Peevaebbe412018-01-15 13:53:24 +0000850 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700851 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852}
853
Emilian Peevaebbe412018-01-15 13:53:24 +0000854status_t Camera3Device::setStreamingRequestList(
855 const List<const PhysicalCameraSettingsList> &requestsList,
856 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 ATRACE_CALL();
858
Emilian Peevaebbe412018-01-15 13:53:24 +0000859 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700860}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861
862sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000863 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 status_t res;
865
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800867 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
868 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000869 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700870 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700871 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700873 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700875 } else if (mStatus == STATUS_UNCONFIGURED) {
876 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 CLOGE("No streams configured");
878 return NULL;
879 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 }
881
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800884}
885
Jianing Weicb0652e2014-03-12 18:29:36 -0700886status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700888 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800890
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 switch (mStatus) {
892 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700893 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 return INVALID_OPERATION;
895 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700898 case STATUS_UNCONFIGURED:
899 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 case STATUS_ACTIVE:
901 // OK
902 break;
903 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700904 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 return INVALID_OPERATION;
906 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800907 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700908
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700909 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910}
911
912status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
913 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917}
918
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700919status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800920 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700923 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700924 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800925 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
926 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700927
928 status_t res;
929 bool wasActive = false;
930
931 switch (mStatus) {
932 case STATUS_ERROR:
933 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
934 return INVALID_OPERATION;
935 case STATUS_UNINITIALIZED:
936 ALOGE("%s: Device not initialized", __FUNCTION__);
937 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 case STATUS_UNCONFIGURED:
939 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700940 // OK
941 break;
942 case STATUS_ACTIVE:
943 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700944 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700945 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700947 return res;
948 }
949 wasActive = true;
950 break;
951 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700953 return INVALID_OPERATION;
954 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700956
957 if (mInputStream != 0) {
958 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
959 return INVALID_OPERATION;
960 }
961
962 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
963 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700965
966 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800967 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700968
969 *id = mNextStreamId++;
970
971 // Continue captures if active at start
972 if (wasActive) {
973 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100974 // Reuse current operating mode and session parameters for new stream config
975 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700976 if (res != OK) {
977 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
978 __FUNCTION__, mNextStreamId, strerror(-res), res);
979 return res;
980 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700981 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700982 }
983
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800984 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700985 return OK;
986}
987
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700988status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700989 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700990 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800991 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800992 const std::unordered_set<int32_t> &sensorPixelModesUsed,
993 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800994 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800995 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700996 ATRACE_CALL();
997
998 if (consumer == nullptr) {
999 ALOGE("%s: consumer must not be null", __FUNCTION__);
1000 return BAD_VALUE;
1001 }
1002
1003 std::vector<sp<Surface>> consumers;
1004 consumers.push_back(consumer);
1005
1006 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001007 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001008 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001009 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001010}
1011
1012static bool isRawFormat(int format) {
1013 switch (format) {
1014 case HAL_PIXEL_FORMAT_RAW16:
1015 case HAL_PIXEL_FORMAT_RAW12:
1016 case HAL_PIXEL_FORMAT_RAW10:
1017 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1018 return true;
1019 default:
1020 return false;
1021 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001022}
1023
1024status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1025 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001026 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001027 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001028 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001029 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1030 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001031 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001032
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001034 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001036 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001037 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001038 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1039 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001040 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001041 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001042 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001043
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 status_t res;
1045 bool wasActive = false;
1046
1047 switch (mStatus) {
1048 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001049 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 return INVALID_OPERATION;
1051 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 case STATUS_UNCONFIGURED:
1055 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 // OK
1057 break;
1058 case STATUS_ACTIVE:
1059 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001060 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001061 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001062 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return res;
1064 }
1065 wasActive = true;
1066 break;
1067 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001068 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 return INVALID_OPERATION;
1070 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072
1073 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001074
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 if (consumers.size() == 0 && !hasDeferredConsumer) {
1076 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1077 return BAD_VALUE;
1078 }
Zhijun He5d677d12016-05-29 16:52:39 -07001079
Shuzhen Wang0129d522016-10-30 22:43:41 -07001080 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001081 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1082 return BAD_VALUE;
1083 }
1084
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001085 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1086 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1087 // be found in only one sensor pixel mode.
1088 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1089 return BAD_VALUE;
1090 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001091 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001093 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001094 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001095 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001096 if (blobBufferSize <= 0) {
1097 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1098 return BAD_VALUE;
1099 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001100 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1101 blobBufferSize = width * height;
1102 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001103 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001104 if (blobBufferSize <= 0) {
1105 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1106 return BAD_VALUE;
1107 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001108 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001109 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001110 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001111 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001112 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001113 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001114 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001115 bool maxResolution =
1116 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1117 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001118 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1119 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001120 if (rawOpaqueBufferSize <= 0) {
1121 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1122 return BAD_VALUE;
1123 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001124 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001125 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001126 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001127 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001128 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001129 } else if (isShared) {
1130 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1131 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001132 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001133 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001134 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001135 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001136 newStream = new Camera3OutputStream(mNextStreamId,
1137 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001138 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001139 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001140 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001142 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001143 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001144 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001145 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001146 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 }
Emilian Peev40ead602017-09-26 15:46:36 +01001148
1149 size_t consumerCount = consumers.size();
1150 for (size_t i = 0; i < consumerCount; i++) {
1151 int id = newStream->getSurfaceId(consumers[i]);
1152 if (id < 0) {
1153 SET_ERR_L("Invalid surface id");
1154 return BAD_VALUE;
1155 }
1156 if (surfaceIds != nullptr) {
1157 surfaceIds->push_back(id);
1158 }
1159 }
1160
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162
Emilian Peev08dd2452017-04-06 16:55:14 +01001163 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001164
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001165 newStream->setImageDumpMask(mImageDumpMask);
1166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167 res = mOutputStreams.add(mNextStreamId, newStream);
1168 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001169 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 return res;
1171 }
1172
Shuzhen Wang316781a2020-08-18 18:11:01 -07001173 mSessionStatsBuilder.addStream(mNextStreamId);
1174
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001176 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177
1178 // Continue captures if active at start
1179 if (wasActive) {
1180 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001181 // Reuse current operating mode and session parameters for new stream config
1182 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1185 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186 return res;
1187 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001189 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001190 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
Emilian Peev710c1422017-08-30 11:19:38 +01001194status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001196 if (nullptr == streamInfo) {
1197 return BAD_VALUE;
1198 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001201
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 switch (mStatus) {
1203 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001204 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return INVALID_OPERATION;
1206 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001207 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 case STATUS_UNCONFIGURED:
1210 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 case STATUS_ACTIVE:
1212 // OK
1213 break;
1214 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001215 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 return INVALID_OPERATION;
1217 }
1218
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001219 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1220 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001222 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 }
1224
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001225 streamInfo->width = stream->getWidth();
1226 streamInfo->height = stream->getHeight();
1227 streamInfo->format = stream->getFormat();
1228 streamInfo->dataSpace = stream->getDataSpace();
1229 streamInfo->formatOverridden = stream->isFormatOverridden();
1230 streamInfo->originalFormat = stream->getOriginalFormat();
1231 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1232 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001233 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235}
1236
1237status_t Camera3Device::setStreamTransform(int id,
1238 int transform) {
1239 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001240 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 switch (mStatus) {
1244 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
1247 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 case STATUS_UNCONFIGURED:
1251 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 case STATUS_ACTIVE:
1253 // OK
1254 break;
1255 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return INVALID_OPERATION;
1258 }
1259
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001260 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1261 if (stream == nullptr) {
1262 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return BAD_VALUE;
1264 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001265 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
1268status_t Camera3Device::deleteStream(int id) {
1269 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 Mutex::Autolock l(mLock);
1272 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001274 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001276 // CameraDevice semantics require device to already be idle before
1277 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001279 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001280 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 }
1282
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001283 if (mStatus == STATUS_ERROR) {
1284 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1285 __FUNCTION__, mId.string());
1286 return -EBUSY;
1287 }
1288
Igor Murashkin2fba5842013-04-22 14:03:54 -07001289 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001290 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 if (mInputStream != NULL && id == mInputStream->getId()) {
1292 deletedStream = mInputStream;
1293 mInputStream.clear();
1294 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001295 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001296 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 return BAD_VALUE;
1298 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001299 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001300 }
1301
1302 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001303 if (stream != nullptr) {
1304 deletedStream = stream;
1305 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 }
1307
1308 // Free up the stream endpoint so that it can be used by some other stream
1309 res = deletedStream->disconnect();
1310 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 // fall through since we want to still list the stream as deleted.
1313 }
1314 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001315 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316
1317 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001318}
1319
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001320status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001321 ATRACE_CALL();
1322 ALOGV("%s: E", __FUNCTION__);
1323
1324 Mutex::Autolock il(mInterfaceLock);
1325 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001326
Emilian Peev811d2952018-05-25 11:08:40 +01001327 // In case the client doesn't include any session parameter, try a
1328 // speculative configuration using the values from the last cached
1329 // default request.
1330 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001331 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001332 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1333 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1334 mLastTemplateId);
1335 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1336 operatingMode);
1337 }
1338
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001339 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1340}
1341
1342status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1343 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001344 //Filter out any incoming session parameters
1345 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001346 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1347 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001348 CameraMetadata filteredParams(availableSessionKeys.count);
1349 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1350 filteredParams.getAndLock());
1351 set_camera_metadata_vendor_id(meta, mVendorTagId);
1352 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001353 if (availableSessionKeys.count > 0) {
1354 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1355 camera_metadata_ro_entry entry = params.find(
1356 availableSessionKeys.data.i32[i]);
1357 if (entry.count > 0) {
1358 filteredParams.update(entry);
1359 }
1360 }
1361 }
1362
1363 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001364}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001365
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001366status_t Camera3Device::getInputBufferProducer(
1367 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001368 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001369 Mutex::Autolock il(mInterfaceLock);
1370 Mutex::Autolock l(mLock);
1371
1372 if (producer == NULL) {
1373 return BAD_VALUE;
1374 } else if (mInputStream == NULL) {
1375 return INVALID_OPERATION;
1376 }
1377
1378 return mInputStream->getInputBufferProducer(producer);
1379}
1380
Emilian Peevf4816702020-04-03 15:44:51 -07001381status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382 CameraMetadata *request) {
1383 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001384 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001385
Emilian Peevf4816702020-04-03 15:44:51 -07001386 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001387 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001388 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001389 return BAD_VALUE;
1390 }
1391
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001394 {
1395 Mutex::Autolock l(mLock);
1396 switch (mStatus) {
1397 case STATUS_ERROR:
1398 CLOGE("Device has encountered a serious error");
1399 return INVALID_OPERATION;
1400 case STATUS_UNINITIALIZED:
1401 CLOGE("Device is not initialized!");
1402 return INVALID_OPERATION;
1403 case STATUS_UNCONFIGURED:
1404 case STATUS_CONFIGURED:
1405 case STATUS_ACTIVE:
1406 // OK
1407 break;
1408 default:
1409 SET_ERR_L("Unexpected status: %d", mStatus);
1410 return INVALID_OPERATION;
1411 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001412
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001413 if (!mRequestTemplateCache[templateId].isEmpty()) {
1414 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001415 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001416 return OK;
1417 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001418 }
1419
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001420 camera_metadata_t *rawRequest;
1421 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001422 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001423
1424 {
1425 Mutex::Autolock l(mLock);
1426 if (res == BAD_VALUE) {
1427 ALOGI("%s: template %d is not supported on this camera device",
1428 __FUNCTION__, templateId);
1429 return res;
1430 } else if (res != OK) {
1431 CLOGE("Unable to construct request template %d: %s (%d)",
1432 templateId, strerror(-res), res);
1433 return res;
1434 }
1435
1436 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1437 mRequestTemplateCache[templateId].acquire(rawRequest);
1438
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001439 // Override the template request with zoomRatioMapper
1440 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1441 &mRequestTemplateCache[templateId]);
1442 if (res != OK) {
1443 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1444 templateId, strerror(-res), res);
1445 return res;
1446 }
1447
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001448 // Fill in JPEG_QUALITY if not available
1449 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1450 static const uint8_t kDefaultJpegQuality = 95;
1451 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1452 &kDefaultJpegQuality, 1);
1453 }
1454
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001455 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001456 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001457 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001458 return OK;
1459}
1460
1461status_t Camera3Device::waitUntilDrained() {
1462 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001463 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001464 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001467 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001468}
1469
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001470status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 switch (mStatus) {
1472 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001473 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 ALOGV("%s: Already idle", __FUNCTION__);
1475 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001476 case STATUS_CONFIGURED:
1477 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 case STATUS_ERROR:
1479 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 break;
1482 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484 return INVALID_OPERATION;
1485 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001486 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1487 maxExpectedDuration);
1488 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001489 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001490 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001491 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1492 res);
1493 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001494 return res;
1495}
1496
Ruben Brunk183f0562015-08-12 12:55:02 -07001497void Camera3Device::internalUpdateStatusLocked(Status status) {
1498 mStatus = status;
1499 mRecentStatusUpdates.add(mStatus);
1500 mStatusChanged.broadcast();
1501}
1502
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001503// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001504status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001505 if (mRequestThread.get() != nullptr) {
1506 mRequestThread->setPaused(true);
1507 } else {
1508 return NO_INIT;
1509 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001511 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1512 maxExpectedDuration);
1513 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001514 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001515 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001517 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001518 }
1519
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520 return res;
1521}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001523// Resume after internalPauseAndWaitLocked
1524status_t Camera3Device::internalResumeLocked() {
1525 status_t res;
1526
1527 mRequestThread->setPaused(false);
1528
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001529 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1530 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001531 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1532 if (res != OK) {
1533 SET_ERR_L("Can't transition to active in %f seconds!",
1534 kActiveTimeout/1e9);
1535 }
1536 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538}
1539
Ruben Brunk183f0562015-08-12 12:55:02 -07001540status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001541 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001542
1543 size_t startIndex = 0;
1544 if (mStatusWaiters == 0) {
1545 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1546 // this status list
1547 mRecentStatusUpdates.clear();
1548 } else {
1549 // If other threads are waiting on updates to this status list, set the position of the
1550 // first element that this list will check rather than clearing the list.
1551 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001552 }
1553
Ruben Brunk183f0562015-08-12 12:55:02 -07001554 mStatusWaiters++;
1555
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001556 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001557 if (!active && mUseHalBufManager) {
1558 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001559 if (mStatus == STATUS_ACTIVE) {
1560 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001561 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001562 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001563 mRequestBufferSM.onWaitUntilIdle();
1564 }
1565
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001567 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001569 if (active == (mStatus == STATUS_ACTIVE)) {
1570 // Desired state is current
1571 break;
1572 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001573
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001574 nsecs_t timeElapsed = systemTime() - startTime;
1575 nsecs_t timeToWait = timeout - timeElapsed;
1576 if (timeToWait <= 0) {
1577 // Thread woke up spuriously but has timed out since.
1578 // Force out of loop with TIMED_OUT result.
1579 res = TIMED_OUT;
1580 break;
1581 }
1582 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 if (res != OK) break;
1584
Ruben Brunk183f0562015-08-12 12:55:02 -07001585 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1586 // transitions.
1587 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1588 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1589 __FUNCTION__);
1590
1591 // Encountered desired state since we began waiting
1592 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1594 stateSeen = true;
1595 break;
1596 }
1597 }
1598 } while (!stateSeen);
1599
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001600 if (signalPipelineDrain) {
1601 mRequestThread->resetPipelineDrain();
1602 }
1603
Ruben Brunk183f0562015-08-12 12:55:02 -07001604 mStatusWaiters--;
1605
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001606 return res;
1607}
1608
1609
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001610status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001612 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001614 if (listener != NULL && mListener != NULL) {
1615 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1616 }
1617 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001618 mRequestThread->setNotificationListener(listener);
1619 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001620
1621 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001622}
1623
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001624bool Camera3Device::willNotify3A() {
1625 return false;
1626}
1627
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001628status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001629 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001630 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001631
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001632 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001633 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1634 if (st == std::cv_status::timeout) {
1635 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001636 }
1637 }
1638 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001639}
1640
Jianing Weicb0652e2014-03-12 18:29:36 -07001641status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001642 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001643 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001644
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001645 if (mResultQueue.empty()) {
1646 return NOT_ENOUGH_DATA;
1647 }
1648
Jianing Weicb0652e2014-03-12 18:29:36 -07001649 if (frame == NULL) {
1650 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1651 return BAD_VALUE;
1652 }
1653
1654 CaptureResult &result = *(mResultQueue.begin());
1655 frame->mResultExtras = result.mResultExtras;
1656 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001657 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001658 mResultQueue.erase(mResultQueue.begin());
1659
1660 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001661}
1662
1663status_t Camera3Device::triggerAutofocus(uint32_t id) {
1664 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001665 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001666
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001667 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1668 // Mix-in this trigger into the next request and only the next request.
1669 RequestTrigger trigger[] = {
1670 {
1671 ANDROID_CONTROL_AF_TRIGGER,
1672 ANDROID_CONTROL_AF_TRIGGER_START
1673 },
1674 {
1675 ANDROID_CONTROL_AF_TRIGGER_ID,
1676 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001677 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001678 };
1679
1680 return mRequestThread->queueTrigger(trigger,
1681 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001682}
1683
1684status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1685 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001686 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001687
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001688 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1689 // Mix-in this trigger into the next request and only the next request.
1690 RequestTrigger trigger[] = {
1691 {
1692 ANDROID_CONTROL_AF_TRIGGER,
1693 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1694 },
1695 {
1696 ANDROID_CONTROL_AF_TRIGGER_ID,
1697 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001698 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001699 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001701 return mRequestThread->queueTrigger(trigger,
1702 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001703}
1704
1705status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1706 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001707 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001708
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001709 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1710 // Mix-in this trigger into the next request and only the next request.
1711 RequestTrigger trigger[] = {
1712 {
1713 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1714 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1715 },
1716 {
1717 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1718 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001719 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001720 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001722 return mRequestThread->queueTrigger(trigger,
1723 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001724}
1725
Jianing Weicb0652e2014-03-12 18:29:36 -07001726status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001727 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001728 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001730
Zhijun He7ef20392014-04-21 16:04:17 -07001731 {
1732 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001733
1734 // b/116514106 "disconnect()" can get called twice for the same device. The
1735 // camera device will not be initialized during the second run.
1736 if (mStatus == STATUS_UNINITIALIZED) {
1737 return OK;
1738 }
1739
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001740 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001741
1742 // Stop session and stream counter
1743 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001744 }
1745
Ravneetdbd5b242022-03-02 07:22:46 +00001746 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001747 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001748 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1749 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1750
1751 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001752}
1753
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001754status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001755 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1756}
1757
1758status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001759 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001760 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001761 Mutex::Autolock il(mInterfaceLock);
1762 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001763
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001764 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1765 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001766 CLOGE("Stream %d does not exist", streamId);
1767 return BAD_VALUE;
1768 }
1769
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001770 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001771 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001772 return BAD_VALUE;
1773 }
1774
1775 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001776 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001777 return BAD_VALUE;
1778 }
1779
Ruben Brunkc78ac262015-08-13 17:58:46 -07001780 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001781}
1782
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001783status_t Camera3Device::tearDown(int streamId) {
1784 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001785 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001786 Mutex::Autolock il(mInterfaceLock);
1787 Mutex::Autolock l(mLock);
1788
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001789 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1790 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001791 CLOGE("Stream %d does not exist", streamId);
1792 return BAD_VALUE;
1793 }
1794
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001795 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1796 CLOGE("Stream %d is a target of a in-progress request", streamId);
1797 return BAD_VALUE;
1798 }
1799
1800 return stream->tearDown();
1801}
1802
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001803status_t Camera3Device::addBufferListenerForStream(int streamId,
1804 wp<Camera3StreamBufferListener> listener) {
1805 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001806 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001807 Mutex::Autolock il(mInterfaceLock);
1808 Mutex::Autolock l(mLock);
1809
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001810 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1811 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001812 CLOGE("Stream %d does not exist", streamId);
1813 return BAD_VALUE;
1814 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001815 stream->addBufferListener(listener);
1816
1817 return OK;
1818}
1819
Austin Borger4a870a32022-02-25 01:48:41 +00001820float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1821 camera_metadata_entry minDurations =
1822 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1823 for (size_t i = 0; i < minDurations.count; i += 4) {
1824 if (minDurations.data.i64[i] == stream->getFormat()
1825 && minDurations.data.i64[i+1] == stream->getWidth()
1826 && minDurations.data.i64[i+2] == stream->getHeight()) {
1827 int64_t minFrameDuration = minDurations.data.i64[i+3];
1828 return 1e9f / minFrameDuration;
1829 }
1830 }
1831 return 0.0f;
1832}
1833
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001834/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001835 * Methods called by subclasses
1836 */
1837
1838void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001839 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001840 std::vector<int> streamIds;
1841 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001842 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001843
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001844 {
1845 // Need mLock to safely update state and synchronize to current
1846 // state of methods in flight.
1847 Mutex::Autolock l(mLock);
1848 // We can get various system-idle notices from the status tracker
1849 // while starting up. Only care about them if we've actually sent
1850 // in some requests recently.
1851 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1852 return;
1853 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001854 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1855 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001856 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001857
1858 // Skip notifying listener if we're doing some user-transparent
1859 // state changes
1860 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001861
Austin Borger4a870a32022-02-25 01:48:41 +00001862 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1863 auto stream = mOutputStreams[i];
1864 if (stream.get() == nullptr) continue;
1865
1866 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1867 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1868
1869 // Populate stream statistics in case of Idle
1870 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001871 streamIds.push_back(stream->getId());
1872 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1873 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001874 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001875 if (camera3Stream != nullptr) {
1876 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001877 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001878 }
1879 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001880 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001881 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001882 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001883 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001884 }
1885 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001887
1888 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001889 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001890 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001891 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001892 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001893 status_t res = OK;
1894 if (listener != nullptr) {
1895 if (idle) {
1896 // Get session stats from the builder, and notify the listener.
1897 int64_t requestCount, resultErrorCount;
1898 bool deviceError;
1899 std::map<int, StreamStats> streamStatsMap;
1900 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1901 &deviceError, &streamStatsMap);
1902 for (size_t i = 0; i < streamIds.size(); i++) {
1903 int streamId = streamIds[i];
1904 auto stats = streamStatsMap.find(streamId);
1905 if (stats != streamStatsMap.end()) {
1906 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1907 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1908 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1909 streamStats[i].mHistogramType =
1910 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1911 streamStats[i].mHistogramBins.assign(
1912 stats->second.mCaptureLatencyBins.begin(),
1913 stats->second.mCaptureLatencyBins.end());
1914 streamStats[i].mHistogramCounts.assign(
1915 stats->second.mCaptureLatencyHistogram.begin(),
1916 stats->second.mCaptureLatencyHistogram.end());
1917 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001918 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001919 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1920 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001921 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001922 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001923 }
1924 if (res != OK) {
1925 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1926 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001927 }
1928}
1929
Shuzhen Wang758c2152017-01-10 18:26:18 -08001930status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001931 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001932 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001933 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1934 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001935
1936 if (surfaceIds == nullptr) {
1937 return BAD_VALUE;
1938 }
1939
Zhijun He5d677d12016-05-29 16:52:39 -07001940 Mutex::Autolock il(mInterfaceLock);
1941 Mutex::Autolock l(mLock);
1942
Shuzhen Wang758c2152017-01-10 18:26:18 -08001943 if (consumers.size() == 0) {
1944 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001945 return BAD_VALUE;
1946 }
1947
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001948 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1949 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001950 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001951 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001952 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001953
1954 // isConsumerConfigurationDeferred will be off after setConsumers
1955 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001956 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001957 if (res != OK) {
1958 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1959 return res;
1960 }
1961
Emilian Peev40ead602017-09-26 15:46:36 +01001962 for (auto &consumer : consumers) {
1963 int id = stream->getSurfaceId(consumer);
1964 if (id < 0) {
1965 CLOGE("Invalid surface id!");
1966 return BAD_VALUE;
1967 }
1968 surfaceIds->push_back(id);
1969 }
1970
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001971 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001972 if (!stream->isConfiguring()) {
1973 CLOGE("Stream %d was already fully configured.", streamId);
1974 return INVALID_OPERATION;
1975 }
Zhijun He5d677d12016-05-29 16:52:39 -07001976
Shuzhen Wang0129d522016-10-30 22:43:41 -07001977 res = stream->finishConfiguration();
1978 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001979 // If finishConfiguration fails due to abandoned surface, do not set
1980 // device to error state.
1981 bool isSurfaceAbandoned =
1982 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1983 if (!isSurfaceAbandoned) {
1984 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1985 stream->getId(), strerror(-res), res);
1986 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001987 return res;
1988 }
Zhijun He5d677d12016-05-29 16:52:39 -07001989 }
1990
1991 return OK;
1992}
1993
Emilian Peev40ead602017-09-26 15:46:36 +01001994status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1995 const std::vector<OutputStreamInfo> &outputInfo,
1996 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1997 Mutex::Autolock il(mInterfaceLock);
1998 Mutex::Autolock l(mLock);
1999
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002000 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2001 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002002 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002003 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002004 }
2005
2006 for (const auto &it : removedSurfaceIds) {
2007 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2008 CLOGE("Shared surface still part of a pending request!");
2009 return -EBUSY;
2010 }
2011 }
2012
Emilian Peev40ead602017-09-26 15:46:36 +01002013 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2014 if (res != OK) {
2015 CLOGE("Stream %d failed to update stream (error %d %s) ",
2016 streamId, res, strerror(-res));
2017 if (res == UNKNOWN_ERROR) {
2018 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2019 __FUNCTION__);
2020 }
2021 return res;
2022 }
2023
2024 return res;
2025}
2026
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002027status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2028 Mutex::Autolock il(mInterfaceLock);
2029 Mutex::Autolock l(mLock);
2030
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002031 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2032 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002033 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2034 return BAD_VALUE;
2035 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002036
2037 if (dropping) {
2038 mSessionStatsBuilder.stopCounter(streamId);
2039 } else {
2040 mSessionStatsBuilder.startCounter(streamId);
2041 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002042 return stream->dropBuffers(dropping);
2043}
2044
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002045/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002046 * Camera3Device private methods
2047 */
2048
2049sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002050 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002051 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002053 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002054 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002055
2056 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002057 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058 if (inputStreams.count > 0) {
2059 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002060 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002061 CLOGE("Request references unknown input stream %d",
2062 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002063 return NULL;
2064 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002065
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002067 SET_ERR_L("%s: input stream %d is not configured!",
2068 __FUNCTION__, mInputStream->getId());
2069 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002071 // Check if stream prepare is blocking requests.
2072 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002073 CLOGE("Request references an input stream that's being prepared!");
2074 return NULL;
2075 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076
2077 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002078 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002079 }
2080
2081 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002082 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002084 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 return NULL;
2086 }
2087
2088 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002089 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2090 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002091 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002092 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093 return NULL;
2094 }
Zhijun He5d677d12016-05-29 16:52:39 -07002095 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002096 auto iter = surfaceMap.find(streams.data.i32[i]);
2097 if (iter != surfaceMap.end()) {
2098 const std::vector<size_t>& surfaces = iter->second;
2099 for (const auto& surface : surfaces) {
2100 if (stream->isConsumerConfigurationDeferred(surface)) {
2101 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2102 "due to deferred consumer", stream->getId(), surface);
2103 return NULL;
2104 }
2105 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002106 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002107 }
2108
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002109 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002110 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2111 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002113 // Check if stream prepare is blocking requests.
2114 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002115 CLOGE("Request references an output stream that's being prepared!");
2116 return NULL;
2117 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118
2119 newRequest->mOutputStreams.push(stream);
2120 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002121 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002122 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002124 auto rotateAndCropEntry =
2125 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2126 if (rotateAndCropEntry.count > 0 &&
2127 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2128 newRequest->mRotateAndCropAuto = true;
2129 } else {
2130 newRequest->mRotateAndCropAuto = false;
2131 }
2132
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002133 auto zoomRatioEntry =
2134 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2135 if (zoomRatioEntry.count > 0 &&
2136 zoomRatioEntry.data.f[0] == 1.0f) {
2137 newRequest->mZoomRatioIs1x = true;
2138 } else {
2139 newRequest->mZoomRatioIs1x = false;
2140 }
2141
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002142 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002143 for (auto& settings : newRequest->mSettingsList) {
2144 auto testPatternModeEntry =
2145 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2146 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2147 testPatternModeEntry.data.i32[0] :
2148 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002149
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002150 auto testPatternDataEntry =
2151 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2152 if (testPatternDataEntry.count >= 4) {
2153 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2154 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2155 } else {
2156 settings.mOriginalTestPatternData[0] = 0;
2157 settings.mOriginalTestPatternData[1] = 0;
2158 settings.mOriginalTestPatternData[2] = 0;
2159 settings.mOriginalTestPatternData[3] = 0;
2160 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002161 }
2162 }
2163
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002164 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002165}
2166
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002167void Camera3Device::cancelStreamsConfigurationLocked() {
2168 int res = OK;
2169 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2170 res = mInputStream->cancelConfiguration();
2171 if (res != OK) {
2172 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2173 mInputStream->getId(), strerror(-res), res);
2174 }
2175 }
2176
2177 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002178 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002179 if (outputStream->isConfiguring()) {
2180 res = outputStream->cancelConfiguration();
2181 if (res != OK) {
2182 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2183 outputStream->getId(), strerror(-res), res);
2184 }
2185 }
2186 }
2187
2188 // Return state to that at start of call, so that future configures
2189 // properly clean things up
2190 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2191 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002192
2193 res = mPreparerThread->resume();
2194 if (res != OK) {
2195 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2196 }
2197}
2198
Emilian Peev0d0191e2020-04-21 17:01:18 -07002199bool Camera3Device::checkAbandonedStreamsLocked() {
2200 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2201 return true;
2202 }
2203
2204 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2205 auto stream = mOutputStreams[i];
2206 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2207 return true;
2208 }
2209 }
2210
2211 return false;
2212}
2213
Emilian Peev3bead5f2020-05-28 17:29:08 -07002214bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002215 ATRACE_CALL();
2216 bool ret = false;
2217
Shuzhen Wang316781a2020-08-18 18:11:01 -07002218 nsecs_t startTime = systemTime();
2219
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002220 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002221 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2222
2223 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002224 if (checkAbandonedStreamsLocked()) {
2225 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2226 __FUNCTION__);
2227 return true;
2228 }
2229
Emilian Peev3bead5f2020-05-28 17:29:08 -07002230 status_t rc = NO_ERROR;
2231 bool markClientActive = false;
2232 if (mStatus == STATUS_ACTIVE) {
2233 markClientActive = true;
2234 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002235
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) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003451 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3452 if (statusTracker != nullptr) {
3453 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3454 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003455 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003456 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003457 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003458
3459 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3460 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3461 if (res != OK) {
3462 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3463 cleanUpFailedRequests(/*sendRequestError*/ false);
3464 return false;
3465 }
3466 }
3467 }
3468 }
3469
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003470 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003471 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003472 if (res == TIMED_OUT) {
3473 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003474 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003475 // Check if any stream is abandoned.
3476 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003477 return true;
3478 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003479 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003480 return false;
3481 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003482
Zhijun Hecc27e112013-10-03 16:12:43 -07003483 // Inform waitUntilRequestProcessed thread of a new request ID
3484 {
3485 Mutex::Autolock al(mLatestRequestMutex);
3486
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003487 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003488 mLatestRequestSignal.signal();
3489 }
3490
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003491 // Submit a batch of requests to HAL.
3492 // Use flush lock only when submitting multilple requests in a batch.
3493 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3494 // which may take a long time to finish so synchronizing flush() and
3495 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3496 // For now, only synchronize for high speed recording and we should figure something out for
3497 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003498 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003499
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003500 if (useFlushLock) {
3501 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003502 }
3503
Zhijun Hef0645c12016-08-02 00:58:11 -07003504 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003505 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003506
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003507 sp<Camera3Device> parent = mParent.promote();
3508 if (parent != nullptr) {
3509 parent->mRequestBufferSM.onSubmittingRequest();
3510 }
3511
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003512 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003513 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003514 submitRequestSuccess = sendRequestsBatch();
3515
Shuzhen Wang686f6442017-06-20 16:16:04 -07003516 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3517 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003518
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003519 if (useFlushLock) {
3520 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003521 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003522
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003523 // Unset as current request
3524 {
3525 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003526 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003527 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003528 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003529
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003530 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003531}
3532
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003533status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3534 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3535 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3536 ANDROID_SCALER_CROP_REGION_SET};
3537 if (request == nullptr) {
3538 ALOGE("%s request metadata nullptr", __FUNCTION__);
3539 return BAD_VALUE;
3540 }
3541 status_t res = OK;
3542 for (const auto &key : kFwkOnlyRegionKeys) {
3543 if (request->exists(key)) {
3544 res = request->erase(key);
3545 if (res != OK) {
3546 return res;
3547 }
3548 }
3549 }
3550 return OK;
3551}
3552
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003553status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003554 ATRACE_CALL();
3555
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003556 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003557 for (size_t i = 0; i < mNextRequests.size(); i++) {
3558 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003559 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003560 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3561 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003562
3563 // Prepare a request to HAL
3564 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3565
3566 // Insert any queued triggers (before metadata is locked)
3567 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003568 if (res < 0) {
3569 SET_ERR("RequestThread: Unable to insert triggers "
3570 "(capture request %d, HAL device: %s (%d)",
3571 halRequest->frame_number, strerror(-res), res);
3572 return INVALID_OPERATION;
3573 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003574
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003575 int triggerCount = res;
3576 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3577 mPrevTriggers = triggerCount;
3578
Emilian Peeve23f1d92021-09-20 14:56:01 -07003579 // Do not override rotate&crop for stream configurations that include
3580 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3581 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3582 bool rotateAndCropChanged = mComposerOutput ? false :
3583 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003584 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003585
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003586 // If the request is the same as last, or we had triggers now or last time or
3587 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003588 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003589 (mPrevRequest != captureRequest || triggersMixedIn ||
3590 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003591 // Request settings are all the same within one batch, so only treat the first
3592 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003593 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003594 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003595 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003596 /**
3597 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003598 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003599 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003600 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003601 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003602 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003603 "(capture request %d, HAL device: %s (%d)",
3604 halRequest->frame_number, strerror(-res), res);
3605 return INVALID_OPERATION;
3606 }
3607
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003608 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003609 sp<Camera3Device> parent = mParent.promote();
3610 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003611 List<PhysicalCameraSettings>::iterator it;
3612 for (it = captureRequest->mSettingsList.begin();
3613 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003614 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3615 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003616 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3617 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3618 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3619 res);
3620 return INVALID_OPERATION;
3621 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003622 continue;
3623 }
3624
3625 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3626 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3627 updateCaptureRequest(&(it->metadata));
3628 if (res != OK) {
3629 SET_ERR("RequestThread: Unable to correct capture requests "
3630 "for scaler crop region and metering regions for request "
3631 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3632 res);
3633 return INVALID_OPERATION;
3634 }
3635 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003636 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3637 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3638 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3639 res);
3640 return INVALID_OPERATION;
3641 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003642 }
3643 }
3644
3645 // Correct metadata regions for distortion correction if enabled
3646 for (it = captureRequest->mSettingsList.begin();
3647 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003648 if (parent->mDistortionMappers.find(it->cameraId) ==
3649 parent->mDistortionMappers.end()) {
3650 continue;
3651 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003652
3653 if (!captureRequest->mDistortionCorrectionUpdated) {
3654 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3655 &(it->metadata));
3656 if (res != OK) {
3657 SET_ERR("RequestThread: Unable to correct capture requests "
3658 "for lens distortion for request %d: %s (%d)",
3659 halRequest->frame_number, strerror(-res), res);
3660 return INVALID_OPERATION;
3661 }
3662 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003663 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003664 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003665
3666 for (it = captureRequest->mSettingsList.begin();
3667 it != captureRequest->mSettingsList.end(); it++) {
3668 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3669 parent->mZoomRatioMappers.end()) {
3670 continue;
3671 }
3672
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003673 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003674 cameraIdsWithZoom.insert(it->cameraId);
3675 }
3676
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003677 if (!captureRequest->mZoomRatioUpdated) {
3678 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3679 &(it->metadata));
3680 if (res != OK) {
3681 SET_ERR("RequestThread: Unable to correct capture requests "
3682 "for zoom ratio for request %d: %s (%d)",
3683 halRequest->frame_number, strerror(-res), res);
3684 return INVALID_OPERATION;
3685 }
3686 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003687 }
3688 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003689 if (captureRequest->mRotateAndCropAuto &&
3690 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003691 for (it = captureRequest->mSettingsList.begin();
3692 it != captureRequest->mSettingsList.end(); it++) {
3693 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3694 if (mapper != parent->mRotateAndCropMappers.end()) {
3695 res = mapper->second.updateCaptureRequest(&(it->metadata));
3696 if (res != OK) {
3697 SET_ERR("RequestThread: Unable to correct capture requests "
3698 "for rotate-and-crop for request %d: %s (%d)",
3699 halRequest->frame_number, strerror(-res), res);
3700 return INVALID_OPERATION;
3701 }
3702 }
3703 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003704 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003705 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003706 }
3707 }
3708
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003709 /**
3710 * The request should be presorted so accesses in HAL
3711 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3712 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003713 captureRequest->mSettingsList.begin()->metadata.sort();
3714 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003715 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003716 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003717 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3718
3719 IF_ALOGV() {
3720 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3721 find_camera_metadata_ro_entry(
3722 halRequest->settings,
3723 ANDROID_CONTROL_AF_TRIGGER,
3724 &e
3725 );
3726 if (e.count > 0) {
3727 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3728 __FUNCTION__,
3729 halRequest->frame_number,
3730 e.data.u8[0]);
3731 }
3732 }
3733 } else {
3734 // leave request.settings NULL to indicate 'reuse latest given'
3735 ALOGVV("%s: Request settings are REUSED",
3736 __FUNCTION__);
3737 }
3738
Emilian Peevaebbe412018-01-15 13:53:24 +00003739 if (captureRequest->mSettingsList.size() > 1) {
3740 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3741 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003742 if (newRequest) {
3743 halRequest->physcam_settings =
3744 new const camera_metadata* [halRequest->num_physcam_settings];
3745 } else {
3746 halRequest->physcam_settings = nullptr;
3747 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003748 auto it = ++captureRequest->mSettingsList.begin();
3749 size_t i = 0;
3750 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3751 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003752 if (newRequest) {
3753 it->metadata.sort();
3754 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3755 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003756 }
3757 }
3758
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003759 uint32_t totalNumBuffers = 0;
3760
3761 // Fill in buffers
3762 if (captureRequest->mInputStream != NULL) {
3763 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003764
3765 halRequest->input_width = captureRequest->mInputBufferSize.width;
3766 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003767 totalNumBuffers += 1;
3768 } else {
3769 halRequest->input_buffer = NULL;
3770 }
3771
Emilian Peevf4816702020-04-03 15:44:51 -07003772 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003773 captureRequest->mOutputStreams.size());
3774 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003775 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003776
3777 sp<Camera3Device> parent = mParent.promote();
3778 if (parent == NULL) {
3779 // Should not happen, and nowhere to send errors to, so just log it
3780 CLOGE("RequestThread: Parent is gone");
3781 return INVALID_OPERATION;
3782 }
3783 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3784
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003785 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003786 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003787 sp<Camera3OutputStreamInterface> outputStream =
3788 captureRequest->mOutputStreams.editItemAt(j);
3789 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003790
3791 // Prepare video buffers for high speed recording on the first video request.
3792 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3793 // Only try to prepare video stream on the first video request.
3794 mPrepareVideoStream = false;
3795
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003796 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3797 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003798 while (res == NOT_ENOUGH_DATA) {
3799 res = outputStream->prepareNextBuffer();
3800 }
3801 if (res != OK) {
3802 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3803 __FUNCTION__, strerror(-res), res);
3804 outputStream->cancelPrepare();
3805 }
3806 }
3807
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003808 std::vector<size_t> uniqueSurfaceIds;
3809 res = outputStream->getUniqueSurfaceIds(
3810 captureRequest->mOutputSurfaces[streamId],
3811 &uniqueSurfaceIds);
3812 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3813 if (res != OK && res != INVALID_OPERATION) {
3814 ALOGE("%s: failed to query stream %d unique surface IDs",
3815 __FUNCTION__, streamId);
3816 return res;
3817 }
3818 if (res == OK) {
3819 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3820 }
3821
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003822 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003823 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003824 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003825 return TIMED_OUT;
3826 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003827 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003828 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003829 buffer.stream = outputStream->asHalStream();
3830 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003831 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003832 buffer.acquire_fence = -1;
3833 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003834 // Mark the output stream as unpreparable to block clients from calling
3835 // 'prepare' after this request reaches CameraHal and before the respective
3836 // buffers are requested.
3837 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003838 } else {
3839 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3840 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003841 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003842 if (res != OK) {
3843 // Can't get output buffer from gralloc queue - this could be due to
3844 // abandoned queue or other consumer misbehavior, so not a fatal
3845 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003846 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003847 " %s (%d)", strerror(-res), res);
3848
3849 return TIMED_OUT;
3850 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003851 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003852
3853 {
3854 sp<Camera3Device> parent = mParent.promote();
3855 if (parent != nullptr) {
3856 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3857 for (const auto& settings : captureRequest->mSettingsList) {
3858 if ((streamCameraId.isEmpty() &&
3859 parent->getId() == settings.cameraId.c_str()) ||
3860 streamCameraId == settings.cameraId.c_str()) {
3861 outputStream->fireBufferRequestForFrameNumber(
3862 captureRequest->mResultExtras.frameNumber,
3863 settings.metadata);
3864 }
3865 }
3866 }
3867 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003868
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003869 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003870 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3871 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3872 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3873 } else if (!physicalCameraId.isEmpty()) {
3874 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003875 }
3876 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003877 }
3878 totalNumBuffers += halRequest->num_output_buffers;
3879
3880 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003881 // If this request list is for constrained high speed recording (not
3882 // preview), and the current request is not the last one in the batch,
3883 // do not send callback to the app.
3884 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003885 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003886 hasCallback = false;
3887 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003888 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003889 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003890 const camera_metadata_t* settings = halRequest->settings;
3891 bool shouldUnlockSettings = false;
3892 if (settings == nullptr) {
3893 shouldUnlockSettings = true;
3894 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3895 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003896 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3897 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003898 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003899 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3900 isStillCapture = true;
3901 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3902 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003903
Emilian Peevaf8416e2021-02-04 17:52:43 -08003904 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003905 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003906 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3907 isZslCapture = true;
3908 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003909 }
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003910 auto expectedDurationRange = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003911 res = parent->registerInFlight(halRequest->frame_number,
3912 totalNumBuffers, captureRequest->mResultExtras,
3913 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003914 hasCallback,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003915 /*min*/expectedDurationRange.first,
3916 /*max*/expectedDurationRange.second,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003917 requestedPhysicalCameras, isStillCapture, isZslCapture,
3918 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003919 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003920 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003921 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3922 ", burstId = %" PRId32 ".",
3923 __FUNCTION__,
3924 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3925 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003926
3927 if (shouldUnlockSettings) {
3928 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3929 }
3930
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003931 if (res != OK) {
3932 SET_ERR("RequestThread: Unable to register new in-flight request:"
3933 " %s (%d)", strerror(-res), res);
3934 return INVALID_OPERATION;
3935 }
3936 }
3937
3938 return OK;
3939}
3940
Igor Murashkin1e479c02013-09-06 16:55:14 -07003941CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003942 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003943 Mutex::Autolock al(mLatestRequestMutex);
3944
3945 ALOGV("RequestThread::%s", __FUNCTION__);
3946
3947 return mLatestRequest;
3948}
3949
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003950bool Camera3Device::RequestThread::isStreamPending(
3951 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003952 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003953 Mutex::Autolock l(mRequestLock);
3954
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003955 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003956 if (!nextRequest.submitted) {
3957 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3958 if (stream == s) return true;
3959 }
3960 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003961 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003962 }
3963
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003964 for (const auto& request : mRequestQueue) {
3965 for (const auto& s : request->mOutputStreams) {
3966 if (stream == s) return true;
3967 }
3968 if (stream == request->mInputStream) return true;
3969 }
3970
3971 for (const auto& request : mRepeatingRequests) {
3972 for (const auto& s : request->mOutputStreams) {
3973 if (stream == s) return true;
3974 }
3975 if (stream == request->mInputStream) return true;
3976 }
3977
3978 return false;
3979}
Jianing Weicb0652e2014-03-12 18:29:36 -07003980
Emilian Peev40ead602017-09-26 15:46:36 +01003981bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3982 ATRACE_CALL();
3983 Mutex::Autolock l(mRequestLock);
3984
3985 for (const auto& nextRequest : mNextRequests) {
3986 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3987 if (s.first == streamId) {
3988 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3989 if (it != s.second.end()) {
3990 return true;
3991 }
3992 }
3993 }
3994 }
3995
3996 for (const auto& request : mRequestQueue) {
3997 for (const auto& s : request->mOutputSurfaces) {
3998 if (s.first == streamId) {
3999 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4000 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004001 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004002 }
4003 }
4004 }
4005 }
4006
4007 for (const auto& request : mRepeatingRequests) {
4008 for (const auto& s : request->mOutputSurfaces) {
4009 if (s.first == streamId) {
4010 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4011 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004012 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004013 }
4014 }
4015 }
4016 }
4017
4018 return false;
4019}
4020
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004021void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4022 if (!mUseHalBufManager) {
4023 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4024 return;
4025 }
4026
4027 Mutex::Autolock pl(mPauseLock);
4028 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004029 mInterface->signalPipelineDrain(streamIds);
4030 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004031 }
4032 // If request thread is still busy, wait until paused then notify HAL
4033 mNotifyPipelineDrain = true;
4034 mStreamIdsToBeDrained = streamIds;
4035}
4036
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004037void Camera3Device::RequestThread::resetPipelineDrain() {
4038 Mutex::Autolock pl(mPauseLock);
4039 mNotifyPipelineDrain = false;
4040 mStreamIdsToBeDrained.clear();
4041}
4042
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004043void Camera3Device::RequestThread::clearPreviousRequest() {
4044 Mutex::Autolock l(mRequestLock);
4045 mPrevRequest.clear();
4046}
4047
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004048status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4049 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4050 ATRACE_CALL();
4051 Mutex::Autolock l(mTriggerMutex);
4052 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4053 return BAD_VALUE;
4054 }
4055 mRotateAndCropOverride = rotateAndCropValue;
4056 return OK;
4057}
4058
Emilian Peeve23f1d92021-09-20 14:56:01 -07004059status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4060 ATRACE_CALL();
4061 Mutex::Autolock l(mTriggerMutex);
4062 mComposerOutput = composerSurfacePresent;
4063 return OK;
4064}
4065
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004066status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004067 ATRACE_CALL();
4068 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004069 if (muteMode != mCameraMute) {
4070 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004071 mCameraMuteChanged = true;
4072 }
4073 return OK;
4074}
4075
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004076nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004077 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004078 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004079 return mExpectedInflightDuration > kMinInflightDuration ?
4080 mExpectedInflightDuration : kMinInflightDuration;
4081}
4082
Emilian Peevaebbe412018-01-15 13:53:24 +00004083void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004084 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004085 if ((request == nullptr) || (halRequest == nullptr)) {
4086 ALOGE("%s: Invalid request!", __FUNCTION__);
4087 return;
4088 }
4089
4090 if (halRequest->num_physcam_settings > 0) {
4091 if (halRequest->physcam_id != nullptr) {
4092 delete [] halRequest->physcam_id;
4093 halRequest->physcam_id = nullptr;
4094 }
4095 if (halRequest->physcam_settings != nullptr) {
4096 auto it = ++(request->mSettingsList.begin());
4097 size_t i = 0;
4098 for (; it != request->mSettingsList.end(); it++, i++) {
4099 it->metadata.unlock(halRequest->physcam_settings[i]);
4100 }
4101 delete [] halRequest->physcam_settings;
4102 halRequest->physcam_settings = nullptr;
4103 }
4104 }
4105}
4106
Ravneetaeb20dc2022-03-30 05:33:03 +00004107status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4108 Mutex::Autolock il(mInterfaceLock);
4109 Mutex::Autolock l(mLock);
4110
4111 if (mCameraServiceWatchdog != NULL) {
4112 mCameraServiceWatchdog->setEnabled(enabled);
4113 }
4114
4115 return OK;
4116}
4117
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004118void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4119 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004120 return;
4121 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004122
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004123 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004124 // Skip the ones that have been submitted successfully.
4125 if (nextRequest.submitted) {
4126 continue;
4127 }
4128
4129 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004130 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4131 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004132
4133 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004134 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004135 }
4136
Emilian Peevaebbe412018-01-15 13:53:24 +00004137 cleanupPhysicalSettings(captureRequest, halRequest);
4138
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004139 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004140 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004141 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4142 }
4143
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004144 // No output buffer can be returned when using HAL buffer manager
4145 if (!mUseHalBufManager) {
4146 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4147 //Buffers that failed processing could still have
4148 //valid acquire fence.
4149 int acquireFence = (*outputBuffers)[i].acquire_fence;
4150 if (0 <= acquireFence) {
4151 close(acquireFence);
4152 outputBuffers->editItemAt(i).acquire_fence = -1;
4153 }
Emilian Peevf4816702020-04-03 15:44:51 -07004154 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004155 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4156 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004157 /*timestampIncreasing*/true, std::vector<size_t> (),
4158 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004159 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004160 }
4161
4162 if (sendRequestError) {
4163 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004164 sp<NotificationListener> listener = mListener.promote();
4165 if (listener != NULL) {
4166 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004167 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004168 captureRequest->mResultExtras);
4169 }
4170 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004171
4172 // Remove yet-to-be submitted inflight request from inflightMap
4173 {
4174 sp<Camera3Device> parent = mParent.promote();
4175 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004176 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004177 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4178 if (idx >= 0) {
4179 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4180 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4181 parent->removeInFlightMapEntryLocked(idx);
4182 }
4183 }
4184 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004185 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004186
4187 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004188 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004189}
4190
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004191void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004192 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004193 // Optimized a bit for the simple steady-state case (single repeating
4194 // request), to avoid putting that request in the queue temporarily.
4195 Mutex::Autolock l(mRequestLock);
4196
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004197 assert(mNextRequests.empty());
4198
4199 NextRequest nextRequest;
4200 nextRequest.captureRequest = waitForNextRequestLocked();
4201 if (nextRequest.captureRequest == nullptr) {
4202 return;
4203 }
4204
Emilian Peevf4816702020-04-03 15:44:51 -07004205 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004206 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004207 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004208
4209 // Wait for additional requests
4210 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4211
4212 for (size_t i = 1; i < batchSize; i++) {
4213 NextRequest additionalRequest;
4214 additionalRequest.captureRequest = waitForNextRequestLocked();
4215 if (additionalRequest.captureRequest == nullptr) {
4216 break;
4217 }
4218
Emilian Peevf4816702020-04-03 15:44:51 -07004219 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004220 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004221 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004222 }
4223
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004224 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004225 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004226 mNextRequests.size(), batchSize);
4227 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004228 }
4229
4230 return;
4231}
4232
4233sp<Camera3Device::CaptureRequest>
4234 Camera3Device::RequestThread::waitForNextRequestLocked() {
4235 status_t res;
4236 sp<CaptureRequest> nextRequest;
4237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004238 while (mRequestQueue.empty()) {
4239 if (!mRepeatingRequests.empty()) {
4240 // Always atomically enqueue all requests in a repeating request
4241 // list. Guarantees a complete in-sequence set of captures to
4242 // application.
4243 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004244 if (mFirstRepeating) {
4245 mFirstRepeating = false;
4246 } else {
4247 for (auto& request : requests) {
4248 // For repeating requests, override timestamp request using
4249 // the time a request is inserted into the request queue,
4250 // because the original repeating request will have an old
4251 // fixed timestamp.
4252 request->mRequestTimeNs = systemTime();
4253 }
4254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004255 RequestList::const_iterator firstRequest =
4256 requests.begin();
4257 nextRequest = *firstRequest;
4258 mRequestQueue.insert(mRequestQueue.end(),
4259 ++firstRequest,
4260 requests.end());
4261 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004262
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004263 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004264
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004265 break;
4266 }
4267
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004268 if (!mRequestClearing) {
4269 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4270 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004271
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004272 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4273 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004274 Mutex::Autolock pl(mPauseLock);
4275 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004276 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004277 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004278 if (mNotifyPipelineDrain) {
4279 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4280 mNotifyPipelineDrain = false;
4281 mStreamIdsToBeDrained.clear();
4282 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004283 // Let the tracker know
4284 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4285 if (statusTracker != 0) {
4286 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4287 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004288 sp<Camera3Device> parent = mParent.promote();
4289 if (parent != nullptr) {
4290 parent->mRequestBufferSM.onRequestThreadPaused();
4291 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004292 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004293 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004294 // Stop waiting for now and let thread management happen
4295 return NULL;
4296 }
4297 }
4298
4299 if (nextRequest == NULL) {
4300 // Don't have a repeating request already in hand, so queue
4301 // must have an entry now.
4302 RequestList::iterator firstRequest =
4303 mRequestQueue.begin();
4304 nextRequest = *firstRequest;
4305 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004306 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4307 sp<NotificationListener> listener = mListener.promote();
4308 if (listener != NULL) {
4309 listener->notifyRequestQueueEmpty();
4310 }
4311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004312 }
4313
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004314 // In case we've been unpaused by setPaused clearing mDoPause, need to
4315 // update internal pause state (capture/setRepeatingRequest unpause
4316 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004317 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004318 if (mPaused) {
4319 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4320 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4321 if (statusTracker != 0) {
4322 statusTracker->markComponentActive(mStatusId);
4323 }
4324 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004325 mPaused = false;
4326
4327 // Check if we've reconfigured since last time, and reset the preview
4328 // request if so. Can't use 'NULL request == repeat' across configure calls.
4329 if (mReconfigured) {
4330 mPrevRequest.clear();
4331 mReconfigured = false;
4332 }
4333
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004334 if (nextRequest != NULL) {
4335 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004336 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4337 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004338
4339 // Since RequestThread::clear() removes buffers from the input stream,
4340 // get the right buffer here before unlocking mRequestLock
4341 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004342 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4343 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004344 if (res != OK) {
4345 // Can't get input buffer from gralloc queue - this could be due to
4346 // disconnected queue or other producer misbehavior, so not a fatal
4347 // error
4348 ALOGE("%s: Can't get input buffer, skipping request:"
4349 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004350
4351 sp<NotificationListener> listener = mListener.promote();
4352 if (listener != NULL) {
4353 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004354 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004355 nextRequest->mResultExtras);
4356 }
4357 return NULL;
4358 }
4359 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004360 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004361
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004362 return nextRequest;
4363}
4364
4365bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004366 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004367 status_t res;
4368 Mutex::Autolock l(mPauseLock);
4369 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004370 if (mPaused == false) {
4371 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004372 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004373 if (mNotifyPipelineDrain) {
4374 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4375 mNotifyPipelineDrain = false;
4376 mStreamIdsToBeDrained.clear();
4377 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004378 // Let the tracker know
4379 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4380 if (statusTracker != 0) {
4381 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4382 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004383 sp<Camera3Device> parent = mParent.promote();
4384 if (parent != nullptr) {
4385 parent->mRequestBufferSM.onRequestThreadPaused();
4386 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004387 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004388
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004389 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004390 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004391 return true;
4392 }
4393 }
4394 // We don't set mPaused to false here, because waitForNextRequest needs
4395 // to further manage the paused state in case of starvation.
4396 return false;
4397}
4398
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004399void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004400 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004401 // With work to do, mark thread as unpaused.
4402 // If paused by request (setPaused), don't resume, to avoid
4403 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004404 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004405 Mutex::Autolock p(mPauseLock);
4406 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004407 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4408 if (mPaused) {
4409 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4410 if (statusTracker != 0) {
4411 statusTracker->markComponentActive(mStatusId);
4412 }
4413 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004414 mPaused = false;
4415 }
4416}
4417
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004418void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4419 sp<Camera3Device> parent = mParent.promote();
4420 if (parent != NULL) {
4421 va_list args;
4422 va_start(args, fmt);
4423
4424 parent->setErrorStateV(fmt, args);
4425
4426 va_end(args);
4427 }
4428}
4429
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004430status_t Camera3Device::RequestThread::insertTriggers(
4431 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004432 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004433 Mutex::Autolock al(mTriggerMutex);
4434
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004435 sp<Camera3Device> parent = mParent.promote();
4436 if (parent == NULL) {
4437 CLOGE("RequestThread: Parent is gone");
4438 return DEAD_OBJECT;
4439 }
4440
Emilian Peevaebbe412018-01-15 13:53:24 +00004441 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004442 size_t count = mTriggerMap.size();
4443
4444 for (size_t i = 0; i < count; ++i) {
4445 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004446 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004447
4448 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4449 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4450 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004451 if (isAeTrigger) {
4452 request->mResultExtras.precaptureTriggerId = triggerId;
4453 mCurrentPreCaptureTriggerId = triggerId;
4454 } else {
4455 request->mResultExtras.afTriggerId = triggerId;
4456 mCurrentAfTriggerId = triggerId;
4457 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004458 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004459 }
4460
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004461 camera_metadata_entry entry = metadata.find(tag);
4462
4463 if (entry.count > 0) {
4464 /**
4465 * Already has an entry for this trigger in the request.
4466 * Rewrite it with our requested trigger value.
4467 */
4468 RequestTrigger oldTrigger = trigger;
4469
4470 oldTrigger.entryValue = entry.data.u8[0];
4471
4472 mTriggerReplacedMap.add(tag, oldTrigger);
4473 } else {
4474 /**
4475 * More typical, no trigger entry, so we just add it
4476 */
4477 mTriggerRemovedMap.add(tag, trigger);
4478 }
4479
4480 status_t res;
4481
4482 switch (trigger.getTagType()) {
4483 case TYPE_BYTE: {
4484 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4485 res = metadata.update(tag,
4486 &entryValue,
4487 /*count*/1);
4488 break;
4489 }
4490 case TYPE_INT32:
4491 res = metadata.update(tag,
4492 &trigger.entryValue,
4493 /*count*/1);
4494 break;
4495 default:
4496 ALOGE("%s: Type not supported: 0x%x",
4497 __FUNCTION__,
4498 trigger.getTagType());
4499 return INVALID_OPERATION;
4500 }
4501
4502 if (res != OK) {
4503 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4504 ", value %d", __FUNCTION__, trigger.getTagName(),
4505 trigger.entryValue);
4506 return res;
4507 }
4508
4509 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4510 trigger.getTagName(),
4511 trigger.entryValue);
4512 }
4513
4514 mTriggerMap.clear();
4515
4516 return count;
4517}
4518
4519status_t Camera3Device::RequestThread::removeTriggers(
4520 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004521 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004522 Mutex::Autolock al(mTriggerMutex);
4523
Emilian Peevaebbe412018-01-15 13:53:24 +00004524 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004525
4526 /**
4527 * Replace all old entries with their old values.
4528 */
4529 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4530 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4531
4532 status_t res;
4533
4534 uint32_t tag = trigger.metadataTag;
4535 switch (trigger.getTagType()) {
4536 case TYPE_BYTE: {
4537 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4538 res = metadata.update(tag,
4539 &entryValue,
4540 /*count*/1);
4541 break;
4542 }
4543 case TYPE_INT32:
4544 res = metadata.update(tag,
4545 &trigger.entryValue,
4546 /*count*/1);
4547 break;
4548 default:
4549 ALOGE("%s: Type not supported: 0x%x",
4550 __FUNCTION__,
4551 trigger.getTagType());
4552 return INVALID_OPERATION;
4553 }
4554
4555 if (res != OK) {
4556 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4557 ", trigger value %d", __FUNCTION__,
4558 trigger.getTagName(), trigger.entryValue);
4559 return res;
4560 }
4561 }
4562 mTriggerReplacedMap.clear();
4563
4564 /**
4565 * Remove all new entries.
4566 */
4567 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4568 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4569 status_t res = metadata.erase(trigger.metadataTag);
4570
4571 if (res != OK) {
4572 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4573 ", trigger value %d", __FUNCTION__,
4574 trigger.getTagName(), trigger.entryValue);
4575 return res;
4576 }
4577 }
4578 mTriggerRemovedMap.clear();
4579
4580 return OK;
4581}
4582
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004583status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004584 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004585 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004586 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004587 status_t res;
4588
Emilian Peevaebbe412018-01-15 13:53:24 +00004589 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004590
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004591 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004592 // exists
4593 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4594 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4595 if (afTrigger.count > 0 &&
4596 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4597 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004598 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004599 if (res != OK) return res;
4600 }
4601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004602 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004603 // if none already exists
4604 camera_metadata_entry pcTrigger =
4605 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4606 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4607 if (pcTrigger.count > 0 &&
4608 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4609 pcId.count == 0) {
4610 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004611 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004612 if (res != OK) return res;
4613 }
4614
4615 return OK;
4616}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004617
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004618bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4619 const sp<CaptureRequest> &request) {
4620 ATRACE_CALL();
4621
4622 if (request->mRotateAndCropAuto) {
4623 Mutex::Autolock l(mTriggerMutex);
4624 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4625
4626 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4627 if (rotateAndCropEntry.count > 0) {
4628 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4629 return false;
4630 } else {
4631 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4632 return true;
4633 }
4634 } else {
4635 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4636 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4637 &rotateAndCrop_u8, 1);
4638 return true;
4639 }
4640 }
4641 return false;
4642}
4643
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004644bool Camera3Device::RequestThread::overrideTestPattern(
4645 const sp<CaptureRequest> &request) {
4646 ATRACE_CALL();
4647
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004648 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004649
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004650 Mutex::Autolock l(mTriggerMutex);
4651
4652 bool changed = false;
4653
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004654 // For a multi-camera, the physical cameras support the same set of
4655 // test pattern modes as the logical camera.
4656 for (auto& settings : request->mSettingsList) {
4657 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004658
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004659 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4660 int32_t testPatternData[4] = {
4661 settings.mOriginalTestPatternData[0],
4662 settings.mOriginalTestPatternData[1],
4663 settings.mOriginalTestPatternData[2],
4664 settings.mOriginalTestPatternData[3]
4665 };
4666 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4667 testPatternMode = mCameraMute;
4668 testPatternData[0] = 0;
4669 testPatternData[1] = 0;
4670 testPatternData[2] = 0;
4671 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004672 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004673
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004674 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4675 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4676 if (testPatternEntry.count > 0) {
4677 if (testPatternEntry.data.i32[0] != testPatternMode) {
4678 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004679 changed = true;
4680 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004681 } else if (supportTestPatternModeKey) {
4682 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4683 &testPatternMode, 1);
4684 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004685 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004686
4687 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4688 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4689 if (testPatternColor.count >= 4) {
4690 for (size_t i = 0; i < 4; i++) {
4691 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4692 testPatternColor.data.i32[i] = testPatternData[i];
4693 changed = true;
4694 }
4695 }
4696 } else if (supportTestPatternDataKey) {
4697 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4698 testPatternData, 4);
4699 changed = true;
4700 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004701 }
4702
4703 return changed;
4704}
4705
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004706status_t Camera3Device::RequestThread::setHalInterface(
4707 sp<HalInterface> newHalInterface) {
4708 if (newHalInterface.get() == nullptr) {
4709 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4710 return DEAD_OBJECT;
4711 }
4712
4713 mInterface = newHalInterface;
4714
4715 return OK;
4716}
4717
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004718/**
4719 * PreparerThread inner class methods
4720 */
4721
4722Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004723 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004724 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004725}
4726
4727Camera3Device::PreparerThread::~PreparerThread() {
4728 Thread::requestExitAndWait();
4729 if (mCurrentStream != nullptr) {
4730 mCurrentStream->cancelPrepare();
4731 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4732 mCurrentStream.clear();
4733 }
4734 clear();
4735}
4736
Ruben Brunkc78ac262015-08-13 17:58:46 -07004737status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004738 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004739 status_t res;
4740
4741 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004742 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004743
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004744 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004745 if (res == OK) {
4746 // No preparation needed, fire listener right off
4747 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004748 if (listener != NULL) {
4749 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004750 }
4751 return OK;
4752 } else if (res != NOT_ENOUGH_DATA) {
4753 return res;
4754 }
4755
4756 // Need to prepare, start up thread if necessary
4757 if (!mActive) {
4758 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4759 // isn't running
4760 Thread::requestExitAndWait();
4761 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4762 if (res != OK) {
4763 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004764 if (listener != NULL) {
4765 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004766 }
4767 return res;
4768 }
4769 mCancelNow = false;
4770 mActive = true;
4771 ALOGV("%s: Preparer stream started", __FUNCTION__);
4772 }
4773
4774 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004775 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004776 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4777
4778 return OK;
4779}
4780
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004781void Camera3Device::PreparerThread::pause() {
4782 ATRACE_CALL();
4783
4784 Mutex::Autolock l(mLock);
4785
4786 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4787 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4788 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4789 int currentMaxCount = mCurrentMaxCount;
4790 mPendingStreams.clear();
4791 mCancelNow = true;
4792 while (mActive) {
4793 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4794 if (res == TIMED_OUT) {
4795 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4796 return;
4797 } else if (res != OK) {
4798 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4799 return;
4800 }
4801 }
4802
4803 //Check whether the prepare thread was able to complete the current
4804 //stream. In case work is still pending emplace it along with the rest
4805 //of the streams in the pending list.
4806 if (currentStream != nullptr) {
4807 if (!mCurrentPrepareComplete) {
4808 pendingStreams.emplace(currentMaxCount, currentStream);
4809 }
4810 }
4811
4812 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4813 for (const auto& it : mPendingStreams) {
4814 it.second->cancelPrepare();
4815 }
4816}
4817
4818status_t Camera3Device::PreparerThread::resume() {
4819 ATRACE_CALL();
4820 status_t res;
4821
4822 Mutex::Autolock l(mLock);
4823 sp<NotificationListener> listener = mListener.promote();
4824
4825 if (mActive) {
4826 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4827 return NO_INIT;
4828 }
4829
4830 auto it = mPendingStreams.begin();
4831 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004832 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004833 if (res == OK) {
4834 if (listener != NULL) {
4835 listener->notifyPrepared(it->second->getId());
4836 }
4837 it = mPendingStreams.erase(it);
4838 } else if (res != NOT_ENOUGH_DATA) {
4839 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4840 res, strerror(-res));
4841 it = mPendingStreams.erase(it);
4842 } else {
4843 it++;
4844 }
4845 }
4846
4847 if (mPendingStreams.empty()) {
4848 return OK;
4849 }
4850
4851 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4852 if (res != OK) {
4853 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4854 __FUNCTION__, res, strerror(-res));
4855 return res;
4856 }
4857 mCancelNow = false;
4858 mActive = true;
4859 ALOGV("%s: Preparer stream started", __FUNCTION__);
4860
4861 return OK;
4862}
4863
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004864status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004865 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004866 Mutex::Autolock l(mLock);
4867
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004868 for (const auto& it : mPendingStreams) {
4869 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004870 }
4871 mPendingStreams.clear();
4872 mCancelNow = true;
4873
4874 return OK;
4875}
4876
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004877void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004878 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004879 Mutex::Autolock l(mLock);
4880 mListener = listener;
4881}
4882
4883bool Camera3Device::PreparerThread::threadLoop() {
4884 status_t res;
4885 {
4886 Mutex::Autolock l(mLock);
4887 if (mCurrentStream == nullptr) {
4888 // End thread if done with work
4889 if (mPendingStreams.empty()) {
4890 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4891 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4892 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4893 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004894 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004895 return false;
4896 }
4897
4898 // Get next stream to prepare
4899 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004900 mCurrentStream = it->second;
4901 mCurrentMaxCount = it->first;
4902 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004903 mPendingStreams.erase(it);
4904 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4905 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4906 } else if (mCancelNow) {
4907 mCurrentStream->cancelPrepare();
4908 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4909 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4910 mCurrentStream.clear();
4911 mCancelNow = false;
4912 return true;
4913 }
4914 }
4915
4916 res = mCurrentStream->prepareNextBuffer();
4917 if (res == NOT_ENOUGH_DATA) return true;
4918 if (res != OK) {
4919 // Something bad happened; try to recover by cancelling prepare and
4920 // signalling listener anyway
4921 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4922 mCurrentStream->getId(), res, strerror(-res));
4923 mCurrentStream->cancelPrepare();
4924 }
4925
4926 // This stream has finished, notify listener
4927 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004928 sp<NotificationListener> listener = mListener.promote();
4929 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004930 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4931 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004932 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004933 }
4934
4935 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4936 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004937 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004938
4939 return true;
4940}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004941
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004942status_t Camera3Device::RequestBufferStateMachine::initialize(
4943 sp<camera3::StatusTracker> statusTracker) {
4944 if (statusTracker == nullptr) {
4945 ALOGE("%s: statusTracker is null", __FUNCTION__);
4946 return BAD_VALUE;
4947 }
4948
4949 std::lock_guard<std::mutex> lock(mLock);
4950 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004951 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004952 return OK;
4953}
4954
4955bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4956 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004957 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004958 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004959 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004960 return true;
4961 }
4962 return false;
4963}
4964
4965void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4966 std::lock_guard<std::mutex> lock(mLock);
4967 if (!mRequestBufferOngoing) {
4968 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4969 return;
4970 }
4971 mRequestBufferOngoing = false;
4972 if (mStatus == RB_STATUS_PENDING_STOP) {
4973 checkSwitchToStopLocked();
4974 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004975 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004976}
4977
4978void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4979 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004980 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004981 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004982 return;
4983}
4984
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004985void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004986 std::lock_guard<std::mutex> lock(mLock);
4987 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004988 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4989 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004990 mInflightMapEmpty = false;
4991 if (mStatus == RB_STATUS_STOPPED) {
4992 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004993 }
4994 return;
4995}
4996
4997void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4998 std::lock_guard<std::mutex> lock(mLock);
4999 mRequestThreadPaused = true;
5000 if (mStatus == RB_STATUS_PENDING_STOP) {
5001 checkSwitchToStopLocked();
5002 }
5003 return;
5004}
5005
5006void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5007 std::lock_guard<std::mutex> lock(mLock);
5008 mInflightMapEmpty = true;
5009 if (mStatus == RB_STATUS_PENDING_STOP) {
5010 checkSwitchToStopLocked();
5011 }
5012 return;
5013}
5014
5015void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5016 std::lock_guard<std::mutex> lock(mLock);
5017 if (!checkSwitchToStopLocked()) {
5018 mStatus = RB_STATUS_PENDING_STOP;
5019 }
5020 return;
5021}
5022
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005023bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5024 std::lock_guard<std::mutex> lock(mLock);
5025 if (mRequestBufferOngoing) {
5026 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5027 __FUNCTION__);
5028 return false;
5029 }
5030 mSwitchedToOffline = true;
5031 mInflightMapEmpty = true;
5032 mRequestThreadPaused = true;
5033 mStatus = RB_STATUS_STOPPED;
5034 return true;
5035}
5036
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005037void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5038 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5039 if (statusTracker != nullptr) {
5040 if (active) {
5041 statusTracker->markComponentActive(mRequestBufferStatusId);
5042 } else {
5043 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5044 }
5045 }
5046}
5047
5048bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5049 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5050 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005051 return true;
5052 }
5053 return false;
5054}
5055
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005056bool Camera3Device::startRequestBuffer() {
5057 return mRequestBufferSM.startRequestBuffer();
5058}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005059
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005060void Camera3Device::endRequestBuffer() {
5061 mRequestBufferSM.endRequestBuffer();
5062}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005063
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005064nsecs_t Camera3Device::getWaitDuration() {
5065 return kBaseGetBufferWait + getExpectedInFlightDuration();
5066}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005068void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5069 mInterface->getInflightBufferKeys(out);
5070}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005071
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005072void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5073 mInterface->getInflightRequestBufferKeys(out);
5074}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005075
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005076std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5077 std::vector<sp<Camera3StreamInterface>> ret;
5078 bool hasInputStream = mInputStream != nullptr;
5079 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5080 if (hasInputStream) {
5081 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005082 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005083 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5084 ret.push_back(mOutputStreams[i]);
5085 }
5086 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5087 ret.push_back(mDeletedStreams[i]);
5088 }
5089 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005090}
5091
Emilian Peevcc0b7952020-01-07 13:54:47 -08005092void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5093 ATRACE_CALL();
5094
5095 if (offlineStreamIds == nullptr) {
5096 return;
5097 }
5098
5099 Mutex::Autolock il(mInterfaceLock);
5100
5101 auto streamIds = mOutputStreams.getStreamIds();
5102 bool hasInputStream = mInputStream != nullptr;
5103 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5104 offlineStreamIds->push_back(mInputStream->getId());
5105 }
5106
5107 for (const auto & streamId : streamIds) {
5108 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5109 // Streams that use the camera buffer manager are currently not supported in
5110 // offline mode
5111 if (stream->getOfflineProcessingSupport() &&
5112 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5113 offlineStreamIds->push_back(streamId);
5114 }
5115 }
5116}
5117
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005118status_t Camera3Device::setRotateAndCropAutoBehavior(
5119 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5120 ATRACE_CALL();
5121 Mutex::Autolock il(mInterfaceLock);
5122 Mutex::Autolock l(mLock);
5123 if (mRequestThread == nullptr) {
5124 return INVALID_OPERATION;
5125 }
5126 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5127}
5128
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005129bool Camera3Device::supportsCameraMute() {
5130 Mutex::Autolock il(mInterfaceLock);
5131 Mutex::Autolock l(mLock);
5132
5133 return mSupportCameraMute;
5134}
5135
5136status_t Camera3Device::setCameraMute(bool enabled) {
5137 ATRACE_CALL();
5138 Mutex::Autolock il(mInterfaceLock);
5139 Mutex::Autolock l(mLock);
5140
5141 if (mRequestThread == nullptr || !mSupportCameraMute) {
5142 return INVALID_OPERATION;
5143 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005144 int32_t muteMode =
5145 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5146 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5147 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5148 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005149}
5150
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005151status_t Camera3Device::injectCamera(const String8& injectedCamId,
5152 sp<CameraProviderManager> manager) {
5153 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5154 ATRACE_CALL();
5155 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005156 // When the camera device is active, injectCamera() and stopInjection() will call
5157 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5158 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5159 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5160 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5161 // waitUntilStateThenRelock().
5162 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005163
5164 status_t res = NO_ERROR;
5165 if (mInjectionMethods->isInjecting()) {
5166 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5167 return OK;
5168 } else {
5169 res = mInjectionMethods->stopInjection();
5170 if (res != OK) {
5171 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5172 __FUNCTION__, res);
5173 return res;
5174 }
5175 }
5176 }
5177
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005178 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005179 if (res != OK) {
5180 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5181 __FUNCTION__, res);
5182 return res;
5183 }
5184
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005185 // When the second display of android is cast to the remote device, and the opened camera is
5186 // also cast to the second display, in this case, because the camera has configured the streams
5187 // at this time, we can directly call injectCamera() to replace the internal camera with
5188 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005189 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5190 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5191
5192 camera3::camera_stream_configuration injectionConfig;
5193 std::vector<uint32_t> injectionBufferSizes;
5194 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5195 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5196 || injectionBufferSizes.size() <= 0) {
5197 ALOGE("Failed to inject camera due to abandoned configuration! "
5198 "mOperatingMode: %d injectionConfig.num_streams: %d "
5199 "injectionBufferSizes.size(): %zu", mOperatingMode,
5200 injectionConfig.num_streams, injectionBufferSizes.size());
5201 return DEAD_OBJECT;
5202 }
5203
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005204 res = mInjectionMethods->injectCamera(
5205 injectionConfig, injectionBufferSizes);
5206 if (res != OK) {
5207 ALOGE("Can't finish inject camera process!");
5208 return res;
5209 }
5210 }
5211
5212 return OK;
5213}
5214
5215status_t Camera3Device::stopInjection() {
5216 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5217 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005218 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005219 return mInjectionMethods->stopInjection();
5220}
5221
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005222}; // namespace android