blob: 28a150cf09285ce76db699ce1078ea440c941ffe [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080068#include <tuple>
69
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080070using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080071using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072
73namespace android {
74
Austin Borger74fca042022-05-23 12:41:21 -070075Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
Austin Borger18b30a72022-10-27 12:20:29 -070076 const String8 &id, bool overrideForPerfClass, bool overrideToPortrait, bool legacyClient):
Austin Borger74fca042022-05-23 12:41:21 -070077 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080078 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070079 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080080 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070081 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070082 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070083 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070084 mUsePartialResult(false),
85 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080086 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080087 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070088 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070089 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070090 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070092 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000094 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010095 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070096 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070097 mNeedFixupMonochromeTags(false),
Austin Borger18b30a72022-10-27 12:20:29 -070098 mOverrideForPerfClass(overrideForPerfClass),
99 mOverrideToPortrait(overrideToPortrait)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100{
101 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800102 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103}
104
105Camera3Device::~Camera3Device()
106{
107 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800108 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700109 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110}
111
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800112const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800113 return mId;
114}
115
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800116status_t Camera3Device::initializeCommonLocked() {
117
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700118 /** Start up status tracker thread */
119 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700121 if (res != OK) {
122 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
123 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800124 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700125 mStatusTracker.clear();
126 return res;
127 }
128
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700129 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700130 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700131
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700132 if (mUseHalBufManager) {
133 res = mRequestBufferSM.initialize(mStatusTracker);
134 if (res != OK) {
135 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
136 strerror(-res), res);
137 mInterface->close();
138 mStatusTracker.clear();
139 return res;
140 }
141 }
142
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800143 /** Create buffer manager */
144 mBufferManager = new Camera3BufferManager();
145
146 Vector<int32_t> sessionParamKeys;
147 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
148 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
149 if (sessionKeysEntry.count > 0) {
150 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
151 }
152
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700153 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
154 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
155 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
156 if (availableTestPatternModes.data.i32[i] ==
157 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
158 mSupportCameraMute = true;
159 mSupportTestPatternSolidColor = true;
160 break;
161 } else if (availableTestPatternModes.data.i32[i] ==
162 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
163 mSupportCameraMute = true;
164 mSupportTestPatternSolidColor = false;
165 }
166 }
167
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700168 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800169 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700170 this, mStatusTracker, mInterface, sessionParamKeys,
Austin Borger18b30a72022-10-27 12:20:29 -0700171 mUseHalBufManager, mSupportCameraMute, mOverrideToPortrait);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800172 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800173 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700174 SET_ERR_L("Unable to start request queue thread: %s (%d)",
175 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800176 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800177 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178 return res;
179 }
180
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700181 mPreparerThread = new PreparerThread();
182
Ruben Brunk183f0562015-08-12 12:55:02 -0700183 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400185 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700186 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800188 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800190 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800191 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800192 camera_metadata_entry timestampSource =
193 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
194 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
195 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800196 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800197 }
198
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700199 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100200 camera_metadata_entry partialResultsCount =
201 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
202 if (partialResultsCount.count > 0) {
203 mNumPartialResults = partialResultsCount.data.i32[0];
204 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700205 }
206
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800207 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
208 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700209 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700210 if (res != OK) {
211 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
212 return res;
213 }
214 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800215
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800216 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800217 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800218
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700219 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
220 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
221 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
222 }
223
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800224 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
225 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
226 }
227
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800228 // Hidl/AidlCamera3DeviceInjectionMethods
229 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800230
Ravneetaeb20dc2022-03-30 05:33:03 +0000231 /** Start watchdog thread */
232 mCameraServiceWatchdog = new CameraServiceWatchdog();
Austin Borger7b129542022-06-09 13:23:06 -0700233 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
234 if (res != OK) {
235 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
236 strerror(-res), res);
237 return res;
238 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000239
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800240 return OK;
241}
242
243status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700244 return disconnectImpl();
245}
246
247status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800248 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700249 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800250
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700251 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700252 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800254 Mutex::Autolock il(mInterfaceLock);
255 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
256 {
257 Mutex::Autolock l(mLock);
258 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000260 if (mRequestThread != NULL) {
261 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
262 res = mRequestThread->clear();
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 if (res != OK) {
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000264 SET_ERR_L("Can't stop streaming");
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800265 // Continue to close device even in case of error
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000266 } else {
267 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
268 if (res != OK) {
269 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
270 maxExpectedDuration);
271 // Continue to close device even in case of error
272 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800273 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 }
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000275 // Signal to request thread that we're not expecting any
276 // more requests. This will be true since once we're in
277 // disconnect and we've cleared off the request queue, the
278 // request thread can't receive any new requests through
279 // binder calls - since disconnect holds
280 // mBinderSerialization lock.
281 mRequestThread->setRequestClearing();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700282 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800283
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800284 if (mStatus == STATUS_ERROR) {
285 CLOGE("Shutting down in an error state");
286 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800288 if (mStatusTracker != NULL) {
289 mStatusTracker->requestExit();
290 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800292 if (mRequestThread != NULL) {
293 mRequestThread->requestExit();
294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800296 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
297 for (size_t i = 0; i < mOutputStreams.size(); i++) {
298 streams.push_back(mOutputStreams[i]);
299 }
300 if (mInputStream != nullptr) {
301 streams.push_back(mInputStream);
302 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700303 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700304 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800305 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
306 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700307 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
308 // HAL may be in a bad state, so waiting for request thread
309 // (which may be stuck in the HAL processCaptureRequest call)
310 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800311 // give up mInterfaceLock here and then lock it again. Could this lead
312 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 mRequestThread->join();
314 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700315 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800316 Mutex::Autolock il(mInterfaceLock);
317 if (mStatusTracker != NULL) {
318 mStatusTracker->join();
319 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800320
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800321 if (mInjectionMethods->isInjecting()) {
322 mInjectionMethods->stopInjection();
323 }
324
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800325 HalInterface* interface;
326 {
327 Mutex::Autolock l(mLock);
328 mRequestThread.clear();
329 Mutex::Autolock stLock(mTrackerLock);
330 mStatusTracker.clear();
331 interface = mInterface.get();
332 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700333
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800334 // Call close without internal mutex held, as the HAL close may need to
335 // wait on assorted callbacks,etc, to complete before it can return.
Ravneetdbd5b242022-03-02 07:22:46 +0000336 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700337
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800338 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800340 {
341 Mutex::Autolock l(mLock);
342 mInterface->clear();
343 mOutputStreams.clear();
344 mInputStream.clear();
345 mDeletedStreams.clear();
346 mBufferManager.clear();
347 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
348 }
349
350 for (auto& weakStream : streams) {
351 sp<Camera3StreamInterface> stream = weakStream.promote();
352 if (stream != nullptr) {
353 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
354 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
355 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700356 }
357 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700358 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000359
360 if (mCameraServiceWatchdog != NULL) {
361 mCameraServiceWatchdog->requestExit();
362 mCameraServiceWatchdog.clear();
363 }
364
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700365 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800366}
367
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700368// For dumping/debugging only -
369// try to acquire a lock a few times, eventually give up to proceed with
370// debug/dump operations
371bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
372 bool gotLock = false;
373 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
374 if (lock.tryLock() == NO_ERROR) {
375 gotLock = true;
376 break;
377 } else {
378 usleep(kDumpSleepDuration);
379 }
380 }
381 return gotLock;
382}
383
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800384nsecs_t Camera3Device::getMonoToBoottimeOffset() {
385 // try three times to get the clock offset, choose the one
386 // with the minimum gap in measurements.
387 const int tries = 3;
388 nsecs_t bestGap, measured;
389 for (int i = 0; i < tries; ++i) {
390 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
391 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
392 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
393 const nsecs_t gap = tmono2 - tmono;
394 if (i == 0 || gap < bestGap) {
395 bestGap = gap;
396 measured = tbase - ((tmono + tmono2) >> 1);
397 }
398 }
399 return measured;
400}
401
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800402ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
403 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700404 // Get max jpeg size (area-wise) for default sensor pixel mode
405 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800406 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700407 /*isUltraHighResolutionSensor*/false);
408 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
409 // not ultra high res sensor
410 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800411 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700412 /*isUltraHighResolution*/true);
413 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800414 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
415 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700416 return BAD_VALUE;
417 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700418 bool useMaxSensorPixelModeThreshold = false;
419 if (uhrMaxJpegResolution.width != 0 &&
420 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
421 // Use the ultra high res max jpeg size and max jpeg buffer size
422 useMaxSensorPixelModeThreshold = true;
423 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700424
Zhijun Hef7da0962014-04-24 13:27:56 -0700425 // Get max jpeg buffer size
426 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800427 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700428 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800429 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
430 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700431 return BAD_VALUE;
432 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700433 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700434
435 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
436 if (useMaxSensorPixelModeThreshold) {
437 maxJpegBufferSize =
438 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
439 maxDefaultJpegResolution, maxJpegBufferSize);
440 chosenMaxJpegResolution = uhrMaxJpegResolution;
441 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800442 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700443
444 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700445 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700446 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800447 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
448 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700449 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800450 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
451 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700452 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700453 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700454 return jpegBufferSize;
455}
456
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800457ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700458 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800459 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700460 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800461 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
462 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700463 return BAD_VALUE;
464 }
465 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
466 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
467 return maxBytesForPointCloud;
468}
469
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800470ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
471 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800472 const int PER_CONFIGURATION_SIZE = 3;
473 const int WIDTH_OFFSET = 0;
474 const int HEIGHT_OFFSET = 1;
475 const int SIZE_OFFSET = 2;
476 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800477 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800478 camera3::SessionConfigurationUtils::getAppropriateModeTag(
479 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
480 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800481 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800482 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800483 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
484 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800485 return BAD_VALUE;
486 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700487
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800488 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
489 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
490 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
491 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
492 }
493 }
494
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800495 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
496 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800497 return BAD_VALUE;
498}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700499
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800500status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
501 ATRACE_CALL();
502 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700503
504 // Try to lock, but continue in case of failure (to avoid blocking in
505 // deadlocks)
506 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
507 bool gotLock = tryLockSpinRightRound(mLock);
508
509 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800510 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
511 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700512 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800513 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
514 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700515
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800516 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700517
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800518 String16 templatesOption("-t");
519 int n = args.size();
520 for (int i = 0; i < n; i++) {
521 if (args[i] == templatesOption) {
522 dumpTemplates = true;
523 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000524 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700525 if (i + 1 < n) {
526 String8 monitorTags = String8(args[i + 1]);
527 if (monitorTags == "off") {
528 mTagMonitor.disableMonitoring();
529 } else {
530 mTagMonitor.parseTagsToMonitor(monitorTags);
531 }
532 } else {
533 mTagMonitor.disableMonitoring();
534 }
535 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800536 }
537
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 const char *status =
541 mStatus == STATUS_ERROR ? "ERROR" :
542 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700543 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
544 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800545 mStatus == STATUS_ACTIVE ? "ACTIVE" :
546 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700547
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700549 if (mStatus == STATUS_ERROR) {
550 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
551 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800552 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800553 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000554 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
555 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
556 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800557 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558
559 if (mInputStream != NULL) {
560 write(fd, lines.string(), lines.size());
561 mInputStream->dump(fd, args);
562 } else {
563 lines.appendFormat(" No input stream.\n");
564 write(fd, lines.string(), lines.size());
565 }
566 for (size_t i = 0; i < mOutputStreams.size(); i++) {
567 mOutputStreams[i]->dump(fd,args);
568 }
569
Zhijun He431503c2016-03-07 17:30:16 -0800570 if (mBufferManager != NULL) {
571 lines = String8(" Camera3 Buffer Manager:\n");
572 write(fd, lines.string(), lines.size());
573 mBufferManager->dump(fd, args);
574 }
Zhijun He125684a2015-12-26 15:07:30 -0800575
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700576 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700577 if (mInFlightLock.try_lock()) {
578 if (mInFlightMap.size() == 0) {
579 lines.append(" None\n");
580 } else {
581 for (size_t i = 0; i < mInFlightMap.size(); i++) {
582 InFlightRequest r = mInFlightMap.valueAt(i);
583 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
584 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
585 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
586 r.numBuffersLeft);
587 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700588 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700589 mInFlightLock.unlock();
590 } else {
591 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700592 }
593 write(fd, lines.string(), lines.size());
594
Shuzhen Wang686f6442017-06-20 16:16:04 -0700595 if (mRequestThread != NULL) {
596 mRequestThread->dumpCaptureRequestLatency(fd,
597 " ProcessCaptureRequest latency histogram:");
598 }
599
Igor Murashkin1e479c02013-09-06 16:55:14 -0700600 {
601 lines = String8(" Last request sent:\n");
602 write(fd, lines.string(), lines.size());
603
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700604 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700605 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
606 }
607
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800608 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700609 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800610 "TEMPLATE_PREVIEW",
611 "TEMPLATE_STILL_CAPTURE",
612 "TEMPLATE_VIDEO_RECORD",
613 "TEMPLATE_VIDEO_SNAPSHOT",
614 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800615 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800616 };
617
Emilian Peevf4816702020-04-03 15:44:51 -0700618 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800619 camera_metadata_t *templateRequest = nullptr;
620 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700621 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800622 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800623 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800624 lines.append(" Not supported\n");
625 write(fd, lines.string(), lines.size());
626 } else {
627 write(fd, lines.string(), lines.size());
628 dump_indented_camera_metadata(templateRequest,
629 fd, /*verbosity*/2, /*indentation*/8);
630 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800631 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800632 }
633 }
634
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700635 mTagMonitor.dumpMonitoredMetadata(fd);
636
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800637 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800638 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800639 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800640 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 if (gotLock) mLock.unlock();
644 if (gotInterfaceLock) mInterfaceLock.unlock();
645
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800646 return OK;
647}
648
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700649status_t Camera3Device::startWatchingTags(const String8 &tags) {
650 mTagMonitor.parseTagsToMonitor(tags);
651 return OK;
652}
653
654status_t Camera3Device::stopWatchingTags() {
655 mTagMonitor.disableMonitoring();
656 return OK;
657}
658
659status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
660 mTagMonitor.getLatestMonitoredTagEvents(out);
661 return OK;
662}
663
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800664const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
667 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700668 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 mStatus == STATUS_ERROR ?
670 "when in error state" : "before init");
671 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700672 if (physicalId.isEmpty()) {
673 return mDeviceInfo;
674 } else {
675 std::string id(physicalId.c_str());
676 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
677 return mPhysicalDeviceInfoMap.at(id);
678 } else {
679 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
680 return mDeviceInfo;
681 }
682 }
683}
684
685const CameraMetadata& Camera3Device::info() const {
686 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800687 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800688}
689
Jianing Wei90e59c92014-03-12 18:29:36 -0700690status_t Camera3Device::checkStatusOkToCaptureLocked() {
691 switch (mStatus) {
692 case STATUS_ERROR:
693 CLOGE("Device has encountered a serious error");
694 return INVALID_OPERATION;
695 case STATUS_UNINITIALIZED:
696 CLOGE("Device not initialized");
697 return INVALID_OPERATION;
698 case STATUS_UNCONFIGURED:
699 case STATUS_CONFIGURED:
700 case STATUS_ACTIVE:
701 // OK
702 break;
703 default:
704 SET_ERR_L("Unexpected status: %d", mStatus);
705 return INVALID_OPERATION;
706 }
707 return OK;
708}
709
710status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000711 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700712 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700713 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700714 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700715 if (requestList == NULL) {
716 CLOGE("requestList cannot be NULL.");
717 return BAD_VALUE;
718 }
719
Jianing Weicb0652e2014-03-12 18:29:36 -0700720 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000721 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700722 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
723 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
724 ++metadataIt, ++surfaceMapIt) {
725 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700726 if (newRequest == 0) {
727 CLOGE("Can't create capture request");
728 return BAD_VALUE;
729 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700730
Shuzhen Wang9d066012016-09-30 11:30:20 -0700731 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700732 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700733
Jianing Weicb0652e2014-03-12 18:29:36 -0700734 // Setup burst Id and request Id
735 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800736 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
737 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700738 CLOGE("RequestID does not exist in metadata");
739 return BAD_VALUE;
740 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800741 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700742
Jianing Wei90e59c92014-03-12 18:29:36 -0700743 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700744
745 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700746 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700747 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
748 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
749 return BAD_VALUE;
750 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700751
752 // Setup batch size if this is a high speed video recording request.
753 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
754 auto firstRequest = requestList->begin();
755 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
756 if (outputStream->isVideoStream()) {
757 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800758 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700759 break;
760 }
761 }
762 }
763
Jianing Wei90e59c92014-03-12 18:29:36 -0700764 return OK;
765}
766
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800767status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800768 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800769
Emilian Peevaebbe412018-01-15 13:53:24 +0000770 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700771 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000772 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800774 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700775}
776
Emilian Peevaebbe412018-01-15 13:53:24 +0000777void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700778 std::list<const SurfaceMap>& surfaceMaps,
779 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000780 PhysicalCameraSettingsList requestList;
781 requestList.push_back({std::string(getId().string()), request});
782 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700783
784 SurfaceMap surfaceMap;
785 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
786 // With no surface list passed in, stream and surface will have 1-to-1
787 // mapping. So the surface index is 0 for each stream in the surfaceMap.
788 for (size_t i = 0; i < streams.count; i++) {
789 surfaceMap[streams.data.i32[i]].push_back(0);
790 }
791 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800792}
793
Jianing Wei90e59c92014-03-12 18:29:36 -0700794status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000795 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700796 const std::list<const SurfaceMap> &surfaceMaps,
797 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700798 /*out*/
799 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700800 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700801 nsecs_t requestTimeNs = systemTime();
802
Jianing Wei90e59c92014-03-12 18:29:36 -0700803 Mutex::Autolock il(mInterfaceLock);
804 Mutex::Autolock l(mLock);
805
806 status_t res = checkStatusOkToCaptureLocked();
807 if (res != OK) {
808 // error logged by previous call
809 return res;
810 }
811
812 RequestList requestList;
813
Shuzhen Wang0129d522016-10-30 22:43:41 -0700814 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700815 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700816 if (res != OK) {
817 // error logged by previous call
818 return res;
819 }
820
821 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700822 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700823 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700824 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700825 }
826
827 if (res == OK) {
828 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
829 if (res != OK) {
830 SET_ERR_L("Can't transition to active in %f seconds!",
831 kActiveTimeout/1e9);
832 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800833 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700834 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700835 } else {
836 CLOGE("Cannot queue request. Impossible.");
837 return BAD_VALUE;
838 }
839
840 return res;
841}
842
Emilian Peevaebbe412018-01-15 13:53:24 +0000843status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700844 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700845 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700846 ATRACE_CALL();
847
Emilian Peevaebbe412018-01-15 13:53:24 +0000848 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700849}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800850
Jianing Weicb0652e2014-03-12 18:29:36 -0700851status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
852 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800854
Emilian Peevaebbe412018-01-15 13:53:24 +0000855 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700856 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000857 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700858
Emilian Peevaebbe412018-01-15 13:53:24 +0000859 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700860 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861}
862
Emilian Peevaebbe412018-01-15 13:53:24 +0000863status_t Camera3Device::setStreamingRequestList(
864 const List<const PhysicalCameraSettingsList> &requestsList,
865 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700866 ATRACE_CALL();
867
Emilian Peevaebbe412018-01-15 13:53:24 +0000868 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700869}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800870
871sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000872 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 status_t res;
874
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700875 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800876 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
877 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000878 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700879 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700880 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700882 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700884 } else if (mStatus == STATUS_UNCONFIGURED) {
885 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 CLOGE("No streams configured");
887 return NULL;
888 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 }
890
Shuzhen Wang0129d522016-10-30 22:43:41 -0700891 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800893}
894
Jianing Weicb0652e2014-03-12 18:29:36 -0700895status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700897 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800899
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 switch (mStatus) {
901 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return INVALID_OPERATION;
904 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700905 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700907 case STATUS_UNCONFIGURED:
908 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800909 case STATUS_ACTIVE:
910 // OK
911 break;
912 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700913 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 return INVALID_OPERATION;
915 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800916 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700917
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700918 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800919}
920
921status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
922 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700925 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926}
927
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700928status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800929 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700930 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700932 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700933 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800934 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
935 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700936
937 status_t res;
938 bool wasActive = false;
939
940 switch (mStatus) {
941 case STATUS_ERROR:
942 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
943 return INVALID_OPERATION;
944 case STATUS_UNINITIALIZED:
945 ALOGE("%s: Device not initialized", __FUNCTION__);
946 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 case STATUS_UNCONFIGURED:
948 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700949 // OK
950 break;
951 case STATUS_ACTIVE:
952 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700953 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700954 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700955 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700956 return res;
957 }
958 wasActive = true;
959 break;
960 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700961 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700962 return INVALID_OPERATION;
963 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700964 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700965
966 if (mInputStream != 0) {
967 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
968 return INVALID_OPERATION;
969 }
970
971 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
972 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700973 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700974
975 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800976 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700977
978 *id = mNextStreamId++;
979
980 // Continue captures if active at start
981 if (wasActive) {
982 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100983 // Reuse current operating mode and session parameters for new stream config
984 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700985 if (res != OK) {
986 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
987 __FUNCTION__, mNextStreamId, strerror(-res), res);
988 return res;
989 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700991 }
992
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800993 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700994 return OK;
995}
996
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700997status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700998 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700999 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001000 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001001 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1002 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001003 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001004 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001005 ATRACE_CALL();
1006
1007 if (consumer == nullptr) {
1008 ALOGE("%s: consumer must not be null", __FUNCTION__);
1009 return BAD_VALUE;
1010 }
1011
1012 std::vector<sp<Surface>> consumers;
1013 consumers.push_back(consumer);
1014
1015 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001016 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001017 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001018 streamUseCase, timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001019}
1020
1021static bool isRawFormat(int format) {
1022 switch (format) {
1023 case HAL_PIXEL_FORMAT_RAW16:
1024 case HAL_PIXEL_FORMAT_RAW12:
1025 case HAL_PIXEL_FORMAT_RAW10:
1026 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1027 return true;
1028 default:
1029 return false;
1030 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001031}
1032
1033status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1034 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001035 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001036 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001037 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001038 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001039 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001040 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001041
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001042 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001043 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001045 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001046 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001047 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001048 " mirrorMode %d, colorSpace %d, useReadoutTimestamp %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001049 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001050 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001051 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace,
1052 useReadoutTimestamp);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 status_t res;
1055 bool wasActive = false;
1056
1057 switch (mStatus) {
1058 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001059 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 return INVALID_OPERATION;
1061 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001062 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001064 case STATUS_UNCONFIGURED:
1065 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 // OK
1067 break;
1068 case STATUS_ACTIVE:
1069 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001070 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001072 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 return res;
1074 }
1075 wasActive = true;
1076 break;
1077 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 return INVALID_OPERATION;
1080 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001081 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082
1083 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001084
Shuzhen Wang0129d522016-10-30 22:43:41 -07001085 if (consumers.size() == 0 && !hasDeferredConsumer) {
1086 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1087 return BAD_VALUE;
1088 }
Zhijun He5d677d12016-05-29 16:52:39 -07001089
Shuzhen Wang0129d522016-10-30 22:43:41 -07001090 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001091 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1092 return BAD_VALUE;
1093 }
1094
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001095 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1096 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1097 // be found in only one sensor pixel mode.
1098 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1099 return BAD_VALUE;
1100 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001101 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001102 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001103 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001104 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001105 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001106 if (blobBufferSize <= 0) {
1107 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1108 return BAD_VALUE;
1109 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001110 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1111 blobBufferSize = width * height;
1112 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001113 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001114 if (blobBufferSize <= 0) {
1115 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1116 return BAD_VALUE;
1117 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001118 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001119 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001120 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001121 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001122 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001123 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001124 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001125 bool maxResolution =
1126 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1127 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001128 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1129 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001130 if (rawOpaqueBufferSize <= 0) {
1131 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1132 return BAD_VALUE;
1133 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001134 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001135 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001136 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001137 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001138 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001139 } else if (isShared) {
1140 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1141 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001142 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001143 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001144 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001145 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001146 newStream = new Camera3OutputStream(mNextStreamId,
1147 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001148 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001149 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001150 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001152 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001153 width, height, 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);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 }
Emilian Peev40ead602017-09-26 15:46:36 +01001158
1159 size_t consumerCount = consumers.size();
1160 for (size_t i = 0; i < consumerCount; i++) {
1161 int id = newStream->getSurfaceId(consumers[i]);
1162 if (id < 0) {
1163 SET_ERR_L("Invalid surface id");
1164 return BAD_VALUE;
1165 }
1166 if (surfaceIds != nullptr) {
1167 surfaceIds->push_back(id);
1168 }
1169 }
1170
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001171 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001172
Emilian Peev08dd2452017-04-06 16:55:14 +01001173 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001174
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001175 newStream->setImageDumpMask(mImageDumpMask);
1176
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 res = mOutputStreams.add(mNextStreamId, newStream);
1178 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001179 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001180 return res;
1181 }
1182
Shuzhen Wang316781a2020-08-18 18:11:01 -07001183 mSessionStatsBuilder.addStream(mNextStreamId);
1184
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001186 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187
1188 // Continue captures if active at start
1189 if (wasActive) {
1190 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001191 // Reuse current operating mode and session parameters for new stream config
1192 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001194 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1195 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 return res;
1197 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001198 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001200 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001201 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202}
1203
Emilian Peev710c1422017-08-30 11:19:38 +01001204status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001206 if (nullptr == streamInfo) {
1207 return BAD_VALUE;
1208 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001209 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 switch (mStatus) {
1213 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
1216 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001217 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001219 case STATUS_UNCONFIGURED:
1220 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001221 case STATUS_ACTIVE:
1222 // OK
1223 break;
1224 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001225 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001226 return INVALID_OPERATION;
1227 }
1228
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001229 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1230 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001231 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001232 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233 }
1234
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001235 streamInfo->width = stream->getWidth();
1236 streamInfo->height = stream->getHeight();
1237 streamInfo->format = stream->getFormat();
1238 streamInfo->dataSpace = stream->getDataSpace();
1239 streamInfo->formatOverridden = stream->isFormatOverridden();
1240 streamInfo->originalFormat = stream->getOriginalFormat();
1241 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1242 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001243 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001244 streamInfo->colorSpace = stream->getColorSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246}
1247
1248status_t Camera3Device::setStreamTransform(int id,
1249 int transform) {
1250 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001251 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001253
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 switch (mStatus) {
1255 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001256 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 return INVALID_OPERATION;
1258 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001259 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001260 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001261 case STATUS_UNCONFIGURED:
1262 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 case STATUS_ACTIVE:
1264 // OK
1265 break;
1266 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001267 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001268 return INVALID_OPERATION;
1269 }
1270
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001271 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1272 if (stream == nullptr) {
1273 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001274 return BAD_VALUE;
1275 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001276 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001277}
1278
1279status_t Camera3Device::deleteStream(int id) {
1280 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001281 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001282 Mutex::Autolock l(mLock);
1283 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001285 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001286
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001287 // CameraDevice semantics require device to already be idle before
1288 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001289 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001290 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001291 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 }
1293
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001294 if (mStatus == STATUS_ERROR) {
1295 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1296 __FUNCTION__, mId.string());
1297 return -EBUSY;
1298 }
1299
Igor Murashkin2fba5842013-04-22 14:03:54 -07001300 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001301 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 if (mInputStream != NULL && id == mInputStream->getId()) {
1303 deletedStream = mInputStream;
1304 mInputStream.clear();
1305 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001306 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001307 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001308 return BAD_VALUE;
1309 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001310 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001311 }
1312
1313 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001314 if (stream != nullptr) {
1315 deletedStream = stream;
1316 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317 }
1318
1319 // Free up the stream endpoint so that it can be used by some other stream
1320 res = deletedStream->disconnect();
1321 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001322 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 // fall through since we want to still list the stream as deleted.
1324 }
1325 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001326 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001327
1328 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001329}
1330
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001331status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001332 ATRACE_CALL();
1333 ALOGV("%s: E", __FUNCTION__);
1334
1335 Mutex::Autolock il(mInterfaceLock);
1336 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001337
Emilian Peev811d2952018-05-25 11:08:40 +01001338 // In case the client doesn't include any session parameter, try a
1339 // speculative configuration using the values from the last cached
1340 // default request.
1341 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001342 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001343 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1344 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1345 mLastTemplateId);
1346 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1347 operatingMode);
1348 }
1349
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001350 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1351}
1352
1353status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1354 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001355 //Filter out any incoming session parameters
1356 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001357 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1358 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001359 CameraMetadata filteredParams(availableSessionKeys.count);
1360 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1361 filteredParams.getAndLock());
1362 set_camera_metadata_vendor_id(meta, mVendorTagId);
1363 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001364 if (availableSessionKeys.count > 0) {
1365 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1366 camera_metadata_ro_entry entry = params.find(
1367 availableSessionKeys.data.i32[i]);
1368 if (entry.count > 0) {
1369 filteredParams.update(entry);
1370 }
1371 }
1372 }
1373
1374 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001375}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001376
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001377status_t Camera3Device::getInputBufferProducer(
1378 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001379 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001380 Mutex::Autolock il(mInterfaceLock);
1381 Mutex::Autolock l(mLock);
1382
1383 if (producer == NULL) {
1384 return BAD_VALUE;
1385 } else if (mInputStream == NULL) {
1386 return INVALID_OPERATION;
1387 }
1388
1389 return mInputStream->getInputBufferProducer(producer);
1390}
1391
Emilian Peevf4816702020-04-03 15:44:51 -07001392status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393 CameraMetadata *request) {
1394 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001395 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001396
Emilian Peevf4816702020-04-03 15:44:51 -07001397 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001398 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001399 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001400 return BAD_VALUE;
1401 }
1402
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001405 {
1406 Mutex::Autolock l(mLock);
1407 switch (mStatus) {
1408 case STATUS_ERROR:
1409 CLOGE("Device has encountered a serious error");
1410 return INVALID_OPERATION;
1411 case STATUS_UNINITIALIZED:
1412 CLOGE("Device is not initialized!");
1413 return INVALID_OPERATION;
1414 case STATUS_UNCONFIGURED:
1415 case STATUS_CONFIGURED:
1416 case STATUS_ACTIVE:
1417 // OK
1418 break;
1419 default:
1420 SET_ERR_L("Unexpected status: %d", mStatus);
1421 return INVALID_OPERATION;
1422 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001423
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001424 if (!mRequestTemplateCache[templateId].isEmpty()) {
1425 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001426 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001427 return OK;
1428 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001429 }
1430
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001431 camera_metadata_t *rawRequest;
1432 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001433 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001434
1435 {
1436 Mutex::Autolock l(mLock);
1437 if (res == BAD_VALUE) {
1438 ALOGI("%s: template %d is not supported on this camera device",
1439 __FUNCTION__, templateId);
1440 return res;
1441 } else if (res != OK) {
1442 CLOGE("Unable to construct request template %d: %s (%d)",
1443 templateId, strerror(-res), res);
1444 return res;
1445 }
1446
1447 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1448 mRequestTemplateCache[templateId].acquire(rawRequest);
1449
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001450 // Override the template request with zoomRatioMapper
1451 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1452 &mRequestTemplateCache[templateId]);
1453 if (res != OK) {
1454 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1455 templateId, strerror(-res), res);
1456 return res;
1457 }
1458
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001459 // Fill in JPEG_QUALITY if not available
1460 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1461 static const uint8_t kDefaultJpegQuality = 95;
1462 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1463 &kDefaultJpegQuality, 1);
1464 }
1465
Bharatt Kukrejad33fe9f2022-11-23 21:52:52 +00001466 // Fill in AUTOFRAMING if not available
1467 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1468 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1469 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1470 &kDefaultAutoframingMode, 1);
1471 }
1472
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001473 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001474 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001475 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001476 return OK;
1477}
1478
1479status_t Camera3Device::waitUntilDrained() {
1480 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001481 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001482 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001484
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001485 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001486}
1487
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001488status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001489 switch (mStatus) {
1490 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001491 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001492 ALOGV("%s: Already idle", __FUNCTION__);
1493 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001494 case STATUS_CONFIGURED:
1495 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496 case STATUS_ERROR:
1497 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001498 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001499 break;
1500 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001501 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001502 return INVALID_OPERATION;
1503 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001504 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1505 maxExpectedDuration);
1506 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001507 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001508 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001509 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1510 res);
1511 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001512 return res;
1513}
1514
Ruben Brunk183f0562015-08-12 12:55:02 -07001515void Camera3Device::internalUpdateStatusLocked(Status status) {
1516 mStatus = status;
1517 mRecentStatusUpdates.add(mStatus);
1518 mStatusChanged.broadcast();
1519}
1520
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001521// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001522status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001523 if (mRequestThread.get() != nullptr) {
1524 mRequestThread->setPaused(true);
1525 } else {
1526 return NO_INIT;
1527 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001528
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001529 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1530 maxExpectedDuration);
1531 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001532 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001533 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001534 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001535 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 }
1537
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001538 return res;
1539}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001540
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001541// Resume after internalPauseAndWaitLocked
1542status_t Camera3Device::internalResumeLocked() {
1543 status_t res;
1544
1545 mRequestThread->setPaused(false);
1546
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001547 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1548 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001549 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1550 if (res != OK) {
1551 SET_ERR_L("Can't transition to active in %f seconds!",
1552 kActiveTimeout/1e9);
1553 }
1554 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001555 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001556}
1557
Ruben Brunk183f0562015-08-12 12:55:02 -07001558status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001559 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001560
1561 size_t startIndex = 0;
1562 if (mStatusWaiters == 0) {
1563 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1564 // this status list
1565 mRecentStatusUpdates.clear();
1566 } else {
1567 // If other threads are waiting on updates to this status list, set the position of the
1568 // first element that this list will check rather than clearing the list.
1569 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001570 }
1571
Ruben Brunk183f0562015-08-12 12:55:02 -07001572 mStatusWaiters++;
1573
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001574 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001575 if (!active && mUseHalBufManager) {
1576 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001577 if (mStatus == STATUS_ACTIVE) {
1578 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001579 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001580 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001581 mRequestBufferSM.onWaitUntilIdle();
1582 }
1583
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001584 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001585 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001586 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001587 if (active == (mStatus == STATUS_ACTIVE)) {
1588 // Desired state is current
1589 break;
1590 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001591
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001592 nsecs_t timeElapsed = systemTime() - startTime;
1593 nsecs_t timeToWait = timeout - timeElapsed;
1594 if (timeToWait <= 0) {
1595 // Thread woke up spuriously but has timed out since.
1596 // Force out of loop with TIMED_OUT result.
1597 res = TIMED_OUT;
1598 break;
1599 }
1600 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001601 if (res != OK) break;
1602
Ruben Brunk183f0562015-08-12 12:55:02 -07001603 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1604 // transitions.
1605 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1606 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1607 __FUNCTION__);
1608
1609 // Encountered desired state since we began waiting
1610 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001611 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1612 stateSeen = true;
1613 break;
1614 }
1615 }
1616 } while (!stateSeen);
1617
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001618 if (signalPipelineDrain) {
1619 mRequestThread->resetPipelineDrain();
1620 }
1621
Ruben Brunk183f0562015-08-12 12:55:02 -07001622 mStatusWaiters--;
1623
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001624 return res;
1625}
1626
1627
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001628status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001629 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001630 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001631
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001632 if (listener != NULL && mListener != NULL) {
1633 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1634 }
1635 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001636 mRequestThread->setNotificationListener(listener);
1637 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001638
1639 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001640}
1641
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001642bool Camera3Device::willNotify3A() {
1643 return false;
1644}
1645
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001646status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001647 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001648 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001649
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001650 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001651 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1652 if (st == std::cv_status::timeout) {
1653 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001654 }
1655 }
1656 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001657}
1658
Jianing Weicb0652e2014-03-12 18:29:36 -07001659status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001661 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001662
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001663 if (mResultQueue.empty()) {
1664 return NOT_ENOUGH_DATA;
1665 }
1666
Jianing Weicb0652e2014-03-12 18:29:36 -07001667 if (frame == NULL) {
1668 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1669 return BAD_VALUE;
1670 }
1671
1672 CaptureResult &result = *(mResultQueue.begin());
1673 frame->mResultExtras = result.mResultExtras;
1674 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001675 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001676 mResultQueue.erase(mResultQueue.begin());
1677
1678 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001679}
1680
1681status_t Camera3Device::triggerAutofocus(uint32_t id) {
1682 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001683 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001684
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001685 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1686 // Mix-in this trigger into the next request and only the next request.
1687 RequestTrigger trigger[] = {
1688 {
1689 ANDROID_CONTROL_AF_TRIGGER,
1690 ANDROID_CONTROL_AF_TRIGGER_START
1691 },
1692 {
1693 ANDROID_CONTROL_AF_TRIGGER_ID,
1694 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001695 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001696 };
1697
1698 return mRequestThread->queueTrigger(trigger,
1699 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700}
1701
1702status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1703 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001704 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001705
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001706 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1707 // Mix-in this trigger into the next request and only the next request.
1708 RequestTrigger trigger[] = {
1709 {
1710 ANDROID_CONTROL_AF_TRIGGER,
1711 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1712 },
1713 {
1714 ANDROID_CONTROL_AF_TRIGGER_ID,
1715 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001716 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001717 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001718
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001719 return mRequestThread->queueTrigger(trigger,
1720 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721}
1722
1723status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1724 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001725 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001726
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001727 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1728 // Mix-in this trigger into the next request and only the next request.
1729 RequestTrigger trigger[] = {
1730 {
1731 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1732 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1733 },
1734 {
1735 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1736 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001737 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001738 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001740 return mRequestThread->queueTrigger(trigger,
1741 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001742}
1743
Jianing Weicb0652e2014-03-12 18:29:36 -07001744status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001745 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001746 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001747 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001748
Zhijun He7ef20392014-04-21 16:04:17 -07001749 {
1750 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001751
1752 // b/116514106 "disconnect()" can get called twice for the same device. The
1753 // camera device will not be initialized during the second run.
1754 if (mStatus == STATUS_UNINITIALIZED) {
1755 return OK;
1756 }
1757
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001758 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001759
1760 // Stop session and stream counter
1761 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001762 }
1763
Ravneetdbd5b242022-03-02 07:22:46 +00001764 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001765 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001766 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1767 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1768
1769 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001770}
1771
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001772status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001773 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1774}
1775
1776status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001777 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001778 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001779 Mutex::Autolock il(mInterfaceLock);
1780 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001781
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001782 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1783 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001784 CLOGE("Stream %d does not exist", streamId);
1785 return BAD_VALUE;
1786 }
1787
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001788 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001789 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001790 return BAD_VALUE;
1791 }
1792
1793 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001794 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001795 return BAD_VALUE;
1796 }
1797
Ruben Brunkc78ac262015-08-13 17:58:46 -07001798 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001799}
1800
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001801status_t Camera3Device::tearDown(int streamId) {
1802 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001803 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001804 Mutex::Autolock il(mInterfaceLock);
1805 Mutex::Autolock l(mLock);
1806
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001807 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1808 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001809 CLOGE("Stream %d does not exist", streamId);
1810 return BAD_VALUE;
1811 }
1812
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001813 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1814 CLOGE("Stream %d is a target of a in-progress request", streamId);
1815 return BAD_VALUE;
1816 }
1817
1818 return stream->tearDown();
1819}
1820
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001821status_t Camera3Device::addBufferListenerForStream(int streamId,
1822 wp<Camera3StreamBufferListener> listener) {
1823 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001824 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001825 Mutex::Autolock il(mInterfaceLock);
1826 Mutex::Autolock l(mLock);
1827
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001828 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1829 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001830 CLOGE("Stream %d does not exist", streamId);
1831 return BAD_VALUE;
1832 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001833 stream->addBufferListener(listener);
1834
1835 return OK;
1836}
1837
Austin Borger4a870a32022-02-25 01:48:41 +00001838float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1839 camera_metadata_entry minDurations =
1840 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1841 for (size_t i = 0; i < minDurations.count; i += 4) {
1842 if (minDurations.data.i64[i] == stream->getFormat()
1843 && minDurations.data.i64[i+1] == stream->getWidth()
1844 && minDurations.data.i64[i+2] == stream->getHeight()) {
1845 int64_t minFrameDuration = minDurations.data.i64[i+3];
1846 return 1e9f / minFrameDuration;
1847 }
1848 }
1849 return 0.0f;
1850}
1851
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001852/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001853 * Methods called by subclasses
1854 */
1855
1856void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001857 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001858 std::vector<int> streamIds;
1859 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001860 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001861
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001862 {
1863 // Need mLock to safely update state and synchronize to current
1864 // state of methods in flight.
1865 Mutex::Autolock l(mLock);
1866 // We can get various system-idle notices from the status tracker
1867 // while starting up. Only care about them if we've actually sent
1868 // in some requests recently.
1869 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1870 return;
1871 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001872 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1873 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001874 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001875
1876 // Skip notifying listener if we're doing some user-transparent
1877 // state changes
1878 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001879
Austin Borger4a870a32022-02-25 01:48:41 +00001880 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1881 auto stream = mOutputStreams[i];
1882 if (stream.get() == nullptr) continue;
1883
1884 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1885 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1886
1887 // Populate stream statistics in case of Idle
1888 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001889 streamIds.push_back(stream->getId());
1890 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1891 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001892 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001893 if (camera3Stream != nullptr) {
1894 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001895 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001896 }
1897 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001898 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001899 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001900 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001901 stream->getDynamicRangeProfile(), streamUseCase,
1902 stream->getColorSpace());
Shuzhen Wang316781a2020-08-18 18:11:01 -07001903 }
1904 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001905 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001906
1907 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001908 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001909 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001910 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001911 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001912 status_t res = OK;
1913 if (listener != nullptr) {
1914 if (idle) {
1915 // Get session stats from the builder, and notify the listener.
1916 int64_t requestCount, resultErrorCount;
1917 bool deviceError;
1918 std::map<int, StreamStats> streamStatsMap;
1919 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1920 &deviceError, &streamStatsMap);
1921 for (size_t i = 0; i < streamIds.size(); i++) {
1922 int streamId = streamIds[i];
1923 auto stats = streamStatsMap.find(streamId);
1924 if (stats != streamStatsMap.end()) {
1925 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1926 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1927 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1928 streamStats[i].mHistogramType =
1929 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1930 streamStats[i].mHistogramBins.assign(
1931 stats->second.mCaptureLatencyBins.begin(),
1932 stats->second.mCaptureLatencyBins.end());
1933 streamStats[i].mHistogramCounts.assign(
1934 stats->second.mCaptureLatencyHistogram.begin(),
1935 stats->second.mCaptureLatencyHistogram.end());
1936 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001937 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001938 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1939 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001940 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001941 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001942 }
1943 if (res != OK) {
1944 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1945 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001946 }
1947}
1948
Shuzhen Wang758c2152017-01-10 18:26:18 -08001949status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001950 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001951 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001952 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1953 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001954
1955 if (surfaceIds == nullptr) {
1956 return BAD_VALUE;
1957 }
1958
Zhijun He5d677d12016-05-29 16:52:39 -07001959 Mutex::Autolock il(mInterfaceLock);
1960 Mutex::Autolock l(mLock);
1961
Shuzhen Wang758c2152017-01-10 18:26:18 -08001962 if (consumers.size() == 0) {
1963 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001964 return BAD_VALUE;
1965 }
1966
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001967 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1968 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001969 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001970 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001971 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001972
1973 // isConsumerConfigurationDeferred will be off after setConsumers
1974 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001975 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001976 if (res != OK) {
1977 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1978 return res;
1979 }
1980
Emilian Peev40ead602017-09-26 15:46:36 +01001981 for (auto &consumer : consumers) {
1982 int id = stream->getSurfaceId(consumer);
1983 if (id < 0) {
1984 CLOGE("Invalid surface id!");
1985 return BAD_VALUE;
1986 }
1987 surfaceIds->push_back(id);
1988 }
1989
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001990 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001991 if (!stream->isConfiguring()) {
1992 CLOGE("Stream %d was already fully configured.", streamId);
1993 return INVALID_OPERATION;
1994 }
Zhijun He5d677d12016-05-29 16:52:39 -07001995
Shuzhen Wang0129d522016-10-30 22:43:41 -07001996 res = stream->finishConfiguration();
1997 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001998 // If finishConfiguration fails due to abandoned surface, do not set
1999 // device to error state.
2000 bool isSurfaceAbandoned =
2001 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2002 if (!isSurfaceAbandoned) {
2003 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2004 stream->getId(), strerror(-res), res);
2005 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002006 return res;
2007 }
Zhijun He5d677d12016-05-29 16:52:39 -07002008 }
2009
2010 return OK;
2011}
2012
Emilian Peev40ead602017-09-26 15:46:36 +01002013status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2014 const std::vector<OutputStreamInfo> &outputInfo,
2015 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2016 Mutex::Autolock il(mInterfaceLock);
2017 Mutex::Autolock l(mLock);
2018
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002019 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2020 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002021 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002022 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002023 }
2024
2025 for (const auto &it : removedSurfaceIds) {
2026 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2027 CLOGE("Shared surface still part of a pending request!");
2028 return -EBUSY;
2029 }
2030 }
2031
Emilian Peev40ead602017-09-26 15:46:36 +01002032 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2033 if (res != OK) {
2034 CLOGE("Stream %d failed to update stream (error %d %s) ",
2035 streamId, res, strerror(-res));
2036 if (res == UNKNOWN_ERROR) {
2037 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2038 __FUNCTION__);
2039 }
2040 return res;
2041 }
2042
2043 return res;
2044}
2045
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002046status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2047 Mutex::Autolock il(mInterfaceLock);
2048 Mutex::Autolock l(mLock);
2049
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002050 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2051 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002052 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2053 return BAD_VALUE;
2054 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002055
2056 if (dropping) {
2057 mSessionStatsBuilder.stopCounter(streamId);
2058 } else {
2059 mSessionStatsBuilder.startCounter(streamId);
2060 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002061 return stream->dropBuffers(dropping);
2062}
2063
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002064/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065 * Camera3Device private methods
2066 */
2067
2068sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002069 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002072 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002073 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002074
2075 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002076 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 if (inputStreams.count > 0) {
2078 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002079 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002080 CLOGE("Request references unknown input stream %d",
2081 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082 return NULL;
2083 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002084
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002086 SET_ERR_L("%s: input stream %d is not configured!",
2087 __FUNCTION__, mInputStream->getId());
2088 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002089 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002090 // Check if stream prepare is blocking requests.
2091 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002092 CLOGE("Request references an input stream that's being prepared!");
2093 return NULL;
2094 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002095
2096 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002097 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002098 }
2099
2100 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002101 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002102 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002103 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002104 return NULL;
2105 }
2106
2107 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002108 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2109 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002110 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002111 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 return NULL;
2113 }
Zhijun He5d677d12016-05-29 16:52:39 -07002114 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002115 auto iter = surfaceMap.find(streams.data.i32[i]);
2116 if (iter != surfaceMap.end()) {
2117 const std::vector<size_t>& surfaces = iter->second;
2118 for (const auto& surface : surfaces) {
2119 if (stream->isConsumerConfigurationDeferred(surface)) {
2120 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2121 "due to deferred consumer", stream->getId(), surface);
2122 return NULL;
2123 }
2124 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002125 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002126 }
2127
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002128 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002129 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2130 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002131 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002132 // Check if stream prepare is blocking requests.
2133 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002134 CLOGE("Request references an output stream that's being prepared!");
2135 return NULL;
2136 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002137
2138 newRequest->mOutputStreams.push(stream);
2139 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002140 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002141 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002142
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002143 auto rotateAndCropEntry =
2144 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2145 if (rotateAndCropEntry.count > 0 &&
2146 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2147 newRequest->mRotateAndCropAuto = true;
2148 } else {
2149 newRequest->mRotateAndCropAuto = false;
2150 }
2151
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002152 auto autoframingEntry =
2153 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2154 if (autoframingEntry.count > 0 &&
2155 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2156 newRequest->mAutoframingAuto = true;
2157 } else {
2158 newRequest->mAutoframingAuto = false;
2159 }
2160
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002161 auto zoomRatioEntry =
2162 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2163 if (zoomRatioEntry.count > 0 &&
2164 zoomRatioEntry.data.f[0] == 1.0f) {
2165 newRequest->mZoomRatioIs1x = true;
2166 } else {
2167 newRequest->mZoomRatioIs1x = false;
2168 }
2169
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002170 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002171 for (auto& settings : newRequest->mSettingsList) {
2172 auto testPatternModeEntry =
2173 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2174 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2175 testPatternModeEntry.data.i32[0] :
2176 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002177
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002178 auto testPatternDataEntry =
2179 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2180 if (testPatternDataEntry.count >= 4) {
2181 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2182 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2183 } else {
2184 settings.mOriginalTestPatternData[0] = 0;
2185 settings.mOriginalTestPatternData[1] = 0;
2186 settings.mOriginalTestPatternData[2] = 0;
2187 settings.mOriginalTestPatternData[3] = 0;
2188 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002189 }
2190 }
2191
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002192 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002193}
2194
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002195void Camera3Device::cancelStreamsConfigurationLocked() {
2196 int res = OK;
2197 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2198 res = mInputStream->cancelConfiguration();
2199 if (res != OK) {
2200 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2201 mInputStream->getId(), strerror(-res), res);
2202 }
2203 }
2204
2205 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002206 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002207 if (outputStream->isConfiguring()) {
2208 res = outputStream->cancelConfiguration();
2209 if (res != OK) {
2210 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2211 outputStream->getId(), strerror(-res), res);
2212 }
2213 }
2214 }
2215
2216 // Return state to that at start of call, so that future configures
2217 // properly clean things up
2218 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2219 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002220
2221 res = mPreparerThread->resume();
2222 if (res != OK) {
2223 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2224 }
2225}
2226
Emilian Peev0d0191e2020-04-21 17:01:18 -07002227bool Camera3Device::checkAbandonedStreamsLocked() {
2228 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2229 return true;
2230 }
2231
2232 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2233 auto stream = mOutputStreams[i];
2234 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2235 return true;
2236 }
2237 }
2238
2239 return false;
2240}
2241
Emilian Peev3bead5f2020-05-28 17:29:08 -07002242bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002243 ATRACE_CALL();
2244 bool ret = false;
2245
Shuzhen Wang316781a2020-08-18 18:11:01 -07002246 nsecs_t startTime = systemTime();
2247
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002248 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002249 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2250
2251 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002252 if (checkAbandonedStreamsLocked()) {
2253 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2254 __FUNCTION__);
2255 return true;
2256 }
2257
Emilian Peev3bead5f2020-05-28 17:29:08 -07002258 status_t rc = NO_ERROR;
2259 bool markClientActive = false;
2260 if (mStatus == STATUS_ACTIVE) {
2261 markClientActive = true;
2262 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002263
2264 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2265 }
2266
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002267 if (rc == NO_ERROR) {
2268 mNeedConfig = true;
2269 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2270 if (rc == NO_ERROR) {
2271 ret = true;
2272 mPauseStateNotify = false;
2273 //Moving to active state while holding 'mLock' is important.
2274 //There could be pending calls to 'create-/deleteStream' which
2275 //will trigger another stream configuration while the already
2276 //present streams end up with outstanding buffers that will
2277 //not get drained.
2278 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002279 } else if (rc == DEAD_OBJECT) {
2280 // DEAD_OBJECT can be returned if either the consumer surface is
2281 // abandoned, or the HAL has died.
2282 // - If the HAL has died, configureStreamsLocked call will set
2283 // device to error state,
2284 // - If surface is abandoned, we should not set device to error
2285 // state.
2286 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002287 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002288 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002289 }
2290 } else {
2291 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2292 }
2293
Austin Borger74fca042022-05-23 12:41:21 -07002294 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002295 ns2ms(systemTime() - startTime));
2296
Emilian Peev3bead5f2020-05-28 17:29:08 -07002297 if (markClientActive) {
2298 mStatusTracker->markComponentActive(clientStatusId);
2299 }
2300
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002301 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002302}
2303
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002304status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002305 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002306 ATRACE_CALL();
2307 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002308
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002309 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002310 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002311 return INVALID_OPERATION;
2312 }
2313
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002314 if (operatingMode < 0) {
2315 CLOGE("Invalid operating mode: %d", operatingMode);
2316 return BAD_VALUE;
2317 }
2318
2319 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002320 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002321
2322 if (mOperatingMode != operatingMode) {
2323 mNeedConfig = true;
2324 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2325 mOperatingMode = operatingMode;
2326 }
2327
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002328 // Reset min expected duration when session is reconfigured.
2329 mMinExpectedDuration = 0;
2330
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002331 // In case called from configureStreams, abort queued input buffers not belonging to
2332 // any pending requests.
2333 if (mInputStream != NULL && notifyRequestThread) {
2334 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002335 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002336 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002337 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002338 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002339 if (res != OK) {
2340 // Exhausted acquiring all input buffers.
2341 break;
2342 }
2343
Emilian Peevf4816702020-04-03 15:44:51 -07002344 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002345 res = mInputStream->returnInputBuffer(inputBuffer);
2346 if (res != OK) {
2347 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2348 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2349 }
2350 }
2351 }
2352
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002353 if (!mNeedConfig) {
2354 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2355 return OK;
2356 }
2357
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002358 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002359 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002360 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2361 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002362 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002363 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002364 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002365 }
2366
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002368 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002369
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002370 mPreparerThread->pause();
2371
Emilian Peevf4816702020-04-03 15:44:51 -07002372 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002373 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002374 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002375 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376
Emilian Peevf4816702020-04-03 15:44:51 -07002377 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002378 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002379 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002380
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002381
2382 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002383 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002384 inputStream = mInputStream->startConfiguration();
2385 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002386 CLOGE("Can't start input stream configuration");
2387 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002388 return INVALID_OPERATION;
2389 }
2390 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002391
2392 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002393 }
2394
Shuzhen Wang99080502021-03-07 21:08:20 -08002395 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002396 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002397 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002398
2399 // Don't configure bidi streams twice, nor add them twice to the list
2400 if (mOutputStreams[i].get() ==
2401 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2402
2403 config.num_streams--;
2404 continue;
2405 }
2406
Emilian Peevf4816702020-04-03 15:44:51 -07002407 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002408 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002410 CLOGE("Can't start output stream configuration");
2411 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412 return INVALID_OPERATION;
2413 }
2414 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002415
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002416 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002417 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2418 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002419 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2420 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002421 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2422 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002423 } else if (outputStream->data_space ==
2424 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2425 bufferSizes[k] = outputStream->width * outputStream->height;
2426 } else {
2427 ALOGW("%s: Blob dataSpace %d not supported",
2428 __FUNCTION__, outputStream->data_space);
2429 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002430 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002431
2432 if (mOutputStreams[i]->isMultiResolution()) {
2433 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2434 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2435 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2436 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002437
2438 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2439 composerSurfacePresent = true;
2440 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002441 }
2442
2443 config.streams = streams.editArray();
2444
2445 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002446 // max_buffers, usage, and priv fields, as well as data_space and format
2447 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002448
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002449 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002450 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002451 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002452
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002453 if (res == BAD_VALUE) {
2454 // HAL rejected this set of streams as unsupported, clean up config
2455 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002456 CLOGE("Set of requested inputs/outputs not supported by HAL");
2457 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002458 return BAD_VALUE;
2459 } else if (res != OK) {
2460 // Some other kind of error from configure_streams - this is not
2461 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002462 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2463 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002464 return res;
2465 }
2466
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002467 // Finish all stream configuration immediately.
2468 // TODO: Try to relax this later back to lazy completion, which should be
2469 // faster
2470
Igor Murashkin073f8572013-05-02 14:59:28 -07002471 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002472 bool streamReConfigured = false;
2473 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002474 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002475 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002476 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002477 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002478 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2479 return DEAD_OBJECT;
2480 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002481 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002482 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002483 if (streamReConfigured) {
2484 mInterface->onStreamReConfigured(mInputStream->getId());
2485 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002486 }
2487
2488 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002489 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002490 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002491 bool streamReConfigured = false;
2492 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002493 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002494 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002495 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002496 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002497 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2498 return DEAD_OBJECT;
2499 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002500 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002501 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002502 if (streamReConfigured) {
2503 mInterface->onStreamReConfigured(outputStream->getId());
2504 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002505 }
2506 }
2507
Emilian Peeve23f1d92021-09-20 14:56:01 -07002508 mRequestThread->setComposerSurface(composerSurfacePresent);
2509
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002510 // Request thread needs to know to avoid using repeat-last-settings protocol
2511 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002512 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002513 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2514 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002515 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002516
Zhijun He90f7c372016-08-16 16:19:43 -07002517 char value[PROPERTY_VALUE_MAX];
2518 property_get("camera.fifo.disable", value, "0");
2519 int32_t disableFifo = atoi(value);
2520 if (disableFifo != 1) {
2521 // Boost priority of request thread to SCHED_FIFO.
2522 pid_t requestThreadTid = mRequestThread->getTid();
2523 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002524 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002525 if (res != OK) {
2526 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2527 strerror(-res), res);
2528 } else {
2529 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2530 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002531 }
2532
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002533 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002534 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2535 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2536 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2537 sessionParams.unlock(newSessionParams);
2538 mSessionParams.unlock(currentSessionParams);
2539 if (updateSessionParams) {
2540 mSessionParams = sessionParams;
2541 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002542
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002543 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002544
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002545 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002546 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002547
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002548 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002549
Zhijun He0a210512014-07-24 13:45:15 -07002550 // tear down the deleted streams after configure streams.
2551 mDeletedStreams.clear();
2552
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002553 auto rc = mPreparerThread->resume();
2554 if (rc != OK) {
2555 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2556 return rc;
2557 }
2558
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002559 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002560 mRequestBufferSM.onStreamsConfigured();
2561 }
2562
Cliff Wu3b268182021-07-06 15:44:43 +08002563 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002564 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002565 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002566 // configure the injection streams.
2567 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002568 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002569 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2570 res = mInjectionMethods->injectCamera(config, bufferSizes);
2571 if (res != OK) {
2572 ALOGE("Can't finish inject camera process!");
2573 return res;
2574 }
Cliff Wu3b268182021-07-06 15:44:43 +08002575 } else {
2576 // First run configureStreamsLocked() and then call injectCamera() case:
2577 // If the stream configuration has been completed and camera deive is active, but the
2578 // injection camera has not been injected yet, we need to store the stream configuration of
2579 // the internal camera (because the stream configuration of the injection camera is based
2580 // on the internal camera). When injecting occurs later, this configuration can be used by
2581 // the injection camera.
2582 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2583 " injection camera has not been injected yet.", __FUNCTION__);
2584 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002585 }
2586
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002587 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002588}
2589
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002590status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002591 ATRACE_CALL();
2592 status_t res;
2593
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002594 if (mFakeStreamId != NO_STREAM) {
2595 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002596 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002597 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002598 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002599 return INVALID_OPERATION;
2600 }
2601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002603
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002604 sp<Camera3OutputStreamInterface> fakeStream =
2605 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002607 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002608 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002609 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002610 return res;
2611 }
2612
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002613 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002614 mNextStreamId++;
2615
2616 return OK;
2617}
2618
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002619status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002620 ATRACE_CALL();
2621 status_t res;
2622
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002623 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002624 if (mOutputStreams.size() == 1) return OK;
2625
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002626 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002627
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002628 // Ok, have a fake stream and there's at least one other output stream,
2629 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002630
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002631 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002632 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002633 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002634 return INVALID_OPERATION;
2635 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002636 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002637
2638 // Free up the stream endpoint so that it can be used by some other stream
2639 res = deletedStream->disconnect();
2640 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002641 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002642 // fall through since we want to still list the stream as deleted.
2643 }
2644 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002645 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002646
2647 return res;
2648}
2649
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002650void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002651 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002652 Mutex::Autolock l(mLock);
2653 va_list args;
2654 va_start(args, fmt);
2655
2656 setErrorStateLockedV(fmt, args);
2657
2658 va_end(args);
2659}
2660
2661void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002662 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002663 Mutex::Autolock l(mLock);
2664 setErrorStateLockedV(fmt, args);
2665}
2666
2667void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2668 va_list args;
2669 va_start(args, fmt);
2670
2671 setErrorStateLockedV(fmt, args);
2672
2673 va_end(args);
2674}
2675
2676void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002677 // Print out all error messages to log
2678 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002679 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002680
2681 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002682 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002683
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002684 mErrorCause = errorCause;
2685
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002686 if (mRequestThread != nullptr) {
2687 mRequestThread->setPaused(true);
2688 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002689 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002690
2691 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002692 sp<NotificationListener> listener = mListener.promote();
2693 if (listener != NULL) {
2694 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002695 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002696 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002697 }
2698
2699 // Save stack trace. View by dumping it later.
2700 CameraTraces::saveTrace();
2701 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002702}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002703
2704/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002705 * In-flight request management
2706 */
2707
Jianing Weicb0652e2014-03-12 18:29:36 -07002708status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002709 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002710 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002711 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002712 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002713 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002714 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002715 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002716 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002717
2718 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002719 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002720 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002721 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2722 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002723 if (res < 0) return res;
2724
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002725 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002726 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2727 // avoid a deadlock during reprocess requests.
2728 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002729 if (mStatusTracker != nullptr) {
2730 mStatusTracker->markComponentActive(mInFlightStatusId);
2731 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002732 }
2733
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002734 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002735 return OK;
2736}
2737
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002738void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002739 // Indicate idle inFlightMap to the status tracker
2740 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002741 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002742 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2743 // avoid a deadlock during reprocess requests.
2744 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002745 if (mStatusTracker != nullptr) {
2746 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2747 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002748 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002749 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002750}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002751
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002752void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002753 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002754 // something has likely gone wrong. This might still be legit only if application send in
2755 // a long burst of long exposure requests.
2756 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2757 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2758 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2759 mInFlightMap.size(), mExpectedInflightDuration);
2760 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2761 kInFlightWarnLimitHighSpeed) {
2762 CLOGW("In-flight list too large for high speed configuration: %zu,"
2763 "total inflight duration %" PRIu64,
2764 mInFlightMap.size(), mExpectedInflightDuration);
2765 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002766 }
2767}
2768
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002769void Camera3Device::onInflightMapFlushedLocked() {
2770 mExpectedInflightDuration = 0;
2771}
2772
2773void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002774 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002775 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2776 mInFlightMap.removeItemsAt(idx, 1);
2777
2778 onInflightEntryRemovedLocked(duration);
2779}
2780
2781
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002782void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002783 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002784 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002785 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002786 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002787 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002788 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002789
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002790 FlushInflightReqStates states {
2791 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002792 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002793
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002794 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002795}
2796
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002797CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002798 ALOGV("%s", __FUNCTION__);
2799
Igor Murashkin1e479c02013-09-06 16:55:14 -07002800 CameraMetadata retVal;
2801
2802 if (mRequestThread != NULL) {
2803 retVal = mRequestThread->getLatestRequest();
2804 }
2805
Igor Murashkin1e479c02013-09-06 16:55:14 -07002806 return retVal;
2807}
2808
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002809void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002810 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002811 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002812 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2813 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002814
2815 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002816 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002817}
2818
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002819void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002820 std::vector<native_handle_t*> *handles, bool closeFd) {
2821 if (handles == nullptr) {
2822 return;
2823 }
2824 if (closeFd) {
2825 for (auto& handle : *handles) {
2826 native_handle_close(handle);
2827 }
2828 }
2829 for (auto& handle : *handles) {
2830 native_handle_delete(handle);
2831 }
2832 handles->clear();
2833 return;
2834}
2835
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002836/**
2837 * HalInterface inner class methods
2838 */
2839
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002840void Camera3Device::HalInterface::getInflightBufferKeys(
2841 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002842 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002843 return;
2844}
2845
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002846void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2847 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002848 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002849 return;
2850}
2851
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002852bool Camera3Device::HalInterface::verifyBufferIds(
2853 int32_t streamId, std::vector<uint64_t>& bufIds) {
2854 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002855}
2856
2857status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002858 int32_t frameNumber, int32_t streamId,
2859 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002860 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002861}
2862
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002863status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002864 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002865 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002866}
2867
2868// Find and pop a buffer_handle_t based on bufferId
2869status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002870 uint64_t bufferId,
2871 /*out*/ buffer_handle_t** buffer,
2872 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002873 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002874}
2875
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002876std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2877 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002878 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002879}
2880
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002881uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2882 const native_handle_t* handle) {
2883 return mBufferRecords.removeOneBufferCache(streamId, handle);
2884}
2885
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002886void Camera3Device::HalInterface::onBufferFreed(
2887 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002888 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2889 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2890 if (bufferId != BUFFER_ID_NO_BUFFER) {
2891 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002892 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002893}
2894
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002895void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002896 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2897 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2898 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002899 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2900 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002901}
2902
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002903/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002904 * RequestThread inner class methods
2905 */
2906
2907Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002908 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002909 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002910 bool useHalBufManager,
Austin Borger18b30a72022-10-27 12:20:29 -07002911 bool supportCameraMute,
2912 bool overrideToPortrait) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002913 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002914 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002915 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002916 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002917 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002918 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002919 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002920 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 mReconfigured(false),
2922 mDoPause(false),
2923 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002924 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002925 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002926 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002927 mCurrentAfTriggerId(0),
2928 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002929 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00002930 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002931 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002932 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002933 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002934 mRepeatingLastFrameNumber(
2935 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002936 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002937 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002938 mRequestLatency(kRequestLatencyBinSize),
2939 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002940 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002941 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07002942 mSupportCameraMute(supportCameraMute),
2943 mOverrideToPortrait(overrideToPortrait) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002944 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002945}
2946
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002947Camera3Device::RequestThread::~RequestThread() {}
2948
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002949void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002950 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002951 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002952 Mutex::Autolock l(mRequestLock);
2953 mListener = listener;
2954}
2955
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002956void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002957 const CameraMetadata& sessionParams,
2958 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002959 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002960 Mutex::Autolock l(mRequestLock);
2961 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002962 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002963 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002964 // Prepare video stream for high speed recording.
2965 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002966 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002967}
2968
Jianing Wei90e59c92014-03-12 18:29:36 -07002969status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002970 List<sp<CaptureRequest> > &requests,
2971 /*out*/
2972 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002973 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002974 Mutex::Autolock l(mRequestLock);
2975 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2976 ++it) {
2977 mRequestQueue.push_back(*it);
2978 }
2979
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002980 if (lastFrameNumber != NULL) {
2981 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2982 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2983 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2984 *lastFrameNumber);
2985 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002986
Jianing Wei90e59c92014-03-12 18:29:36 -07002987 unpauseForNewRequests();
2988
2989 return OK;
2990}
2991
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002992
2993status_t Camera3Device::RequestThread::queueTrigger(
2994 RequestTrigger trigger[],
2995 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002996 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002997 Mutex::Autolock l(mTriggerMutex);
2998 status_t ret;
2999
3000 for (size_t i = 0; i < count; ++i) {
3001 ret = queueTriggerLocked(trigger[i]);
3002
3003 if (ret != OK) {
3004 return ret;
3005 }
3006 }
3007
3008 return OK;
3009}
3010
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003011const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3012 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003013 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003014 if (d != nullptr) return d->mId;
3015 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003016}
3017
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003018status_t Camera3Device::RequestThread::queueTriggerLocked(
3019 RequestTrigger trigger) {
3020
3021 uint32_t tag = trigger.metadataTag;
3022 ssize_t index = mTriggerMap.indexOfKey(tag);
3023
3024 switch (trigger.getTagType()) {
3025 case TYPE_BYTE:
3026 // fall-through
3027 case TYPE_INT32:
3028 break;
3029 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003030 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3031 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003032 return INVALID_OPERATION;
3033 }
3034
3035 /**
3036 * Collect only the latest trigger, since we only have 1 field
3037 * in the request settings per trigger tag, and can't send more than 1
3038 * trigger per request.
3039 */
3040 if (index != NAME_NOT_FOUND) {
3041 mTriggerMap.editValueAt(index) = trigger;
3042 } else {
3043 mTriggerMap.add(tag, trigger);
3044 }
3045
3046 return OK;
3047}
3048
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003049status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003050 const RequestList &requests,
3051 /*out*/
3052 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003053 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003054 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003055 if (lastFrameNumber != NULL) {
3056 *lastFrameNumber = mRepeatingLastFrameNumber;
3057 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003059 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003060 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3061 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003062
3063 unpauseForNewRequests();
3064
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003065 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003066 return OK;
3067}
3068
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003069bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003070 if (mRepeatingRequests.empty()) {
3071 return false;
3072 }
3073 int32_t requestId = requestIn->mResultExtras.requestId;
3074 const RequestList &repeatRequests = mRepeatingRequests;
3075 // All repeating requests are guaranteed to have same id so only check first quest
3076 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3077 return (firstRequest->mResultExtras.requestId == requestId);
3078}
3079
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003080status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003081 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003082 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003083 return clearRepeatingRequestsLocked(lastFrameNumber);
3084
3085}
3086
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003087status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3088 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003089 std::vector<int32_t> streamIds;
3090 for (const auto& request : mRepeatingRequests) {
3091 for (const auto& stream : request->mOutputStreams) {
3092 streamIds.push_back(stream->getId());
3093 }
3094 }
3095
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003096 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003097 if (lastFrameNumber != NULL) {
3098 *lastFrameNumber = mRepeatingLastFrameNumber;
3099 }
Emilian Peev2295df72021-11-12 18:14:10 -08003100
3101 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3102
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003103 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003104 return OK;
3105}
3106
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003107status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003108 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003109 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003110 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003111 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003112
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003113 // Send errors for all requests pending in the request queue, including
3114 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003115 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003116 if (listener != NULL) {
3117 for (RequestList::iterator it = mRequestQueue.begin();
3118 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003119 // Abort the input buffers for reprocess requests.
3120 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003121 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003122 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003123 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003124 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003125 if (res != OK) {
3126 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3127 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3128 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003129 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003130 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3131 if (res != OK) {
3132 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3133 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3134 }
3135 }
3136 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003137 // Set the frame number this request would have had, if it
3138 // had been submitted; this frame number will not be reused.
3139 // The requestId and burstId fields were set when the request was
3140 // submitted originally (in convertMetadataListToRequestListLocked)
3141 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003142 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003143 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003144 }
3145 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003146 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003147
3148 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003149 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003150 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003151 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003152 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003153 return OK;
3154}
3155
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003156status_t Camera3Device::RequestThread::flush() {
3157 ATRACE_CALL();
3158 Mutex::Autolock l(mFlushLock);
3159
Emilian Peev08dd2452017-04-06 16:55:14 +01003160 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003161}
3162
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003163void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003164 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003165 Mutex::Autolock l(mPauseLock);
3166 mDoPause = paused;
3167 mDoPauseSignal.signal();
3168}
3169
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003170status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3171 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003172 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003173 Mutex::Autolock l(mLatestRequestMutex);
3174 status_t res;
3175 while (mLatestRequestId != requestId) {
3176 nsecs_t startTime = systemTime();
3177
3178 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3179 if (res != OK) return res;
3180
3181 timeout -= (systemTime() - startTime);
3182 }
3183
3184 return OK;
3185}
3186
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003187void Camera3Device::RequestThread::requestExit() {
3188 // Call parent to set up shutdown
3189 Thread::requestExit();
3190 // The exit from any possible waits
3191 mDoPauseSignal.signal();
3192 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003193
3194 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3195 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003196}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003197
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003198void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003199 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003200 bool surfaceAbandoned = false;
3201 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003202 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003203 {
3204 Mutex::Autolock l(mRequestLock);
3205 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3206 // repeating requests.
3207 for (const auto& request : mRepeatingRequests) {
3208 for (const auto& s : request->mOutputStreams) {
3209 if (s->isAbandoned()) {
3210 surfaceAbandoned = true;
3211 clearRepeatingRequestsLocked(&lastFrameNumber);
3212 break;
3213 }
3214 }
3215 if (surfaceAbandoned) {
3216 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003217 }
3218 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003219 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003220 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003221
3222 if (listener != NULL && surfaceAbandoned) {
3223 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003224 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003225}
3226
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003227bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003228 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003229 status_t res;
3230 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003231 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003232 uint32_t numRequestProcessed = 0;
3233 for (size_t i = 0; i < batchSize; i++) {
3234 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003235 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003236 }
3237
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003238 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3239
3240 bool triggerRemoveFailed = false;
3241 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3242 for (size_t i = 0; i < numRequestProcessed; i++) {
3243 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3244 nextRequest.submitted = true;
3245
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003246 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003247
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003248 if (!triggerRemoveFailed) {
3249 // Remove any previously queued triggers (after unlock)
3250 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3251 if (removeTriggerRes != OK) {
3252 triggerRemoveFailed = true;
3253 triggerFailedRequest = nextRequest;
3254 }
3255 }
3256 }
3257
3258 if (triggerRemoveFailed) {
3259 SET_ERR("RequestThread: Unable to remove triggers "
3260 "(capture request %d, HAL device: %s (%d)",
3261 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3262 cleanUpFailedRequests(/*sendRequestError*/ false);
3263 return false;
3264 }
3265
3266 if (res != OK) {
3267 // Should only get a failure here for malformed requests or device-level
3268 // errors, so consider all errors fatal. Bad metadata failures should
3269 // come through notify.
3270 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3271 mNextRequests[numRequestProcessed].halRequest.frame_number,
3272 strerror(-res), res);
3273 cleanUpFailedRequests(/*sendRequestError*/ false);
3274 return false;
3275 }
3276 return true;
3277}
3278
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003279Camera3Device::RequestThread::ExpectedDurationInfo
3280 Camera3Device::RequestThread::calculateExpectedDurationRange(
3281 const camera_metadata_t *request) {
3282 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003283 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003284 InFlightRequest::kDefaultMaxExpectedDuration,
3285 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003286 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3287 find_camera_metadata_ro_entry(request,
3288 ANDROID_CONTROL_AE_MODE,
3289 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003290 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003291
3292 switch (e.data.u8[0]) {
3293 case ANDROID_CONTROL_AE_MODE_OFF:
3294 find_camera_metadata_ro_entry(request,
3295 ANDROID_SENSOR_EXPOSURE_TIME,
3296 &e);
3297 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003298 expectedDurationInfo.minDuration = e.data.i64[0];
3299 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003300 }
3301 find_camera_metadata_ro_entry(request,
3302 ANDROID_SENSOR_FRAME_DURATION,
3303 &e);
3304 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003305 expectedDurationInfo.minDuration =
3306 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3307 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003308 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003309 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003310 break;
3311 default:
3312 find_camera_metadata_ro_entry(request,
3313 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3314 &e);
3315 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003316 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3317 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003318 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003319 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003320 break;
3321 }
3322
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003323 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003324}
3325
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003326bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3327 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3328 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3329 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3330 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3331 return true;
3332 }
3333
3334 return false;
3335}
3336
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003337void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3338 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003339 camera_capture_request_t& halRequest = nextRequest.halRequest;
3340 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003341 Mutex::Autolock al(mLatestRequestMutex);
3342
Shuzhen Wang83bff122020-11-20 15:51:39 -08003343 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003344 mLatestRequest.acquire(cloned);
3345
3346 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003347 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3348 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3349 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003350 CameraMetadata(cloned));
3351 }
3352
3353 sp<Camera3Device> parent = mParent.promote();
3354 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003355 int32_t inputStreamId = -1;
3356 if (halRequest.input_buffer != nullptr) {
3357 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3358 }
3359
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003360 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003361 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003362 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3363 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003364 }
3365 }
3366
Shuzhen Wang83bff122020-11-20 15:51:39 -08003367 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003368 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003369 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003370 }
3371
Shuzhen Wang83bff122020-11-20 15:51:39 -08003372 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003373}
3374
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003375bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3376 ATRACE_CALL();
3377 bool updatesDetected = false;
3378
Emilian Peev4ec17882019-01-24 17:16:58 -08003379 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003380 for (auto tag : mSessionParamKeys) {
3381 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003382 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003383
3384 if (entry.count > 0) {
3385 bool isDifferent = false;
3386 if (lastEntry.count > 0) {
3387 // Have a last value, compare to see if changed
3388 if (lastEntry.type == entry.type &&
3389 lastEntry.count == entry.count) {
3390 // Same type and count, compare values
3391 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3392 size_t entryBytes = bytesPerValue * lastEntry.count;
3393 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3394 if (cmp != 0) {
3395 isDifferent = true;
3396 }
3397 } else {
3398 // Count or type has changed
3399 isDifferent = true;
3400 }
3401 } else {
3402 // No last entry, so always consider to be different
3403 isDifferent = true;
3404 }
3405
3406 if (isDifferent) {
3407 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003408 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3409 updatesDetected = true;
3410 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003411 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003412 }
3413 } else if (lastEntry.count > 0) {
3414 // Value has been removed
3415 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003416 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003417 updatesDetected = true;
3418 }
3419 }
3420
Emilian Peev4ec17882019-01-24 17:16:58 -08003421 bool reconfigureRequired;
3422 if (updatesDetected) {
3423 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3424 updatedParams);
3425 mLatestSessionParams = updatedParams;
3426 } else {
3427 reconfigureRequired = false;
3428 }
3429
3430 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003431}
3432
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003433bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003434 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003435 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003436 // Any function called from threadLoop() must not hold mInterfaceLock since
3437 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3438 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003439
3440 // Handle paused state.
3441 if (waitIfPaused()) {
3442 return true;
3443 }
3444
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003445 // Wait for the next batch of requests.
3446 waitForNextRequestBatch();
3447 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003448 return true;
3449 }
3450
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003451 // Get the latest request ID, if any
3452 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003453 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003454 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003455 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003456 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003457 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003458 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3459 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003460 }
3461
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003462 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3463 // or a single request from streaming or burst. In either case the first element
3464 // should contain the latest camera settings that we need to check for any session
3465 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003466 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003467 res = OK;
3468
3469 //Input stream buffers are already acquired at this point so an input stream
3470 //will not be able to move to idle state unless we force it.
3471 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3472 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3473 if (res != OK) {
3474 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3475 cleanUpFailedRequests(/*sendRequestError*/ false);
3476 return false;
3477 }
3478 }
3479
3480 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003481 sp<Camera3Device> parent = mParent.promote();
3482 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003483 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3484 if (statusTracker != nullptr) {
3485 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3486 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003487 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003488 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003489 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003490
3491 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3492 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3493 if (res != OK) {
3494 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3495 cleanUpFailedRequests(/*sendRequestError*/ false);
3496 return false;
3497 }
3498 }
3499 }
3500 }
3501
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003502 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003503 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003504 if (res == TIMED_OUT) {
3505 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003506 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003507 // Check if any stream is abandoned.
3508 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003509 return true;
3510 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003511 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003512 return false;
3513 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003514
Zhijun Hecc27e112013-10-03 16:12:43 -07003515 // Inform waitUntilRequestProcessed thread of a new request ID
3516 {
3517 Mutex::Autolock al(mLatestRequestMutex);
3518
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003519 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003520 mLatestRequestSignal.signal();
3521 }
3522
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003523 // Submit a batch of requests to HAL.
3524 // Use flush lock only when submitting multilple requests in a batch.
3525 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3526 // which may take a long time to finish so synchronizing flush() and
3527 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3528 // For now, only synchronize for high speed recording and we should figure something out for
3529 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003530 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003531
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003532 if (useFlushLock) {
3533 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003534 }
3535
Zhijun Hef0645c12016-08-02 00:58:11 -07003536 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003537 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003538
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003539 sp<Camera3Device> parent = mParent.promote();
3540 if (parent != nullptr) {
3541 parent->mRequestBufferSM.onSubmittingRequest();
3542 }
3543
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003544 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003545 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003546 submitRequestSuccess = sendRequestsBatch();
3547
Shuzhen Wang686f6442017-06-20 16:16:04 -07003548 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3549 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003550
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003551 if (useFlushLock) {
3552 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003553 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003554
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003555 // Unset as current request
3556 {
3557 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003558 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003559 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003560 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003561
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003562 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003563}
3564
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003565status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3566 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3567 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3568 ANDROID_SCALER_CROP_REGION_SET};
3569 if (request == nullptr) {
3570 ALOGE("%s request metadata nullptr", __FUNCTION__);
3571 return BAD_VALUE;
3572 }
3573 status_t res = OK;
3574 for (const auto &key : kFwkOnlyRegionKeys) {
3575 if (request->exists(key)) {
3576 res = request->erase(key);
3577 if (res != OK) {
3578 return res;
3579 }
3580 }
3581 }
3582 return OK;
3583}
3584
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003585status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003586 ATRACE_CALL();
3587
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003588 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003589 for (size_t i = 0; i < mNextRequests.size(); i++) {
3590 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003591 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003592 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3593 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003594
3595 // Prepare a request to HAL
3596 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3597
3598 // Insert any queued triggers (before metadata is locked)
3599 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003600 if (res < 0) {
3601 SET_ERR("RequestThread: Unable to insert triggers "
3602 "(capture request %d, HAL device: %s (%d)",
3603 halRequest->frame_number, strerror(-res), res);
3604 return INVALID_OPERATION;
3605 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003606
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003607 int triggerCount = res;
3608 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3609 mPrevTriggers = triggerCount;
3610
Emilian Peeve23f1d92021-09-20 14:56:01 -07003611 // Do not override rotate&crop for stream configurations that include
Austin Borger18b30a72022-10-27 12:20:29 -07003612 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3613 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3614 bool rotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
Emilian Peeve23f1d92021-09-20 14:56:01 -07003615 overrideAutoRotateAndCrop(captureRequest);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003616 bool autoframingChanged = overrideAutoframing(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003617 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003618
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003619 // If the request is the same as last, or we had triggers now or last time or
3620 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003621 bool newRequest =
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003622 (mPrevRequest != captureRequest || triggersMixedIn || rotateAndCropChanged ||
3623 autoframingChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003624 // Request settings are all the same within one batch, so only treat the first
3625 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003626 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003627 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003628 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003629 /**
3630 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003631 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003632 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003633 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003634 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003635 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003636 "(capture request %d, HAL device: %s (%d)",
3637 halRequest->frame_number, strerror(-res), res);
3638 return INVALID_OPERATION;
3639 }
3640
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003641 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003642 sp<Camera3Device> parent = mParent.promote();
3643 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003644 List<PhysicalCameraSettings>::iterator it;
3645 for (it = captureRequest->mSettingsList.begin();
3646 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003647 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3648 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003649 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3650 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3651 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3652 res);
3653 return INVALID_OPERATION;
3654 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003655 continue;
3656 }
3657
3658 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3659 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3660 updateCaptureRequest(&(it->metadata));
3661 if (res != OK) {
3662 SET_ERR("RequestThread: Unable to correct capture requests "
3663 "for scaler crop region and metering regions for request "
3664 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3665 res);
3666 return INVALID_OPERATION;
3667 }
3668 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003669 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3670 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3671 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3672 res);
3673 return INVALID_OPERATION;
3674 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003675 }
3676 }
3677
3678 // Correct metadata regions for distortion correction if enabled
3679 for (it = captureRequest->mSettingsList.begin();
3680 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003681 if (parent->mDistortionMappers.find(it->cameraId) ==
3682 parent->mDistortionMappers.end()) {
3683 continue;
3684 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003685
3686 if (!captureRequest->mDistortionCorrectionUpdated) {
3687 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3688 &(it->metadata));
3689 if (res != OK) {
3690 SET_ERR("RequestThread: Unable to correct capture requests "
3691 "for lens distortion for request %d: %s (%d)",
3692 halRequest->frame_number, strerror(-res), res);
3693 return INVALID_OPERATION;
3694 }
3695 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003696 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003697 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003698
3699 for (it = captureRequest->mSettingsList.begin();
3700 it != captureRequest->mSettingsList.end(); it++) {
3701 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3702 parent->mZoomRatioMappers.end()) {
3703 continue;
3704 }
3705
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003706 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003707 cameraIdsWithZoom.insert(it->cameraId);
3708 }
3709
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003710 if (!captureRequest->mZoomRatioUpdated) {
3711 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3712 &(it->metadata));
3713 if (res != OK) {
3714 SET_ERR("RequestThread: Unable to correct capture requests "
3715 "for zoom ratio for request %d: %s (%d)",
3716 halRequest->frame_number, strerror(-res), res);
3717 return INVALID_OPERATION;
3718 }
3719 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003720 }
3721 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003722 if (captureRequest->mRotateAndCropAuto &&
3723 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003724 for (it = captureRequest->mSettingsList.begin();
3725 it != captureRequest->mSettingsList.end(); it++) {
3726 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3727 if (mapper != parent->mRotateAndCropMappers.end()) {
3728 res = mapper->second.updateCaptureRequest(&(it->metadata));
3729 if (res != OK) {
3730 SET_ERR("RequestThread: Unable to correct capture requests "
3731 "for rotate-and-crop for request %d: %s (%d)",
3732 halRequest->frame_number, strerror(-res), res);
3733 return INVALID_OPERATION;
3734 }
3735 }
3736 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003737 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003738 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003739 }
3740 }
3741
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003742 /**
3743 * The request should be presorted so accesses in HAL
3744 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3745 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003746 captureRequest->mSettingsList.begin()->metadata.sort();
3747 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003748 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003749 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003750 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3751
3752 IF_ALOGV() {
3753 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3754 find_camera_metadata_ro_entry(
3755 halRequest->settings,
3756 ANDROID_CONTROL_AF_TRIGGER,
3757 &e
3758 );
3759 if (e.count > 0) {
3760 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3761 __FUNCTION__,
3762 halRequest->frame_number,
3763 e.data.u8[0]);
3764 }
3765 }
3766 } else {
3767 // leave request.settings NULL to indicate 'reuse latest given'
3768 ALOGVV("%s: Request settings are REUSED",
3769 __FUNCTION__);
3770 }
3771
Emilian Peevaebbe412018-01-15 13:53:24 +00003772 if (captureRequest->mSettingsList.size() > 1) {
3773 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3774 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003775 if (newRequest) {
3776 halRequest->physcam_settings =
3777 new const camera_metadata* [halRequest->num_physcam_settings];
3778 } else {
3779 halRequest->physcam_settings = nullptr;
3780 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003781 auto it = ++captureRequest->mSettingsList.begin();
3782 size_t i = 0;
3783 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3784 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003785 if (newRequest) {
3786 it->metadata.sort();
3787 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3788 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003789 }
3790 }
3791
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003792 uint32_t totalNumBuffers = 0;
3793
3794 // Fill in buffers
3795 if (captureRequest->mInputStream != NULL) {
3796 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003797
3798 halRequest->input_width = captureRequest->mInputBufferSize.width;
3799 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003800 totalNumBuffers += 1;
3801 } else {
3802 halRequest->input_buffer = NULL;
3803 }
3804
Emilian Peevf4816702020-04-03 15:44:51 -07003805 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003806 captureRequest->mOutputStreams.size());
3807 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003808 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003809
3810 sp<Camera3Device> parent = mParent.promote();
3811 if (parent == NULL) {
3812 // Should not happen, and nowhere to send errors to, so just log it
3813 CLOGE("RequestThread: Parent is gone");
3814 return INVALID_OPERATION;
3815 }
3816 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3817
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003818 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003819 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003820 sp<Camera3OutputStreamInterface> outputStream =
3821 captureRequest->mOutputStreams.editItemAt(j);
3822 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003823
3824 // Prepare video buffers for high speed recording on the first video request.
3825 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3826 // Only try to prepare video stream on the first video request.
3827 mPrepareVideoStream = false;
3828
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003829 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3830 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003831 while (res == NOT_ENOUGH_DATA) {
3832 res = outputStream->prepareNextBuffer();
3833 }
3834 if (res != OK) {
3835 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3836 __FUNCTION__, strerror(-res), res);
3837 outputStream->cancelPrepare();
3838 }
3839 }
3840
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003841 std::vector<size_t> uniqueSurfaceIds;
3842 res = outputStream->getUniqueSurfaceIds(
3843 captureRequest->mOutputSurfaces[streamId],
3844 &uniqueSurfaceIds);
3845 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3846 if (res != OK && res != INVALID_OPERATION) {
3847 ALOGE("%s: failed to query stream %d unique surface IDs",
3848 __FUNCTION__, streamId);
3849 return res;
3850 }
3851 if (res == OK) {
3852 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3853 }
3854
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003855 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003856 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003857 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003858 return TIMED_OUT;
3859 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003860 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003861 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003862 buffer.stream = outputStream->asHalStream();
3863 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003864 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003865 buffer.acquire_fence = -1;
3866 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003867 // Mark the output stream as unpreparable to block clients from calling
3868 // 'prepare' after this request reaches CameraHal and before the respective
3869 // buffers are requested.
3870 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003871 } else {
3872 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3873 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003874 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003875 if (res != OK) {
3876 // Can't get output buffer from gralloc queue - this could be due to
3877 // abandoned queue or other consumer misbehavior, so not a fatal
3878 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003879 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003880 " %s (%d)", strerror(-res), res);
3881
3882 return TIMED_OUT;
3883 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003884 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003885
3886 {
3887 sp<Camera3Device> parent = mParent.promote();
3888 if (parent != nullptr) {
3889 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3890 for (const auto& settings : captureRequest->mSettingsList) {
3891 if ((streamCameraId.isEmpty() &&
3892 parent->getId() == settings.cameraId.c_str()) ||
3893 streamCameraId == settings.cameraId.c_str()) {
3894 outputStream->fireBufferRequestForFrameNumber(
3895 captureRequest->mResultExtras.frameNumber,
3896 settings.metadata);
3897 }
3898 }
3899 }
3900 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003901
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003902 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003903 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3904 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3905 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3906 } else if (!physicalCameraId.isEmpty()) {
3907 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003908 }
3909 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003910 }
3911 totalNumBuffers += halRequest->num_output_buffers;
3912
3913 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003914 // If this request list is for constrained high speed recording (not
3915 // preview), and the current request is not the last one in the batch,
3916 // do not send callback to the app.
3917 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003918 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003919 hasCallback = false;
3920 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003921 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003922 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003923 const camera_metadata_t* settings = halRequest->settings;
3924 bool shouldUnlockSettings = false;
3925 if (settings == nullptr) {
3926 shouldUnlockSettings = true;
3927 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3928 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003929 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3930 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003931 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003932 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3933 isStillCapture = true;
3934 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3935 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003936
Emilian Peevaf8416e2021-02-04 17:52:43 -08003937 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003938 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003939 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3940 isZslCapture = true;
3941 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003942 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003943 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003944 res = parent->registerInFlight(halRequest->frame_number,
3945 totalNumBuffers, captureRequest->mResultExtras,
3946 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003947 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003948 expectedDurationInfo.minDuration,
3949 expectedDurationInfo.maxDuration,
3950 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003951 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003952 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
3953 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003954 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003955 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003956 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3957 ", burstId = %" PRId32 ".",
3958 __FUNCTION__,
3959 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3960 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003961
3962 if (shouldUnlockSettings) {
3963 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3964 }
3965
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003966 if (res != OK) {
3967 SET_ERR("RequestThread: Unable to register new in-flight request:"
3968 " %s (%d)", strerror(-res), res);
3969 return INVALID_OPERATION;
3970 }
3971 }
3972
3973 return OK;
3974}
3975
Igor Murashkin1e479c02013-09-06 16:55:14 -07003976CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003977 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003978 Mutex::Autolock al(mLatestRequestMutex);
3979
3980 ALOGV("RequestThread::%s", __FUNCTION__);
3981
3982 return mLatestRequest;
3983}
3984
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003985bool Camera3Device::RequestThread::isStreamPending(
3986 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003987 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003988 Mutex::Autolock l(mRequestLock);
3989
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003990 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003991 if (!nextRequest.submitted) {
3992 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3993 if (stream == s) return true;
3994 }
3995 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003996 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003997 }
3998
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003999 for (const auto& request : mRequestQueue) {
4000 for (const auto& s : request->mOutputStreams) {
4001 if (stream == s) return true;
4002 }
4003 if (stream == request->mInputStream) return true;
4004 }
4005
4006 for (const auto& request : mRepeatingRequests) {
4007 for (const auto& s : request->mOutputStreams) {
4008 if (stream == s) return true;
4009 }
4010 if (stream == request->mInputStream) return true;
4011 }
4012
4013 return false;
4014}
Jianing Weicb0652e2014-03-12 18:29:36 -07004015
Emilian Peev40ead602017-09-26 15:46:36 +01004016bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4017 ATRACE_CALL();
4018 Mutex::Autolock l(mRequestLock);
4019
4020 for (const auto& nextRequest : mNextRequests) {
4021 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4022 if (s.first == streamId) {
4023 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4024 if (it != s.second.end()) {
4025 return true;
4026 }
4027 }
4028 }
4029 }
4030
4031 for (const auto& request : mRequestQueue) {
4032 for (const auto& s : request->mOutputSurfaces) {
4033 if (s.first == streamId) {
4034 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4035 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004036 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004037 }
4038 }
4039 }
4040 }
4041
4042 for (const auto& request : mRepeatingRequests) {
4043 for (const auto& s : request->mOutputSurfaces) {
4044 if (s.first == streamId) {
4045 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4046 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004047 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004048 }
4049 }
4050 }
4051 }
4052
4053 return false;
4054}
4055
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004056void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4057 if (!mUseHalBufManager) {
4058 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4059 return;
4060 }
4061
4062 Mutex::Autolock pl(mPauseLock);
4063 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004064 mInterface->signalPipelineDrain(streamIds);
4065 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004066 }
4067 // If request thread is still busy, wait until paused then notify HAL
4068 mNotifyPipelineDrain = true;
4069 mStreamIdsToBeDrained = streamIds;
4070}
4071
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004072void Camera3Device::RequestThread::resetPipelineDrain() {
4073 Mutex::Autolock pl(mPauseLock);
4074 mNotifyPipelineDrain = false;
4075 mStreamIdsToBeDrained.clear();
4076}
4077
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004078void Camera3Device::RequestThread::clearPreviousRequest() {
4079 Mutex::Autolock l(mRequestLock);
4080 mPrevRequest.clear();
4081}
4082
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004083status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4084 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4085 ATRACE_CALL();
4086 Mutex::Autolock l(mTriggerMutex);
4087 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4088 return BAD_VALUE;
4089 }
4090 mRotateAndCropOverride = rotateAndCropValue;
4091 return OK;
4092}
4093
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004094status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4095 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4096 ATRACE_CALL();
4097 Mutex::Autolock l(mTriggerMutex);
4098 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
4099 return BAD_VALUE;
4100 }
4101 mAutoframingOverride = autoframingValue;
4102 return OK;
4103}
4104
Emilian Peeve23f1d92021-09-20 14:56:01 -07004105status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4106 ATRACE_CALL();
4107 Mutex::Autolock l(mTriggerMutex);
4108 mComposerOutput = composerSurfacePresent;
4109 return OK;
4110}
4111
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004112status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004113 ATRACE_CALL();
4114 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004115 if (muteMode != mCameraMute) {
4116 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004117 mCameraMuteChanged = true;
4118 }
4119 return OK;
4120}
4121
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004122nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004123 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004124 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004125 return mExpectedInflightDuration > kMinInflightDuration ?
4126 mExpectedInflightDuration : kMinInflightDuration;
4127}
4128
Emilian Peevaebbe412018-01-15 13:53:24 +00004129void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004130 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004131 if ((request == nullptr) || (halRequest == nullptr)) {
4132 ALOGE("%s: Invalid request!", __FUNCTION__);
4133 return;
4134 }
4135
4136 if (halRequest->num_physcam_settings > 0) {
4137 if (halRequest->physcam_id != nullptr) {
4138 delete [] halRequest->physcam_id;
4139 halRequest->physcam_id = nullptr;
4140 }
4141 if (halRequest->physcam_settings != nullptr) {
4142 auto it = ++(request->mSettingsList.begin());
4143 size_t i = 0;
4144 for (; it != request->mSettingsList.end(); it++, i++) {
4145 it->metadata.unlock(halRequest->physcam_settings[i]);
4146 }
4147 delete [] halRequest->physcam_settings;
4148 halRequest->physcam_settings = nullptr;
4149 }
4150 }
4151}
4152
Ravneetaeb20dc2022-03-30 05:33:03 +00004153status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4154 Mutex::Autolock il(mInterfaceLock);
4155 Mutex::Autolock l(mLock);
4156
4157 if (mCameraServiceWatchdog != NULL) {
4158 mCameraServiceWatchdog->setEnabled(enabled);
4159 }
4160
4161 return OK;
4162}
4163
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004164void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4165 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004166 return;
4167 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004168
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004169 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004170 // Skip the ones that have been submitted successfully.
4171 if (nextRequest.submitted) {
4172 continue;
4173 }
4174
4175 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004176 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4177 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004178
4179 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004180 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004181 }
4182
Emilian Peevaebbe412018-01-15 13:53:24 +00004183 cleanupPhysicalSettings(captureRequest, halRequest);
4184
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004185 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004186 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004187 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4188 }
4189
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004190 // No output buffer can be returned when using HAL buffer manager
4191 if (!mUseHalBufManager) {
4192 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4193 //Buffers that failed processing could still have
4194 //valid acquire fence.
4195 int acquireFence = (*outputBuffers)[i].acquire_fence;
4196 if (0 <= acquireFence) {
4197 close(acquireFence);
4198 outputBuffers->editItemAt(i).acquire_fence = -1;
4199 }
Emilian Peevf4816702020-04-03 15:44:51 -07004200 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004201 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4202 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004203 /*timestampIncreasing*/true, std::vector<size_t> (),
4204 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004205 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004206 }
4207
4208 if (sendRequestError) {
4209 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004210 sp<NotificationListener> listener = mListener.promote();
4211 if (listener != NULL) {
4212 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004213 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004214 captureRequest->mResultExtras);
4215 }
4216 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004217
4218 // Remove yet-to-be submitted inflight request from inflightMap
4219 {
4220 sp<Camera3Device> parent = mParent.promote();
4221 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004222 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004223 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4224 if (idx >= 0) {
4225 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4226 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4227 parent->removeInFlightMapEntryLocked(idx);
4228 }
4229 }
4230 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004231 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004232
4233 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004234 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004235}
4236
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004237void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004238 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004239 // Optimized a bit for the simple steady-state case (single repeating
4240 // request), to avoid putting that request in the queue temporarily.
4241 Mutex::Autolock l(mRequestLock);
4242
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004243 assert(mNextRequests.empty());
4244
4245 NextRequest nextRequest;
4246 nextRequest.captureRequest = waitForNextRequestLocked();
4247 if (nextRequest.captureRequest == nullptr) {
4248 return;
4249 }
4250
Emilian Peevf4816702020-04-03 15:44:51 -07004251 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004252 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004253 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004254
4255 // Wait for additional requests
4256 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4257
4258 for (size_t i = 1; i < batchSize; i++) {
4259 NextRequest additionalRequest;
4260 additionalRequest.captureRequest = waitForNextRequestLocked();
4261 if (additionalRequest.captureRequest == nullptr) {
4262 break;
4263 }
4264
Emilian Peevf4816702020-04-03 15:44:51 -07004265 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004266 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004267 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004268 }
4269
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004270 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004271 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004272 mNextRequests.size(), batchSize);
4273 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004274 }
4275
4276 return;
4277}
4278
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004279void Camera3Device::RequestThread::setRequestClearing() {
4280 Mutex::Autolock l(mRequestLock);
4281 mRequestClearing = true;
4282}
4283
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004284sp<Camera3Device::CaptureRequest>
4285 Camera3Device::RequestThread::waitForNextRequestLocked() {
4286 status_t res;
4287 sp<CaptureRequest> nextRequest;
4288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004289 while (mRequestQueue.empty()) {
4290 if (!mRepeatingRequests.empty()) {
4291 // Always atomically enqueue all requests in a repeating request
4292 // list. Guarantees a complete in-sequence set of captures to
4293 // application.
4294 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004295 if (mFirstRepeating) {
4296 mFirstRepeating = false;
4297 } else {
4298 for (auto& request : requests) {
4299 // For repeating requests, override timestamp request using
4300 // the time a request is inserted into the request queue,
4301 // because the original repeating request will have an old
4302 // fixed timestamp.
4303 request->mRequestTimeNs = systemTime();
4304 }
4305 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004306 RequestList::const_iterator firstRequest =
4307 requests.begin();
4308 nextRequest = *firstRequest;
4309 mRequestQueue.insert(mRequestQueue.end(),
4310 ++firstRequest,
4311 requests.end());
4312 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004313
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004314 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004315
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004316 break;
4317 }
4318
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004319 if (!mRequestClearing) {
4320 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004322
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004323 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4324 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004325 Mutex::Autolock pl(mPauseLock);
4326 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004327 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004328 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004329 if (mNotifyPipelineDrain) {
4330 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4331 mNotifyPipelineDrain = false;
4332 mStreamIdsToBeDrained.clear();
4333 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004334 // Let the tracker know
4335 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4336 if (statusTracker != 0) {
4337 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4338 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004339 sp<Camera3Device> parent = mParent.promote();
4340 if (parent != nullptr) {
4341 parent->mRequestBufferSM.onRequestThreadPaused();
4342 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004343 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004344 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004345 // Stop waiting for now and let thread management happen
4346 return NULL;
4347 }
4348 }
4349
4350 if (nextRequest == NULL) {
4351 // Don't have a repeating request already in hand, so queue
4352 // must have an entry now.
4353 RequestList::iterator firstRequest =
4354 mRequestQueue.begin();
4355 nextRequest = *firstRequest;
4356 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004357 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4358 sp<NotificationListener> listener = mListener.promote();
4359 if (listener != NULL) {
4360 listener->notifyRequestQueueEmpty();
4361 }
4362 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004363 }
4364
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004365 // In case we've been unpaused by setPaused clearing mDoPause, need to
4366 // update internal pause state (capture/setRepeatingRequest unpause
4367 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004368 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004369 if (mPaused) {
4370 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4371 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4372 if (statusTracker != 0) {
4373 statusTracker->markComponentActive(mStatusId);
4374 }
4375 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004376 mPaused = false;
4377
4378 // Check if we've reconfigured since last time, and reset the preview
4379 // request if so. Can't use 'NULL request == repeat' across configure calls.
4380 if (mReconfigured) {
4381 mPrevRequest.clear();
4382 mReconfigured = false;
4383 }
4384
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004385 if (nextRequest != NULL) {
4386 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004387 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4388 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004389
4390 // Since RequestThread::clear() removes buffers from the input stream,
4391 // get the right buffer here before unlocking mRequestLock
4392 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004393 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4394 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004395 if (res != OK) {
4396 // Can't get input buffer from gralloc queue - this could be due to
4397 // disconnected queue or other producer misbehavior, so not a fatal
4398 // error
4399 ALOGE("%s: Can't get input buffer, skipping request:"
4400 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004401
4402 sp<NotificationListener> listener = mListener.promote();
4403 if (listener != NULL) {
4404 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004405 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004406 nextRequest->mResultExtras);
4407 }
4408 return NULL;
4409 }
4410 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004411 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004412
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004413 return nextRequest;
4414}
4415
4416bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004417 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004418 status_t res;
4419 Mutex::Autolock l(mPauseLock);
4420 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004421 if (mPaused == false) {
4422 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004423 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004424 if (mNotifyPipelineDrain) {
4425 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4426 mNotifyPipelineDrain = false;
4427 mStreamIdsToBeDrained.clear();
4428 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004429 // Let the tracker know
4430 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4431 if (statusTracker != 0) {
4432 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4433 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004434 sp<Camera3Device> parent = mParent.promote();
4435 if (parent != nullptr) {
4436 parent->mRequestBufferSM.onRequestThreadPaused();
4437 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004438 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004439
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004440 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004441 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004442 return true;
4443 }
4444 }
4445 // We don't set mPaused to false here, because waitForNextRequest needs
4446 // to further manage the paused state in case of starvation.
4447 return false;
4448}
4449
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004450void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004451 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004452 // With work to do, mark thread as unpaused.
4453 // If paused by request (setPaused), don't resume, to avoid
4454 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004455 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004456 Mutex::Autolock p(mPauseLock);
4457 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004458 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4459 if (mPaused) {
4460 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4461 if (statusTracker != 0) {
4462 statusTracker->markComponentActive(mStatusId);
4463 }
4464 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004465 mPaused = false;
4466 }
4467}
4468
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004469void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4470 sp<Camera3Device> parent = mParent.promote();
4471 if (parent != NULL) {
4472 va_list args;
4473 va_start(args, fmt);
4474
4475 parent->setErrorStateV(fmt, args);
4476
4477 va_end(args);
4478 }
4479}
4480
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004481status_t Camera3Device::RequestThread::insertTriggers(
4482 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004483 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004484 Mutex::Autolock al(mTriggerMutex);
4485
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004486 sp<Camera3Device> parent = mParent.promote();
4487 if (parent == NULL) {
4488 CLOGE("RequestThread: Parent is gone");
4489 return DEAD_OBJECT;
4490 }
4491
Emilian Peevaebbe412018-01-15 13:53:24 +00004492 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004493 size_t count = mTriggerMap.size();
4494
4495 for (size_t i = 0; i < count; ++i) {
4496 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004497 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004498
4499 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4500 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4501 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004502 if (isAeTrigger) {
4503 request->mResultExtras.precaptureTriggerId = triggerId;
4504 mCurrentPreCaptureTriggerId = triggerId;
4505 } else {
4506 request->mResultExtras.afTriggerId = triggerId;
4507 mCurrentAfTriggerId = triggerId;
4508 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004509 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004510 }
4511
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004512 camera_metadata_entry entry = metadata.find(tag);
4513
4514 if (entry.count > 0) {
4515 /**
4516 * Already has an entry for this trigger in the request.
4517 * Rewrite it with our requested trigger value.
4518 */
4519 RequestTrigger oldTrigger = trigger;
4520
4521 oldTrigger.entryValue = entry.data.u8[0];
4522
4523 mTriggerReplacedMap.add(tag, oldTrigger);
4524 } else {
4525 /**
4526 * More typical, no trigger entry, so we just add it
4527 */
4528 mTriggerRemovedMap.add(tag, trigger);
4529 }
4530
4531 status_t res;
4532
4533 switch (trigger.getTagType()) {
4534 case TYPE_BYTE: {
4535 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4536 res = metadata.update(tag,
4537 &entryValue,
4538 /*count*/1);
4539 break;
4540 }
4541 case TYPE_INT32:
4542 res = metadata.update(tag,
4543 &trigger.entryValue,
4544 /*count*/1);
4545 break;
4546 default:
4547 ALOGE("%s: Type not supported: 0x%x",
4548 __FUNCTION__,
4549 trigger.getTagType());
4550 return INVALID_OPERATION;
4551 }
4552
4553 if (res != OK) {
4554 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4555 ", value %d", __FUNCTION__, trigger.getTagName(),
4556 trigger.entryValue);
4557 return res;
4558 }
4559
4560 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4561 trigger.getTagName(),
4562 trigger.entryValue);
4563 }
4564
4565 mTriggerMap.clear();
4566
4567 return count;
4568}
4569
4570status_t Camera3Device::RequestThread::removeTriggers(
4571 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004572 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004573 Mutex::Autolock al(mTriggerMutex);
4574
Emilian Peevaebbe412018-01-15 13:53:24 +00004575 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004576
4577 /**
4578 * Replace all old entries with their old values.
4579 */
4580 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4581 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4582
4583 status_t res;
4584
4585 uint32_t tag = trigger.metadataTag;
4586 switch (trigger.getTagType()) {
4587 case TYPE_BYTE: {
4588 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4589 res = metadata.update(tag,
4590 &entryValue,
4591 /*count*/1);
4592 break;
4593 }
4594 case TYPE_INT32:
4595 res = metadata.update(tag,
4596 &trigger.entryValue,
4597 /*count*/1);
4598 break;
4599 default:
4600 ALOGE("%s: Type not supported: 0x%x",
4601 __FUNCTION__,
4602 trigger.getTagType());
4603 return INVALID_OPERATION;
4604 }
4605
4606 if (res != OK) {
4607 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4608 ", trigger value %d", __FUNCTION__,
4609 trigger.getTagName(), trigger.entryValue);
4610 return res;
4611 }
4612 }
4613 mTriggerReplacedMap.clear();
4614
4615 /**
4616 * Remove all new entries.
4617 */
4618 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4619 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4620 status_t res = metadata.erase(trigger.metadataTag);
4621
4622 if (res != OK) {
4623 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4624 ", trigger value %d", __FUNCTION__,
4625 trigger.getTagName(), trigger.entryValue);
4626 return res;
4627 }
4628 }
4629 mTriggerRemovedMap.clear();
4630
4631 return OK;
4632}
4633
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004634status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004635 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004636 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004637 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004638 status_t res;
4639
Emilian Peevaebbe412018-01-15 13:53:24 +00004640 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004641
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004642 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004643 // exists
4644 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4645 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4646 if (afTrigger.count > 0 &&
4647 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4648 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004649 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004650 if (res != OK) return res;
4651 }
4652
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004653 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004654 // if none already exists
4655 camera_metadata_entry pcTrigger =
4656 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4657 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4658 if (pcTrigger.count > 0 &&
4659 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4660 pcId.count == 0) {
4661 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004662 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004663 if (res != OK) return res;
4664 }
4665
4666 return OK;
4667}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004668
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004669bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4670 const sp<CaptureRequest> &request) {
4671 ATRACE_CALL();
4672
Austin Borger18b30a72022-10-27 12:20:29 -07004673 if (mOverrideToPortrait) {
4674 Mutex::Autolock l(mTriggerMutex);
4675 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4676 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4677 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4678 &rotateAndCrop_u8, 1);
4679 return true;
4680 }
4681
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004682 if (request->mRotateAndCropAuto) {
4683 Mutex::Autolock l(mTriggerMutex);
4684 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4685
4686 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4687 if (rotateAndCropEntry.count > 0) {
4688 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4689 return false;
4690 } else {
4691 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4692 return true;
4693 }
4694 } else {
4695 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4696 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4697 &rotateAndCrop_u8, 1);
4698 return true;
4699 }
4700 }
4701 return false;
4702}
4703
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004704bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4705 ATRACE_CALL();
4706
4707 if (request->mAutoframingAuto) {
4708 Mutex::Autolock l(mTriggerMutex);
4709 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4710
4711 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4712 if (autoframingEntry.count > 0) {
4713 if (autoframingEntry.data.u8[0] == mAutoframingOverride) {
4714 return false;
4715 } else {
4716 autoframingEntry.data.u8[0] = mAutoframingOverride;
4717 return true;
4718 }
4719 } else {
4720 uint8_t autoframing_u8 = mAutoframingOverride;
4721 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4722 &autoframing_u8, 1);
4723 return true;
4724 }
4725 }
4726 return false;
4727}
4728
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004729bool Camera3Device::RequestThread::overrideTestPattern(
4730 const sp<CaptureRequest> &request) {
4731 ATRACE_CALL();
4732
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004733 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004734
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004735 Mutex::Autolock l(mTriggerMutex);
4736
4737 bool changed = false;
4738
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004739 // For a multi-camera, the physical cameras support the same set of
4740 // test pattern modes as the logical camera.
4741 for (auto& settings : request->mSettingsList) {
4742 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004743
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004744 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4745 int32_t testPatternData[4] = {
4746 settings.mOriginalTestPatternData[0],
4747 settings.mOriginalTestPatternData[1],
4748 settings.mOriginalTestPatternData[2],
4749 settings.mOriginalTestPatternData[3]
4750 };
4751 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4752 testPatternMode = mCameraMute;
4753 testPatternData[0] = 0;
4754 testPatternData[1] = 0;
4755 testPatternData[2] = 0;
4756 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004757 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004758
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004759 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4760 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4761 if (testPatternEntry.count > 0) {
4762 if (testPatternEntry.data.i32[0] != testPatternMode) {
4763 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004764 changed = true;
4765 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004766 } else if (supportTestPatternModeKey) {
4767 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4768 &testPatternMode, 1);
4769 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004770 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004771
4772 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4773 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4774 if (testPatternColor.count >= 4) {
4775 for (size_t i = 0; i < 4; i++) {
4776 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4777 testPatternColor.data.i32[i] = testPatternData[i];
4778 changed = true;
4779 }
4780 }
4781 } else if (supportTestPatternDataKey) {
4782 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4783 testPatternData, 4);
4784 changed = true;
4785 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004786 }
4787
4788 return changed;
4789}
4790
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004791status_t Camera3Device::RequestThread::setHalInterface(
4792 sp<HalInterface> newHalInterface) {
4793 if (newHalInterface.get() == nullptr) {
4794 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4795 return DEAD_OBJECT;
4796 }
4797
4798 mInterface = newHalInterface;
4799
4800 return OK;
4801}
4802
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004803/**
4804 * PreparerThread inner class methods
4805 */
4806
4807Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004808 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004809 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004810}
4811
4812Camera3Device::PreparerThread::~PreparerThread() {
4813 Thread::requestExitAndWait();
4814 if (mCurrentStream != nullptr) {
4815 mCurrentStream->cancelPrepare();
4816 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4817 mCurrentStream.clear();
4818 }
4819 clear();
4820}
4821
Ruben Brunkc78ac262015-08-13 17:58:46 -07004822status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004823 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004824 status_t res;
4825
4826 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004827 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004828
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004829 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004830 if (res == OK) {
4831 // No preparation needed, fire listener right off
4832 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004833 if (listener != NULL) {
4834 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004835 }
4836 return OK;
4837 } else if (res != NOT_ENOUGH_DATA) {
4838 return res;
4839 }
4840
4841 // Need to prepare, start up thread if necessary
4842 if (!mActive) {
4843 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4844 // isn't running
4845 Thread::requestExitAndWait();
4846 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4847 if (res != OK) {
4848 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004849 if (listener != NULL) {
4850 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004851 }
4852 return res;
4853 }
4854 mCancelNow = false;
4855 mActive = true;
4856 ALOGV("%s: Preparer stream started", __FUNCTION__);
4857 }
4858
4859 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004860 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004861 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4862
4863 return OK;
4864}
4865
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004866void Camera3Device::PreparerThread::pause() {
4867 ATRACE_CALL();
4868
4869 Mutex::Autolock l(mLock);
4870
4871 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4872 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4873 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4874 int currentMaxCount = mCurrentMaxCount;
4875 mPendingStreams.clear();
4876 mCancelNow = true;
4877 while (mActive) {
4878 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4879 if (res == TIMED_OUT) {
4880 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4881 return;
4882 } else if (res != OK) {
4883 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4884 return;
4885 }
4886 }
4887
4888 //Check whether the prepare thread was able to complete the current
4889 //stream. In case work is still pending emplace it along with the rest
4890 //of the streams in the pending list.
4891 if (currentStream != nullptr) {
4892 if (!mCurrentPrepareComplete) {
4893 pendingStreams.emplace(currentMaxCount, currentStream);
4894 }
4895 }
4896
4897 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4898 for (const auto& it : mPendingStreams) {
4899 it.second->cancelPrepare();
4900 }
4901}
4902
4903status_t Camera3Device::PreparerThread::resume() {
4904 ATRACE_CALL();
4905 status_t res;
4906
4907 Mutex::Autolock l(mLock);
4908 sp<NotificationListener> listener = mListener.promote();
4909
4910 if (mActive) {
4911 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4912 return NO_INIT;
4913 }
4914
4915 auto it = mPendingStreams.begin();
4916 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004917 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004918 if (res == OK) {
4919 if (listener != NULL) {
4920 listener->notifyPrepared(it->second->getId());
4921 }
4922 it = mPendingStreams.erase(it);
4923 } else if (res != NOT_ENOUGH_DATA) {
4924 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4925 res, strerror(-res));
4926 it = mPendingStreams.erase(it);
4927 } else {
4928 it++;
4929 }
4930 }
4931
4932 if (mPendingStreams.empty()) {
4933 return OK;
4934 }
4935
4936 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4937 if (res != OK) {
4938 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4939 __FUNCTION__, res, strerror(-res));
4940 return res;
4941 }
4942 mCancelNow = false;
4943 mActive = true;
4944 ALOGV("%s: Preparer stream started", __FUNCTION__);
4945
4946 return OK;
4947}
4948
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004949status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004950 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004951 Mutex::Autolock l(mLock);
4952
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004953 for (const auto& it : mPendingStreams) {
4954 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004955 }
4956 mPendingStreams.clear();
4957 mCancelNow = true;
4958
4959 return OK;
4960}
4961
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004962void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004963 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004964 Mutex::Autolock l(mLock);
4965 mListener = listener;
4966}
4967
4968bool Camera3Device::PreparerThread::threadLoop() {
4969 status_t res;
4970 {
4971 Mutex::Autolock l(mLock);
4972 if (mCurrentStream == nullptr) {
4973 // End thread if done with work
4974 if (mPendingStreams.empty()) {
4975 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4976 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4977 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4978 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004979 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004980 return false;
4981 }
4982
4983 // Get next stream to prepare
4984 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004985 mCurrentStream = it->second;
4986 mCurrentMaxCount = it->first;
4987 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004988 mPendingStreams.erase(it);
4989 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4990 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4991 } else if (mCancelNow) {
4992 mCurrentStream->cancelPrepare();
4993 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4994 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4995 mCurrentStream.clear();
4996 mCancelNow = false;
4997 return true;
4998 }
4999 }
5000
5001 res = mCurrentStream->prepareNextBuffer();
5002 if (res == NOT_ENOUGH_DATA) return true;
5003 if (res != OK) {
5004 // Something bad happened; try to recover by cancelling prepare and
5005 // signalling listener anyway
5006 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5007 mCurrentStream->getId(), res, strerror(-res));
5008 mCurrentStream->cancelPrepare();
5009 }
5010
5011 // This stream has finished, notify listener
5012 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005013 sp<NotificationListener> listener = mListener.promote();
5014 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005015 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5016 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005017 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005018 }
5019
5020 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5021 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005022 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005023
5024 return true;
5025}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005026
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005027status_t Camera3Device::RequestBufferStateMachine::initialize(
5028 sp<camera3::StatusTracker> statusTracker) {
5029 if (statusTracker == nullptr) {
5030 ALOGE("%s: statusTracker is null", __FUNCTION__);
5031 return BAD_VALUE;
5032 }
5033
5034 std::lock_guard<std::mutex> lock(mLock);
5035 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005036 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005037 return OK;
5038}
5039
5040bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5041 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005042 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005043 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005044 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005045 return true;
5046 }
5047 return false;
5048}
5049
5050void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5051 std::lock_guard<std::mutex> lock(mLock);
5052 if (!mRequestBufferOngoing) {
5053 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5054 return;
5055 }
5056 mRequestBufferOngoing = false;
5057 if (mStatus == RB_STATUS_PENDING_STOP) {
5058 checkSwitchToStopLocked();
5059 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005060 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005061}
5062
5063void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5064 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005065 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005066 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005067 return;
5068}
5069
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005070void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005071 std::lock_guard<std::mutex> lock(mLock);
5072 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005073 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5074 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005075 mInflightMapEmpty = false;
5076 if (mStatus == RB_STATUS_STOPPED) {
5077 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005078 }
5079 return;
5080}
5081
5082void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5083 std::lock_guard<std::mutex> lock(mLock);
5084 mRequestThreadPaused = true;
5085 if (mStatus == RB_STATUS_PENDING_STOP) {
5086 checkSwitchToStopLocked();
5087 }
5088 return;
5089}
5090
5091void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5092 std::lock_guard<std::mutex> lock(mLock);
5093 mInflightMapEmpty = true;
5094 if (mStatus == RB_STATUS_PENDING_STOP) {
5095 checkSwitchToStopLocked();
5096 }
5097 return;
5098}
5099
5100void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5101 std::lock_guard<std::mutex> lock(mLock);
5102 if (!checkSwitchToStopLocked()) {
5103 mStatus = RB_STATUS_PENDING_STOP;
5104 }
5105 return;
5106}
5107
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005108bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5109 std::lock_guard<std::mutex> lock(mLock);
5110 if (mRequestBufferOngoing) {
5111 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5112 __FUNCTION__);
5113 return false;
5114 }
5115 mSwitchedToOffline = true;
5116 mInflightMapEmpty = true;
5117 mRequestThreadPaused = true;
5118 mStatus = RB_STATUS_STOPPED;
5119 return true;
5120}
5121
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005122void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5123 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5124 if (statusTracker != nullptr) {
5125 if (active) {
5126 statusTracker->markComponentActive(mRequestBufferStatusId);
5127 } else {
5128 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5129 }
5130 }
5131}
5132
5133bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5134 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5135 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005136 return true;
5137 }
5138 return false;
5139}
5140
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005141bool Camera3Device::startRequestBuffer() {
5142 return mRequestBufferSM.startRequestBuffer();
5143}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005144
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005145void Camera3Device::endRequestBuffer() {
5146 mRequestBufferSM.endRequestBuffer();
5147}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005148
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005149nsecs_t Camera3Device::getWaitDuration() {
5150 return kBaseGetBufferWait + getExpectedInFlightDuration();
5151}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005152
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005153void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5154 mInterface->getInflightBufferKeys(out);
5155}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005156
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005157void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5158 mInterface->getInflightRequestBufferKeys(out);
5159}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005160
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005161std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5162 std::vector<sp<Camera3StreamInterface>> ret;
5163 bool hasInputStream = mInputStream != nullptr;
5164 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5165 if (hasInputStream) {
5166 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005167 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005168 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5169 ret.push_back(mOutputStreams[i]);
5170 }
5171 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5172 ret.push_back(mDeletedStreams[i]);
5173 }
5174 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005175}
5176
Emilian Peevcc0b7952020-01-07 13:54:47 -08005177void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5178 ATRACE_CALL();
5179
5180 if (offlineStreamIds == nullptr) {
5181 return;
5182 }
5183
5184 Mutex::Autolock il(mInterfaceLock);
5185
5186 auto streamIds = mOutputStreams.getStreamIds();
5187 bool hasInputStream = mInputStream != nullptr;
5188 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5189 offlineStreamIds->push_back(mInputStream->getId());
5190 }
5191
5192 for (const auto & streamId : streamIds) {
5193 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5194 // Streams that use the camera buffer manager are currently not supported in
5195 // offline mode
5196 if (stream->getOfflineProcessingSupport() &&
5197 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5198 offlineStreamIds->push_back(streamId);
5199 }
5200 }
5201}
5202
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005203status_t Camera3Device::setRotateAndCropAutoBehavior(
5204 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5205 ATRACE_CALL();
5206 Mutex::Autolock il(mInterfaceLock);
5207 Mutex::Autolock l(mLock);
5208 if (mRequestThread == nullptr) {
5209 return INVALID_OPERATION;
5210 }
5211 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5212}
5213
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005214status_t Camera3Device::setAutoframingAutoBehavior(
5215 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5216 ATRACE_CALL();
5217 Mutex::Autolock il(mInterfaceLock);
5218 Mutex::Autolock l(mLock);
5219 if (mRequestThread == nullptr) {
5220 return INVALID_OPERATION;
5221 }
5222 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5223}
5224
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005225bool Camera3Device::supportsCameraMute() {
5226 Mutex::Autolock il(mInterfaceLock);
5227 Mutex::Autolock l(mLock);
5228
5229 return mSupportCameraMute;
5230}
5231
5232status_t Camera3Device::setCameraMute(bool enabled) {
5233 ATRACE_CALL();
5234 Mutex::Autolock il(mInterfaceLock);
5235 Mutex::Autolock l(mLock);
5236
5237 if (mRequestThread == nullptr || !mSupportCameraMute) {
5238 return INVALID_OPERATION;
5239 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005240 int32_t muteMode =
5241 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5242 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5243 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5244 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005245}
5246
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005247status_t Camera3Device::injectCamera(const String8& injectedCamId,
5248 sp<CameraProviderManager> manager) {
5249 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5250 ATRACE_CALL();
5251 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005252 // When the camera device is active, injectCamera() and stopInjection() will call
5253 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5254 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5255 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5256 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5257 // waitUntilStateThenRelock().
5258 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005259
5260 status_t res = NO_ERROR;
5261 if (mInjectionMethods->isInjecting()) {
5262 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5263 return OK;
5264 } else {
5265 res = mInjectionMethods->stopInjection();
5266 if (res != OK) {
5267 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5268 __FUNCTION__, res);
5269 return res;
5270 }
5271 }
5272 }
5273
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005274 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005275 if (res != OK) {
5276 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5277 __FUNCTION__, res);
5278 return res;
5279 }
5280
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005281 // When the second display of android is cast to the remote device, and the opened camera is
5282 // also cast to the second display, in this case, because the camera has configured the streams
5283 // at this time, we can directly call injectCamera() to replace the internal camera with
5284 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005285 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5286 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5287
5288 camera3::camera_stream_configuration injectionConfig;
5289 std::vector<uint32_t> injectionBufferSizes;
5290 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5291 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5292 || injectionBufferSizes.size() <= 0) {
5293 ALOGE("Failed to inject camera due to abandoned configuration! "
5294 "mOperatingMode: %d injectionConfig.num_streams: %d "
5295 "injectionBufferSizes.size(): %zu", mOperatingMode,
5296 injectionConfig.num_streams, injectionBufferSizes.size());
5297 return DEAD_OBJECT;
5298 }
5299
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005300 res = mInjectionMethods->injectCamera(
5301 injectionConfig, injectionBufferSizes);
5302 if (res != OK) {
5303 ALOGE("Can't finish inject camera process!");
5304 return res;
5305 }
5306 }
5307
5308 return OK;
5309}
5310
5311status_t Camera3Device::stopInjection() {
5312 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5313 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005314 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005315 return mInjectionMethods->stopInjection();
5316}
5317
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005318}; // namespace android