blob: 445b39705259d2253f64a175f98385bbf46c84dd [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070066#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080068#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080069#include <tuple>
70
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080072using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073
74namespace android {
75
Emilian Peev5104fe92021-10-21 14:27:09 -070076Camera3Device::Camera3Device(const String8 &id, bool overrideForPerfClass, bool legacyClient):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080077 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070078 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080079 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070080 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070081 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070082 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070083 mUsePartialResult(false),
84 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080085 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080086 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070087 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070088 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070089 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070090 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070091 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070092 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000093 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010094 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070095 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070096 mNeedFixupMonochromeTags(false),
97 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800100 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101}
102
103Camera3Device::~Camera3Device()
104{
105 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800106 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700107 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108}
109
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800110const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800111 return mId;
112}
113
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800114status_t Camera3Device::initializeCommonLocked() {
115
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700116 /** Start up status tracker thread */
117 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800118 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700119 if (res != OK) {
120 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
121 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800122 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700123 mStatusTracker.clear();
124 return res;
125 }
126
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700127 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700128 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700129
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700130 if (mUseHalBufManager) {
131 res = mRequestBufferSM.initialize(mStatusTracker);
132 if (res != OK) {
133 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
134 strerror(-res), res);
135 mInterface->close();
136 mStatusTracker.clear();
137 return res;
138 }
139 }
140
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800141 /** Create buffer manager */
142 mBufferManager = new Camera3BufferManager();
143
144 Vector<int32_t> sessionParamKeys;
145 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
146 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
147 if (sessionKeysEntry.count > 0) {
148 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
149 }
150
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700151 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
152 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
153 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
154 if (availableTestPatternModes.data.i32[i] ==
155 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
156 mSupportCameraMute = true;
157 mSupportTestPatternSolidColor = true;
158 break;
159 } else if (availableTestPatternModes.data.i32[i] ==
160 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
161 mSupportCameraMute = true;
162 mSupportTestPatternSolidColor = false;
163 }
164 }
165
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700166 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800167 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700168 this, mStatusTracker, mInterface, sessionParamKeys,
169 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800170 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700172 SET_ERR_L("Unable to start request queue thread: %s (%d)",
173 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800174 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800175 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 return res;
177 }
178
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700179 mPreparerThread = new PreparerThread();
180
Ruben Brunk183f0562015-08-12 12:55:02 -0700181 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400183 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700184 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800186 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800188 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800189 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800190 camera_metadata_entry timestampSource =
191 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
192 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
193 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800194 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800195 }
196
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700197 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100198 camera_metadata_entry partialResultsCount =
199 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
200 if (partialResultsCount.count > 0) {
201 mNumPartialResults = partialResultsCount.data.i32[0];
202 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 }
204
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800205 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
206 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700207 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700208 if (res != OK) {
209 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
210 return res;
211 }
212 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800213
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800214 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800215 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800216
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700217 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
218 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
219 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
220 }
221
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800222 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
223 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
224 }
225
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800226 // Hidl/AidlCamera3DeviceInjectionMethods
227 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800228
Ravneet74cd3732022-03-30 05:33:03 +0000229 /** Start watchdog thread */
230 mCameraServiceWatchdog = new CameraServiceWatchdog();
231 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
232 if (res != OK) {
233 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
234 strerror(-res), res);
235 return res;
236 }
237
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238 return OK;
239}
240
241status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700242 return disconnectImpl();
243}
244
245status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800246 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700247 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800248
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700249 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700250 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800252 Mutex::Autolock il(mInterfaceLock);
253 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
254 {
255 Mutex::Autolock l(mLock);
256 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000258 if (mRequestThread != NULL) {
259 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
260 res = mRequestThread->clear();
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800261 if (res != OK) {
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000262 SET_ERR_L("Can't stop streaming");
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800263 // Continue to close device even in case of error
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000264 } else {
265 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
266 if (res != OK) {
267 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
268 maxExpectedDuration);
269 // Continue to close device even in case of error
270 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800271 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 }
Jayant Chowdharya93f3462022-11-01 23:32:45 +0000273 // Signal to request thread that we're not expecting any
274 // more requests. This will be true since once we're in
275 // disconnect and we've cleared off the request queue, the
276 // request thread can't receive any new requests through
277 // binder calls - since disconnect holds
278 // mBinderSerialization lock.
279 mRequestThread->setRequestClearing();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700280 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800282 if (mStatus == STATUS_ERROR) {
283 CLOGE("Shutting down in an error state");
284 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800286 if (mStatusTracker != NULL) {
287 mStatusTracker->requestExit();
288 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800290 if (mRequestThread != NULL) {
291 mRequestThread->requestExit();
292 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800294 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
295 for (size_t i = 0; i < mOutputStreams.size(); i++) {
296 streams.push_back(mOutputStreams[i]);
297 }
298 if (mInputStream != nullptr) {
299 streams.push_back(mInputStream);
300 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700301 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700302 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800303 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
304 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700305 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
306 // HAL may be in a bad state, so waiting for request thread
307 // (which may be stuck in the HAL processCaptureRequest call)
308 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800309 // give up mInterfaceLock here and then lock it again. Could this lead
310 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311 mRequestThread->join();
312 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800314 Mutex::Autolock il(mInterfaceLock);
315 if (mStatusTracker != NULL) {
316 mStatusTracker->join();
317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800319 if (mInjectionMethods->isInjecting()) {
320 mInjectionMethods->stopInjection();
321 }
322
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800323 HalInterface* interface;
324 {
325 Mutex::Autolock l(mLock);
326 mRequestThread.clear();
327 Mutex::Autolock stLock(mTrackerLock);
328 mStatusTracker.clear();
329 interface = mInterface.get();
330 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700331
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800332 // Call close without internal mutex held, as the HAL close may need to
333 // wait on assorted callbacks,etc, to complete before it can return.
Ravneet98ffa752022-03-02 07:22:46 +0000334 mCameraServiceWatchdog->WATCH(interface->close());
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700335
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800336 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800337
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800338 {
339 Mutex::Autolock l(mLock);
340 mInterface->clear();
341 mOutputStreams.clear();
342 mInputStream.clear();
343 mDeletedStreams.clear();
344 mBufferManager.clear();
345 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
346 }
347
348 for (auto& weakStream : streams) {
349 sp<Camera3StreamInterface> stream = weakStream.promote();
350 if (stream != nullptr) {
351 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
352 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
353 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700354 }
355 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700356 ALOGI("%s: X", __FUNCTION__);
Ravneet98ffa752022-03-02 07:22:46 +0000357
358 if (mCameraServiceWatchdog != NULL) {
359 mCameraServiceWatchdog->requestExit();
360 mCameraServiceWatchdog.clear();
361 }
362
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700363 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800364}
365
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700366// For dumping/debugging only -
367// try to acquire a lock a few times, eventually give up to proceed with
368// debug/dump operations
369bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
370 bool gotLock = false;
371 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
372 if (lock.tryLock() == NO_ERROR) {
373 gotLock = true;
374 break;
375 } else {
376 usleep(kDumpSleepDuration);
377 }
378 }
379 return gotLock;
380}
381
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800382nsecs_t Camera3Device::getMonoToBoottimeOffset() {
383 // try three times to get the clock offset, choose the one
384 // with the minimum gap in measurements.
385 const int tries = 3;
386 nsecs_t bestGap, measured;
387 for (int i = 0; i < tries; ++i) {
388 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
389 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
390 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
391 const nsecs_t gap = tmono2 - tmono;
392 if (i == 0 || gap < bestGap) {
393 bestGap = gap;
394 measured = tbase - ((tmono + tmono2) >> 1);
395 }
396 }
397 return measured;
398}
399
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800400ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
401 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700402 // Get max jpeg size (area-wise) for default sensor pixel mode
403 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800404 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700405 /*isUltraHighResolutionSensor*/false);
406 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
407 // not ultra high res sensor
408 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800409 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700410 /*isUltraHighResolution*/true);
411 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800412 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
413 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700414 return BAD_VALUE;
415 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700416 bool useMaxSensorPixelModeThreshold = false;
417 if (uhrMaxJpegResolution.width != 0 &&
418 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
419 // Use the ultra high res max jpeg size and max jpeg buffer size
420 useMaxSensorPixelModeThreshold = true;
421 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700422
Zhijun Hef7da0962014-04-24 13:27:56 -0700423 // Get max jpeg buffer size
424 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800425 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700426 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800427 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
428 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700429 return BAD_VALUE;
430 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700431 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700432
433 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
434 if (useMaxSensorPixelModeThreshold) {
435 maxJpegBufferSize =
436 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
437 maxDefaultJpegResolution, maxJpegBufferSize);
438 chosenMaxJpegResolution = uhrMaxJpegResolution;
439 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800440 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700441
442 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700443 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700444 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800445 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
446 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700447 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800448 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
449 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700450 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700451 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700452 return jpegBufferSize;
453}
454
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800455ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700456 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800457 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700458 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800459 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
460 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700461 return BAD_VALUE;
462 }
463 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
464 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
465 return maxBytesForPointCloud;
466}
467
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800468ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
469 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800470 const int PER_CONFIGURATION_SIZE = 3;
471 const int WIDTH_OFFSET = 0;
472 const int HEIGHT_OFFSET = 1;
473 const int SIZE_OFFSET = 2;
474 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800475 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800476 camera3::SessionConfigurationUtils::getAppropriateModeTag(
477 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
478 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800479 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800480 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800481 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
482 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800483 return BAD_VALUE;
484 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700485
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800486 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
487 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
488 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
489 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
490 }
491 }
492
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800493 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
494 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800495 return BAD_VALUE;
496}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700497
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800498status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
499 ATRACE_CALL();
500 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700501
502 // Try to lock, but continue in case of failure (to avoid blocking in
503 // deadlocks)
504 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
505 bool gotLock = tryLockSpinRightRound(mLock);
506
507 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800508 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
509 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700510 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800511 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
512 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700513
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800514 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700515
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800516 String16 templatesOption("-t");
517 int n = args.size();
518 for (int i = 0; i < n; i++) {
519 if (args[i] == templatesOption) {
520 dumpTemplates = true;
521 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000522 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700523 if (i + 1 < n) {
524 String8 monitorTags = String8(args[i + 1]);
525 if (monitorTags == "off") {
526 mTagMonitor.disableMonitoring();
527 } else {
528 mTagMonitor.parseTagsToMonitor(monitorTags);
529 }
530 } else {
531 mTagMonitor.disableMonitoring();
532 }
533 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800534 }
535
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800536 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800537
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 const char *status =
539 mStatus == STATUS_ERROR ? "ERROR" :
540 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700541 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
542 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 mStatus == STATUS_ACTIVE ? "ACTIVE" :
544 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700545
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800546 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700547 if (mStatus == STATUS_ERROR) {
548 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
549 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800550 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800551 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000552 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
553 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
554 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800555 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556
557 if (mInputStream != NULL) {
558 write(fd, lines.string(), lines.size());
559 mInputStream->dump(fd, args);
560 } else {
561 lines.appendFormat(" No input stream.\n");
562 write(fd, lines.string(), lines.size());
563 }
564 for (size_t i = 0; i < mOutputStreams.size(); i++) {
565 mOutputStreams[i]->dump(fd,args);
566 }
567
Zhijun He431503c2016-03-07 17:30:16 -0800568 if (mBufferManager != NULL) {
569 lines = String8(" Camera3 Buffer Manager:\n");
570 write(fd, lines.string(), lines.size());
571 mBufferManager->dump(fd, args);
572 }
Zhijun He125684a2015-12-26 15:07:30 -0800573
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700574 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700575 if (mInFlightLock.try_lock()) {
576 if (mInFlightMap.size() == 0) {
577 lines.append(" None\n");
578 } else {
579 for (size_t i = 0; i < mInFlightMap.size(); i++) {
580 InFlightRequest r = mInFlightMap.valueAt(i);
581 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
582 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
583 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
584 r.numBuffersLeft);
585 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700586 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700587 mInFlightLock.unlock();
588 } else {
589 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700590 }
591 write(fd, lines.string(), lines.size());
592
Shuzhen Wang686f6442017-06-20 16:16:04 -0700593 if (mRequestThread != NULL) {
594 mRequestThread->dumpCaptureRequestLatency(fd,
595 " ProcessCaptureRequest latency histogram:");
596 }
597
Igor Murashkin1e479c02013-09-06 16:55:14 -0700598 {
599 lines = String8(" Last request sent:\n");
600 write(fd, lines.string(), lines.size());
601
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700603 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
604 }
605
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800606 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700607 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800608 "TEMPLATE_PREVIEW",
609 "TEMPLATE_STILL_CAPTURE",
610 "TEMPLATE_VIDEO_RECORD",
611 "TEMPLATE_VIDEO_SNAPSHOT",
612 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800613 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800614 };
615
Emilian Peevf4816702020-04-03 15:44:51 -0700616 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800617 camera_metadata_t *templateRequest = nullptr;
618 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700619 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800620 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800621 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800622 lines.append(" Not supported\n");
623 write(fd, lines.string(), lines.size());
624 } else {
625 write(fd, lines.string(), lines.size());
626 dump_indented_camera_metadata(templateRequest,
627 fd, /*verbosity*/2, /*indentation*/8);
628 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800629 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800630 }
631 }
632
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700633 mTagMonitor.dumpMonitoredMetadata(fd);
634
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800635 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800636 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800638 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800639 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800640
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700641 if (gotLock) mLock.unlock();
642 if (gotInterfaceLock) mInterfaceLock.unlock();
643
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800644 return OK;
645}
646
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700647status_t Camera3Device::startWatchingTags(const String8 &tags) {
648 mTagMonitor.parseTagsToMonitor(tags);
649 return OK;
650}
651
652status_t Camera3Device::stopWatchingTags() {
653 mTagMonitor.disableMonitoring();
654 return OK;
655}
656
657status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
658 mTagMonitor.getLatestMonitoredTagEvents(out);
659 return OK;
660}
661
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800662const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800663 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
665 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700666 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 mStatus == STATUS_ERROR ?
668 "when in error state" : "before init");
669 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700670 if (physicalId.isEmpty()) {
671 return mDeviceInfo;
672 } else {
673 std::string id(physicalId.c_str());
674 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
675 return mPhysicalDeviceInfoMap.at(id);
676 } else {
677 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
678 return mDeviceInfo;
679 }
680 }
681}
682
683const CameraMetadata& Camera3Device::info() const {
684 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800685 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800686}
687
Jianing Wei90e59c92014-03-12 18:29:36 -0700688status_t Camera3Device::checkStatusOkToCaptureLocked() {
689 switch (mStatus) {
690 case STATUS_ERROR:
691 CLOGE("Device has encountered a serious error");
692 return INVALID_OPERATION;
693 case STATUS_UNINITIALIZED:
694 CLOGE("Device not initialized");
695 return INVALID_OPERATION;
696 case STATUS_UNCONFIGURED:
697 case STATUS_CONFIGURED:
698 case STATUS_ACTIVE:
699 // OK
700 break;
701 default:
702 SET_ERR_L("Unexpected status: %d", mStatus);
703 return INVALID_OPERATION;
704 }
705 return OK;
706}
707
708status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000709 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700710 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700711 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700712 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700713 if (requestList == NULL) {
714 CLOGE("requestList cannot be NULL.");
715 return BAD_VALUE;
716 }
717
Jianing Weicb0652e2014-03-12 18:29:36 -0700718 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000719 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700720 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
721 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
722 ++metadataIt, ++surfaceMapIt) {
723 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700724 if (newRequest == 0) {
725 CLOGE("Can't create capture request");
726 return BAD_VALUE;
727 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700728
Shuzhen Wang9d066012016-09-30 11:30:20 -0700729 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700730 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700731
Jianing Weicb0652e2014-03-12 18:29:36 -0700732 // Setup burst Id and request Id
733 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800734 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
735 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700736 CLOGE("RequestID does not exist in metadata");
737 return BAD_VALUE;
738 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800739 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700740
Jianing Wei90e59c92014-03-12 18:29:36 -0700741 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700742
743 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700744 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700745 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
746 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
747 return BAD_VALUE;
748 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700749
750 // Setup batch size if this is a high speed video recording request.
751 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
752 auto firstRequest = requestList->begin();
753 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
754 if (outputStream->isVideoStream()) {
755 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800756 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700757 break;
758 }
759 }
760 }
761
Jianing Wei90e59c92014-03-12 18:29:36 -0700762 return OK;
763}
764
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800765status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800766 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800767
Emilian Peevaebbe412018-01-15 13:53:24 +0000768 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700769 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000770 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700771
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800772 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700773}
774
Emilian Peevaebbe412018-01-15 13:53:24 +0000775void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700776 std::list<const SurfaceMap>& surfaceMaps,
777 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000778 PhysicalCameraSettingsList requestList;
779 requestList.push_back({std::string(getId().string()), request});
780 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700781
782 SurfaceMap surfaceMap;
783 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
784 // With no surface list passed in, stream and surface will have 1-to-1
785 // mapping. So the surface index is 0 for each stream in the surfaceMap.
786 for (size_t i = 0; i < streams.count; i++) {
787 surfaceMap[streams.data.i32[i]].push_back(0);
788 }
789 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800790}
791
Jianing Wei90e59c92014-03-12 18:29:36 -0700792status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000793 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700794 const std::list<const SurfaceMap> &surfaceMaps,
795 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700796 /*out*/
797 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700798 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700799 nsecs_t requestTimeNs = systemTime();
800
Jianing Wei90e59c92014-03-12 18:29:36 -0700801 Mutex::Autolock il(mInterfaceLock);
802 Mutex::Autolock l(mLock);
803
804 status_t res = checkStatusOkToCaptureLocked();
805 if (res != OK) {
806 // error logged by previous call
807 return res;
808 }
809
810 RequestList requestList;
811
Shuzhen Wang0129d522016-10-30 22:43:41 -0700812 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700813 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700814 if (res != OK) {
815 // error logged by previous call
816 return res;
817 }
818
819 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700820 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700821 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700822 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700823 }
824
825 if (res == OK) {
826 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
827 if (res != OK) {
828 SET_ERR_L("Can't transition to active in %f seconds!",
829 kActiveTimeout/1e9);
830 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800831 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700832 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700833 } else {
834 CLOGE("Cannot queue request. Impossible.");
835 return BAD_VALUE;
836 }
837
838 return res;
839}
840
Emilian Peevaebbe412018-01-15 13:53:24 +0000841status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700842 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700843 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700844 ATRACE_CALL();
845
Emilian Peevaebbe412018-01-15 13:53:24 +0000846 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700847}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848
Jianing Weicb0652e2014-03-12 18:29:36 -0700849status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
850 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852
Emilian Peevaebbe412018-01-15 13:53:24 +0000853 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700854 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000855 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700856
Emilian Peevaebbe412018-01-15 13:53:24 +0000857 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700858 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800859}
860
Emilian Peevaebbe412018-01-15 13:53:24 +0000861status_t Camera3Device::setStreamingRequestList(
862 const List<const PhysicalCameraSettingsList> &requestsList,
863 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700864 ATRACE_CALL();
865
Emilian Peevaebbe412018-01-15 13:53:24 +0000866 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700867}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868
869sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000870 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 status_t res;
872
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700873 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800874 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
875 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000876 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700877 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700878 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700880 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700882 } else if (mStatus == STATUS_UNCONFIGURED) {
883 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 CLOGE("No streams configured");
885 return NULL;
886 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800887 }
888
Shuzhen Wang0129d522016-10-30 22:43:41 -0700889 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800890 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800891}
892
Jianing Weicb0652e2014-03-12 18:29:36 -0700893status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800894 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800897
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 switch (mStatus) {
899 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700900 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return INVALID_OPERATION;
902 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700903 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 case STATUS_UNCONFIGURED:
906 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 case STATUS_ACTIVE:
908 // OK
909 break;
910 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700911 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 return INVALID_OPERATION;
913 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800914 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700915
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700916 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917}
918
919status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
920 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700921 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800922
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700923 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924}
925
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700926status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800927 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700928 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700929 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700930 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700931 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800932 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
933 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700934
935 status_t res;
936 bool wasActive = false;
937
938 switch (mStatus) {
939 case STATUS_ERROR:
940 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
941 return INVALID_OPERATION;
942 case STATUS_UNINITIALIZED:
943 ALOGE("%s: Device not initialized", __FUNCTION__);
944 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700945 case STATUS_UNCONFIGURED:
946 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700947 // OK
948 break;
949 case STATUS_ACTIVE:
950 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700951 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700952 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700953 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700954 return res;
955 }
956 wasActive = true;
957 break;
958 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700960 return INVALID_OPERATION;
961 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700963
964 if (mInputStream != 0) {
965 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
966 return INVALID_OPERATION;
967 }
968
969 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
970 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700972
973 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800974 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700975
976 *id = mNextStreamId++;
977
978 // Continue captures if active at start
979 if (wasActive) {
980 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100981 // Reuse current operating mode and session parameters for new stream config
982 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700983 if (res != OK) {
984 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
985 __FUNCTION__, mNextStreamId, strerror(-res), res);
986 return res;
987 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700989 }
990
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800991 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700992 return OK;
993}
994
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700995status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700996 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -0700997 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800998 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800999 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1000 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001001 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Emilian Peevc81a7592022-02-14 17:38:18 -08001002 int timestampBase, int mirrorMode) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001003 ATRACE_CALL();
1004
1005 if (consumer == nullptr) {
1006 ALOGE("%s: consumer must not be null", __FUNCTION__);
1007 return BAD_VALUE;
1008 }
1009
1010 std::vector<sp<Surface>> consumers;
1011 consumers.push_back(consumer);
1012
1013 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001014 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001015 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001016 streamUseCase, timestampBase, mirrorMode);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001017}
1018
1019static bool isRawFormat(int format) {
1020 switch (format) {
1021 case HAL_PIXEL_FORMAT_RAW16:
1022 case HAL_PIXEL_FORMAT_RAW12:
1023 case HAL_PIXEL_FORMAT_RAW10:
1024 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1025 return true;
1026 default:
1027 return false;
1028 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001029}
1030
1031status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1032 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001033 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001034 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001035 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001036 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1037 int timestampBase, int mirrorMode) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001038 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001039
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001040 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001041 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001043 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001044 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001045 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1046 " mirrorMode %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001047 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001048 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001049 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001050
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 status_t res;
1052 bool wasActive = false;
1053
1054 switch (mStatus) {
1055 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 return INVALID_OPERATION;
1058 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001059 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001061 case STATUS_UNCONFIGURED:
1062 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 // OK
1064 break;
1065 case STATUS_ACTIVE:
1066 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001067 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 return res;
1071 }
1072 wasActive = true;
1073 break;
1074 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079
1080 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001081
Shuzhen Wang0129d522016-10-30 22:43:41 -07001082 if (consumers.size() == 0 && !hasDeferredConsumer) {
1083 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1084 return BAD_VALUE;
1085 }
Zhijun He5d677d12016-05-29 16:52:39 -07001086
Shuzhen Wang0129d522016-10-30 22:43:41 -07001087 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001088 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1089 return BAD_VALUE;
1090 }
1091
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001092 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1093 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1094 // be found in only one sensor pixel mode.
1095 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1096 return BAD_VALUE;
1097 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001098 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001100 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001101 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001102 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001103 if (blobBufferSize <= 0) {
1104 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1105 return BAD_VALUE;
1106 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001107 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1108 blobBufferSize = width * height;
1109 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001110 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001111 if (blobBufferSize <= 0) {
1112 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1113 return BAD_VALUE;
1114 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001115 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001116 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001117 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001118 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001119 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001120 timestampBase, mirrorMode);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001121 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001122 bool maxResolution =
1123 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1124 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001125 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1126 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001127 if (rawOpaqueBufferSize <= 0) {
1128 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1129 return BAD_VALUE;
1130 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001131 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001132 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001133 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001134 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001135 timestampBase, mirrorMode);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001136 } else if (isShared) {
1137 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1138 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001139 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001140 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001141 timestampBase, mirrorMode);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001142 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001143 newStream = new Camera3OutputStream(mNextStreamId,
1144 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001145 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001146 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001147 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001149 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001150 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001151 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001152 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001153 timestampBase, mirrorMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 }
Emilian Peev40ead602017-09-26 15:46:36 +01001155
1156 size_t consumerCount = consumers.size();
1157 for (size_t i = 0; i < consumerCount; i++) {
1158 int id = newStream->getSurfaceId(consumers[i]);
1159 if (id < 0) {
1160 SET_ERR_L("Invalid surface id");
1161 return BAD_VALUE;
1162 }
1163 if (surfaceIds != nullptr) {
1164 surfaceIds->push_back(id);
1165 }
1166 }
1167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169
Emilian Peev08dd2452017-04-06 16:55:14 +01001170 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001171
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001172 newStream->setImageDumpMask(mImageDumpMask);
1173
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 res = mOutputStreams.add(mNextStreamId, newStream);
1175 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return res;
1178 }
1179
Shuzhen Wang316781a2020-08-18 18:11:01 -07001180 mSessionStatsBuilder.addStream(mNextStreamId);
1181
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001182 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001183 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001184
1185 // Continue captures if active at start
1186 if (wasActive) {
1187 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001188 // Reuse current operating mode and session parameters for new stream config
1189 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001190 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001191 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1192 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001193 return res;
1194 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001195 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001197 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199}
1200
Emilian Peev710c1422017-08-30 11:19:38 +01001201status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001203 if (nullptr == streamInfo) {
1204 return BAD_VALUE;
1205 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001206 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001208
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001209 switch (mStatus) {
1210 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001211 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 return INVALID_OPERATION;
1213 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001214 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001216 case STATUS_UNCONFIGURED:
1217 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 case STATUS_ACTIVE:
1219 // OK
1220 break;
1221 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001222 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 return INVALID_OPERATION;
1224 }
1225
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001226 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1227 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001228 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001229 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 }
1231
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001232 streamInfo->width = stream->getWidth();
1233 streamInfo->height = stream->getHeight();
1234 streamInfo->format = stream->getFormat();
1235 streamInfo->dataSpace = stream->getDataSpace();
1236 streamInfo->formatOverridden = stream->isFormatOverridden();
1237 streamInfo->originalFormat = stream->getOriginalFormat();
1238 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1239 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001240 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242}
1243
1244status_t Camera3Device::setStreamTransform(int id,
1245 int transform) {
1246 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001249
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 switch (mStatus) {
1251 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001252 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 return INVALID_OPERATION;
1254 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001257 case STATUS_UNCONFIGURED:
1258 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 case STATUS_ACTIVE:
1260 // OK
1261 break;
1262 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001263 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 return INVALID_OPERATION;
1265 }
1266
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001267 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1268 if (stream == nullptr) {
1269 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 return BAD_VALUE;
1271 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001272 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273}
1274
1275status_t Camera3Device::deleteStream(int id) {
1276 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001277 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 Mutex::Autolock l(mLock);
1279 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001280
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001281 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001282
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001283 // CameraDevice semantics require device to already be idle before
1284 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001285 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001286 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001287 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001288 }
1289
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001290 if (mStatus == STATUS_ERROR) {
1291 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1292 __FUNCTION__, mId.string());
1293 return -EBUSY;
1294 }
1295
Igor Murashkin2fba5842013-04-22 14:03:54 -07001296 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001297 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001298 if (mInputStream != NULL && id == mInputStream->getId()) {
1299 deletedStream = mInputStream;
1300 mInputStream.clear();
1301 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001302 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001303 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304 return BAD_VALUE;
1305 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001306 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001307 }
1308
1309 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001310 if (stream != nullptr) {
1311 deletedStream = stream;
1312 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001313 }
1314
1315 // Free up the stream endpoint so that it can be used by some other stream
1316 res = deletedStream->disconnect();
1317 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001318 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319 // fall through since we want to still list the stream as deleted.
1320 }
1321 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001322 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323
1324 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001325}
1326
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001327status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001328 ATRACE_CALL();
1329 ALOGV("%s: E", __FUNCTION__);
1330
1331 Mutex::Autolock il(mInterfaceLock);
1332 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001333
Emilian Peev811d2952018-05-25 11:08:40 +01001334 // In case the client doesn't include any session parameter, try a
1335 // speculative configuration using the values from the last cached
1336 // default request.
1337 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001338 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001339 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1340 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1341 mLastTemplateId);
1342 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1343 operatingMode);
1344 }
1345
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001346 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1347}
1348
1349status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1350 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001351 //Filter out any incoming session parameters
1352 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001353 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1354 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001355 CameraMetadata filteredParams(availableSessionKeys.count);
1356 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1357 filteredParams.getAndLock());
1358 set_camera_metadata_vendor_id(meta, mVendorTagId);
1359 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001360 if (availableSessionKeys.count > 0) {
1361 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1362 camera_metadata_ro_entry entry = params.find(
1363 availableSessionKeys.data.i32[i]);
1364 if (entry.count > 0) {
1365 filteredParams.update(entry);
1366 }
1367 }
1368 }
1369
1370 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001371}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001373status_t Camera3Device::getInputBufferProducer(
1374 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001375 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001376 Mutex::Autolock il(mInterfaceLock);
1377 Mutex::Autolock l(mLock);
1378
1379 if (producer == NULL) {
1380 return BAD_VALUE;
1381 } else if (mInputStream == NULL) {
1382 return INVALID_OPERATION;
1383 }
1384
1385 return mInputStream->getInputBufferProducer(producer);
1386}
1387
Emilian Peevf4816702020-04-03 15:44:51 -07001388status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001389 CameraMetadata *request) {
1390 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001391 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001392
Emilian Peevf4816702020-04-03 15:44:51 -07001393 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001394 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001395 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001396 return BAD_VALUE;
1397 }
1398
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001399 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001401 {
1402 Mutex::Autolock l(mLock);
1403 switch (mStatus) {
1404 case STATUS_ERROR:
1405 CLOGE("Device has encountered a serious error");
1406 return INVALID_OPERATION;
1407 case STATUS_UNINITIALIZED:
1408 CLOGE("Device is not initialized!");
1409 return INVALID_OPERATION;
1410 case STATUS_UNCONFIGURED:
1411 case STATUS_CONFIGURED:
1412 case STATUS_ACTIVE:
1413 // OK
1414 break;
1415 default:
1416 SET_ERR_L("Unexpected status: %d", mStatus);
1417 return INVALID_OPERATION;
1418 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001419
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001420 if (!mRequestTemplateCache[templateId].isEmpty()) {
1421 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001422 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001423 return OK;
1424 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001425 }
1426
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001427 camera_metadata_t *rawRequest;
1428 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001429 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001430
1431 {
1432 Mutex::Autolock l(mLock);
1433 if (res == BAD_VALUE) {
1434 ALOGI("%s: template %d is not supported on this camera device",
1435 __FUNCTION__, templateId);
1436 return res;
1437 } else if (res != OK) {
1438 CLOGE("Unable to construct request template %d: %s (%d)",
1439 templateId, strerror(-res), res);
1440 return res;
1441 }
1442
1443 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1444 mRequestTemplateCache[templateId].acquire(rawRequest);
1445
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001446 // Override the template request with zoomRatioMapper
1447 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1448 &mRequestTemplateCache[templateId]);
1449 if (res != OK) {
1450 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1451 templateId, strerror(-res), res);
1452 return res;
1453 }
1454
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001455 // Fill in JPEG_QUALITY if not available
1456 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1457 static const uint8_t kDefaultJpegQuality = 95;
1458 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1459 &kDefaultJpegQuality, 1);
1460 }
1461
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001462 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001463 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001464 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001465 return OK;
1466}
1467
1468status_t Camera3Device::waitUntilDrained() {
1469 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001471 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001472 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001474 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001475}
1476
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001477status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 switch (mStatus) {
1479 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001480 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 ALOGV("%s: Already idle", __FUNCTION__);
1482 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001483 case STATUS_CONFIGURED:
1484 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001485 case STATUS_ERROR:
1486 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001487 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 break;
1489 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001490 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001491 return INVALID_OPERATION;
1492 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001493 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1494 maxExpectedDuration);
1495 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001496 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001497 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001498 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1499 res);
1500 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001501 return res;
1502}
1503
Ruben Brunk183f0562015-08-12 12:55:02 -07001504void Camera3Device::internalUpdateStatusLocked(Status status) {
1505 mStatus = status;
1506 mRecentStatusUpdates.add(mStatus);
1507 mStatusChanged.broadcast();
1508}
1509
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001511status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001512 if (mRequestThread.get() != nullptr) {
1513 mRequestThread->setPaused(true);
1514 } else {
1515 return NO_INIT;
1516 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001517
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001518 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1519 maxExpectedDuration);
1520 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001521 if (res != OK) {
1522 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001523 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001524 }
1525
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001526 return res;
1527}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001528
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001529// Resume after internalPauseAndWaitLocked
1530status_t Camera3Device::internalResumeLocked() {
1531 status_t res;
1532
1533 mRequestThread->setPaused(false);
1534
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001535 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1536 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001537 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1538 if (res != OK) {
1539 SET_ERR_L("Can't transition to active in %f seconds!",
1540 kActiveTimeout/1e9);
1541 }
1542 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544}
1545
Ruben Brunk183f0562015-08-12 12:55:02 -07001546status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001547 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001548
1549 size_t startIndex = 0;
1550 if (mStatusWaiters == 0) {
1551 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1552 // this status list
1553 mRecentStatusUpdates.clear();
1554 } else {
1555 // If other threads are waiting on updates to this status list, set the position of the
1556 // first element that this list will check rather than clearing the list.
1557 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001558 }
1559
Ruben Brunk183f0562015-08-12 12:55:02 -07001560 mStatusWaiters++;
1561
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001562 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001563 if (!active && mUseHalBufManager) {
1564 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001565 if (mStatus == STATUS_ACTIVE) {
1566 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001567 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001568 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001569 mRequestBufferSM.onWaitUntilIdle();
1570 }
1571
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001572 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001573 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001574 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001575 if (active == (mStatus == STATUS_ACTIVE)) {
1576 // Desired state is current
1577 break;
1578 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001579
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001580 nsecs_t timeElapsed = systemTime() - startTime;
1581 nsecs_t timeToWait = timeout - timeElapsed;
1582 if (timeToWait <= 0) {
1583 // Thread woke up spuriously but has timed out since.
1584 // Force out of loop with TIMED_OUT result.
1585 res = TIMED_OUT;
1586 break;
1587 }
1588 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001589 if (res != OK) break;
1590
Ruben Brunk183f0562015-08-12 12:55:02 -07001591 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1592 // transitions.
1593 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1594 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1595 __FUNCTION__);
1596
1597 // Encountered desired state since we began waiting
1598 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001599 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1600 stateSeen = true;
1601 break;
1602 }
1603 }
1604 } while (!stateSeen);
1605
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001606 if (signalPipelineDrain) {
1607 mRequestThread->resetPipelineDrain();
1608 }
1609
Ruben Brunk183f0562015-08-12 12:55:02 -07001610 mStatusWaiters--;
1611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001612 return res;
1613}
1614
1615
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001616status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001617 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001618 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001619
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001620 if (listener != NULL && mListener != NULL) {
1621 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1622 }
1623 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001624 mRequestThread->setNotificationListener(listener);
1625 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001626
1627 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001628}
1629
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001630bool Camera3Device::willNotify3A() {
1631 return false;
1632}
1633
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001634status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001635 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001636 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001637
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001638 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001639 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1640 if (st == std::cv_status::timeout) {
1641 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001642 }
1643 }
1644 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001645}
1646
Jianing Weicb0652e2014-03-12 18:29:36 -07001647status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001648 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001649 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001650
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001651 if (mResultQueue.empty()) {
1652 return NOT_ENOUGH_DATA;
1653 }
1654
Jianing Weicb0652e2014-03-12 18:29:36 -07001655 if (frame == NULL) {
1656 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1657 return BAD_VALUE;
1658 }
1659
1660 CaptureResult &result = *(mResultQueue.begin());
1661 frame->mResultExtras = result.mResultExtras;
1662 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001663 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001664 mResultQueue.erase(mResultQueue.begin());
1665
1666 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001667}
1668
1669status_t Camera3Device::triggerAutofocus(uint32_t id) {
1670 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001671 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001673 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1674 // Mix-in this trigger into the next request and only the next request.
1675 RequestTrigger trigger[] = {
1676 {
1677 ANDROID_CONTROL_AF_TRIGGER,
1678 ANDROID_CONTROL_AF_TRIGGER_START
1679 },
1680 {
1681 ANDROID_CONTROL_AF_TRIGGER_ID,
1682 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001683 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001684 };
1685
1686 return mRequestThread->queueTrigger(trigger,
1687 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001688}
1689
1690status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1691 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001692 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001693
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001694 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1695 // Mix-in this trigger into the next request and only the next request.
1696 RequestTrigger trigger[] = {
1697 {
1698 ANDROID_CONTROL_AF_TRIGGER,
1699 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1700 },
1701 {
1702 ANDROID_CONTROL_AF_TRIGGER_ID,
1703 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001704 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001705 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001706
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001707 return mRequestThread->queueTrigger(trigger,
1708 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001709}
1710
1711status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1712 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001713 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001714
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001715 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1716 // Mix-in this trigger into the next request and only the next request.
1717 RequestTrigger trigger[] = {
1718 {
1719 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1720 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1721 },
1722 {
1723 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1724 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001725 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001726 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001727
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001728 return mRequestThread->queueTrigger(trigger,
1729 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001730}
1731
Jianing Weicb0652e2014-03-12 18:29:36 -07001732status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001733 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001734 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001735 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001736
Zhijun He7ef20392014-04-21 16:04:17 -07001737 {
1738 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001739
1740 // b/116514106 "disconnect()" can get called twice for the same device. The
1741 // camera device will not be initialized during the second run.
1742 if (mStatus == STATUS_UNINITIALIZED) {
1743 return OK;
1744 }
1745
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001746 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001747
1748 // Stop session and stream counter
1749 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001750 }
1751
Ravneet98ffa752022-03-02 07:22:46 +00001752 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal6dbbb0b2022-06-29 01:34:01 +00001753 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneet98ffa752022-03-02 07:22:46 +00001754 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1755 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1756
1757 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001758}
1759
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001760status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001761 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1762}
1763
1764status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001765 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001766 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001767 Mutex::Autolock il(mInterfaceLock);
1768 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001769
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001770 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1771 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001772 CLOGE("Stream %d does not exist", streamId);
1773 return BAD_VALUE;
1774 }
1775
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001776 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001777 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001778 return BAD_VALUE;
1779 }
1780
1781 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001782 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001783 return BAD_VALUE;
1784 }
1785
Ruben Brunkc78ac262015-08-13 17:58:46 -07001786 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001787}
1788
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001789status_t Camera3Device::tearDown(int streamId) {
1790 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001791 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001792 Mutex::Autolock il(mInterfaceLock);
1793 Mutex::Autolock l(mLock);
1794
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001795 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1796 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001797 CLOGE("Stream %d does not exist", streamId);
1798 return BAD_VALUE;
1799 }
1800
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001801 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1802 CLOGE("Stream %d is a target of a in-progress request", streamId);
1803 return BAD_VALUE;
1804 }
1805
1806 return stream->tearDown();
1807}
1808
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001809status_t Camera3Device::addBufferListenerForStream(int streamId,
1810 wp<Camera3StreamBufferListener> listener) {
1811 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001812 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001813 Mutex::Autolock il(mInterfaceLock);
1814 Mutex::Autolock l(mLock);
1815
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001816 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1817 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001818 CLOGE("Stream %d does not exist", streamId);
1819 return BAD_VALUE;
1820 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001821 stream->addBufferListener(listener);
1822
1823 return OK;
1824}
1825
Austin Borger4a870a32022-02-25 01:48:41 +00001826float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1827 camera_metadata_entry minDurations =
1828 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1829 for (size_t i = 0; i < minDurations.count; i += 4) {
1830 if (minDurations.data.i64[i] == stream->getFormat()
1831 && minDurations.data.i64[i+1] == stream->getWidth()
1832 && minDurations.data.i64[i+2] == stream->getHeight()) {
1833 int64_t minFrameDuration = minDurations.data.i64[i+3];
1834 return 1e9f / minFrameDuration;
1835 }
1836 }
1837 return 0.0f;
1838}
1839
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001840/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001841 * Methods called by subclasses
1842 */
1843
1844void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001845 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001846 std::vector<int> streamIds;
1847 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001848 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001849
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001850 {
1851 // Need mLock to safely update state and synchronize to current
1852 // state of methods in flight.
1853 Mutex::Autolock l(mLock);
1854 // We can get various system-idle notices from the status tracker
1855 // while starting up. Only care about them if we've actually sent
1856 // in some requests recently.
1857 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1858 return;
1859 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001860 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1861 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001862 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001863
1864 // Skip notifying listener if we're doing some user-transparent
1865 // state changes
1866 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001867
Austin Borger4a870a32022-02-25 01:48:41 +00001868 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1869 auto stream = mOutputStreams[i];
1870 if (stream.get() == nullptr) continue;
1871
1872 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1873 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1874
1875 // Populate stream statistics in case of Idle
1876 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001877 streamIds.push_back(stream->getId());
1878 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1879 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001880 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001881 if (camera3Stream != nullptr) {
1882 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001883 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001884 }
1885 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Austin Borger4a870a32022-02-25 01:48:41 +00001886 stream->getFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001887 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001888 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001889 stream->getDynamicRangeProfile(), streamUseCase);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001890 }
1891 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001892 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001893
1894 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001895 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001896 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001897 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001898 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001899 status_t res = OK;
1900 if (listener != nullptr) {
1901 if (idle) {
1902 // Get session stats from the builder, and notify the listener.
1903 int64_t requestCount, resultErrorCount;
1904 bool deviceError;
1905 std::map<int, StreamStats> streamStatsMap;
1906 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1907 &deviceError, &streamStatsMap);
1908 for (size_t i = 0; i < streamIds.size(); i++) {
1909 int streamId = streamIds[i];
1910 auto stats = streamStatsMap.find(streamId);
1911 if (stats != streamStatsMap.end()) {
1912 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
1913 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
1914 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
1915 streamStats[i].mHistogramType =
1916 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
1917 streamStats[i].mHistogramBins.assign(
1918 stats->second.mCaptureLatencyBins.begin(),
1919 stats->second.mCaptureLatencyBins.end());
1920 streamStats[i].mHistogramCounts.assign(
1921 stats->second.mCaptureLatencyHistogram.begin(),
1922 stats->second.mCaptureLatencyHistogram.end());
1923 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001924 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001925 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
1926 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00001927 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001928 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001929 }
1930 if (res != OK) {
1931 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
1932 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001933 }
1934}
1935
Shuzhen Wang758c2152017-01-10 18:26:18 -08001936status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001937 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001938 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001939 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1940 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001941
1942 if (surfaceIds == nullptr) {
1943 return BAD_VALUE;
1944 }
1945
Zhijun He5d677d12016-05-29 16:52:39 -07001946 Mutex::Autolock il(mInterfaceLock);
1947 Mutex::Autolock l(mLock);
1948
Shuzhen Wang758c2152017-01-10 18:26:18 -08001949 if (consumers.size() == 0) {
1950 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001951 return BAD_VALUE;
1952 }
1953
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001954 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
1955 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07001956 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001957 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07001958 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001959
1960 // isConsumerConfigurationDeferred will be off after setConsumers
1961 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001962 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001963 if (res != OK) {
1964 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1965 return res;
1966 }
1967
Emilian Peev40ead602017-09-26 15:46:36 +01001968 for (auto &consumer : consumers) {
1969 int id = stream->getSurfaceId(consumer);
1970 if (id < 0) {
1971 CLOGE("Invalid surface id!");
1972 return BAD_VALUE;
1973 }
1974 surfaceIds->push_back(id);
1975 }
1976
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07001977 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001978 if (!stream->isConfiguring()) {
1979 CLOGE("Stream %d was already fully configured.", streamId);
1980 return INVALID_OPERATION;
1981 }
Zhijun He5d677d12016-05-29 16:52:39 -07001982
Shuzhen Wang0129d522016-10-30 22:43:41 -07001983 res = stream->finishConfiguration();
1984 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07001985 // If finishConfiguration fails due to abandoned surface, do not set
1986 // device to error state.
1987 bool isSurfaceAbandoned =
1988 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
1989 if (!isSurfaceAbandoned) {
1990 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1991 stream->getId(), strerror(-res), res);
1992 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001993 return res;
1994 }
Zhijun He5d677d12016-05-29 16:52:39 -07001995 }
1996
1997 return OK;
1998}
1999
Emilian Peev40ead602017-09-26 15:46:36 +01002000status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2001 const std::vector<OutputStreamInfo> &outputInfo,
2002 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2003 Mutex::Autolock il(mInterfaceLock);
2004 Mutex::Autolock l(mLock);
2005
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002006 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2007 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002008 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002009 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002010 }
2011
2012 for (const auto &it : removedSurfaceIds) {
2013 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2014 CLOGE("Shared surface still part of a pending request!");
2015 return -EBUSY;
2016 }
2017 }
2018
Emilian Peev40ead602017-09-26 15:46:36 +01002019 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2020 if (res != OK) {
2021 CLOGE("Stream %d failed to update stream (error %d %s) ",
2022 streamId, res, strerror(-res));
2023 if (res == UNKNOWN_ERROR) {
2024 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2025 __FUNCTION__);
2026 }
2027 return res;
2028 }
2029
2030 return res;
2031}
2032
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002033status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2034 Mutex::Autolock il(mInterfaceLock);
2035 Mutex::Autolock l(mLock);
2036
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002037 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2038 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002039 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2040 return BAD_VALUE;
2041 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002042
2043 if (dropping) {
2044 mSessionStatsBuilder.stopCounter(streamId);
2045 } else {
2046 mSessionStatsBuilder.startCounter(streamId);
2047 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002048 return stream->dropBuffers(dropping);
2049}
2050
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002051/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052 * Camera3Device private methods
2053 */
2054
2055sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002056 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002059 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002060 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002061
2062 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002063 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 if (inputStreams.count > 0) {
2065 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002066 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002067 CLOGE("Request references unknown input stream %d",
2068 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 return NULL;
2070 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002071
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002072 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002073 SET_ERR_L("%s: input stream %d is not configured!",
2074 __FUNCTION__, mInputStream->getId());
2075 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002077 // Check if stream prepare is blocking requests.
2078 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002079 CLOGE("Request references an input stream that's being prepared!");
2080 return NULL;
2081 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082
2083 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002084 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 }
2086
2087 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002088 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002089 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002090 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002091 return NULL;
2092 }
2093
2094 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002095 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2096 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002097 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002098 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002099 return NULL;
2100 }
Zhijun He5d677d12016-05-29 16:52:39 -07002101 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002102 auto iter = surfaceMap.find(streams.data.i32[i]);
2103 if (iter != surfaceMap.end()) {
2104 const std::vector<size_t>& surfaces = iter->second;
2105 for (const auto& surface : surfaces) {
2106 if (stream->isConsumerConfigurationDeferred(surface)) {
2107 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2108 "due to deferred consumer", stream->getId(), surface);
2109 return NULL;
2110 }
2111 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002112 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002113 }
2114
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002115 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002116 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2117 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002119 // Check if stream prepare is blocking requests.
2120 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002121 CLOGE("Request references an output stream that's being prepared!");
2122 return NULL;
2123 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002124
2125 newRequest->mOutputStreams.push(stream);
2126 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002127 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002128 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002130 auto rotateAndCropEntry =
2131 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2132 if (rotateAndCropEntry.count > 0 &&
2133 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2134 newRequest->mRotateAndCropAuto = true;
2135 } else {
2136 newRequest->mRotateAndCropAuto = false;
2137 }
2138
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002139 auto zoomRatioEntry =
2140 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2141 if (zoomRatioEntry.count > 0 &&
2142 zoomRatioEntry.data.f[0] == 1.0f) {
2143 newRequest->mZoomRatioIs1x = true;
2144 } else {
2145 newRequest->mZoomRatioIs1x = false;
2146 }
2147
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002148 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002149 for (auto& settings : newRequest->mSettingsList) {
2150 auto testPatternModeEntry =
2151 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2152 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2153 testPatternModeEntry.data.i32[0] :
2154 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002155
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002156 auto testPatternDataEntry =
2157 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2158 if (testPatternDataEntry.count >= 4) {
2159 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2160 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2161 } else {
2162 settings.mOriginalTestPatternData[0] = 0;
2163 settings.mOriginalTestPatternData[1] = 0;
2164 settings.mOriginalTestPatternData[2] = 0;
2165 settings.mOriginalTestPatternData[3] = 0;
2166 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002167 }
2168 }
2169
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002170 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002171}
2172
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002173void Camera3Device::cancelStreamsConfigurationLocked() {
2174 int res = OK;
2175 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2176 res = mInputStream->cancelConfiguration();
2177 if (res != OK) {
2178 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2179 mInputStream->getId(), strerror(-res), res);
2180 }
2181 }
2182
2183 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002184 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002185 if (outputStream->isConfiguring()) {
2186 res = outputStream->cancelConfiguration();
2187 if (res != OK) {
2188 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2189 outputStream->getId(), strerror(-res), res);
2190 }
2191 }
2192 }
2193
2194 // Return state to that at start of call, so that future configures
2195 // properly clean things up
2196 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2197 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002198
2199 res = mPreparerThread->resume();
2200 if (res != OK) {
2201 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2202 }
2203}
2204
Emilian Peev0d0191e2020-04-21 17:01:18 -07002205bool Camera3Device::checkAbandonedStreamsLocked() {
2206 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2207 return true;
2208 }
2209
2210 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2211 auto stream = mOutputStreams[i];
2212 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2213 return true;
2214 }
2215 }
2216
2217 return false;
2218}
2219
Emilian Peev3bead5f2020-05-28 17:29:08 -07002220bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002221 ATRACE_CALL();
2222 bool ret = false;
2223
Shuzhen Wang316781a2020-08-18 18:11:01 -07002224 nsecs_t startTime = systemTime();
2225
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002226 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002227 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2228
2229 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002230 if (checkAbandonedStreamsLocked()) {
2231 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2232 __FUNCTION__);
2233 return true;
2234 }
2235
Emilian Peev3bead5f2020-05-28 17:29:08 -07002236 status_t rc = NO_ERROR;
2237 bool markClientActive = false;
2238 if (mStatus == STATUS_ACTIVE) {
2239 markClientActive = true;
2240 mPauseStateNotify = true;
2241 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2242
2243 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2244 }
2245
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002246 if (rc == NO_ERROR) {
2247 mNeedConfig = true;
2248 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2249 if (rc == NO_ERROR) {
2250 ret = true;
2251 mPauseStateNotify = false;
2252 //Moving to active state while holding 'mLock' is important.
2253 //There could be pending calls to 'create-/deleteStream' which
2254 //will trigger another stream configuration while the already
2255 //present streams end up with outstanding buffers that will
2256 //not get drained.
2257 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002258 } else if (rc == DEAD_OBJECT) {
2259 // DEAD_OBJECT can be returned if either the consumer surface is
2260 // abandoned, or the HAL has died.
2261 // - If the HAL has died, configureStreamsLocked call will set
2262 // device to error state,
2263 // - If surface is abandoned, we should not set device to error
2264 // state.
2265 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002266 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002267 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002268 }
2269 } else {
2270 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2271 }
2272
Shuzhen Wang316781a2020-08-18 18:11:01 -07002273 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2274 ns2ms(systemTime() - startTime));
2275
Emilian Peev3bead5f2020-05-28 17:29:08 -07002276 if (markClientActive) {
2277 mStatusTracker->markComponentActive(clientStatusId);
2278 }
2279
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002280 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002281}
2282
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002283status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002284 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002285 ATRACE_CALL();
2286 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002287
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002288 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002289 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002290 return INVALID_OPERATION;
2291 }
2292
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002293 if (operatingMode < 0) {
2294 CLOGE("Invalid operating mode: %d", operatingMode);
2295 return BAD_VALUE;
2296 }
2297
2298 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002299 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002300
2301 if (mOperatingMode != operatingMode) {
2302 mNeedConfig = true;
2303 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2304 mOperatingMode = operatingMode;
2305 }
2306
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002307 // Reset min expected duration when session is reconfigured.
2308 mMinExpectedDuration = 0;
2309
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002310 // In case called from configureStreams, abort queued input buffers not belonging to
2311 // any pending requests.
2312 if (mInputStream != NULL && notifyRequestThread) {
2313 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002314 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002315 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002316 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002317 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002318 if (res != OK) {
2319 // Exhausted acquiring all input buffers.
2320 break;
2321 }
2322
Emilian Peevf4816702020-04-03 15:44:51 -07002323 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002324 res = mInputStream->returnInputBuffer(inputBuffer);
2325 if (res != OK) {
2326 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2327 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2328 }
2329 }
2330 }
2331
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002332 if (!mNeedConfig) {
2333 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2334 return OK;
2335 }
2336
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002337 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002338 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002339 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2340 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002341 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002342 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002343 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002344 }
2345
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002346 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002347 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002348
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002349 mPreparerThread->pause();
2350
Emilian Peevf4816702020-04-03 15:44:51 -07002351 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002352 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002353 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002354 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002355
Emilian Peevf4816702020-04-03 15:44:51 -07002356 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002357 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002358 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002359
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002360
2361 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002362 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002363 inputStream = mInputStream->startConfiguration();
2364 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002365 CLOGE("Can't start input stream configuration");
2366 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367 return INVALID_OPERATION;
2368 }
2369 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002370
2371 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002372 }
2373
Shuzhen Wang99080502021-03-07 21:08:20 -08002374 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002375 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002377
2378 // Don't configure bidi streams twice, nor add them twice to the list
2379 if (mOutputStreams[i].get() ==
2380 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2381
2382 config.num_streams--;
2383 continue;
2384 }
2385
Emilian Peevf4816702020-04-03 15:44:51 -07002386 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002387 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002388 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002389 CLOGE("Can't start output stream configuration");
2390 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002391 return INVALID_OPERATION;
2392 }
2393 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002394
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002395 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002396 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2397 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002398 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2399 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002400 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2401 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002402 } else if (outputStream->data_space ==
2403 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2404 bufferSizes[k] = outputStream->width * outputStream->height;
2405 } else {
2406 ALOGW("%s: Blob dataSpace %d not supported",
2407 __FUNCTION__, outputStream->data_space);
2408 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002409 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002410
2411 if (mOutputStreams[i]->isMultiResolution()) {
2412 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2413 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2414 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2415 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002416
2417 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2418 composerSurfacePresent = true;
2419 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002420 }
2421
2422 config.streams = streams.editArray();
2423
2424 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002425 // max_buffers, usage, and priv fields, as well as data_space and format
2426 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002427
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002428 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002429 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002430 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002431
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002432 if (res == BAD_VALUE) {
2433 // HAL rejected this set of streams as unsupported, clean up config
2434 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002435 CLOGE("Set of requested inputs/outputs not supported by HAL");
2436 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002437 return BAD_VALUE;
2438 } else if (res != OK) {
2439 // Some other kind of error from configure_streams - this is not
2440 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002441 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2442 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002443 return res;
2444 }
2445
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002446 // Finish all stream configuration immediately.
2447 // TODO: Try to relax this later back to lazy completion, which should be
2448 // faster
2449
Igor Murashkin073f8572013-05-02 14:59:28 -07002450 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002451 bool streamReConfigured = false;
2452 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002453 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002454 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002455 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002456 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002457 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2458 return DEAD_OBJECT;
2459 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002460 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002461 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002462 if (streamReConfigured) {
2463 mInterface->onStreamReConfigured(mInputStream->getId());
2464 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002465 }
2466
2467 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002468 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002469 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002470 bool streamReConfigured = false;
2471 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002472 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002473 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002474 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002475 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002476 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2477 return DEAD_OBJECT;
2478 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002479 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002480 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002481 if (streamReConfigured) {
2482 mInterface->onStreamReConfigured(outputStream->getId());
2483 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002484 }
2485 }
2486
Emilian Peeve23f1d92021-09-20 14:56:01 -07002487 mRequestThread->setComposerSurface(composerSurfacePresent);
2488
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002489 // Request thread needs to know to avoid using repeat-last-settings protocol
2490 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002491 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002492 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2493 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002494 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002495
Zhijun He90f7c372016-08-16 16:19:43 -07002496 char value[PROPERTY_VALUE_MAX];
2497 property_get("camera.fifo.disable", value, "0");
2498 int32_t disableFifo = atoi(value);
2499 if (disableFifo != 1) {
2500 // Boost priority of request thread to SCHED_FIFO.
2501 pid_t requestThreadTid = mRequestThread->getTid();
2502 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002503 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002504 if (res != OK) {
2505 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2506 strerror(-res), res);
2507 } else {
2508 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2509 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002510 }
2511
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002512 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002513 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2514 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2515 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2516 sessionParams.unlock(newSessionParams);
2517 mSessionParams.unlock(currentSessionParams);
2518 if (updateSessionParams) {
2519 mSessionParams = sessionParams;
2520 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002521
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002522 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002523
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002524 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002525 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002526
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002527 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002528
Zhijun He0a210512014-07-24 13:45:15 -07002529 // tear down the deleted streams after configure streams.
2530 mDeletedStreams.clear();
2531
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002532 auto rc = mPreparerThread->resume();
2533 if (rc != OK) {
2534 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2535 return rc;
2536 }
2537
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002538 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002539 mRequestBufferSM.onStreamsConfigured();
2540 }
2541
Cliff Wu3b268182021-07-06 15:44:43 +08002542 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002543 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002544 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002545 // configure the injection streams.
2546 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002547 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002548 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2549 res = mInjectionMethods->injectCamera(config, bufferSizes);
2550 if (res != OK) {
2551 ALOGE("Can't finish inject camera process!");
2552 return res;
2553 }
Cliff Wu3b268182021-07-06 15:44:43 +08002554 } else {
2555 // First run configureStreamsLocked() and then call injectCamera() case:
2556 // If the stream configuration has been completed and camera deive is active, but the
2557 // injection camera has not been injected yet, we need to store the stream configuration of
2558 // the internal camera (because the stream configuration of the injection camera is based
2559 // on the internal camera). When injecting occurs later, this configuration can be used by
2560 // the injection camera.
2561 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2562 " injection camera has not been injected yet.", __FUNCTION__);
2563 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002564 }
2565
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002566 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002567}
2568
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002569status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002570 ATRACE_CALL();
2571 status_t res;
2572
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002573 if (mFakeStreamId != NO_STREAM) {
2574 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002575 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002576 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002577 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002578 return INVALID_OPERATION;
2579 }
2580
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002581 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002582
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002583 sp<Camera3OutputStreamInterface> fakeStream =
2584 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002585
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002586 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002587 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002588 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002589 return res;
2590 }
2591
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002592 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002593 mNextStreamId++;
2594
2595 return OK;
2596}
2597
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002598status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002599 ATRACE_CALL();
2600 status_t res;
2601
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002602 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002603 if (mOutputStreams.size() == 1) return OK;
2604
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002605 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002607 // Ok, have a fake stream and there's at least one other output stream,
2608 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002609
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002610 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002611 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002612 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002613 return INVALID_OPERATION;
2614 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002615 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002616
2617 // Free up the stream endpoint so that it can be used by some other stream
2618 res = deletedStream->disconnect();
2619 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002620 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002621 // fall through since we want to still list the stream as deleted.
2622 }
2623 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002624 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002625
2626 return res;
2627}
2628
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002629void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002630 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002631 Mutex::Autolock l(mLock);
2632 va_list args;
2633 va_start(args, fmt);
2634
2635 setErrorStateLockedV(fmt, args);
2636
2637 va_end(args);
2638}
2639
2640void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002641 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002642 Mutex::Autolock l(mLock);
2643 setErrorStateLockedV(fmt, args);
2644}
2645
2646void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2647 va_list args;
2648 va_start(args, fmt);
2649
2650 setErrorStateLockedV(fmt, args);
2651
2652 va_end(args);
2653}
2654
2655void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002656 // Print out all error messages to log
2657 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002658 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002659
2660 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002661 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002662
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002663 mErrorCause = errorCause;
2664
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002665 if (mRequestThread != nullptr) {
2666 mRequestThread->setPaused(true);
2667 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002668 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002669
2670 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002671 sp<NotificationListener> listener = mListener.promote();
2672 if (listener != NULL) {
2673 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002674 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002675 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002676 }
2677
2678 // Save stack trace. View by dumping it later.
2679 CameraTraces::saveTrace();
2680 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002681}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002682
2683/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002684 * In-flight request management
2685 */
2686
Jianing Weicb0652e2014-03-12 18:29:36 -07002687status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002688 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002689 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002690 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Shuzhen Wang99080502021-03-07 21:08:20 -08002691 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2692 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002693 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002694 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002695 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002696
2697 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002698 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002699 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002700 isStillCapture, isZslCapture, rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs,
2701 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002702 if (res < 0) return res;
2703
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002704 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002705 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2706 // avoid a deadlock during reprocess requests.
2707 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002708 if (mStatusTracker != nullptr) {
2709 mStatusTracker->markComponentActive(mInFlightStatusId);
2710 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002711 }
2712
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002713 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002714 return OK;
2715}
2716
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002717void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002718 // Indicate idle inFlightMap to the status tracker
2719 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002720 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002721 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2722 // avoid a deadlock during reprocess requests.
2723 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002724 if (mStatusTracker != nullptr) {
2725 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2726 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002727 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002728 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002729}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002730
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002731void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002732 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002733 // something has likely gone wrong. This might still be legit only if application send in
2734 // a long burst of long exposure requests.
2735 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2736 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2737 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2738 mInFlightMap.size(), mExpectedInflightDuration);
2739 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2740 kInFlightWarnLimitHighSpeed) {
2741 CLOGW("In-flight list too large for high speed configuration: %zu,"
2742 "total inflight duration %" PRIu64,
2743 mInFlightMap.size(), mExpectedInflightDuration);
2744 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002745 }
2746}
2747
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002748void Camera3Device::onInflightMapFlushedLocked() {
2749 mExpectedInflightDuration = 0;
2750}
2751
2752void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002753 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002754 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2755 mInFlightMap.removeItemsAt(idx, 1);
2756
2757 onInflightEntryRemovedLocked(duration);
2758}
2759
2760
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002761void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002762 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002763 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002764 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002765 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002766 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002767 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002768
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002769 FlushInflightReqStates states {
2770 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002771 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002772
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002773 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002774}
2775
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002776CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002777 ALOGV("%s", __FUNCTION__);
2778
Igor Murashkin1e479c02013-09-06 16:55:14 -07002779 CameraMetadata retVal;
2780
2781 if (mRequestThread != NULL) {
2782 retVal = mRequestThread->getLatestRequest();
2783 }
2784
Igor Murashkin1e479c02013-09-06 16:55:14 -07002785 return retVal;
2786}
2787
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002788void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002789 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002790 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002791 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2792 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002793
2794 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002795 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002796}
2797
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002798void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002799 std::vector<native_handle_t*> *handles, bool closeFd) {
2800 if (handles == nullptr) {
2801 return;
2802 }
2803 if (closeFd) {
2804 for (auto& handle : *handles) {
2805 native_handle_close(handle);
2806 }
2807 }
2808 for (auto& handle : *handles) {
2809 native_handle_delete(handle);
2810 }
2811 handles->clear();
2812 return;
2813}
2814
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002815/**
2816 * HalInterface inner class methods
2817 */
2818
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002819void Camera3Device::HalInterface::getInflightBufferKeys(
2820 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002821 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002822 return;
2823}
2824
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002825void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2826 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002827 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002828 return;
2829}
2830
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002831bool Camera3Device::HalInterface::verifyBufferIds(
2832 int32_t streamId, std::vector<uint64_t>& bufIds) {
2833 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002834}
2835
2836status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002837 int32_t frameNumber, int32_t streamId,
2838 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002839 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002840}
2841
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002842status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002843 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002844 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002845}
2846
2847// Find and pop a buffer_handle_t based on bufferId
2848status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002849 uint64_t bufferId,
2850 /*out*/ buffer_handle_t** buffer,
2851 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002852 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002853}
2854
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002855std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2856 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002857 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002858}
2859
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002860uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2861 const native_handle_t* handle) {
2862 return mBufferRecords.removeOneBufferCache(streamId, handle);
2863}
2864
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002865void Camera3Device::HalInterface::onBufferFreed(
2866 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002867 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2868 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2869 if (bufferId != BUFFER_ID_NO_BUFFER) {
2870 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002871 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002872}
2873
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002874void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002875 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
2876 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2877 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002878 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
2879 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002880}
2881
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002882/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883 * RequestThread inner class methods
2884 */
2885
2886Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002887 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002888 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002889 bool useHalBufManager,
2890 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002891 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002892 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002893 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002894 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07002895 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002896 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07002897 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07002898 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002899 mReconfigured(false),
2900 mDoPause(false),
2901 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07002902 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002903 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002904 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002905 mCurrentAfTriggerId(0),
2906 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002907 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07002908 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07002909 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002910 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002911 mRepeatingLastFrameNumber(
2912 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07002913 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002914 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002915 mRequestLatency(kRequestLatencyBinSize),
2916 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002917 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07002918 mUseHalBufManager(useHalBufManager),
2919 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07002920 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921}
2922
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002923Camera3Device::RequestThread::~RequestThread() {}
2924
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002925void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002926 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002927 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002928 Mutex::Autolock l(mRequestLock);
2929 mListener = listener;
2930}
2931
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002932void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08002933 const CameraMetadata& sessionParams,
2934 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002935 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002936 Mutex::Autolock l(mRequestLock);
2937 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002938 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08002939 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002940 // Prepare video stream for high speed recording.
2941 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01002942 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002943}
2944
Jianing Wei90e59c92014-03-12 18:29:36 -07002945status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002946 List<sp<CaptureRequest> > &requests,
2947 /*out*/
2948 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002949 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07002950 Mutex::Autolock l(mRequestLock);
2951 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2952 ++it) {
2953 mRequestQueue.push_back(*it);
2954 }
2955
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002956 if (lastFrameNumber != NULL) {
2957 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2958 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2959 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2960 *lastFrameNumber);
2961 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002962
Jianing Wei90e59c92014-03-12 18:29:36 -07002963 unpauseForNewRequests();
2964
2965 return OK;
2966}
2967
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002968
2969status_t Camera3Device::RequestThread::queueTrigger(
2970 RequestTrigger trigger[],
2971 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002972 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002973 Mutex::Autolock l(mTriggerMutex);
2974 status_t ret;
2975
2976 for (size_t i = 0; i < count; ++i) {
2977 ret = queueTriggerLocked(trigger[i]);
2978
2979 if (ret != OK) {
2980 return ret;
2981 }
2982 }
2983
2984 return OK;
2985}
2986
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002987const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2988 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002989 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002990 if (d != nullptr) return d->mId;
2991 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002992}
2993
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002994status_t Camera3Device::RequestThread::queueTriggerLocked(
2995 RequestTrigger trigger) {
2996
2997 uint32_t tag = trigger.metadataTag;
2998 ssize_t index = mTriggerMap.indexOfKey(tag);
2999
3000 switch (trigger.getTagType()) {
3001 case TYPE_BYTE:
3002 // fall-through
3003 case TYPE_INT32:
3004 break;
3005 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003006 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3007 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003008 return INVALID_OPERATION;
3009 }
3010
3011 /**
3012 * Collect only the latest trigger, since we only have 1 field
3013 * in the request settings per trigger tag, and can't send more than 1
3014 * trigger per request.
3015 */
3016 if (index != NAME_NOT_FOUND) {
3017 mTriggerMap.editValueAt(index) = trigger;
3018 } else {
3019 mTriggerMap.add(tag, trigger);
3020 }
3021
3022 return OK;
3023}
3024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003025status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003026 const RequestList &requests,
3027 /*out*/
3028 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003029 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003030 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003031 if (lastFrameNumber != NULL) {
3032 *lastFrameNumber = mRepeatingLastFrameNumber;
3033 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003034 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003035 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003036 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3037 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003038
3039 unpauseForNewRequests();
3040
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003041 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003042 return OK;
3043}
3044
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003045bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003046 if (mRepeatingRequests.empty()) {
3047 return false;
3048 }
3049 int32_t requestId = requestIn->mResultExtras.requestId;
3050 const RequestList &repeatRequests = mRepeatingRequests;
3051 // All repeating requests are guaranteed to have same id so only check first quest
3052 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3053 return (firstRequest->mResultExtras.requestId == requestId);
3054}
3055
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003056status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003057 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003058 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003059 return clearRepeatingRequestsLocked(lastFrameNumber);
3060
3061}
3062
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003063status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3064 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003065 std::vector<int32_t> streamIds;
3066 for (const auto& request : mRepeatingRequests) {
3067 for (const auto& stream : request->mOutputStreams) {
3068 streamIds.push_back(stream->getId());
3069 }
3070 }
3071
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003072 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003073 if (lastFrameNumber != NULL) {
3074 *lastFrameNumber = mRepeatingLastFrameNumber;
3075 }
Emilian Peev2295df72021-11-12 18:14:10 -08003076
3077 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3078
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003079 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003080 return OK;
3081}
3082
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003083status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003084 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003085 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003086 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003087 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003088
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003089 // Send errors for all requests pending in the request queue, including
3090 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003091 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003092 if (listener != NULL) {
3093 for (RequestList::iterator it = mRequestQueue.begin();
3094 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003095 // Abort the input buffers for reprocess requests.
3096 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003097 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003098 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003099 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003100 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003101 if (res != OK) {
3102 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3103 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3104 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003105 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003106 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3107 if (res != OK) {
3108 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3109 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3110 }
3111 }
3112 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003113 // Set the frame number this request would have had, if it
3114 // had been submitted; this frame number will not be reused.
3115 // The requestId and burstId fields were set when the request was
3116 // submitted originally (in convertMetadataListToRequestListLocked)
3117 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003118 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003119 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003120 }
3121 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003122 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003123
3124 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003125 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003126 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003127 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003128 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003129 return OK;
3130}
3131
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003132status_t Camera3Device::RequestThread::flush() {
3133 ATRACE_CALL();
3134 Mutex::Autolock l(mFlushLock);
3135
Emilian Peev08dd2452017-04-06 16:55:14 +01003136 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003137}
3138
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003139void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003140 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003141 Mutex::Autolock l(mPauseLock);
3142 mDoPause = paused;
3143 mDoPauseSignal.signal();
3144}
3145
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003146status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3147 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003148 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003149 Mutex::Autolock l(mLatestRequestMutex);
3150 status_t res;
3151 while (mLatestRequestId != requestId) {
3152 nsecs_t startTime = systemTime();
3153
3154 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3155 if (res != OK) return res;
3156
3157 timeout -= (systemTime() - startTime);
3158 }
3159
3160 return OK;
3161}
3162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003163void Camera3Device::RequestThread::requestExit() {
3164 // Call parent to set up shutdown
3165 Thread::requestExit();
3166 // The exit from any possible waits
3167 mDoPauseSignal.signal();
3168 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003169
3170 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3171 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003172}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003173
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003174void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003175 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003176 bool surfaceAbandoned = false;
3177 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003178 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003179 {
3180 Mutex::Autolock l(mRequestLock);
3181 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3182 // repeating requests.
3183 for (const auto& request : mRepeatingRequests) {
3184 for (const auto& s : request->mOutputStreams) {
3185 if (s->isAbandoned()) {
3186 surfaceAbandoned = true;
3187 clearRepeatingRequestsLocked(&lastFrameNumber);
3188 break;
3189 }
3190 }
3191 if (surfaceAbandoned) {
3192 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003193 }
3194 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003195 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003196 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003197
3198 if (listener != NULL && surfaceAbandoned) {
3199 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003200 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003201}
3202
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003203bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003204 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003205 status_t res;
3206 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003207 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003208 uint32_t numRequestProcessed = 0;
3209 for (size_t i = 0; i < batchSize; i++) {
3210 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003211 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003212 }
3213
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003214 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3215
3216 bool triggerRemoveFailed = false;
3217 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3218 for (size_t i = 0; i < numRequestProcessed; i++) {
3219 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3220 nextRequest.submitted = true;
3221
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003222 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003223
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003224 if (!triggerRemoveFailed) {
3225 // Remove any previously queued triggers (after unlock)
3226 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3227 if (removeTriggerRes != OK) {
3228 triggerRemoveFailed = true;
3229 triggerFailedRequest = nextRequest;
3230 }
3231 }
3232 }
3233
3234 if (triggerRemoveFailed) {
3235 SET_ERR("RequestThread: Unable to remove triggers "
3236 "(capture request %d, HAL device: %s (%d)",
3237 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3238 cleanUpFailedRequests(/*sendRequestError*/ false);
3239 return false;
3240 }
3241
3242 if (res != OK) {
3243 // Should only get a failure here for malformed requests or device-level
3244 // errors, so consider all errors fatal. Bad metadata failures should
3245 // come through notify.
3246 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3247 mNextRequests[numRequestProcessed].halRequest.frame_number,
3248 strerror(-res), res);
3249 cleanUpFailedRequests(/*sendRequestError*/ false);
3250 return false;
3251 }
3252 return true;
3253}
3254
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003255Camera3Device::RequestThread::ExpectedDurationInfo
3256 Camera3Device::RequestThread::calculateExpectedDurationRange(
3257 const camera_metadata_t *request) {
3258 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003259 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003260 InFlightRequest::kDefaultMaxExpectedDuration,
3261 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003262 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3263 find_camera_metadata_ro_entry(request,
3264 ANDROID_CONTROL_AE_MODE,
3265 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003266 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003267
3268 switch (e.data.u8[0]) {
3269 case ANDROID_CONTROL_AE_MODE_OFF:
3270 find_camera_metadata_ro_entry(request,
3271 ANDROID_SENSOR_EXPOSURE_TIME,
3272 &e);
3273 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003274 expectedDurationInfo.minDuration = e.data.i64[0];
3275 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003276 }
3277 find_camera_metadata_ro_entry(request,
3278 ANDROID_SENSOR_FRAME_DURATION,
3279 &e);
3280 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003281 expectedDurationInfo.minDuration =
3282 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3283 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003284 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003285 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003286 break;
3287 default:
3288 find_camera_metadata_ro_entry(request,
3289 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3290 &e);
3291 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003292 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3293 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003294 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003295 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003296 break;
3297 }
3298
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003299 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003300}
3301
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003302bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3303 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3304 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3305 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3306 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3307 return true;
3308 }
3309
3310 return false;
3311}
3312
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003313void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3314 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003315 camera_capture_request_t& halRequest = nextRequest.halRequest;
3316 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003317 Mutex::Autolock al(mLatestRequestMutex);
3318
Shuzhen Wang83bff122020-11-20 15:51:39 -08003319 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003320 mLatestRequest.acquire(cloned);
3321
3322 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003323 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3324 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3325 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003326 CameraMetadata(cloned));
3327 }
3328
3329 sp<Camera3Device> parent = mParent.promote();
3330 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003331 int32_t inputStreamId = -1;
3332 if (halRequest.input_buffer != nullptr) {
3333 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3334 }
3335
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003336 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003337 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003338 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3339 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003340 }
3341 }
3342
Shuzhen Wang83bff122020-11-20 15:51:39 -08003343 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003344 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003345 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003346 }
3347
Shuzhen Wang83bff122020-11-20 15:51:39 -08003348 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003349}
3350
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003351bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3352 ATRACE_CALL();
3353 bool updatesDetected = false;
3354
Emilian Peev4ec17882019-01-24 17:16:58 -08003355 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003356 for (auto tag : mSessionParamKeys) {
3357 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003358 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003359
3360 if (entry.count > 0) {
3361 bool isDifferent = false;
3362 if (lastEntry.count > 0) {
3363 // Have a last value, compare to see if changed
3364 if (lastEntry.type == entry.type &&
3365 lastEntry.count == entry.count) {
3366 // Same type and count, compare values
3367 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3368 size_t entryBytes = bytesPerValue * lastEntry.count;
3369 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3370 if (cmp != 0) {
3371 isDifferent = true;
3372 }
3373 } else {
3374 // Count or type has changed
3375 isDifferent = true;
3376 }
3377 } else {
3378 // No last entry, so always consider to be different
3379 isDifferent = true;
3380 }
3381
3382 if (isDifferent) {
3383 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003384 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3385 updatesDetected = true;
3386 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003387 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003388 }
3389 } else if (lastEntry.count > 0) {
3390 // Value has been removed
3391 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003392 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003393 updatesDetected = true;
3394 }
3395 }
3396
Emilian Peev4ec17882019-01-24 17:16:58 -08003397 bool reconfigureRequired;
3398 if (updatesDetected) {
3399 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3400 updatedParams);
3401 mLatestSessionParams = updatedParams;
3402 } else {
3403 reconfigureRequired = false;
3404 }
3405
3406 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003407}
3408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003409bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003410 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003411 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003412 // Any function called from threadLoop() must not hold mInterfaceLock since
3413 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3414 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003415
3416 // Handle paused state.
3417 if (waitIfPaused()) {
3418 return true;
3419 }
3420
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003421 // Wait for the next batch of requests.
3422 waitForNextRequestBatch();
3423 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003424 return true;
3425 }
3426
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003427 // Get the latest request ID, if any
3428 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003429 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003430 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003431 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003432 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003433 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003434 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3435 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003436 }
3437
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003438 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3439 // or a single request from streaming or burst. In either case the first element
3440 // should contain the latest camera settings that we need to check for any session
3441 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003442 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003443 res = OK;
3444
3445 //Input stream buffers are already acquired at this point so an input stream
3446 //will not be able to move to idle state unless we force it.
3447 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3448 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3449 if (res != OK) {
3450 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3451 cleanUpFailedRequests(/*sendRequestError*/ false);
3452 return false;
3453 }
3454 }
3455
3456 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003457 sp<Camera3Device> parent = mParent.promote();
3458 if (parent != nullptr) {
3459 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003460 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07003461 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003462
3463 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3464 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3465 if (res != OK) {
3466 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3467 cleanUpFailedRequests(/*sendRequestError*/ false);
3468 return false;
3469 }
3470 }
3471 }
3472 }
3473
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003474 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003475 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003476 if (res == TIMED_OUT) {
3477 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003478 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003479 // Check if any stream is abandoned.
3480 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003481 return true;
3482 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003483 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003484 return false;
3485 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003486
Zhijun Hecc27e112013-10-03 16:12:43 -07003487 // Inform waitUntilRequestProcessed thread of a new request ID
3488 {
3489 Mutex::Autolock al(mLatestRequestMutex);
3490
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003491 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003492 mLatestRequestSignal.signal();
3493 }
3494
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003495 // Submit a batch of requests to HAL.
3496 // Use flush lock only when submitting multilple requests in a batch.
3497 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3498 // which may take a long time to finish so synchronizing flush() and
3499 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3500 // For now, only synchronize for high speed recording and we should figure something out for
3501 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003502 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003503
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003504 if (useFlushLock) {
3505 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003506 }
3507
Zhijun Hef0645c12016-08-02 00:58:11 -07003508 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003509 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003510
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003511 sp<Camera3Device> parent = mParent.promote();
3512 if (parent != nullptr) {
3513 parent->mRequestBufferSM.onSubmittingRequest();
3514 }
3515
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003516 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003517 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003518 submitRequestSuccess = sendRequestsBatch();
3519
Shuzhen Wang686f6442017-06-20 16:16:04 -07003520 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3521 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003522
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003523 if (useFlushLock) {
3524 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003526
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003527 // Unset as current request
3528 {
3529 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003530 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003531 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003532 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003533
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003534 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003535}
3536
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003537status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3538 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3539 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3540 ANDROID_SCALER_CROP_REGION_SET};
3541 if (request == nullptr) {
3542 ALOGE("%s request metadata nullptr", __FUNCTION__);
3543 return BAD_VALUE;
3544 }
3545 status_t res = OK;
3546 for (const auto &key : kFwkOnlyRegionKeys) {
3547 if (request->exists(key)) {
3548 res = request->erase(key);
3549 if (res != OK) {
3550 return res;
3551 }
3552 }
3553 }
3554 return OK;
3555}
3556
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003557status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003558 ATRACE_CALL();
3559
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003560 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003561 for (size_t i = 0; i < mNextRequests.size(); i++) {
3562 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003563 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003564 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3565 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003566
3567 // Prepare a request to HAL
3568 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3569
3570 // Insert any queued triggers (before metadata is locked)
3571 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003572 if (res < 0) {
3573 SET_ERR("RequestThread: Unable to insert triggers "
3574 "(capture request %d, HAL device: %s (%d)",
3575 halRequest->frame_number, strerror(-res), res);
3576 return INVALID_OPERATION;
3577 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003578
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003579 int triggerCount = res;
3580 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3581 mPrevTriggers = triggerCount;
3582
Emilian Peeve23f1d92021-09-20 14:56:01 -07003583 // Do not override rotate&crop for stream configurations that include
3584 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
3585 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3586 bool rotateAndCropChanged = mComposerOutput ? false :
3587 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003588 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003589
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003590 // If the request is the same as last, or we had triggers now or last time or
3591 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003592 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003593 (mPrevRequest != captureRequest || triggersMixedIn ||
3594 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003595 // Request settings are all the same within one batch, so only treat the first
3596 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003597 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003598 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003599 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003600 /**
3601 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003602 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003603 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003604 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003605 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003606 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003607 "(capture request %d, HAL device: %s (%d)",
3608 halRequest->frame_number, strerror(-res), res);
3609 return INVALID_OPERATION;
3610 }
3611
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003612 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003613 sp<Camera3Device> parent = mParent.promote();
3614 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003615 List<PhysicalCameraSettings>::iterator it;
3616 for (it = captureRequest->mSettingsList.begin();
3617 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003618 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3619 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003620 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3621 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3622 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3623 res);
3624 return INVALID_OPERATION;
3625 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003626 continue;
3627 }
3628
3629 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3630 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3631 updateCaptureRequest(&(it->metadata));
3632 if (res != OK) {
3633 SET_ERR("RequestThread: Unable to correct capture requests "
3634 "for scaler crop region and metering regions for request "
3635 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3636 res);
3637 return INVALID_OPERATION;
3638 }
3639 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003640 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3641 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3642 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3643 res);
3644 return INVALID_OPERATION;
3645 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003646 }
3647 }
3648
3649 // Correct metadata regions for distortion correction if enabled
3650 for (it = captureRequest->mSettingsList.begin();
3651 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003652 if (parent->mDistortionMappers.find(it->cameraId) ==
3653 parent->mDistortionMappers.end()) {
3654 continue;
3655 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003656
3657 if (!captureRequest->mDistortionCorrectionUpdated) {
3658 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3659 &(it->metadata));
3660 if (res != OK) {
3661 SET_ERR("RequestThread: Unable to correct capture requests "
3662 "for lens distortion for request %d: %s (%d)",
3663 halRequest->frame_number, strerror(-res), res);
3664 return INVALID_OPERATION;
3665 }
3666 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003667 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003668 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003669
3670 for (it = captureRequest->mSettingsList.begin();
3671 it != captureRequest->mSettingsList.end(); it++) {
3672 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3673 parent->mZoomRatioMappers.end()) {
3674 continue;
3675 }
3676
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003677 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003678 cameraIdsWithZoom.insert(it->cameraId);
3679 }
3680
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003681 if (!captureRequest->mZoomRatioUpdated) {
3682 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3683 &(it->metadata));
3684 if (res != OK) {
3685 SET_ERR("RequestThread: Unable to correct capture requests "
3686 "for zoom ratio for request %d: %s (%d)",
3687 halRequest->frame_number, strerror(-res), res);
3688 return INVALID_OPERATION;
3689 }
3690 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003691 }
3692 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003693 if (captureRequest->mRotateAndCropAuto &&
3694 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003695 for (it = captureRequest->mSettingsList.begin();
3696 it != captureRequest->mSettingsList.end(); it++) {
3697 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3698 if (mapper != parent->mRotateAndCropMappers.end()) {
3699 res = mapper->second.updateCaptureRequest(&(it->metadata));
3700 if (res != OK) {
3701 SET_ERR("RequestThread: Unable to correct capture requests "
3702 "for rotate-and-crop for request %d: %s (%d)",
3703 halRequest->frame_number, strerror(-res), res);
3704 return INVALID_OPERATION;
3705 }
3706 }
3707 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003708 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003709 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003710 }
3711 }
3712
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003713 /**
3714 * The request should be presorted so accesses in HAL
3715 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3716 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003717 captureRequest->mSettingsList.begin()->metadata.sort();
3718 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003719 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003720 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003721 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3722
3723 IF_ALOGV() {
3724 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3725 find_camera_metadata_ro_entry(
3726 halRequest->settings,
3727 ANDROID_CONTROL_AF_TRIGGER,
3728 &e
3729 );
3730 if (e.count > 0) {
3731 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3732 __FUNCTION__,
3733 halRequest->frame_number,
3734 e.data.u8[0]);
3735 }
3736 }
3737 } else {
3738 // leave request.settings NULL to indicate 'reuse latest given'
3739 ALOGVV("%s: Request settings are REUSED",
3740 __FUNCTION__);
3741 }
3742
Emilian Peevaebbe412018-01-15 13:53:24 +00003743 if (captureRequest->mSettingsList.size() > 1) {
3744 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3745 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003746 if (newRequest) {
3747 halRequest->physcam_settings =
3748 new const camera_metadata* [halRequest->num_physcam_settings];
3749 } else {
3750 halRequest->physcam_settings = nullptr;
3751 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003752 auto it = ++captureRequest->mSettingsList.begin();
3753 size_t i = 0;
3754 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3755 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003756 if (newRequest) {
3757 it->metadata.sort();
3758 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3759 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003760 }
3761 }
3762
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003763 uint32_t totalNumBuffers = 0;
3764
3765 // Fill in buffers
3766 if (captureRequest->mInputStream != NULL) {
3767 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003768
3769 halRequest->input_width = captureRequest->mInputBufferSize.width;
3770 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003771 totalNumBuffers += 1;
3772 } else {
3773 halRequest->input_buffer = NULL;
3774 }
3775
Emilian Peevf4816702020-04-03 15:44:51 -07003776 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003777 captureRequest->mOutputStreams.size());
3778 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003779 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003780
3781 sp<Camera3Device> parent = mParent.promote();
3782 if (parent == NULL) {
3783 // Should not happen, and nowhere to send errors to, so just log it
3784 CLOGE("RequestThread: Parent is gone");
3785 return INVALID_OPERATION;
3786 }
3787 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3788
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003789 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003790 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003791 sp<Camera3OutputStreamInterface> outputStream =
3792 captureRequest->mOutputStreams.editItemAt(j);
3793 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003794
3795 // Prepare video buffers for high speed recording on the first video request.
3796 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3797 // Only try to prepare video stream on the first video request.
3798 mPrepareVideoStream = false;
3799
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003800 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3801 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003802 while (res == NOT_ENOUGH_DATA) {
3803 res = outputStream->prepareNextBuffer();
3804 }
3805 if (res != OK) {
3806 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3807 __FUNCTION__, strerror(-res), res);
3808 outputStream->cancelPrepare();
3809 }
3810 }
3811
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003812 std::vector<size_t> uniqueSurfaceIds;
3813 res = outputStream->getUniqueSurfaceIds(
3814 captureRequest->mOutputSurfaces[streamId],
3815 &uniqueSurfaceIds);
3816 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3817 if (res != OK && res != INVALID_OPERATION) {
3818 ALOGE("%s: failed to query stream %d unique surface IDs",
3819 __FUNCTION__, streamId);
3820 return res;
3821 }
3822 if (res == OK) {
3823 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3824 }
3825
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003826 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003827 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003828 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003829 return TIMED_OUT;
3830 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003831 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003832 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003833 buffer.stream = outputStream->asHalStream();
3834 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003835 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003836 buffer.acquire_fence = -1;
3837 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003838 // Mark the output stream as unpreparable to block clients from calling
3839 // 'prepare' after this request reaches CameraHal and before the respective
3840 // buffers are requested.
3841 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003842 } else {
3843 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3844 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003845 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003846 if (res != OK) {
3847 // Can't get output buffer from gralloc queue - this could be due to
3848 // abandoned queue or other consumer misbehavior, so not a fatal
3849 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003850 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003851 " %s (%d)", strerror(-res), res);
3852
3853 return TIMED_OUT;
3854 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003855 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08003856
3857 {
3858 sp<Camera3Device> parent = mParent.promote();
3859 if (parent != nullptr) {
3860 const String8& streamCameraId = outputStream->getPhysicalCameraId();
3861 for (const auto& settings : captureRequest->mSettingsList) {
3862 if ((streamCameraId.isEmpty() &&
3863 parent->getId() == settings.cameraId.c_str()) ||
3864 streamCameraId == settings.cameraId.c_str()) {
3865 outputStream->fireBufferRequestForFrameNumber(
3866 captureRequest->mResultExtras.frameNumber,
3867 settings.metadata);
3868 }
3869 }
3870 }
3871 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07003872
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003873 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08003874 int32_t streamGroupId = outputStream->getHalStreamGroupId();
3875 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
3876 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
3877 } else if (!physicalCameraId.isEmpty()) {
3878 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003879 }
3880 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003881 }
3882 totalNumBuffers += halRequest->num_output_buffers;
3883
3884 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08003885 // If this request list is for constrained high speed recording (not
3886 // preview), and the current request is not the last one in the batch,
3887 // do not send callback to the app.
3888 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003889 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08003890 hasCallback = false;
3891 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003892 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003893 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003894 const camera_metadata_t* settings = halRequest->settings;
3895 bool shouldUnlockSettings = false;
3896 if (settings == nullptr) {
3897 shouldUnlockSettings = true;
3898 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3899 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003900 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
3901 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003902 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01003903 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
3904 isStillCapture = true;
3905 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
3906 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003907
Emilian Peevaf8416e2021-02-04 17:52:43 -08003908 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003909 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003910 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
3911 isZslCapture = true;
3912 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01003913 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003914 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003915 res = parent->registerInFlight(halRequest->frame_number,
3916 totalNumBuffers, captureRequest->mResultExtras,
3917 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003918 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003919 expectedDurationInfo.minDuration,
3920 expectedDurationInfo.maxDuration,
3921 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003922 requestedPhysicalCameras, isStillCapture, isZslCapture,
3923 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003924 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07003925 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003926 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3927 ", burstId = %" PRId32 ".",
3928 __FUNCTION__,
3929 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3930 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08003931
3932 if (shouldUnlockSettings) {
3933 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
3934 }
3935
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003936 if (res != OK) {
3937 SET_ERR("RequestThread: Unable to register new in-flight request:"
3938 " %s (%d)", strerror(-res), res);
3939 return INVALID_OPERATION;
3940 }
3941 }
3942
3943 return OK;
3944}
3945
Igor Murashkin1e479c02013-09-06 16:55:14 -07003946CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003947 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003948 Mutex::Autolock al(mLatestRequestMutex);
3949
3950 ALOGV("RequestThread::%s", __FUNCTION__);
3951
3952 return mLatestRequest;
3953}
3954
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003955bool Camera3Device::RequestThread::isStreamPending(
3956 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003957 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003958 Mutex::Autolock l(mRequestLock);
3959
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003960 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003961 if (!nextRequest.submitted) {
3962 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
3963 if (stream == s) return true;
3964 }
3965 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003966 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003967 }
3968
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003969 for (const auto& request : mRequestQueue) {
3970 for (const auto& s : request->mOutputStreams) {
3971 if (stream == s) return true;
3972 }
3973 if (stream == request->mInputStream) return true;
3974 }
3975
3976 for (const auto& request : mRepeatingRequests) {
3977 for (const auto& s : request->mOutputStreams) {
3978 if (stream == s) return true;
3979 }
3980 if (stream == request->mInputStream) return true;
3981 }
3982
3983 return false;
3984}
Jianing Weicb0652e2014-03-12 18:29:36 -07003985
Emilian Peev40ead602017-09-26 15:46:36 +01003986bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
3987 ATRACE_CALL();
3988 Mutex::Autolock l(mRequestLock);
3989
3990 for (const auto& nextRequest : mNextRequests) {
3991 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
3992 if (s.first == streamId) {
3993 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
3994 if (it != s.second.end()) {
3995 return true;
3996 }
3997 }
3998 }
3999 }
4000
4001 for (const auto& request : mRequestQueue) {
4002 for (const auto& s : request->mOutputSurfaces) {
4003 if (s.first == streamId) {
4004 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4005 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004006 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004007 }
4008 }
4009 }
4010 }
4011
4012 for (const auto& request : mRepeatingRequests) {
4013 for (const auto& s : request->mOutputSurfaces) {
4014 if (s.first == streamId) {
4015 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4016 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004017 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004018 }
4019 }
4020 }
4021 }
4022
4023 return false;
4024}
4025
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004026void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4027 if (!mUseHalBufManager) {
4028 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4029 return;
4030 }
4031
4032 Mutex::Autolock pl(mPauseLock);
4033 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004034 mInterface->signalPipelineDrain(streamIds);
4035 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004036 }
4037 // If request thread is still busy, wait until paused then notify HAL
4038 mNotifyPipelineDrain = true;
4039 mStreamIdsToBeDrained = streamIds;
4040}
4041
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004042void Camera3Device::RequestThread::resetPipelineDrain() {
4043 Mutex::Autolock pl(mPauseLock);
4044 mNotifyPipelineDrain = false;
4045 mStreamIdsToBeDrained.clear();
4046}
4047
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004048void Camera3Device::RequestThread::clearPreviousRequest() {
4049 Mutex::Autolock l(mRequestLock);
4050 mPrevRequest.clear();
4051}
4052
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004053status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4054 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4055 ATRACE_CALL();
4056 Mutex::Autolock l(mTriggerMutex);
4057 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
4058 return BAD_VALUE;
4059 }
4060 mRotateAndCropOverride = rotateAndCropValue;
4061 return OK;
4062}
4063
Emilian Peeve23f1d92021-09-20 14:56:01 -07004064status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4065 ATRACE_CALL();
4066 Mutex::Autolock l(mTriggerMutex);
4067 mComposerOutput = composerSurfacePresent;
4068 return OK;
4069}
4070
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004071status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004072 ATRACE_CALL();
4073 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004074 if (muteMode != mCameraMute) {
4075 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004076 mCameraMuteChanged = true;
4077 }
4078 return OK;
4079}
4080
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004081nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004082 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004083 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004084 return mExpectedInflightDuration > kMinInflightDuration ?
4085 mExpectedInflightDuration : kMinInflightDuration;
4086}
4087
Emilian Peevaebbe412018-01-15 13:53:24 +00004088void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004089 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004090 if ((request == nullptr) || (halRequest == nullptr)) {
4091 ALOGE("%s: Invalid request!", __FUNCTION__);
4092 return;
4093 }
4094
4095 if (halRequest->num_physcam_settings > 0) {
4096 if (halRequest->physcam_id != nullptr) {
4097 delete [] halRequest->physcam_id;
4098 halRequest->physcam_id = nullptr;
4099 }
4100 if (halRequest->physcam_settings != nullptr) {
4101 auto it = ++(request->mSettingsList.begin());
4102 size_t i = 0;
4103 for (; it != request->mSettingsList.end(); it++, i++) {
4104 it->metadata.unlock(halRequest->physcam_settings[i]);
4105 }
4106 delete [] halRequest->physcam_settings;
4107 halRequest->physcam_settings = nullptr;
4108 }
4109 }
4110}
4111
Ravneet74cd3732022-03-30 05:33:03 +00004112status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4113 Mutex::Autolock il(mInterfaceLock);
4114 Mutex::Autolock l(mLock);
4115
4116 if (mCameraServiceWatchdog != NULL) {
4117 mCameraServiceWatchdog->setEnabled(enabled);
4118 }
4119
4120 return OK;
4121}
4122
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004123void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4124 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004125 return;
4126 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004127
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004128 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004129 // Skip the ones that have been submitted successfully.
4130 if (nextRequest.submitted) {
4131 continue;
4132 }
4133
4134 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004135 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4136 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004137
4138 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004139 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004140 }
4141
Emilian Peevaebbe412018-01-15 13:53:24 +00004142 cleanupPhysicalSettings(captureRequest, halRequest);
4143
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004144 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004145 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004146 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4147 }
4148
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004149 // No output buffer can be returned when using HAL buffer manager
4150 if (!mUseHalBufManager) {
4151 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4152 //Buffers that failed processing could still have
4153 //valid acquire fence.
4154 int acquireFence = (*outputBuffers)[i].acquire_fence;
4155 if (0 <= acquireFence) {
4156 close(acquireFence);
4157 outputBuffers->editItemAt(i).acquire_fence = -1;
4158 }
Emilian Peevf4816702020-04-03 15:44:51 -07004159 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004160 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4161 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004162 /*timestampIncreasing*/true, std::vector<size_t> (),
4163 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004164 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004165 }
4166
4167 if (sendRequestError) {
4168 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004169 sp<NotificationListener> listener = mListener.promote();
4170 if (listener != NULL) {
4171 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004172 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004173 captureRequest->mResultExtras);
4174 }
4175 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004176
4177 // Remove yet-to-be submitted inflight request from inflightMap
4178 {
4179 sp<Camera3Device> parent = mParent.promote();
4180 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004181 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004182 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4183 if (idx >= 0) {
4184 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4185 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4186 parent->removeInFlightMapEntryLocked(idx);
4187 }
4188 }
4189 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004190 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004191
4192 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004193 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004194}
4195
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004196void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004197 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004198 // Optimized a bit for the simple steady-state case (single repeating
4199 // request), to avoid putting that request in the queue temporarily.
4200 Mutex::Autolock l(mRequestLock);
4201
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004202 assert(mNextRequests.empty());
4203
4204 NextRequest nextRequest;
4205 nextRequest.captureRequest = waitForNextRequestLocked();
4206 if (nextRequest.captureRequest == nullptr) {
4207 return;
4208 }
4209
Emilian Peevf4816702020-04-03 15:44:51 -07004210 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004211 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004212 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004213
4214 // Wait for additional requests
4215 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4216
4217 for (size_t i = 1; i < batchSize; i++) {
4218 NextRequest additionalRequest;
4219 additionalRequest.captureRequest = waitForNextRequestLocked();
4220 if (additionalRequest.captureRequest == nullptr) {
4221 break;
4222 }
4223
Emilian Peevf4816702020-04-03 15:44:51 -07004224 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004225 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004226 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004227 }
4228
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004229 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004230 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004231 mNextRequests.size(), batchSize);
4232 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004233 }
4234
4235 return;
4236}
4237
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004238void Camera3Device::RequestThread::setRequestClearing() {
4239 Mutex::Autolock l(mRequestLock);
4240 mRequestClearing = true;
4241}
4242
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004243sp<Camera3Device::CaptureRequest>
4244 Camera3Device::RequestThread::waitForNextRequestLocked() {
4245 status_t res;
4246 sp<CaptureRequest> nextRequest;
4247
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004248 while (mRequestQueue.empty()) {
4249 if (!mRepeatingRequests.empty()) {
4250 // Always atomically enqueue all requests in a repeating request
4251 // list. Guarantees a complete in-sequence set of captures to
4252 // application.
4253 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004254 if (mFirstRepeating) {
4255 mFirstRepeating = false;
4256 } else {
4257 for (auto& request : requests) {
4258 // For repeating requests, override timestamp request using
4259 // the time a request is inserted into the request queue,
4260 // because the original repeating request will have an old
4261 // fixed timestamp.
4262 request->mRequestTimeNs = systemTime();
4263 }
4264 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004265 RequestList::const_iterator firstRequest =
4266 requests.begin();
4267 nextRequest = *firstRequest;
4268 mRequestQueue.insert(mRequestQueue.end(),
4269 ++firstRequest,
4270 requests.end());
4271 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004272
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004273 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004274
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004275 break;
4276 }
4277
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004278 if (!mRequestClearing) {
4279 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4280 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004281
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004282 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4283 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004284 Mutex::Autolock pl(mPauseLock);
4285 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004286 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004287 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004288 if (mNotifyPipelineDrain) {
4289 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4290 mNotifyPipelineDrain = false;
4291 mStreamIdsToBeDrained.clear();
4292 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004293 // Let the tracker know
4294 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4295 if (statusTracker != 0) {
4296 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4297 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004298 sp<Camera3Device> parent = mParent.promote();
4299 if (parent != nullptr) {
4300 parent->mRequestBufferSM.onRequestThreadPaused();
4301 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004302 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004303 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004304 // Stop waiting for now and let thread management happen
4305 return NULL;
4306 }
4307 }
4308
4309 if (nextRequest == NULL) {
4310 // Don't have a repeating request already in hand, so queue
4311 // must have an entry now.
4312 RequestList::iterator firstRequest =
4313 mRequestQueue.begin();
4314 nextRequest = *firstRequest;
4315 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004316 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4317 sp<NotificationListener> listener = mListener.promote();
4318 if (listener != NULL) {
4319 listener->notifyRequestQueueEmpty();
4320 }
4321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004322 }
4323
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004324 // In case we've been unpaused by setPaused clearing mDoPause, need to
4325 // update internal pause state (capture/setRepeatingRequest unpause
4326 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004327 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004328 if (mPaused) {
4329 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4330 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4331 if (statusTracker != 0) {
4332 statusTracker->markComponentActive(mStatusId);
4333 }
4334 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004335 mPaused = false;
4336
4337 // Check if we've reconfigured since last time, and reset the preview
4338 // request if so. Can't use 'NULL request == repeat' across configure calls.
4339 if (mReconfigured) {
4340 mPrevRequest.clear();
4341 mReconfigured = false;
4342 }
4343
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004344 if (nextRequest != NULL) {
4345 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004346 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4347 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004348
4349 // Since RequestThread::clear() removes buffers from the input stream,
4350 // get the right buffer here before unlocking mRequestLock
4351 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004352 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4353 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004354 if (res != OK) {
4355 // Can't get input buffer from gralloc queue - this could be due to
4356 // disconnected queue or other producer misbehavior, so not a fatal
4357 // error
4358 ALOGE("%s: Can't get input buffer, skipping request:"
4359 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004360
4361 sp<NotificationListener> listener = mListener.promote();
4362 if (listener != NULL) {
4363 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004364 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004365 nextRequest->mResultExtras);
4366 }
4367 return NULL;
4368 }
4369 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004370 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004371
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004372 return nextRequest;
4373}
4374
4375bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004376 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004377 status_t res;
4378 Mutex::Autolock l(mPauseLock);
4379 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004380 if (mPaused == false) {
4381 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004382 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004383 if (mNotifyPipelineDrain) {
4384 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4385 mNotifyPipelineDrain = false;
4386 mStreamIdsToBeDrained.clear();
4387 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004388 // Let the tracker know
4389 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4390 if (statusTracker != 0) {
4391 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4392 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004393 sp<Camera3Device> parent = mParent.promote();
4394 if (parent != nullptr) {
4395 parent->mRequestBufferSM.onRequestThreadPaused();
4396 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004397 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004398
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004399 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004400 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004401 return true;
4402 }
4403 }
4404 // We don't set mPaused to false here, because waitForNextRequest needs
4405 // to further manage the paused state in case of starvation.
4406 return false;
4407}
4408
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004409void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004410 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004411 // With work to do, mark thread as unpaused.
4412 // If paused by request (setPaused), don't resume, to avoid
4413 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004414 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004415 Mutex::Autolock p(mPauseLock);
4416 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004417 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4418 if (mPaused) {
4419 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4420 if (statusTracker != 0) {
4421 statusTracker->markComponentActive(mStatusId);
4422 }
4423 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004424 mPaused = false;
4425 }
4426}
4427
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004428void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4429 sp<Camera3Device> parent = mParent.promote();
4430 if (parent != NULL) {
4431 va_list args;
4432 va_start(args, fmt);
4433
4434 parent->setErrorStateV(fmt, args);
4435
4436 va_end(args);
4437 }
4438}
4439
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004440status_t Camera3Device::RequestThread::insertTriggers(
4441 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004442 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004443 Mutex::Autolock al(mTriggerMutex);
4444
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004445 sp<Camera3Device> parent = mParent.promote();
4446 if (parent == NULL) {
4447 CLOGE("RequestThread: Parent is gone");
4448 return DEAD_OBJECT;
4449 }
4450
Emilian Peevaebbe412018-01-15 13:53:24 +00004451 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004452 size_t count = mTriggerMap.size();
4453
4454 for (size_t i = 0; i < count; ++i) {
4455 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004456 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004457
4458 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4459 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4460 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004461 if (isAeTrigger) {
4462 request->mResultExtras.precaptureTriggerId = triggerId;
4463 mCurrentPreCaptureTriggerId = triggerId;
4464 } else {
4465 request->mResultExtras.afTriggerId = triggerId;
4466 mCurrentAfTriggerId = triggerId;
4467 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004468 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004469 }
4470
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004471 camera_metadata_entry entry = metadata.find(tag);
4472
4473 if (entry.count > 0) {
4474 /**
4475 * Already has an entry for this trigger in the request.
4476 * Rewrite it with our requested trigger value.
4477 */
4478 RequestTrigger oldTrigger = trigger;
4479
4480 oldTrigger.entryValue = entry.data.u8[0];
4481
4482 mTriggerReplacedMap.add(tag, oldTrigger);
4483 } else {
4484 /**
4485 * More typical, no trigger entry, so we just add it
4486 */
4487 mTriggerRemovedMap.add(tag, trigger);
4488 }
4489
4490 status_t res;
4491
4492 switch (trigger.getTagType()) {
4493 case TYPE_BYTE: {
4494 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4495 res = metadata.update(tag,
4496 &entryValue,
4497 /*count*/1);
4498 break;
4499 }
4500 case TYPE_INT32:
4501 res = metadata.update(tag,
4502 &trigger.entryValue,
4503 /*count*/1);
4504 break;
4505 default:
4506 ALOGE("%s: Type not supported: 0x%x",
4507 __FUNCTION__,
4508 trigger.getTagType());
4509 return INVALID_OPERATION;
4510 }
4511
4512 if (res != OK) {
4513 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4514 ", value %d", __FUNCTION__, trigger.getTagName(),
4515 trigger.entryValue);
4516 return res;
4517 }
4518
4519 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4520 trigger.getTagName(),
4521 trigger.entryValue);
4522 }
4523
4524 mTriggerMap.clear();
4525
4526 return count;
4527}
4528
4529status_t Camera3Device::RequestThread::removeTriggers(
4530 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004531 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004532 Mutex::Autolock al(mTriggerMutex);
4533
Emilian Peevaebbe412018-01-15 13:53:24 +00004534 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004535
4536 /**
4537 * Replace all old entries with their old values.
4538 */
4539 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4540 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4541
4542 status_t res;
4543
4544 uint32_t tag = trigger.metadataTag;
4545 switch (trigger.getTagType()) {
4546 case TYPE_BYTE: {
4547 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4548 res = metadata.update(tag,
4549 &entryValue,
4550 /*count*/1);
4551 break;
4552 }
4553 case TYPE_INT32:
4554 res = metadata.update(tag,
4555 &trigger.entryValue,
4556 /*count*/1);
4557 break;
4558 default:
4559 ALOGE("%s: Type not supported: 0x%x",
4560 __FUNCTION__,
4561 trigger.getTagType());
4562 return INVALID_OPERATION;
4563 }
4564
4565 if (res != OK) {
4566 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4567 ", trigger value %d", __FUNCTION__,
4568 trigger.getTagName(), trigger.entryValue);
4569 return res;
4570 }
4571 }
4572 mTriggerReplacedMap.clear();
4573
4574 /**
4575 * Remove all new entries.
4576 */
4577 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4578 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4579 status_t res = metadata.erase(trigger.metadataTag);
4580
4581 if (res != OK) {
4582 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4583 ", trigger value %d", __FUNCTION__,
4584 trigger.getTagName(), trigger.entryValue);
4585 return res;
4586 }
4587 }
4588 mTriggerRemovedMap.clear();
4589
4590 return OK;
4591}
4592
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004593status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004594 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004595 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004596 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004597 status_t res;
4598
Emilian Peevaebbe412018-01-15 13:53:24 +00004599 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004600
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004601 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004602 // exists
4603 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4604 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4605 if (afTrigger.count > 0 &&
4606 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4607 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004608 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004609 if (res != OK) return res;
4610 }
4611
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004612 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004613 // if none already exists
4614 camera_metadata_entry pcTrigger =
4615 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4616 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4617 if (pcTrigger.count > 0 &&
4618 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4619 pcId.count == 0) {
4620 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004621 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004622 if (res != OK) return res;
4623 }
4624
4625 return OK;
4626}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004627
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004628bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
4629 const sp<CaptureRequest> &request) {
4630 ATRACE_CALL();
4631
4632 if (request->mRotateAndCropAuto) {
4633 Mutex::Autolock l(mTriggerMutex);
4634 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4635
4636 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4637 if (rotateAndCropEntry.count > 0) {
4638 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
4639 return false;
4640 } else {
4641 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
4642 return true;
4643 }
4644 } else {
4645 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
4646 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4647 &rotateAndCrop_u8, 1);
4648 return true;
4649 }
4650 }
4651 return false;
4652}
4653
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004654bool Camera3Device::RequestThread::overrideTestPattern(
4655 const sp<CaptureRequest> &request) {
4656 ATRACE_CALL();
4657
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004658 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004659
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004660 Mutex::Autolock l(mTriggerMutex);
4661
4662 bool changed = false;
4663
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004664 // For a multi-camera, the physical cameras support the same set of
4665 // test pattern modes as the logical camera.
4666 for (auto& settings : request->mSettingsList) {
4667 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004668
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004669 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4670 int32_t testPatternData[4] = {
4671 settings.mOriginalTestPatternData[0],
4672 settings.mOriginalTestPatternData[1],
4673 settings.mOriginalTestPatternData[2],
4674 settings.mOriginalTestPatternData[3]
4675 };
4676 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4677 testPatternMode = mCameraMute;
4678 testPatternData[0] = 0;
4679 testPatternData[1] = 0;
4680 testPatternData[2] = 0;
4681 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004682 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004683
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004684 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4685 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4686 if (testPatternEntry.count > 0) {
4687 if (testPatternEntry.data.i32[0] != testPatternMode) {
4688 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004689 changed = true;
4690 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004691 } else if (supportTestPatternModeKey) {
4692 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4693 &testPatternMode, 1);
4694 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004695 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004696
4697 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4698 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4699 if (testPatternColor.count >= 4) {
4700 for (size_t i = 0; i < 4; i++) {
4701 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4702 testPatternColor.data.i32[i] = testPatternData[i];
4703 changed = true;
4704 }
4705 }
4706 } else if (supportTestPatternDataKey) {
4707 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4708 testPatternData, 4);
4709 changed = true;
4710 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004711 }
4712
4713 return changed;
4714}
4715
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004716status_t Camera3Device::RequestThread::setHalInterface(
4717 sp<HalInterface> newHalInterface) {
4718 if (newHalInterface.get() == nullptr) {
4719 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4720 return DEAD_OBJECT;
4721 }
4722
4723 mInterface = newHalInterface;
4724
4725 return OK;
4726}
4727
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004728/**
4729 * PreparerThread inner class methods
4730 */
4731
4732Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004733 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004734 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004735}
4736
4737Camera3Device::PreparerThread::~PreparerThread() {
4738 Thread::requestExitAndWait();
4739 if (mCurrentStream != nullptr) {
4740 mCurrentStream->cancelPrepare();
4741 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4742 mCurrentStream.clear();
4743 }
4744 clear();
4745}
4746
Ruben Brunkc78ac262015-08-13 17:58:46 -07004747status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004748 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004749 status_t res;
4750
4751 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004752 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004753
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004754 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004755 if (res == OK) {
4756 // No preparation needed, fire listener right off
4757 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004758 if (listener != NULL) {
4759 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004760 }
4761 return OK;
4762 } else if (res != NOT_ENOUGH_DATA) {
4763 return res;
4764 }
4765
4766 // Need to prepare, start up thread if necessary
4767 if (!mActive) {
4768 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4769 // isn't running
4770 Thread::requestExitAndWait();
4771 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4772 if (res != OK) {
4773 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004774 if (listener != NULL) {
4775 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004776 }
4777 return res;
4778 }
4779 mCancelNow = false;
4780 mActive = true;
4781 ALOGV("%s: Preparer stream started", __FUNCTION__);
4782 }
4783
4784 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004785 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004786 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4787
4788 return OK;
4789}
4790
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004791void Camera3Device::PreparerThread::pause() {
4792 ATRACE_CALL();
4793
4794 Mutex::Autolock l(mLock);
4795
4796 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
4797 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
4798 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
4799 int currentMaxCount = mCurrentMaxCount;
4800 mPendingStreams.clear();
4801 mCancelNow = true;
4802 while (mActive) {
4803 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
4804 if (res == TIMED_OUT) {
4805 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
4806 return;
4807 } else if (res != OK) {
4808 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
4809 return;
4810 }
4811 }
4812
4813 //Check whether the prepare thread was able to complete the current
4814 //stream. In case work is still pending emplace it along with the rest
4815 //of the streams in the pending list.
4816 if (currentStream != nullptr) {
4817 if (!mCurrentPrepareComplete) {
4818 pendingStreams.emplace(currentMaxCount, currentStream);
4819 }
4820 }
4821
4822 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
4823 for (const auto& it : mPendingStreams) {
4824 it.second->cancelPrepare();
4825 }
4826}
4827
4828status_t Camera3Device::PreparerThread::resume() {
4829 ATRACE_CALL();
4830 status_t res;
4831
4832 Mutex::Autolock l(mLock);
4833 sp<NotificationListener> listener = mListener.promote();
4834
4835 if (mActive) {
4836 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
4837 return NO_INIT;
4838 }
4839
4840 auto it = mPendingStreams.begin();
4841 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004842 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004843 if (res == OK) {
4844 if (listener != NULL) {
4845 listener->notifyPrepared(it->second->getId());
4846 }
4847 it = mPendingStreams.erase(it);
4848 } else if (res != NOT_ENOUGH_DATA) {
4849 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
4850 res, strerror(-res));
4851 it = mPendingStreams.erase(it);
4852 } else {
4853 it++;
4854 }
4855 }
4856
4857 if (mPendingStreams.empty()) {
4858 return OK;
4859 }
4860
4861 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4862 if (res != OK) {
4863 ALOGE("%s: Unable to start preparer stream: %d (%s)",
4864 __FUNCTION__, res, strerror(-res));
4865 return res;
4866 }
4867 mCancelNow = false;
4868 mActive = true;
4869 ALOGV("%s: Preparer stream started", __FUNCTION__);
4870
4871 return OK;
4872}
4873
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004874status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004875 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004876 Mutex::Autolock l(mLock);
4877
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004878 for (const auto& it : mPendingStreams) {
4879 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004880 }
4881 mPendingStreams.clear();
4882 mCancelNow = true;
4883
4884 return OK;
4885}
4886
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004887void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004888 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004889 Mutex::Autolock l(mLock);
4890 mListener = listener;
4891}
4892
4893bool Camera3Device::PreparerThread::threadLoop() {
4894 status_t res;
4895 {
4896 Mutex::Autolock l(mLock);
4897 if (mCurrentStream == nullptr) {
4898 // End thread if done with work
4899 if (mPendingStreams.empty()) {
4900 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4901 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4902 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4903 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004904 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004905 return false;
4906 }
4907
4908 // Get next stream to prepare
4909 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004910 mCurrentStream = it->second;
4911 mCurrentMaxCount = it->first;
4912 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004913 mPendingStreams.erase(it);
4914 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4915 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4916 } else if (mCancelNow) {
4917 mCurrentStream->cancelPrepare();
4918 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4919 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4920 mCurrentStream.clear();
4921 mCancelNow = false;
4922 return true;
4923 }
4924 }
4925
4926 res = mCurrentStream->prepareNextBuffer();
4927 if (res == NOT_ENOUGH_DATA) return true;
4928 if (res != OK) {
4929 // Something bad happened; try to recover by cancelling prepare and
4930 // signalling listener anyway
4931 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4932 mCurrentStream->getId(), res, strerror(-res));
4933 mCurrentStream->cancelPrepare();
4934 }
4935
4936 // This stream has finished, notify listener
4937 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004938 sp<NotificationListener> listener = mListener.promote();
4939 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004940 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4941 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004942 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004943 }
4944
4945 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4946 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004947 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004948
4949 return true;
4950}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004951
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004952status_t Camera3Device::RequestBufferStateMachine::initialize(
4953 sp<camera3::StatusTracker> statusTracker) {
4954 if (statusTracker == nullptr) {
4955 ALOGE("%s: statusTracker is null", __FUNCTION__);
4956 return BAD_VALUE;
4957 }
4958
4959 std::lock_guard<std::mutex> lock(mLock);
4960 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004961 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004962 return OK;
4963}
4964
4965bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
4966 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004967 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004968 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004969 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004970 return true;
4971 }
4972 return false;
4973}
4974
4975void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
4976 std::lock_guard<std::mutex> lock(mLock);
4977 if (!mRequestBufferOngoing) {
4978 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
4979 return;
4980 }
4981 mRequestBufferOngoing = false;
4982 if (mStatus == RB_STATUS_PENDING_STOP) {
4983 checkSwitchToStopLocked();
4984 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08004985 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004986}
4987
4988void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
4989 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004990 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004991 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004992 return;
4993}
4994
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004995void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004996 std::lock_guard<std::mutex> lock(mLock);
4997 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004998 // inflight map register actually happens in prepareHalRequest now, but it is close enough
4999 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005000 mInflightMapEmpty = false;
5001 if (mStatus == RB_STATUS_STOPPED) {
5002 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005003 }
5004 return;
5005}
5006
5007void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5008 std::lock_guard<std::mutex> lock(mLock);
5009 mRequestThreadPaused = true;
5010 if (mStatus == RB_STATUS_PENDING_STOP) {
5011 checkSwitchToStopLocked();
5012 }
5013 return;
5014}
5015
5016void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5017 std::lock_guard<std::mutex> lock(mLock);
5018 mInflightMapEmpty = true;
5019 if (mStatus == RB_STATUS_PENDING_STOP) {
5020 checkSwitchToStopLocked();
5021 }
5022 return;
5023}
5024
5025void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5026 std::lock_guard<std::mutex> lock(mLock);
5027 if (!checkSwitchToStopLocked()) {
5028 mStatus = RB_STATUS_PENDING_STOP;
5029 }
5030 return;
5031}
5032
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005033bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5034 std::lock_guard<std::mutex> lock(mLock);
5035 if (mRequestBufferOngoing) {
5036 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5037 __FUNCTION__);
5038 return false;
5039 }
5040 mSwitchedToOffline = true;
5041 mInflightMapEmpty = true;
5042 mRequestThreadPaused = true;
5043 mStatus = RB_STATUS_STOPPED;
5044 return true;
5045}
5046
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005047void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5048 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5049 if (statusTracker != nullptr) {
5050 if (active) {
5051 statusTracker->markComponentActive(mRequestBufferStatusId);
5052 } else {
5053 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5054 }
5055 }
5056}
5057
5058bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5059 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5060 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005061 return true;
5062 }
5063 return false;
5064}
5065
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005066bool Camera3Device::startRequestBuffer() {
5067 return mRequestBufferSM.startRequestBuffer();
5068}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005069
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005070void Camera3Device::endRequestBuffer() {
5071 mRequestBufferSM.endRequestBuffer();
5072}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005073
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005074nsecs_t Camera3Device::getWaitDuration() {
5075 return kBaseGetBufferWait + getExpectedInFlightDuration();
5076}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005077
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005078void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5079 mInterface->getInflightBufferKeys(out);
5080}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005081
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005082void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5083 mInterface->getInflightRequestBufferKeys(out);
5084}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005085
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005086std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5087 std::vector<sp<Camera3StreamInterface>> ret;
5088 bool hasInputStream = mInputStream != nullptr;
5089 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5090 if (hasInputStream) {
5091 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005092 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005093 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5094 ret.push_back(mOutputStreams[i]);
5095 }
5096 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5097 ret.push_back(mDeletedStreams[i]);
5098 }
5099 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005100}
5101
Emilian Peevcc0b7952020-01-07 13:54:47 -08005102void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5103 ATRACE_CALL();
5104
5105 if (offlineStreamIds == nullptr) {
5106 return;
5107 }
5108
5109 Mutex::Autolock il(mInterfaceLock);
5110
5111 auto streamIds = mOutputStreams.getStreamIds();
5112 bool hasInputStream = mInputStream != nullptr;
5113 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5114 offlineStreamIds->push_back(mInputStream->getId());
5115 }
5116
5117 for (const auto & streamId : streamIds) {
5118 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5119 // Streams that use the camera buffer manager are currently not supported in
5120 // offline mode
5121 if (stream->getOfflineProcessingSupport() &&
5122 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5123 offlineStreamIds->push_back(streamId);
5124 }
5125 }
5126}
5127
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005128status_t Camera3Device::setRotateAndCropAutoBehavior(
5129 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5130 ATRACE_CALL();
5131 Mutex::Autolock il(mInterfaceLock);
5132 Mutex::Autolock l(mLock);
5133 if (mRequestThread == nullptr) {
5134 return INVALID_OPERATION;
5135 }
5136 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5137}
5138
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005139bool Camera3Device::supportsCameraMute() {
5140 Mutex::Autolock il(mInterfaceLock);
5141 Mutex::Autolock l(mLock);
5142
5143 return mSupportCameraMute;
5144}
5145
5146status_t Camera3Device::setCameraMute(bool enabled) {
5147 ATRACE_CALL();
5148 Mutex::Autolock il(mInterfaceLock);
5149 Mutex::Autolock l(mLock);
5150
5151 if (mRequestThread == nullptr || !mSupportCameraMute) {
5152 return INVALID_OPERATION;
5153 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005154 int32_t muteMode =
5155 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5156 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5157 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5158 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005159}
5160
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005161status_t Camera3Device::injectCamera(const String8& injectedCamId,
5162 sp<CameraProviderManager> manager) {
5163 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5164 ATRACE_CALL();
5165 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005166 // When the camera device is active, injectCamera() and stopInjection() will call
5167 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5168 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5169 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5170 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5171 // waitUntilStateThenRelock().
5172 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005173
5174 status_t res = NO_ERROR;
5175 if (mInjectionMethods->isInjecting()) {
5176 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5177 return OK;
5178 } else {
5179 res = mInjectionMethods->stopInjection();
5180 if (res != OK) {
5181 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5182 __FUNCTION__, res);
5183 return res;
5184 }
5185 }
5186 }
5187
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005188 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005189 if (res != OK) {
5190 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5191 __FUNCTION__, res);
5192 return res;
5193 }
5194
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005195 // When the second display of android is cast to the remote device, and the opened camera is
5196 // also cast to the second display, in this case, because the camera has configured the streams
5197 // at this time, we can directly call injectCamera() to replace the internal camera with
5198 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005199 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5200 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5201
5202 camera3::camera_stream_configuration injectionConfig;
5203 std::vector<uint32_t> injectionBufferSizes;
5204 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5205 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5206 || injectionBufferSizes.size() <= 0) {
5207 ALOGE("Failed to inject camera due to abandoned configuration! "
5208 "mOperatingMode: %d injectionConfig.num_streams: %d "
5209 "injectionBufferSizes.size(): %zu", mOperatingMode,
5210 injectionConfig.num_streams, injectionBufferSizes.size());
5211 return DEAD_OBJECT;
5212 }
5213
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005214 res = mInjectionMethods->injectCamera(
5215 injectionConfig, injectionBufferSizes);
5216 if (res != OK) {
5217 ALOGE("Can't finish inject camera process!");
5218 return res;
5219 }
5220 }
5221
5222 return OK;
5223}
5224
5225status_t Camera3Device::stopInjection() {
5226 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5227 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005228 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005229 return mInjectionMethods->stopInjection();
5230}
5231
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005232}; // namespace android