blob: dcafd740f3fce695af2b61cc610a0ecef9d4e333 [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
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Emilian Peeve579d8b2023-02-28 14:16:08 -080056#include "aidl/android/hardware/graphics/common/Dataspace.h"
Emilian Peeva1b26262023-02-06 17:27:41 -080057#include "aidl/AidlUtils.h"
58#include "device3/Camera3Device.h"
59#include "device3/Camera3FakeStream.h"
60#include "device3/Camera3InputStream.h"
61#include "device3/Camera3OutputStream.h"
62#include "device3/Camera3SharedOutputStream.h"
63#include "mediautils/SchedulingPolicyService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070064#include "utils/CameraThreadState.h"
Emilian Peeva1b26262023-02-06 17:27:41 -080065#include "utils/CameraTraces.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080066#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070067#include "utils/TraceHFR.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080068
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080069#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080070#include <tuple>
71
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080072using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080073using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074
75namespace android {
76
Austin Borger74fca042022-05-23 12:41:21 -070077Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
Austin Borger18b30a72022-10-27 12:20:29 -070078 const String8 &id, bool overrideForPerfClass, bool overrideToPortrait, bool legacyClient):
Austin Borger74fca042022-05-23 12:41:21 -070079 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080080 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070081 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080082 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070083 mIsConstrainedHighSpeedConfiguration(false),
Emilian Peeve579d8b2023-02-28 14:16:08 -080084 mSupportNativeJpegR(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070085 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070086 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070087 mUsePartialResult(false),
88 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080089 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080090 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070092 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070094 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070095 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070096 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000097 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010098 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070099 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700100 mNeedFixupMonochromeTags(false),
Austin Borger18b30a72022-10-27 12:20:29 -0700101 mOverrideForPerfClass(overrideForPerfClass),
Austin Borgerc8099762023-01-12 17:08:46 -0800102 mOverrideToPortrait(overrideToPortrait),
Emilian Peevd865f0d2023-03-17 17:13:07 -0700103 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
104 mComposerOutput(false),
105 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700106 mSettingsOverride(-1),
Austin Borgerc8099762023-01-12 17:08:46 -0800107 mActivePhysicalId("")
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108{
109 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800110 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800111}
112
113Camera3Device::~Camera3Device()
114{
115 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800116 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700117 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118}
119
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800121 return mId;
122}
123
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800124status_t Camera3Device::initializeCommonLocked() {
125
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700126 /** Start up status tracker thread */
127 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800128 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700129 if (res != OK) {
130 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
131 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800132 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700133 mStatusTracker.clear();
134 return res;
135 }
136
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700137 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700138 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700139
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700140 if (mUseHalBufManager) {
141 res = mRequestBufferSM.initialize(mStatusTracker);
142 if (res != OK) {
143 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
144 strerror(-res), res);
145 mInterface->close();
146 mStatusTracker.clear();
147 return res;
148 }
149 }
150
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800151 /** Create buffer manager */
152 mBufferManager = new Camera3BufferManager();
153
154 Vector<int32_t> sessionParamKeys;
155 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
156 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
157 if (sessionKeysEntry.count > 0) {
158 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
159 }
160
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700161 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
162 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
163 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
164 if (availableTestPatternModes.data.i32[i] ==
165 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
166 mSupportCameraMute = true;
167 mSupportTestPatternSolidColor = true;
168 break;
169 } else if (availableTestPatternModes.data.i32[i] ==
170 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
171 mSupportCameraMute = true;
172 mSupportTestPatternSolidColor = false;
173 }
174 }
175
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700176 camera_metadata_entry_t availableSettingsOverrides = mDeviceInfo.find(
177 ANDROID_CONTROL_AVAILABLE_SETTINGS_OVERRIDES);
178 for (size_t i = 0; i < availableSettingsOverrides.count; i++) {
179 if (availableSettingsOverrides.data.i32[i] ==
180 ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM) {
181 mSupportZoomOverride = true;
182 break;
183 }
184 }
185
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700186 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800187 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700188 this, mStatusTracker, mInterface, sessionParamKeys,
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700189 mUseHalBufManager, mSupportCameraMute, mOverrideToPortrait,
190 mSupportZoomOverride);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800191 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700193 SET_ERR_L("Unable to start request queue thread: %s (%d)",
194 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800195 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800196 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 return res;
198 }
199
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700200 mPreparerThread = new PreparerThread();
201
Ruben Brunk183f0562015-08-12 12:55:02 -0700202 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800203 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400204 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700205 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700206 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800207 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800210 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800211 camera_metadata_entry timestampSource =
212 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
213 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
214 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800215 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800216 }
217
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700218 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700224 }
225
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800226 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
227 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700228 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700229 if (res != OK) {
230 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
231 return res;
232 }
233 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800234
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800235 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800236 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800237
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800238 if (SessionConfigurationUtils::supportsUltraHighResolutionCapture(mDeviceInfo)) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700239 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
240 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
241 }
242
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800243 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
244 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
245 }
246
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800247 // Hidl/AidlCamera3DeviceInjectionMethods
248 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800249
Ravneetaeb20dc2022-03-30 05:33:03 +0000250 /** Start watchdog thread */
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800251 mCameraServiceWatchdog = new CameraServiceWatchdog(mId, mCameraServiceProxyWrapper);
Austin Borger7b129542022-06-09 13:23:06 -0700252 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
253 if (res != OK) {
254 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
255 strerror(-res), res);
256 return res;
257 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000258
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800259 return OK;
260}
261
262status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700263 return disconnectImpl();
264}
265
266status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800267 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700268 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700271 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800273 Mutex::Autolock il(mInterfaceLock);
274 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
275 {
276 Mutex::Autolock l(mLock);
277 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000279 if (mRequestThread != NULL) {
280 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
281 res = mRequestThread->clear();
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800282 if (res != OK) {
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000283 SET_ERR_L("Can't stop streaming");
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800284 // Continue to close device even in case of error
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000285 } else {
286 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
287 if (res != OK) {
288 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
289 maxExpectedDuration);
290 // Continue to close device even in case of error
291 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800292 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293 }
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000294 // Signal to request thread that we're not expecting any
295 // more requests. This will be true since once we're in
296 // disconnect and we've cleared off the request queue, the
297 // request thread can't receive any new requests through
298 // binder calls - since disconnect holds
299 // mBinderSerialization lock.
300 mRequestThread->setRequestClearing();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700301 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800302
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800303 if (mStatus == STATUS_ERROR) {
304 CLOGE("Shutting down in an error state");
305 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800307 if (mStatusTracker != NULL) {
308 mStatusTracker->requestExit();
309 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800311 if (mRequestThread != NULL) {
312 mRequestThread->requestExit();
313 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800315 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
316 for (size_t i = 0; i < mOutputStreams.size(); i++) {
317 streams.push_back(mOutputStreams[i]);
318 }
319 if (mInputStream != nullptr) {
320 streams.push_back(mInputStream);
321 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700322 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700323 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800324 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
325 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700326 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
327 // HAL may be in a bad state, so waiting for request thread
328 // (which may be stuck in the HAL processCaptureRequest call)
329 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800330 // give up mInterfaceLock here and then lock it again. Could this lead
331 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332 mRequestThread->join();
333 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700334 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800335 Mutex::Autolock il(mInterfaceLock);
336 if (mStatusTracker != NULL) {
337 mStatusTracker->join();
338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800340 if (mInjectionMethods->isInjecting()) {
341 mInjectionMethods->stopInjection();
342 }
343
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800344 HalInterface* interface;
345 {
346 Mutex::Autolock l(mLock);
347 mRequestThread.clear();
348 Mutex::Autolock stLock(mTrackerLock);
349 mStatusTracker.clear();
350 interface = mInterface.get();
351 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700352
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800353 // Call close without internal mutex held, as the HAL close may need to
354 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000355 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700356
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800357 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800358
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800359 {
360 Mutex::Autolock l(mLock);
361 mInterface->clear();
362 mOutputStreams.clear();
363 mInputStream.clear();
364 mDeletedStreams.clear();
365 mBufferManager.clear();
366 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
367 }
368
369 for (auto& weakStream : streams) {
370 sp<Camera3StreamInterface> stream = weakStream.promote();
371 if (stream != nullptr) {
372 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
373 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
374 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700375 }
376 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700377 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000378
379 if (mCameraServiceWatchdog != NULL) {
380 mCameraServiceWatchdog->requestExit();
381 mCameraServiceWatchdog.clear();
382 }
383
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700384 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800385}
386
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700387// For dumping/debugging only -
388// try to acquire a lock a few times, eventually give up to proceed with
389// debug/dump operations
390bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
391 bool gotLock = false;
392 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
393 if (lock.tryLock() == NO_ERROR) {
394 gotLock = true;
395 break;
396 } else {
397 usleep(kDumpSleepDuration);
398 }
399 }
400 return gotLock;
401}
402
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800403nsecs_t Camera3Device::getMonoToBoottimeOffset() {
404 // try three times to get the clock offset, choose the one
405 // with the minimum gap in measurements.
406 const int tries = 3;
407 nsecs_t bestGap, measured;
408 for (int i = 0; i < tries; ++i) {
409 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
410 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
411 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
412 const nsecs_t gap = tmono2 - tmono;
413 if (i == 0 || gap < bestGap) {
414 bestGap = gap;
415 measured = tbase - ((tmono + tmono2) >> 1);
416 }
417 }
418 return measured;
419}
420
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800421ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
422 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700423 // Get max jpeg size (area-wise) for default sensor pixel mode
424 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800425 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800426 /*supportsUltraHighResolutionCapture*/false);
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700427 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
428 // not ultra high res sensor
429 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800430 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700431 /*isUltraHighResolution*/true);
432 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800433 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
434 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700435 return BAD_VALUE;
436 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700437 bool useMaxSensorPixelModeThreshold = false;
438 if (uhrMaxJpegResolution.width != 0 &&
439 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
440 // Use the ultra high res max jpeg size and max jpeg buffer size
441 useMaxSensorPixelModeThreshold = true;
442 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700443
Zhijun Hef7da0962014-04-24 13:27:56 -0700444 // Get max jpeg buffer size
445 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800446 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700447 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800448 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
449 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700450 return BAD_VALUE;
451 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700452 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700453
454 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
455 if (useMaxSensorPixelModeThreshold) {
456 maxJpegBufferSize =
457 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
458 maxDefaultJpegResolution, maxJpegBufferSize);
459 chosenMaxJpegResolution = uhrMaxJpegResolution;
460 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800461 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700462
463 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700464 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700465 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800466 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
467 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700468 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800469 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
470 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700471 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700472 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700473 return jpegBufferSize;
474}
475
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800476ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700477 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800478 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700479 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800480 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
481 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700482 return BAD_VALUE;
483 }
484 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
485 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
486 return maxBytesForPointCloud;
487}
488
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800489ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
490 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800491 const int PER_CONFIGURATION_SIZE = 3;
492 const int WIDTH_OFFSET = 0;
493 const int HEIGHT_OFFSET = 1;
494 const int SIZE_OFFSET = 2;
495 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800496 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800497 camera3::SessionConfigurationUtils::getAppropriateModeTag(
498 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
499 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800500 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800501 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800502 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
503 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 return BAD_VALUE;
505 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700506
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800507 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
508 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
509 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
510 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
511 }
512 }
513
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800514 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
515 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800516 return BAD_VALUE;
517}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700518
Jing Mikec7f9b132023-03-12 11:12:04 +0800519status_t Camera3Device::dump(int fd, [[maybe_unused]] const Vector<String16> &args) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800520 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700521
522 // Try to lock, but continue in case of failure (to avoid blocking in
523 // deadlocks)
524 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
525 bool gotLock = tryLockSpinRightRound(mLock);
526
527 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800528 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
529 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700530 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800531 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
532 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700533
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800534 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700535
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 String16 templatesOption("-t");
537 int n = args.size();
538 for (int i = 0; i < n; i++) {
539 if (args[i] == templatesOption) {
540 dumpTemplates = true;
541 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000542 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700543 if (i + 1 < n) {
544 String8 monitorTags = String8(args[i + 1]);
545 if (monitorTags == "off") {
546 mTagMonitor.disableMonitoring();
547 } else {
548 mTagMonitor.parseTagsToMonitor(monitorTags);
549 }
550 } else {
551 mTagMonitor.disableMonitoring();
552 }
553 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800554 }
555
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800557
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558 const char *status =
559 mStatus == STATUS_ERROR ? "ERROR" :
560 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700561 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
562 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800563 mStatus == STATUS_ACTIVE ? "ACTIVE" :
564 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700565
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700567 if (mStatus == STATUS_ERROR) {
568 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
569 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800570 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800571 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000572 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
573 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
574 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800575 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576
577 if (mInputStream != NULL) {
578 write(fd, lines.string(), lines.size());
579 mInputStream->dump(fd, args);
580 } else {
581 lines.appendFormat(" No input stream.\n");
582 write(fd, lines.string(), lines.size());
583 }
584 for (size_t i = 0; i < mOutputStreams.size(); i++) {
585 mOutputStreams[i]->dump(fd,args);
586 }
587
Zhijun He431503c2016-03-07 17:30:16 -0800588 if (mBufferManager != NULL) {
589 lines = String8(" Camera3 Buffer Manager:\n");
590 write(fd, lines.string(), lines.size());
591 mBufferManager->dump(fd, args);
592 }
Zhijun He125684a2015-12-26 15:07:30 -0800593
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700594 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700595 if (mInFlightLock.try_lock()) {
596 if (mInFlightMap.size() == 0) {
597 lines.append(" None\n");
598 } else {
599 for (size_t i = 0; i < mInFlightMap.size(); i++) {
600 InFlightRequest r = mInFlightMap.valueAt(i);
601 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
602 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
603 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
604 r.numBuffersLeft);
605 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700606 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700607 mInFlightLock.unlock();
608 } else {
609 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700610 }
611 write(fd, lines.string(), lines.size());
612
Shuzhen Wang686f6442017-06-20 16:16:04 -0700613 if (mRequestThread != NULL) {
614 mRequestThread->dumpCaptureRequestLatency(fd,
615 " ProcessCaptureRequest latency histogram:");
616 }
617
Igor Murashkin1e479c02013-09-06 16:55:14 -0700618 {
619 lines = String8(" Last request sent:\n");
620 write(fd, lines.string(), lines.size());
621
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700622 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700623 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
624 }
625
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800626 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700627 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800628 "TEMPLATE_PREVIEW",
629 "TEMPLATE_STILL_CAPTURE",
630 "TEMPLATE_VIDEO_RECORD",
631 "TEMPLATE_VIDEO_SNAPSHOT",
632 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800633 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800634 };
635
Emilian Peevf4816702020-04-03 15:44:51 -0700636 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800637 camera_metadata_t *templateRequest = nullptr;
638 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700639 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800640 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800641 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800642 lines.append(" Not supported\n");
643 write(fd, lines.string(), lines.size());
644 } else {
645 write(fd, lines.string(), lines.size());
646 dump_indented_camera_metadata(templateRequest,
647 fd, /*verbosity*/2, /*indentation*/8);
648 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800649 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800650 }
651 }
652
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700653 mTagMonitor.dumpMonitoredMetadata(fd);
654
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800655 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800656 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800658 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700661 if (gotLock) mLock.unlock();
662 if (gotInterfaceLock) mInterfaceLock.unlock();
663
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664 return OK;
665}
666
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700667status_t Camera3Device::startWatchingTags(const String8 &tags) {
668 mTagMonitor.parseTagsToMonitor(tags);
669 return OK;
670}
671
672status_t Camera3Device::stopWatchingTags() {
673 mTagMonitor.disableMonitoring();
674 return OK;
675}
676
677status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
678 mTagMonitor.getLatestMonitoredTagEvents(out);
679 return OK;
680}
681
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800682const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800683 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800684 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
685 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700686 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800687 mStatus == STATUS_ERROR ?
688 "when in error state" : "before init");
689 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700690 if (physicalId.isEmpty()) {
691 return mDeviceInfo;
692 } else {
693 std::string id(physicalId.c_str());
694 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
695 return mPhysicalDeviceInfoMap.at(id);
696 } else {
697 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
698 return mDeviceInfo;
699 }
700 }
701}
702
703const CameraMetadata& Camera3Device::info() const {
704 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800705 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800706}
707
Jianing Wei90e59c92014-03-12 18:29:36 -0700708status_t Camera3Device::checkStatusOkToCaptureLocked() {
709 switch (mStatus) {
710 case STATUS_ERROR:
711 CLOGE("Device has encountered a serious error");
712 return INVALID_OPERATION;
713 case STATUS_UNINITIALIZED:
714 CLOGE("Device not initialized");
715 return INVALID_OPERATION;
716 case STATUS_UNCONFIGURED:
717 case STATUS_CONFIGURED:
718 case STATUS_ACTIVE:
719 // OK
720 break;
721 default:
722 SET_ERR_L("Unexpected status: %d", mStatus);
723 return INVALID_OPERATION;
724 }
725 return OK;
726}
727
728status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000729 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700730 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700731 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700732 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700733 if (requestList == NULL) {
734 CLOGE("requestList cannot be NULL.");
735 return BAD_VALUE;
736 }
737
Jianing Weicb0652e2014-03-12 18:29:36 -0700738 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000739 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700740 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
741 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
742 ++metadataIt, ++surfaceMapIt) {
743 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700744 if (newRequest == 0) {
745 CLOGE("Can't create capture request");
746 return BAD_VALUE;
747 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700748
Shuzhen Wang9d066012016-09-30 11:30:20 -0700749 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700750 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700751
Jianing Weicb0652e2014-03-12 18:29:36 -0700752 // Setup burst Id and request Id
753 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800754 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
755 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700756 CLOGE("RequestID does not exist in metadata");
757 return BAD_VALUE;
758 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800759 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700760
Jianing Wei90e59c92014-03-12 18:29:36 -0700761 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700762
763 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700764 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
766 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
767 return BAD_VALUE;
768 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700769
770 // Setup batch size if this is a high speed video recording request.
771 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
772 auto firstRequest = requestList->begin();
773 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
774 if (outputStream->isVideoStream()) {
775 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800776 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700777 break;
778 }
779 }
780 }
781
Jianing Wei90e59c92014-03-12 18:29:36 -0700782 return OK;
783}
784
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800785status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800786 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800787
Emilian Peevaebbe412018-01-15 13:53:24 +0000788 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700789 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000790 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700791
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800792 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700793}
794
Emilian Peevaebbe412018-01-15 13:53:24 +0000795void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700796 std::list<const SurfaceMap>& surfaceMaps,
797 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000798 PhysicalCameraSettingsList requestList;
799 requestList.push_back({std::string(getId().string()), request});
800 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700801
802 SurfaceMap surfaceMap;
803 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
804 // With no surface list passed in, stream and surface will have 1-to-1
805 // mapping. So the surface index is 0 for each stream in the surfaceMap.
806 for (size_t i = 0; i < streams.count; i++) {
807 surfaceMap[streams.data.i32[i]].push_back(0);
808 }
809 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800810}
811
Jianing Wei90e59c92014-03-12 18:29:36 -0700812status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000813 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700814 const std::list<const SurfaceMap> &surfaceMaps,
815 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700816 /*out*/
817 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700818 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700819 nsecs_t requestTimeNs = systemTime();
820
Jianing Wei90e59c92014-03-12 18:29:36 -0700821 Mutex::Autolock il(mInterfaceLock);
822 Mutex::Autolock l(mLock);
823
824 status_t res = checkStatusOkToCaptureLocked();
825 if (res != OK) {
826 // error logged by previous call
827 return res;
828 }
829
830 RequestList requestList;
831
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700833 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700834 if (res != OK) {
835 // error logged by previous call
836 return res;
837 }
838
839 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700840 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700841 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700842 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700843 }
844
845 if (res == OK) {
846 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
847 if (res != OK) {
848 SET_ERR_L("Can't transition to active in %f seconds!",
849 kActiveTimeout/1e9);
850 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800851 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700852 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700853 } else {
854 CLOGE("Cannot queue request. Impossible.");
855 return BAD_VALUE;
856 }
857
858 return res;
859}
860
Emilian Peevaebbe412018-01-15 13:53:24 +0000861status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700862 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700863 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700864 ATRACE_CALL();
865
Emilian Peevaebbe412018-01-15 13:53:24 +0000866 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700867}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800868
Jianing Weicb0652e2014-03-12 18:29:36 -0700869status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
870 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800871 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800872
Emilian Peevaebbe412018-01-15 13:53:24 +0000873 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700874 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000875 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700876
Emilian Peevaebbe412018-01-15 13:53:24 +0000877 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700878 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879}
880
Emilian Peevaebbe412018-01-15 13:53:24 +0000881status_t Camera3Device::setStreamingRequestList(
882 const List<const PhysicalCameraSettingsList> &requestsList,
883 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700884 ATRACE_CALL();
885
Emilian Peevaebbe412018-01-15 13:53:24 +0000886 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700887}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888
889sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000890 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 status_t res;
892
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700893 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800894 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
895 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000896 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700897 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700898 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700900 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700902 } else if (mStatus == STATUS_UNCONFIGURED) {
903 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 CLOGE("No streams configured");
905 return NULL;
906 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 }
908
Shuzhen Wang0129d522016-10-30 22:43:41 -0700909 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911}
912
Jianing Weicb0652e2014-03-12 18:29:36 -0700913status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800914 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700915 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 switch (mStatus) {
919 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700920 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 return INVALID_OPERATION;
922 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700923 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700925 case STATUS_UNCONFIGURED:
926 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 case STATUS_ACTIVE:
928 // OK
929 break;
930 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700931 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 return INVALID_OPERATION;
933 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800934 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700935
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700936 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800937}
938
939status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
940 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800942
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700943 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800944}
945
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700946status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800947 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700948 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700949 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700950 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700951 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800952 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
953 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700954
955 status_t res;
956 bool wasActive = false;
957
958 switch (mStatus) {
959 case STATUS_ERROR:
960 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
961 return INVALID_OPERATION;
962 case STATUS_UNINITIALIZED:
963 ALOGE("%s: Device not initialized", __FUNCTION__);
964 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700965 case STATUS_UNCONFIGURED:
966 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700967 // OK
968 break;
969 case STATUS_ACTIVE:
970 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700971 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700972 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700973 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700974 return res;
975 }
976 wasActive = true;
977 break;
978 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700979 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700980 return INVALID_OPERATION;
981 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700982 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700983
984 if (mInputStream != 0) {
985 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
986 return INVALID_OPERATION;
987 }
988
989 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
990 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700991 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700992
993 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800994 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700995
996 *id = mNextStreamId++;
997
998 // Continue captures if active at start
999 if (wasActive) {
1000 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001001 // Reuse current operating mode and session parameters for new stream config
1002 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001003 if (res != OK) {
1004 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1005 __FUNCTION__, mNextStreamId, strerror(-res), res);
1006 return res;
1007 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001008 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001009 }
1010
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001011 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001012 return OK;
1013}
1014
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001015status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001016 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001017 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001018 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001019 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1020 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001021 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001022 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001023 ATRACE_CALL();
1024
1025 if (consumer == nullptr) {
1026 ALOGE("%s: consumer must not be null", __FUNCTION__);
1027 return BAD_VALUE;
1028 }
1029
1030 std::vector<sp<Surface>> consumers;
1031 consumers.push_back(consumer);
1032
1033 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001034 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001035 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001036 streamUseCase, timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001037}
1038
1039static bool isRawFormat(int format) {
1040 switch (format) {
1041 case HAL_PIXEL_FORMAT_RAW16:
1042 case HAL_PIXEL_FORMAT_RAW12:
1043 case HAL_PIXEL_FORMAT_RAW10:
1044 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1045 return true;
1046 default:
1047 return false;
1048 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001049}
1050
1051status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1052 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001053 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001054 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001055 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001056 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001057 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001059
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001060 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001061 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001063 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001064 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001065 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001066 " mirrorMode %d, colorSpace %d, useReadoutTimestamp %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001067 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001068 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001069 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace,
1070 useReadoutTimestamp);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 status_t res;
1073 bool wasActive = false;
1074
1075 switch (mStatus) {
1076 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001077 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001078 return INVALID_OPERATION;
1079 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001080 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001082 case STATUS_UNCONFIGURED:
1083 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 // OK
1085 break;
1086 case STATUS_ACTIVE:
1087 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001088 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001090 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 return res;
1092 }
1093 wasActive = true;
1094 break;
1095 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 return INVALID_OPERATION;
1098 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001099 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100
1101 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001102
Shuzhen Wang0129d522016-10-30 22:43:41 -07001103 if (consumers.size() == 0 && !hasDeferredConsumer) {
1104 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1105 return BAD_VALUE;
1106 }
Zhijun He5d677d12016-05-29 16:52:39 -07001107
Shuzhen Wang0129d522016-10-30 22:43:41 -07001108 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001109 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1110 return BAD_VALUE;
1111 }
1112
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001113 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1114 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1115 // be found in only one sensor pixel mode.
1116 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1117 return BAD_VALUE;
1118 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001119 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001121 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001122 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001123 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001124 if (blobBufferSize <= 0) {
1125 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1126 return BAD_VALUE;
1127 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001128 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1129 blobBufferSize = width * height;
1130 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001131 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001132 if (blobBufferSize <= 0) {
1133 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1134 return BAD_VALUE;
1135 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001136 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001137 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001138 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001139 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001140 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001141 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001142 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001143 bool maxResolution =
1144 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1145 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001146 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1147 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001148 if (rawOpaqueBufferSize <= 0) {
1149 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1150 return BAD_VALUE;
1151 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001152 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001153 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001154 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001155 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001156 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001157 } else if (isShared) {
1158 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1159 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001160 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001161 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001162 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001163 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001164 newStream = new Camera3OutputStream(mNextStreamId,
1165 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001166 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001167 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001168 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001170 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001171 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001172 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001173 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001174 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001175 }
Emilian Peev40ead602017-09-26 15:46:36 +01001176
1177 size_t consumerCount = consumers.size();
1178 for (size_t i = 0; i < consumerCount; i++) {
1179 int id = newStream->getSurfaceId(consumers[i]);
1180 if (id < 0) {
1181 SET_ERR_L("Invalid surface id");
1182 return BAD_VALUE;
1183 }
1184 if (surfaceIds != nullptr) {
1185 surfaceIds->push_back(id);
1186 }
1187 }
1188
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001189 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190
Emilian Peev08dd2452017-04-06 16:55:14 +01001191 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001192
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001193 newStream->setImageDumpMask(mImageDumpMask);
1194
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 res = mOutputStreams.add(mNextStreamId, newStream);
1196 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001197 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return res;
1199 }
1200
Shuzhen Wang316781a2020-08-18 18:11:01 -07001201 mSessionStatsBuilder.addStream(mNextStreamId);
1202
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001204 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205
1206 // Continue captures if active at start
1207 if (wasActive) {
1208 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001209 // Reuse current operating mode and session parameters for new stream config
1210 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001212 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1213 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 return res;
1215 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001216 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001218 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001219 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220}
1221
Emilian Peev710c1422017-08-30 11:19:38 +01001222status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001223 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001224 if (nullptr == streamInfo) {
1225 return BAD_VALUE;
1226 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001227 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001229
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 switch (mStatus) {
1231 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001232 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233 return INVALID_OPERATION;
1234 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001235 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001237 case STATUS_UNCONFIGURED:
1238 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001239 case STATUS_ACTIVE:
1240 // OK
1241 break;
1242 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001243 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001244 return INVALID_OPERATION;
1245 }
1246
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001247 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1248 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001249 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001250 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 }
1252
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001253 streamInfo->width = stream->getWidth();
1254 streamInfo->height = stream->getHeight();
1255 streamInfo->format = stream->getFormat();
1256 streamInfo->dataSpace = stream->getDataSpace();
1257 streamInfo->formatOverridden = stream->isFormatOverridden();
1258 streamInfo->originalFormat = stream->getOriginalFormat();
1259 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1260 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001261 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001262 streamInfo->colorSpace = stream->getColorSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264}
1265
1266status_t Camera3Device::setStreamTransform(int id,
1267 int transform) {
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);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001272 switch (mStatus) {
1273 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 return INVALID_OPERATION;
1276 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 case STATUS_UNCONFIGURED:
1280 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 case STATUS_ACTIVE:
1282 // OK
1283 break;
1284 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001285 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 return INVALID_OPERATION;
1287 }
1288
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001289 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1290 if (stream == nullptr) {
1291 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 return BAD_VALUE;
1293 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001294 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001295}
1296
1297status_t Camera3Device::deleteStream(int id) {
1298 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 Mutex::Autolock l(mLock);
1301 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001302
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001303 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001304
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305 // CameraDevice semantics require device to already be idle before
1306 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001307 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001308 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001309 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 }
1311
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001312 if (mStatus == STATUS_ERROR) {
1313 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1314 __FUNCTION__, mId.string());
1315 return -EBUSY;
1316 }
1317
Igor Murashkin2fba5842013-04-22 14:03:54 -07001318 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001319 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320 if (mInputStream != NULL && id == mInputStream->getId()) {
1321 deletedStream = mInputStream;
1322 mInputStream.clear();
1323 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001324 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001325 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326 return BAD_VALUE;
1327 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001328 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001329 }
1330
1331 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001332 if (stream != nullptr) {
1333 deletedStream = stream;
1334 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001335 }
1336
1337 // Free up the stream endpoint so that it can be used by some other stream
1338 res = deletedStream->disconnect();
1339 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001340 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 // fall through since we want to still list the stream as deleted.
1342 }
1343 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001344 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345
1346 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001347}
1348
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001349status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001350 ATRACE_CALL();
1351 ALOGV("%s: E", __FUNCTION__);
1352
1353 Mutex::Autolock il(mInterfaceLock);
1354 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001355
Emilian Peev811d2952018-05-25 11:08:40 +01001356 // In case the client doesn't include any session parameter, try a
1357 // speculative configuration using the values from the last cached
1358 // default request.
1359 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001360 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001361 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1362 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1363 mLastTemplateId);
1364 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1365 operatingMode);
1366 }
1367
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001368 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1369}
1370
1371status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1372 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001373 //Filter out any incoming session parameters
1374 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001375 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1376 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001377 CameraMetadata filteredParams(availableSessionKeys.count);
1378 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1379 filteredParams.getAndLock());
1380 set_camera_metadata_vendor_id(meta, mVendorTagId);
1381 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001382 if (availableSessionKeys.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07001383 bool rotateAndCropSessionKey = false;
1384 bool autoframingSessionKey = false;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001385 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1386 camera_metadata_ro_entry entry = params.find(
1387 availableSessionKeys.data.i32[i]);
1388 if (entry.count > 0) {
1389 filteredParams.update(entry);
1390 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07001391 if (ANDROID_SCALER_ROTATE_AND_CROP == availableSessionKeys.data.i32[i]) {
1392 rotateAndCropSessionKey = true;
1393 }
1394 if (ANDROID_CONTROL_AUTOFRAMING == availableSessionKeys.data.i32[i]) {
1395 autoframingSessionKey = true;
1396 }
1397 }
1398
1399 if (rotateAndCropSessionKey || autoframingSessionKey) {
1400 sp<CaptureRequest> request = new CaptureRequest();
1401 PhysicalCameraSettings settingsList;
1402 settingsList.metadata = filteredParams;
1403 request->mSettingsList.push_back(settingsList);
1404
1405 if (rotateAndCropSessionKey) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07001406 auto rotateAndCropEntry = filteredParams.find(ANDROID_SCALER_ROTATE_AND_CROP);
1407 if (rotateAndCropEntry.count > 0 &&
1408 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
1409 request->mRotateAndCropAuto = true;
1410 } else {
1411 request->mRotateAndCropAuto = false;
1412 }
1413
1414 overrideAutoRotateAndCrop(request, mOverrideToPortrait, mRotateAndCropOverride);
1415 }
1416
1417 if (autoframingSessionKey) {
1418 auto autoframingEntry = filteredParams.find(ANDROID_CONTROL_AUTOFRAMING);
1419 if (autoframingEntry.count > 0 &&
1420 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
1421 overrideAutoframing(request, mAutoframingOverride);
1422 }
1423 }
1424
1425 filteredParams = request->mSettingsList.begin()->metadata;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001426 }
1427 }
1428
1429 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001430}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001431
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001432status_t Camera3Device::getInputBufferProducer(
1433 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001434 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001435 Mutex::Autolock il(mInterfaceLock);
1436 Mutex::Autolock l(mLock);
1437
1438 if (producer == NULL) {
1439 return BAD_VALUE;
1440 } else if (mInputStream == NULL) {
1441 return INVALID_OPERATION;
1442 }
1443
1444 return mInputStream->getInputBufferProducer(producer);
1445}
1446
Emilian Peevf4816702020-04-03 15:44:51 -07001447status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001448 CameraMetadata *request) {
1449 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001450 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001451
Emilian Peevf4816702020-04-03 15:44:51 -07001452 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001453 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001454 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001455 return BAD_VALUE;
1456 }
1457
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001458 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001460 {
1461 Mutex::Autolock l(mLock);
1462 switch (mStatus) {
1463 case STATUS_ERROR:
1464 CLOGE("Device has encountered a serious error");
1465 return INVALID_OPERATION;
1466 case STATUS_UNINITIALIZED:
1467 CLOGE("Device is not initialized!");
1468 return INVALID_OPERATION;
1469 case STATUS_UNCONFIGURED:
1470 case STATUS_CONFIGURED:
1471 case STATUS_ACTIVE:
1472 // OK
1473 break;
1474 default:
1475 SET_ERR_L("Unexpected status: %d", mStatus);
1476 return INVALID_OPERATION;
1477 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001478
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001479 if (!mRequestTemplateCache[templateId].isEmpty()) {
1480 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001481 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001482 return OK;
1483 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001484 }
1485
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001486 camera_metadata_t *rawRequest;
1487 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001488 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001489
1490 {
1491 Mutex::Autolock l(mLock);
1492 if (res == BAD_VALUE) {
1493 ALOGI("%s: template %d is not supported on this camera device",
1494 __FUNCTION__, templateId);
1495 return res;
1496 } else if (res != OK) {
1497 CLOGE("Unable to construct request template %d: %s (%d)",
1498 templateId, strerror(-res), res);
1499 return res;
1500 }
1501
1502 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1503 mRequestTemplateCache[templateId].acquire(rawRequest);
1504
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001505 // Override the template request with zoomRatioMapper
1506 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1507 &mRequestTemplateCache[templateId]);
1508 if (res != OK) {
1509 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1510 templateId, strerror(-res), res);
1511 return res;
1512 }
1513
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001514 // Fill in JPEG_QUALITY if not available
1515 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1516 static const uint8_t kDefaultJpegQuality = 95;
1517 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1518 &kDefaultJpegQuality, 1);
1519 }
1520
Bharatt Kukrejad33fe9f2022-11-23 21:52:52 +00001521 // Fill in AUTOFRAMING if not available
1522 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1523 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1524 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1525 &kDefaultAutoframingMode, 1);
1526 }
1527
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001528 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001529 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001530 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001531 return OK;
1532}
1533
1534status_t Camera3Device::waitUntilDrained() {
1535 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001536 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001537 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001540 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001541}
1542
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001543status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001544 switch (mStatus) {
1545 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001546 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001547 ALOGV("%s: Already idle", __FUNCTION__);
1548 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001549 case STATUS_CONFIGURED:
1550 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551 case STATUS_ERROR:
1552 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001553 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001554 break;
1555 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001556 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 return INVALID_OPERATION;
1558 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001559 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1560 maxExpectedDuration);
1561 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001562 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001563 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001564 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1565 res);
1566 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 return res;
1568}
1569
Ruben Brunk183f0562015-08-12 12:55:02 -07001570void Camera3Device::internalUpdateStatusLocked(Status status) {
1571 mStatus = status;
1572 mRecentStatusUpdates.add(mStatus);
1573 mStatusChanged.broadcast();
1574}
1575
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001577status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001578 if (mRequestThread.get() != nullptr) {
1579 mRequestThread->setPaused(true);
1580 } else {
1581 return NO_INIT;
1582 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001584 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1585 maxExpectedDuration);
1586 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001588 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001589 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001590 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001591 }
1592
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 return res;
1594}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001596// Resume after internalPauseAndWaitLocked
1597status_t Camera3Device::internalResumeLocked() {
1598 status_t res;
1599
1600 mRequestThread->setPaused(false);
1601
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001602 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1603 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001604 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1605 if (res != OK) {
1606 SET_ERR_L("Can't transition to active in %f seconds!",
1607 kActiveTimeout/1e9);
1608 }
1609 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001610 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611}
1612
Ruben Brunk183f0562015-08-12 12:55:02 -07001613status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001614 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001615
1616 size_t startIndex = 0;
1617 if (mStatusWaiters == 0) {
1618 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1619 // this status list
1620 mRecentStatusUpdates.clear();
1621 } else {
1622 // If other threads are waiting on updates to this status list, set the position of the
1623 // first element that this list will check rather than clearing the list.
1624 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 }
1626
Ruben Brunk183f0562015-08-12 12:55:02 -07001627 mStatusWaiters++;
1628
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001629 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001630 if (!active && mUseHalBufManager) {
1631 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001632 if (mStatus == STATUS_ACTIVE) {
1633 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001634 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001635 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001636 mRequestBufferSM.onWaitUntilIdle();
1637 }
1638
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001639 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001640 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001641 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001642 if (active == (mStatus == STATUS_ACTIVE)) {
1643 // Desired state is current
1644 break;
1645 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001646
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001647 nsecs_t timeElapsed = systemTime() - startTime;
1648 nsecs_t timeToWait = timeout - timeElapsed;
1649 if (timeToWait <= 0) {
1650 // Thread woke up spuriously but has timed out since.
1651 // Force out of loop with TIMED_OUT result.
1652 res = TIMED_OUT;
1653 break;
1654 }
1655 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 if (res != OK) break;
1657
Ruben Brunk183f0562015-08-12 12:55:02 -07001658 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1659 // transitions.
1660 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1661 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1662 __FUNCTION__);
1663
1664 // Encountered desired state since we began waiting
1665 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001666 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1667 stateSeen = true;
1668 break;
1669 }
1670 }
1671 } while (!stateSeen);
1672
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001673 if (signalPipelineDrain) {
1674 mRequestThread->resetPipelineDrain();
1675 }
1676
Ruben Brunk183f0562015-08-12 12:55:02 -07001677 mStatusWaiters--;
1678
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001679 return res;
1680}
1681
1682
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001683status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001684 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001685 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001686
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001687 if (listener != NULL && mListener != NULL) {
1688 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1689 }
1690 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001691 mRequestThread->setNotificationListener(listener);
1692 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001693
1694 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001695}
1696
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001697bool Camera3Device::willNotify3A() {
1698 return false;
1699}
1700
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001701status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001702 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001703 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001704
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001705 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001706 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1707 if (st == std::cv_status::timeout) {
1708 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001709 }
1710 }
1711 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001712}
1713
Jianing Weicb0652e2014-03-12 18:29:36 -07001714status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001715 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001716 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001717
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001718 if (mResultQueue.empty()) {
1719 return NOT_ENOUGH_DATA;
1720 }
1721
Jianing Weicb0652e2014-03-12 18:29:36 -07001722 if (frame == NULL) {
1723 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1724 return BAD_VALUE;
1725 }
1726
1727 CaptureResult &result = *(mResultQueue.begin());
1728 frame->mResultExtras = result.mResultExtras;
1729 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001730 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001731 mResultQueue.erase(mResultQueue.begin());
1732
1733 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001734}
1735
1736status_t Camera3Device::triggerAutofocus(uint32_t id) {
1737 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001738 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001740 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1741 // Mix-in this trigger into the next request and only the next request.
1742 RequestTrigger trigger[] = {
1743 {
1744 ANDROID_CONTROL_AF_TRIGGER,
1745 ANDROID_CONTROL_AF_TRIGGER_START
1746 },
1747 {
1748 ANDROID_CONTROL_AF_TRIGGER_ID,
1749 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001750 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001751 };
1752
1753 return mRequestThread->queueTrigger(trigger,
1754 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001755}
1756
1757status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1758 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001759 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001761 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1762 // Mix-in this trigger into the next request and only the next request.
1763 RequestTrigger trigger[] = {
1764 {
1765 ANDROID_CONTROL_AF_TRIGGER,
1766 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1767 },
1768 {
1769 ANDROID_CONTROL_AF_TRIGGER_ID,
1770 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001771 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001772 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001773
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001774 return mRequestThread->queueTrigger(trigger,
1775 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001776}
1777
1778status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1779 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001780 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001781
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001782 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1783 // Mix-in this trigger into the next request and only the next request.
1784 RequestTrigger trigger[] = {
1785 {
1786 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1787 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1788 },
1789 {
1790 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1791 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001792 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001793 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001794
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001795 return mRequestThread->queueTrigger(trigger,
1796 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001797}
1798
Jianing Weicb0652e2014-03-12 18:29:36 -07001799status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001800 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001801 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001802 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001803
Zhijun He7ef20392014-04-21 16:04:17 -07001804 {
1805 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001806
1807 // b/116514106 "disconnect()" can get called twice for the same device. The
1808 // camera device will not be initialized during the second run.
1809 if (mStatus == STATUS_UNINITIALIZED) {
1810 return OK;
1811 }
1812
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001813 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001814
1815 // Stop session and stream counter
1816 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001817 }
1818
Ravneetdbd5b242022-03-02 07:22:46 +00001819 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001820 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001821 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1822 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1823
1824 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001825}
1826
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001827status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001828 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1829}
1830
1831status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001832 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001833 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001834 Mutex::Autolock il(mInterfaceLock);
1835 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001836
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001837 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1838 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001839 CLOGE("Stream %d does not exist", streamId);
1840 return BAD_VALUE;
1841 }
1842
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001843 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001844 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001845 return BAD_VALUE;
1846 }
1847
1848 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001849 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001850 return BAD_VALUE;
1851 }
1852
Ruben Brunkc78ac262015-08-13 17:58:46 -07001853 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001854}
1855
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001856status_t Camera3Device::tearDown(int streamId) {
1857 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001858 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001859 Mutex::Autolock il(mInterfaceLock);
1860 Mutex::Autolock l(mLock);
1861
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001862 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1863 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001864 CLOGE("Stream %d does not exist", streamId);
1865 return BAD_VALUE;
1866 }
1867
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001868 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1869 CLOGE("Stream %d is a target of a in-progress request", streamId);
1870 return BAD_VALUE;
1871 }
1872
1873 return stream->tearDown();
1874}
1875
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001876status_t Camera3Device::addBufferListenerForStream(int streamId,
1877 wp<Camera3StreamBufferListener> listener) {
1878 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001879 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001880 Mutex::Autolock il(mInterfaceLock);
1881 Mutex::Autolock l(mLock);
1882
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001883 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1884 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001885 CLOGE("Stream %d does not exist", streamId);
1886 return BAD_VALUE;
1887 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001888 stream->addBufferListener(listener);
1889
1890 return OK;
1891}
1892
Austin Borger4a870a32022-02-25 01:48:41 +00001893float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1894 camera_metadata_entry minDurations =
1895 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1896 for (size_t i = 0; i < minDurations.count; i += 4) {
1897 if (minDurations.data.i64[i] == stream->getFormat()
1898 && minDurations.data.i64[i+1] == stream->getWidth()
1899 && minDurations.data.i64[i+2] == stream->getHeight()) {
1900 int64_t minFrameDuration = minDurations.data.i64[i+3];
1901 return 1e9f / minFrameDuration;
1902 }
1903 }
1904 return 0.0f;
1905}
1906
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001907/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001908 * Methods called by subclasses
1909 */
1910
1911void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001912 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001913 std::vector<int> streamIds;
1914 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001915 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001916
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001917 {
1918 // Need mLock to safely update state and synchronize to current
1919 // state of methods in flight.
1920 Mutex::Autolock l(mLock);
1921 // We can get various system-idle notices from the status tracker
1922 // while starting up. Only care about them if we've actually sent
1923 // in some requests recently.
1924 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1925 return;
1926 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001927 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1928 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001929 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001930
1931 // Skip notifying listener if we're doing some user-transparent
1932 // state changes
1933 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001934
Austin Borger4a870a32022-02-25 01:48:41 +00001935 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1936 auto stream = mOutputStreams[i];
1937 if (stream.get() == nullptr) continue;
1938
1939 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1940 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1941
1942 // Populate stream statistics in case of Idle
1943 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001944 streamIds.push_back(stream->getId());
1945 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1946 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001947 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001948 if (camera3Stream != nullptr) {
1949 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001950 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001951 }
1952 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Shuzhen Wangb02d3fc2023-02-23 21:01:25 +00001953 stream->getOriginalFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001954 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001955 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001956 stream->getDynamicRangeProfile(), streamUseCase,
1957 stream->getColorSpace());
Shuzhen Wang316781a2020-08-18 18:11:01 -07001958 }
1959 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001960 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001961
1962 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001964 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001965 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001967 status_t res = OK;
1968 if (listener != nullptr) {
1969 if (idle) {
1970 // Get session stats from the builder, and notify the listener.
1971 int64_t requestCount, resultErrorCount;
1972 bool deviceError;
1973 std::map<int, StreamStats> streamStatsMap;
1974 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1975 &deviceError, &streamStatsMap);
1976 for (size_t i = 0; i < streamIds.size(); i++) {
1977 int streamId = streamIds[i];
1978 auto stats = streamStatsMap.find(streamId);
1979 if (stats != streamStatsMap.end()) {
1980 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1981 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1982 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1983 streamStats[i].mHistogramType =
1984 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1985 streamStats[i].mHistogramBins.assign(
1986 stats->second.mCaptureLatencyBins.begin(),
1987 stats->second.mCaptureLatencyBins.end());
1988 streamStats[i].mHistogramCounts.assign(
1989 stats->second.mCaptureLatencyHistogram.begin(),
1990 stats->second.mCaptureLatencyHistogram.end());
1991 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001992 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001993 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1994 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001995 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001996 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001997 }
1998 if (res != OK) {
1999 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
2000 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002001 }
2002}
2003
Shuzhen Wang758c2152017-01-10 18:26:18 -08002004status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002005 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002006 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002007 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2008 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002009
2010 if (surfaceIds == nullptr) {
2011 return BAD_VALUE;
2012 }
2013
Zhijun He5d677d12016-05-29 16:52:39 -07002014 Mutex::Autolock il(mInterfaceLock);
2015 Mutex::Autolock l(mLock);
2016
Shuzhen Wang758c2152017-01-10 18:26:18 -08002017 if (consumers.size() == 0) {
2018 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002019 return BAD_VALUE;
2020 }
2021
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002022 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2023 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002024 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002025 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002026 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002027
2028 // isConsumerConfigurationDeferred will be off after setConsumers
2029 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002030 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002031 if (res != OK) {
2032 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2033 return res;
2034 }
2035
Emilian Peev40ead602017-09-26 15:46:36 +01002036 for (auto &consumer : consumers) {
2037 int id = stream->getSurfaceId(consumer);
2038 if (id < 0) {
2039 CLOGE("Invalid surface id!");
2040 return BAD_VALUE;
2041 }
2042 surfaceIds->push_back(id);
2043 }
2044
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002045 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002046 if (!stream->isConfiguring()) {
2047 CLOGE("Stream %d was already fully configured.", streamId);
2048 return INVALID_OPERATION;
2049 }
Zhijun He5d677d12016-05-29 16:52:39 -07002050
Shuzhen Wang0129d522016-10-30 22:43:41 -07002051 res = stream->finishConfiguration();
2052 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002053 // If finishConfiguration fails due to abandoned surface, do not set
2054 // device to error state.
2055 bool isSurfaceAbandoned =
2056 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2057 if (!isSurfaceAbandoned) {
2058 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2059 stream->getId(), strerror(-res), res);
2060 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002061 return res;
2062 }
Zhijun He5d677d12016-05-29 16:52:39 -07002063 }
2064
2065 return OK;
2066}
2067
Emilian Peev40ead602017-09-26 15:46:36 +01002068status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2069 const std::vector<OutputStreamInfo> &outputInfo,
2070 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2071 Mutex::Autolock il(mInterfaceLock);
2072 Mutex::Autolock l(mLock);
2073
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002074 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2075 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002076 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002077 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002078 }
2079
2080 for (const auto &it : removedSurfaceIds) {
2081 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2082 CLOGE("Shared surface still part of a pending request!");
2083 return -EBUSY;
2084 }
2085 }
2086
Emilian Peev40ead602017-09-26 15:46:36 +01002087 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2088 if (res != OK) {
2089 CLOGE("Stream %d failed to update stream (error %d %s) ",
2090 streamId, res, strerror(-res));
2091 if (res == UNKNOWN_ERROR) {
2092 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2093 __FUNCTION__);
2094 }
2095 return res;
2096 }
2097
2098 return res;
2099}
2100
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002101status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2102 Mutex::Autolock il(mInterfaceLock);
2103 Mutex::Autolock l(mLock);
2104
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002105 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2106 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002107 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2108 return BAD_VALUE;
2109 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002110
2111 if (dropping) {
2112 mSessionStatsBuilder.stopCounter(streamId);
2113 } else {
2114 mSessionStatsBuilder.startCounter(streamId);
2115 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002116 return stream->dropBuffers(dropping);
2117}
2118
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002119/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002120 * Camera3Device private methods
2121 */
2122
2123sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002124 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002125 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002126
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002127 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002128 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129
2130 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002131 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002132 if (inputStreams.count > 0) {
2133 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002134 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002135 CLOGE("Request references unknown input stream %d",
2136 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002137 return NULL;
2138 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002139
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002141 SET_ERR_L("%s: input stream %d is not configured!",
2142 __FUNCTION__, mInputStream->getId());
2143 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002144 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002145 // Check if stream prepare is blocking requests.
2146 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002147 CLOGE("Request references an input stream that's being prepared!");
2148 return NULL;
2149 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002150
2151 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002152 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002153 }
2154
2155 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002156 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002157 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002158 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002159 return NULL;
2160 }
2161
2162 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002163 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2164 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002165 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002166 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002167 return NULL;
2168 }
Zhijun He5d677d12016-05-29 16:52:39 -07002169 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002170 auto iter = surfaceMap.find(streams.data.i32[i]);
2171 if (iter != surfaceMap.end()) {
2172 const std::vector<size_t>& surfaces = iter->second;
2173 for (const auto& surface : surfaces) {
2174 if (stream->isConsumerConfigurationDeferred(surface)) {
2175 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2176 "due to deferred consumer", stream->getId(), surface);
2177 return NULL;
2178 }
2179 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002180 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002181 }
2182
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002183 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002184 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2185 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002186 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002187 // Check if stream prepare is blocking requests.
2188 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002189 CLOGE("Request references an output stream that's being prepared!");
2190 return NULL;
2191 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002192
2193 newRequest->mOutputStreams.push(stream);
2194 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002195 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002196 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002197
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002198 auto rotateAndCropEntry =
2199 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2200 if (rotateAndCropEntry.count > 0 &&
2201 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2202 newRequest->mRotateAndCropAuto = true;
2203 } else {
2204 newRequest->mRotateAndCropAuto = false;
2205 }
2206
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002207 auto autoframingEntry =
2208 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2209 if (autoframingEntry.count > 0 &&
2210 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2211 newRequest->mAutoframingAuto = true;
2212 } else {
2213 newRequest->mAutoframingAuto = false;
2214 }
2215
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002216 auto zoomRatioEntry =
2217 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2218 if (zoomRatioEntry.count > 0 &&
2219 zoomRatioEntry.data.f[0] == 1.0f) {
2220 newRequest->mZoomRatioIs1x = true;
2221 } else {
2222 newRequest->mZoomRatioIs1x = false;
2223 }
2224
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002225 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002226 for (auto& settings : newRequest->mSettingsList) {
2227 auto testPatternModeEntry =
2228 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2229 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2230 testPatternModeEntry.data.i32[0] :
2231 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002232
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002233 auto testPatternDataEntry =
2234 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2235 if (testPatternDataEntry.count >= 4) {
2236 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2237 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2238 } else {
2239 settings.mOriginalTestPatternData[0] = 0;
2240 settings.mOriginalTestPatternData[1] = 0;
2241 settings.mOriginalTestPatternData[2] = 0;
2242 settings.mOriginalTestPatternData[3] = 0;
2243 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002244 }
2245 }
2246
Shuzhen Wangaf22e912023-04-11 16:03:17 -07002247 if (mSupportZoomOverride) {
2248 for (auto& settings : newRequest->mSettingsList) {
2249 auto settingsOverrideEntry =
2250 settings.metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
2251 settings.mOriginalSettingsOverride = settingsOverrideEntry.count > 0 ?
2252 settingsOverrideEntry.data.i32[0] :
2253 ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
2254 }
2255 }
2256
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002257 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002258}
2259
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002260void Camera3Device::cancelStreamsConfigurationLocked() {
2261 int res = OK;
2262 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2263 res = mInputStream->cancelConfiguration();
2264 if (res != OK) {
2265 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2266 mInputStream->getId(), strerror(-res), res);
2267 }
2268 }
2269
2270 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002271 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002272 if (outputStream->isConfiguring()) {
2273 res = outputStream->cancelConfiguration();
2274 if (res != OK) {
2275 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2276 outputStream->getId(), strerror(-res), res);
2277 }
2278 }
2279 }
2280
2281 // Return state to that at start of call, so that future configures
2282 // properly clean things up
2283 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2284 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002285
2286 res = mPreparerThread->resume();
2287 if (res != OK) {
2288 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2289 }
2290}
2291
Emilian Peev0d0191e2020-04-21 17:01:18 -07002292bool Camera3Device::checkAbandonedStreamsLocked() {
2293 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2294 return true;
2295 }
2296
2297 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2298 auto stream = mOutputStreams[i];
2299 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2300 return true;
2301 }
2302 }
2303
2304 return false;
2305}
2306
Emilian Peev3bead5f2020-05-28 17:29:08 -07002307bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002308 ATRACE_CALL();
2309 bool ret = false;
2310
Shuzhen Wang316781a2020-08-18 18:11:01 -07002311 nsecs_t startTime = systemTime();
2312
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002313 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002314 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2315
2316 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002317 if (checkAbandonedStreamsLocked()) {
2318 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2319 __FUNCTION__);
2320 return true;
2321 }
2322
Emilian Peev3bead5f2020-05-28 17:29:08 -07002323 status_t rc = NO_ERROR;
2324 bool markClientActive = false;
2325 if (mStatus == STATUS_ACTIVE) {
2326 markClientActive = true;
2327 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002328
2329 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2330 }
2331
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002332 if (rc == NO_ERROR) {
2333 mNeedConfig = true;
2334 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2335 if (rc == NO_ERROR) {
2336 ret = true;
2337 mPauseStateNotify = false;
2338 //Moving to active state while holding 'mLock' is important.
2339 //There could be pending calls to 'create-/deleteStream' which
2340 //will trigger another stream configuration while the already
2341 //present streams end up with outstanding buffers that will
2342 //not get drained.
2343 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002344 } else if (rc == DEAD_OBJECT) {
2345 // DEAD_OBJECT can be returned if either the consumer surface is
2346 // abandoned, or the HAL has died.
2347 // - If the HAL has died, configureStreamsLocked call will set
2348 // device to error state,
2349 // - If surface is abandoned, we should not set device to error
2350 // state.
2351 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002352 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002353 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002354 }
2355 } else {
2356 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2357 }
2358
Austin Borger74fca042022-05-23 12:41:21 -07002359 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002360 ns2ms(systemTime() - startTime));
2361
Emilian Peev3bead5f2020-05-28 17:29:08 -07002362 if (markClientActive) {
2363 mStatusTracker->markComponentActive(clientStatusId);
2364 }
2365
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002366 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002367}
2368
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002369status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002370 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002371 ATRACE_CALL();
2372 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002373
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002374 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002375 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 return INVALID_OPERATION;
2377 }
2378
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002379 if (operatingMode < 0) {
2380 CLOGE("Invalid operating mode: %d", operatingMode);
2381 return BAD_VALUE;
2382 }
2383
2384 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002385 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002386
2387 if (mOperatingMode != operatingMode) {
2388 mNeedConfig = true;
2389 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2390 mOperatingMode = operatingMode;
2391 }
2392
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002393 // Reset min expected duration when session is reconfigured.
2394 mMinExpectedDuration = 0;
2395
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002396 // In case called from configureStreams, abort queued input buffers not belonging to
2397 // any pending requests.
2398 if (mInputStream != NULL && notifyRequestThread) {
2399 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002400 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002401 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002402 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002403 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002404 if (res != OK) {
2405 // Exhausted acquiring all input buffers.
2406 break;
2407 }
2408
Emilian Peevf4816702020-04-03 15:44:51 -07002409 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002410 res = mInputStream->returnInputBuffer(inputBuffer);
2411 if (res != OK) {
2412 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2413 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2414 }
2415 }
2416 }
2417
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002418 if (!mNeedConfig) {
2419 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2420 return OK;
2421 }
2422
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002423 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002424 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002425 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2426 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002427 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002428 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002429 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002430 }
2431
Shuzhen Wang16610a62022-12-15 22:38:07 -08002432 // Override stream use case based on "adb shell command"
2433 overrideStreamUseCaseLocked();
2434
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002435 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002436 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002437
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002438 mPreparerThread->pause();
2439
Emilian Peevf4816702020-04-03 15:44:51 -07002440 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002441 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002442 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002443 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002444
Emilian Peevf4816702020-04-03 15:44:51 -07002445 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002446 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002447 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002448
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002449
2450 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002451 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002452 inputStream = mInputStream->startConfiguration();
2453 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002454 CLOGE("Can't start input stream configuration");
2455 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002456 return INVALID_OPERATION;
2457 }
2458 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002459
2460 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002461 }
2462
Shuzhen Wang99080502021-03-07 21:08:20 -08002463 mGroupIdPhysicalCameraMap.clear();
Emilian Peevd865f0d2023-03-17 17:13:07 -07002464 mComposerOutput = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002465 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002466
2467 // Don't configure bidi streams twice, nor add them twice to the list
2468 if (mOutputStreams[i].get() ==
2469 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2470
2471 config.num_streams--;
2472 continue;
2473 }
2474
Emilian Peevf4816702020-04-03 15:44:51 -07002475 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002476 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002477 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002478 CLOGE("Can't start output stream configuration");
2479 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480 return INVALID_OPERATION;
2481 }
2482 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002483
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002484 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002485 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2486 // always occupy the initial entry.
Emilian Peeve579d8b2023-02-28 14:16:08 -08002487 if ((outputStream->data_space == HAL_DATASPACE_V0_JFIF) ||
2488 (outputStream->data_space ==
2489 static_cast<android_dataspace_t>(
2490 aidl::android::hardware::graphics::common::Dataspace::JPEG_R))) {
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002491 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002492 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2493 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002494 } else if (outputStream->data_space ==
2495 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2496 bufferSizes[k] = outputStream->width * outputStream->height;
2497 } else {
2498 ALOGW("%s: Blob dataSpace %d not supported",
2499 __FUNCTION__, outputStream->data_space);
2500 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002501 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002502
2503 if (mOutputStreams[i]->isMultiResolution()) {
2504 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2505 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2506 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2507 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002508
2509 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07002510 mComposerOutput = true;
Emilian Peeve23f1d92021-09-20 14:56:01 -07002511 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002512 }
2513
2514 config.streams = streams.editArray();
2515
2516 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002517 // max_buffers, usage, and priv fields, as well as data_space and format
2518 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002519
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002520 int64_t logId = mCameraServiceProxyWrapper->getCurrentLogIdForCamera(mId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002521 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002522 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes, logId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002523 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002524
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002525 if (res == BAD_VALUE) {
2526 // HAL rejected this set of streams as unsupported, clean up config
2527 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002528 CLOGE("Set of requested inputs/outputs not supported by HAL");
2529 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002530 return BAD_VALUE;
2531 } else if (res != OK) {
2532 // Some other kind of error from configure_streams - this is not
2533 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002534 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2535 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002536 return res;
2537 }
2538
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002539 // Finish all stream configuration immediately.
2540 // TODO: Try to relax this later back to lazy completion, which should be
2541 // faster
2542
Igor Murashkin073f8572013-05-02 14:59:28 -07002543 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002544 bool streamReConfigured = false;
2545 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002546 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002547 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002548 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002549 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002550 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2551 return DEAD_OBJECT;
2552 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002553 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002554 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002555 if (streamReConfigured) {
2556 mInterface->onStreamReConfigured(mInputStream->getId());
2557 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002558 }
2559
2560 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002561 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002562 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002563 bool streamReConfigured = false;
2564 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002565 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002566 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002567 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002568 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002569 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2570 return DEAD_OBJECT;
2571 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002572 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002573 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002574 if (streamReConfigured) {
2575 mInterface->onStreamReConfigured(outputStream->getId());
2576 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002577 }
2578 }
2579
Emilian Peevd865f0d2023-03-17 17:13:07 -07002580 mRequestThread->setComposerSurface(mComposerOutput);
Emilian Peeve23f1d92021-09-20 14:56:01 -07002581
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002582 // Request thread needs to know to avoid using repeat-last-settings protocol
2583 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002584 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002585 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2586 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002587 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002588
Zhijun He90f7c372016-08-16 16:19:43 -07002589 char value[PROPERTY_VALUE_MAX];
2590 property_get("camera.fifo.disable", value, "0");
2591 int32_t disableFifo = atoi(value);
2592 if (disableFifo != 1) {
2593 // Boost priority of request thread to SCHED_FIFO.
2594 pid_t requestThreadTid = mRequestThread->getTid();
2595 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002596 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002597 if (res != OK) {
2598 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2599 strerror(-res), res);
2600 } else {
2601 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2602 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002603 }
2604
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002605 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002606 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2607 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2608 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2609 sessionParams.unlock(newSessionParams);
2610 mSessionParams.unlock(currentSessionParams);
2611 if (updateSessionParams) {
2612 mSessionParams = sessionParams;
2613 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002614
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002615 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002616
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002617 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002618 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002619
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002620 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002621
Zhijun He0a210512014-07-24 13:45:15 -07002622 // tear down the deleted streams after configure streams.
2623 mDeletedStreams.clear();
2624
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002625 auto rc = mPreparerThread->resume();
2626 if (rc != OK) {
2627 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2628 return rc;
2629 }
2630
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002631 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002632 mRequestBufferSM.onStreamsConfigured();
2633 }
2634
Cliff Wu3b268182021-07-06 15:44:43 +08002635 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002636 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002637 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002638 // configure the injection streams.
2639 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002640 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002641 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2642 res = mInjectionMethods->injectCamera(config, bufferSizes);
2643 if (res != OK) {
2644 ALOGE("Can't finish inject camera process!");
2645 return res;
2646 }
Cliff Wu3b268182021-07-06 15:44:43 +08002647 } else {
2648 // First run configureStreamsLocked() and then call injectCamera() case:
2649 // If the stream configuration has been completed and camera deive is active, but the
2650 // injection camera has not been injected yet, we need to store the stream configuration of
2651 // the internal camera (because the stream configuration of the injection camera is based
2652 // on the internal camera). When injecting occurs later, this configuration can be used by
2653 // the injection camera.
2654 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2655 " injection camera has not been injected yet.", __FUNCTION__);
2656 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002657 }
2658
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002659 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002660}
2661
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002662status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002663 ATRACE_CALL();
2664 status_t res;
2665
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002666 if (mFakeStreamId != NO_STREAM) {
2667 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002668 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002669 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002670 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002671 return INVALID_OPERATION;
2672 }
2673
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002674 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002675
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002676 sp<Camera3OutputStreamInterface> fakeStream =
2677 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002678
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002679 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002680 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002681 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002682 return res;
2683 }
2684
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002685 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002686 mNextStreamId++;
2687
2688 return OK;
2689}
2690
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002691status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002692 ATRACE_CALL();
2693 status_t res;
2694
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002695 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002696 if (mOutputStreams.size() == 1) return OK;
2697
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002698 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002699
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002700 // Ok, have a fake stream and there's at least one other output stream,
2701 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002702
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002703 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002704 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002705 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002706 return INVALID_OPERATION;
2707 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002708 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002709
2710 // Free up the stream endpoint so that it can be used by some other stream
2711 res = deletedStream->disconnect();
2712 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002713 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002714 // fall through since we want to still list the stream as deleted.
2715 }
2716 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002717 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002718
2719 return res;
2720}
2721
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002722void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002723 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002724 Mutex::Autolock l(mLock);
2725 va_list args;
2726 va_start(args, fmt);
2727
2728 setErrorStateLockedV(fmt, args);
2729
2730 va_end(args);
2731}
2732
2733void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002734 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002735 Mutex::Autolock l(mLock);
2736 setErrorStateLockedV(fmt, args);
2737}
2738
2739void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2740 va_list args;
2741 va_start(args, fmt);
2742
2743 setErrorStateLockedV(fmt, args);
2744
2745 va_end(args);
2746}
2747
2748void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002749 // Print out all error messages to log
2750 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002751 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002752
2753 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002754 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002755
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002756 mErrorCause = errorCause;
2757
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002758 if (mRequestThread != nullptr) {
2759 mRequestThread->setPaused(true);
2760 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002761 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002762
2763 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002764 sp<NotificationListener> listener = mListener.promote();
2765 if (listener != NULL) {
2766 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002767 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002768 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002769 }
2770
2771 // Save stack trace. View by dumping it later.
2772 CameraTraces::saveTrace();
2773 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002774}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002775
2776/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002777 * In-flight request management
2778 */
2779
Jianing Weicb0652e2014-03-12 18:29:36 -07002780status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002781 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002782 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002783 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002784 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002785 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002786 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002787 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002788 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002789
2790 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002791 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002792 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002793 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2794 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002795 if (res < 0) return res;
2796
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002797 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002798 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2799 // avoid a deadlock during reprocess requests.
2800 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002801 if (mStatusTracker != nullptr) {
2802 mStatusTracker->markComponentActive(mInFlightStatusId);
2803 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002804 }
2805
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002806 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002807 return OK;
2808}
2809
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002810void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002811 // Indicate idle inFlightMap to the status tracker
2812 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002813 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002814 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2815 // avoid a deadlock during reprocess requests.
2816 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002817 if (mStatusTracker != nullptr) {
2818 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2819 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002820 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002821 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002822}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002823
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002824void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002825 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002826 // something has likely gone wrong. This might still be legit only if application send in
2827 // a long burst of long exposure requests.
2828 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2829 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2830 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2831 mInFlightMap.size(), mExpectedInflightDuration);
2832 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2833 kInFlightWarnLimitHighSpeed) {
2834 CLOGW("In-flight list too large for high speed configuration: %zu,"
2835 "total inflight duration %" PRIu64,
2836 mInFlightMap.size(), mExpectedInflightDuration);
2837 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002838 }
2839}
2840
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002841void Camera3Device::onInflightMapFlushedLocked() {
2842 mExpectedInflightDuration = 0;
2843}
2844
2845void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002846 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002847 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2848 mInFlightMap.removeItemsAt(idx, 1);
2849
2850 onInflightEntryRemovedLocked(duration);
2851}
2852
2853
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002854void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002855 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002856 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002857 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002858 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002859 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002860 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002861
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002862 FlushInflightReqStates states {
2863 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002864 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002865
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002866 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002867}
2868
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002869CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002870 ALOGV("%s", __FUNCTION__);
2871
Igor Murashkin1e479c02013-09-06 16:55:14 -07002872 CameraMetadata retVal;
2873
2874 if (mRequestThread != NULL) {
2875 retVal = mRequestThread->getLatestRequest();
2876 }
2877
Igor Murashkin1e479c02013-09-06 16:55:14 -07002878 return retVal;
2879}
2880
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002881void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002882 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002883 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002884 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2885 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002886
2887 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002888 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002889}
2890
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002891void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002892 std::vector<native_handle_t*> *handles, bool closeFd) {
2893 if (handles == nullptr) {
2894 return;
2895 }
2896 if (closeFd) {
2897 for (auto& handle : *handles) {
2898 native_handle_close(handle);
2899 }
2900 }
2901 for (auto& handle : *handles) {
2902 native_handle_delete(handle);
2903 }
2904 handles->clear();
2905 return;
2906}
2907
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002908/**
2909 * HalInterface inner class methods
2910 */
2911
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002912void Camera3Device::HalInterface::getInflightBufferKeys(
2913 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002914 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002915 return;
2916}
2917
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002918void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2919 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002920 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002921 return;
2922}
2923
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002924bool Camera3Device::HalInterface::verifyBufferIds(
2925 int32_t streamId, std::vector<uint64_t>& bufIds) {
2926 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002927}
2928
2929status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002930 int32_t frameNumber, int32_t streamId,
2931 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002932 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002933}
2934
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002935status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002936 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002937 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002938}
2939
2940// Find and pop a buffer_handle_t based on bufferId
2941status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002942 uint64_t bufferId,
2943 /*out*/ buffer_handle_t** buffer,
2944 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002945 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002946}
2947
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002948std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2949 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002950 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002951}
2952
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002953uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2954 const native_handle_t* handle) {
2955 return mBufferRecords.removeOneBufferCache(streamId, handle);
2956}
2957
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002958void Camera3Device::HalInterface::onBufferFreed(
2959 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002960 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2961 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2962 if (bufferId != BUFFER_ID_NO_BUFFER) {
2963 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002964 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002965}
2966
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002967void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002968 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2969 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2970 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002971 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2972 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002973}
2974
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002975/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002976 * RequestThread inner class methods
2977 */
2978
2979Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002980 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002981 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002982 bool useHalBufManager,
Austin Borger18b30a72022-10-27 12:20:29 -07002983 bool supportCameraMute,
Shuzhen Wangaf22e912023-04-11 16:03:17 -07002984 bool overrideToPortrait,
2985 bool supportSettingsOverride) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002986 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002987 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002988 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002989 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002990 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002991 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002992 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002993 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002994 mReconfigured(false),
2995 mDoPause(false),
2996 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002997 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002998 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002999 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003000 mCurrentAfTriggerId(0),
3001 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003002 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00003003 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07003004 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07003005 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003006 mCameraMuteChanged(false),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003007 mSettingsOverride(ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003008 mRepeatingLastFrameNumber(
3009 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003010 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003011 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003012 mRequestLatency(kRequestLatencyBinSize),
3013 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003014 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003015 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07003016 mSupportCameraMute(supportCameraMute),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003017 mOverrideToPortrait(overrideToPortrait),
3018 mSupportSettingsOverride(supportSettingsOverride) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003019 mStatusId = statusTracker->addComponent("RequestThread");
Emilian Peeva1b26262023-02-06 17:27:41 -08003020 mVndkVersion = property_get_int32("ro.vndk.version", __ANDROID_API_FUTURE__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003021}
3022
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003023Camera3Device::RequestThread::~RequestThread() {}
3024
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003025void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003026 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003027 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003028 Mutex::Autolock l(mRequestLock);
3029 mListener = listener;
3030}
3031
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003032void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08003033 const CameraMetadata& sessionParams,
3034 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003035 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003036 Mutex::Autolock l(mRequestLock);
3037 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003038 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08003039 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003040 // Prepare video stream for high speed recording.
3041 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003042 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003043}
3044
Jianing Wei90e59c92014-03-12 18:29:36 -07003045status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003046 List<sp<CaptureRequest> > &requests,
3047 /*out*/
3048 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003049 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003050 Mutex::Autolock l(mRequestLock);
3051 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3052 ++it) {
3053 mRequestQueue.push_back(*it);
3054 }
3055
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003056 if (lastFrameNumber != NULL) {
3057 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3058 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3059 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3060 *lastFrameNumber);
3061 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003062
Jianing Wei90e59c92014-03-12 18:29:36 -07003063 unpauseForNewRequests();
3064
3065 return OK;
3066}
3067
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003068
3069status_t Camera3Device::RequestThread::queueTrigger(
3070 RequestTrigger trigger[],
3071 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003072 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003073 Mutex::Autolock l(mTriggerMutex);
3074 status_t ret;
3075
3076 for (size_t i = 0; i < count; ++i) {
3077 ret = queueTriggerLocked(trigger[i]);
3078
3079 if (ret != OK) {
3080 return ret;
3081 }
3082 }
3083
3084 return OK;
3085}
3086
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003087const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3088 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003089 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003090 if (d != nullptr) return d->mId;
3091 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003092}
3093
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003094status_t Camera3Device::RequestThread::queueTriggerLocked(
3095 RequestTrigger trigger) {
3096
3097 uint32_t tag = trigger.metadataTag;
3098 ssize_t index = mTriggerMap.indexOfKey(tag);
3099
3100 switch (trigger.getTagType()) {
3101 case TYPE_BYTE:
3102 // fall-through
3103 case TYPE_INT32:
3104 break;
3105 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003106 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3107 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003108 return INVALID_OPERATION;
3109 }
3110
3111 /**
3112 * Collect only the latest trigger, since we only have 1 field
3113 * in the request settings per trigger tag, and can't send more than 1
3114 * trigger per request.
3115 */
3116 if (index != NAME_NOT_FOUND) {
3117 mTriggerMap.editValueAt(index) = trigger;
3118 } else {
3119 mTriggerMap.add(tag, trigger);
3120 }
3121
3122 return OK;
3123}
3124
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003125status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003126 const RequestList &requests,
3127 /*out*/
3128 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003129 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003130 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003131 if (lastFrameNumber != NULL) {
3132 *lastFrameNumber = mRepeatingLastFrameNumber;
3133 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003134 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003135 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003136 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3137 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003138
3139 unpauseForNewRequests();
3140
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003141 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003142 return OK;
3143}
3144
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003145bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003146 if (mRepeatingRequests.empty()) {
3147 return false;
3148 }
3149 int32_t requestId = requestIn->mResultExtras.requestId;
3150 const RequestList &repeatRequests = mRepeatingRequests;
3151 // All repeating requests are guaranteed to have same id so only check first quest
3152 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3153 return (firstRequest->mResultExtras.requestId == requestId);
3154}
3155
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003156status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003157 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003158 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003159 return clearRepeatingRequestsLocked(lastFrameNumber);
3160
3161}
3162
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003163status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3164 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003165 std::vector<int32_t> streamIds;
3166 for (const auto& request : mRepeatingRequests) {
3167 for (const auto& stream : request->mOutputStreams) {
3168 streamIds.push_back(stream->getId());
3169 }
3170 }
3171
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003172 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003173 if (lastFrameNumber != NULL) {
3174 *lastFrameNumber = mRepeatingLastFrameNumber;
3175 }
Emilian Peev2295df72021-11-12 18:14:10 -08003176
3177 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3178
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003179 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003180 return OK;
3181}
3182
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003183status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003184 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003185 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003186 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003187 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003188
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003189 // Send errors for all requests pending in the request queue, including
3190 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003191 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003192 if (listener != NULL) {
3193 for (RequestList::iterator it = mRequestQueue.begin();
3194 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003195 // Abort the input buffers for reprocess requests.
3196 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003197 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003198 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003199 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003200 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003201 if (res != OK) {
3202 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3203 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3204 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003205 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003206 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3207 if (res != OK) {
3208 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3209 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3210 }
3211 }
3212 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003213 // Set the frame number this request would have had, if it
3214 // had been submitted; this frame number will not be reused.
3215 // The requestId and burstId fields were set when the request was
3216 // submitted originally (in convertMetadataListToRequestListLocked)
3217 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003218 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003219 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003220 }
3221 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003222 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003223
3224 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003225 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003226 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003227 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003228 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003229 return OK;
3230}
3231
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003232status_t Camera3Device::RequestThread::flush() {
3233 ATRACE_CALL();
3234 Mutex::Autolock l(mFlushLock);
3235
Emilian Peev08dd2452017-04-06 16:55:14 +01003236 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003237}
3238
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003239void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003240 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003241 Mutex::Autolock l(mPauseLock);
3242 mDoPause = paused;
3243 mDoPauseSignal.signal();
3244}
3245
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003246status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3247 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003248 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003249 Mutex::Autolock l(mLatestRequestMutex);
3250 status_t res;
3251 while (mLatestRequestId != requestId) {
3252 nsecs_t startTime = systemTime();
3253
3254 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3255 if (res != OK) return res;
3256
3257 timeout -= (systemTime() - startTime);
3258 }
3259
3260 return OK;
3261}
3262
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003263void Camera3Device::RequestThread::requestExit() {
3264 // Call parent to set up shutdown
3265 Thread::requestExit();
3266 // The exit from any possible waits
3267 mDoPauseSignal.signal();
3268 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003269
3270 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3271 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003272}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003273
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003274void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003275 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003276 bool surfaceAbandoned = false;
3277 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003278 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003279 {
3280 Mutex::Autolock l(mRequestLock);
3281 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3282 // repeating requests.
3283 for (const auto& request : mRepeatingRequests) {
3284 for (const auto& s : request->mOutputStreams) {
3285 if (s->isAbandoned()) {
3286 surfaceAbandoned = true;
3287 clearRepeatingRequestsLocked(&lastFrameNumber);
3288 break;
3289 }
3290 }
3291 if (surfaceAbandoned) {
3292 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003293 }
3294 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003295 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003296 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003297
3298 if (listener != NULL && surfaceAbandoned) {
3299 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003300 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003301}
3302
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003303bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003304 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003305 status_t res;
3306 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003307 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003308 uint32_t numRequestProcessed = 0;
3309 for (size_t i = 0; i < batchSize; i++) {
3310 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003311 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003312 }
3313
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003314 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3315
3316 bool triggerRemoveFailed = false;
3317 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3318 for (size_t i = 0; i < numRequestProcessed; i++) {
3319 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3320 nextRequest.submitted = true;
3321
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003322 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003323
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003324 if (!triggerRemoveFailed) {
3325 // Remove any previously queued triggers (after unlock)
3326 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3327 if (removeTriggerRes != OK) {
3328 triggerRemoveFailed = true;
3329 triggerFailedRequest = nextRequest;
3330 }
3331 }
3332 }
3333
3334 if (triggerRemoveFailed) {
3335 SET_ERR("RequestThread: Unable to remove triggers "
3336 "(capture request %d, HAL device: %s (%d)",
3337 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3338 cleanUpFailedRequests(/*sendRequestError*/ false);
3339 return false;
3340 }
3341
3342 if (res != OK) {
3343 // Should only get a failure here for malformed requests or device-level
3344 // errors, so consider all errors fatal. Bad metadata failures should
3345 // come through notify.
3346 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3347 mNextRequests[numRequestProcessed].halRequest.frame_number,
3348 strerror(-res), res);
3349 cleanUpFailedRequests(/*sendRequestError*/ false);
3350 return false;
3351 }
3352 return true;
3353}
3354
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003355Camera3Device::RequestThread::ExpectedDurationInfo
3356 Camera3Device::RequestThread::calculateExpectedDurationRange(
3357 const camera_metadata_t *request) {
3358 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003359 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003360 InFlightRequest::kDefaultMaxExpectedDuration,
3361 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003362 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3363 find_camera_metadata_ro_entry(request,
3364 ANDROID_CONTROL_AE_MODE,
3365 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003366 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003367
3368 switch (e.data.u8[0]) {
3369 case ANDROID_CONTROL_AE_MODE_OFF:
3370 find_camera_metadata_ro_entry(request,
3371 ANDROID_SENSOR_EXPOSURE_TIME,
3372 &e);
3373 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003374 expectedDurationInfo.minDuration = e.data.i64[0];
3375 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003376 }
3377 find_camera_metadata_ro_entry(request,
3378 ANDROID_SENSOR_FRAME_DURATION,
3379 &e);
3380 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003381 expectedDurationInfo.minDuration =
3382 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3383 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003384 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003385 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003386 break;
3387 default:
3388 find_camera_metadata_ro_entry(request,
3389 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3390 &e);
3391 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003392 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3393 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003394 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003395 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003396 break;
3397 }
3398
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003399 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003400}
3401
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003402bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3403 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3404 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3405 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3406 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3407 return true;
3408 }
3409
3410 return false;
3411}
3412
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003413void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3414 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003415 camera_capture_request_t& halRequest = nextRequest.halRequest;
3416 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003417 Mutex::Autolock al(mLatestRequestMutex);
3418
Shuzhen Wang83bff122020-11-20 15:51:39 -08003419 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003420 mLatestRequest.acquire(cloned);
3421
3422 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003423 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3424 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3425 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003426 CameraMetadata(cloned));
3427 }
3428
3429 sp<Camera3Device> parent = mParent.promote();
3430 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003431 int32_t inputStreamId = -1;
3432 if (halRequest.input_buffer != nullptr) {
3433 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3434 }
3435
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003436 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003437 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003438 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3439 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003440 }
3441 }
3442
Shuzhen Wang83bff122020-11-20 15:51:39 -08003443 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003444 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003445 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003446 }
3447
Shuzhen Wang83bff122020-11-20 15:51:39 -08003448 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003449}
3450
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003451bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3452 ATRACE_CALL();
3453 bool updatesDetected = false;
3454
Emilian Peev4ec17882019-01-24 17:16:58 -08003455 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003456 for (auto tag : mSessionParamKeys) {
3457 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003458 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003459
3460 if (entry.count > 0) {
3461 bool isDifferent = false;
3462 if (lastEntry.count > 0) {
3463 // Have a last value, compare to see if changed
3464 if (lastEntry.type == entry.type &&
3465 lastEntry.count == entry.count) {
3466 // Same type and count, compare values
3467 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3468 size_t entryBytes = bytesPerValue * lastEntry.count;
3469 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3470 if (cmp != 0) {
3471 isDifferent = true;
3472 }
3473 } else {
3474 // Count or type has changed
3475 isDifferent = true;
3476 }
3477 } else {
3478 // No last entry, so always consider to be different
3479 isDifferent = true;
3480 }
3481
3482 if (isDifferent) {
3483 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003484 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3485 updatesDetected = true;
3486 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003487 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003488 }
3489 } else if (lastEntry.count > 0) {
3490 // Value has been removed
3491 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003492 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003493 updatesDetected = true;
3494 }
3495 }
3496
Emilian Peev4ec17882019-01-24 17:16:58 -08003497 bool reconfigureRequired;
3498 if (updatesDetected) {
3499 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3500 updatedParams);
3501 mLatestSessionParams = updatedParams;
3502 } else {
3503 reconfigureRequired = false;
3504 }
3505
3506 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003507}
3508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003509bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003510 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003511 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003512 // Any function called from threadLoop() must not hold mInterfaceLock since
3513 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3514 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003515
3516 // Handle paused state.
3517 if (waitIfPaused()) {
3518 return true;
3519 }
3520
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003521 // Wait for the next batch of requests.
3522 waitForNextRequestBatch();
3523 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003524 return true;
3525 }
3526
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003527 // Get the latest request ID, if any
3528 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003529 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003530 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003531 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003532 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003533 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003534 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3535 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003536 }
3537
Emilian Peevd865f0d2023-03-17 17:13:07 -07003538 for (size_t i = 0; i < mNextRequests.size(); i++) {
3539 auto& nextRequest = mNextRequests.editItemAt(i);
3540 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3541 // Do not override rotate&crop for stream configurations that include
3542 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3543 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3544 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3545 overrideAutoRotateAndCrop(captureRequest);
3546 captureRequest->mAutoframingChanged = overrideAutoframing(captureRequest);
3547 }
3548
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003549 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3550 // or a single request from streaming or burst. In either case the first element
3551 // should contain the latest camera settings that we need to check for any session
3552 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003553 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003554 res = OK;
3555
3556 //Input stream buffers are already acquired at this point so an input stream
3557 //will not be able to move to idle state unless we force it.
3558 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3559 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3560 if (res != OK) {
3561 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3562 cleanUpFailedRequests(/*sendRequestError*/ false);
3563 return false;
3564 }
3565 }
3566
3567 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003568 sp<Camera3Device> parent = mParent.promote();
3569 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003570 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3571 if (statusTracker != nullptr) {
3572 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3573 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003574 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003575 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003576 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003577
3578 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3579 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3580 if (res != OK) {
3581 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3582 cleanUpFailedRequests(/*sendRequestError*/ false);
3583 return false;
3584 }
3585 }
3586 }
3587 }
3588
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003589 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003590 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003591 if (res == TIMED_OUT) {
3592 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003593 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003594 // Check if any stream is abandoned.
3595 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003596 return true;
3597 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003598 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003599 return false;
3600 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003601
Zhijun Hecc27e112013-10-03 16:12:43 -07003602 // Inform waitUntilRequestProcessed thread of a new request ID
3603 {
3604 Mutex::Autolock al(mLatestRequestMutex);
3605
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003606 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003607 mLatestRequestSignal.signal();
3608 }
3609
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003610 // Submit a batch of requests to HAL.
3611 // Use flush lock only when submitting multilple requests in a batch.
3612 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3613 // which may take a long time to finish so synchronizing flush() and
3614 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3615 // For now, only synchronize for high speed recording and we should figure something out for
3616 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003617 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003618
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003619 if (useFlushLock) {
3620 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003621 }
3622
Zhijun Hef0645c12016-08-02 00:58:11 -07003623 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003624 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003625
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003626 sp<Camera3Device> parent = mParent.promote();
3627 if (parent != nullptr) {
3628 parent->mRequestBufferSM.onSubmittingRequest();
3629 }
3630
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003631 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003632 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003633 submitRequestSuccess = sendRequestsBatch();
3634
Shuzhen Wang686f6442017-06-20 16:16:04 -07003635 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3636 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003637
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003638 if (useFlushLock) {
3639 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003640 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003641
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003642 // Unset as current request
3643 {
3644 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003645 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003646 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003647 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003648
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003649 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003650}
3651
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003652status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3653 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3654 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3655 ANDROID_SCALER_CROP_REGION_SET};
3656 if (request == nullptr) {
3657 ALOGE("%s request metadata nullptr", __FUNCTION__);
3658 return BAD_VALUE;
3659 }
3660 status_t res = OK;
3661 for (const auto &key : kFwkOnlyRegionKeys) {
3662 if (request->exists(key)) {
3663 res = request->erase(key);
3664 if (res != OK) {
3665 return res;
3666 }
3667 }
3668 }
3669 return OK;
3670}
3671
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003672status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003673 ATRACE_CALL();
3674
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003675 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003676 for (size_t i = 0; i < mNextRequests.size(); i++) {
3677 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003678 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003679 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3680 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003681
3682 // Prepare a request to HAL
3683 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3684
3685 // Insert any queued triggers (before metadata is locked)
3686 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003687 if (res < 0) {
3688 SET_ERR("RequestThread: Unable to insert triggers "
3689 "(capture request %d, HAL device: %s (%d)",
3690 halRequest->frame_number, strerror(-res), res);
3691 return INVALID_OPERATION;
3692 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003693
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003694 int triggerCount = res;
3695 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3696 mPrevTriggers = triggerCount;
3697
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003698 bool testPatternChanged = overrideTestPattern(captureRequest);
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003699 bool settingsOverrideChanged = overrideSettingsOverride(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003700
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003701 // If the request is the same as last, or we had triggers now or last time or
3702 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003703 bool newRequest =
Emilian Peevd865f0d2023-03-17 17:13:07 -07003704 (mPrevRequest != captureRequest || triggersMixedIn ||
3705 captureRequest->mRotateAndCropChanged ||
3706 captureRequest->mAutoframingChanged ||
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003707 testPatternChanged || settingsOverrideChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003708 // Request settings are all the same within one batch, so only treat the first
3709 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003710 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003711 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003712 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003713 /**
3714 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003715 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003716 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003717 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003718 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003719 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003720 "(capture request %d, HAL device: %s (%d)",
3721 halRequest->frame_number, strerror(-res), res);
3722 return INVALID_OPERATION;
3723 }
3724
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003725 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003726 sp<Camera3Device> parent = mParent.promote();
3727 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003728 List<PhysicalCameraSettings>::iterator it;
3729 for (it = captureRequest->mSettingsList.begin();
3730 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003731 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3732 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003733 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3734 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3735 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3736 res);
3737 return INVALID_OPERATION;
3738 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003739 continue;
3740 }
3741
3742 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3743 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3744 updateCaptureRequest(&(it->metadata));
3745 if (res != OK) {
3746 SET_ERR("RequestThread: Unable to correct capture requests "
3747 "for scaler crop region and metering regions for request "
3748 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3749 res);
3750 return INVALID_OPERATION;
3751 }
3752 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003753 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3754 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3755 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3756 res);
3757 return INVALID_OPERATION;
3758 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003759 }
3760 }
3761
3762 // Correct metadata regions for distortion correction if enabled
3763 for (it = captureRequest->mSettingsList.begin();
3764 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003765 if (parent->mDistortionMappers.find(it->cameraId) ==
3766 parent->mDistortionMappers.end()) {
3767 continue;
3768 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003769
3770 if (!captureRequest->mDistortionCorrectionUpdated) {
3771 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3772 &(it->metadata));
3773 if (res != OK) {
3774 SET_ERR("RequestThread: Unable to correct capture requests "
3775 "for lens distortion for request %d: %s (%d)",
3776 halRequest->frame_number, strerror(-res), res);
3777 return INVALID_OPERATION;
3778 }
3779 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003780 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003781 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003782
3783 for (it = captureRequest->mSettingsList.begin();
3784 it != captureRequest->mSettingsList.end(); it++) {
3785 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3786 parent->mZoomRatioMappers.end()) {
3787 continue;
3788 }
3789
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003790 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003791 cameraIdsWithZoom.insert(it->cameraId);
3792 }
3793
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003794 if (!captureRequest->mZoomRatioUpdated) {
3795 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3796 &(it->metadata));
3797 if (res != OK) {
3798 SET_ERR("RequestThread: Unable to correct capture requests "
3799 "for zoom ratio for request %d: %s (%d)",
3800 halRequest->frame_number, strerror(-res), res);
3801 return INVALID_OPERATION;
3802 }
3803 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003804 }
3805 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003806 if (captureRequest->mRotateAndCropAuto &&
3807 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003808 for (it = captureRequest->mSettingsList.begin();
3809 it != captureRequest->mSettingsList.end(); it++) {
3810 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3811 if (mapper != parent->mRotateAndCropMappers.end()) {
3812 res = mapper->second.updateCaptureRequest(&(it->metadata));
3813 if (res != OK) {
3814 SET_ERR("RequestThread: Unable to correct capture requests "
3815 "for rotate-and-crop for request %d: %s (%d)",
3816 halRequest->frame_number, strerror(-res), res);
3817 return INVALID_OPERATION;
3818 }
3819 }
3820 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003821 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003822 }
Emilian Peeva1b26262023-02-06 17:27:41 -08003823
3824 for (it = captureRequest->mSettingsList.begin();
3825 it != captureRequest->mSettingsList.end(); it++) {
3826 res = hardware::cameraservice::utils::conversion::aidl::filterVndkKeys(
3827 mVndkVersion, it->metadata, false /*isStatic*/);
3828 if (res != OK) {
3829 SET_ERR("RequestThread: Failed during VNDK filter of capture requests "
3830 "%d: %s (%d)", halRequest->frame_number, strerror(-res), res);
3831 return INVALID_OPERATION;
3832 }
3833 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003834 }
3835 }
3836
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003837 /**
3838 * The request should be presorted so accesses in HAL
3839 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3840 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003841 captureRequest->mSettingsList.begin()->metadata.sort();
3842 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003843 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003844 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003845 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3846
3847 IF_ALOGV() {
3848 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3849 find_camera_metadata_ro_entry(
3850 halRequest->settings,
3851 ANDROID_CONTROL_AF_TRIGGER,
3852 &e
3853 );
3854 if (e.count > 0) {
3855 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3856 __FUNCTION__,
3857 halRequest->frame_number,
3858 e.data.u8[0]);
3859 }
3860 }
3861 } else {
3862 // leave request.settings NULL to indicate 'reuse latest given'
3863 ALOGVV("%s: Request settings are REUSED",
3864 __FUNCTION__);
3865 }
3866
Emilian Peevaebbe412018-01-15 13:53:24 +00003867 if (captureRequest->mSettingsList.size() > 1) {
3868 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3869 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003870 if (newRequest) {
3871 halRequest->physcam_settings =
3872 new const camera_metadata* [halRequest->num_physcam_settings];
3873 } else {
3874 halRequest->physcam_settings = nullptr;
3875 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003876 auto it = ++captureRequest->mSettingsList.begin();
3877 size_t i = 0;
3878 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3879 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003880 if (newRequest) {
3881 it->metadata.sort();
3882 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3883 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003884 }
3885 }
3886
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003887 uint32_t totalNumBuffers = 0;
3888
3889 // Fill in buffers
3890 if (captureRequest->mInputStream != NULL) {
3891 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003892
3893 halRequest->input_width = captureRequest->mInputBufferSize.width;
3894 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003895 totalNumBuffers += 1;
3896 } else {
3897 halRequest->input_buffer = NULL;
3898 }
3899
Emilian Peevf4816702020-04-03 15:44:51 -07003900 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003901 captureRequest->mOutputStreams.size());
3902 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003903 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003904
3905 sp<Camera3Device> parent = mParent.promote();
3906 if (parent == NULL) {
3907 // Should not happen, and nowhere to send errors to, so just log it
3908 CLOGE("RequestThread: Parent is gone");
3909 return INVALID_OPERATION;
3910 }
3911 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3912
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003913 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003914 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003915 sp<Camera3OutputStreamInterface> outputStream =
3916 captureRequest->mOutputStreams.editItemAt(j);
3917 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003918
3919 // Prepare video buffers for high speed recording on the first video request.
3920 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3921 // Only try to prepare video stream on the first video request.
3922 mPrepareVideoStream = false;
3923
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003924 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3925 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003926 while (res == NOT_ENOUGH_DATA) {
3927 res = outputStream->prepareNextBuffer();
3928 }
3929 if (res != OK) {
3930 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3931 __FUNCTION__, strerror(-res), res);
3932 outputStream->cancelPrepare();
3933 }
3934 }
3935
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003936 std::vector<size_t> uniqueSurfaceIds;
3937 res = outputStream->getUniqueSurfaceIds(
3938 captureRequest->mOutputSurfaces[streamId],
3939 &uniqueSurfaceIds);
3940 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3941 if (res != OK && res != INVALID_OPERATION) {
3942 ALOGE("%s: failed to query stream %d unique surface IDs",
3943 __FUNCTION__, streamId);
3944 return res;
3945 }
3946 if (res == OK) {
3947 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3948 }
3949
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003950 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003951 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003952 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003953 return TIMED_OUT;
3954 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003955 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003956 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003957 buffer.stream = outputStream->asHalStream();
3958 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003959 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003960 buffer.acquire_fence = -1;
3961 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003962 // Mark the output stream as unpreparable to block clients from calling
3963 // 'prepare' after this request reaches CameraHal and before the respective
3964 // buffers are requested.
3965 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003966 } else {
3967 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3968 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003969 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003970 if (res != OK) {
3971 // Can't get output buffer from gralloc queue - this could be due to
3972 // abandoned queue or other consumer misbehavior, so not a fatal
3973 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003974 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003975 " %s (%d)", strerror(-res), res);
3976
3977 return TIMED_OUT;
3978 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003979 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003980
3981 {
3982 sp<Camera3Device> parent = mParent.promote();
3983 if (parent != nullptr) {
3984 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3985 for (const auto& settings : captureRequest->mSettingsList) {
3986 if ((streamCameraId.isEmpty() &&
3987 parent->getId() == settings.cameraId.c_str()) ||
3988 streamCameraId == settings.cameraId.c_str()) {
3989 outputStream->fireBufferRequestForFrameNumber(
3990 captureRequest->mResultExtras.frameNumber,
3991 settings.metadata);
3992 }
3993 }
3994 }
3995 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003996
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003997 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003998 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3999 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4000 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4001 } else if (!physicalCameraId.isEmpty()) {
4002 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004003 }
4004 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004005 }
4006 totalNumBuffers += halRequest->num_output_buffers;
4007
4008 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004009 // If this request list is for constrained high speed recording (not
4010 // preview), and the current request is not the last one in the batch,
4011 // do not send callback to the app.
4012 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004013 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004014 hasCallback = false;
4015 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004016 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004017 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004018 const camera_metadata_t* settings = halRequest->settings;
4019 bool shouldUnlockSettings = false;
4020 if (settings == nullptr) {
4021 shouldUnlockSettings = true;
4022 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4023 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004024 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4025 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004026 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004027 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4028 isStillCapture = true;
4029 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4030 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004031
Emilian Peevaf8416e2021-02-04 17:52:43 -08004032 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004033 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004034 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4035 isZslCapture = true;
4036 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004037 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004038 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004039 res = parent->registerInFlight(halRequest->frame_number,
4040 totalNumBuffers, captureRequest->mResultExtras,
4041 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004042 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004043 expectedDurationInfo.minDuration,
4044 expectedDurationInfo.maxDuration,
4045 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004046 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004047 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
4048 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004049 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004050 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004051 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4052 ", burstId = %" PRId32 ".",
4053 __FUNCTION__,
4054 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4055 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004056
4057 if (shouldUnlockSettings) {
4058 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4059 }
4060
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004061 if (res != OK) {
4062 SET_ERR("RequestThread: Unable to register new in-flight request:"
4063 " %s (%d)", strerror(-res), res);
4064 return INVALID_OPERATION;
4065 }
4066 }
4067
4068 return OK;
4069}
4070
Igor Murashkin1e479c02013-09-06 16:55:14 -07004071CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004072 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004073 Mutex::Autolock al(mLatestRequestMutex);
4074
4075 ALOGV("RequestThread::%s", __FUNCTION__);
4076
4077 return mLatestRequest;
4078}
4079
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004080bool Camera3Device::RequestThread::isStreamPending(
4081 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004082 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004083 Mutex::Autolock l(mRequestLock);
4084
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004085 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004086 if (!nextRequest.submitted) {
4087 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4088 if (stream == s) return true;
4089 }
4090 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004091 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004092 }
4093
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004094 for (const auto& request : mRequestQueue) {
4095 for (const auto& s : request->mOutputStreams) {
4096 if (stream == s) return true;
4097 }
4098 if (stream == request->mInputStream) return true;
4099 }
4100
4101 for (const auto& request : mRepeatingRequests) {
4102 for (const auto& s : request->mOutputStreams) {
4103 if (stream == s) return true;
4104 }
4105 if (stream == request->mInputStream) return true;
4106 }
4107
4108 return false;
4109}
Jianing Weicb0652e2014-03-12 18:29:36 -07004110
Emilian Peev40ead602017-09-26 15:46:36 +01004111bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4112 ATRACE_CALL();
4113 Mutex::Autolock l(mRequestLock);
4114
4115 for (const auto& nextRequest : mNextRequests) {
4116 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4117 if (s.first == streamId) {
4118 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4119 if (it != s.second.end()) {
4120 return true;
4121 }
4122 }
4123 }
4124 }
4125
4126 for (const auto& request : mRequestQueue) {
4127 for (const auto& s : request->mOutputSurfaces) {
4128 if (s.first == streamId) {
4129 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4130 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004131 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004132 }
4133 }
4134 }
4135 }
4136
4137 for (const auto& request : mRepeatingRequests) {
4138 for (const auto& s : request->mOutputSurfaces) {
4139 if (s.first == streamId) {
4140 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4141 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004142 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004143 }
4144 }
4145 }
4146 }
4147
4148 return false;
4149}
4150
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004151void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4152 if (!mUseHalBufManager) {
4153 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4154 return;
4155 }
4156
4157 Mutex::Autolock pl(mPauseLock);
4158 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004159 mInterface->signalPipelineDrain(streamIds);
4160 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004161 }
4162 // If request thread is still busy, wait until paused then notify HAL
4163 mNotifyPipelineDrain = true;
4164 mStreamIdsToBeDrained = streamIds;
4165}
4166
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004167void Camera3Device::RequestThread::resetPipelineDrain() {
4168 Mutex::Autolock pl(mPauseLock);
4169 mNotifyPipelineDrain = false;
4170 mStreamIdsToBeDrained.clear();
4171}
4172
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004173void Camera3Device::RequestThread::clearPreviousRequest() {
4174 Mutex::Autolock l(mRequestLock);
4175 mPrevRequest.clear();
4176}
4177
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004178status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4179 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4180 ATRACE_CALL();
4181 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004182 mRotateAndCropOverride = rotateAndCropValue;
4183 return OK;
4184}
4185
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004186status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4187 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4188 ATRACE_CALL();
4189 Mutex::Autolock l(mTriggerMutex);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004190 mAutoframingOverride = autoframingValue;
4191 return OK;
4192}
4193
Emilian Peeve23f1d92021-09-20 14:56:01 -07004194status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4195 ATRACE_CALL();
4196 Mutex::Autolock l(mTriggerMutex);
4197 mComposerOutput = composerSurfacePresent;
4198 return OK;
4199}
4200
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004201status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004202 ATRACE_CALL();
4203 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004204 if (muteMode != mCameraMute) {
4205 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004206 mCameraMuteChanged = true;
4207 }
4208 return OK;
4209}
4210
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004211status_t Camera3Device::RequestThread::setZoomOverride(int32_t zoomOverride) {
4212 ATRACE_CALL();
4213 Mutex::Autolock l(mTriggerMutex);
4214 mSettingsOverride = zoomOverride;
4215 return OK;
4216}
4217
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004218nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004219 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004220 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004221 return mExpectedInflightDuration > kMinInflightDuration ?
4222 mExpectedInflightDuration : kMinInflightDuration;
4223}
4224
Emilian Peevaebbe412018-01-15 13:53:24 +00004225void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004226 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004227 if ((request == nullptr) || (halRequest == nullptr)) {
4228 ALOGE("%s: Invalid request!", __FUNCTION__);
4229 return;
4230 }
4231
4232 if (halRequest->num_physcam_settings > 0) {
4233 if (halRequest->physcam_id != nullptr) {
4234 delete [] halRequest->physcam_id;
4235 halRequest->physcam_id = nullptr;
4236 }
4237 if (halRequest->physcam_settings != nullptr) {
4238 auto it = ++(request->mSettingsList.begin());
4239 size_t i = 0;
4240 for (; it != request->mSettingsList.end(); it++, i++) {
4241 it->metadata.unlock(halRequest->physcam_settings[i]);
4242 }
4243 delete [] halRequest->physcam_settings;
4244 halRequest->physcam_settings = nullptr;
4245 }
4246 }
4247}
4248
Ravneetaeb20dc2022-03-30 05:33:03 +00004249status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4250 Mutex::Autolock il(mInterfaceLock);
4251 Mutex::Autolock l(mLock);
4252
4253 if (mCameraServiceWatchdog != NULL) {
4254 mCameraServiceWatchdog->setEnabled(enabled);
4255 }
4256
4257 return OK;
4258}
4259
Shuzhen Wang16610a62022-12-15 22:38:07 -08004260void Camera3Device::setStreamUseCaseOverrides(
4261 const std::vector<int64_t>& useCaseOverrides) {
4262 Mutex::Autolock il(mInterfaceLock);
4263 Mutex::Autolock l(mLock);
4264 mStreamUseCaseOverrides = useCaseOverrides;
4265}
4266
4267void Camera3Device::clearStreamUseCaseOverrides() {
4268 Mutex::Autolock il(mInterfaceLock);
4269 Mutex::Autolock l(mLock);
4270 mStreamUseCaseOverrides.clear();
4271}
4272
Shuzhen Wang03fe6232023-02-05 12:41:15 -08004273bool Camera3Device::hasDeviceError() {
4274 Mutex::Autolock il(mInterfaceLock);
4275 Mutex::Autolock l(mLock);
4276 return mStatus == STATUS_ERROR;
4277}
4278
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004279void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4280 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004281 return;
4282 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004283
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004284 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004285 // Skip the ones that have been submitted successfully.
4286 if (nextRequest.submitted) {
4287 continue;
4288 }
4289
4290 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004291 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4292 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004293
4294 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004295 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004296 }
4297
Emilian Peevaebbe412018-01-15 13:53:24 +00004298 cleanupPhysicalSettings(captureRequest, halRequest);
4299
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004300 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004301 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004302 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4303 }
4304
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004305 // No output buffer can be returned when using HAL buffer manager
4306 if (!mUseHalBufManager) {
4307 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4308 //Buffers that failed processing could still have
4309 //valid acquire fence.
4310 int acquireFence = (*outputBuffers)[i].acquire_fence;
4311 if (0 <= acquireFence) {
4312 close(acquireFence);
4313 outputBuffers->editItemAt(i).acquire_fence = -1;
4314 }
Emilian Peevf4816702020-04-03 15:44:51 -07004315 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004316 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4317 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004318 /*timestampIncreasing*/true, std::vector<size_t> (),
4319 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004320 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004321 }
4322
4323 if (sendRequestError) {
4324 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004325 sp<NotificationListener> listener = mListener.promote();
4326 if (listener != NULL) {
4327 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004328 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004329 captureRequest->mResultExtras);
4330 }
4331 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004332
4333 // Remove yet-to-be submitted inflight request from inflightMap
4334 {
4335 sp<Camera3Device> parent = mParent.promote();
4336 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004337 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004338 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4339 if (idx >= 0) {
4340 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4341 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4342 parent->removeInFlightMapEntryLocked(idx);
4343 }
4344 }
4345 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004346 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004347
4348 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004349 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004350}
4351
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004352void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004353 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004354 // Optimized a bit for the simple steady-state case (single repeating
4355 // request), to avoid putting that request in the queue temporarily.
4356 Mutex::Autolock l(mRequestLock);
4357
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004358 assert(mNextRequests.empty());
4359
4360 NextRequest nextRequest;
4361 nextRequest.captureRequest = waitForNextRequestLocked();
4362 if (nextRequest.captureRequest == nullptr) {
4363 return;
4364 }
4365
Emilian Peevf4816702020-04-03 15:44:51 -07004366 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004367 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004368 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004369
4370 // Wait for additional requests
4371 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4372
4373 for (size_t i = 1; i < batchSize; i++) {
4374 NextRequest additionalRequest;
4375 additionalRequest.captureRequest = waitForNextRequestLocked();
4376 if (additionalRequest.captureRequest == nullptr) {
4377 break;
4378 }
4379
Emilian Peevf4816702020-04-03 15:44:51 -07004380 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004381 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004382 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004383 }
4384
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004385 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004386 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004387 mNextRequests.size(), batchSize);
4388 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004389 }
4390
4391 return;
4392}
4393
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004394void Camera3Device::RequestThread::setRequestClearing() {
4395 Mutex::Autolock l(mRequestLock);
4396 mRequestClearing = true;
4397}
4398
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004399sp<Camera3Device::CaptureRequest>
4400 Camera3Device::RequestThread::waitForNextRequestLocked() {
4401 status_t res;
4402 sp<CaptureRequest> nextRequest;
4403
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004404 while (mRequestQueue.empty()) {
4405 if (!mRepeatingRequests.empty()) {
4406 // Always atomically enqueue all requests in a repeating request
4407 // list. Guarantees a complete in-sequence set of captures to
4408 // application.
4409 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004410 if (mFirstRepeating) {
4411 mFirstRepeating = false;
4412 } else {
4413 for (auto& request : requests) {
4414 // For repeating requests, override timestamp request using
4415 // the time a request is inserted into the request queue,
4416 // because the original repeating request will have an old
4417 // fixed timestamp.
4418 request->mRequestTimeNs = systemTime();
4419 }
4420 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004421 RequestList::const_iterator firstRequest =
4422 requests.begin();
4423 nextRequest = *firstRequest;
4424 mRequestQueue.insert(mRequestQueue.end(),
4425 ++firstRequest,
4426 requests.end());
4427 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004428
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004429 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004430
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004431 break;
4432 }
4433
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004434 if (!mRequestClearing) {
4435 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4436 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004437
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004438 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4439 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004440 Mutex::Autolock pl(mPauseLock);
4441 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004442 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004443 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004444 if (mNotifyPipelineDrain) {
4445 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4446 mNotifyPipelineDrain = false;
4447 mStreamIdsToBeDrained.clear();
4448 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004449 // Let the tracker know
4450 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4451 if (statusTracker != 0) {
4452 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4453 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004454 sp<Camera3Device> parent = mParent.promote();
4455 if (parent != nullptr) {
4456 parent->mRequestBufferSM.onRequestThreadPaused();
4457 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004458 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004459 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004460 // Stop waiting for now and let thread management happen
4461 return NULL;
4462 }
4463 }
4464
4465 if (nextRequest == NULL) {
4466 // Don't have a repeating request already in hand, so queue
4467 // must have an entry now.
4468 RequestList::iterator firstRequest =
4469 mRequestQueue.begin();
4470 nextRequest = *firstRequest;
4471 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004472 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4473 sp<NotificationListener> listener = mListener.promote();
4474 if (listener != NULL) {
4475 listener->notifyRequestQueueEmpty();
4476 }
4477 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004478 }
4479
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004480 // In case we've been unpaused by setPaused clearing mDoPause, need to
4481 // update internal pause state (capture/setRepeatingRequest unpause
4482 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004483 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004484 if (mPaused) {
4485 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4486 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4487 if (statusTracker != 0) {
4488 statusTracker->markComponentActive(mStatusId);
4489 }
4490 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004491 mPaused = false;
4492
4493 // Check if we've reconfigured since last time, and reset the preview
4494 // request if so. Can't use 'NULL request == repeat' across configure calls.
4495 if (mReconfigured) {
4496 mPrevRequest.clear();
4497 mReconfigured = false;
4498 }
4499
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004500 if (nextRequest != NULL) {
4501 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004502 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4503 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004504
4505 // Since RequestThread::clear() removes buffers from the input stream,
4506 // get the right buffer here before unlocking mRequestLock
4507 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004508 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4509 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004510 if (res != OK) {
4511 // Can't get input buffer from gralloc queue - this could be due to
4512 // disconnected queue or other producer misbehavior, so not a fatal
4513 // error
4514 ALOGE("%s: Can't get input buffer, skipping request:"
4515 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004516
4517 sp<NotificationListener> listener = mListener.promote();
4518 if (listener != NULL) {
4519 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004520 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004521 nextRequest->mResultExtras);
4522 }
4523 return NULL;
4524 }
4525 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004526 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004527
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004528 return nextRequest;
4529}
4530
4531bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004532 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004533 status_t res;
4534 Mutex::Autolock l(mPauseLock);
4535 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004536 if (mPaused == false) {
4537 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004538 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004539 if (mNotifyPipelineDrain) {
4540 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4541 mNotifyPipelineDrain = false;
4542 mStreamIdsToBeDrained.clear();
4543 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004544 // Let the tracker know
4545 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4546 if (statusTracker != 0) {
4547 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4548 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004549 sp<Camera3Device> parent = mParent.promote();
4550 if (parent != nullptr) {
4551 parent->mRequestBufferSM.onRequestThreadPaused();
4552 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004553 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004554
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004555 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004556 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004557 return true;
4558 }
4559 }
4560 // We don't set mPaused to false here, because waitForNextRequest needs
4561 // to further manage the paused state in case of starvation.
4562 return false;
4563}
4564
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004565void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004566 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004567 // With work to do, mark thread as unpaused.
4568 // If paused by request (setPaused), don't resume, to avoid
4569 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004570 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004571 Mutex::Autolock p(mPauseLock);
4572 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004573 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4574 if (mPaused) {
4575 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4576 if (statusTracker != 0) {
4577 statusTracker->markComponentActive(mStatusId);
4578 }
4579 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004580 mPaused = false;
4581 }
4582}
4583
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004584void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4585 sp<Camera3Device> parent = mParent.promote();
4586 if (parent != NULL) {
4587 va_list args;
4588 va_start(args, fmt);
4589
4590 parent->setErrorStateV(fmt, args);
4591
4592 va_end(args);
4593 }
4594}
4595
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004596status_t Camera3Device::RequestThread::insertTriggers(
4597 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004598 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004599 Mutex::Autolock al(mTriggerMutex);
4600
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004601 sp<Camera3Device> parent = mParent.promote();
4602 if (parent == NULL) {
4603 CLOGE("RequestThread: Parent is gone");
4604 return DEAD_OBJECT;
4605 }
4606
Emilian Peevaebbe412018-01-15 13:53:24 +00004607 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004608 size_t count = mTriggerMap.size();
4609
4610 for (size_t i = 0; i < count; ++i) {
4611 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004612 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004613
4614 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4615 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4616 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004617 if (isAeTrigger) {
4618 request->mResultExtras.precaptureTriggerId = triggerId;
4619 mCurrentPreCaptureTriggerId = triggerId;
4620 } else {
4621 request->mResultExtras.afTriggerId = triggerId;
4622 mCurrentAfTriggerId = triggerId;
4623 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004624 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004625 }
4626
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004627 camera_metadata_entry entry = metadata.find(tag);
4628
4629 if (entry.count > 0) {
4630 /**
4631 * Already has an entry for this trigger in the request.
4632 * Rewrite it with our requested trigger value.
4633 */
4634 RequestTrigger oldTrigger = trigger;
4635
4636 oldTrigger.entryValue = entry.data.u8[0];
4637
4638 mTriggerReplacedMap.add(tag, oldTrigger);
4639 } else {
4640 /**
4641 * More typical, no trigger entry, so we just add it
4642 */
4643 mTriggerRemovedMap.add(tag, trigger);
4644 }
4645
4646 status_t res;
4647
4648 switch (trigger.getTagType()) {
4649 case TYPE_BYTE: {
4650 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4651 res = metadata.update(tag,
4652 &entryValue,
4653 /*count*/1);
4654 break;
4655 }
4656 case TYPE_INT32:
4657 res = metadata.update(tag,
4658 &trigger.entryValue,
4659 /*count*/1);
4660 break;
4661 default:
4662 ALOGE("%s: Type not supported: 0x%x",
4663 __FUNCTION__,
4664 trigger.getTagType());
4665 return INVALID_OPERATION;
4666 }
4667
4668 if (res != OK) {
4669 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4670 ", value %d", __FUNCTION__, trigger.getTagName(),
4671 trigger.entryValue);
4672 return res;
4673 }
4674
4675 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4676 trigger.getTagName(),
4677 trigger.entryValue);
4678 }
4679
4680 mTriggerMap.clear();
4681
4682 return count;
4683}
4684
4685status_t Camera3Device::RequestThread::removeTriggers(
4686 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004687 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004688 Mutex::Autolock al(mTriggerMutex);
4689
Emilian Peevaebbe412018-01-15 13:53:24 +00004690 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004691
4692 /**
4693 * Replace all old entries with their old values.
4694 */
4695 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4696 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4697
4698 status_t res;
4699
4700 uint32_t tag = trigger.metadataTag;
4701 switch (trigger.getTagType()) {
4702 case TYPE_BYTE: {
4703 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4704 res = metadata.update(tag,
4705 &entryValue,
4706 /*count*/1);
4707 break;
4708 }
4709 case TYPE_INT32:
4710 res = metadata.update(tag,
4711 &trigger.entryValue,
4712 /*count*/1);
4713 break;
4714 default:
4715 ALOGE("%s: Type not supported: 0x%x",
4716 __FUNCTION__,
4717 trigger.getTagType());
4718 return INVALID_OPERATION;
4719 }
4720
4721 if (res != OK) {
4722 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4723 ", trigger value %d", __FUNCTION__,
4724 trigger.getTagName(), trigger.entryValue);
4725 return res;
4726 }
4727 }
4728 mTriggerReplacedMap.clear();
4729
4730 /**
4731 * Remove all new entries.
4732 */
4733 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4734 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4735 status_t res = metadata.erase(trigger.metadataTag);
4736
4737 if (res != OK) {
4738 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4739 ", trigger value %d", __FUNCTION__,
4740 trigger.getTagName(), trigger.entryValue);
4741 return res;
4742 }
4743 }
4744 mTriggerRemovedMap.clear();
4745
4746 return OK;
4747}
4748
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004749status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004750 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004751 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004752 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004753 status_t res;
4754
Emilian Peevaebbe412018-01-15 13:53:24 +00004755 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004756
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004757 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004758 // exists
4759 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4760 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4761 if (afTrigger.count > 0 &&
4762 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4763 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004764 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004765 if (res != OK) return res;
4766 }
4767
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004768 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004769 // if none already exists
4770 camera_metadata_entry pcTrigger =
4771 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4772 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4773 if (pcTrigger.count > 0 &&
4774 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4775 pcId.count == 0) {
4776 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004777 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004778 if (res != OK) return res;
4779 }
4780
4781 return OK;
4782}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004783
Emilian Peevd865f0d2023-03-17 17:13:07 -07004784bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4785 ATRACE_CALL();
4786 Mutex::Autolock l(mTriggerMutex);
4787 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4788 this->mRotateAndCropOverride);
4789}
4790
4791bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4792 bool overrideToPortrait,
4793 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004794 ATRACE_CALL();
4795
Emilian Peevd865f0d2023-03-17 17:13:07 -07004796 if (overrideToPortrait) {
4797 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
Austin Borger18b30a72022-10-27 12:20:29 -07004798 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4799 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4800 &rotateAndCrop_u8, 1);
4801 return true;
4802 }
4803
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004804 if (request->mRotateAndCropAuto) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004805 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4806
4807 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4808 if (rotateAndCropEntry.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004809 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004810 return false;
4811 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004812 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004813 return true;
4814 }
4815 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004816 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4817 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004818 return true;
4819 }
4820 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004821
4822 return false;
4823}
4824
4825bool Camera3Device::overrideAutoframing(const sp<CaptureRequest> &request /*out*/,
4826 camera_metadata_enum_android_control_autoframing_t autoframingOverride) {
4827 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4828 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4829 if (autoframingEntry.count > 0) {
4830 if (autoframingEntry.data.u8[0] == autoframingOverride) {
4831 return false;
4832 } else {
4833 autoframingEntry.data.u8[0] = autoframingOverride;
4834 return true;
4835 }
4836 } else {
4837 uint8_t autoframing_u8 = autoframingOverride;
4838 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4839 &autoframing_u8, 1);
4840 return true;
4841 }
4842
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004843 return false;
4844}
4845
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004846bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4847 ATRACE_CALL();
4848
4849 if (request->mAutoframingAuto) {
4850 Mutex::Autolock l(mTriggerMutex);
Emilian Peevd865f0d2023-03-17 17:13:07 -07004851 return Camera3Device::overrideAutoframing(request, mAutoframingOverride);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004852 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004853
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004854 return false;
4855}
4856
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004857bool Camera3Device::RequestThread::overrideTestPattern(
4858 const sp<CaptureRequest> &request) {
4859 ATRACE_CALL();
4860
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004861 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004862
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004863 Mutex::Autolock l(mTriggerMutex);
4864
4865 bool changed = false;
4866
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004867 // For a multi-camera, the physical cameras support the same set of
4868 // test pattern modes as the logical camera.
4869 for (auto& settings : request->mSettingsList) {
4870 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004871
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004872 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4873 int32_t testPatternData[4] = {
4874 settings.mOriginalTestPatternData[0],
4875 settings.mOriginalTestPatternData[1],
4876 settings.mOriginalTestPatternData[2],
4877 settings.mOriginalTestPatternData[3]
4878 };
4879 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4880 testPatternMode = mCameraMute;
4881 testPatternData[0] = 0;
4882 testPatternData[1] = 0;
4883 testPatternData[2] = 0;
4884 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004885 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004886
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004887 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4888 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4889 if (testPatternEntry.count > 0) {
4890 if (testPatternEntry.data.i32[0] != testPatternMode) {
4891 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004892 changed = true;
4893 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004894 } else if (supportTestPatternModeKey) {
4895 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4896 &testPatternMode, 1);
4897 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004898 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004899
4900 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4901 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4902 if (testPatternColor.count >= 4) {
4903 for (size_t i = 0; i < 4; i++) {
4904 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4905 testPatternColor.data.i32[i] = testPatternData[i];
4906 changed = true;
4907 }
4908 }
4909 } else if (supportTestPatternDataKey) {
4910 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4911 testPatternData, 4);
4912 changed = true;
4913 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004914 }
4915
4916 return changed;
4917}
4918
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004919bool Camera3Device::RequestThread::overrideSettingsOverride(
4920 const sp<CaptureRequest> &request) {
4921 ATRACE_CALL();
4922
4923 if (!mSupportSettingsOverride) return false;
4924
4925 Mutex::Autolock l(mTriggerMutex);
4926
4927 // For a multi-camera, only override the logical camera's metadata.
4928 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4929 camera_metadata_entry entry = metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
4930 int32_t originalValue = request->mSettingsList.begin()->mOriginalSettingsOverride;
4931 if (mSettingsOverride != -1 &&
4932 (entry.count == 0 || entry.data.i32[0] != mSettingsOverride)) {
4933 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4934 &mSettingsOverride, 1);
4935 return true;
4936 } else if (mSettingsOverride == -1 &&
4937 (entry.count == 0 || entry.data.i32[0] != originalValue)) {
4938 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4939 &originalValue, 1);
4940 return true;
4941 }
4942
4943 return false;
4944}
4945
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004946status_t Camera3Device::RequestThread::setHalInterface(
4947 sp<HalInterface> newHalInterface) {
4948 if (newHalInterface.get() == nullptr) {
4949 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4950 return DEAD_OBJECT;
4951 }
4952
4953 mInterface = newHalInterface;
4954
4955 return OK;
4956}
4957
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004958/**
4959 * PreparerThread inner class methods
4960 */
4961
4962Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004963 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004964 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004965}
4966
4967Camera3Device::PreparerThread::~PreparerThread() {
4968 Thread::requestExitAndWait();
4969 if (mCurrentStream != nullptr) {
4970 mCurrentStream->cancelPrepare();
4971 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4972 mCurrentStream.clear();
4973 }
4974 clear();
4975}
4976
Ruben Brunkc78ac262015-08-13 17:58:46 -07004977status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004978 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004979 status_t res;
4980
4981 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004982 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004983
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004984 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004985 if (res == OK) {
4986 // No preparation needed, fire listener right off
4987 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004988 if (listener != NULL) {
4989 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004990 }
4991 return OK;
4992 } else if (res != NOT_ENOUGH_DATA) {
4993 return res;
4994 }
4995
4996 // Need to prepare, start up thread if necessary
4997 if (!mActive) {
4998 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4999 // isn't running
5000 Thread::requestExitAndWait();
5001 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5002 if (res != OK) {
5003 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005004 if (listener != NULL) {
5005 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005006 }
5007 return res;
5008 }
5009 mCancelNow = false;
5010 mActive = true;
5011 ALOGV("%s: Preparer stream started", __FUNCTION__);
5012 }
5013
5014 // queue up the work
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005015 mPendingStreams.push_back(
5016 std::tuple<int, sp<camera3::Camera3StreamInterface>>(maxCount, stream));
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005017 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5018
5019 return OK;
5020}
5021
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005022void Camera3Device::PreparerThread::pause() {
5023 ATRACE_CALL();
5024
5025 Mutex::Autolock l(mLock);
5026
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005027 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> pendingStreams;
5028 pendingStreams.insert(pendingStreams.begin(), mPendingStreams.begin(), mPendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005029 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5030 int currentMaxCount = mCurrentMaxCount;
5031 mPendingStreams.clear();
5032 mCancelNow = true;
5033 while (mActive) {
5034 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5035 if (res == TIMED_OUT) {
5036 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5037 return;
5038 } else if (res != OK) {
5039 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5040 return;
5041 }
5042 }
5043
5044 //Check whether the prepare thread was able to complete the current
5045 //stream. In case work is still pending emplace it along with the rest
5046 //of the streams in the pending list.
5047 if (currentStream != nullptr) {
5048 if (!mCurrentPrepareComplete) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005049 pendingStreams.push_back(std::tuple(currentMaxCount, currentStream));
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005050 }
5051 }
5052
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005053 mPendingStreams.insert(mPendingStreams.begin(), pendingStreams.begin(), pendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005054 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005055 std::get<1>(it)->cancelPrepare();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005056 }
5057}
5058
5059status_t Camera3Device::PreparerThread::resume() {
5060 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005061 ALOGV("%s: PreparerThread", __FUNCTION__);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005062 status_t res;
5063
5064 Mutex::Autolock l(mLock);
5065 sp<NotificationListener> listener = mListener.promote();
5066
5067 if (mActive) {
5068 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5069 return NO_INIT;
5070 }
5071
5072 auto it = mPendingStreams.begin();
5073 for (; it != mPendingStreams.end();) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005074 res = std::get<1>(*it)->startPrepare(std::get<0>(*it), true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005075 if (res == OK) {
5076 if (listener != NULL) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005077 listener->notifyPrepared(std::get<1>(*it)->getId());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005078 }
5079 it = mPendingStreams.erase(it);
5080 } else if (res != NOT_ENOUGH_DATA) {
5081 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5082 res, strerror(-res));
5083 it = mPendingStreams.erase(it);
5084 } else {
5085 it++;
5086 }
5087 }
5088
5089 if (mPendingStreams.empty()) {
5090 return OK;
5091 }
5092
5093 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5094 if (res != OK) {
5095 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5096 __FUNCTION__, res, strerror(-res));
5097 return res;
5098 }
5099 mCancelNow = false;
5100 mActive = true;
5101 ALOGV("%s: Preparer stream started", __FUNCTION__);
5102
5103 return OK;
5104}
5105
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005106status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005107 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005108 Mutex::Autolock l(mLock);
5109
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005110 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005111 std::get<1>(it)->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005112 }
5113 mPendingStreams.clear();
5114 mCancelNow = true;
5115
5116 return OK;
5117}
5118
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005119void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005120 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005121 Mutex::Autolock l(mLock);
5122 mListener = listener;
5123}
5124
5125bool Camera3Device::PreparerThread::threadLoop() {
5126 status_t res;
5127 {
5128 Mutex::Autolock l(mLock);
5129 if (mCurrentStream == nullptr) {
5130 // End thread if done with work
5131 if (mPendingStreams.empty()) {
5132 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5133 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5134 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5135 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005136 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005137 return false;
5138 }
5139
5140 // Get next stream to prepare
5141 auto it = mPendingStreams.begin();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005142 mCurrentMaxCount = std::get<0>(*it);
5143 mCurrentStream = std::get<1>(*it);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005144 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005145 mPendingStreams.erase(it);
5146 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5147 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5148 } else if (mCancelNow) {
5149 mCurrentStream->cancelPrepare();
5150 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5151 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5152 mCurrentStream.clear();
5153 mCancelNow = false;
5154 return true;
5155 }
5156 }
5157
5158 res = mCurrentStream->prepareNextBuffer();
5159 if (res == NOT_ENOUGH_DATA) return true;
5160 if (res != OK) {
5161 // Something bad happened; try to recover by cancelling prepare and
5162 // signalling listener anyway
5163 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5164 mCurrentStream->getId(), res, strerror(-res));
5165 mCurrentStream->cancelPrepare();
5166 }
5167
5168 // This stream has finished, notify listener
5169 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005170 sp<NotificationListener> listener = mListener.promote();
5171 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005172 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5173 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005174 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005175 }
5176
5177 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5178 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005179 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005180
5181 return true;
5182}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005183
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005184status_t Camera3Device::RequestBufferStateMachine::initialize(
5185 sp<camera3::StatusTracker> statusTracker) {
5186 if (statusTracker == nullptr) {
5187 ALOGE("%s: statusTracker is null", __FUNCTION__);
5188 return BAD_VALUE;
5189 }
5190
5191 std::lock_guard<std::mutex> lock(mLock);
5192 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005193 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005194 return OK;
5195}
5196
5197bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5198 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005199 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005200 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005201 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005202 return true;
5203 }
5204 return false;
5205}
5206
5207void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5208 std::lock_guard<std::mutex> lock(mLock);
5209 if (!mRequestBufferOngoing) {
5210 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5211 return;
5212 }
5213 mRequestBufferOngoing = false;
5214 if (mStatus == RB_STATUS_PENDING_STOP) {
5215 checkSwitchToStopLocked();
5216 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005217 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005218}
5219
5220void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5221 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005222 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005223 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005224 return;
5225}
5226
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005227void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005228 std::lock_guard<std::mutex> lock(mLock);
5229 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005230 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5231 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005232 mInflightMapEmpty = false;
5233 if (mStatus == RB_STATUS_STOPPED) {
5234 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005235 }
5236 return;
5237}
5238
5239void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5240 std::lock_guard<std::mutex> lock(mLock);
5241 mRequestThreadPaused = true;
5242 if (mStatus == RB_STATUS_PENDING_STOP) {
5243 checkSwitchToStopLocked();
5244 }
5245 return;
5246}
5247
5248void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5249 std::lock_guard<std::mutex> lock(mLock);
5250 mInflightMapEmpty = true;
5251 if (mStatus == RB_STATUS_PENDING_STOP) {
5252 checkSwitchToStopLocked();
5253 }
5254 return;
5255}
5256
5257void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5258 std::lock_guard<std::mutex> lock(mLock);
5259 if (!checkSwitchToStopLocked()) {
5260 mStatus = RB_STATUS_PENDING_STOP;
5261 }
5262 return;
5263}
5264
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005265bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5266 std::lock_guard<std::mutex> lock(mLock);
5267 if (mRequestBufferOngoing) {
5268 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5269 __FUNCTION__);
5270 return false;
5271 }
5272 mSwitchedToOffline = true;
5273 mInflightMapEmpty = true;
5274 mRequestThreadPaused = true;
5275 mStatus = RB_STATUS_STOPPED;
5276 return true;
5277}
5278
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005279void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5280 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5281 if (statusTracker != nullptr) {
5282 if (active) {
5283 statusTracker->markComponentActive(mRequestBufferStatusId);
5284 } else {
5285 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5286 }
5287 }
5288}
5289
5290bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5291 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5292 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005293 return true;
5294 }
5295 return false;
5296}
5297
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005298bool Camera3Device::startRequestBuffer() {
5299 return mRequestBufferSM.startRequestBuffer();
5300}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005301
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005302void Camera3Device::endRequestBuffer() {
5303 mRequestBufferSM.endRequestBuffer();
5304}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005305
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005306nsecs_t Camera3Device::getWaitDuration() {
5307 return kBaseGetBufferWait + getExpectedInFlightDuration();
5308}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005309
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005310void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5311 mInterface->getInflightBufferKeys(out);
5312}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005313
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005314void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5315 mInterface->getInflightRequestBufferKeys(out);
5316}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005317
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005318std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5319 std::vector<sp<Camera3StreamInterface>> ret;
5320 bool hasInputStream = mInputStream != nullptr;
5321 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5322 if (hasInputStream) {
5323 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005324 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005325 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5326 ret.push_back(mOutputStreams[i]);
5327 }
5328 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5329 ret.push_back(mDeletedStreams[i]);
5330 }
5331 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005332}
5333
Emilian Peevcc0b7952020-01-07 13:54:47 -08005334void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5335 ATRACE_CALL();
5336
5337 if (offlineStreamIds == nullptr) {
5338 return;
5339 }
5340
5341 Mutex::Autolock il(mInterfaceLock);
5342
5343 auto streamIds = mOutputStreams.getStreamIds();
5344 bool hasInputStream = mInputStream != nullptr;
5345 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5346 offlineStreamIds->push_back(mInputStream->getId());
5347 }
5348
5349 for (const auto & streamId : streamIds) {
5350 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5351 // Streams that use the camera buffer manager are currently not supported in
5352 // offline mode
5353 if (stream->getOfflineProcessingSupport() &&
5354 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5355 offlineStreamIds->push_back(streamId);
5356 }
5357 }
5358}
5359
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005360status_t Camera3Device::setRotateAndCropAutoBehavior(
5361 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5362 ATRACE_CALL();
5363 Mutex::Autolock il(mInterfaceLock);
5364 Mutex::Autolock l(mLock);
5365 if (mRequestThread == nullptr) {
5366 return INVALID_OPERATION;
5367 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005368 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5369 return BAD_VALUE;
5370 }
5371 mRotateAndCropOverride = rotateAndCropValue;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005372 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5373}
5374
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005375status_t Camera3Device::setAutoframingAutoBehavior(
5376 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5377 ATRACE_CALL();
5378 Mutex::Autolock il(mInterfaceLock);
5379 Mutex::Autolock l(mLock);
5380 if (mRequestThread == nullptr) {
5381 return INVALID_OPERATION;
5382 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005383 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
5384 return BAD_VALUE;
5385 }
5386 mAutoframingOverride = autoframingValue;
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005387 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5388}
5389
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005390bool Camera3Device::supportsCameraMute() {
5391 Mutex::Autolock il(mInterfaceLock);
5392 Mutex::Autolock l(mLock);
5393
5394 return mSupportCameraMute;
5395}
5396
5397status_t Camera3Device::setCameraMute(bool enabled) {
5398 ATRACE_CALL();
5399 Mutex::Autolock il(mInterfaceLock);
5400 Mutex::Autolock l(mLock);
5401
5402 if (mRequestThread == nullptr || !mSupportCameraMute) {
5403 return INVALID_OPERATION;
5404 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005405 int32_t muteMode =
5406 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5407 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5408 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5409 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005410}
5411
Shuzhen Wangaf22e912023-04-11 16:03:17 -07005412bool Camera3Device::supportsZoomOverride() {
5413 Mutex::Autolock il(mInterfaceLock);
5414 Mutex::Autolock l(mLock);
5415
5416 return mSupportZoomOverride;
5417}
5418
5419status_t Camera3Device::setZoomOverride(int32_t zoomOverride) {
5420 ATRACE_CALL();
5421 Mutex::Autolock il(mInterfaceLock);
5422 Mutex::Autolock l(mLock);
5423
5424 if (mRequestThread == nullptr || !mSupportZoomOverride) {
5425 return INVALID_OPERATION;
5426 }
5427
5428 return mRequestThread->setZoomOverride(zoomOverride);
5429}
5430
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005431status_t Camera3Device::injectCamera(const String8& injectedCamId,
5432 sp<CameraProviderManager> manager) {
5433 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5434 ATRACE_CALL();
5435 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005436 // When the camera device is active, injectCamera() and stopInjection() will call
5437 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5438 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5439 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5440 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5441 // waitUntilStateThenRelock().
5442 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005443
5444 status_t res = NO_ERROR;
5445 if (mInjectionMethods->isInjecting()) {
5446 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5447 return OK;
5448 } else {
5449 res = mInjectionMethods->stopInjection();
5450 if (res != OK) {
5451 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5452 __FUNCTION__, res);
5453 return res;
5454 }
5455 }
5456 }
5457
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005458 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005459 if (res != OK) {
5460 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5461 __FUNCTION__, res);
5462 return res;
5463 }
5464
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005465 // When the second display of android is cast to the remote device, and the opened camera is
5466 // also cast to the second display, in this case, because the camera has configured the streams
5467 // at this time, we can directly call injectCamera() to replace the internal camera with
5468 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005469 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5470 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5471
5472 camera3::camera_stream_configuration injectionConfig;
5473 std::vector<uint32_t> injectionBufferSizes;
5474 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5475 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5476 || injectionBufferSizes.size() <= 0) {
5477 ALOGE("Failed to inject camera due to abandoned configuration! "
5478 "mOperatingMode: %d injectionConfig.num_streams: %d "
5479 "injectionBufferSizes.size(): %zu", mOperatingMode,
5480 injectionConfig.num_streams, injectionBufferSizes.size());
5481 return DEAD_OBJECT;
5482 }
5483
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005484 res = mInjectionMethods->injectCamera(
5485 injectionConfig, injectionBufferSizes);
5486 if (res != OK) {
5487 ALOGE("Can't finish inject camera process!");
5488 return res;
5489 }
5490 }
5491
5492 return OK;
5493}
5494
5495status_t Camera3Device::stopInjection() {
5496 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5497 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005498 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005499 return mInjectionMethods->stopInjection();
5500}
5501
Shuzhen Wang16610a62022-12-15 22:38:07 -08005502void Camera3Device::overrideStreamUseCaseLocked() {
5503 if (mStreamUseCaseOverrides.size() == 0) {
5504 return;
5505 }
5506
5507 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5508 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5509 // Refer to CameraService::printHelp for details.
5510 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5511 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5512 outputStreamsIndices[i] = i;
5513 }
5514
5515 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5516 [&](int a, int b) -> bool {
5517
5518 auto formatScore = [](int format) {
5519 switch (format) {
5520 case HAL_PIXEL_FORMAT_BLOB:
5521 return 4;
5522 case HAL_PIXEL_FORMAT_RAW16:
5523 case HAL_PIXEL_FORMAT_RAW10:
5524 case HAL_PIXEL_FORMAT_RAW12:
5525 return 3;
5526 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5527 return 2;
5528 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5529 return 1;
5530 default:
5531 return 0;
5532 }
5533 };
5534
5535 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5536 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5537 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5538 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5539 if (sizeA > sizeB ||
5540 (sizeA == sizeB && formatAScore >= formatBScore)) {
5541 return true;
5542 } else {
5543 return false;
5544 }
5545 });
5546
5547 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5548 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5549 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5550 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5551 }
5552}
5553
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005554}; // namespace android