blob: 8eb7fd0ebd249e05f63dec5f03f1447b454383b1 [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"
Shuzhen Wang316781a2020-08-18 18:11:01 -070066#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080068#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080069#include <tuple>
70
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080072using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073
74namespace android {
75
Emilian Peev5104fe92021-10-21 14:27:09 -070076Camera3Device::Camera3Device(const String8 &id, bool overrideForPerfClass, bool legacyClient):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080077 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070078 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080079 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070080 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070081 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070082 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070083 mUsePartialResult(false),
84 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080085 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080086 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070087 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070088 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070089 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070090 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070091 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070092 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000093 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010094 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070095 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070096 mNeedFixupMonochromeTags(false),
97 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800100 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101}
102
103Camera3Device::~Camera3Device()
104{
105 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800106 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700107 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108}
109
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800110const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800111 return mId;
112}
113
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800114status_t Camera3Device::initializeCommonLocked() {
115
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700116 /** Start up status tracker thread */
117 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800118 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700119 if (res != OK) {
120 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
121 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800122 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700123 mStatusTracker.clear();
124 return res;
125 }
126
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700127 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700128 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700129
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700130 if (mUseHalBufManager) {
131 res = mRequestBufferSM.initialize(mStatusTracker);
132 if (res != OK) {
133 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
134 strerror(-res), res);
135 mInterface->close();
136 mStatusTracker.clear();
137 return res;
138 }
139 }
140
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800141 /** Create buffer manager */
142 mBufferManager = new Camera3BufferManager();
143
144 Vector<int32_t> sessionParamKeys;
145 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
146 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
147 if (sessionKeysEntry.count > 0) {
148 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
149 }
150
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700151 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
152 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
153 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
154 if (availableTestPatternModes.data.i32[i] ==
155 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
156 mSupportCameraMute = true;
157 mSupportTestPatternSolidColor = true;
158 break;
159 } else if (availableTestPatternModes.data.i32[i] ==
160 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
161 mSupportCameraMute = true;
162 mSupportTestPatternSolidColor = false;
163 }
164 }
165
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700166 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800167 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700168 this, mStatusTracker, mInterface, sessionParamKeys,
169 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800170 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700172 SET_ERR_L("Unable to start request queue thread: %s (%d)",
173 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800174 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800175 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 return res;
177 }
178
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700179 mPreparerThread = new PreparerThread();
180
Ruben Brunk183f0562015-08-12 12:55:02 -0700181 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400183 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700184 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800186 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800188 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800189 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800190 camera_metadata_entry timestampSource =
191 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
192 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
193 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800194 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800195 }
196
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700197 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100198 camera_metadata_entry partialResultsCount =
199 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
200 if (partialResultsCount.count > 0) {
201 mNumPartialResults = partialResultsCount.data.i32[0];
202 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 }
204
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800205 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
206 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700207 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700208 if (res != OK) {
209 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
210 return res;
211 }
212 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800213
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800214 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800215 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800216
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700217 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
218 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
219 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
220 }
221
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800222 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
223 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
224 }
225
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800226 // Hidl/AidlCamera3DeviceInjectionMethods
227 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800228
Ravneet74cd3732022-03-30 05:33:03 +0000229 /** Start watchdog thread */
230 mCameraServiceWatchdog = new CameraServiceWatchdog();
231 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
232 if (res != OK) {
233 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
234 strerror(-res), res);
235 return res;
236 }
237
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238 return OK;
239}
240
241status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700242 return disconnectImpl();
243}
244
245status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800246 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700247 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800248
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700249 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700250 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800252 Mutex::Autolock il(mInterfaceLock);
253 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
254 {
255 Mutex::Autolock l(mLock);
256 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800258 if (mStatus == STATUS_ACTIVE ||
259 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
260 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800262 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800264 } else {
265 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
266 if (res != OK) {
267 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
268 maxExpectedDuration);
269 // Continue to close device even in case of error
270 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700272 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800273
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800274 if (mStatus == STATUS_ERROR) {
275 CLOGE("Shutting down in an error state");
276 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800278 if (mStatusTracker != NULL) {
279 mStatusTracker->requestExit();
280 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800282 if (mRequestThread != NULL) {
283 mRequestThread->requestExit();
284 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800286 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
287 for (size_t i = 0; i < mOutputStreams.size(); i++) {
288 streams.push_back(mOutputStreams[i]);
289 }
290 if (mInputStream != nullptr) {
291 streams.push_back(mInputStream);
292 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700293 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800295 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
296 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700297 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
298 // HAL may be in a bad state, so waiting for request thread
299 // (which may be stuck in the HAL processCaptureRequest call)
300 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800301 // give up mInterfaceLock here and then lock it again. Could this lead
302 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 mRequestThread->join();
304 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700305 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800306 Mutex::Autolock il(mInterfaceLock);
307 if (mStatusTracker != NULL) {
308 mStatusTracker->join();
309 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800311 if (mInjectionMethods->isInjecting()) {
312 mInjectionMethods->stopInjection();
313 }
314
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800315 HalInterface* interface;
316 {
317 Mutex::Autolock l(mLock);
318 mRequestThread.clear();
319 Mutex::Autolock stLock(mTrackerLock);
320 mStatusTracker.clear();
321 interface = mInterface.get();
322 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800324 // Call close without internal mutex held, as the HAL close may need to
325 // wait on assorted callbacks,etc, to complete before it can return.
Ravneet98ffa752022-03-02 07:22:46 +0000326 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700327
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800328 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800330 {
331 Mutex::Autolock l(mLock);
332 mInterface->clear();
333 mOutputStreams.clear();
334 mInputStream.clear();
335 mDeletedStreams.clear();
336 mBufferManager.clear();
337 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
338 }
339
340 for (auto& weakStream : streams) {
341 sp<Camera3StreamInterface> stream = weakStream.promote();
342 if (stream != nullptr) {
343 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
344 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
345 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700346 }
347 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700348 ALOGI("%s: X", __FUNCTION__);
Ravneet98ffa752022-03-02 07:22:46 +0000349
350 if (mCameraServiceWatchdog != NULL) {
351 mCameraServiceWatchdog->requestExit();
352 mCameraServiceWatchdog.clear();
353 }
354
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700355 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800356}
357
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700358// For dumping/debugging only -
359// try to acquire a lock a few times, eventually give up to proceed with
360// debug/dump operations
361bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
362 bool gotLock = false;
363 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
364 if (lock.tryLock() == NO_ERROR) {
365 gotLock = true;
366 break;
367 } else {
368 usleep(kDumpSleepDuration);
369 }
370 }
371 return gotLock;
372}
373
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800374nsecs_t Camera3Device::getMonoToBoottimeOffset() {
375 // try three times to get the clock offset, choose the one
376 // with the minimum gap in measurements.
377 const int tries = 3;
378 nsecs_t bestGap, measured;
379 for (int i = 0; i < tries; ++i) {
380 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
381 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
382 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
383 const nsecs_t gap = tmono2 - tmono;
384 if (i == 0 || gap < bestGap) {
385 bestGap = gap;
386 measured = tbase - ((tmono + tmono2) >> 1);
387 }
388 }
389 return measured;
390}
391
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800392ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
393 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700394 // Get max jpeg size (area-wise) for default sensor pixel mode
395 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800396 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700397 /*isUltraHighResolutionSensor*/false);
398 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
399 // not ultra high res sensor
400 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800401 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700402 /*isUltraHighResolution*/true);
403 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800404 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
405 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700406 return BAD_VALUE;
407 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700408 bool useMaxSensorPixelModeThreshold = false;
409 if (uhrMaxJpegResolution.width != 0 &&
410 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
411 // Use the ultra high res max jpeg size and max jpeg buffer size
412 useMaxSensorPixelModeThreshold = true;
413 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700414
Zhijun Hef7da0962014-04-24 13:27:56 -0700415 // Get max jpeg buffer size
416 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800417 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700418 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800419 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
420 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700421 return BAD_VALUE;
422 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700423 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700424
425 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
426 if (useMaxSensorPixelModeThreshold) {
427 maxJpegBufferSize =
428 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
429 maxDefaultJpegResolution, maxJpegBufferSize);
430 chosenMaxJpegResolution = uhrMaxJpegResolution;
431 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800432 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700433
434 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700435 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700436 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800437 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
438 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700439 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800440 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
441 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700442 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700443 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 return jpegBufferSize;
445}
446
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800447ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700448 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800449 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700450 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800451 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
452 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700453 return BAD_VALUE;
454 }
455 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
456 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
457 return maxBytesForPointCloud;
458}
459
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800460ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
461 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800462 const int PER_CONFIGURATION_SIZE = 3;
463 const int WIDTH_OFFSET = 0;
464 const int HEIGHT_OFFSET = 1;
465 const int SIZE_OFFSET = 2;
466 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800467 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800468 camera3::SessionConfigurationUtils::getAppropriateModeTag(
469 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
470 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800471 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800472 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800473 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
474 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800475 return BAD_VALUE;
476 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700477
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800478 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
479 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
480 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
481 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
482 }
483 }
484
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800485 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
486 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800487 return BAD_VALUE;
488}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700489
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800490status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
491 ATRACE_CALL();
492 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700493
494 // Try to lock, but continue in case of failure (to avoid blocking in
495 // deadlocks)
496 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
497 bool gotLock = tryLockSpinRightRound(mLock);
498
499 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800500 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
501 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700502 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800503 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
504 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700505
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800506 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700507
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800508 String16 templatesOption("-t");
509 int n = args.size();
510 for (int i = 0; i < n; i++) {
511 if (args[i] == templatesOption) {
512 dumpTemplates = true;
513 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000514 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700515 if (i + 1 < n) {
516 String8 monitorTags = String8(args[i + 1]);
517 if (monitorTags == "off") {
518 mTagMonitor.disableMonitoring();
519 } else {
520 mTagMonitor.parseTagsToMonitor(monitorTags);
521 }
522 } else {
523 mTagMonitor.disableMonitoring();
524 }
525 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800526 }
527
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800528 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800530 const char *status =
531 mStatus == STATUS_ERROR ? "ERROR" :
532 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700533 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
534 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800535 mStatus == STATUS_ACTIVE ? "ACTIVE" :
536 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700537
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700539 if (mStatus == STATUS_ERROR) {
540 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
541 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800542 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800543 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000544 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
545 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
546 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800547 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548
549 if (mInputStream != NULL) {
550 write(fd, lines.string(), lines.size());
551 mInputStream->dump(fd, args);
552 } else {
553 lines.appendFormat(" No input stream.\n");
554 write(fd, lines.string(), lines.size());
555 }
556 for (size_t i = 0; i < mOutputStreams.size(); i++) {
557 mOutputStreams[i]->dump(fd,args);
558 }
559
Zhijun He431503c2016-03-07 17:30:16 -0800560 if (mBufferManager != NULL) {
561 lines = String8(" Camera3 Buffer Manager:\n");
562 write(fd, lines.string(), lines.size());
563 mBufferManager->dump(fd, args);
564 }
Zhijun He125684a2015-12-26 15:07:30 -0800565
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700566 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700567 if (mInFlightLock.try_lock()) {
568 if (mInFlightMap.size() == 0) {
569 lines.append(" None\n");
570 } else {
571 for (size_t i = 0; i < mInFlightMap.size(); i++) {
572 InFlightRequest r = mInFlightMap.valueAt(i);
573 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
574 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
575 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
576 r.numBuffersLeft);
577 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700578 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700579 mInFlightLock.unlock();
580 } else {
581 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700582 }
583 write(fd, lines.string(), lines.size());
584
Shuzhen Wang686f6442017-06-20 16:16:04 -0700585 if (mRequestThread != NULL) {
586 mRequestThread->dumpCaptureRequestLatency(fd,
587 " ProcessCaptureRequest latency histogram:");
588 }
589
Igor Murashkin1e479c02013-09-06 16:55:14 -0700590 {
591 lines = String8(" Last request sent:\n");
592 write(fd, lines.string(), lines.size());
593
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700595 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
596 }
597
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800598 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700599 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800600 "TEMPLATE_PREVIEW",
601 "TEMPLATE_STILL_CAPTURE",
602 "TEMPLATE_VIDEO_RECORD",
603 "TEMPLATE_VIDEO_SNAPSHOT",
604 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800605 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800606 };
607
Emilian Peevf4816702020-04-03 15:44:51 -0700608 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 camera_metadata_t *templateRequest = nullptr;
610 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700611 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800612 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800613 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800614 lines.append(" Not supported\n");
615 write(fd, lines.string(), lines.size());
616 } else {
617 write(fd, lines.string(), lines.size());
618 dump_indented_camera_metadata(templateRequest,
619 fd, /*verbosity*/2, /*indentation*/8);
620 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800621 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800622 }
623 }
624
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700625 mTagMonitor.dumpMonitoredMetadata(fd);
626
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800627 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800628 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800630 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800631 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800632
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700633 if (gotLock) mLock.unlock();
634 if (gotInterfaceLock) mInterfaceLock.unlock();
635
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800636 return OK;
637}
638
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700639status_t Camera3Device::startWatchingTags(const String8 &tags) {
640 mTagMonitor.parseTagsToMonitor(tags);
641 return OK;
642}
643
644status_t Camera3Device::stopWatchingTags() {
645 mTagMonitor.disableMonitoring();
646 return OK;
647}
648
649status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
650 mTagMonitor.getLatestMonitoredTagEvents(out);
651 return OK;
652}
653
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800654const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800656 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
657 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700658 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 mStatus == STATUS_ERROR ?
660 "when in error state" : "before init");
661 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700662 if (physicalId.isEmpty()) {
663 return mDeviceInfo;
664 } else {
665 std::string id(physicalId.c_str());
666 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
667 return mPhysicalDeviceInfoMap.at(id);
668 } else {
669 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
670 return mDeviceInfo;
671 }
672 }
673}
674
675const CameraMetadata& Camera3Device::info() const {
676 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800677 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800678}
679
Jianing Wei90e59c92014-03-12 18:29:36 -0700680status_t Camera3Device::checkStatusOkToCaptureLocked() {
681 switch (mStatus) {
682 case STATUS_ERROR:
683 CLOGE("Device has encountered a serious error");
684 return INVALID_OPERATION;
685 case STATUS_UNINITIALIZED:
686 CLOGE("Device not initialized");
687 return INVALID_OPERATION;
688 case STATUS_UNCONFIGURED:
689 case STATUS_CONFIGURED:
690 case STATUS_ACTIVE:
691 // OK
692 break;
693 default:
694 SET_ERR_L("Unexpected status: %d", mStatus);
695 return INVALID_OPERATION;
696 }
697 return OK;
698}
699
700status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000701 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700702 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700703 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700704 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700705 if (requestList == NULL) {
706 CLOGE("requestList cannot be NULL.");
707 return BAD_VALUE;
708 }
709
Jianing Weicb0652e2014-03-12 18:29:36 -0700710 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000711 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700712 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
713 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
714 ++metadataIt, ++surfaceMapIt) {
715 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700716 if (newRequest == 0) {
717 CLOGE("Can't create capture request");
718 return BAD_VALUE;
719 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700720
Shuzhen Wang9d066012016-09-30 11:30:20 -0700721 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700722 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700723
Jianing Weicb0652e2014-03-12 18:29:36 -0700724 // Setup burst Id and request Id
725 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800726 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
727 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700728 CLOGE("RequestID does not exist in metadata");
729 return BAD_VALUE;
730 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800731 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700732
Jianing Wei90e59c92014-03-12 18:29:36 -0700733 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700734
735 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700736 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700737 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
738 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
739 return BAD_VALUE;
740 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700741
742 // Setup batch size if this is a high speed video recording request.
743 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
744 auto firstRequest = requestList->begin();
745 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
746 if (outputStream->isVideoStream()) {
747 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800748 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700749 break;
750 }
751 }
752 }
753
Jianing Wei90e59c92014-03-12 18:29:36 -0700754 return OK;
755}
756
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800757status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800758 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759
Emilian Peevaebbe412018-01-15 13:53:24 +0000760 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700761 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000762 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700763
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800764 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765}
766
Emilian Peevaebbe412018-01-15 13:53:24 +0000767void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700768 std::list<const SurfaceMap>& surfaceMaps,
769 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000770 PhysicalCameraSettingsList requestList;
771 requestList.push_back({std::string(getId().string()), request});
772 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773
774 SurfaceMap surfaceMap;
775 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
776 // With no surface list passed in, stream and surface will have 1-to-1
777 // mapping. So the surface index is 0 for each stream in the surfaceMap.
778 for (size_t i = 0; i < streams.count; i++) {
779 surfaceMap[streams.data.i32[i]].push_back(0);
780 }
781 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800782}
783
Jianing Wei90e59c92014-03-12 18:29:36 -0700784status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000785 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700786 const std::list<const SurfaceMap> &surfaceMaps,
787 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700788 /*out*/
789 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700790 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700791 nsecs_t requestTimeNs = systemTime();
792
Jianing Wei90e59c92014-03-12 18:29:36 -0700793 Mutex::Autolock il(mInterfaceLock);
794 Mutex::Autolock l(mLock);
795
796 status_t res = checkStatusOkToCaptureLocked();
797 if (res != OK) {
798 // error logged by previous call
799 return res;
800 }
801
802 RequestList requestList;
803
Shuzhen Wang0129d522016-10-30 22:43:41 -0700804 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700805 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700806 if (res != OK) {
807 // error logged by previous call
808 return res;
809 }
810
811 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700812 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700813 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700814 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700815 }
816
817 if (res == OK) {
818 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
819 if (res != OK) {
820 SET_ERR_L("Can't transition to active in %f seconds!",
821 kActiveTimeout/1e9);
822 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800823 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700824 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700825 } else {
826 CLOGE("Cannot queue request. Impossible.");
827 return BAD_VALUE;
828 }
829
830 return res;
831}
832
Emilian Peevaebbe412018-01-15 13:53:24 +0000833status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700834 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700835 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700836 ATRACE_CALL();
837
Emilian Peevaebbe412018-01-15 13:53:24 +0000838 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700839}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Jianing Weicb0652e2014-03-12 18:29:36 -0700841status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
842 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800843 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844
Emilian Peevaebbe412018-01-15 13:53:24 +0000845 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700846 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000847 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700848
Emilian Peevaebbe412018-01-15 13:53:24 +0000849 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700850 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851}
852
Emilian Peevaebbe412018-01-15 13:53:24 +0000853status_t Camera3Device::setStreamingRequestList(
854 const List<const PhysicalCameraSettingsList> &requestsList,
855 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700856 ATRACE_CALL();
857
Emilian Peevaebbe412018-01-15 13:53:24 +0000858 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700859}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860
861sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000862 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800863 status_t res;
864
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700865 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800866 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
867 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000868 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700869 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700870 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700872 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700874 } else if (mStatus == STATUS_UNCONFIGURED) {
875 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 CLOGE("No streams configured");
877 return NULL;
878 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 }
880
Shuzhen Wang0129d522016-10-30 22:43:41 -0700881 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800883}
884
Jianing Weicb0652e2014-03-12 18:29:36 -0700885status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700887 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800889
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 switch (mStatus) {
891 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700892 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 return INVALID_OPERATION;
894 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700895 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700897 case STATUS_UNCONFIGURED:
898 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 case STATUS_ACTIVE:
900 // OK
901 break;
902 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700903 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 return INVALID_OPERATION;
905 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800906 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700907
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700908 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800909}
910
911status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
912 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700913 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800914
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700915 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800916}
917
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700918status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800919 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700920 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700922 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700923 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800924 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
925 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700926
927 status_t res;
928 bool wasActive = false;
929
930 switch (mStatus) {
931 case STATUS_ERROR:
932 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
933 return INVALID_OPERATION;
934 case STATUS_UNINITIALIZED:
935 ALOGE("%s: Device not initialized", __FUNCTION__);
936 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700937 case STATUS_UNCONFIGURED:
938 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700939 // OK
940 break;
941 case STATUS_ACTIVE:
942 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700943 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700944 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700946 return res;
947 }
948 wasActive = true;
949 break;
950 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700951 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700952 return INVALID_OPERATION;
953 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700955
956 if (mInputStream != 0) {
957 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
958 return INVALID_OPERATION;
959 }
960
961 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
962 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700964
965 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800966 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700967
968 *id = mNextStreamId++;
969
970 // Continue captures if active at start
971 if (wasActive) {
972 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100973 // Reuse current operating mode and session parameters for new stream config
974 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700975 if (res != OK) {
976 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
977 __FUNCTION__, mNextStreamId, strerror(-res), res);
978 return res;
979 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700980 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700981 }
982
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800983 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700984 return OK;
985}
986
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700987status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700988 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700989 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800990 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800991 const std::unordered_set<int32_t> &sensorPixelModesUsed,
992 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -0800993 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -0800994 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -0700995 ATRACE_CALL();
996
997 if (consumer == nullptr) {
998 ALOGE("%s: consumer must not be null", __FUNCTION__);
999 return BAD_VALUE;
1000 }
1001
1002 std::vector<sp<Surface>> consumers;
1003 consumers.push_back(consumer);
1004
1005 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001006 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001007 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001008 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001009}
1010
1011static bool isRawFormat(int format) {
1012 switch (format) {
1013 case HAL_PIXEL_FORMAT_RAW16:
1014 case HAL_PIXEL_FORMAT_RAW12:
1015 case HAL_PIXEL_FORMAT_RAW10:
1016 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1017 return true;
1018 default:
1019 return false;
1020 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001021}
1022
1023status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1024 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001025 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001026 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001027 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001028 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1029 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001030 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001031
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001032 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001033 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001035 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001036 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001037 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1038 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001039 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001040 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001041 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001042
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 status_t res;
1044 bool wasActive = false;
1045
1046 switch (mStatus) {
1047 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001048 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049 return INVALID_OPERATION;
1050 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001051 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001052 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001053 case STATUS_UNCONFIGURED:
1054 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001055 // OK
1056 break;
1057 case STATUS_ACTIVE:
1058 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001059 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001061 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 return res;
1063 }
1064 wasActive = true;
1065 break;
1066 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001067 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 return INVALID_OPERATION;
1069 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071
1072 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001073
Shuzhen Wang0129d522016-10-30 22:43:41 -07001074 if (consumers.size() == 0 && !hasDeferredConsumer) {
1075 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1076 return BAD_VALUE;
1077 }
Zhijun He5d677d12016-05-29 16:52:39 -07001078
Shuzhen Wang0129d522016-10-30 22:43:41 -07001079 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001080 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1081 return BAD_VALUE;
1082 }
1083
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001084 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1085 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1086 // be found in only one sensor pixel mode.
1087 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1088 return BAD_VALUE;
1089 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001090 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001092 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001093 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001094 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001095 if (blobBufferSize <= 0) {
1096 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1097 return BAD_VALUE;
1098 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001099 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1100 blobBufferSize = width * height;
1101 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001102 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001103 if (blobBufferSize <= 0) {
1104 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1105 return BAD_VALUE;
1106 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001107 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001108 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001109 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001110 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001111 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001112 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001113 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001114 bool maxResolution =
1115 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1116 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001117 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1118 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001119 if (rawOpaqueBufferSize <= 0) {
1120 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1121 return BAD_VALUE;
1122 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001123 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001124 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001125 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001126 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001127 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001128 } else if (isShared) {
1129 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1130 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001131 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001132 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001133 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001134 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001135 newStream = new Camera3OutputStream(mNextStreamId,
1136 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001137 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001138 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001139 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001141 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001142 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001143 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001144 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001145 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 }
Emilian Peev40ead602017-09-26 15:46:36 +01001147
1148 size_t consumerCount = consumers.size();
1149 for (size_t i = 0; i < consumerCount; i++) {
1150 int id = newStream->getSurfaceId(consumers[i]);
1151 if (id < 0) {
1152 SET_ERR_L("Invalid surface id");
1153 return BAD_VALUE;
1154 }
1155 if (surfaceIds != nullptr) {
1156 surfaceIds->push_back(id);
1157 }
1158 }
1159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001160 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001161
Emilian Peev08dd2452017-04-06 16:55:14 +01001162 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001163
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001164 newStream->setImageDumpMask(mImageDumpMask);
1165
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001166 res = mOutputStreams.add(mNextStreamId, newStream);
1167 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001168 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 return res;
1170 }
1171
Shuzhen Wang316781a2020-08-18 18:11:01 -07001172 mSessionStatsBuilder.addStream(mNextStreamId);
1173
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001175 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176
1177 // Continue captures if active at start
1178 if (wasActive) {
1179 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001180 // Reuse current operating mode and session parameters for new stream config
1181 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001182 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001183 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1184 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 return res;
1186 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001187 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001189 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191}
1192
Emilian Peev710c1422017-08-30 11:19:38 +01001193status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001194 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001195 if (nullptr == streamInfo) {
1196 return BAD_VALUE;
1197 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001201 switch (mStatus) {
1202 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001203 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 return INVALID_OPERATION;
1205 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001206 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 case STATUS_UNCONFIGURED:
1209 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 case STATUS_ACTIVE:
1211 // OK
1212 break;
1213 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
1216 }
1217
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001218 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1219 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001220 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001221 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 }
1223
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001224 streamInfo->width = stream->getWidth();
1225 streamInfo->height = stream->getHeight();
1226 streamInfo->format = stream->getFormat();
1227 streamInfo->dataSpace = stream->getDataSpace();
1228 streamInfo->formatOverridden = stream->isFormatOverridden();
1229 streamInfo->originalFormat = stream->getOriginalFormat();
1230 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1231 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001232 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234}
1235
1236status_t Camera3Device::setStreamTransform(int id,
1237 int transform) {
1238 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001239 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001241
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001242 switch (mStatus) {
1243 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001244 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 return INVALID_OPERATION;
1246 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001247 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001249 case STATUS_UNCONFIGURED:
1250 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 case STATUS_ACTIVE:
1252 // OK
1253 break;
1254 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
1257 }
1258
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001259 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1260 if (stream == nullptr) {
1261 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 return BAD_VALUE;
1263 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001264 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001265}
1266
1267status_t Camera3Device::deleteStream(int id) {
1268 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001269 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 Mutex::Autolock l(mLock);
1271 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001272
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001273 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001274
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 // CameraDevice semantics require device to already be idle before
1276 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001277 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001278 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001279 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 }
1281
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001282 if (mStatus == STATUS_ERROR) {
1283 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1284 __FUNCTION__, mId.string());
1285 return -EBUSY;
1286 }
1287
Igor Murashkin2fba5842013-04-22 14:03:54 -07001288 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001289 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290 if (mInputStream != NULL && id == mInputStream->getId()) {
1291 deletedStream = mInputStream;
1292 mInputStream.clear();
1293 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001294 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001295 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001296 return BAD_VALUE;
1297 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001298 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001299 }
1300
1301 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001302 if (stream != nullptr) {
1303 deletedStream = stream;
1304 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305 }
1306
1307 // Free up the stream endpoint so that it can be used by some other stream
1308 res = deletedStream->disconnect();
1309 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001310 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001311 // fall through since we want to still list the stream as deleted.
1312 }
1313 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001314 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001315
1316 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001317}
1318
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001319status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001320 ATRACE_CALL();
1321 ALOGV("%s: E", __FUNCTION__);
1322
1323 Mutex::Autolock il(mInterfaceLock);
1324 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001325
Emilian Peev811d2952018-05-25 11:08:40 +01001326 // In case the client doesn't include any session parameter, try a
1327 // speculative configuration using the values from the last cached
1328 // default request.
1329 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001330 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001331 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1332 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1333 mLastTemplateId);
1334 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1335 operatingMode);
1336 }
1337
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001338 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1339}
1340
1341status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1342 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001343 //Filter out any incoming session parameters
1344 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001345 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1346 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001347 CameraMetadata filteredParams(availableSessionKeys.count);
1348 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1349 filteredParams.getAndLock());
1350 set_camera_metadata_vendor_id(meta, mVendorTagId);
1351 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001352 if (availableSessionKeys.count > 0) {
1353 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1354 camera_metadata_ro_entry entry = params.find(
1355 availableSessionKeys.data.i32[i]);
1356 if (entry.count > 0) {
1357 filteredParams.update(entry);
1358 }
1359 }
1360 }
1361
1362 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001363}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001364
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001365status_t Camera3Device::getInputBufferProducer(
1366 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001367 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001368 Mutex::Autolock il(mInterfaceLock);
1369 Mutex::Autolock l(mLock);
1370
1371 if (producer == NULL) {
1372 return BAD_VALUE;
1373 } else if (mInputStream == NULL) {
1374 return INVALID_OPERATION;
1375 }
1376
1377 return mInputStream->getInputBufferProducer(producer);
1378}
1379
Emilian Peevf4816702020-04-03 15:44:51 -07001380status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001381 CameraMetadata *request) {
1382 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001383 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001384
Emilian Peevf4816702020-04-03 15:44:51 -07001385 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001386 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001387 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001388 return BAD_VALUE;
1389 }
1390
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001391 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001392
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001393 {
1394 Mutex::Autolock l(mLock);
1395 switch (mStatus) {
1396 case STATUS_ERROR:
1397 CLOGE("Device has encountered a serious error");
1398 return INVALID_OPERATION;
1399 case STATUS_UNINITIALIZED:
1400 CLOGE("Device is not initialized!");
1401 return INVALID_OPERATION;
1402 case STATUS_UNCONFIGURED:
1403 case STATUS_CONFIGURED:
1404 case STATUS_ACTIVE:
1405 // OK
1406 break;
1407 default:
1408 SET_ERR_L("Unexpected status: %d", mStatus);
1409 return INVALID_OPERATION;
1410 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001411
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001412 if (!mRequestTemplateCache[templateId].isEmpty()) {
1413 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001414 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001415 return OK;
1416 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001417 }
1418
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001419 camera_metadata_t *rawRequest;
1420 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001421 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001422
1423 {
1424 Mutex::Autolock l(mLock);
1425 if (res == BAD_VALUE) {
1426 ALOGI("%s: template %d is not supported on this camera device",
1427 __FUNCTION__, templateId);
1428 return res;
1429 } else if (res != OK) {
1430 CLOGE("Unable to construct request template %d: %s (%d)",
1431 templateId, strerror(-res), res);
1432 return res;
1433 }
1434
1435 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1436 mRequestTemplateCache[templateId].acquire(rawRequest);
1437
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001438 // Override the template request with zoomRatioMapper
1439 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1440 &mRequestTemplateCache[templateId]);
1441 if (res != OK) {
1442 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1443 templateId, strerror(-res), res);
1444 return res;
1445 }
1446
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001447 // Fill in JPEG_QUALITY if not available
1448 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1449 static const uint8_t kDefaultJpegQuality = 95;
1450 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1451 &kDefaultJpegQuality, 1);
1452 }
1453
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001454 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001455 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001456 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001457 return OK;
1458}
1459
1460status_t Camera3Device::waitUntilDrained() {
1461 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001462 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001463 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001464 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001465
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001466 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001467}
1468
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001469status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001470 switch (mStatus) {
1471 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001472 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473 ALOGV("%s: Already idle", __FUNCTION__);
1474 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001475 case STATUS_CONFIGURED:
1476 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001477 case STATUS_ERROR:
1478 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001479 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001480 break;
1481 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001482 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 return INVALID_OPERATION;
1484 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001485 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1486 maxExpectedDuration);
1487 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001488 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001489 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001490 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1491 res);
1492 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001493 return res;
1494}
1495
Ruben Brunk183f0562015-08-12 12:55:02 -07001496void Camera3Device::internalUpdateStatusLocked(Status status) {
1497 mStatus = status;
1498 mRecentStatusUpdates.add(mStatus);
1499 mStatusChanged.broadcast();
1500}
1501
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001502// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001503status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001504 if (mRequestThread.get() != nullptr) {
1505 mRequestThread->setPaused(true);
1506 } else {
1507 return NO_INIT;
1508 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001509
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001510 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1511 maxExpectedDuration);
1512 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001513 if (res != OK) {
1514 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001515 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001516 }
1517
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001518 return res;
1519}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001520
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001521// Resume after internalPauseAndWaitLocked
1522status_t Camera3Device::internalResumeLocked() {
1523 status_t res;
1524
1525 mRequestThread->setPaused(false);
1526
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001527 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1528 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001529 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1530 if (res != OK) {
1531 SET_ERR_L("Can't transition to active in %f seconds!",
1532 kActiveTimeout/1e9);
1533 }
1534 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001536}
1537
Ruben Brunk183f0562015-08-12 12:55:02 -07001538status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001539 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001540
1541 size_t startIndex = 0;
1542 if (mStatusWaiters == 0) {
1543 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1544 // this status list
1545 mRecentStatusUpdates.clear();
1546 } else {
1547 // If other threads are waiting on updates to this status list, set the position of the
1548 // first element that this list will check rather than clearing the list.
1549 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001550 }
1551
Ruben Brunk183f0562015-08-12 12:55:02 -07001552 mStatusWaiters++;
1553
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001554 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001555 if (!active && mUseHalBufManager) {
1556 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001557 if (mStatus == STATUS_ACTIVE) {
1558 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001559 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001560 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001561 mRequestBufferSM.onWaitUntilIdle();
1562 }
1563
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001564 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001565 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001567 if (active == (mStatus == STATUS_ACTIVE)) {
1568 // Desired state is current
1569 break;
1570 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001571
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001572 nsecs_t timeElapsed = systemTime() - startTime;
1573 nsecs_t timeToWait = timeout - timeElapsed;
1574 if (timeToWait <= 0) {
1575 // Thread woke up spuriously but has timed out since.
1576 // Force out of loop with TIMED_OUT result.
1577 res = TIMED_OUT;
1578 break;
1579 }
1580 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001581 if (res != OK) break;
1582
Ruben Brunk183f0562015-08-12 12:55:02 -07001583 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1584 // transitions.
1585 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1586 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1587 __FUNCTION__);
1588
1589 // Encountered desired state since we began waiting
1590 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001591 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1592 stateSeen = true;
1593 break;
1594 }
1595 }
1596 } while (!stateSeen);
1597
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001598 if (signalPipelineDrain) {
1599 mRequestThread->resetPipelineDrain();
1600 }
1601
Ruben Brunk183f0562015-08-12 12:55:02 -07001602 mStatusWaiters--;
1603
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001604 return res;
1605}
1606
1607
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001608status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001609 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001610 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001612 if (listener != NULL && mListener != NULL) {
1613 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1614 }
1615 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001616 mRequestThread->setNotificationListener(listener);
1617 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001618
1619 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001620}
1621
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001622bool Camera3Device::willNotify3A() {
1623 return false;
1624}
1625
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001626status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001627 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001628 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001629
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001630 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001631 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1632 if (st == std::cv_status::timeout) {
1633 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001634 }
1635 }
1636 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001637}
1638
Jianing Weicb0652e2014-03-12 18:29:36 -07001639status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001640 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001641 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001642
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001643 if (mResultQueue.empty()) {
1644 return NOT_ENOUGH_DATA;
1645 }
1646
Jianing Weicb0652e2014-03-12 18:29:36 -07001647 if (frame == NULL) {
1648 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1649 return BAD_VALUE;
1650 }
1651
1652 CaptureResult &result = *(mResultQueue.begin());
1653 frame->mResultExtras = result.mResultExtras;
1654 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001655 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001656 mResultQueue.erase(mResultQueue.begin());
1657
1658 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001659}
1660
1661status_t Camera3Device::triggerAutofocus(uint32_t id) {
1662 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001663 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001664
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001665 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1666 // Mix-in this trigger into the next request and only the next request.
1667 RequestTrigger trigger[] = {
1668 {
1669 ANDROID_CONTROL_AF_TRIGGER,
1670 ANDROID_CONTROL_AF_TRIGGER_START
1671 },
1672 {
1673 ANDROID_CONTROL_AF_TRIGGER_ID,
1674 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001675 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001676 };
1677
1678 return mRequestThread->queueTrigger(trigger,
1679 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001680}
1681
1682status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1683 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001684 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001685
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001686 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1687 // Mix-in this trigger into the next request and only the next request.
1688 RequestTrigger trigger[] = {
1689 {
1690 ANDROID_CONTROL_AF_TRIGGER,
1691 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1692 },
1693 {
1694 ANDROID_CONTROL_AF_TRIGGER_ID,
1695 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001696 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001697 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001698
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001699 return mRequestThread->queueTrigger(trigger,
1700 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001701}
1702
1703status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1704 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001705 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001706
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001707 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1708 // Mix-in this trigger into the next request and only the next request.
1709 RequestTrigger trigger[] = {
1710 {
1711 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1712 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1713 },
1714 {
1715 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1716 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001717 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001718 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001719
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001720 return mRequestThread->queueTrigger(trigger,
1721 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001722}
1723
Jianing Weicb0652e2014-03-12 18:29:36 -07001724status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001725 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001726 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001727 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001728
Zhijun He7ef20392014-04-21 16:04:17 -07001729 {
1730 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001731
1732 // b/116514106 "disconnect()" can get called twice for the same device. The
1733 // camera device will not be initialized during the second run.
1734 if (mStatus == STATUS_UNINITIALIZED) {
1735 return OK;
1736 }
1737
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001738 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001739
1740 // Stop session and stream counter
1741 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001742 }
1743
Ravneet98ffa752022-03-02 07:22:46 +00001744 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1745 uint64_t maxExpectedDuration = (getExpectedInFlightDuration() + kBaseGetBufferWait) / 1e6;
1746 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1747 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1748
1749 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001750}
1751
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001752status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001753 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1754}
1755
1756status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001757 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001758 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001759 Mutex::Autolock il(mInterfaceLock);
1760 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001761
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001762 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1763 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001764 CLOGE("Stream %d does not exist", streamId);
1765 return BAD_VALUE;
1766 }
1767
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001768 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001769 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001770 return BAD_VALUE;
1771 }
1772
1773 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001774 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001775 return BAD_VALUE;
1776 }
1777
Ruben Brunkc78ac262015-08-13 17:58:46 -07001778 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001779}
1780
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001781status_t Camera3Device::tearDown(int streamId) {
1782 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001783 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001784 Mutex::Autolock il(mInterfaceLock);
1785 Mutex::Autolock l(mLock);
1786
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001787 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1788 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001789 CLOGE("Stream %d does not exist", streamId);
1790 return BAD_VALUE;
1791 }
1792
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001793 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1794 CLOGE("Stream %d is a target of a in-progress request", streamId);
1795 return BAD_VALUE;
1796 }
1797
1798 return stream->tearDown();
1799}
1800
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001801status_t Camera3Device::addBufferListenerForStream(int streamId,
1802 wp<Camera3StreamBufferListener> listener) {
1803 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001804 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001805 Mutex::Autolock il(mInterfaceLock);
1806 Mutex::Autolock l(mLock);
1807
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001808 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1809 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001810 CLOGE("Stream %d does not exist", streamId);
1811 return BAD_VALUE;
1812 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001813 stream->addBufferListener(listener);
1814
1815 return OK;
1816}
1817
Austin Borger4a870a32022-02-25 01:48:41 +00001818float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1819 camera_metadata_entry minDurations =
1820 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1821 for (size_t i = 0; i < minDurations.count; i += 4) {
1822 if (minDurations.data.i64[i] == stream->getFormat()
1823 && minDurations.data.i64[i+1] == stream->getWidth()
1824 && minDurations.data.i64[i+2] == stream->getHeight()) {
1825 int64_t minFrameDuration = minDurations.data.i64[i+3];
1826 return 1e9f / minFrameDuration;
1827 }
1828 }
1829 return 0.0f;
1830}
1831
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001832/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001833 * Methods called by subclasses
1834 */
1835
1836void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001837 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001838 std::vector<int> streamIds;
1839 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001840 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001841
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001842 {
1843 // Need mLock to safely update state and synchronize to current
1844 // state of methods in flight.
1845 Mutex::Autolock l(mLock);
1846 // We can get various system-idle notices from the status tracker
1847 // while starting up. Only care about them if we've actually sent
1848 // in some requests recently.
1849 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1850 return;
1851 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001852 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1853 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001854 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001855
1856 // Skip notifying listener if we're doing some user-transparent
1857 // state changes
1858 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001859
Austin Borger4a870a32022-02-25 01:48:41 +00001860 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1861 auto stream = mOutputStreams[i];
1862 if (stream.get() == nullptr) continue;
1863
1864 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1865 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1866
1867 // Populate stream statistics in case of Idle
1868 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001869 streamIds.push_back(stream->getId());
1870 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1871 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001872 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001873 if (camera3Stream != nullptr) {
1874 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001875 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001876 }
1877 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001878 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001879 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001880 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001881 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001882 }
1883 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001884 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001885
1886 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001887 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001888 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001889 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001890 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001891 status_t res = OK;
1892 if (listener != nullptr) {
1893 if (idle) {
1894 // Get session stats from the builder, and notify the listener.
1895 int64_t requestCount, resultErrorCount;
1896 bool deviceError;
1897 std::map<int, StreamStats> streamStatsMap;
1898 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1899 &deviceError, &streamStatsMap);
1900 for (size_t i = 0; i < streamIds.size(); i++) {
1901 int streamId = streamIds[i];
1902 auto stats = streamStatsMap.find(streamId);
1903 if (stats != streamStatsMap.end()) {
1904 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1905 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1906 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1907 streamStats[i].mHistogramType =
1908 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1909 streamStats[i].mHistogramBins.assign(
1910 stats->second.mCaptureLatencyBins.begin(),
1911 stats->second.mCaptureLatencyBins.end());
1912 streamStats[i].mHistogramCounts.assign(
1913 stats->second.mCaptureLatencyHistogram.begin(),
1914 stats->second.mCaptureLatencyHistogram.end());
1915 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001916 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001917 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1918 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001919 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001920 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001921 }
1922 if (res != OK) {
1923 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1924 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001925 }
1926}
1927
Shuzhen Wang758c2152017-01-10 18:26:18 -08001928status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001929 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001930 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001931 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1932 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001933
1934 if (surfaceIds == nullptr) {
1935 return BAD_VALUE;
1936 }
1937
Zhijun He5d677d12016-05-29 16:52:39 -07001938 Mutex::Autolock il(mInterfaceLock);
1939 Mutex::Autolock l(mLock);
1940
Shuzhen Wang758c2152017-01-10 18:26:18 -08001941 if (consumers.size() == 0) {
1942 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001943 return BAD_VALUE;
1944 }
1945
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001946 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1947 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001948 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001949 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001950 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001951
1952 // isConsumerConfigurationDeferred will be off after setConsumers
1953 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001954 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001955 if (res != OK) {
1956 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1957 return res;
1958 }
1959
Emilian Peev40ead602017-09-26 15:46:36 +01001960 for (auto &consumer : consumers) {
1961 int id = stream->getSurfaceId(consumer);
1962 if (id < 0) {
1963 CLOGE("Invalid surface id!");
1964 return BAD_VALUE;
1965 }
1966 surfaceIds->push_back(id);
1967 }
1968
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001969 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001970 if (!stream->isConfiguring()) {
1971 CLOGE("Stream %d was already fully configured.", streamId);
1972 return INVALID_OPERATION;
1973 }
Zhijun He5d677d12016-05-29 16:52:39 -07001974
Shuzhen Wang0129d522016-10-30 22:43:41 -07001975 res = stream->finishConfiguration();
1976 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001977 // If finishConfiguration fails due to abandoned surface, do not set
1978 // device to error state.
1979 bool isSurfaceAbandoned =
1980 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1981 if (!isSurfaceAbandoned) {
1982 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1983 stream->getId(), strerror(-res), res);
1984 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001985 return res;
1986 }
Zhijun He5d677d12016-05-29 16:52:39 -07001987 }
1988
1989 return OK;
1990}
1991
Emilian Peev40ead602017-09-26 15:46:36 +01001992status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
1993 const std::vector<OutputStreamInfo> &outputInfo,
1994 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
1995 Mutex::Autolock il(mInterfaceLock);
1996 Mutex::Autolock l(mLock);
1997
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001998 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1999 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002000 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002001 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002002 }
2003
2004 for (const auto &it : removedSurfaceIds) {
2005 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2006 CLOGE("Shared surface still part of a pending request!");
2007 return -EBUSY;
2008 }
2009 }
2010
Emilian Peev40ead602017-09-26 15:46:36 +01002011 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2012 if (res != OK) {
2013 CLOGE("Stream %d failed to update stream (error %d %s) ",
2014 streamId, res, strerror(-res));
2015 if (res == UNKNOWN_ERROR) {
2016 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2017 __FUNCTION__);
2018 }
2019 return res;
2020 }
2021
2022 return res;
2023}
2024
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002025status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2026 Mutex::Autolock il(mInterfaceLock);
2027 Mutex::Autolock l(mLock);
2028
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002029 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2030 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002031 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2032 return BAD_VALUE;
2033 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002034
2035 if (dropping) {
2036 mSessionStatsBuilder.stopCounter(streamId);
2037 } else {
2038 mSessionStatsBuilder.startCounter(streamId);
2039 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002040 return stream->dropBuffers(dropping);
2041}
2042
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002043/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 * Camera3Device private methods
2045 */
2046
2047sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002048 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002049 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002051 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002052 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002053
2054 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002055 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002056 if (inputStreams.count > 0) {
2057 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002058 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002059 CLOGE("Request references unknown input stream %d",
2060 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002061 return NULL;
2062 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002063
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002065 SET_ERR_L("%s: input stream %d is not configured!",
2066 __FUNCTION__, mInputStream->getId());
2067 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002068 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002069 // Check if stream prepare is blocking requests.
2070 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002071 CLOGE("Request references an input stream that's being prepared!");
2072 return NULL;
2073 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002074
2075 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002076 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 }
2078
2079 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002080 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002081 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002082 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083 return NULL;
2084 }
2085
2086 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002087 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2088 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002089 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002090 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002091 return NULL;
2092 }
Zhijun He5d677d12016-05-29 16:52:39 -07002093 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002094 auto iter = surfaceMap.find(streams.data.i32[i]);
2095 if (iter != surfaceMap.end()) {
2096 const std::vector<size_t>& surfaces = iter->second;
2097 for (const auto& surface : surfaces) {
2098 if (stream->isConsumerConfigurationDeferred(surface)) {
2099 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2100 "due to deferred consumer", stream->getId(), surface);
2101 return NULL;
2102 }
2103 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002104 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002105 }
2106
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002107 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002108 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2109 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002111 // Check if stream prepare is blocking requests.
2112 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002113 CLOGE("Request references an output stream that's being prepared!");
2114 return NULL;
2115 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002116
2117 newRequest->mOutputStreams.push(stream);
2118 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002119 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002120 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002121
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002122 auto rotateAndCropEntry =
2123 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2124 if (rotateAndCropEntry.count > 0 &&
2125 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2126 newRequest->mRotateAndCropAuto = true;
2127 } else {
2128 newRequest->mRotateAndCropAuto = false;
2129 }
2130
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002131 auto zoomRatioEntry =
2132 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2133 if (zoomRatioEntry.count > 0 &&
2134 zoomRatioEntry.data.f[0] == 1.0f) {
2135 newRequest->mZoomRatioIs1x = true;
2136 } else {
2137 newRequest->mZoomRatioIs1x = false;
2138 }
2139
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002140 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002141 for (auto& settings : newRequest->mSettingsList) {
2142 auto testPatternModeEntry =
2143 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2144 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2145 testPatternModeEntry.data.i32[0] :
2146 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002147
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002148 auto testPatternDataEntry =
2149 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2150 if (testPatternDataEntry.count >= 4) {
2151 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2152 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2153 } else {
2154 settings.mOriginalTestPatternData[0] = 0;
2155 settings.mOriginalTestPatternData[1] = 0;
2156 settings.mOriginalTestPatternData[2] = 0;
2157 settings.mOriginalTestPatternData[3] = 0;
2158 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002159 }
2160 }
2161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002162 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002163}
2164
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002165void Camera3Device::cancelStreamsConfigurationLocked() {
2166 int res = OK;
2167 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2168 res = mInputStream->cancelConfiguration();
2169 if (res != OK) {
2170 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2171 mInputStream->getId(), strerror(-res), res);
2172 }
2173 }
2174
2175 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002176 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002177 if (outputStream->isConfiguring()) {
2178 res = outputStream->cancelConfiguration();
2179 if (res != OK) {
2180 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2181 outputStream->getId(), strerror(-res), res);
2182 }
2183 }
2184 }
2185
2186 // Return state to that at start of call, so that future configures
2187 // properly clean things up
2188 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2189 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002190
2191 res = mPreparerThread->resume();
2192 if (res != OK) {
2193 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2194 }
2195}
2196
Emilian Peev0d0191e2020-04-21 17:01:18 -07002197bool Camera3Device::checkAbandonedStreamsLocked() {
2198 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2199 return true;
2200 }
2201
2202 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2203 auto stream = mOutputStreams[i];
2204 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2205 return true;
2206 }
2207 }
2208
2209 return false;
2210}
2211
Emilian Peev3bead5f2020-05-28 17:29:08 -07002212bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002213 ATRACE_CALL();
2214 bool ret = false;
2215
Shuzhen Wang316781a2020-08-18 18:11:01 -07002216 nsecs_t startTime = systemTime();
2217
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002218 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002219 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2220
2221 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002222 if (checkAbandonedStreamsLocked()) {
2223 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2224 __FUNCTION__);
2225 return true;
2226 }
2227
Emilian Peev3bead5f2020-05-28 17:29:08 -07002228 status_t rc = NO_ERROR;
2229 bool markClientActive = false;
2230 if (mStatus == STATUS_ACTIVE) {
2231 markClientActive = true;
2232 mPauseStateNotify = true;
2233 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2234
2235 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2236 }
2237
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002238 if (rc == NO_ERROR) {
2239 mNeedConfig = true;
2240 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2241 if (rc == NO_ERROR) {
2242 ret = true;
2243 mPauseStateNotify = false;
2244 //Moving to active state while holding 'mLock' is important.
2245 //There could be pending calls to 'create-/deleteStream' which
2246 //will trigger another stream configuration while the already
2247 //present streams end up with outstanding buffers that will
2248 //not get drained.
2249 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002250 } else if (rc == DEAD_OBJECT) {
2251 // DEAD_OBJECT can be returned if either the consumer surface is
2252 // abandoned, or the HAL has died.
2253 // - If the HAL has died, configureStreamsLocked call will set
2254 // device to error state,
2255 // - If surface is abandoned, we should not set device to error
2256 // state.
2257 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002258 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002259 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002260 }
2261 } else {
2262 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2263 }
2264
Shuzhen Wang316781a2020-08-18 18:11:01 -07002265 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2266 ns2ms(systemTime() - startTime));
2267
Emilian Peev3bead5f2020-05-28 17:29:08 -07002268 if (markClientActive) {
2269 mStatusTracker->markComponentActive(clientStatusId);
2270 }
2271
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002272 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002273}
2274
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002275status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002276 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002277 ATRACE_CALL();
2278 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002279
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002280 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002281 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002282 return INVALID_OPERATION;
2283 }
2284
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002285 if (operatingMode < 0) {
2286 CLOGE("Invalid operating mode: %d", operatingMode);
2287 return BAD_VALUE;
2288 }
2289
2290 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002291 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002292
2293 if (mOperatingMode != operatingMode) {
2294 mNeedConfig = true;
2295 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2296 mOperatingMode = operatingMode;
2297 }
2298
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002299 // Reset min expected duration when session is reconfigured.
2300 mMinExpectedDuration = 0;
2301
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002302 // In case called from configureStreams, abort queued input buffers not belonging to
2303 // any pending requests.
2304 if (mInputStream != NULL && notifyRequestThread) {
2305 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002306 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002307 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002308 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002309 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002310 if (res != OK) {
2311 // Exhausted acquiring all input buffers.
2312 break;
2313 }
2314
Emilian Peevf4816702020-04-03 15:44:51 -07002315 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002316 res = mInputStream->returnInputBuffer(inputBuffer);
2317 if (res != OK) {
2318 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2319 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2320 }
2321 }
2322 }
2323
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002324 if (!mNeedConfig) {
2325 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2326 return OK;
2327 }
2328
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002329 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002330 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002331 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2332 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002333 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002334 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002335 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002336 }
2337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002338 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002339 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002341 mPreparerThread->pause();
2342
Emilian Peevf4816702020-04-03 15:44:51 -07002343 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002344 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002346 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002347
Emilian Peevf4816702020-04-03 15:44:51 -07002348 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002350 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002351
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002352
2353 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002354 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002355 inputStream = mInputStream->startConfiguration();
2356 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002357 CLOGE("Can't start input stream configuration");
2358 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002359 return INVALID_OPERATION;
2360 }
2361 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002362
2363 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002364 }
2365
Shuzhen Wang99080502021-03-07 21:08:20 -08002366 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002367 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002369
2370 // Don't configure bidi streams twice, nor add them twice to the list
2371 if (mOutputStreams[i].get() ==
2372 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2373
2374 config.num_streams--;
2375 continue;
2376 }
2377
Emilian Peevf4816702020-04-03 15:44:51 -07002378 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002379 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002380 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002381 CLOGE("Can't start output stream configuration");
2382 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 return INVALID_OPERATION;
2384 }
2385 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002386
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002387 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002388 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2389 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002390 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2391 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002392 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2393 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002394 } else if (outputStream->data_space ==
2395 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2396 bufferSizes[k] = outputStream->width * outputStream->height;
2397 } else {
2398 ALOGW("%s: Blob dataSpace %d not supported",
2399 __FUNCTION__, outputStream->data_space);
2400 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002401 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002402
2403 if (mOutputStreams[i]->isMultiResolution()) {
2404 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2405 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2406 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2407 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002408
2409 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2410 composerSurfacePresent = true;
2411 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412 }
2413
2414 config.streams = streams.editArray();
2415
2416 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002417 // max_buffers, usage, and priv fields, as well as data_space and format
2418 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002419
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002420 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002421 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002422 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002423
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002424 if (res == BAD_VALUE) {
2425 // HAL rejected this set of streams as unsupported, clean up config
2426 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002427 CLOGE("Set of requested inputs/outputs not supported by HAL");
2428 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002429 return BAD_VALUE;
2430 } else if (res != OK) {
2431 // Some other kind of error from configure_streams - this is not
2432 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002433 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2434 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002435 return res;
2436 }
2437
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002438 // Finish all stream configuration immediately.
2439 // TODO: Try to relax this later back to lazy completion, which should be
2440 // faster
2441
Igor Murashkin073f8572013-05-02 14:59:28 -07002442 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002443 bool streamReConfigured = false;
2444 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002445 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002446 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002447 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002448 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002449 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2450 return DEAD_OBJECT;
2451 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002452 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002453 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002454 if (streamReConfigured) {
2455 mInterface->onStreamReConfigured(mInputStream->getId());
2456 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002457 }
2458
2459 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002460 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002461 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002462 bool streamReConfigured = false;
2463 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002464 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002465 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002466 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002467 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002468 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2469 return DEAD_OBJECT;
2470 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002471 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002472 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002473 if (streamReConfigured) {
2474 mInterface->onStreamReConfigured(outputStream->getId());
2475 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002476 }
2477 }
2478
Emilian Peeve23f1d92021-09-20 14:56:01 -07002479 mRequestThread->setComposerSurface(composerSurfacePresent);
2480
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002481 // Request thread needs to know to avoid using repeat-last-settings protocol
2482 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002483 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002484 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2485 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002486 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487
Zhijun He90f7c372016-08-16 16:19:43 -07002488 char value[PROPERTY_VALUE_MAX];
2489 property_get("camera.fifo.disable", value, "0");
2490 int32_t disableFifo = atoi(value);
2491 if (disableFifo != 1) {
2492 // Boost priority of request thread to SCHED_FIFO.
2493 pid_t requestThreadTid = mRequestThread->getTid();
2494 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002495 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002496 if (res != OK) {
2497 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2498 strerror(-res), res);
2499 } else {
2500 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2501 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002502 }
2503
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002504 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002505 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2506 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2507 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2508 sessionParams.unlock(newSessionParams);
2509 mSessionParams.unlock(currentSessionParams);
2510 if (updateSessionParams) {
2511 mSessionParams = sessionParams;
2512 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002514 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002515
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002516 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002517 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002518
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002519 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002520
Zhijun He0a210512014-07-24 13:45:15 -07002521 // tear down the deleted streams after configure streams.
2522 mDeletedStreams.clear();
2523
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002524 auto rc = mPreparerThread->resume();
2525 if (rc != OK) {
2526 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2527 return rc;
2528 }
2529
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002530 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002531 mRequestBufferSM.onStreamsConfigured();
2532 }
2533
Cliff Wu3b268182021-07-06 15:44:43 +08002534 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002535 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002536 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002537 // configure the injection streams.
2538 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002539 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002540 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2541 res = mInjectionMethods->injectCamera(config, bufferSizes);
2542 if (res != OK) {
2543 ALOGE("Can't finish inject camera process!");
2544 return res;
2545 }
Cliff Wu3b268182021-07-06 15:44:43 +08002546 } else {
2547 // First run configureStreamsLocked() and then call injectCamera() case:
2548 // If the stream configuration has been completed and camera deive is active, but the
2549 // injection camera has not been injected yet, we need to store the stream configuration of
2550 // the internal camera (because the stream configuration of the injection camera is based
2551 // on the internal camera). When injecting occurs later, this configuration can be used by
2552 // the injection camera.
2553 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2554 " injection camera has not been injected yet.", __FUNCTION__);
2555 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002556 }
2557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002558 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002559}
2560
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002561status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002562 ATRACE_CALL();
2563 status_t res;
2564
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002565 if (mFakeStreamId != NO_STREAM) {
2566 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002567 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002568 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002569 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002570 return INVALID_OPERATION;
2571 }
2572
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002573 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002574
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002575 sp<Camera3OutputStreamInterface> fakeStream =
2576 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002577
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002578 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002579 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002580 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002581 return res;
2582 }
2583
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002584 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002585 mNextStreamId++;
2586
2587 return OK;
2588}
2589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002590status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002591 ATRACE_CALL();
2592 status_t res;
2593
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002594 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002595 if (mOutputStreams.size() == 1) return OK;
2596
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002597 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002598
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002599 // Ok, have a fake stream and there's at least one other output stream,
2600 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002603 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002604 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002605 return INVALID_OPERATION;
2606 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002607 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002608
2609 // Free up the stream endpoint so that it can be used by some other stream
2610 res = deletedStream->disconnect();
2611 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002612 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002613 // fall through since we want to still list the stream as deleted.
2614 }
2615 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002616 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002617
2618 return res;
2619}
2620
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002621void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002622 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002623 Mutex::Autolock l(mLock);
2624 va_list args;
2625 va_start(args, fmt);
2626
2627 setErrorStateLockedV(fmt, args);
2628
2629 va_end(args);
2630}
2631
2632void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002633 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002634 Mutex::Autolock l(mLock);
2635 setErrorStateLockedV(fmt, args);
2636}
2637
2638void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2639 va_list args;
2640 va_start(args, fmt);
2641
2642 setErrorStateLockedV(fmt, args);
2643
2644 va_end(args);
2645}
2646
2647void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002648 // Print out all error messages to log
2649 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002650 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002651
2652 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002653 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002654
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002655 mErrorCause = errorCause;
2656
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002657 if (mRequestThread != nullptr) {
2658 mRequestThread->setPaused(true);
2659 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002660 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002661
2662 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002663 sp<NotificationListener> listener = mListener.promote();
2664 if (listener != NULL) {
2665 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002667 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002668 }
2669
2670 // Save stack trace. View by dumping it later.
2671 CameraTraces::saveTrace();
2672 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002673}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002674
2675/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002676 * In-flight request management
2677 */
2678
Jianing Weicb0652e2014-03-12 18:29:36 -07002679status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002680 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002681 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002682 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Shuzhen Wang99080502021-03-07 21:08:20 -08002683 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2684 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002685 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002686 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002687 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002688
2689 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002690 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002691 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002692 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2693 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002694 if (res < 0) return res;
2695
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002696 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002697 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2698 // avoid a deadlock during reprocess requests.
2699 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002700 if (mStatusTracker != nullptr) {
2701 mStatusTracker->markComponentActive(mInFlightStatusId);
2702 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002703 }
2704
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002705 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002706 return OK;
2707}
2708
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002709void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002710 // Indicate idle inFlightMap to the status tracker
2711 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002712 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002713 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2714 // avoid a deadlock during reprocess requests.
2715 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002716 if (mStatusTracker != nullptr) {
2717 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2718 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002719 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002720 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002721}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002722
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002723void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002724 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002725 // something has likely gone wrong. This might still be legit only if application send in
2726 // a long burst of long exposure requests.
2727 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2728 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2729 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2730 mInFlightMap.size(), mExpectedInflightDuration);
2731 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2732 kInFlightWarnLimitHighSpeed) {
2733 CLOGW("In-flight list too large for high speed configuration: %zu,"
2734 "total inflight duration %" PRIu64,
2735 mInFlightMap.size(), mExpectedInflightDuration);
2736 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002737 }
2738}
2739
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002740void Camera3Device::onInflightMapFlushedLocked() {
2741 mExpectedInflightDuration = 0;
2742}
2743
2744void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002745 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002746 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2747 mInFlightMap.removeItemsAt(idx, 1);
2748
2749 onInflightEntryRemovedLocked(duration);
2750}
2751
2752
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002753void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002754 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002755 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002756 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002757 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002758 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002759 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002760
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002761 FlushInflightReqStates states {
2762 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002763 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002764
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002765 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002766}
2767
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002768CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002769 ALOGV("%s", __FUNCTION__);
2770
Igor Murashkin1e479c02013-09-06 16:55:14 -07002771 CameraMetadata retVal;
2772
2773 if (mRequestThread != NULL) {
2774 retVal = mRequestThread->getLatestRequest();
2775 }
2776
Igor Murashkin1e479c02013-09-06 16:55:14 -07002777 return retVal;
2778}
2779
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002780void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002781 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002782 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002783 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2784 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002785
2786 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002787 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002788}
2789
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002790void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002791 std::vector<native_handle_t*> *handles, bool closeFd) {
2792 if (handles == nullptr) {
2793 return;
2794 }
2795 if (closeFd) {
2796 for (auto& handle : *handles) {
2797 native_handle_close(handle);
2798 }
2799 }
2800 for (auto& handle : *handles) {
2801 native_handle_delete(handle);
2802 }
2803 handles->clear();
2804 return;
2805}
2806
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002807/**
2808 * HalInterface inner class methods
2809 */
2810
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002811void Camera3Device::HalInterface::getInflightBufferKeys(
2812 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002813 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002814 return;
2815}
2816
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002817void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2818 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002819 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002820 return;
2821}
2822
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002823bool Camera3Device::HalInterface::verifyBufferIds(
2824 int32_t streamId, std::vector<uint64_t>& bufIds) {
2825 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002826}
2827
2828status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002829 int32_t frameNumber, int32_t streamId,
2830 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002831 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002832}
2833
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002834status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002835 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002836 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002837}
2838
2839// Find and pop a buffer_handle_t based on bufferId
2840status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002841 uint64_t bufferId,
2842 /*out*/ buffer_handle_t** buffer,
2843 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002844 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002845}
2846
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002847std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2848 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002849 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002850}
2851
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002852uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2853 const native_handle_t* handle) {
2854 return mBufferRecords.removeOneBufferCache(streamId, handle);
2855}
2856
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002857void Camera3Device::HalInterface::onBufferFreed(
2858 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002859 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2860 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2861 if (bufferId != BUFFER_ID_NO_BUFFER) {
2862 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002863 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002864}
2865
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002866void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002867 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2868 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2869 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002870 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2871 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002872}
2873
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002874/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002875 * RequestThread inner class methods
2876 */
2877
2878Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002879 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002880 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002881 bool useHalBufManager,
2882 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002883 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002884 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002885 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002886 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002887 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002888 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002889 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002890 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002891 mReconfigured(false),
2892 mDoPause(false),
2893 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002894 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002895 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002896 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002897 mCurrentAfTriggerId(0),
2898 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002899 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002900 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002901 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002902 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002903 mRepeatingLastFrameNumber(
2904 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002905 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002906 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002907 mRequestLatency(kRequestLatencyBinSize),
2908 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002909 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002910 mUseHalBufManager(useHalBufManager),
2911 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002912 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913}
2914
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002915Camera3Device::RequestThread::~RequestThread() {}
2916
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002917void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002918 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002919 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 Mutex::Autolock l(mRequestLock);
2921 mListener = listener;
2922}
2923
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002924void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002925 const CameraMetadata& sessionParams,
2926 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002927 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 Mutex::Autolock l(mRequestLock);
2929 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002930 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002931 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002932 // Prepare video stream for high speed recording.
2933 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002934 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002935}
2936
Jianing Wei90e59c92014-03-12 18:29:36 -07002937status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002938 List<sp<CaptureRequest> > &requests,
2939 /*out*/
2940 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002941 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002942 Mutex::Autolock l(mRequestLock);
2943 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2944 ++it) {
2945 mRequestQueue.push_back(*it);
2946 }
2947
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002948 if (lastFrameNumber != NULL) {
2949 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2950 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2951 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2952 *lastFrameNumber);
2953 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002954
Jianing Wei90e59c92014-03-12 18:29:36 -07002955 unpauseForNewRequests();
2956
2957 return OK;
2958}
2959
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002960
2961status_t Camera3Device::RequestThread::queueTrigger(
2962 RequestTrigger trigger[],
2963 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002964 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002965 Mutex::Autolock l(mTriggerMutex);
2966 status_t ret;
2967
2968 for (size_t i = 0; i < count; ++i) {
2969 ret = queueTriggerLocked(trigger[i]);
2970
2971 if (ret != OK) {
2972 return ret;
2973 }
2974 }
2975
2976 return OK;
2977}
2978
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002979const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2980 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002981 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002982 if (d != nullptr) return d->mId;
2983 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002984}
2985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002986status_t Camera3Device::RequestThread::queueTriggerLocked(
2987 RequestTrigger trigger) {
2988
2989 uint32_t tag = trigger.metadataTag;
2990 ssize_t index = mTriggerMap.indexOfKey(tag);
2991
2992 switch (trigger.getTagType()) {
2993 case TYPE_BYTE:
2994 // fall-through
2995 case TYPE_INT32:
2996 break;
2997 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002998 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2999 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003000 return INVALID_OPERATION;
3001 }
3002
3003 /**
3004 * Collect only the latest trigger, since we only have 1 field
3005 * in the request settings per trigger tag, and can't send more than 1
3006 * trigger per request.
3007 */
3008 if (index != NAME_NOT_FOUND) {
3009 mTriggerMap.editValueAt(index) = trigger;
3010 } else {
3011 mTriggerMap.add(tag, trigger);
3012 }
3013
3014 return OK;
3015}
3016
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003017status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003018 const RequestList &requests,
3019 /*out*/
3020 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003021 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003022 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003023 if (lastFrameNumber != NULL) {
3024 *lastFrameNumber = mRepeatingLastFrameNumber;
3025 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003026 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003027 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003028 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3029 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003030
3031 unpauseForNewRequests();
3032
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003033 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034 return OK;
3035}
3036
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003037bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003038 if (mRepeatingRequests.empty()) {
3039 return false;
3040 }
3041 int32_t requestId = requestIn->mResultExtras.requestId;
3042 const RequestList &repeatRequests = mRepeatingRequests;
3043 // All repeating requests are guaranteed to have same id so only check first quest
3044 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3045 return (firstRequest->mResultExtras.requestId == requestId);
3046}
3047
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003048status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003049 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003050 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003051 return clearRepeatingRequestsLocked(lastFrameNumber);
3052
3053}
3054
3055status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003056 std::vector<int32_t> streamIds;
3057 for (const auto& request : mRepeatingRequests) {
3058 for (const auto& stream : request->mOutputStreams) {
3059 streamIds.push_back(stream->getId());
3060 }
3061 }
3062
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003063 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003064 if (lastFrameNumber != NULL) {
3065 *lastFrameNumber = mRepeatingLastFrameNumber;
3066 }
Emilian Peev2295df72021-11-12 18:14:10 -08003067
3068 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3069
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003070 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003071 return OK;
3072}
3073
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003074status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003075 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003076 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003077 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003078 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003079
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003080 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003081
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003082 // Send errors for all requests pending in the request queue, including
3083 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003084 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003085 if (listener != NULL) {
3086 for (RequestList::iterator it = mRequestQueue.begin();
3087 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003088 // Abort the input buffers for reprocess requests.
3089 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003090 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003091 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003092 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003093 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003094 if (res != OK) {
3095 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3096 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3097 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003098 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003099 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3100 if (res != OK) {
3101 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3102 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3103 }
3104 }
3105 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003106 // Set the frame number this request would have had, if it
3107 // had been submitted; this frame number will not be reused.
3108 // The requestId and burstId fields were set when the request was
3109 // submitted originally (in convertMetadataListToRequestListLocked)
3110 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003111 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003112 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003113 }
3114 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003115 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003116
3117 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003118 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003119 if (lastFrameNumber != NULL) {
3120 *lastFrameNumber = mRepeatingLastFrameNumber;
3121 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003122 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003123 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003124 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003125 return OK;
3126}
3127
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003128status_t Camera3Device::RequestThread::flush() {
3129 ATRACE_CALL();
3130 Mutex::Autolock l(mFlushLock);
3131
Emilian Peev08dd2452017-04-06 16:55:14 +01003132 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003133}
3134
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003135void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003136 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003137 Mutex::Autolock l(mPauseLock);
3138 mDoPause = paused;
3139 mDoPauseSignal.signal();
3140}
3141
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003142status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3143 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003144 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003145 Mutex::Autolock l(mLatestRequestMutex);
3146 status_t res;
3147 while (mLatestRequestId != requestId) {
3148 nsecs_t startTime = systemTime();
3149
3150 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3151 if (res != OK) return res;
3152
3153 timeout -= (systemTime() - startTime);
3154 }
3155
3156 return OK;
3157}
3158
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003159void Camera3Device::RequestThread::requestExit() {
3160 // Call parent to set up shutdown
3161 Thread::requestExit();
3162 // The exit from any possible waits
3163 mDoPauseSignal.signal();
3164 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003165
3166 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3167 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003168}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003169
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003170void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003171 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003172 bool surfaceAbandoned = false;
3173 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003174 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003175 {
3176 Mutex::Autolock l(mRequestLock);
3177 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3178 // repeating requests.
3179 for (const auto& request : mRepeatingRequests) {
3180 for (const auto& s : request->mOutputStreams) {
3181 if (s->isAbandoned()) {
3182 surfaceAbandoned = true;
3183 clearRepeatingRequestsLocked(&lastFrameNumber);
3184 break;
3185 }
3186 }
3187 if (surfaceAbandoned) {
3188 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003189 }
3190 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003191 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003192 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003193
3194 if (listener != NULL && surfaceAbandoned) {
3195 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003196 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003197}
3198
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003199bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003200 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003201 status_t res;
3202 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003203 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003204 uint32_t numRequestProcessed = 0;
3205 for (size_t i = 0; i < batchSize; i++) {
3206 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003207 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003208 }
3209
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003210 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3211
3212 bool triggerRemoveFailed = false;
3213 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3214 for (size_t i = 0; i < numRequestProcessed; i++) {
3215 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3216 nextRequest.submitted = true;
3217
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003218 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003219
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003220 if (!triggerRemoveFailed) {
3221 // Remove any previously queued triggers (after unlock)
3222 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3223 if (removeTriggerRes != OK) {
3224 triggerRemoveFailed = true;
3225 triggerFailedRequest = nextRequest;
3226 }
3227 }
3228 }
3229
3230 if (triggerRemoveFailed) {
3231 SET_ERR("RequestThread: Unable to remove triggers "
3232 "(capture request %d, HAL device: %s (%d)",
3233 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3234 cleanUpFailedRequests(/*sendRequestError*/ false);
3235 return false;
3236 }
3237
3238 if (res != OK) {
3239 // Should only get a failure here for malformed requests or device-level
3240 // errors, so consider all errors fatal. Bad metadata failures should
3241 // come through notify.
3242 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3243 mNextRequests[numRequestProcessed].halRequest.frame_number,
3244 strerror(-res), res);
3245 cleanUpFailedRequests(/*sendRequestError*/ false);
3246 return false;
3247 }
3248 return true;
3249}
3250
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003251Camera3Device::RequestThread::ExpectedDurationInfo
3252 Camera3Device::RequestThread::calculateExpectedDurationRange(
3253 const camera_metadata_t *request) {
3254 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003255 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003256 InFlightRequest::kDefaultMaxExpectedDuration,
3257 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003258 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3259 find_camera_metadata_ro_entry(request,
3260 ANDROID_CONTROL_AE_MODE,
3261 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003262 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003263
3264 switch (e.data.u8[0]) {
3265 case ANDROID_CONTROL_AE_MODE_OFF:
3266 find_camera_metadata_ro_entry(request,
3267 ANDROID_SENSOR_EXPOSURE_TIME,
3268 &e);
3269 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003270 expectedDurationInfo.minDuration = e.data.i64[0];
3271 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003272 }
3273 find_camera_metadata_ro_entry(request,
3274 ANDROID_SENSOR_FRAME_DURATION,
3275 &e);
3276 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003277 expectedDurationInfo.minDuration =
3278 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3279 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003280 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003281 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003282 break;
3283 default:
3284 find_camera_metadata_ro_entry(request,
3285 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3286 &e);
3287 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003288 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3289 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003290 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003291 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003292 break;
3293 }
3294
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003295 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003296}
3297
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003298bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3299 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3300 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3301 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3302 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3303 return true;
3304 }
3305
3306 return false;
3307}
3308
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003309void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3310 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003311 camera_capture_request_t& halRequest = nextRequest.halRequest;
3312 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003313 Mutex::Autolock al(mLatestRequestMutex);
3314
Shuzhen Wang83bff122020-11-20 15:51:39 -08003315 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003316 mLatestRequest.acquire(cloned);
3317
3318 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003319 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3320 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3321 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003322 CameraMetadata(cloned));
3323 }
3324
3325 sp<Camera3Device> parent = mParent.promote();
3326 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003327 int32_t inputStreamId = -1;
3328 if (halRequest.input_buffer != nullptr) {
3329 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3330 }
3331
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003332 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003333 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003334 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3335 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003336 }
3337 }
3338
Shuzhen Wang83bff122020-11-20 15:51:39 -08003339 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003340 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003341 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003342 }
3343
Shuzhen Wang83bff122020-11-20 15:51:39 -08003344 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003345}
3346
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003347bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3348 ATRACE_CALL();
3349 bool updatesDetected = false;
3350
Emilian Peev4ec17882019-01-24 17:16:58 -08003351 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003352 for (auto tag : mSessionParamKeys) {
3353 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003354 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003355
3356 if (entry.count > 0) {
3357 bool isDifferent = false;
3358 if (lastEntry.count > 0) {
3359 // Have a last value, compare to see if changed
3360 if (lastEntry.type == entry.type &&
3361 lastEntry.count == entry.count) {
3362 // Same type and count, compare values
3363 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3364 size_t entryBytes = bytesPerValue * lastEntry.count;
3365 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3366 if (cmp != 0) {
3367 isDifferent = true;
3368 }
3369 } else {
3370 // Count or type has changed
3371 isDifferent = true;
3372 }
3373 } else {
3374 // No last entry, so always consider to be different
3375 isDifferent = true;
3376 }
3377
3378 if (isDifferent) {
3379 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003380 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3381 updatesDetected = true;
3382 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003383 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003384 }
3385 } else if (lastEntry.count > 0) {
3386 // Value has been removed
3387 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003388 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003389 updatesDetected = true;
3390 }
3391 }
3392
Emilian Peev4ec17882019-01-24 17:16:58 -08003393 bool reconfigureRequired;
3394 if (updatesDetected) {
3395 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3396 updatedParams);
3397 mLatestSessionParams = updatedParams;
3398 } else {
3399 reconfigureRequired = false;
3400 }
3401
3402 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003403}
3404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003405bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003406 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003407 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003408 // Any function called from threadLoop() must not hold mInterfaceLock since
3409 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3410 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003411
3412 // Handle paused state.
3413 if (waitIfPaused()) {
3414 return true;
3415 }
3416
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003417 // Wait for the next batch of requests.
3418 waitForNextRequestBatch();
3419 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003420 return true;
3421 }
3422
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003423 // Get the latest request ID, if any
3424 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003425 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003426 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003427 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003428 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003429 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003430 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3431 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003432 }
3433
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003434 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3435 // or a single request from streaming or burst. In either case the first element
3436 // should contain the latest camera settings that we need to check for any session
3437 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003438 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003439 res = OK;
3440
3441 //Input stream buffers are already acquired at this point so an input stream
3442 //will not be able to move to idle state unless we force it.
3443 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3444 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3445 if (res != OK) {
3446 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3447 cleanUpFailedRequests(/*sendRequestError*/ false);
3448 return false;
3449 }
3450 }
3451
3452 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003453 sp<Camera3Device> parent = mParent.promote();
3454 if (parent != nullptr) {
3455 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 Wang696e4da2022-09-08 14:31:13 -07003910 auto expectedDurationInfo = 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 Wang696e4da2022-09-08 14:31:13 -07003915 expectedDurationInfo.minDuration,
3916 expectedDurationInfo.maxDuration,
3917 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003918 requestedPhysicalCameras, isStillCapture, isZslCapture,
3919 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003920 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003921 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003922 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3923 ", burstId = %" PRId32 ".",
3924 __FUNCTION__,
3925 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3926 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003927
3928 if (shouldUnlockSettings) {
3929 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3930 }
3931
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003932 if (res != OK) {
3933 SET_ERR("RequestThread: Unable to register new in-flight request:"
3934 " %s (%d)", strerror(-res), res);
3935 return INVALID_OPERATION;
3936 }
3937 }
3938
3939 return OK;
3940}
3941
Igor Murashkin1e479c02013-09-06 16:55:14 -07003942CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003943 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003944 Mutex::Autolock al(mLatestRequestMutex);
3945
3946 ALOGV("RequestThread::%s", __FUNCTION__);
3947
3948 return mLatestRequest;
3949}
3950
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003951bool Camera3Device::RequestThread::isStreamPending(
3952 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003953 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003954 Mutex::Autolock l(mRequestLock);
3955
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003956 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003957 if (!nextRequest.submitted) {
3958 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3959 if (stream == s) return true;
3960 }
3961 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003962 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003963 }
3964
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003965 for (const auto& request : mRequestQueue) {
3966 for (const auto& s : request->mOutputStreams) {
3967 if (stream == s) return true;
3968 }
3969 if (stream == request->mInputStream) return true;
3970 }
3971
3972 for (const auto& request : mRepeatingRequests) {
3973 for (const auto& s : request->mOutputStreams) {
3974 if (stream == s) return true;
3975 }
3976 if (stream == request->mInputStream) return true;
3977 }
3978
3979 return false;
3980}
Jianing Weicb0652e2014-03-12 18:29:36 -07003981
Emilian Peev40ead602017-09-26 15:46:36 +01003982bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3983 ATRACE_CALL();
3984 Mutex::Autolock l(mRequestLock);
3985
3986 for (const auto& nextRequest : mNextRequests) {
3987 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3988 if (s.first == streamId) {
3989 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3990 if (it != s.second.end()) {
3991 return true;
3992 }
3993 }
3994 }
3995 }
3996
3997 for (const auto& request : mRequestQueue) {
3998 for (const auto& s : request->mOutputSurfaces) {
3999 if (s.first == streamId) {
4000 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4001 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004002 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004003 }
4004 }
4005 }
4006 }
4007
4008 for (const auto& request : mRepeatingRequests) {
4009 for (const auto& s : request->mOutputSurfaces) {
4010 if (s.first == streamId) {
4011 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4012 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004013 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004014 }
4015 }
4016 }
4017 }
4018
4019 return false;
4020}
4021
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004022void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4023 if (!mUseHalBufManager) {
4024 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4025 return;
4026 }
4027
4028 Mutex::Autolock pl(mPauseLock);
4029 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004030 mInterface->signalPipelineDrain(streamIds);
4031 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004032 }
4033 // If request thread is still busy, wait until paused then notify HAL
4034 mNotifyPipelineDrain = true;
4035 mStreamIdsToBeDrained = streamIds;
4036}
4037
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004038void Camera3Device::RequestThread::resetPipelineDrain() {
4039 Mutex::Autolock pl(mPauseLock);
4040 mNotifyPipelineDrain = false;
4041 mStreamIdsToBeDrained.clear();
4042}
4043
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004044void Camera3Device::RequestThread::clearPreviousRequest() {
4045 Mutex::Autolock l(mRequestLock);
4046 mPrevRequest.clear();
4047}
4048
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004049status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4050 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4051 ATRACE_CALL();
4052 Mutex::Autolock l(mTriggerMutex);
4053 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4054 return BAD_VALUE;
4055 }
4056 mRotateAndCropOverride = rotateAndCropValue;
4057 return OK;
4058}
4059
Emilian Peeve23f1d92021-09-20 14:56:01 -07004060status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4061 ATRACE_CALL();
4062 Mutex::Autolock l(mTriggerMutex);
4063 mComposerOutput = composerSurfacePresent;
4064 return OK;
4065}
4066
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004067status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004068 ATRACE_CALL();
4069 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004070 if (muteMode != mCameraMute) {
4071 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004072 mCameraMuteChanged = true;
4073 }
4074 return OK;
4075}
4076
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004077nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004078 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004079 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004080 return mExpectedInflightDuration > kMinInflightDuration ?
4081 mExpectedInflightDuration : kMinInflightDuration;
4082}
4083
Emilian Peevaebbe412018-01-15 13:53:24 +00004084void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004085 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004086 if ((request == nullptr) || (halRequest == nullptr)) {
4087 ALOGE("%s: Invalid request!", __FUNCTION__);
4088 return;
4089 }
4090
4091 if (halRequest->num_physcam_settings > 0) {
4092 if (halRequest->physcam_id != nullptr) {
4093 delete [] halRequest->physcam_id;
4094 halRequest->physcam_id = nullptr;
4095 }
4096 if (halRequest->physcam_settings != nullptr) {
4097 auto it = ++(request->mSettingsList.begin());
4098 size_t i = 0;
4099 for (; it != request->mSettingsList.end(); it++, i++) {
4100 it->metadata.unlock(halRequest->physcam_settings[i]);
4101 }
4102 delete [] halRequest->physcam_settings;
4103 halRequest->physcam_settings = nullptr;
4104 }
4105 }
4106}
4107
Ravneet74cd3732022-03-30 05:33:03 +00004108status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4109 Mutex::Autolock il(mInterfaceLock);
4110 Mutex::Autolock l(mLock);
4111
4112 if (mCameraServiceWatchdog != NULL) {
4113 mCameraServiceWatchdog->setEnabled(enabled);
4114 }
4115
4116 return OK;
4117}
4118
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004119void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4120 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004121 return;
4122 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004123
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004124 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004125 // Skip the ones that have been submitted successfully.
4126 if (nextRequest.submitted) {
4127 continue;
4128 }
4129
4130 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004131 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4132 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004133
4134 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004135 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004136 }
4137
Emilian Peevaebbe412018-01-15 13:53:24 +00004138 cleanupPhysicalSettings(captureRequest, halRequest);
4139
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004140 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004141 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004142 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4143 }
4144
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004145 // No output buffer can be returned when using HAL buffer manager
4146 if (!mUseHalBufManager) {
4147 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4148 //Buffers that failed processing could still have
4149 //valid acquire fence.
4150 int acquireFence = (*outputBuffers)[i].acquire_fence;
4151 if (0 <= acquireFence) {
4152 close(acquireFence);
4153 outputBuffers->editItemAt(i).acquire_fence = -1;
4154 }
Emilian Peevf4816702020-04-03 15:44:51 -07004155 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004156 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4157 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004158 /*timestampIncreasing*/true, std::vector<size_t> (),
4159 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004160 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004161 }
4162
4163 if (sendRequestError) {
4164 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004165 sp<NotificationListener> listener = mListener.promote();
4166 if (listener != NULL) {
4167 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004168 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004169 captureRequest->mResultExtras);
4170 }
4171 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004172
4173 // Remove yet-to-be submitted inflight request from inflightMap
4174 {
4175 sp<Camera3Device> parent = mParent.promote();
4176 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004177 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004178 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4179 if (idx >= 0) {
4180 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4181 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4182 parent->removeInFlightMapEntryLocked(idx);
4183 }
4184 }
4185 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004186 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004187
4188 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004189 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004190}
4191
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004192void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004193 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004194 // Optimized a bit for the simple steady-state case (single repeating
4195 // request), to avoid putting that request in the queue temporarily.
4196 Mutex::Autolock l(mRequestLock);
4197
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004198 assert(mNextRequests.empty());
4199
4200 NextRequest nextRequest;
4201 nextRequest.captureRequest = waitForNextRequestLocked();
4202 if (nextRequest.captureRequest == nullptr) {
4203 return;
4204 }
4205
Emilian Peevf4816702020-04-03 15:44:51 -07004206 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004207 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004208 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004209
4210 // Wait for additional requests
4211 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4212
4213 for (size_t i = 1; i < batchSize; i++) {
4214 NextRequest additionalRequest;
4215 additionalRequest.captureRequest = waitForNextRequestLocked();
4216 if (additionalRequest.captureRequest == nullptr) {
4217 break;
4218 }
4219
Emilian Peevf4816702020-04-03 15:44:51 -07004220 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004221 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004222 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004223 }
4224
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004225 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004226 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004227 mNextRequests.size(), batchSize);
4228 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004229 }
4230
4231 return;
4232}
4233
4234sp<Camera3Device::CaptureRequest>
4235 Camera3Device::RequestThread::waitForNextRequestLocked() {
4236 status_t res;
4237 sp<CaptureRequest> nextRequest;
4238
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004239 while (mRequestQueue.empty()) {
4240 if (!mRepeatingRequests.empty()) {
4241 // Always atomically enqueue all requests in a repeating request
4242 // list. Guarantees a complete in-sequence set of captures to
4243 // application.
4244 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004245 if (mFirstRepeating) {
4246 mFirstRepeating = false;
4247 } else {
4248 for (auto& request : requests) {
4249 // For repeating requests, override timestamp request using
4250 // the time a request is inserted into the request queue,
4251 // because the original repeating request will have an old
4252 // fixed timestamp.
4253 request->mRequestTimeNs = systemTime();
4254 }
4255 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004256 RequestList::const_iterator firstRequest =
4257 requests.begin();
4258 nextRequest = *firstRequest;
4259 mRequestQueue.insert(mRequestQueue.end(),
4260 ++firstRequest,
4261 requests.end());
4262 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004263
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004264 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004265
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004266 break;
4267 }
4268
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004269 if (!mRequestClearing) {
4270 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004273 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4274 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004275 Mutex::Autolock pl(mPauseLock);
4276 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004277 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004278 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004279 if (mNotifyPipelineDrain) {
4280 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4281 mNotifyPipelineDrain = false;
4282 mStreamIdsToBeDrained.clear();
4283 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004284 // Let the tracker know
4285 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4286 if (statusTracker != 0) {
4287 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4288 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004289 sp<Camera3Device> parent = mParent.promote();
4290 if (parent != nullptr) {
4291 parent->mRequestBufferSM.onRequestThreadPaused();
4292 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004293 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004294 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004295 // Stop waiting for now and let thread management happen
4296 return NULL;
4297 }
4298 }
4299
4300 if (nextRequest == NULL) {
4301 // Don't have a repeating request already in hand, so queue
4302 // must have an entry now.
4303 RequestList::iterator firstRequest =
4304 mRequestQueue.begin();
4305 nextRequest = *firstRequest;
4306 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004307 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4308 sp<NotificationListener> listener = mListener.promote();
4309 if (listener != NULL) {
4310 listener->notifyRequestQueueEmpty();
4311 }
4312 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004313 }
4314
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004315 // In case we've been unpaused by setPaused clearing mDoPause, need to
4316 // update internal pause state (capture/setRepeatingRequest unpause
4317 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004318 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004319 if (mPaused) {
4320 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4321 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4322 if (statusTracker != 0) {
4323 statusTracker->markComponentActive(mStatusId);
4324 }
4325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004326 mPaused = false;
4327
4328 // Check if we've reconfigured since last time, and reset the preview
4329 // request if so. Can't use 'NULL request == repeat' across configure calls.
4330 if (mReconfigured) {
4331 mPrevRequest.clear();
4332 mReconfigured = false;
4333 }
4334
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004335 if (nextRequest != NULL) {
4336 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004337 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4338 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004339
4340 // Since RequestThread::clear() removes buffers from the input stream,
4341 // get the right buffer here before unlocking mRequestLock
4342 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004343 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4344 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004345 if (res != OK) {
4346 // Can't get input buffer from gralloc queue - this could be due to
4347 // disconnected queue or other producer misbehavior, so not a fatal
4348 // error
4349 ALOGE("%s: Can't get input buffer, skipping request:"
4350 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004351
4352 sp<NotificationListener> listener = mListener.promote();
4353 if (listener != NULL) {
4354 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004355 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004356 nextRequest->mResultExtras);
4357 }
4358 return NULL;
4359 }
4360 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004361 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004362
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004363 return nextRequest;
4364}
4365
4366bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004367 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004368 status_t res;
4369 Mutex::Autolock l(mPauseLock);
4370 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004371 if (mPaused == false) {
4372 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004373 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004374 if (mNotifyPipelineDrain) {
4375 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4376 mNotifyPipelineDrain = false;
4377 mStreamIdsToBeDrained.clear();
4378 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004379 // Let the tracker know
4380 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4381 if (statusTracker != 0) {
4382 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4383 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004384 sp<Camera3Device> parent = mParent.promote();
4385 if (parent != nullptr) {
4386 parent->mRequestBufferSM.onRequestThreadPaused();
4387 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004388 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004389
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004390 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004391 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004392 return true;
4393 }
4394 }
4395 // We don't set mPaused to false here, because waitForNextRequest needs
4396 // to further manage the paused state in case of starvation.
4397 return false;
4398}
4399
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004400void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004401 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004402 // With work to do, mark thread as unpaused.
4403 // If paused by request (setPaused), don't resume, to avoid
4404 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004405 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004406 Mutex::Autolock p(mPauseLock);
4407 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004408 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4409 if (mPaused) {
4410 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4411 if (statusTracker != 0) {
4412 statusTracker->markComponentActive(mStatusId);
4413 }
4414 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004415 mPaused = false;
4416 }
4417}
4418
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004419void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4420 sp<Camera3Device> parent = mParent.promote();
4421 if (parent != NULL) {
4422 va_list args;
4423 va_start(args, fmt);
4424
4425 parent->setErrorStateV(fmt, args);
4426
4427 va_end(args);
4428 }
4429}
4430
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004431status_t Camera3Device::RequestThread::insertTriggers(
4432 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004433 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004434 Mutex::Autolock al(mTriggerMutex);
4435
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004436 sp<Camera3Device> parent = mParent.promote();
4437 if (parent == NULL) {
4438 CLOGE("RequestThread: Parent is gone");
4439 return DEAD_OBJECT;
4440 }
4441
Emilian Peevaebbe412018-01-15 13:53:24 +00004442 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004443 size_t count = mTriggerMap.size();
4444
4445 for (size_t i = 0; i < count; ++i) {
4446 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004447 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004448
4449 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4450 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4451 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004452 if (isAeTrigger) {
4453 request->mResultExtras.precaptureTriggerId = triggerId;
4454 mCurrentPreCaptureTriggerId = triggerId;
4455 } else {
4456 request->mResultExtras.afTriggerId = triggerId;
4457 mCurrentAfTriggerId = triggerId;
4458 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004459 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004460 }
4461
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004462 camera_metadata_entry entry = metadata.find(tag);
4463
4464 if (entry.count > 0) {
4465 /**
4466 * Already has an entry for this trigger in the request.
4467 * Rewrite it with our requested trigger value.
4468 */
4469 RequestTrigger oldTrigger = trigger;
4470
4471 oldTrigger.entryValue = entry.data.u8[0];
4472
4473 mTriggerReplacedMap.add(tag, oldTrigger);
4474 } else {
4475 /**
4476 * More typical, no trigger entry, so we just add it
4477 */
4478 mTriggerRemovedMap.add(tag, trigger);
4479 }
4480
4481 status_t res;
4482
4483 switch (trigger.getTagType()) {
4484 case TYPE_BYTE: {
4485 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4486 res = metadata.update(tag,
4487 &entryValue,
4488 /*count*/1);
4489 break;
4490 }
4491 case TYPE_INT32:
4492 res = metadata.update(tag,
4493 &trigger.entryValue,
4494 /*count*/1);
4495 break;
4496 default:
4497 ALOGE("%s: Type not supported: 0x%x",
4498 __FUNCTION__,
4499 trigger.getTagType());
4500 return INVALID_OPERATION;
4501 }
4502
4503 if (res != OK) {
4504 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4505 ", value %d", __FUNCTION__, trigger.getTagName(),
4506 trigger.entryValue);
4507 return res;
4508 }
4509
4510 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4511 trigger.getTagName(),
4512 trigger.entryValue);
4513 }
4514
4515 mTriggerMap.clear();
4516
4517 return count;
4518}
4519
4520status_t Camera3Device::RequestThread::removeTriggers(
4521 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004522 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004523 Mutex::Autolock al(mTriggerMutex);
4524
Emilian Peevaebbe412018-01-15 13:53:24 +00004525 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004526
4527 /**
4528 * Replace all old entries with their old values.
4529 */
4530 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4531 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4532
4533 status_t res;
4534
4535 uint32_t tag = trigger.metadataTag;
4536 switch (trigger.getTagType()) {
4537 case TYPE_BYTE: {
4538 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4539 res = metadata.update(tag,
4540 &entryValue,
4541 /*count*/1);
4542 break;
4543 }
4544 case TYPE_INT32:
4545 res = metadata.update(tag,
4546 &trigger.entryValue,
4547 /*count*/1);
4548 break;
4549 default:
4550 ALOGE("%s: Type not supported: 0x%x",
4551 __FUNCTION__,
4552 trigger.getTagType());
4553 return INVALID_OPERATION;
4554 }
4555
4556 if (res != OK) {
4557 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4558 ", trigger value %d", __FUNCTION__,
4559 trigger.getTagName(), trigger.entryValue);
4560 return res;
4561 }
4562 }
4563 mTriggerReplacedMap.clear();
4564
4565 /**
4566 * Remove all new entries.
4567 */
4568 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4569 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4570 status_t res = metadata.erase(trigger.metadataTag);
4571
4572 if (res != OK) {
4573 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4574 ", trigger value %d", __FUNCTION__,
4575 trigger.getTagName(), trigger.entryValue);
4576 return res;
4577 }
4578 }
4579 mTriggerRemovedMap.clear();
4580
4581 return OK;
4582}
4583
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004584status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004585 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004586 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004587 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004588 status_t res;
4589
Emilian Peevaebbe412018-01-15 13:53:24 +00004590 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004591
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004592 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004593 // exists
4594 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4595 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4596 if (afTrigger.count > 0 &&
4597 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4598 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004599 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004600 if (res != OK) return res;
4601 }
4602
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004603 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004604 // if none already exists
4605 camera_metadata_entry pcTrigger =
4606 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4607 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4608 if (pcTrigger.count > 0 &&
4609 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4610 pcId.count == 0) {
4611 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004612 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004613 if (res != OK) return res;
4614 }
4615
4616 return OK;
4617}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004618
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004619bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4620 const sp<CaptureRequest> &request) {
4621 ATRACE_CALL();
4622
4623 if (request->mRotateAndCropAuto) {
4624 Mutex::Autolock l(mTriggerMutex);
4625 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4626
4627 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4628 if (rotateAndCropEntry.count > 0) {
4629 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4630 return false;
4631 } else {
4632 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4633 return true;
4634 }
4635 } else {
4636 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4637 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4638 &rotateAndCrop_u8, 1);
4639 return true;
4640 }
4641 }
4642 return false;
4643}
4644
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004645bool Camera3Device::RequestThread::overrideTestPattern(
4646 const sp<CaptureRequest> &request) {
4647 ATRACE_CALL();
4648
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004649 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004650
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004651 Mutex::Autolock l(mTriggerMutex);
4652
4653 bool changed = false;
4654
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004655 // For a multi-camera, the physical cameras support the same set of
4656 // test pattern modes as the logical camera.
4657 for (auto& settings : request->mSettingsList) {
4658 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004659
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004660 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4661 int32_t testPatternData[4] = {
4662 settings.mOriginalTestPatternData[0],
4663 settings.mOriginalTestPatternData[1],
4664 settings.mOriginalTestPatternData[2],
4665 settings.mOriginalTestPatternData[3]
4666 };
4667 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4668 testPatternMode = mCameraMute;
4669 testPatternData[0] = 0;
4670 testPatternData[1] = 0;
4671 testPatternData[2] = 0;
4672 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004673 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004674
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004675 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4676 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4677 if (testPatternEntry.count > 0) {
4678 if (testPatternEntry.data.i32[0] != testPatternMode) {
4679 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004680 changed = true;
4681 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004682 } else if (supportTestPatternModeKey) {
4683 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4684 &testPatternMode, 1);
4685 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004686 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004687
4688 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4689 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4690 if (testPatternColor.count >= 4) {
4691 for (size_t i = 0; i < 4; i++) {
4692 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4693 testPatternColor.data.i32[i] = testPatternData[i];
4694 changed = true;
4695 }
4696 }
4697 } else if (supportTestPatternDataKey) {
4698 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4699 testPatternData, 4);
4700 changed = true;
4701 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004702 }
4703
4704 return changed;
4705}
4706
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004707status_t Camera3Device::RequestThread::setHalInterface(
4708 sp<HalInterface> newHalInterface) {
4709 if (newHalInterface.get() == nullptr) {
4710 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4711 return DEAD_OBJECT;
4712 }
4713
4714 mInterface = newHalInterface;
4715
4716 return OK;
4717}
4718
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004719/**
4720 * PreparerThread inner class methods
4721 */
4722
4723Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004724 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004725 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004726}
4727
4728Camera3Device::PreparerThread::~PreparerThread() {
4729 Thread::requestExitAndWait();
4730 if (mCurrentStream != nullptr) {
4731 mCurrentStream->cancelPrepare();
4732 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4733 mCurrentStream.clear();
4734 }
4735 clear();
4736}
4737
Ruben Brunkc78ac262015-08-13 17:58:46 -07004738status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004739 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004740 status_t res;
4741
4742 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004743 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004744
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004745 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004746 if (res == OK) {
4747 // No preparation needed, fire listener right off
4748 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004749 if (listener != NULL) {
4750 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004751 }
4752 return OK;
4753 } else if (res != NOT_ENOUGH_DATA) {
4754 return res;
4755 }
4756
4757 // Need to prepare, start up thread if necessary
4758 if (!mActive) {
4759 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4760 // isn't running
4761 Thread::requestExitAndWait();
4762 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4763 if (res != OK) {
4764 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004765 if (listener != NULL) {
4766 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004767 }
4768 return res;
4769 }
4770 mCancelNow = false;
4771 mActive = true;
4772 ALOGV("%s: Preparer stream started", __FUNCTION__);
4773 }
4774
4775 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004776 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004777 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4778
4779 return OK;
4780}
4781
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004782void Camera3Device::PreparerThread::pause() {
4783 ATRACE_CALL();
4784
4785 Mutex::Autolock l(mLock);
4786
4787 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4788 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4789 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4790 int currentMaxCount = mCurrentMaxCount;
4791 mPendingStreams.clear();
4792 mCancelNow = true;
4793 while (mActive) {
4794 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4795 if (res == TIMED_OUT) {
4796 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4797 return;
4798 } else if (res != OK) {
4799 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4800 return;
4801 }
4802 }
4803
4804 //Check whether the prepare thread was able to complete the current
4805 //stream. In case work is still pending emplace it along with the rest
4806 //of the streams in the pending list.
4807 if (currentStream != nullptr) {
4808 if (!mCurrentPrepareComplete) {
4809 pendingStreams.emplace(currentMaxCount, currentStream);
4810 }
4811 }
4812
4813 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4814 for (const auto& it : mPendingStreams) {
4815 it.second->cancelPrepare();
4816 }
4817}
4818
4819status_t Camera3Device::PreparerThread::resume() {
4820 ATRACE_CALL();
4821 status_t res;
4822
4823 Mutex::Autolock l(mLock);
4824 sp<NotificationListener> listener = mListener.promote();
4825
4826 if (mActive) {
4827 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4828 return NO_INIT;
4829 }
4830
4831 auto it = mPendingStreams.begin();
4832 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004833 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004834 if (res == OK) {
4835 if (listener != NULL) {
4836 listener->notifyPrepared(it->second->getId());
4837 }
4838 it = mPendingStreams.erase(it);
4839 } else if (res != NOT_ENOUGH_DATA) {
4840 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4841 res, strerror(-res));
4842 it = mPendingStreams.erase(it);
4843 } else {
4844 it++;
4845 }
4846 }
4847
4848 if (mPendingStreams.empty()) {
4849 return OK;
4850 }
4851
4852 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4853 if (res != OK) {
4854 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4855 __FUNCTION__, res, strerror(-res));
4856 return res;
4857 }
4858 mCancelNow = false;
4859 mActive = true;
4860 ALOGV("%s: Preparer stream started", __FUNCTION__);
4861
4862 return OK;
4863}
4864
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004865status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004866 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004867 Mutex::Autolock l(mLock);
4868
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004869 for (const auto& it : mPendingStreams) {
4870 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004871 }
4872 mPendingStreams.clear();
4873 mCancelNow = true;
4874
4875 return OK;
4876}
4877
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004878void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004879 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004880 Mutex::Autolock l(mLock);
4881 mListener = listener;
4882}
4883
4884bool Camera3Device::PreparerThread::threadLoop() {
4885 status_t res;
4886 {
4887 Mutex::Autolock l(mLock);
4888 if (mCurrentStream == nullptr) {
4889 // End thread if done with work
4890 if (mPendingStreams.empty()) {
4891 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4892 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4893 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4894 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004895 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004896 return false;
4897 }
4898
4899 // Get next stream to prepare
4900 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004901 mCurrentStream = it->second;
4902 mCurrentMaxCount = it->first;
4903 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004904 mPendingStreams.erase(it);
4905 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4906 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4907 } else if (mCancelNow) {
4908 mCurrentStream->cancelPrepare();
4909 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4910 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4911 mCurrentStream.clear();
4912 mCancelNow = false;
4913 return true;
4914 }
4915 }
4916
4917 res = mCurrentStream->prepareNextBuffer();
4918 if (res == NOT_ENOUGH_DATA) return true;
4919 if (res != OK) {
4920 // Something bad happened; try to recover by cancelling prepare and
4921 // signalling listener anyway
4922 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4923 mCurrentStream->getId(), res, strerror(-res));
4924 mCurrentStream->cancelPrepare();
4925 }
4926
4927 // This stream has finished, notify listener
4928 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004929 sp<NotificationListener> listener = mListener.promote();
4930 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004931 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4932 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004933 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004934 }
4935
4936 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4937 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004938 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004939
4940 return true;
4941}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004942
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004943status_t Camera3Device::RequestBufferStateMachine::initialize(
4944 sp<camera3::StatusTracker> statusTracker) {
4945 if (statusTracker == nullptr) {
4946 ALOGE("%s: statusTracker is null", __FUNCTION__);
4947 return BAD_VALUE;
4948 }
4949
4950 std::lock_guard<std::mutex> lock(mLock);
4951 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004952 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004953 return OK;
4954}
4955
4956bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4957 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004958 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004959 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004960 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004961 return true;
4962 }
4963 return false;
4964}
4965
4966void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4967 std::lock_guard<std::mutex> lock(mLock);
4968 if (!mRequestBufferOngoing) {
4969 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4970 return;
4971 }
4972 mRequestBufferOngoing = false;
4973 if (mStatus == RB_STATUS_PENDING_STOP) {
4974 checkSwitchToStopLocked();
4975 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004976 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004977}
4978
4979void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4980 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004981 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004982 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004983 return;
4984}
4985
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004986void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004987 std::lock_guard<std::mutex> lock(mLock);
4988 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004989 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4990 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004991 mInflightMapEmpty = false;
4992 if (mStatus == RB_STATUS_STOPPED) {
4993 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004994 }
4995 return;
4996}
4997
4998void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
4999 std::lock_guard<std::mutex> lock(mLock);
5000 mRequestThreadPaused = true;
5001 if (mStatus == RB_STATUS_PENDING_STOP) {
5002 checkSwitchToStopLocked();
5003 }
5004 return;
5005}
5006
5007void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5008 std::lock_guard<std::mutex> lock(mLock);
5009 mInflightMapEmpty = true;
5010 if (mStatus == RB_STATUS_PENDING_STOP) {
5011 checkSwitchToStopLocked();
5012 }
5013 return;
5014}
5015
5016void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5017 std::lock_guard<std::mutex> lock(mLock);
5018 if (!checkSwitchToStopLocked()) {
5019 mStatus = RB_STATUS_PENDING_STOP;
5020 }
5021 return;
5022}
5023
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005024bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5025 std::lock_guard<std::mutex> lock(mLock);
5026 if (mRequestBufferOngoing) {
5027 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5028 __FUNCTION__);
5029 return false;
5030 }
5031 mSwitchedToOffline = true;
5032 mInflightMapEmpty = true;
5033 mRequestThreadPaused = true;
5034 mStatus = RB_STATUS_STOPPED;
5035 return true;
5036}
5037
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005038void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5039 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5040 if (statusTracker != nullptr) {
5041 if (active) {
5042 statusTracker->markComponentActive(mRequestBufferStatusId);
5043 } else {
5044 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5045 }
5046 }
5047}
5048
5049bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5050 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5051 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005052 return true;
5053 }
5054 return false;
5055}
5056
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005057bool Camera3Device::startRequestBuffer() {
5058 return mRequestBufferSM.startRequestBuffer();
5059}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005060
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005061void Camera3Device::endRequestBuffer() {
5062 mRequestBufferSM.endRequestBuffer();
5063}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005064
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005065nsecs_t Camera3Device::getWaitDuration() {
5066 return kBaseGetBufferWait + getExpectedInFlightDuration();
5067}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005068
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005069void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5070 mInterface->getInflightBufferKeys(out);
5071}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005072
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005073void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5074 mInterface->getInflightRequestBufferKeys(out);
5075}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005076
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005077std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5078 std::vector<sp<Camera3StreamInterface>> ret;
5079 bool hasInputStream = mInputStream != nullptr;
5080 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5081 if (hasInputStream) {
5082 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005083 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005084 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5085 ret.push_back(mOutputStreams[i]);
5086 }
5087 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5088 ret.push_back(mDeletedStreams[i]);
5089 }
5090 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005091}
5092
Emilian Peevcc0b7952020-01-07 13:54:47 -08005093void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5094 ATRACE_CALL();
5095
5096 if (offlineStreamIds == nullptr) {
5097 return;
5098 }
5099
5100 Mutex::Autolock il(mInterfaceLock);
5101
5102 auto streamIds = mOutputStreams.getStreamIds();
5103 bool hasInputStream = mInputStream != nullptr;
5104 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5105 offlineStreamIds->push_back(mInputStream->getId());
5106 }
5107
5108 for (const auto & streamId : streamIds) {
5109 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5110 // Streams that use the camera buffer manager are currently not supported in
5111 // offline mode
5112 if (stream->getOfflineProcessingSupport() &&
5113 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5114 offlineStreamIds->push_back(streamId);
5115 }
5116 }
5117}
5118
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005119status_t Camera3Device::setRotateAndCropAutoBehavior(
5120 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5121 ATRACE_CALL();
5122 Mutex::Autolock il(mInterfaceLock);
5123 Mutex::Autolock l(mLock);
5124 if (mRequestThread == nullptr) {
5125 return INVALID_OPERATION;
5126 }
5127 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5128}
5129
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005130bool Camera3Device::supportsCameraMute() {
5131 Mutex::Autolock il(mInterfaceLock);
5132 Mutex::Autolock l(mLock);
5133
5134 return mSupportCameraMute;
5135}
5136
5137status_t Camera3Device::setCameraMute(bool enabled) {
5138 ATRACE_CALL();
5139 Mutex::Autolock il(mInterfaceLock);
5140 Mutex::Autolock l(mLock);
5141
5142 if (mRequestThread == nullptr || !mSupportCameraMute) {
5143 return INVALID_OPERATION;
5144 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005145 int32_t muteMode =
5146 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5147 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5148 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5149 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005150}
5151
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005152status_t Camera3Device::injectCamera(const String8& injectedCamId,
5153 sp<CameraProviderManager> manager) {
5154 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5155 ATRACE_CALL();
5156 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005157 // When the camera device is active, injectCamera() and stopInjection() will call
5158 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5159 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5160 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5161 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5162 // waitUntilStateThenRelock().
5163 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005164
5165 status_t res = NO_ERROR;
5166 if (mInjectionMethods->isInjecting()) {
5167 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5168 return OK;
5169 } else {
5170 res = mInjectionMethods->stopInjection();
5171 if (res != OK) {
5172 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5173 __FUNCTION__, res);
5174 return res;
5175 }
5176 }
5177 }
5178
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005179 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005180 if (res != OK) {
5181 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5182 __FUNCTION__, res);
5183 return res;
5184 }
5185
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005186 // When the second display of android is cast to the remote device, and the opened camera is
5187 // also cast to the second display, in this case, because the camera has configured the streams
5188 // at this time, we can directly call injectCamera() to replace the internal camera with
5189 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005190 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5191 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5192
5193 camera3::camera_stream_configuration injectionConfig;
5194 std::vector<uint32_t> injectionBufferSizes;
5195 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5196 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5197 || injectionBufferSizes.size() <= 0) {
5198 ALOGE("Failed to inject camera due to abandoned configuration! "
5199 "mOperatingMode: %d injectionConfig.num_streams: %d "
5200 "injectionBufferSizes.size(): %zu", mOperatingMode,
5201 injectionConfig.num_streams, injectionBufferSizes.size());
5202 return DEAD_OBJECT;
5203 }
5204
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005205 res = mInjectionMethods->injectCamera(
5206 injectionConfig, injectionBufferSizes);
5207 if (res != OK) {
5208 ALOGE("Can't finish inject camera process!");
5209 return res;
5210 }
5211 }
5212
5213 return OK;
5214}
5215
5216status_t Camera3Device::stopInjection() {
5217 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5218 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005219 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005220 return mInjectionMethods->stopInjection();
5221}
5222
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005223}; // namespace android