blob: 7b5ccc86f4bfe8d087f26a459623c1e0cc27499c [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,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001004 int timestampBase, int mirrorMode, int32_t colorSpace) {
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,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001018 streamUseCase, timestampBase, mirrorMode, colorSpace);
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,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001039 int timestampBase, int mirrorMode, int32_t colorSpace) {
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,"
Austin Borger9e2b27c2022-07-15 11:27:24 -07001048 " mirrorMode %d colorSpace %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,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001051 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 status_t res;
1054 bool wasActive = false;
1055
1056 switch (mStatus) {
1057 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001058 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001059 return INVALID_OPERATION;
1060 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001061 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063 case STATUS_UNCONFIGURED:
1064 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 // OK
1066 break;
1067 case STATUS_ACTIVE:
1068 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001069 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001072 return res;
1073 }
1074 wasActive = true;
1075 break;
1076 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001077 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001078 return INVALID_OPERATION;
1079 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081
1082 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001083
Shuzhen Wang0129d522016-10-30 22:43:41 -07001084 if (consumers.size() == 0 && !hasDeferredConsumer) {
1085 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1086 return BAD_VALUE;
1087 }
Zhijun He5d677d12016-05-29 16:52:39 -07001088
Shuzhen Wang0129d522016-10-30 22:43:41 -07001089 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001090 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1091 return BAD_VALUE;
1092 }
1093
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001094 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1095 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1096 // be found in only one sensor pixel mode.
1097 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1098 return BAD_VALUE;
1099 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001100 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001101 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001102 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001103 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001104 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001105 if (blobBufferSize <= 0) {
1106 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1107 return BAD_VALUE;
1108 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001109 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1110 blobBufferSize = width * height;
1111 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001112 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001113 if (blobBufferSize <= 0) {
1114 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1115 return BAD_VALUE;
1116 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001117 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001118 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001119 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001120 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001121 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001122 timestampBase, mirrorMode, colorSpace);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001123 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001124 bool maxResolution =
1125 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1126 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001127 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1128 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001129 if (rawOpaqueBufferSize <= 0) {
1130 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1131 return BAD_VALUE;
1132 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001133 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001134 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001135 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001136 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001137 timestampBase, mirrorMode, colorSpace);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001138 } else if (isShared) {
1139 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1140 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001141 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001142 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001143 timestampBase, mirrorMode, colorSpace);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001144 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001145 newStream = new Camera3OutputStream(mNextStreamId,
1146 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001147 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001148 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001149 timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001150 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001151 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001152 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001153 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001154 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Austin Borger9e2b27c2022-07-15 11:27:24 -07001155 timestampBase, mirrorMode, colorSpace);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001156 }
Emilian Peev40ead602017-09-26 15:46:36 +01001157
1158 size_t consumerCount = consumers.size();
1159 for (size_t i = 0; i < consumerCount; i++) {
1160 int id = newStream->getSurfaceId(consumers[i]);
1161 if (id < 0) {
1162 SET_ERR_L("Invalid surface id");
1163 return BAD_VALUE;
1164 }
1165 if (surfaceIds != nullptr) {
1166 surfaceIds->push_back(id);
1167 }
1168 }
1169
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001170 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171
Emilian Peev08dd2452017-04-06 16:55:14 +01001172 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001173
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001174 newStream->setImageDumpMask(mImageDumpMask);
1175
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176 res = mOutputStreams.add(mNextStreamId, newStream);
1177 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return res;
1180 }
1181
Shuzhen Wang316781a2020-08-18 18:11:01 -07001182 mSessionStatsBuilder.addStream(mNextStreamId);
1183
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001185 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001186
1187 // Continue captures if active at start
1188 if (wasActive) {
1189 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001190 // Reuse current operating mode and session parameters for new stream config
1191 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001193 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1194 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 return res;
1196 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001197 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001199 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001201}
1202
Emilian Peev710c1422017-08-30 11:19:38 +01001203status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001205 if (nullptr == streamInfo) {
1206 return BAD_VALUE;
1207 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001209 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001210
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 switch (mStatus) {
1212 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001213 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 return INVALID_OPERATION;
1215 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001218 case STATUS_UNCONFIGURED:
1219 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001220 case STATUS_ACTIVE:
1221 // OK
1222 break;
1223 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001224 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 return INVALID_OPERATION;
1226 }
1227
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001228 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1229 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001230 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001231 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 }
1233
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001234 streamInfo->width = stream->getWidth();
1235 streamInfo->height = stream->getHeight();
1236 streamInfo->format = stream->getFormat();
1237 streamInfo->dataSpace = stream->getDataSpace();
1238 streamInfo->formatOverridden = stream->isFormatOverridden();
1239 streamInfo->originalFormat = stream->getOriginalFormat();
1240 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1241 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001242 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001243 streamInfo->colorSpace = stream->getColorSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001244 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001245}
1246
1247status_t Camera3Device::setStreamTransform(int id,
1248 int transform) {
1249 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001252
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 switch (mStatus) {
1254 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
1257 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001258 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001260 case STATUS_UNCONFIGURED:
1261 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 case STATUS_ACTIVE:
1263 // OK
1264 break;
1265 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001266 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001267 return INVALID_OPERATION;
1268 }
1269
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001270 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1271 if (stream == nullptr) {
1272 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001273 return BAD_VALUE;
1274 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001275 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276}
1277
1278status_t Camera3Device::deleteStream(int id) {
1279 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001280 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 Mutex::Autolock l(mLock);
1282 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001283
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001284 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001285
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 // CameraDevice semantics require device to already be idle before
1287 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001289 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001290 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 }
1292
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001293 if (mStatus == STATUS_ERROR) {
1294 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1295 __FUNCTION__, mId.string());
1296 return -EBUSY;
1297 }
1298
Igor Murashkin2fba5842013-04-22 14:03:54 -07001299 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001300 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301 if (mInputStream != NULL && id == mInputStream->getId()) {
1302 deletedStream = mInputStream;
1303 mInputStream.clear();
1304 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001305 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001306 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001307 return BAD_VALUE;
1308 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001309 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001310 }
1311
1312 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001313 if (stream != nullptr) {
1314 deletedStream = stream;
1315 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316 }
1317
1318 // Free up the stream endpoint so that it can be used by some other stream
1319 res = deletedStream->disconnect();
1320 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001321 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 // fall through since we want to still list the stream as deleted.
1323 }
1324 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001325 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326
1327 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001328}
1329
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001330status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001331 ATRACE_CALL();
1332 ALOGV("%s: E", __FUNCTION__);
1333
1334 Mutex::Autolock il(mInterfaceLock);
1335 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001336
Emilian Peev811d2952018-05-25 11:08:40 +01001337 // In case the client doesn't include any session parameter, try a
1338 // speculative configuration using the values from the last cached
1339 // default request.
1340 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001341 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001342 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1343 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1344 mLastTemplateId);
1345 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1346 operatingMode);
1347 }
1348
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001349 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1350}
1351
1352status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1353 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001354 //Filter out any incoming session parameters
1355 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001356 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1357 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001358 CameraMetadata filteredParams(availableSessionKeys.count);
1359 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1360 filteredParams.getAndLock());
1361 set_camera_metadata_vendor_id(meta, mVendorTagId);
1362 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001363 if (availableSessionKeys.count > 0) {
1364 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1365 camera_metadata_ro_entry entry = params.find(
1366 availableSessionKeys.data.i32[i]);
1367 if (entry.count > 0) {
1368 filteredParams.update(entry);
1369 }
1370 }
1371 }
1372
1373 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001374}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001375
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001376status_t Camera3Device::getInputBufferProducer(
1377 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001378 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001379 Mutex::Autolock il(mInterfaceLock);
1380 Mutex::Autolock l(mLock);
1381
1382 if (producer == NULL) {
1383 return BAD_VALUE;
1384 } else if (mInputStream == NULL) {
1385 return INVALID_OPERATION;
1386 }
1387
1388 return mInputStream->getInputBufferProducer(producer);
1389}
1390
Emilian Peevf4816702020-04-03 15:44:51 -07001391status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001392 CameraMetadata *request) {
1393 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001394 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001395
Emilian Peevf4816702020-04-03 15:44:51 -07001396 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001397 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001398 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001399 return BAD_VALUE;
1400 }
1401
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001404 {
1405 Mutex::Autolock l(mLock);
1406 switch (mStatus) {
1407 case STATUS_ERROR:
1408 CLOGE("Device has encountered a serious error");
1409 return INVALID_OPERATION;
1410 case STATUS_UNINITIALIZED:
1411 CLOGE("Device is not initialized!");
1412 return INVALID_OPERATION;
1413 case STATUS_UNCONFIGURED:
1414 case STATUS_CONFIGURED:
1415 case STATUS_ACTIVE:
1416 // OK
1417 break;
1418 default:
1419 SET_ERR_L("Unexpected status: %d", mStatus);
1420 return INVALID_OPERATION;
1421 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001422
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001423 if (!mRequestTemplateCache[templateId].isEmpty()) {
1424 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001425 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001426 return OK;
1427 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001428 }
1429
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001430 camera_metadata_t *rawRequest;
1431 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001432 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001433
1434 {
1435 Mutex::Autolock l(mLock);
1436 if (res == BAD_VALUE) {
1437 ALOGI("%s: template %d is not supported on this camera device",
1438 __FUNCTION__, templateId);
1439 return res;
1440 } else if (res != OK) {
1441 CLOGE("Unable to construct request template %d: %s (%d)",
1442 templateId, strerror(-res), res);
1443 return res;
1444 }
1445
1446 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1447 mRequestTemplateCache[templateId].acquire(rawRequest);
1448
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001449 // Override the template request with zoomRatioMapper
1450 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1451 &mRequestTemplateCache[templateId]);
1452 if (res != OK) {
1453 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1454 templateId, strerror(-res), res);
1455 return res;
1456 }
1457
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001458 // Fill in JPEG_QUALITY if not available
1459 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1460 static const uint8_t kDefaultJpegQuality = 95;
1461 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1462 &kDefaultJpegQuality, 1);
1463 }
1464
Bharatt Kukrejad33fe9f2022-11-23 21:52:52 +00001465 // Fill in AUTOFRAMING if not available
1466 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1467 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1468 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1469 &kDefaultAutoframingMode, 1);
1470 }
1471
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001472 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001473 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001474 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001475 return OK;
1476}
1477
1478status_t Camera3Device::waitUntilDrained() {
1479 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001481 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001483
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001484 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001485}
1486
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001487status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 switch (mStatus) {
1489 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001490 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001491 ALOGV("%s: Already idle", __FUNCTION__);
1492 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001493 case STATUS_CONFIGURED:
1494 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001495 case STATUS_ERROR:
1496 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001497 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498 break;
1499 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001500 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501 return INVALID_OPERATION;
1502 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001503 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1504 maxExpectedDuration);
1505 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001506 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001507 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001508 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1509 res);
1510 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001511 return res;
1512}
1513
Ruben Brunk183f0562015-08-12 12:55:02 -07001514void Camera3Device::internalUpdateStatusLocked(Status status) {
1515 mStatus = status;
1516 mRecentStatusUpdates.add(mStatus);
1517 mStatusChanged.broadcast();
1518}
1519
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001521status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001522 if (mRequestThread.get() != nullptr) {
1523 mRequestThread->setPaused(true);
1524 } else {
1525 return NO_INIT;
1526 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001527
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001528 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1529 maxExpectedDuration);
1530 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001531 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001532 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001533 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001534 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 }
1536
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 return res;
1538}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001539
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001540// Resume after internalPauseAndWaitLocked
1541status_t Camera3Device::internalResumeLocked() {
1542 status_t res;
1543
1544 mRequestThread->setPaused(false);
1545
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001546 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1547 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001548 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1549 if (res != OK) {
1550 SET_ERR_L("Can't transition to active in %f seconds!",
1551 kActiveTimeout/1e9);
1552 }
1553 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001554 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001555}
1556
Ruben Brunk183f0562015-08-12 12:55:02 -07001557status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001558 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001559
1560 size_t startIndex = 0;
1561 if (mStatusWaiters == 0) {
1562 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1563 // this status list
1564 mRecentStatusUpdates.clear();
1565 } else {
1566 // If other threads are waiting on updates to this status list, set the position of the
1567 // first element that this list will check rather than clearing the list.
1568 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001569 }
1570
Ruben Brunk183f0562015-08-12 12:55:02 -07001571 mStatusWaiters++;
1572
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001573 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001574 if (!active && mUseHalBufManager) {
1575 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001576 if (mStatus == STATUS_ACTIVE) {
1577 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001578 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001579 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001580 mRequestBufferSM.onWaitUntilIdle();
1581 }
1582
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001584 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001586 if (active == (mStatus == STATUS_ACTIVE)) {
1587 // Desired state is current
1588 break;
1589 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001590
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001591 nsecs_t timeElapsed = systemTime() - startTime;
1592 nsecs_t timeToWait = timeout - timeElapsed;
1593 if (timeToWait <= 0) {
1594 // Thread woke up spuriously but has timed out since.
1595 // Force out of loop with TIMED_OUT result.
1596 res = TIMED_OUT;
1597 break;
1598 }
1599 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001600 if (res != OK) break;
1601
Ruben Brunk183f0562015-08-12 12:55:02 -07001602 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1603 // transitions.
1604 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1605 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1606 __FUNCTION__);
1607
1608 // Encountered desired state since we began waiting
1609 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001610 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1611 stateSeen = true;
1612 break;
1613 }
1614 }
1615 } while (!stateSeen);
1616
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001617 if (signalPipelineDrain) {
1618 mRequestThread->resetPipelineDrain();
1619 }
1620
Ruben Brunk183f0562015-08-12 12:55:02 -07001621 mStatusWaiters--;
1622
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001623 return res;
1624}
1625
1626
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001627status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001628 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001629 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001630
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001631 if (listener != NULL && mListener != NULL) {
1632 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1633 }
1634 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001635 mRequestThread->setNotificationListener(listener);
1636 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001637
1638 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001639}
1640
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001641bool Camera3Device::willNotify3A() {
1642 return false;
1643}
1644
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001645status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001646 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001647 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001648
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001649 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001650 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1651 if (st == std::cv_status::timeout) {
1652 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001653 }
1654 }
1655 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001656}
1657
Jianing Weicb0652e2014-03-12 18:29:36 -07001658status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001659 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001660 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001661
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001662 if (mResultQueue.empty()) {
1663 return NOT_ENOUGH_DATA;
1664 }
1665
Jianing Weicb0652e2014-03-12 18:29:36 -07001666 if (frame == NULL) {
1667 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1668 return BAD_VALUE;
1669 }
1670
1671 CaptureResult &result = *(mResultQueue.begin());
1672 frame->mResultExtras = result.mResultExtras;
1673 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001674 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001675 mResultQueue.erase(mResultQueue.begin());
1676
1677 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001678}
1679
1680status_t Camera3Device::triggerAutofocus(uint32_t id) {
1681 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001682 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001683
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001684 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1685 // Mix-in this trigger into the next request and only the next request.
1686 RequestTrigger trigger[] = {
1687 {
1688 ANDROID_CONTROL_AF_TRIGGER,
1689 ANDROID_CONTROL_AF_TRIGGER_START
1690 },
1691 {
1692 ANDROID_CONTROL_AF_TRIGGER_ID,
1693 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001694 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001695 };
1696
1697 return mRequestThread->queueTrigger(trigger,
1698 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001699}
1700
1701status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1702 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001703 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001704
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001705 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1706 // Mix-in this trigger into the next request and only the next request.
1707 RequestTrigger trigger[] = {
1708 {
1709 ANDROID_CONTROL_AF_TRIGGER,
1710 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1711 },
1712 {
1713 ANDROID_CONTROL_AF_TRIGGER_ID,
1714 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001715 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001716 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001717
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001718 return mRequestThread->queueTrigger(trigger,
1719 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001720}
1721
1722status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1723 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001724 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001725
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001726 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1727 // Mix-in this trigger into the next request and only the next request.
1728 RequestTrigger trigger[] = {
1729 {
1730 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1731 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1732 },
1733 {
1734 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1735 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001736 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001737 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001738
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001739 return mRequestThread->queueTrigger(trigger,
1740 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001741}
1742
Jianing Weicb0652e2014-03-12 18:29:36 -07001743status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001744 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001745 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001747
Zhijun He7ef20392014-04-21 16:04:17 -07001748 {
1749 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001750
1751 // b/116514106 "disconnect()" can get called twice for the same device. The
1752 // camera device will not be initialized during the second run.
1753 if (mStatus == STATUS_UNINITIALIZED) {
1754 return OK;
1755 }
1756
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001757 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001758
1759 // Stop session and stream counter
1760 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001761 }
1762
Ravneetdbd5b242022-03-02 07:22:46 +00001763 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001764 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001765 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1766 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1767
1768 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001769}
1770
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001771status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001772 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1773}
1774
1775status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001776 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001777 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001778 Mutex::Autolock il(mInterfaceLock);
1779 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001780
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001781 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1782 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001783 CLOGE("Stream %d does not exist", streamId);
1784 return BAD_VALUE;
1785 }
1786
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001787 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001788 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001789 return BAD_VALUE;
1790 }
1791
1792 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001793 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001794 return BAD_VALUE;
1795 }
1796
Ruben Brunkc78ac262015-08-13 17:58:46 -07001797 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001798}
1799
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001800status_t Camera3Device::tearDown(int streamId) {
1801 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001802 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001803 Mutex::Autolock il(mInterfaceLock);
1804 Mutex::Autolock l(mLock);
1805
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001806 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1807 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001808 CLOGE("Stream %d does not exist", streamId);
1809 return BAD_VALUE;
1810 }
1811
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001812 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1813 CLOGE("Stream %d is a target of a in-progress request", streamId);
1814 return BAD_VALUE;
1815 }
1816
1817 return stream->tearDown();
1818}
1819
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001820status_t Camera3Device::addBufferListenerForStream(int streamId,
1821 wp<Camera3StreamBufferListener> listener) {
1822 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001823 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001824 Mutex::Autolock il(mInterfaceLock);
1825 Mutex::Autolock l(mLock);
1826
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001827 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1828 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001829 CLOGE("Stream %d does not exist", streamId);
1830 return BAD_VALUE;
1831 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001832 stream->addBufferListener(listener);
1833
1834 return OK;
1835}
1836
Austin Borger4a870a32022-02-25 01:48:41 +00001837float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1838 camera_metadata_entry minDurations =
1839 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1840 for (size_t i = 0; i < minDurations.count; i += 4) {
1841 if (minDurations.data.i64[i] == stream->getFormat()
1842 && minDurations.data.i64[i+1] == stream->getWidth()
1843 && minDurations.data.i64[i+2] == stream->getHeight()) {
1844 int64_t minFrameDuration = minDurations.data.i64[i+3];
1845 return 1e9f / minFrameDuration;
1846 }
1847 }
1848 return 0.0f;
1849}
1850
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001852 * Methods called by subclasses
1853 */
1854
1855void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001856 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001857 std::vector<int> streamIds;
1858 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001859 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001860
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001861 {
1862 // Need mLock to safely update state and synchronize to current
1863 // state of methods in flight.
1864 Mutex::Autolock l(mLock);
1865 // We can get various system-idle notices from the status tracker
1866 // while starting up. Only care about them if we've actually sent
1867 // in some requests recently.
1868 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1869 return;
1870 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001871 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1872 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001873 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001874
1875 // Skip notifying listener if we're doing some user-transparent
1876 // state changes
1877 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001878
Austin Borger4a870a32022-02-25 01:48:41 +00001879 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1880 auto stream = mOutputStreams[i];
1881 if (stream.get() == nullptr) continue;
1882
1883 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1884 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1885
1886 // Populate stream statistics in case of Idle
1887 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001888 streamIds.push_back(stream->getId());
1889 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1890 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001891 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001892 if (camera3Stream != nullptr) {
1893 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001894 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001895 }
1896 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001897 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001898 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001899 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001900 stream->getDynamicRangeProfile(), streamUseCase,
1901 stream->getColorSpace());
Shuzhen Wang316781a2020-08-18 18:11:01 -07001902 }
1903 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001904 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001905
1906 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001907 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001908 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001909 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001910 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001911 status_t res = OK;
1912 if (listener != nullptr) {
1913 if (idle) {
1914 // Get session stats from the builder, and notify the listener.
1915 int64_t requestCount, resultErrorCount;
1916 bool deviceError;
1917 std::map<int, StreamStats> streamStatsMap;
1918 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1919 &deviceError, &streamStatsMap);
1920 for (size_t i = 0; i < streamIds.size(); i++) {
1921 int streamId = streamIds[i];
1922 auto stats = streamStatsMap.find(streamId);
1923 if (stats != streamStatsMap.end()) {
1924 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1925 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1926 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1927 streamStats[i].mHistogramType =
1928 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1929 streamStats[i].mHistogramBins.assign(
1930 stats->second.mCaptureLatencyBins.begin(),
1931 stats->second.mCaptureLatencyBins.end());
1932 streamStats[i].mHistogramCounts.assign(
1933 stats->second.mCaptureLatencyHistogram.begin(),
1934 stats->second.mCaptureLatencyHistogram.end());
1935 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001936 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001937 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1938 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001939 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001940 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001941 }
1942 if (res != OK) {
1943 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1944 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001945 }
1946}
1947
Shuzhen Wang758c2152017-01-10 18:26:18 -08001948status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001949 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001950 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001951 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1952 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001953
1954 if (surfaceIds == nullptr) {
1955 return BAD_VALUE;
1956 }
1957
Zhijun He5d677d12016-05-29 16:52:39 -07001958 Mutex::Autolock il(mInterfaceLock);
1959 Mutex::Autolock l(mLock);
1960
Shuzhen Wang758c2152017-01-10 18:26:18 -08001961 if (consumers.size() == 0) {
1962 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001963 return BAD_VALUE;
1964 }
1965
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001966 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1967 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001968 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001969 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001970 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001971
1972 // isConsumerConfigurationDeferred will be off after setConsumers
1973 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001974 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001975 if (res != OK) {
1976 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1977 return res;
1978 }
1979
Emilian Peev40ead602017-09-26 15:46:36 +01001980 for (auto &consumer : consumers) {
1981 int id = stream->getSurfaceId(consumer);
1982 if (id < 0) {
1983 CLOGE("Invalid surface id!");
1984 return BAD_VALUE;
1985 }
1986 surfaceIds->push_back(id);
1987 }
1988
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001989 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001990 if (!stream->isConfiguring()) {
1991 CLOGE("Stream %d was already fully configured.", streamId);
1992 return INVALID_OPERATION;
1993 }
Zhijun He5d677d12016-05-29 16:52:39 -07001994
Shuzhen Wang0129d522016-10-30 22:43:41 -07001995 res = stream->finishConfiguration();
1996 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001997 // If finishConfiguration fails due to abandoned surface, do not set
1998 // device to error state.
1999 bool isSurfaceAbandoned =
2000 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2001 if (!isSurfaceAbandoned) {
2002 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2003 stream->getId(), strerror(-res), res);
2004 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002005 return res;
2006 }
Zhijun He5d677d12016-05-29 16:52:39 -07002007 }
2008
2009 return OK;
2010}
2011
Emilian Peev40ead602017-09-26 15:46:36 +01002012status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2013 const std::vector<OutputStreamInfo> &outputInfo,
2014 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2015 Mutex::Autolock il(mInterfaceLock);
2016 Mutex::Autolock l(mLock);
2017
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002018 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2019 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002020 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002021 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002022 }
2023
2024 for (const auto &it : removedSurfaceIds) {
2025 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2026 CLOGE("Shared surface still part of a pending request!");
2027 return -EBUSY;
2028 }
2029 }
2030
Emilian Peev40ead602017-09-26 15:46:36 +01002031 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2032 if (res != OK) {
2033 CLOGE("Stream %d failed to update stream (error %d %s) ",
2034 streamId, res, strerror(-res));
2035 if (res == UNKNOWN_ERROR) {
2036 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2037 __FUNCTION__);
2038 }
2039 return res;
2040 }
2041
2042 return res;
2043}
2044
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002045status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2046 Mutex::Autolock il(mInterfaceLock);
2047 Mutex::Autolock l(mLock);
2048
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002049 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2050 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002051 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2052 return BAD_VALUE;
2053 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002054
2055 if (dropping) {
2056 mSessionStatsBuilder.stopCounter(streamId);
2057 } else {
2058 mSessionStatsBuilder.startCounter(streamId);
2059 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002060 return stream->dropBuffers(dropping);
2061}
2062
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002063/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 * Camera3Device private methods
2065 */
2066
2067sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002068 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002071 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002072 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073
2074 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002075 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 if (inputStreams.count > 0) {
2077 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002078 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002079 CLOGE("Request references unknown input stream %d",
2080 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002081 return NULL;
2082 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002083
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002084 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002085 SET_ERR_L("%s: input stream %d is not configured!",
2086 __FUNCTION__, mInputStream->getId());
2087 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002088 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002089 // Check if stream prepare is blocking requests.
2090 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002091 CLOGE("Request references an input stream that's being prepared!");
2092 return NULL;
2093 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002094
2095 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002096 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097 }
2098
2099 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002100 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002101 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002102 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002103 return NULL;
2104 }
2105
2106 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002107 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2108 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002109 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002110 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002111 return NULL;
2112 }
Zhijun He5d677d12016-05-29 16:52:39 -07002113 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002114 auto iter = surfaceMap.find(streams.data.i32[i]);
2115 if (iter != surfaceMap.end()) {
2116 const std::vector<size_t>& surfaces = iter->second;
2117 for (const auto& surface : surfaces) {
2118 if (stream->isConsumerConfigurationDeferred(surface)) {
2119 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2120 "due to deferred consumer", stream->getId(), surface);
2121 return NULL;
2122 }
2123 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002124 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002125 }
2126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002127 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002128 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2129 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002130 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002131 // Check if stream prepare is blocking requests.
2132 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002133 CLOGE("Request references an output stream that's being prepared!");
2134 return NULL;
2135 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002136
2137 newRequest->mOutputStreams.push(stream);
2138 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002139 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002140 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002142 auto rotateAndCropEntry =
2143 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2144 if (rotateAndCropEntry.count > 0 &&
2145 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2146 newRequest->mRotateAndCropAuto = true;
2147 } else {
2148 newRequest->mRotateAndCropAuto = false;
2149 }
2150
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002151 auto autoframingEntry =
2152 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2153 if (autoframingEntry.count > 0 &&
2154 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2155 newRequest->mAutoframingAuto = true;
2156 } else {
2157 newRequest->mAutoframingAuto = false;
2158 }
2159
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002160 auto zoomRatioEntry =
2161 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2162 if (zoomRatioEntry.count > 0 &&
2163 zoomRatioEntry.data.f[0] == 1.0f) {
2164 newRequest->mZoomRatioIs1x = true;
2165 } else {
2166 newRequest->mZoomRatioIs1x = false;
2167 }
2168
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002169 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002170 for (auto& settings : newRequest->mSettingsList) {
2171 auto testPatternModeEntry =
2172 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2173 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2174 testPatternModeEntry.data.i32[0] :
2175 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002176
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002177 auto testPatternDataEntry =
2178 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2179 if (testPatternDataEntry.count >= 4) {
2180 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2181 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2182 } else {
2183 settings.mOriginalTestPatternData[0] = 0;
2184 settings.mOriginalTestPatternData[1] = 0;
2185 settings.mOriginalTestPatternData[2] = 0;
2186 settings.mOriginalTestPatternData[3] = 0;
2187 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002188 }
2189 }
2190
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002191 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002192}
2193
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002194void Camera3Device::cancelStreamsConfigurationLocked() {
2195 int res = OK;
2196 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2197 res = mInputStream->cancelConfiguration();
2198 if (res != OK) {
2199 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2200 mInputStream->getId(), strerror(-res), res);
2201 }
2202 }
2203
2204 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002205 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002206 if (outputStream->isConfiguring()) {
2207 res = outputStream->cancelConfiguration();
2208 if (res != OK) {
2209 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2210 outputStream->getId(), strerror(-res), res);
2211 }
2212 }
2213 }
2214
2215 // Return state to that at start of call, so that future configures
2216 // properly clean things up
2217 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2218 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002219
2220 res = mPreparerThread->resume();
2221 if (res != OK) {
2222 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2223 }
2224}
2225
Emilian Peev0d0191e2020-04-21 17:01:18 -07002226bool Camera3Device::checkAbandonedStreamsLocked() {
2227 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2228 return true;
2229 }
2230
2231 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2232 auto stream = mOutputStreams[i];
2233 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2234 return true;
2235 }
2236 }
2237
2238 return false;
2239}
2240
Emilian Peev3bead5f2020-05-28 17:29:08 -07002241bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002242 ATRACE_CALL();
2243 bool ret = false;
2244
Shuzhen Wang316781a2020-08-18 18:11:01 -07002245 nsecs_t startTime = systemTime();
2246
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002247 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002248 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2249
2250 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002251 if (checkAbandonedStreamsLocked()) {
2252 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2253 __FUNCTION__);
2254 return true;
2255 }
2256
Emilian Peev3bead5f2020-05-28 17:29:08 -07002257 status_t rc = NO_ERROR;
2258 bool markClientActive = false;
2259 if (mStatus == STATUS_ACTIVE) {
2260 markClientActive = true;
2261 mPauseStateNotify = true;
Emilian Peev3bead5f2020-05-28 17:29:08 -07002262
2263 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2264 }
2265
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002266 if (rc == NO_ERROR) {
2267 mNeedConfig = true;
2268 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2269 if (rc == NO_ERROR) {
2270 ret = true;
2271 mPauseStateNotify = false;
2272 //Moving to active state while holding 'mLock' is important.
2273 //There could be pending calls to 'create-/deleteStream' which
2274 //will trigger another stream configuration while the already
2275 //present streams end up with outstanding buffers that will
2276 //not get drained.
2277 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002278 } else if (rc == DEAD_OBJECT) {
2279 // DEAD_OBJECT can be returned if either the consumer surface is
2280 // abandoned, or the HAL has died.
2281 // - If the HAL has died, configureStreamsLocked call will set
2282 // device to error state,
2283 // - If surface is abandoned, we should not set device to error
2284 // state.
2285 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002286 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002287 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002288 }
2289 } else {
2290 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2291 }
2292
Austin Borger74fca042022-05-23 12:41:21 -07002293 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002294 ns2ms(systemTime() - startTime));
2295
Emilian Peev3bead5f2020-05-28 17:29:08 -07002296 if (markClientActive) {
2297 mStatusTracker->markComponentActive(clientStatusId);
2298 }
2299
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002300 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002301}
2302
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002303status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002304 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002305 ATRACE_CALL();
2306 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002307
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002308 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002309 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002310 return INVALID_OPERATION;
2311 }
2312
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002313 if (operatingMode < 0) {
2314 CLOGE("Invalid operating mode: %d", operatingMode);
2315 return BAD_VALUE;
2316 }
2317
2318 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002319 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002320
2321 if (mOperatingMode != operatingMode) {
2322 mNeedConfig = true;
2323 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2324 mOperatingMode = operatingMode;
2325 }
2326
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002327 // Reset min expected duration when session is reconfigured.
2328 mMinExpectedDuration = 0;
2329
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002330 // In case called from configureStreams, abort queued input buffers not belonging to
2331 // any pending requests.
2332 if (mInputStream != NULL && notifyRequestThread) {
2333 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002334 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002335 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002336 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002337 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002338 if (res != OK) {
2339 // Exhausted acquiring all input buffers.
2340 break;
2341 }
2342
Emilian Peevf4816702020-04-03 15:44:51 -07002343 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002344 res = mInputStream->returnInputBuffer(inputBuffer);
2345 if (res != OK) {
2346 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2347 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2348 }
2349 }
2350 }
2351
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002352 if (!mNeedConfig) {
2353 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2354 return OK;
2355 }
2356
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002357 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002358 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002359 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2360 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002361 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002362 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002363 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002364 }
2365
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002366 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002367 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002369 mPreparerThread->pause();
2370
Emilian Peevf4816702020-04-03 15:44:51 -07002371 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002372 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002374 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002375
Emilian Peevf4816702020-04-03 15:44:51 -07002376 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002378 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002379
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002380
2381 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002382 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 inputStream = mInputStream->startConfiguration();
2384 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002385 CLOGE("Can't start input stream configuration");
2386 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002387 return INVALID_OPERATION;
2388 }
2389 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002390
2391 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002392 }
2393
Shuzhen Wang99080502021-03-07 21:08:20 -08002394 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002395 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002396 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002397
2398 // Don't configure bidi streams twice, nor add them twice to the list
2399 if (mOutputStreams[i].get() ==
2400 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2401
2402 config.num_streams--;
2403 continue;
2404 }
2405
Emilian Peevf4816702020-04-03 15:44:51 -07002406 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002407 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002408 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002409 CLOGE("Can't start output stream configuration");
2410 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002411 return INVALID_OPERATION;
2412 }
2413 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002414
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002415 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002416 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2417 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002418 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2419 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002420 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2421 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002422 } else if (outputStream->data_space ==
2423 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2424 bufferSizes[k] = outputStream->width * outputStream->height;
2425 } else {
2426 ALOGW("%s: Blob dataSpace %d not supported",
2427 __FUNCTION__, outputStream->data_space);
2428 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002429 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002430
2431 if (mOutputStreams[i]->isMultiResolution()) {
2432 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2433 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2434 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2435 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002436
2437 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2438 composerSurfacePresent = true;
2439 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002440 }
2441
2442 config.streams = streams.editArray();
2443
2444 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002445 // max_buffers, usage, and priv fields, as well as data_space and format
2446 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002447
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002448 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002449 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002450 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002451
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002452 if (res == BAD_VALUE) {
2453 // HAL rejected this set of streams as unsupported, clean up config
2454 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002455 CLOGE("Set of requested inputs/outputs not supported by HAL");
2456 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002457 return BAD_VALUE;
2458 } else if (res != OK) {
2459 // Some other kind of error from configure_streams - this is not
2460 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002461 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2462 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002463 return res;
2464 }
2465
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002466 // Finish all stream configuration immediately.
2467 // TODO: Try to relax this later back to lazy completion, which should be
2468 // faster
2469
Igor Murashkin073f8572013-05-02 14:59:28 -07002470 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002471 bool streamReConfigured = false;
2472 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002473 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002474 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002475 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002476 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002477 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2478 return DEAD_OBJECT;
2479 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002480 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002481 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002482 if (streamReConfigured) {
2483 mInterface->onStreamReConfigured(mInputStream->getId());
2484 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002485 }
2486
2487 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002488 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002489 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002490 bool streamReConfigured = false;
2491 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002492 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002493 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002494 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002495 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002496 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2497 return DEAD_OBJECT;
2498 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002499 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002500 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002501 if (streamReConfigured) {
2502 mInterface->onStreamReConfigured(outputStream->getId());
2503 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002504 }
2505 }
2506
Emilian Peeve23f1d92021-09-20 14:56:01 -07002507 mRequestThread->setComposerSurface(composerSurfacePresent);
2508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002509 // Request thread needs to know to avoid using repeat-last-settings protocol
2510 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002511 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002512 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2513 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002514 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002515
Zhijun He90f7c372016-08-16 16:19:43 -07002516 char value[PROPERTY_VALUE_MAX];
2517 property_get("camera.fifo.disable", value, "0");
2518 int32_t disableFifo = atoi(value);
2519 if (disableFifo != 1) {
2520 // Boost priority of request thread to SCHED_FIFO.
2521 pid_t requestThreadTid = mRequestThread->getTid();
2522 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002523 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002524 if (res != OK) {
2525 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2526 strerror(-res), res);
2527 } else {
2528 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2529 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002530 }
2531
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002532 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002533 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2534 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2535 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2536 sessionParams.unlock(newSessionParams);
2537 mSessionParams.unlock(currentSessionParams);
2538 if (updateSessionParams) {
2539 mSessionParams = sessionParams;
2540 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002541
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002542 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002543
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002544 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002545 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002546
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002547 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002548
Zhijun He0a210512014-07-24 13:45:15 -07002549 // tear down the deleted streams after configure streams.
2550 mDeletedStreams.clear();
2551
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002552 auto rc = mPreparerThread->resume();
2553 if (rc != OK) {
2554 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2555 return rc;
2556 }
2557
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002558 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002559 mRequestBufferSM.onStreamsConfigured();
2560 }
2561
Cliff Wu3b268182021-07-06 15:44:43 +08002562 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002563 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002564 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002565 // configure the injection streams.
2566 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002567 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002568 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2569 res = mInjectionMethods->injectCamera(config, bufferSizes);
2570 if (res != OK) {
2571 ALOGE("Can't finish inject camera process!");
2572 return res;
2573 }
Cliff Wu3b268182021-07-06 15:44:43 +08002574 } else {
2575 // First run configureStreamsLocked() and then call injectCamera() case:
2576 // If the stream configuration has been completed and camera deive is active, but the
2577 // injection camera has not been injected yet, we need to store the stream configuration of
2578 // the internal camera (because the stream configuration of the injection camera is based
2579 // on the internal camera). When injecting occurs later, this configuration can be used by
2580 // the injection camera.
2581 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2582 " injection camera has not been injected yet.", __FUNCTION__);
2583 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002584 }
2585
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002586 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002587}
2588
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002589status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002590 ATRACE_CALL();
2591 status_t res;
2592
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002593 if (mFakeStreamId != NO_STREAM) {
2594 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002595 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002596 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002597 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002598 return INVALID_OPERATION;
2599 }
2600
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002601 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002602
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002603 sp<Camera3OutputStreamInterface> fakeStream =
2604 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002605
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002606 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002607 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002608 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002609 return res;
2610 }
2611
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002612 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002613 mNextStreamId++;
2614
2615 return OK;
2616}
2617
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002618status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002619 ATRACE_CALL();
2620 status_t res;
2621
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002622 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002623 if (mOutputStreams.size() == 1) return OK;
2624
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002625 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002626
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002627 // Ok, have a fake stream and there's at least one other output stream,
2628 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002629
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002630 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002631 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002632 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002633 return INVALID_OPERATION;
2634 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002635 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002636
2637 // Free up the stream endpoint so that it can be used by some other stream
2638 res = deletedStream->disconnect();
2639 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002640 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002641 // fall through since we want to still list the stream as deleted.
2642 }
2643 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002644 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002645
2646 return res;
2647}
2648
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002649void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002650 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002651 Mutex::Autolock l(mLock);
2652 va_list args;
2653 va_start(args, fmt);
2654
2655 setErrorStateLockedV(fmt, args);
2656
2657 va_end(args);
2658}
2659
2660void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002661 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002662 Mutex::Autolock l(mLock);
2663 setErrorStateLockedV(fmt, args);
2664}
2665
2666void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2667 va_list args;
2668 va_start(args, fmt);
2669
2670 setErrorStateLockedV(fmt, args);
2671
2672 va_end(args);
2673}
2674
2675void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002676 // Print out all error messages to log
2677 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002678 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002679
2680 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002681 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002682
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002683 mErrorCause = errorCause;
2684
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002685 if (mRequestThread != nullptr) {
2686 mRequestThread->setPaused(true);
2687 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002688 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002689
2690 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002691 sp<NotificationListener> listener = mListener.promote();
2692 if (listener != NULL) {
2693 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002694 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002695 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002696 }
2697
2698 // Save stack trace. View by dumping it later.
2699 CameraTraces::saveTrace();
2700 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002701}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702
2703/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002704 * In-flight request management
2705 */
2706
Jianing Weicb0652e2014-03-12 18:29:36 -07002707status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002708 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002709 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002710 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002711 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002712 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002713 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002714 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002715 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002716
2717 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002718 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002719 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002720 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2721 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002722 if (res < 0) return res;
2723
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002724 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002725 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2726 // avoid a deadlock during reprocess requests.
2727 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002728 if (mStatusTracker != nullptr) {
2729 mStatusTracker->markComponentActive(mInFlightStatusId);
2730 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002731 }
2732
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002733 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002734 return OK;
2735}
2736
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002737void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002738 // Indicate idle inFlightMap to the status tracker
2739 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002740 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002741 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2742 // avoid a deadlock during reprocess requests.
2743 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002744 if (mStatusTracker != nullptr) {
2745 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2746 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002747 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002748 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002749}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002750
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002751void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002752 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002753 // something has likely gone wrong. This might still be legit only if application send in
2754 // a long burst of long exposure requests.
2755 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2756 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2757 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2758 mInFlightMap.size(), mExpectedInflightDuration);
2759 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2760 kInFlightWarnLimitHighSpeed) {
2761 CLOGW("In-flight list too large for high speed configuration: %zu,"
2762 "total inflight duration %" PRIu64,
2763 mInFlightMap.size(), mExpectedInflightDuration);
2764 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002765 }
2766}
2767
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002768void Camera3Device::onInflightMapFlushedLocked() {
2769 mExpectedInflightDuration = 0;
2770}
2771
2772void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002773 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002774 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2775 mInFlightMap.removeItemsAt(idx, 1);
2776
2777 onInflightEntryRemovedLocked(duration);
2778}
2779
2780
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002781void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002782 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002783 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002784 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002785 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002786 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002787 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002788
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002789 FlushInflightReqStates states {
2790 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002791 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002792
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002793 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002794}
2795
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002796CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002797 ALOGV("%s", __FUNCTION__);
2798
Igor Murashkin1e479c02013-09-06 16:55:14 -07002799 CameraMetadata retVal;
2800
2801 if (mRequestThread != NULL) {
2802 retVal = mRequestThread->getLatestRequest();
2803 }
2804
Igor Murashkin1e479c02013-09-06 16:55:14 -07002805 return retVal;
2806}
2807
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002808void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002809 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002810 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002811 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2812 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002813
2814 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002815 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002816}
2817
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002818void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002819 std::vector<native_handle_t*> *handles, bool closeFd) {
2820 if (handles == nullptr) {
2821 return;
2822 }
2823 if (closeFd) {
2824 for (auto& handle : *handles) {
2825 native_handle_close(handle);
2826 }
2827 }
2828 for (auto& handle : *handles) {
2829 native_handle_delete(handle);
2830 }
2831 handles->clear();
2832 return;
2833}
2834
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002835/**
2836 * HalInterface inner class methods
2837 */
2838
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002839void Camera3Device::HalInterface::getInflightBufferKeys(
2840 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002841 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002842 return;
2843}
2844
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002845void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2846 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002847 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002848 return;
2849}
2850
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002851bool Camera3Device::HalInterface::verifyBufferIds(
2852 int32_t streamId, std::vector<uint64_t>& bufIds) {
2853 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002854}
2855
2856status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002857 int32_t frameNumber, int32_t streamId,
2858 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002859 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002860}
2861
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002862status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002863 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002864 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002865}
2866
2867// Find and pop a buffer_handle_t based on bufferId
2868status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002869 uint64_t bufferId,
2870 /*out*/ buffer_handle_t** buffer,
2871 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002872 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002873}
2874
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002875std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2876 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002877 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002878}
2879
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002880uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2881 const native_handle_t* handle) {
2882 return mBufferRecords.removeOneBufferCache(streamId, handle);
2883}
2884
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002885void Camera3Device::HalInterface::onBufferFreed(
2886 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002887 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2888 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2889 if (bufferId != BUFFER_ID_NO_BUFFER) {
2890 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002891 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002892}
2893
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002894void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002895 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2896 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2897 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002898 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2899 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002900}
2901
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002902/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002903 * RequestThread inner class methods
2904 */
2905
2906Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002907 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002908 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002909 bool useHalBufManager,
Austin Borger18b30a72022-10-27 12:20:29 -07002910 bool supportCameraMute,
2911 bool overrideToPortrait) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002912 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002913 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002914 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002915 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002916 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002917 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002918 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002919 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002920 mReconfigured(false),
2921 mDoPause(false),
2922 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002923 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002924 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002925 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002926 mCurrentAfTriggerId(0),
2927 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002928 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002929 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_ON),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002930 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002931 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002932 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002933 mRepeatingLastFrameNumber(
2934 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002935 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002936 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002937 mRequestLatency(kRequestLatencyBinSize),
2938 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002939 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002940 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07002941 mSupportCameraMute(supportCameraMute),
2942 mOverrideToPortrait(overrideToPortrait) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002943 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002944}
2945
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002946Camera3Device::RequestThread::~RequestThread() {}
2947
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002948void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002949 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002950 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002951 Mutex::Autolock l(mRequestLock);
2952 mListener = listener;
2953}
2954
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002955void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002956 const CameraMetadata& sessionParams,
2957 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002958 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002959 Mutex::Autolock l(mRequestLock);
2960 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002961 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002962 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002963 // Prepare video stream for high speed recording.
2964 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002965 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002966}
2967
Jianing Wei90e59c92014-03-12 18:29:36 -07002968status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002969 List<sp<CaptureRequest> > &requests,
2970 /*out*/
2971 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002972 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002973 Mutex::Autolock l(mRequestLock);
2974 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2975 ++it) {
2976 mRequestQueue.push_back(*it);
2977 }
2978
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002979 if (lastFrameNumber != NULL) {
2980 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2981 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2982 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2983 *lastFrameNumber);
2984 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002985
Jianing Wei90e59c92014-03-12 18:29:36 -07002986 unpauseForNewRequests();
2987
2988 return OK;
2989}
2990
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002991
2992status_t Camera3Device::RequestThread::queueTrigger(
2993 RequestTrigger trigger[],
2994 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002995 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002996 Mutex::Autolock l(mTriggerMutex);
2997 status_t ret;
2998
2999 for (size_t i = 0; i < count; ++i) {
3000 ret = queueTriggerLocked(trigger[i]);
3001
3002 if (ret != OK) {
3003 return ret;
3004 }
3005 }
3006
3007 return OK;
3008}
3009
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003010const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3011 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003012 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003013 if (d != nullptr) return d->mId;
3014 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003015}
3016
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003017status_t Camera3Device::RequestThread::queueTriggerLocked(
3018 RequestTrigger trigger) {
3019
3020 uint32_t tag = trigger.metadataTag;
3021 ssize_t index = mTriggerMap.indexOfKey(tag);
3022
3023 switch (trigger.getTagType()) {
3024 case TYPE_BYTE:
3025 // fall-through
3026 case TYPE_INT32:
3027 break;
3028 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003029 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3030 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003031 return INVALID_OPERATION;
3032 }
3033
3034 /**
3035 * Collect only the latest trigger, since we only have 1 field
3036 * in the request settings per trigger tag, and can't send more than 1
3037 * trigger per request.
3038 */
3039 if (index != NAME_NOT_FOUND) {
3040 mTriggerMap.editValueAt(index) = trigger;
3041 } else {
3042 mTriggerMap.add(tag, trigger);
3043 }
3044
3045 return OK;
3046}
3047
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003048status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003049 const RequestList &requests,
3050 /*out*/
3051 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003052 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003053 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003054 if (lastFrameNumber != NULL) {
3055 *lastFrameNumber = mRepeatingLastFrameNumber;
3056 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003057 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003058 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003059 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3060 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003061
3062 unpauseForNewRequests();
3063
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003064 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003065 return OK;
3066}
3067
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003068bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003069 if (mRepeatingRequests.empty()) {
3070 return false;
3071 }
3072 int32_t requestId = requestIn->mResultExtras.requestId;
3073 const RequestList &repeatRequests = mRepeatingRequests;
3074 // All repeating requests are guaranteed to have same id so only check first quest
3075 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3076 return (firstRequest->mResultExtras.requestId == requestId);
3077}
3078
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003079status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003080 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003081 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003082 return clearRepeatingRequestsLocked(lastFrameNumber);
3083
3084}
3085
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003086status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3087 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003088 std::vector<int32_t> streamIds;
3089 for (const auto& request : mRepeatingRequests) {
3090 for (const auto& stream : request->mOutputStreams) {
3091 streamIds.push_back(stream->getId());
3092 }
3093 }
3094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003095 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003096 if (lastFrameNumber != NULL) {
3097 *lastFrameNumber = mRepeatingLastFrameNumber;
3098 }
Emilian Peev2295df72021-11-12 18:14:10 -08003099
3100 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3101
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003102 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003103 return OK;
3104}
3105
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003106status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003107 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003108 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003109 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003110 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003111
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003112 // Send errors for all requests pending in the request queue, including
3113 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003114 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003115 if (listener != NULL) {
3116 for (RequestList::iterator it = mRequestQueue.begin();
3117 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003118 // Abort the input buffers for reprocess requests.
3119 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003120 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003121 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003122 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003123 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003124 if (res != OK) {
3125 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3126 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3127 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003128 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003129 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3130 if (res != OK) {
3131 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3132 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3133 }
3134 }
3135 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003136 // Set the frame number this request would have had, if it
3137 // had been submitted; this frame number will not be reused.
3138 // The requestId and burstId fields were set when the request was
3139 // submitted originally (in convertMetadataListToRequestListLocked)
3140 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003141 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003142 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003143 }
3144 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003145 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003146
3147 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003148 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003149 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003150 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003151 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003152 return OK;
3153}
3154
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003155status_t Camera3Device::RequestThread::flush() {
3156 ATRACE_CALL();
3157 Mutex::Autolock l(mFlushLock);
3158
Emilian Peev08dd2452017-04-06 16:55:14 +01003159 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003160}
3161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003162void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003163 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003164 Mutex::Autolock l(mPauseLock);
3165 mDoPause = paused;
3166 mDoPauseSignal.signal();
3167}
3168
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003169status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3170 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003171 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003172 Mutex::Autolock l(mLatestRequestMutex);
3173 status_t res;
3174 while (mLatestRequestId != requestId) {
3175 nsecs_t startTime = systemTime();
3176
3177 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3178 if (res != OK) return res;
3179
3180 timeout -= (systemTime() - startTime);
3181 }
3182
3183 return OK;
3184}
3185
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003186void Camera3Device::RequestThread::requestExit() {
3187 // Call parent to set up shutdown
3188 Thread::requestExit();
3189 // The exit from any possible waits
3190 mDoPauseSignal.signal();
3191 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003192
3193 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3194 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003195}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003196
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003197void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003198 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003199 bool surfaceAbandoned = false;
3200 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003201 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003202 {
3203 Mutex::Autolock l(mRequestLock);
3204 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3205 // repeating requests.
3206 for (const auto& request : mRepeatingRequests) {
3207 for (const auto& s : request->mOutputStreams) {
3208 if (s->isAbandoned()) {
3209 surfaceAbandoned = true;
3210 clearRepeatingRequestsLocked(&lastFrameNumber);
3211 break;
3212 }
3213 }
3214 if (surfaceAbandoned) {
3215 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003216 }
3217 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003218 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003219 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003220
3221 if (listener != NULL && surfaceAbandoned) {
3222 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003223 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003224}
3225
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003226bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003227 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003228 status_t res;
3229 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003230 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003231 uint32_t numRequestProcessed = 0;
3232 for (size_t i = 0; i < batchSize; i++) {
3233 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003234 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003235 }
3236
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003237 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3238
3239 bool triggerRemoveFailed = false;
3240 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3241 for (size_t i = 0; i < numRequestProcessed; i++) {
3242 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3243 nextRequest.submitted = true;
3244
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003245 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003246
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003247 if (!triggerRemoveFailed) {
3248 // Remove any previously queued triggers (after unlock)
3249 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3250 if (removeTriggerRes != OK) {
3251 triggerRemoveFailed = true;
3252 triggerFailedRequest = nextRequest;
3253 }
3254 }
3255 }
3256
3257 if (triggerRemoveFailed) {
3258 SET_ERR("RequestThread: Unable to remove triggers "
3259 "(capture request %d, HAL device: %s (%d)",
3260 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3261 cleanUpFailedRequests(/*sendRequestError*/ false);
3262 return false;
3263 }
3264
3265 if (res != OK) {
3266 // Should only get a failure here for malformed requests or device-level
3267 // errors, so consider all errors fatal. Bad metadata failures should
3268 // come through notify.
3269 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3270 mNextRequests[numRequestProcessed].halRequest.frame_number,
3271 strerror(-res), res);
3272 cleanUpFailedRequests(/*sendRequestError*/ false);
3273 return false;
3274 }
3275 return true;
3276}
3277
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003278Camera3Device::RequestThread::ExpectedDurationInfo
3279 Camera3Device::RequestThread::calculateExpectedDurationRange(
3280 const camera_metadata_t *request) {
3281 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003282 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003283 InFlightRequest::kDefaultMaxExpectedDuration,
3284 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003285 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3286 find_camera_metadata_ro_entry(request,
3287 ANDROID_CONTROL_AE_MODE,
3288 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003289 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003290
3291 switch (e.data.u8[0]) {
3292 case ANDROID_CONTROL_AE_MODE_OFF:
3293 find_camera_metadata_ro_entry(request,
3294 ANDROID_SENSOR_EXPOSURE_TIME,
3295 &e);
3296 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003297 expectedDurationInfo.minDuration = e.data.i64[0];
3298 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003299 }
3300 find_camera_metadata_ro_entry(request,
3301 ANDROID_SENSOR_FRAME_DURATION,
3302 &e);
3303 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003304 expectedDurationInfo.minDuration =
3305 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3306 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003307 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003308 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003309 break;
3310 default:
3311 find_camera_metadata_ro_entry(request,
3312 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3313 &e);
3314 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003315 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3316 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003317 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003318 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003319 break;
3320 }
3321
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003322 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003323}
3324
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003325bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3326 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3327 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3328 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3329 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3330 return true;
3331 }
3332
3333 return false;
3334}
3335
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003336void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3337 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003338 camera_capture_request_t& halRequest = nextRequest.halRequest;
3339 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003340 Mutex::Autolock al(mLatestRequestMutex);
3341
Shuzhen Wang83bff122020-11-20 15:51:39 -08003342 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003343 mLatestRequest.acquire(cloned);
3344
3345 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003346 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3347 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3348 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003349 CameraMetadata(cloned));
3350 }
3351
3352 sp<Camera3Device> parent = mParent.promote();
3353 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003354 int32_t inputStreamId = -1;
3355 if (halRequest.input_buffer != nullptr) {
3356 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3357 }
3358
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003359 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003360 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003361 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3362 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003363 }
3364 }
3365
Shuzhen Wang83bff122020-11-20 15:51:39 -08003366 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003367 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003368 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003369 }
3370
Shuzhen Wang83bff122020-11-20 15:51:39 -08003371 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003372}
3373
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003374bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3375 ATRACE_CALL();
3376 bool updatesDetected = false;
3377
Emilian Peev4ec17882019-01-24 17:16:58 -08003378 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003379 for (auto tag : mSessionParamKeys) {
3380 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003381 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003382
3383 if (entry.count > 0) {
3384 bool isDifferent = false;
3385 if (lastEntry.count > 0) {
3386 // Have a last value, compare to see if changed
3387 if (lastEntry.type == entry.type &&
3388 lastEntry.count == entry.count) {
3389 // Same type and count, compare values
3390 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3391 size_t entryBytes = bytesPerValue * lastEntry.count;
3392 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3393 if (cmp != 0) {
3394 isDifferent = true;
3395 }
3396 } else {
3397 // Count or type has changed
3398 isDifferent = true;
3399 }
3400 } else {
3401 // No last entry, so always consider to be different
3402 isDifferent = true;
3403 }
3404
3405 if (isDifferent) {
3406 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003407 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3408 updatesDetected = true;
3409 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003410 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003411 }
3412 } else if (lastEntry.count > 0) {
3413 // Value has been removed
3414 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003415 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003416 updatesDetected = true;
3417 }
3418 }
3419
Emilian Peev4ec17882019-01-24 17:16:58 -08003420 bool reconfigureRequired;
3421 if (updatesDetected) {
3422 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3423 updatedParams);
3424 mLatestSessionParams = updatedParams;
3425 } else {
3426 reconfigureRequired = false;
3427 }
3428
3429 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003430}
3431
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003432bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003433 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003434 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003435 // Any function called from threadLoop() must not hold mInterfaceLock since
3436 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3437 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003438
3439 // Handle paused state.
3440 if (waitIfPaused()) {
3441 return true;
3442 }
3443
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003444 // Wait for the next batch of requests.
3445 waitForNextRequestBatch();
3446 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003447 return true;
3448 }
3449
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003450 // Get the latest request ID, if any
3451 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003452 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003453 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003454 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003455 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003456 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003457 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3458 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003459 }
3460
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003461 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3462 // or a single request from streaming or burst. In either case the first element
3463 // should contain the latest camera settings that we need to check for any session
3464 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003465 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003466 res = OK;
3467
3468 //Input stream buffers are already acquired at this point so an input stream
3469 //will not be able to move to idle state unless we force it.
3470 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3471 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3472 if (res != OK) {
3473 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3474 cleanUpFailedRequests(/*sendRequestError*/ false);
3475 return false;
3476 }
3477 }
3478
3479 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003480 sp<Camera3Device> parent = mParent.promote();
3481 if (parent != nullptr) {
Bharatt Kukrejad55c7a52022-08-16 00:48:40 +00003482 sp<StatusTracker> statusTracker = mStatusTracker.promote();
3483 if (statusTracker != nullptr) {
3484 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
3485 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003486 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003487 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003488 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003489
3490 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3491 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3492 if (res != OK) {
3493 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3494 cleanUpFailedRequests(/*sendRequestError*/ false);
3495 return false;
3496 }
3497 }
3498 }
3499 }
3500
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003501 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003502 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003503 if (res == TIMED_OUT) {
3504 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003505 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003506 // Check if any stream is abandoned.
3507 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003508 return true;
3509 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003510 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003511 return false;
3512 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003513
Zhijun Hecc27e112013-10-03 16:12:43 -07003514 // Inform waitUntilRequestProcessed thread of a new request ID
3515 {
3516 Mutex::Autolock al(mLatestRequestMutex);
3517
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003518 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003519 mLatestRequestSignal.signal();
3520 }
3521
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003522 // Submit a batch of requests to HAL.
3523 // Use flush lock only when submitting multilple requests in a batch.
3524 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3525 // which may take a long time to finish so synchronizing flush() and
3526 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3527 // For now, only synchronize for high speed recording and we should figure something out for
3528 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003529 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003530
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003531 if (useFlushLock) {
3532 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003533 }
3534
Zhijun Hef0645c12016-08-02 00:58:11 -07003535 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003536 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003537
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003538 sp<Camera3Device> parent = mParent.promote();
3539 if (parent != nullptr) {
3540 parent->mRequestBufferSM.onSubmittingRequest();
3541 }
3542
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003543 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003544 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003545 submitRequestSuccess = sendRequestsBatch();
3546
Shuzhen Wang686f6442017-06-20 16:16:04 -07003547 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3548 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003549
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003550 if (useFlushLock) {
3551 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003552 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003553
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003554 // Unset as current request
3555 {
3556 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003557 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003558 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003559 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003560
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003561 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003562}
3563
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003564status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3565 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3566 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3567 ANDROID_SCALER_CROP_REGION_SET};
3568 if (request == nullptr) {
3569 ALOGE("%s request metadata nullptr", __FUNCTION__);
3570 return BAD_VALUE;
3571 }
3572 status_t res = OK;
3573 for (const auto &key : kFwkOnlyRegionKeys) {
3574 if (request->exists(key)) {
3575 res = request->erase(key);
3576 if (res != OK) {
3577 return res;
3578 }
3579 }
3580 }
3581 return OK;
3582}
3583
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003584status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003585 ATRACE_CALL();
3586
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003587 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003588 for (size_t i = 0; i < mNextRequests.size(); i++) {
3589 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003590 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003591 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3592 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003593
3594 // Prepare a request to HAL
3595 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3596
3597 // Insert any queued triggers (before metadata is locked)
3598 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003599 if (res < 0) {
3600 SET_ERR("RequestThread: Unable to insert triggers "
3601 "(capture request %d, HAL device: %s (%d)",
3602 halRequest->frame_number, strerror(-res), res);
3603 return INVALID_OPERATION;
3604 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003605
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003606 int triggerCount = res;
3607 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3608 mPrevTriggers = triggerCount;
3609
Emilian Peeve23f1d92021-09-20 14:56:01 -07003610 // Do not override rotate&crop for stream configurations that include
Austin Borger18b30a72022-10-27 12:20:29 -07003611 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3612 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3613 bool rotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
Emilian Peeve23f1d92021-09-20 14:56:01 -07003614 overrideAutoRotateAndCrop(captureRequest);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003615 bool autoframingChanged = overrideAutoframing(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003616 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003617
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003618 // If the request is the same as last, or we had triggers now or last time or
3619 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003620 bool newRequest =
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003621 (mPrevRequest != captureRequest || triggersMixedIn || rotateAndCropChanged ||
3622 autoframingChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003623 // Request settings are all the same within one batch, so only treat the first
3624 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003625 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003626 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003627 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003628 /**
3629 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003630 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003631 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003632 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003633 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003634 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003635 "(capture request %d, HAL device: %s (%d)",
3636 halRequest->frame_number, strerror(-res), res);
3637 return INVALID_OPERATION;
3638 }
3639
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003640 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003641 sp<Camera3Device> parent = mParent.promote();
3642 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003643 List<PhysicalCameraSettings>::iterator it;
3644 for (it = captureRequest->mSettingsList.begin();
3645 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003646 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3647 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003648 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3649 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3650 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3651 res);
3652 return INVALID_OPERATION;
3653 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003654 continue;
3655 }
3656
3657 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3658 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3659 updateCaptureRequest(&(it->metadata));
3660 if (res != OK) {
3661 SET_ERR("RequestThread: Unable to correct capture requests "
3662 "for scaler crop region and metering regions for request "
3663 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3664 res);
3665 return INVALID_OPERATION;
3666 }
3667 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003668 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3669 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3670 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3671 res);
3672 return INVALID_OPERATION;
3673 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003674 }
3675 }
3676
3677 // Correct metadata regions for distortion correction if enabled
3678 for (it = captureRequest->mSettingsList.begin();
3679 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003680 if (parent->mDistortionMappers.find(it->cameraId) ==
3681 parent->mDistortionMappers.end()) {
3682 continue;
3683 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003684
3685 if (!captureRequest->mDistortionCorrectionUpdated) {
3686 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3687 &(it->metadata));
3688 if (res != OK) {
3689 SET_ERR("RequestThread: Unable to correct capture requests "
3690 "for lens distortion for request %d: %s (%d)",
3691 halRequest->frame_number, strerror(-res), res);
3692 return INVALID_OPERATION;
3693 }
3694 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003695 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003696 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003697
3698 for (it = captureRequest->mSettingsList.begin();
3699 it != captureRequest->mSettingsList.end(); it++) {
3700 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3701 parent->mZoomRatioMappers.end()) {
3702 continue;
3703 }
3704
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003705 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003706 cameraIdsWithZoom.insert(it->cameraId);
3707 }
3708
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003709 if (!captureRequest->mZoomRatioUpdated) {
3710 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3711 &(it->metadata));
3712 if (res != OK) {
3713 SET_ERR("RequestThread: Unable to correct capture requests "
3714 "for zoom ratio for request %d: %s (%d)",
3715 halRequest->frame_number, strerror(-res), res);
3716 return INVALID_OPERATION;
3717 }
3718 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003719 }
3720 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003721 if (captureRequest->mRotateAndCropAuto &&
3722 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003723 for (it = captureRequest->mSettingsList.begin();
3724 it != captureRequest->mSettingsList.end(); it++) {
3725 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3726 if (mapper != parent->mRotateAndCropMappers.end()) {
3727 res = mapper->second.updateCaptureRequest(&(it->metadata));
3728 if (res != OK) {
3729 SET_ERR("RequestThread: Unable to correct capture requests "
3730 "for rotate-and-crop for request %d: %s (%d)",
3731 halRequest->frame_number, strerror(-res), res);
3732 return INVALID_OPERATION;
3733 }
3734 }
3735 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003736 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003737 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003738 }
3739 }
3740
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003741 /**
3742 * The request should be presorted so accesses in HAL
3743 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3744 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003745 captureRequest->mSettingsList.begin()->metadata.sort();
3746 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003747 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003748 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003749 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3750
3751 IF_ALOGV() {
3752 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3753 find_camera_metadata_ro_entry(
3754 halRequest->settings,
3755 ANDROID_CONTROL_AF_TRIGGER,
3756 &e
3757 );
3758 if (e.count > 0) {
3759 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3760 __FUNCTION__,
3761 halRequest->frame_number,
3762 e.data.u8[0]);
3763 }
3764 }
3765 } else {
3766 // leave request.settings NULL to indicate 'reuse latest given'
3767 ALOGVV("%s: Request settings are REUSED",
3768 __FUNCTION__);
3769 }
3770
Emilian Peevaebbe412018-01-15 13:53:24 +00003771 if (captureRequest->mSettingsList.size() > 1) {
3772 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3773 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003774 if (newRequest) {
3775 halRequest->physcam_settings =
3776 new const camera_metadata* [halRequest->num_physcam_settings];
3777 } else {
3778 halRequest->physcam_settings = nullptr;
3779 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003780 auto it = ++captureRequest->mSettingsList.begin();
3781 size_t i = 0;
3782 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3783 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003784 if (newRequest) {
3785 it->metadata.sort();
3786 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3787 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003788 }
3789 }
3790
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003791 uint32_t totalNumBuffers = 0;
3792
3793 // Fill in buffers
3794 if (captureRequest->mInputStream != NULL) {
3795 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003796
3797 halRequest->input_width = captureRequest->mInputBufferSize.width;
3798 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003799 totalNumBuffers += 1;
3800 } else {
3801 halRequest->input_buffer = NULL;
3802 }
3803
Emilian Peevf4816702020-04-03 15:44:51 -07003804 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003805 captureRequest->mOutputStreams.size());
3806 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003807 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003808
3809 sp<Camera3Device> parent = mParent.promote();
3810 if (parent == NULL) {
3811 // Should not happen, and nowhere to send errors to, so just log it
3812 CLOGE("RequestThread: Parent is gone");
3813 return INVALID_OPERATION;
3814 }
3815 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3816
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003817 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003818 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003819 sp<Camera3OutputStreamInterface> outputStream =
3820 captureRequest->mOutputStreams.editItemAt(j);
3821 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003822
3823 // Prepare video buffers for high speed recording on the first video request.
3824 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3825 // Only try to prepare video stream on the first video request.
3826 mPrepareVideoStream = false;
3827
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003828 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3829 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003830 while (res == NOT_ENOUGH_DATA) {
3831 res = outputStream->prepareNextBuffer();
3832 }
3833 if (res != OK) {
3834 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3835 __FUNCTION__, strerror(-res), res);
3836 outputStream->cancelPrepare();
3837 }
3838 }
3839
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003840 std::vector<size_t> uniqueSurfaceIds;
3841 res = outputStream->getUniqueSurfaceIds(
3842 captureRequest->mOutputSurfaces[streamId],
3843 &uniqueSurfaceIds);
3844 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3845 if (res != OK && res != INVALID_OPERATION) {
3846 ALOGE("%s: failed to query stream %d unique surface IDs",
3847 __FUNCTION__, streamId);
3848 return res;
3849 }
3850 if (res == OK) {
3851 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3852 }
3853
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003854 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003855 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003856 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003857 return TIMED_OUT;
3858 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003859 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003860 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003861 buffer.stream = outputStream->asHalStream();
3862 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003863 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003864 buffer.acquire_fence = -1;
3865 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003866 // Mark the output stream as unpreparable to block clients from calling
3867 // 'prepare' after this request reaches CameraHal and before the respective
3868 // buffers are requested.
3869 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003870 } else {
3871 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3872 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003873 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003874 if (res != OK) {
3875 // Can't get output buffer from gralloc queue - this could be due to
3876 // abandoned queue or other consumer misbehavior, so not a fatal
3877 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003878 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003879 " %s (%d)", strerror(-res), res);
3880
3881 return TIMED_OUT;
3882 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003883 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003884
3885 {
3886 sp<Camera3Device> parent = mParent.promote();
3887 if (parent != nullptr) {
3888 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3889 for (const auto& settings : captureRequest->mSettingsList) {
3890 if ((streamCameraId.isEmpty() &&
3891 parent->getId() == settings.cameraId.c_str()) ||
3892 streamCameraId == settings.cameraId.c_str()) {
3893 outputStream->fireBufferRequestForFrameNumber(
3894 captureRequest->mResultExtras.frameNumber,
3895 settings.metadata);
3896 }
3897 }
3898 }
3899 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003900
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003901 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003902 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3903 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3904 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3905 } else if (!physicalCameraId.isEmpty()) {
3906 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003907 }
3908 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003909 }
3910 totalNumBuffers += halRequest->num_output_buffers;
3911
3912 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003913 // If this request list is for constrained high speed recording (not
3914 // preview), and the current request is not the last one in the batch,
3915 // do not send callback to the app.
3916 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003917 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003918 hasCallback = false;
3919 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003920 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003921 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003922 const camera_metadata_t* settings = halRequest->settings;
3923 bool shouldUnlockSettings = false;
3924 if (settings == nullptr) {
3925 shouldUnlockSettings = true;
3926 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3927 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003928 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3929 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003930 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003931 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3932 isStillCapture = true;
3933 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3934 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003935
Emilian Peevaf8416e2021-02-04 17:52:43 -08003936 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003937 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003938 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3939 isZslCapture = true;
3940 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003941 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003942 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003943 res = parent->registerInFlight(halRequest->frame_number,
3944 totalNumBuffers, captureRequest->mResultExtras,
3945 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003946 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003947 expectedDurationInfo.minDuration,
3948 expectedDurationInfo.maxDuration,
3949 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003950 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00003951 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
3952 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003953 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003954 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003955 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3956 ", burstId = %" PRId32 ".",
3957 __FUNCTION__,
3958 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3959 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003960
3961 if (shouldUnlockSettings) {
3962 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3963 }
3964
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003965 if (res != OK) {
3966 SET_ERR("RequestThread: Unable to register new in-flight request:"
3967 " %s (%d)", strerror(-res), res);
3968 return INVALID_OPERATION;
3969 }
3970 }
3971
3972 return OK;
3973}
3974
Igor Murashkin1e479c02013-09-06 16:55:14 -07003975CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003976 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003977 Mutex::Autolock al(mLatestRequestMutex);
3978
3979 ALOGV("RequestThread::%s", __FUNCTION__);
3980
3981 return mLatestRequest;
3982}
3983
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003984bool Camera3Device::RequestThread::isStreamPending(
3985 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003986 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003987 Mutex::Autolock l(mRequestLock);
3988
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003989 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003990 if (!nextRequest.submitted) {
3991 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3992 if (stream == s) return true;
3993 }
3994 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003995 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003996 }
3997
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003998 for (const auto& request : mRequestQueue) {
3999 for (const auto& s : request->mOutputStreams) {
4000 if (stream == s) return true;
4001 }
4002 if (stream == request->mInputStream) return true;
4003 }
4004
4005 for (const auto& request : mRepeatingRequests) {
4006 for (const auto& s : request->mOutputStreams) {
4007 if (stream == s) return true;
4008 }
4009 if (stream == request->mInputStream) return true;
4010 }
4011
4012 return false;
4013}
Jianing Weicb0652e2014-03-12 18:29:36 -07004014
Emilian Peev40ead602017-09-26 15:46:36 +01004015bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4016 ATRACE_CALL();
4017 Mutex::Autolock l(mRequestLock);
4018
4019 for (const auto& nextRequest : mNextRequests) {
4020 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4021 if (s.first == streamId) {
4022 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4023 if (it != s.second.end()) {
4024 return true;
4025 }
4026 }
4027 }
4028 }
4029
4030 for (const auto& request : mRequestQueue) {
4031 for (const auto& s : request->mOutputSurfaces) {
4032 if (s.first == streamId) {
4033 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4034 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004035 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004036 }
4037 }
4038 }
4039 }
4040
4041 for (const auto& request : mRepeatingRequests) {
4042 for (const auto& s : request->mOutputSurfaces) {
4043 if (s.first == streamId) {
4044 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4045 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004046 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004047 }
4048 }
4049 }
4050 }
4051
4052 return false;
4053}
4054
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004055void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4056 if (!mUseHalBufManager) {
4057 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4058 return;
4059 }
4060
4061 Mutex::Autolock pl(mPauseLock);
4062 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004063 mInterface->signalPipelineDrain(streamIds);
4064 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004065 }
4066 // If request thread is still busy, wait until paused then notify HAL
4067 mNotifyPipelineDrain = true;
4068 mStreamIdsToBeDrained = streamIds;
4069}
4070
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004071void Camera3Device::RequestThread::resetPipelineDrain() {
4072 Mutex::Autolock pl(mPauseLock);
4073 mNotifyPipelineDrain = false;
4074 mStreamIdsToBeDrained.clear();
4075}
4076
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004077void Camera3Device::RequestThread::clearPreviousRequest() {
4078 Mutex::Autolock l(mRequestLock);
4079 mPrevRequest.clear();
4080}
4081
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004082status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4083 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4084 ATRACE_CALL();
4085 Mutex::Autolock l(mTriggerMutex);
4086 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4087 return BAD_VALUE;
4088 }
4089 mRotateAndCropOverride = rotateAndCropValue;
4090 return OK;
4091}
4092
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004093status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4094 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4095 ATRACE_CALL();
4096 Mutex::Autolock l(mTriggerMutex);
4097 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
4098 return BAD_VALUE;
4099 }
4100 mAutoframingOverride = autoframingValue;
4101 return OK;
4102}
4103
Emilian Peeve23f1d92021-09-20 14:56:01 -07004104status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4105 ATRACE_CALL();
4106 Mutex::Autolock l(mTriggerMutex);
4107 mComposerOutput = composerSurfacePresent;
4108 return OK;
4109}
4110
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004111status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004112 ATRACE_CALL();
4113 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004114 if (muteMode != mCameraMute) {
4115 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004116 mCameraMuteChanged = true;
4117 }
4118 return OK;
4119}
4120
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004121nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004122 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004123 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004124 return mExpectedInflightDuration > kMinInflightDuration ?
4125 mExpectedInflightDuration : kMinInflightDuration;
4126}
4127
Emilian Peevaebbe412018-01-15 13:53:24 +00004128void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004129 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004130 if ((request == nullptr) || (halRequest == nullptr)) {
4131 ALOGE("%s: Invalid request!", __FUNCTION__);
4132 return;
4133 }
4134
4135 if (halRequest->num_physcam_settings > 0) {
4136 if (halRequest->physcam_id != nullptr) {
4137 delete [] halRequest->physcam_id;
4138 halRequest->physcam_id = nullptr;
4139 }
4140 if (halRequest->physcam_settings != nullptr) {
4141 auto it = ++(request->mSettingsList.begin());
4142 size_t i = 0;
4143 for (; it != request->mSettingsList.end(); it++, i++) {
4144 it->metadata.unlock(halRequest->physcam_settings[i]);
4145 }
4146 delete [] halRequest->physcam_settings;
4147 halRequest->physcam_settings = nullptr;
4148 }
4149 }
4150}
4151
Ravneetaeb20dc2022-03-30 05:33:03 +00004152status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4153 Mutex::Autolock il(mInterfaceLock);
4154 Mutex::Autolock l(mLock);
4155
4156 if (mCameraServiceWatchdog != NULL) {
4157 mCameraServiceWatchdog->setEnabled(enabled);
4158 }
4159
4160 return OK;
4161}
4162
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004163void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4164 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004165 return;
4166 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004167
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004168 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004169 // Skip the ones that have been submitted successfully.
4170 if (nextRequest.submitted) {
4171 continue;
4172 }
4173
4174 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004175 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4176 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004177
4178 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004179 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004180 }
4181
Emilian Peevaebbe412018-01-15 13:53:24 +00004182 cleanupPhysicalSettings(captureRequest, halRequest);
4183
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004184 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004185 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004186 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4187 }
4188
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004189 // No output buffer can be returned when using HAL buffer manager
4190 if (!mUseHalBufManager) {
4191 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4192 //Buffers that failed processing could still have
4193 //valid acquire fence.
4194 int acquireFence = (*outputBuffers)[i].acquire_fence;
4195 if (0 <= acquireFence) {
4196 close(acquireFence);
4197 outputBuffers->editItemAt(i).acquire_fence = -1;
4198 }
Emilian Peevf4816702020-04-03 15:44:51 -07004199 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004200 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4201 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004202 /*timestampIncreasing*/true, std::vector<size_t> (),
4203 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004204 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004205 }
4206
4207 if (sendRequestError) {
4208 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004209 sp<NotificationListener> listener = mListener.promote();
4210 if (listener != NULL) {
4211 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004212 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004213 captureRequest->mResultExtras);
4214 }
4215 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004216
4217 // Remove yet-to-be submitted inflight request from inflightMap
4218 {
4219 sp<Camera3Device> parent = mParent.promote();
4220 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004221 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004222 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4223 if (idx >= 0) {
4224 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4225 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4226 parent->removeInFlightMapEntryLocked(idx);
4227 }
4228 }
4229 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004230 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004231
4232 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004233 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004234}
4235
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004236void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004237 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004238 // Optimized a bit for the simple steady-state case (single repeating
4239 // request), to avoid putting that request in the queue temporarily.
4240 Mutex::Autolock l(mRequestLock);
4241
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004242 assert(mNextRequests.empty());
4243
4244 NextRequest nextRequest;
4245 nextRequest.captureRequest = waitForNextRequestLocked();
4246 if (nextRequest.captureRequest == nullptr) {
4247 return;
4248 }
4249
Emilian Peevf4816702020-04-03 15:44:51 -07004250 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004251 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004252 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004253
4254 // Wait for additional requests
4255 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4256
4257 for (size_t i = 1; i < batchSize; i++) {
4258 NextRequest additionalRequest;
4259 additionalRequest.captureRequest = waitForNextRequestLocked();
4260 if (additionalRequest.captureRequest == nullptr) {
4261 break;
4262 }
4263
Emilian Peevf4816702020-04-03 15:44:51 -07004264 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004265 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004266 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004267 }
4268
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004269 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004270 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004271 mNextRequests.size(), batchSize);
4272 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004273 }
4274
4275 return;
4276}
4277
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004278void Camera3Device::RequestThread::setRequestClearing() {
4279 Mutex::Autolock l(mRequestLock);
4280 mRequestClearing = true;
4281}
4282
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004283sp<Camera3Device::CaptureRequest>
4284 Camera3Device::RequestThread::waitForNextRequestLocked() {
4285 status_t res;
4286 sp<CaptureRequest> nextRequest;
4287
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004288 while (mRequestQueue.empty()) {
4289 if (!mRepeatingRequests.empty()) {
4290 // Always atomically enqueue all requests in a repeating request
4291 // list. Guarantees a complete in-sequence set of captures to
4292 // application.
4293 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004294 if (mFirstRepeating) {
4295 mFirstRepeating = false;
4296 } else {
4297 for (auto& request : requests) {
4298 // For repeating requests, override timestamp request using
4299 // the time a request is inserted into the request queue,
4300 // because the original repeating request will have an old
4301 // fixed timestamp.
4302 request->mRequestTimeNs = systemTime();
4303 }
4304 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004305 RequestList::const_iterator firstRequest =
4306 requests.begin();
4307 nextRequest = *firstRequest;
4308 mRequestQueue.insert(mRequestQueue.end(),
4309 ++firstRequest,
4310 requests.end());
4311 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004312
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004313 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004315 break;
4316 }
4317
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004318 if (!mRequestClearing) {
4319 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004321
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004322 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4323 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004324 Mutex::Autolock pl(mPauseLock);
4325 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004326 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004327 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004328 if (mNotifyPipelineDrain) {
4329 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4330 mNotifyPipelineDrain = false;
4331 mStreamIdsToBeDrained.clear();
4332 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004333 // Let the tracker know
4334 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4335 if (statusTracker != 0) {
4336 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4337 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004338 sp<Camera3Device> parent = mParent.promote();
4339 if (parent != nullptr) {
4340 parent->mRequestBufferSM.onRequestThreadPaused();
4341 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004342 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004343 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004344 // Stop waiting for now and let thread management happen
4345 return NULL;
4346 }
4347 }
4348
4349 if (nextRequest == NULL) {
4350 // Don't have a repeating request already in hand, so queue
4351 // must have an entry now.
4352 RequestList::iterator firstRequest =
4353 mRequestQueue.begin();
4354 nextRequest = *firstRequest;
4355 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004356 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4357 sp<NotificationListener> listener = mListener.promote();
4358 if (listener != NULL) {
4359 listener->notifyRequestQueueEmpty();
4360 }
4361 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004362 }
4363
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004364 // In case we've been unpaused by setPaused clearing mDoPause, need to
4365 // update internal pause state (capture/setRepeatingRequest unpause
4366 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004367 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004368 if (mPaused) {
4369 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4370 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4371 if (statusTracker != 0) {
4372 statusTracker->markComponentActive(mStatusId);
4373 }
4374 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004375 mPaused = false;
4376
4377 // Check if we've reconfigured since last time, and reset the preview
4378 // request if so. Can't use 'NULL request == repeat' across configure calls.
4379 if (mReconfigured) {
4380 mPrevRequest.clear();
4381 mReconfigured = false;
4382 }
4383
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004384 if (nextRequest != NULL) {
4385 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004386 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4387 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004388
4389 // Since RequestThread::clear() removes buffers from the input stream,
4390 // get the right buffer here before unlocking mRequestLock
4391 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004392 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4393 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004394 if (res != OK) {
4395 // Can't get input buffer from gralloc queue - this could be due to
4396 // disconnected queue or other producer misbehavior, so not a fatal
4397 // error
4398 ALOGE("%s: Can't get input buffer, skipping request:"
4399 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004400
4401 sp<NotificationListener> listener = mListener.promote();
4402 if (listener != NULL) {
4403 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004404 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004405 nextRequest->mResultExtras);
4406 }
4407 return NULL;
4408 }
4409 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004410 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004411
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004412 return nextRequest;
4413}
4414
4415bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004416 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004417 status_t res;
4418 Mutex::Autolock l(mPauseLock);
4419 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004420 if (mPaused == false) {
4421 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004422 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004423 if (mNotifyPipelineDrain) {
4424 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4425 mNotifyPipelineDrain = false;
4426 mStreamIdsToBeDrained.clear();
4427 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004428 // Let the tracker know
4429 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4430 if (statusTracker != 0) {
4431 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4432 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004433 sp<Camera3Device> parent = mParent.promote();
4434 if (parent != nullptr) {
4435 parent->mRequestBufferSM.onRequestThreadPaused();
4436 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004437 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004438
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004439 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004440 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004441 return true;
4442 }
4443 }
4444 // We don't set mPaused to false here, because waitForNextRequest needs
4445 // to further manage the paused state in case of starvation.
4446 return false;
4447}
4448
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004449void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004450 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004451 // With work to do, mark thread as unpaused.
4452 // If paused by request (setPaused), don't resume, to avoid
4453 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004454 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004455 Mutex::Autolock p(mPauseLock);
4456 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004457 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4458 if (mPaused) {
4459 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4460 if (statusTracker != 0) {
4461 statusTracker->markComponentActive(mStatusId);
4462 }
4463 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004464 mPaused = false;
4465 }
4466}
4467
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004468void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4469 sp<Camera3Device> parent = mParent.promote();
4470 if (parent != NULL) {
4471 va_list args;
4472 va_start(args, fmt);
4473
4474 parent->setErrorStateV(fmt, args);
4475
4476 va_end(args);
4477 }
4478}
4479
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004480status_t Camera3Device::RequestThread::insertTriggers(
4481 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004482 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004483 Mutex::Autolock al(mTriggerMutex);
4484
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004485 sp<Camera3Device> parent = mParent.promote();
4486 if (parent == NULL) {
4487 CLOGE("RequestThread: Parent is gone");
4488 return DEAD_OBJECT;
4489 }
4490
Emilian Peevaebbe412018-01-15 13:53:24 +00004491 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004492 size_t count = mTriggerMap.size();
4493
4494 for (size_t i = 0; i < count; ++i) {
4495 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004496 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004497
4498 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4499 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4500 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004501 if (isAeTrigger) {
4502 request->mResultExtras.precaptureTriggerId = triggerId;
4503 mCurrentPreCaptureTriggerId = triggerId;
4504 } else {
4505 request->mResultExtras.afTriggerId = triggerId;
4506 mCurrentAfTriggerId = triggerId;
4507 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004508 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004509 }
4510
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004511 camera_metadata_entry entry = metadata.find(tag);
4512
4513 if (entry.count > 0) {
4514 /**
4515 * Already has an entry for this trigger in the request.
4516 * Rewrite it with our requested trigger value.
4517 */
4518 RequestTrigger oldTrigger = trigger;
4519
4520 oldTrigger.entryValue = entry.data.u8[0];
4521
4522 mTriggerReplacedMap.add(tag, oldTrigger);
4523 } else {
4524 /**
4525 * More typical, no trigger entry, so we just add it
4526 */
4527 mTriggerRemovedMap.add(tag, trigger);
4528 }
4529
4530 status_t res;
4531
4532 switch (trigger.getTagType()) {
4533 case TYPE_BYTE: {
4534 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4535 res = metadata.update(tag,
4536 &entryValue,
4537 /*count*/1);
4538 break;
4539 }
4540 case TYPE_INT32:
4541 res = metadata.update(tag,
4542 &trigger.entryValue,
4543 /*count*/1);
4544 break;
4545 default:
4546 ALOGE("%s: Type not supported: 0x%x",
4547 __FUNCTION__,
4548 trigger.getTagType());
4549 return INVALID_OPERATION;
4550 }
4551
4552 if (res != OK) {
4553 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4554 ", value %d", __FUNCTION__, trigger.getTagName(),
4555 trigger.entryValue);
4556 return res;
4557 }
4558
4559 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4560 trigger.getTagName(),
4561 trigger.entryValue);
4562 }
4563
4564 mTriggerMap.clear();
4565
4566 return count;
4567}
4568
4569status_t Camera3Device::RequestThread::removeTriggers(
4570 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004571 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004572 Mutex::Autolock al(mTriggerMutex);
4573
Emilian Peevaebbe412018-01-15 13:53:24 +00004574 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004575
4576 /**
4577 * Replace all old entries with their old values.
4578 */
4579 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4580 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4581
4582 status_t res;
4583
4584 uint32_t tag = trigger.metadataTag;
4585 switch (trigger.getTagType()) {
4586 case TYPE_BYTE: {
4587 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4588 res = metadata.update(tag,
4589 &entryValue,
4590 /*count*/1);
4591 break;
4592 }
4593 case TYPE_INT32:
4594 res = metadata.update(tag,
4595 &trigger.entryValue,
4596 /*count*/1);
4597 break;
4598 default:
4599 ALOGE("%s: Type not supported: 0x%x",
4600 __FUNCTION__,
4601 trigger.getTagType());
4602 return INVALID_OPERATION;
4603 }
4604
4605 if (res != OK) {
4606 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4607 ", trigger value %d", __FUNCTION__,
4608 trigger.getTagName(), trigger.entryValue);
4609 return res;
4610 }
4611 }
4612 mTriggerReplacedMap.clear();
4613
4614 /**
4615 * Remove all new entries.
4616 */
4617 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4618 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4619 status_t res = metadata.erase(trigger.metadataTag);
4620
4621 if (res != OK) {
4622 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4623 ", trigger value %d", __FUNCTION__,
4624 trigger.getTagName(), trigger.entryValue);
4625 return res;
4626 }
4627 }
4628 mTriggerRemovedMap.clear();
4629
4630 return OK;
4631}
4632
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004633status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004634 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004635 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004636 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004637 status_t res;
4638
Emilian Peevaebbe412018-01-15 13:53:24 +00004639 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004640
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004641 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004642 // exists
4643 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4644 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4645 if (afTrigger.count > 0 &&
4646 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4647 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004648 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004649 if (res != OK) return res;
4650 }
4651
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004652 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004653 // if none already exists
4654 camera_metadata_entry pcTrigger =
4655 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4656 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4657 if (pcTrigger.count > 0 &&
4658 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4659 pcId.count == 0) {
4660 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004661 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004662 if (res != OK) return res;
4663 }
4664
4665 return OK;
4666}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004667
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004668bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4669 const sp<CaptureRequest> &request) {
4670 ATRACE_CALL();
4671
Austin Borger18b30a72022-10-27 12:20:29 -07004672 if (mOverrideToPortrait) {
4673 Mutex::Autolock l(mTriggerMutex);
4674 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4675 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4676 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4677 &rotateAndCrop_u8, 1);
4678 return true;
4679 }
4680
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004681 if (request->mRotateAndCropAuto) {
4682 Mutex::Autolock l(mTriggerMutex);
4683 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4684
4685 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4686 if (rotateAndCropEntry.count > 0) {
4687 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4688 return false;
4689 } else {
4690 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4691 return true;
4692 }
4693 } else {
4694 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4695 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4696 &rotateAndCrop_u8, 1);
4697 return true;
4698 }
4699 }
4700 return false;
4701}
4702
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004703bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4704 ATRACE_CALL();
4705
4706 if (request->mAutoframingAuto) {
4707 Mutex::Autolock l(mTriggerMutex);
4708 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4709
4710 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4711 if (autoframingEntry.count > 0) {
4712 if (autoframingEntry.data.u8[0] == mAutoframingOverride) {
4713 return false;
4714 } else {
4715 autoframingEntry.data.u8[0] = mAutoframingOverride;
4716 return true;
4717 }
4718 } else {
4719 uint8_t autoframing_u8 = mAutoframingOverride;
4720 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4721 &autoframing_u8, 1);
4722 return true;
4723 }
4724 }
4725 return false;
4726}
4727
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004728bool Camera3Device::RequestThread::overrideTestPattern(
4729 const sp<CaptureRequest> &request) {
4730 ATRACE_CALL();
4731
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004732 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004733
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004734 Mutex::Autolock l(mTriggerMutex);
4735
4736 bool changed = false;
4737
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004738 // For a multi-camera, the physical cameras support the same set of
4739 // test pattern modes as the logical camera.
4740 for (auto& settings : request->mSettingsList) {
4741 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004742
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004743 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4744 int32_t testPatternData[4] = {
4745 settings.mOriginalTestPatternData[0],
4746 settings.mOriginalTestPatternData[1],
4747 settings.mOriginalTestPatternData[2],
4748 settings.mOriginalTestPatternData[3]
4749 };
4750 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4751 testPatternMode = mCameraMute;
4752 testPatternData[0] = 0;
4753 testPatternData[1] = 0;
4754 testPatternData[2] = 0;
4755 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004756 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004757
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004758 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4759 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4760 if (testPatternEntry.count > 0) {
4761 if (testPatternEntry.data.i32[0] != testPatternMode) {
4762 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004763 changed = true;
4764 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004765 } else if (supportTestPatternModeKey) {
4766 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4767 &testPatternMode, 1);
4768 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004769 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004770
4771 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4772 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4773 if (testPatternColor.count >= 4) {
4774 for (size_t i = 0; i < 4; i++) {
4775 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4776 testPatternColor.data.i32[i] = testPatternData[i];
4777 changed = true;
4778 }
4779 }
4780 } else if (supportTestPatternDataKey) {
4781 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4782 testPatternData, 4);
4783 changed = true;
4784 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004785 }
4786
4787 return changed;
4788}
4789
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004790status_t Camera3Device::RequestThread::setHalInterface(
4791 sp<HalInterface> newHalInterface) {
4792 if (newHalInterface.get() == nullptr) {
4793 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4794 return DEAD_OBJECT;
4795 }
4796
4797 mInterface = newHalInterface;
4798
4799 return OK;
4800}
4801
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004802/**
4803 * PreparerThread inner class methods
4804 */
4805
4806Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004807 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004808 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004809}
4810
4811Camera3Device::PreparerThread::~PreparerThread() {
4812 Thread::requestExitAndWait();
4813 if (mCurrentStream != nullptr) {
4814 mCurrentStream->cancelPrepare();
4815 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4816 mCurrentStream.clear();
4817 }
4818 clear();
4819}
4820
Ruben Brunkc78ac262015-08-13 17:58:46 -07004821status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004822 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004823 status_t res;
4824
4825 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004826 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004827
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004828 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004829 if (res == OK) {
4830 // No preparation needed, fire listener right off
4831 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004832 if (listener != NULL) {
4833 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004834 }
4835 return OK;
4836 } else if (res != NOT_ENOUGH_DATA) {
4837 return res;
4838 }
4839
4840 // Need to prepare, start up thread if necessary
4841 if (!mActive) {
4842 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4843 // isn't running
4844 Thread::requestExitAndWait();
4845 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4846 if (res != OK) {
4847 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004848 if (listener != NULL) {
4849 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004850 }
4851 return res;
4852 }
4853 mCancelNow = false;
4854 mActive = true;
4855 ALOGV("%s: Preparer stream started", __FUNCTION__);
4856 }
4857
4858 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004859 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004860 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4861
4862 return OK;
4863}
4864
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004865void Camera3Device::PreparerThread::pause() {
4866 ATRACE_CALL();
4867
4868 Mutex::Autolock l(mLock);
4869
4870 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4871 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4872 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4873 int currentMaxCount = mCurrentMaxCount;
4874 mPendingStreams.clear();
4875 mCancelNow = true;
4876 while (mActive) {
4877 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4878 if (res == TIMED_OUT) {
4879 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4880 return;
4881 } else if (res != OK) {
4882 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4883 return;
4884 }
4885 }
4886
4887 //Check whether the prepare thread was able to complete the current
4888 //stream. In case work is still pending emplace it along with the rest
4889 //of the streams in the pending list.
4890 if (currentStream != nullptr) {
4891 if (!mCurrentPrepareComplete) {
4892 pendingStreams.emplace(currentMaxCount, currentStream);
4893 }
4894 }
4895
4896 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4897 for (const auto& it : mPendingStreams) {
4898 it.second->cancelPrepare();
4899 }
4900}
4901
4902status_t Camera3Device::PreparerThread::resume() {
4903 ATRACE_CALL();
4904 status_t res;
4905
4906 Mutex::Autolock l(mLock);
4907 sp<NotificationListener> listener = mListener.promote();
4908
4909 if (mActive) {
4910 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4911 return NO_INIT;
4912 }
4913
4914 auto it = mPendingStreams.begin();
4915 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004916 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004917 if (res == OK) {
4918 if (listener != NULL) {
4919 listener->notifyPrepared(it->second->getId());
4920 }
4921 it = mPendingStreams.erase(it);
4922 } else if (res != NOT_ENOUGH_DATA) {
4923 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4924 res, strerror(-res));
4925 it = mPendingStreams.erase(it);
4926 } else {
4927 it++;
4928 }
4929 }
4930
4931 if (mPendingStreams.empty()) {
4932 return OK;
4933 }
4934
4935 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4936 if (res != OK) {
4937 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4938 __FUNCTION__, res, strerror(-res));
4939 return res;
4940 }
4941 mCancelNow = false;
4942 mActive = true;
4943 ALOGV("%s: Preparer stream started", __FUNCTION__);
4944
4945 return OK;
4946}
4947
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004948status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004949 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004950 Mutex::Autolock l(mLock);
4951
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004952 for (const auto& it : mPendingStreams) {
4953 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004954 }
4955 mPendingStreams.clear();
4956 mCancelNow = true;
4957
4958 return OK;
4959}
4960
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004961void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004962 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004963 Mutex::Autolock l(mLock);
4964 mListener = listener;
4965}
4966
4967bool Camera3Device::PreparerThread::threadLoop() {
4968 status_t res;
4969 {
4970 Mutex::Autolock l(mLock);
4971 if (mCurrentStream == nullptr) {
4972 // End thread if done with work
4973 if (mPendingStreams.empty()) {
4974 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4975 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4976 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4977 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004978 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004979 return false;
4980 }
4981
4982 // Get next stream to prepare
4983 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004984 mCurrentStream = it->second;
4985 mCurrentMaxCount = it->first;
4986 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004987 mPendingStreams.erase(it);
4988 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4989 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4990 } else if (mCancelNow) {
4991 mCurrentStream->cancelPrepare();
4992 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4993 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4994 mCurrentStream.clear();
4995 mCancelNow = false;
4996 return true;
4997 }
4998 }
4999
5000 res = mCurrentStream->prepareNextBuffer();
5001 if (res == NOT_ENOUGH_DATA) return true;
5002 if (res != OK) {
5003 // Something bad happened; try to recover by cancelling prepare and
5004 // signalling listener anyway
5005 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5006 mCurrentStream->getId(), res, strerror(-res));
5007 mCurrentStream->cancelPrepare();
5008 }
5009
5010 // This stream has finished, notify listener
5011 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005012 sp<NotificationListener> listener = mListener.promote();
5013 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005014 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5015 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005016 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005017 }
5018
5019 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5020 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005021 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005022
5023 return true;
5024}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005025
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005026status_t Camera3Device::RequestBufferStateMachine::initialize(
5027 sp<camera3::StatusTracker> statusTracker) {
5028 if (statusTracker == nullptr) {
5029 ALOGE("%s: statusTracker is null", __FUNCTION__);
5030 return BAD_VALUE;
5031 }
5032
5033 std::lock_guard<std::mutex> lock(mLock);
5034 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005035 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005036 return OK;
5037}
5038
5039bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5040 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005041 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005042 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005043 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005044 return true;
5045 }
5046 return false;
5047}
5048
5049void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5050 std::lock_guard<std::mutex> lock(mLock);
5051 if (!mRequestBufferOngoing) {
5052 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5053 return;
5054 }
5055 mRequestBufferOngoing = false;
5056 if (mStatus == RB_STATUS_PENDING_STOP) {
5057 checkSwitchToStopLocked();
5058 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005059 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005060}
5061
5062void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5063 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005064 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005065 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005066 return;
5067}
5068
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005069void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005070 std::lock_guard<std::mutex> lock(mLock);
5071 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005072 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5073 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005074 mInflightMapEmpty = false;
5075 if (mStatus == RB_STATUS_STOPPED) {
5076 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005077 }
5078 return;
5079}
5080
5081void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5082 std::lock_guard<std::mutex> lock(mLock);
5083 mRequestThreadPaused = true;
5084 if (mStatus == RB_STATUS_PENDING_STOP) {
5085 checkSwitchToStopLocked();
5086 }
5087 return;
5088}
5089
5090void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5091 std::lock_guard<std::mutex> lock(mLock);
5092 mInflightMapEmpty = true;
5093 if (mStatus == RB_STATUS_PENDING_STOP) {
5094 checkSwitchToStopLocked();
5095 }
5096 return;
5097}
5098
5099void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5100 std::lock_guard<std::mutex> lock(mLock);
5101 if (!checkSwitchToStopLocked()) {
5102 mStatus = RB_STATUS_PENDING_STOP;
5103 }
5104 return;
5105}
5106
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005107bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5108 std::lock_guard<std::mutex> lock(mLock);
5109 if (mRequestBufferOngoing) {
5110 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5111 __FUNCTION__);
5112 return false;
5113 }
5114 mSwitchedToOffline = true;
5115 mInflightMapEmpty = true;
5116 mRequestThreadPaused = true;
5117 mStatus = RB_STATUS_STOPPED;
5118 return true;
5119}
5120
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005121void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5122 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5123 if (statusTracker != nullptr) {
5124 if (active) {
5125 statusTracker->markComponentActive(mRequestBufferStatusId);
5126 } else {
5127 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5128 }
5129 }
5130}
5131
5132bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5133 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5134 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005135 return true;
5136 }
5137 return false;
5138}
5139
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005140bool Camera3Device::startRequestBuffer() {
5141 return mRequestBufferSM.startRequestBuffer();
5142}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005143
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005144void Camera3Device::endRequestBuffer() {
5145 mRequestBufferSM.endRequestBuffer();
5146}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005147
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005148nsecs_t Camera3Device::getWaitDuration() {
5149 return kBaseGetBufferWait + getExpectedInFlightDuration();
5150}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005151
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005152void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5153 mInterface->getInflightBufferKeys(out);
5154}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005155
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005156void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5157 mInterface->getInflightRequestBufferKeys(out);
5158}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005159
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005160std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5161 std::vector<sp<Camera3StreamInterface>> ret;
5162 bool hasInputStream = mInputStream != nullptr;
5163 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5164 if (hasInputStream) {
5165 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005166 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005167 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5168 ret.push_back(mOutputStreams[i]);
5169 }
5170 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5171 ret.push_back(mDeletedStreams[i]);
5172 }
5173 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005174}
5175
Emilian Peevcc0b7952020-01-07 13:54:47 -08005176void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5177 ATRACE_CALL();
5178
5179 if (offlineStreamIds == nullptr) {
5180 return;
5181 }
5182
5183 Mutex::Autolock il(mInterfaceLock);
5184
5185 auto streamIds = mOutputStreams.getStreamIds();
5186 bool hasInputStream = mInputStream != nullptr;
5187 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5188 offlineStreamIds->push_back(mInputStream->getId());
5189 }
5190
5191 for (const auto & streamId : streamIds) {
5192 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5193 // Streams that use the camera buffer manager are currently not supported in
5194 // offline mode
5195 if (stream->getOfflineProcessingSupport() &&
5196 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5197 offlineStreamIds->push_back(streamId);
5198 }
5199 }
5200}
5201
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005202status_t Camera3Device::setRotateAndCropAutoBehavior(
5203 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5204 ATRACE_CALL();
5205 Mutex::Autolock il(mInterfaceLock);
5206 Mutex::Autolock l(mLock);
5207 if (mRequestThread == nullptr) {
5208 return INVALID_OPERATION;
5209 }
5210 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5211}
5212
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005213status_t Camera3Device::setAutoframingAutoBehavior(
5214 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5215 ATRACE_CALL();
5216 Mutex::Autolock il(mInterfaceLock);
5217 Mutex::Autolock l(mLock);
5218 if (mRequestThread == nullptr) {
5219 return INVALID_OPERATION;
5220 }
5221 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5222}
5223
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005224bool Camera3Device::supportsCameraMute() {
5225 Mutex::Autolock il(mInterfaceLock);
5226 Mutex::Autolock l(mLock);
5227
5228 return mSupportCameraMute;
5229}
5230
5231status_t Camera3Device::setCameraMute(bool enabled) {
5232 ATRACE_CALL();
5233 Mutex::Autolock il(mInterfaceLock);
5234 Mutex::Autolock l(mLock);
5235
5236 if (mRequestThread == nullptr || !mSupportCameraMute) {
5237 return INVALID_OPERATION;
5238 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005239 int32_t muteMode =
5240 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5241 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5242 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5243 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005244}
5245
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005246status_t Camera3Device::injectCamera(const String8& injectedCamId,
5247 sp<CameraProviderManager> manager) {
5248 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5249 ATRACE_CALL();
5250 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005251 // When the camera device is active, injectCamera() and stopInjection() will call
5252 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5253 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5254 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5255 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5256 // waitUntilStateThenRelock().
5257 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005258
5259 status_t res = NO_ERROR;
5260 if (mInjectionMethods->isInjecting()) {
5261 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5262 return OK;
5263 } else {
5264 res = mInjectionMethods->stopInjection();
5265 if (res != OK) {
5266 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5267 __FUNCTION__, res);
5268 return res;
5269 }
5270 }
5271 }
5272
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005273 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005274 if (res != OK) {
5275 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5276 __FUNCTION__, res);
5277 return res;
5278 }
5279
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005280 // When the second display of android is cast to the remote device, and the opened camera is
5281 // also cast to the second display, in this case, because the camera has configured the streams
5282 // at this time, we can directly call injectCamera() to replace the internal camera with
5283 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005284 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5285 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5286
5287 camera3::camera_stream_configuration injectionConfig;
5288 std::vector<uint32_t> injectionBufferSizes;
5289 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5290 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5291 || injectionBufferSizes.size() <= 0) {
5292 ALOGE("Failed to inject camera due to abandoned configuration! "
5293 "mOperatingMode: %d injectionConfig.num_streams: %d "
5294 "injectionBufferSizes.size(): %zu", mOperatingMode,
5295 injectionConfig.num_streams, injectionBufferSizes.size());
5296 return DEAD_OBJECT;
5297 }
5298
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005299 res = mInjectionMethods->injectCamera(
5300 injectionConfig, injectionBufferSizes);
5301 if (res != OK) {
5302 ALOGE("Can't finish inject camera process!");
5303 return res;
5304 }
5305 }
5306
5307 return OK;
5308}
5309
5310status_t Camera3Device::stopInjection() {
5311 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5312 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005313 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005314 return mInjectionMethods->stopInjection();
5315}
5316
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005317}; // namespace android