blob: 318889248186ad3e810077aeb335b66c1ac2e76d [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
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080052#include <android/hardware/camera2/ICameraDeviceUser.h>
53
Igor Murashkinff3e31d2013-10-23 16:40:06 -070054#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070055#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070056#include "device3/Camera3Device.h"
57#include "device3/Camera3OutputStream.h"
58#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070059#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070060#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070061#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070062#include "utils/CameraThreadState.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080063
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080064#include <tuple>
65
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080067using namespace android::hardware::camera;
68using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069
70namespace android {
71
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080072Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080073 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080074 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070075 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070076 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070077 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070078 mUsePartialResult(false),
79 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080080 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070081 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070082 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070083 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070084 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070085 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070086 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000087 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010088 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070089 mLastTemplateId(-1),
90 mNeedFixupMonochromeTags(false)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091{
92 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094}
95
96Camera3Device::~Camera3Device()
97{
98 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080099 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700100 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101}
102
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800103const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800104 return mId;
105}
106
Emilian Peevbd8c5032018-02-14 23:05:40 +0000107status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800108 ATRACE_CALL();
109 Mutex::Autolock il(mInterfaceLock);
110 Mutex::Autolock l(mLock);
111
112 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
113 if (mStatus != STATUS_UNINITIALIZED) {
114 CLOGE("Already initialized!");
115 return INVALID_OPERATION;
116 }
117 if (manager == nullptr) return INVALID_OPERATION;
118
119 sp<ICameraDeviceSession> session;
120 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800121 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800122 /*out*/ &session);
123 ATRACE_END();
124 if (res != OK) {
125 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
126 return res;
127 }
128
Steven Moreland5ff9c912017-03-09 23:13:00 -0800129 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800130 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700131 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800132 session->close();
133 return res;
134 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800135
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700136 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700137 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700138 if (isLogical) {
139 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700140 res = manager->getCameraCharacteristics(
141 physicalId, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700142 if (res != OK) {
143 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
144 physicalId.c_str(), strerror(-res), res);
145 session->close();
146 return res;
147 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700148
149 if (DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId])) {
150 mDistortionMappers[physicalId].setupStaticInfo(mPhysicalDeviceInfoMap[physicalId]);
151 if (res != OK) {
152 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
153 "correction", physicalId.c_str());
154 session->close();
155 return res;
156 }
157 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700158 }
159 }
160
Yifan Hongf79b5542017-04-11 14:44:25 -0700161 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700162 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
163 [&queue](const auto& descriptor) {
164 queue = std::make_shared<RequestMetadataQueue>(descriptor);
165 if (!queue->isValid() || queue->availableToWrite() <= 0) {
166 ALOGE("HAL returns empty request metadata fmq, not use it");
167 queue = nullptr;
168 // don't use the queue onwards.
169 }
170 });
171 if (!requestQueueRet.isOk()) {
172 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
173 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700174 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700175 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700176
177 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700178 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700179 [&resQueue](const auto& descriptor) {
180 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
181 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700182 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700183 resQueue = nullptr;
184 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700185 }
186 });
187 if (!resultQueueRet.isOk()) {
188 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
189 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700190 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700191 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700192 IF_ALOGV() {
193 session->interfaceChain([](
194 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
195 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800196 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700197 ALOGV(" %s", iface.c_str());
198 }
199 });
200 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700201
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800202 camera_metadata_entry bufMgrMode =
203 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
204 if (bufMgrMode.count > 0) {
205 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
206 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
207 }
208
209 mInterface = new HalInterface(session, queue, mUseHalBufManager);
Emilian Peev71c73a22017-03-21 16:35:51 +0000210 std::string providerType;
211 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000212 mTagMonitor.initialize(mVendorTagId);
213 if (!monitorTags.isEmpty()) {
214 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
215 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800216
Shuzhen Wang268a1362018-10-16 16:32:59 -0700217 // Metadata tags needs fixup for monochrome camera device version less
218 // than 3.5.
219 hardware::hidl_version maxVersion{0,0};
220 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
221 if (res != OK) {
222 ALOGE("%s: Error in getting camera device version id: %s (%d)",
223 __FUNCTION__, strerror(-res), res);
224 return res;
225 }
226 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
227 maxVersion.get_major(), maxVersion.get_minor());
228
229 bool isMonochrome = false;
230 camera_metadata_entry_t entry = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
231 for (size_t i = 0; i < entry.count; i++) {
232 uint8_t capability = entry.data.u8[i];
233 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
234 isMonochrome = true;
235 }
236 }
237 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
238
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800239 return initializeCommonLocked();
240}
241
242status_t Camera3Device::initializeCommonLocked() {
243
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700244 /** Start up status tracker thread */
245 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800246 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700247 if (res != OK) {
248 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
249 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800250 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 mStatusTracker.clear();
252 return res;
253 }
254
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700255 /** Register in-flight map to the status tracker */
256 mInFlightStatusId = mStatusTracker->addComponent();
257
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700258 if (mUseHalBufManager) {
259 res = mRequestBufferSM.initialize(mStatusTracker);
260 if (res != OK) {
261 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
262 strerror(-res), res);
263 mInterface->close();
264 mStatusTracker.clear();
265 return res;
266 }
267 }
268
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800269 /** Create buffer manager */
270 mBufferManager = new Camera3BufferManager();
271
272 Vector<int32_t> sessionParamKeys;
273 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
274 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
275 if (sessionKeysEntry.count > 0) {
276 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
277 }
278
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700279 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700280 mRequestThread = new RequestThread(
281 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800282 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700284 SET_ERR_L("Unable to start request queue thread: %s (%d)",
285 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800286 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800287 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800288 return res;
289 }
290
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700291 mPreparerThread = new PreparerThread();
292
Ruben Brunk183f0562015-08-12 12:55:02 -0700293 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800294 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700295 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700296 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700297 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800298
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800299 // Measure the clock domain offset between camera and video/hw_composer
300 camera_metadata_entry timestampSource =
301 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
302 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
303 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
304 mTimestampOffset = getMonoToBoottimeOffset();
305 }
306
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700307 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100308 camera_metadata_entry partialResultsCount =
309 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
310 if (partialResultsCount.count > 0) {
311 mNumPartialResults = partialResultsCount.data.i32[0];
312 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700313 }
314
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700315 camera_metadata_entry configs =
316 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
317 for (uint32_t i = 0; i < configs.count; i += 4) {
318 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
319 configs.data.i32[i + 3] ==
320 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
321 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
322 configs.data.i32[i + 2]));
323 }
324 }
325
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700326 if (DistortionMapper::isDistortionSupported(mDeviceInfo)) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700327 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700328 if (res != OK) {
329 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
330 return res;
331 }
332 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800333 return OK;
334}
335
336status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700337 return disconnectImpl();
338}
339
340status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800341 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700342 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800343
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700344 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800345
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700346 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700347 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700348 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700349 {
350 Mutex::Autolock l(mLock);
351 if (mStatus == STATUS_UNINITIALIZED) return res;
352
353 if (mStatus == STATUS_ACTIVE ||
354 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
355 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700356 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700357 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700358 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700360 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700361 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700362 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
363 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700364 // Continue to close device even in case of error
365 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700366 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800368
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700369 if (mStatus == STATUS_ERROR) {
370 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700371 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700372
373 if (mStatusTracker != NULL) {
374 mStatusTracker->requestExit();
375 }
376
377 if (mRequestThread != NULL) {
378 mRequestThread->requestExit();
379 }
380
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700381 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
382 for (size_t i = 0; i < mOutputStreams.size(); i++) {
383 streams.push_back(mOutputStreams[i]);
384 }
385 if (mInputStream != nullptr) {
386 streams.push_back(mInputStream);
387 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700388 }
389
390 // Joining done without holding mLock, otherwise deadlocks may ensue
391 // as the threads try to access parent state
392 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
393 // HAL may be in a bad state, so waiting for request thread
394 // (which may be stuck in the HAL processCaptureRequest call)
395 // could be dangerous.
396 mRequestThread->join();
397 }
398
399 if (mStatusTracker != NULL) {
400 mStatusTracker->join();
401 }
402
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800403 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404 {
405 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406 mRequestThread.clear();
Emilian Peev2843c362018-09-26 08:49:40 +0100407 Mutex::Autolock stLock(mTrackerLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700408 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800409 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700410 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800411
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700412 // Call close without internal mutex held, as the HAL close may need to
413 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800414 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700415
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700416 flushInflightRequests();
417
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700418 {
419 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800420 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700421 mOutputStreams.clear();
422 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700423 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700424 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700425 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700426 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800427
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700428 for (auto& weakStream : streams) {
429 sp<Camera3StreamInterface> stream = weakStream.promote();
430 if (stream != nullptr) {
431 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
432 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
433 }
434 }
435
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700436 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700437 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800438}
439
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440// For dumping/debugging only -
441// try to acquire a lock a few times, eventually give up to proceed with
442// debug/dump operations
443bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
444 bool gotLock = false;
445 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
446 if (lock.tryLock() == NO_ERROR) {
447 gotLock = true;
448 break;
449 } else {
450 usleep(kDumpSleepDuration);
451 }
452 }
453 return gotLock;
454}
455
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700456Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
457 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100458 const int STREAM_CONFIGURATION_SIZE = 4;
459 const int STREAM_FORMAT_OFFSET = 0;
460 const int STREAM_WIDTH_OFFSET = 1;
461 const int STREAM_HEIGHT_OFFSET = 2;
462 const int STREAM_IS_INPUT_OFFSET = 3;
463 camera_metadata_ro_entry_t availableStreamConfigs =
464 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
465 if (availableStreamConfigs.count == 0 ||
466 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
467 return Size(0, 0);
468 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700469
Emilian Peev08dd2452017-04-06 16:55:14 +0100470 // Get max jpeg size (area-wise).
471 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
472 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
473 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
474 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
475 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
476 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
477 && format == HAL_PIXEL_FORMAT_BLOB &&
478 (width * height > maxJpegWidth * maxJpegHeight)) {
479 maxJpegWidth = width;
480 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700481 }
482 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100483
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700484 return Size(maxJpegWidth, maxJpegHeight);
485}
486
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800487nsecs_t Camera3Device::getMonoToBoottimeOffset() {
488 // try three times to get the clock offset, choose the one
489 // with the minimum gap in measurements.
490 const int tries = 3;
491 nsecs_t bestGap, measured;
492 for (int i = 0; i < tries; ++i) {
493 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
494 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
495 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
496 const nsecs_t gap = tmono2 - tmono;
497 if (i == 0 || gap < bestGap) {
498 bestGap = gap;
499 measured = tbase - ((tmono + tmono2) >> 1);
500 }
501 }
502 return measured;
503}
504
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800505hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
506 int frameworkFormat) {
507 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
508}
509
510DataspaceFlags Camera3Device::mapToHidlDataspace(
511 android_dataspace dataSpace) {
512 return dataSpace;
513}
514
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700515BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100516 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700517 return usage;
518}
519
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800520StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
521 switch (rotation) {
522 case CAMERA3_STREAM_ROTATION_0:
523 return StreamRotation::ROTATION_0;
524 case CAMERA3_STREAM_ROTATION_90:
525 return StreamRotation::ROTATION_90;
526 case CAMERA3_STREAM_ROTATION_180:
527 return StreamRotation::ROTATION_180;
528 case CAMERA3_STREAM_ROTATION_270:
529 return StreamRotation::ROTATION_270;
530 }
531 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
532 return StreamRotation::ROTATION_0;
533}
534
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800535status_t Camera3Device::mapToStreamConfigurationMode(
536 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
537 if (mode == nullptr) return BAD_VALUE;
538 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
539 switch(operationMode) {
540 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
541 *mode = StreamConfigurationMode::NORMAL_MODE;
542 break;
543 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
544 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
545 break;
546 default:
547 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
548 return BAD_VALUE;
549 }
550 } else {
551 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800552 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800553 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800554}
555
556camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
557 switch (status) {
558 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
559 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
560 }
561 return CAMERA3_BUFFER_STATUS_ERROR;
562}
563
564int Camera3Device::mapToFrameworkFormat(
565 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
566 return static_cast<uint32_t>(pixelFormat);
567}
568
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700569android_dataspace Camera3Device::mapToFrameworkDataspace(
570 DataspaceFlags dataSpace) {
571 return static_cast<android_dataspace>(dataSpace);
572}
573
Emilian Peev050f5dc2017-05-18 14:43:56 +0100574uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700575 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700576 return usage;
577}
578
Emilian Peev050f5dc2017-05-18 14:43:56 +0100579uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700580 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700581 return usage;
582}
583
Zhijun Hef7da0962014-04-24 13:27:56 -0700584ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700585 // Get max jpeg size (area-wise).
586 Size maxJpegResolution = getMaxJpegResolution();
587 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800588 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
589 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700590 return BAD_VALUE;
591 }
592
Zhijun Hef7da0962014-04-24 13:27:56 -0700593 // Get max jpeg buffer size
594 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700595 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
596 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800597 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
598 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700599 return BAD_VALUE;
600 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700601 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800602 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700603
604 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700605 float scaleFactor = ((float) (width * height)) /
606 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800607 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
608 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700609 if (jpegBufferSize > maxJpegBufferSize) {
610 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700611 }
612
613 return jpegBufferSize;
614}
615
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700616ssize_t Camera3Device::getPointCloudBufferSize() const {
617 const int FLOATS_PER_POINT=4;
618 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
619 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800620 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
621 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700622 return BAD_VALUE;
623 }
624 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
625 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
626 return maxBytesForPointCloud;
627}
628
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800629ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800630 const int PER_CONFIGURATION_SIZE = 3;
631 const int WIDTH_OFFSET = 0;
632 const int HEIGHT_OFFSET = 1;
633 const int SIZE_OFFSET = 2;
634 camera_metadata_ro_entry rawOpaqueSizes =
635 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800636 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800637 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800638 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
639 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800640 return BAD_VALUE;
641 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700642
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800643 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
644 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
645 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
646 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
647 }
648 }
649
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800650 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
651 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800652 return BAD_VALUE;
653}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700654
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
656 ATRACE_CALL();
657 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700658
659 // Try to lock, but continue in case of failure (to avoid blocking in
660 // deadlocks)
661 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
662 bool gotLock = tryLockSpinRightRound(mLock);
663
664 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800665 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
666 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700667 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800668 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
669 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800671 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700672
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800673 String16 templatesOption("-t");
674 int n = args.size();
675 for (int i = 0; i < n; i++) {
676 if (args[i] == templatesOption) {
677 dumpTemplates = true;
678 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000679 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700680 if (i + 1 < n) {
681 String8 monitorTags = String8(args[i + 1]);
682 if (monitorTags == "off") {
683 mTagMonitor.disableMonitoring();
684 } else {
685 mTagMonitor.parseTagsToMonitor(monitorTags);
686 }
687 } else {
688 mTagMonitor.disableMonitoring();
689 }
690 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800691 }
692
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800693 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800694
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695 const char *status =
696 mStatus == STATUS_ERROR ? "ERROR" :
697 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
699 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800700 mStatus == STATUS_ACTIVE ? "ACTIVE" :
701 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700702
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800703 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700704 if (mStatus == STATUS_ERROR) {
705 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
706 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800708 const char *mode =
709 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
710 mOperatingMode == static_cast<int>(
711 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
712 "CUSTOM";
713 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714
715 if (mInputStream != NULL) {
716 write(fd, lines.string(), lines.size());
717 mInputStream->dump(fd, args);
718 } else {
719 lines.appendFormat(" No input stream.\n");
720 write(fd, lines.string(), lines.size());
721 }
722 for (size_t i = 0; i < mOutputStreams.size(); i++) {
723 mOutputStreams[i]->dump(fd,args);
724 }
725
Zhijun He431503c2016-03-07 17:30:16 -0800726 if (mBufferManager != NULL) {
727 lines = String8(" Camera3 Buffer Manager:\n");
728 write(fd, lines.string(), lines.size());
729 mBufferManager->dump(fd, args);
730 }
Zhijun He125684a2015-12-26 15:07:30 -0800731
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700732 lines = String8(" In-flight requests:\n");
733 if (mInFlightMap.size() == 0) {
734 lines.append(" None\n");
735 } else {
736 for (size_t i = 0; i < mInFlightMap.size(); i++) {
737 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700738 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700739 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800740 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700741 r.numBuffersLeft);
742 }
743 }
744 write(fd, lines.string(), lines.size());
745
Shuzhen Wang686f6442017-06-20 16:16:04 -0700746 if (mRequestThread != NULL) {
747 mRequestThread->dumpCaptureRequestLatency(fd,
748 " ProcessCaptureRequest latency histogram:");
749 }
750
Igor Murashkin1e479c02013-09-06 16:55:14 -0700751 {
752 lines = String8(" Last request sent:\n");
753 write(fd, lines.string(), lines.size());
754
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700755 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700756 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
757 }
758
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800759 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800760 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800761 "TEMPLATE_PREVIEW",
762 "TEMPLATE_STILL_CAPTURE",
763 "TEMPLATE_VIDEO_RECORD",
764 "TEMPLATE_VIDEO_SNAPSHOT",
765 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800766 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800767 };
768
769 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800770 camera_metadata_t *templateRequest = nullptr;
771 mInterface->constructDefaultRequestSettings(
772 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800773 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800774 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800775 lines.append(" Not supported\n");
776 write(fd, lines.string(), lines.size());
777 } else {
778 write(fd, lines.string(), lines.size());
779 dump_indented_camera_metadata(templateRequest,
780 fd, /*verbosity*/2, /*indentation*/8);
781 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800782 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800783 }
784 }
785
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700786 mTagMonitor.dumpMonitoredMetadata(fd);
787
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800788 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800789 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800790 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800791 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800793
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700794 if (gotLock) mLock.unlock();
795 if (gotInterfaceLock) mInterfaceLock.unlock();
796
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797 return OK;
798}
799
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700800const CameraMetadata& Camera3Device::info(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800801 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
803 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700804 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 mStatus == STATUS_ERROR ?
806 "when in error state" : "before init");
807 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700808 if (physicalId.isEmpty()) {
809 return mDeviceInfo;
810 } else {
811 std::string id(physicalId.c_str());
812 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
813 return mPhysicalDeviceInfoMap.at(id);
814 } else {
815 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
816 return mDeviceInfo;
817 }
818 }
819}
820
821const CameraMetadata& Camera3Device::info() const {
822 String8 emptyId;
823 return info(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800824}
825
Jianing Wei90e59c92014-03-12 18:29:36 -0700826status_t Camera3Device::checkStatusOkToCaptureLocked() {
827 switch (mStatus) {
828 case STATUS_ERROR:
829 CLOGE("Device has encountered a serious error");
830 return INVALID_OPERATION;
831 case STATUS_UNINITIALIZED:
832 CLOGE("Device not initialized");
833 return INVALID_OPERATION;
834 case STATUS_UNCONFIGURED:
835 case STATUS_CONFIGURED:
836 case STATUS_ACTIVE:
837 // OK
838 break;
839 default:
840 SET_ERR_L("Unexpected status: %d", mStatus);
841 return INVALID_OPERATION;
842 }
843 return OK;
844}
845
846status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000847 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700848 const std::list<const SurfaceMap> &surfaceMaps,
849 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700850 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700851 if (requestList == NULL) {
852 CLOGE("requestList cannot be NULL.");
853 return BAD_VALUE;
854 }
855
Jianing Weicb0652e2014-03-12 18:29:36 -0700856 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000857 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700858 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
859 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
860 ++metadataIt, ++surfaceMapIt) {
861 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700862 if (newRequest == 0) {
863 CLOGE("Can't create capture request");
864 return BAD_VALUE;
865 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700866
Shuzhen Wang9d066012016-09-30 11:30:20 -0700867 newRequest->mRepeating = repeating;
868
Jianing Weicb0652e2014-03-12 18:29:36 -0700869 // Setup burst Id and request Id
870 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000871 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
872 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700873 CLOGE("RequestID entry exists; but must not be empty in metadata");
874 return BAD_VALUE;
875 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000876 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
877 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700878 } else {
879 CLOGE("RequestID does not exist in metadata");
880 return BAD_VALUE;
881 }
882
Jianing Wei90e59c92014-03-12 18:29:36 -0700883 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700884
885 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700886 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700887 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
888 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
889 return BAD_VALUE;
890 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700891
892 // Setup batch size if this is a high speed video recording request.
893 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
894 auto firstRequest = requestList->begin();
895 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
896 if (outputStream->isVideoStream()) {
897 (*firstRequest)->mBatchSize = requestList->size();
898 break;
899 }
900 }
901 }
902
Jianing Wei90e59c92014-03-12 18:29:36 -0700903 return OK;
904}
905
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800906status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800907 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800908
Emilian Peevaebbe412018-01-15 13:53:24 +0000909 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700910 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000911 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700912
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800913 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914}
915
Emilian Peevaebbe412018-01-15 13:53:24 +0000916void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700917 std::list<const SurfaceMap>& surfaceMaps,
918 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000919 PhysicalCameraSettingsList requestList;
920 requestList.push_back({std::string(getId().string()), request});
921 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700922
923 SurfaceMap surfaceMap;
924 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
925 // With no surface list passed in, stream and surface will have 1-to-1
926 // mapping. So the surface index is 0 for each stream in the surfaceMap.
927 for (size_t i = 0; i < streams.count; i++) {
928 surfaceMap[streams.data.i32[i]].push_back(0);
929 }
930 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800931}
932
Jianing Wei90e59c92014-03-12 18:29:36 -0700933status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000934 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700935 const std::list<const SurfaceMap> &surfaceMaps,
936 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700937 /*out*/
938 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700939 ATRACE_CALL();
940 Mutex::Autolock il(mInterfaceLock);
941 Mutex::Autolock l(mLock);
942
943 status_t res = checkStatusOkToCaptureLocked();
944 if (res != OK) {
945 // error logged by previous call
946 return res;
947 }
948
949 RequestList requestList;
950
Shuzhen Wang0129d522016-10-30 22:43:41 -0700951 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
952 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700953 if (res != OK) {
954 // error logged by previous call
955 return res;
956 }
957
958 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700959 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700960 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700961 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700962 }
963
964 if (res == OK) {
965 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
966 if (res != OK) {
967 SET_ERR_L("Can't transition to active in %f seconds!",
968 kActiveTimeout/1e9);
969 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800970 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700971 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700972 } else {
973 CLOGE("Cannot queue request. Impossible.");
974 return BAD_VALUE;
975 }
976
977 return res;
978}
979
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700980hardware::Return<void> Camera3Device::requestStreamBuffers(
981 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
982 requestStreamBuffers_cb _hidl_cb) {
983 using hardware::camera::device::V3_5::BufferRequestStatus;
984 using hardware::camera::device::V3_5::StreamBufferRet;
985 using hardware::camera::device::V3_5::StreamBufferRequestError;
986
987 std::lock_guard<std::mutex> lock(mRequestBufferInterfaceLock);
988
989 hardware::hidl_vec<StreamBufferRet> bufRets;
990 if (!mUseHalBufManager) {
991 ALOGE("%s: Camera %s does not support HAL buffer management",
992 __FUNCTION__, mId.string());
993 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
994 return hardware::Void();
995 }
996
997 SortedVector<int32_t> streamIds;
998 ssize_t sz = streamIds.setCapacity(bufReqs.size());
999 if (sz < 0 || static_cast<size_t>(sz) != bufReqs.size()) {
1000 ALOGE("%s: failed to allocate memory for %zu buffer requests",
1001 __FUNCTION__, bufReqs.size());
1002 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1003 return hardware::Void();
1004 }
1005
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001006 if (bufReqs.size() > mOutputStreams.size()) {
1007 ALOGE("%s: too many buffer requests (%zu > # of output streams %zu)",
1008 __FUNCTION__, bufReqs.size(), mOutputStreams.size());
1009 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1010 return hardware::Void();
1011 }
1012
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001013 // Check for repeated streamId
1014 for (const auto& bufReq : bufReqs) {
1015 if (streamIds.indexOf(bufReq.streamId) != NAME_NOT_FOUND) {
1016 ALOGE("%s: Stream %d appear multiple times in buffer requests",
1017 __FUNCTION__, bufReq.streamId);
1018 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1019 return hardware::Void();
1020 }
1021 streamIds.add(bufReq.streamId);
1022 }
1023
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001024 if (!mRequestBufferSM.startRequestBuffer()) {
1025 ALOGE("%s: request buffer disallowed while camera service is configuring",
1026 __FUNCTION__);
1027 _hidl_cb(BufferRequestStatus::FAILED_CONFIGURING, bufRets);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001028 return hardware::Void();
1029 }
1030
1031 bufRets.resize(bufReqs.size());
1032
1033 bool allReqsSucceeds = true;
1034 bool oneReqSucceeds = false;
1035 for (size_t i = 0; i < bufReqs.size(); i++) {
1036 const auto& bufReq = bufReqs[i];
1037 auto& bufRet = bufRets[i];
1038 int32_t streamId = bufReq.streamId;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001039 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.get(streamId);
1040 if (outputStream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001041 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
1042 hardware::hidl_vec<StreamBufferRet> emptyBufRets;
1043 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, emptyBufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001044 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001045 return hardware::Void();
1046 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001047
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001048 if (outputStream->isAbandoned()) {
1049 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
1050 allReqsSucceeds = false;
1051 continue;
1052 }
1053
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001054 bufRet.streamId = streamId;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001055 size_t handOutBufferCount = outputStream->getOutstandingBuffersCount();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001056 uint32_t numBuffersRequested = bufReq.numBuffersRequested;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001057 size_t totalHandout = handOutBufferCount + numBuffersRequested;
1058 uint32_t maxBuffers = outputStream->asHalStream()->max_buffers;
1059 if (totalHandout > maxBuffers) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001060 // Not able to allocate enough buffer. Exit early for this stream
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001061 ALOGE("%s: request too much buffers for stream %d: at HAL: %zu + requesting: %d"
1062 " > max: %d", __FUNCTION__, streamId, handOutBufferCount,
1063 numBuffersRequested, maxBuffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001064 bufRet.val.error(StreamBufferRequestError::MAX_BUFFER_EXCEEDED);
1065 allReqsSucceeds = false;
1066 continue;
1067 }
1068
1069 hardware::hidl_vec<StreamBuffer> tmpRetBuffers(numBuffersRequested);
1070 bool currentReqSucceeds = true;
1071 std::vector<camera3_stream_buffer_t> streamBuffers(numBuffersRequested);
1072 size_t numAllocatedBuffers = 0;
1073 size_t numPushedInflightBuffers = 0;
1074 for (size_t b = 0; b < numBuffersRequested; b++) {
1075 camera3_stream_buffer_t& sb = streamBuffers[b];
1076 // Since this method can run concurrently with request thread
1077 // We need to update the wait duration everytime we call getbuffer
1078 nsecs_t waitDuration = kBaseGetBufferWait + getExpectedInFlightDuration();
1079 status_t res = outputStream->getBuffer(&sb, waitDuration);
1080 if (res != OK) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001081 if (res == NO_INIT || res == DEAD_OBJECT) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001082 ALOGV("%s: Can't get output buffer for stream %d: %s (%d)",
1083 __FUNCTION__, streamId, strerror(-res), res);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001084 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001085 } else {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001086 ALOGE("%s: Can't get output buffer for stream %d: %s (%d)",
1087 __FUNCTION__, streamId, strerror(-res), res);
1088 if (res == TIMED_OUT || res == NO_MEMORY) {
1089 bufRet.val.error(StreamBufferRequestError::NO_BUFFER_AVAILABLE);
1090 } else {
1091 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1092 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001093 }
1094 currentReqSucceeds = false;
1095 break;
1096 }
1097 numAllocatedBuffers++;
1098
1099 buffer_handle_t *buffer = sb.buffer;
1100 auto pair = mInterface->getBufferId(*buffer, streamId);
1101 bool isNewBuffer = pair.first;
1102 uint64_t bufferId = pair.second;
1103 StreamBuffer& hBuf = tmpRetBuffers[b];
1104
1105 hBuf.streamId = streamId;
1106 hBuf.bufferId = bufferId;
1107 hBuf.buffer = (isNewBuffer) ? *buffer : nullptr;
1108 hBuf.status = BufferStatus::OK;
1109 hBuf.releaseFence = nullptr;
1110
1111 native_handle_t *acquireFence = nullptr;
1112 if (sb.acquire_fence != -1) {
1113 acquireFence = native_handle_create(1,0);
1114 acquireFence->data[0] = sb.acquire_fence;
1115 }
1116 hBuf.acquireFence.setTo(acquireFence, /*shouldOwn*/true);
1117 hBuf.releaseFence = nullptr;
1118
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08001119 res = mInterface->pushInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001120 if (res != OK) {
1121 ALOGE("%s: Can't get register request buffers for stream %d: %s (%d)",
1122 __FUNCTION__, streamId, strerror(-res), res);
1123 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1124 currentReqSucceeds = false;
1125 break;
1126 }
1127 numPushedInflightBuffers++;
1128 }
1129 if (currentReqSucceeds) {
1130 bufRet.val.buffers(std::move(tmpRetBuffers));
1131 oneReqSucceeds = true;
1132 } else {
1133 allReqsSucceeds = false;
1134 for (size_t b = 0; b < numPushedInflightBuffers; b++) {
1135 StreamBuffer& hBuf = tmpRetBuffers[b];
1136 buffer_handle_t* buffer;
1137 status_t res = mInterface->popInflightRequestBuffer(hBuf.bufferId, &buffer);
1138 if (res != OK) {
1139 SET_ERR("%s: popInflightRequestBuffer failed for stream %d: %s (%d)",
1140 __FUNCTION__, streamId, strerror(-res), res);
1141 }
1142 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001143 for (size_t b = 0; b < numAllocatedBuffers; b++) {
1144 camera3_stream_buffer_t& sb = streamBuffers[b];
1145 sb.acquire_fence = -1;
1146 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
1147 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001148 returnOutputBuffers(streamBuffers.data(), numAllocatedBuffers, 0);
1149 }
1150 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001151
1152 _hidl_cb(allReqsSucceeds ? BufferRequestStatus::OK :
1153 oneReqSucceeds ? BufferRequestStatus::FAILED_PARTIAL :
1154 BufferRequestStatus::FAILED_UNKNOWN,
1155 bufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001156 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001157 return hardware::Void();
1158}
1159
1160hardware::Return<void> Camera3Device::returnStreamBuffers(
1161 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
1162 if (!mUseHalBufManager) {
1163 ALOGE("%s: Camera %s does not support HAL buffer managerment",
1164 __FUNCTION__, mId.string());
1165 return hardware::Void();
1166 }
1167
1168 for (const auto& buf : buffers) {
1169 if (buf.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
1170 ALOGE("%s: cannot return a buffer without bufferId", __FUNCTION__);
1171 continue;
1172 }
1173
1174 buffer_handle_t* buffer;
1175 status_t res = mInterface->popInflightRequestBuffer(buf.bufferId, &buffer);
1176
1177 if (res != OK) {
1178 ALOGE("%s: cannot find in-flight buffer %" PRIu64 " for stream %d",
1179 __FUNCTION__, buf.bufferId, buf.streamId);
1180 continue;
1181 }
1182
1183 camera3_stream_buffer_t streamBuffer;
1184 streamBuffer.buffer = buffer;
1185 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
1186 streamBuffer.acquire_fence = -1;
1187 streamBuffer.release_fence = -1;
1188
1189 if (buf.releaseFence == nullptr) {
1190 streamBuffer.release_fence = -1;
1191 } else if (buf.releaseFence->numFds == 1) {
1192 streamBuffer.release_fence = dup(buf.releaseFence->data[0]);
1193 } else {
1194 ALOGE("%s: Invalid release fence, fd count is %d, not 1",
1195 __FUNCTION__, buf.releaseFence->numFds);
1196 continue;
1197 }
1198
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001199 sp<Camera3StreamInterface> stream = mOutputStreams.get(buf.streamId);
1200 if (stream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001201 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, buf.streamId);
1202 continue;
1203 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001204 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001205 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
1206 }
1207 return hardware::Void();
1208}
1209
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001210hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001211 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001212 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001213 // Ideally we should grab mLock, but that can lead to deadlock, and
1214 // it's not super important to get up to date value of mStatus for this
1215 // warning print, hence skipping the lock here
1216 if (mStatus == STATUS_ERROR) {
1217 // Per API contract, HAL should act as closed after device error
1218 // But mStatus can be set to error by framework as well, so just log
1219 // a warning here.
1220 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001221 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001222
1223 if (mProcessCaptureResultLock.tryLock() != OK) {
1224 // This should never happen; it indicates a wrong client implementation
1225 // that doesn't follow the contract. But, we can be tolerant here.
1226 ALOGE("%s: callback overlapped! waiting 1s...",
1227 __FUNCTION__);
1228 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1229 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1230 __FUNCTION__);
1231 // really don't know what to do, so bail out.
1232 return hardware::Void();
1233 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001234 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001235 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001236 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001237 }
1238 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001239 return hardware::Void();
1240}
1241
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001242// Only one processCaptureResult should be called at a time, so
1243// the locks won't block. The locks are present here simply to enforce this.
1244hardware::Return<void> Camera3Device::processCaptureResult(
1245 const hardware::hidl_vec<
1246 hardware::camera::device::V3_2::CaptureResult>& results) {
1247 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1248
1249 // Ideally we should grab mLock, but that can lead to deadlock, and
1250 // it's not super important to get up to date value of mStatus for this
1251 // warning print, hence skipping the lock here
1252 if (mStatus == STATUS_ERROR) {
1253 // Per API contract, HAL should act as closed after device error
1254 // But mStatus can be set to error by framework as well, so just log
1255 // a warning here.
1256 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1257 }
1258
1259 if (mProcessCaptureResultLock.tryLock() != OK) {
1260 // This should never happen; it indicates a wrong client implementation
1261 // that doesn't follow the contract. But, we can be tolerant here.
1262 ALOGE("%s: callback overlapped! waiting 1s...",
1263 __FUNCTION__);
1264 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1265 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1266 __FUNCTION__);
1267 // really don't know what to do, so bail out.
1268 return hardware::Void();
1269 }
1270 }
1271 for (const auto& result : results) {
1272 processOneCaptureResultLocked(result, noPhysMetadata);
1273 }
1274 mProcessCaptureResultLock.unlock();
1275 return hardware::Void();
1276}
1277
1278status_t Camera3Device::readOneCameraMetadataLocked(
1279 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
1280 const hardware::camera::device::V3_2::CameraMetadata& result) {
1281 if (fmqResultSize > 0) {
1282 resultMetadata.resize(fmqResultSize);
1283 if (mResultMetadataQueue == nullptr) {
1284 return NO_MEMORY; // logged in initialize()
1285 }
1286 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
1287 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
1288 __FUNCTION__, fmqResultSize);
1289 return INVALID_OPERATION;
1290 }
1291 } else {
1292 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
1293 result.size());
1294 }
1295
1296 if (resultMetadata.size() != 0) {
1297 status_t res;
1298 const camera_metadata_t* metadata =
1299 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
1300 size_t expected_metadata_size = resultMetadata.size();
1301 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
1302 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
1303 __FUNCTION__, strerror(-res), res);
1304 return INVALID_OPERATION;
1305 }
1306 }
1307
1308 return OK;
1309}
1310
Yifan Honga640c5a2017-04-12 16:30:31 -07001311void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001312 const hardware::camera::device::V3_2::CaptureResult& result,
1313 const hardware::hidl_vec<
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001314 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001315 camera3_capture_result r;
1316 status_t res;
1317 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -07001318
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001319 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -07001320 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001321 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
1322 if (res != OK) {
1323 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1324 __FUNCTION__, result.frameNumber);
1325 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001326 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001327 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001328
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001329 // Read and validate physical camera metadata
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001330 size_t physResultCount = physicalCameraMetadata.size();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001331 std::vector<const char*> physCamIds(physResultCount);
1332 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1333 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1334 physResultMetadata.resize(physResultCount);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001335 for (size_t i = 0; i < physicalCameraMetadata.size(); i++) {
1336 res = readOneCameraMetadataLocked(physicalCameraMetadata[i].fmqMetadataSize,
1337 physResultMetadata[i], physicalCameraMetadata[i].metadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001338 if (res != OK) {
1339 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1340 __FUNCTION__, result.frameNumber,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001341 physicalCameraMetadata[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001342 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001343 }
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001344 physCamIds[i] = physicalCameraMetadata[i].physicalCameraId.c_str();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001345 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1346 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001347 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001348 r.num_physcam_metadata = physResultCount;
1349 r.physcam_ids = physCamIds.data();
1350 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001351
1352 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1353 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1354 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1355 auto& bDst = outputBuffers[i];
1356 const StreamBuffer &bSrc = result.outputBuffers[i];
1357
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001358 sp<Camera3StreamInterface> stream = mOutputStreams.get(bSrc.streamId);
1359 if (stream == nullptr) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001360 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1361 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001362 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001363 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001364 bDst.stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001365
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001366 bool noBufferReturned = false;
1367 buffer_handle_t *buffer = nullptr;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001368 if (mUseHalBufManager) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001369 // This is suspicious most of the time but can be correct during flush where HAL
1370 // has to return capture result before a buffer is requested
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001371 if (bSrc.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001372 if (bSrc.status == BufferStatus::OK) {
1373 ALOGE("%s: Frame %d: Buffer %zu: No bufferId for stream %d",
1374 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
1375 // Still proceeds so other buffers can be returned
1376 }
1377 noBufferReturned = true;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001378 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001379 if (noBufferReturned) {
1380 res = OK;
1381 } else {
1382 res = mInterface->popInflightRequestBuffer(bSrc.bufferId, &buffer);
1383 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001384 } else {
1385 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
1386 }
1387
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001388 if (res != OK) {
1389 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1390 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001391 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001392 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001393
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001394 bDst.buffer = buffer;
1395 bDst.status = mapHidlBufferStatus(bSrc.status);
1396 bDst.acquire_fence = -1;
1397 if (bSrc.releaseFence == nullptr) {
1398 bDst.release_fence = -1;
1399 } else if (bSrc.releaseFence->numFds == 1) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001400 if (noBufferReturned) {
1401 ALOGE("%s: got releaseFence without output buffer!", __FUNCTION__);
1402 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001403 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1404 } else {
1405 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1406 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001407 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001408 }
1409 }
1410 r.num_output_buffers = outputBuffers.size();
1411 r.output_buffers = outputBuffers.data();
1412
1413 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001414 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001415 r.input_buffer = nullptr;
1416 } else {
1417 if (mInputStream->getId() != result.inputBuffer.streamId) {
1418 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1419 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001420 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001421 }
1422 inputBuffer.stream = mInputStream->asHalStream();
1423 buffer_handle_t *buffer;
1424 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1425 &buffer);
1426 if (res != OK) {
1427 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1428 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001429 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001430 }
1431 inputBuffer.buffer = buffer;
1432 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1433 inputBuffer.acquire_fence = -1;
1434 if (result.inputBuffer.releaseFence == nullptr) {
1435 inputBuffer.release_fence = -1;
1436 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1437 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1438 } else {
1439 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1440 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001441 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001442 }
1443 r.input_buffer = &inputBuffer;
1444 }
1445
1446 r.partial_result = result.partialResult;
1447
1448 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001449}
1450
1451hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001452 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001453 // Ideally we should grab mLock, but that can lead to deadlock, and
1454 // it's not super important to get up to date value of mStatus for this
1455 // warning print, hence skipping the lock here
1456 if (mStatus == STATUS_ERROR) {
1457 // Per API contract, HAL should act as closed after device error
1458 // But mStatus can be set to error by framework as well, so just log
1459 // a warning here.
1460 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001461 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001462
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001463 for (const auto& msg : msgs) {
1464 notify(msg);
1465 }
1466 return hardware::Void();
1467}
1468
1469void Camera3Device::notify(
1470 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1471
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001472 camera3_notify_msg m;
1473 switch (msg.type) {
1474 case MsgType::ERROR:
1475 m.type = CAMERA3_MSG_ERROR;
1476 m.message.error.frame_number = msg.msg.error.frameNumber;
1477 if (msg.msg.error.errorStreamId >= 0) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001478 sp<Camera3StreamInterface> stream = mOutputStreams.get(msg.msg.error.errorStreamId);
1479 if (stream == nullptr) {
1480 ALOGE("%s: Frame %d: Invalid error stream id %d", __FUNCTION__,
1481 m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001482 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001483 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001484 m.message.error.error_stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001485 } else {
1486 m.message.error.error_stream = nullptr;
1487 }
1488 switch (msg.msg.error.errorCode) {
1489 case ErrorCode::ERROR_DEVICE:
1490 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1491 break;
1492 case ErrorCode::ERROR_REQUEST:
1493 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1494 break;
1495 case ErrorCode::ERROR_RESULT:
1496 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1497 break;
1498 case ErrorCode::ERROR_BUFFER:
1499 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1500 break;
1501 }
1502 break;
1503 case MsgType::SHUTTER:
1504 m.type = CAMERA3_MSG_SHUTTER;
1505 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1506 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1507 break;
1508 }
1509 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001510}
1511
Emilian Peevaebbe412018-01-15 13:53:24 +00001512status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001513 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001514 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001515 ATRACE_CALL();
1516
Emilian Peevaebbe412018-01-15 13:53:24 +00001517 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001518}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001519
Jianing Weicb0652e2014-03-12 18:29:36 -07001520status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1521 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523
Emilian Peevaebbe412018-01-15 13:53:24 +00001524 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001525 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001526 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001527
Emilian Peevaebbe412018-01-15 13:53:24 +00001528 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001529 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001530}
1531
Emilian Peevaebbe412018-01-15 13:53:24 +00001532status_t Camera3Device::setStreamingRequestList(
1533 const List<const PhysicalCameraSettingsList> &requestsList,
1534 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001535 ATRACE_CALL();
1536
Emilian Peevaebbe412018-01-15 13:53:24 +00001537 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001538}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001539
1540sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001541 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001542 status_t res;
1543
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001544 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001545 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1546 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001547 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1548 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001549 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001550 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001551 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001552 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001553 } else if (mStatus == STATUS_UNCONFIGURED) {
1554 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001555 CLOGE("No streams configured");
1556 return NULL;
1557 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 }
1559
Shuzhen Wang0129d522016-10-30 22:43:41 -07001560 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001562}
1563
Jianing Weicb0652e2014-03-12 18:29:36 -07001564status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001565 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001568
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569 switch (mStatus) {
1570 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001571 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001572 return INVALID_OPERATION;
1573 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001574 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001575 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576 case STATUS_UNCONFIGURED:
1577 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 case STATUS_ACTIVE:
1579 // OK
1580 break;
1581 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001582 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001583 return INVALID_OPERATION;
1584 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001585 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001586
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001587 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001588}
1589
1590status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1591 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001593
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001594 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001595}
1596
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001597status_t Camera3Device::createInputStream(
1598 uint32_t width, uint32_t height, int format, int *id) {
1599 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001600 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001601 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001602 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001603 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1604 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001605
1606 status_t res;
1607 bool wasActive = false;
1608
1609 switch (mStatus) {
1610 case STATUS_ERROR:
1611 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1612 return INVALID_OPERATION;
1613 case STATUS_UNINITIALIZED:
1614 ALOGE("%s: Device not initialized", __FUNCTION__);
1615 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001616 case STATUS_UNCONFIGURED:
1617 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001618 // OK
1619 break;
1620 case STATUS_ACTIVE:
1621 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001622 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001623 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001624 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001625 return res;
1626 }
1627 wasActive = true;
1628 break;
1629 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001630 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001631 return INVALID_OPERATION;
1632 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001634
1635 if (mInputStream != 0) {
1636 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1637 return INVALID_OPERATION;
1638 }
1639
1640 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1641 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001642 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001643
1644 mInputStream = newStream;
1645
1646 *id = mNextStreamId++;
1647
1648 // Continue captures if active at start
1649 if (wasActive) {
1650 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001651 // Reuse current operating mode and session parameters for new stream config
1652 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001653 if (res != OK) {
1654 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1655 __FUNCTION__, mNextStreamId, strerror(-res), res);
1656 return res;
1657 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001658 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001659 }
1660
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001661 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001662 return OK;
1663}
1664
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001665status_t Camera3Device::StreamSet::add(
1666 int streamId, sp<camera3::Camera3OutputStreamInterface> stream) {
1667 if (stream == nullptr) {
1668 ALOGE("%s: cannot add null stream", __FUNCTION__);
1669 return BAD_VALUE;
1670 }
1671 std::lock_guard<std::mutex> lock(mLock);
1672 return mData.add(streamId, stream);
1673}
1674
1675ssize_t Camera3Device::StreamSet::remove(int streamId) {
1676 std::lock_guard<std::mutex> lock(mLock);
1677 return mData.removeItem(streamId);
1678}
1679
1680sp<camera3::Camera3OutputStreamInterface>
1681Camera3Device::StreamSet::get(int streamId) {
1682 std::lock_guard<std::mutex> lock(mLock);
1683 ssize_t idx = mData.indexOfKey(streamId);
1684 if (idx == NAME_NOT_FOUND) {
1685 return nullptr;
1686 }
1687 return mData.editValueAt(idx);
1688}
1689
1690sp<camera3::Camera3OutputStreamInterface>
1691Camera3Device::StreamSet::operator[] (size_t index) {
1692 std::lock_guard<std::mutex> lock(mLock);
1693 return mData.editValueAt(index);
1694}
1695
1696size_t Camera3Device::StreamSet::size() const {
1697 std::lock_guard<std::mutex> lock(mLock);
1698 return mData.size();
1699}
1700
1701void Camera3Device::StreamSet::clear() {
1702 std::lock_guard<std::mutex> lock(mLock);
1703 return mData.clear();
1704}
1705
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07001706std::vector<int> Camera3Device::StreamSet::getStreamIds() {
1707 std::lock_guard<std::mutex> lock(mLock);
1708 std::vector<int> streamIds(mData.size());
1709 for (size_t i = 0; i < mData.size(); i++) {
1710 streamIds[i] = mData.keyAt(i);
1711 }
1712 return streamIds;
1713}
1714
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001715status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001716 uint32_t width, uint32_t height, int format,
1717 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001718 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001719 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001720 ATRACE_CALL();
1721
1722 if (consumer == nullptr) {
1723 ALOGE("%s: consumer must not be null", __FUNCTION__);
1724 return BAD_VALUE;
1725 }
1726
1727 std::vector<sp<Surface>> consumers;
1728 consumers.push_back(consumer);
1729
1730 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001731 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1732 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001733}
1734
1735status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1736 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1737 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001738 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001739 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001740 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001741
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001743 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001745 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001746 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1747 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1748 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001749
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001750 status_t res;
1751 bool wasActive = false;
1752
1753 switch (mStatus) {
1754 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001755 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001756 return INVALID_OPERATION;
1757 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001758 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001759 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001760 case STATUS_UNCONFIGURED:
1761 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001762 // OK
1763 break;
1764 case STATUS_ACTIVE:
1765 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001766 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001767 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001768 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001769 return res;
1770 }
1771 wasActive = true;
1772 break;
1773 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001774 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775 return INVALID_OPERATION;
1776 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001777 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001778
1779 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001780
Shuzhen Wang0129d522016-10-30 22:43:41 -07001781 if (consumers.size() == 0 && !hasDeferredConsumer) {
1782 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1783 return BAD_VALUE;
1784 }
Zhijun He5d677d12016-05-29 16:52:39 -07001785
Shuzhen Wang0129d522016-10-30 22:43:41 -07001786 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001787 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1788 return BAD_VALUE;
1789 }
1790
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001791 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001792 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001793 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001794 blobBufferSize = getPointCloudBufferSize();
1795 if (blobBufferSize <= 0) {
1796 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1797 return BAD_VALUE;
1798 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001799 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1800 blobBufferSize = width * height;
1801 } else {
1802 blobBufferSize = getJpegBufferSize(width, height);
1803 if (blobBufferSize <= 0) {
1804 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1805 return BAD_VALUE;
1806 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001807 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001808 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001809 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001810 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001811 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1812 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1813 if (rawOpaqueBufferSize <= 0) {
1814 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1815 return BAD_VALUE;
1816 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001817 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001818 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001819 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001820 } else if (isShared) {
1821 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1822 width, height, format, consumerUsage, dataSpace, rotation,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001823 mTimestampOffset, physicalCameraId, streamSetId,
1824 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001825 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001826 newStream = new Camera3OutputStream(mNextStreamId,
1827 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001828 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001830 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001831 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001832 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001833 }
Emilian Peev40ead602017-09-26 15:46:36 +01001834
1835 size_t consumerCount = consumers.size();
1836 for (size_t i = 0; i < consumerCount; i++) {
1837 int id = newStream->getSurfaceId(consumers[i]);
1838 if (id < 0) {
1839 SET_ERR_L("Invalid surface id");
1840 return BAD_VALUE;
1841 }
1842 if (surfaceIds != nullptr) {
1843 surfaceIds->push_back(id);
1844 }
1845 }
1846
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001847 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001848
Emilian Peev08dd2452017-04-06 16:55:14 +01001849 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001850
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851 res = mOutputStreams.add(mNextStreamId, newStream);
1852 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001853 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 return res;
1855 }
1856
1857 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001858 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001859
1860 // Continue captures if active at start
1861 if (wasActive) {
1862 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001863 // Reuse current operating mode and session parameters for new stream config
1864 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001865 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001866 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1867 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001868 return res;
1869 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001870 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001871 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001872 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001874}
1875
Emilian Peev710c1422017-08-30 11:19:38 +01001876status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001877 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001878 if (nullptr == streamInfo) {
1879 return BAD_VALUE;
1880 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001881 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001883
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001884 switch (mStatus) {
1885 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001886 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001887 return INVALID_OPERATION;
1888 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001889 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001890 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001891 case STATUS_UNCONFIGURED:
1892 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001893 case STATUS_ACTIVE:
1894 // OK
1895 break;
1896 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001897 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001898 return INVALID_OPERATION;
1899 }
1900
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001901 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1902 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001903 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001904 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001905 }
1906
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001907 streamInfo->width = stream->getWidth();
1908 streamInfo->height = stream->getHeight();
1909 streamInfo->format = stream->getFormat();
1910 streamInfo->dataSpace = stream->getDataSpace();
1911 streamInfo->formatOverridden = stream->isFormatOverridden();
1912 streamInfo->originalFormat = stream->getOriginalFormat();
1913 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1914 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001915 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001916}
1917
1918status_t Camera3Device::setStreamTransform(int id,
1919 int transform) {
1920 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001921 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001922 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001923
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001924 switch (mStatus) {
1925 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001926 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001927 return INVALID_OPERATION;
1928 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001929 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001930 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001931 case STATUS_UNCONFIGURED:
1932 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001933 case STATUS_ACTIVE:
1934 // OK
1935 break;
1936 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001937 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001938 return INVALID_OPERATION;
1939 }
1940
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001941 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1942 if (stream == nullptr) {
1943 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001944 return BAD_VALUE;
1945 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001946 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001947}
1948
1949status_t Camera3Device::deleteStream(int id) {
1950 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001951 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001952 Mutex::Autolock l(mLock);
1953 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001954
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001955 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001956
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957 // CameraDevice semantics require device to already be idle before
1958 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001959 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001960 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001961 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001962 }
1963
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001964 if (mStatus == STATUS_ERROR) {
1965 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1966 __FUNCTION__, mId.string());
1967 return -EBUSY;
1968 }
1969
Igor Murashkin2fba5842013-04-22 14:03:54 -07001970 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001971 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001972 if (mInputStream != NULL && id == mInputStream->getId()) {
1973 deletedStream = mInputStream;
1974 mInputStream.clear();
1975 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001976 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001977 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001978 return BAD_VALUE;
1979 }
Zhijun He5f446352014-01-22 09:49:33 -08001980 }
1981
1982 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001983 if (stream != nullptr) {
1984 deletedStream = stream;
1985 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001986 }
1987
1988 // Free up the stream endpoint so that it can be used by some other stream
1989 res = deletedStream->disconnect();
1990 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001991 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001992 // fall through since we want to still list the stream as deleted.
1993 }
1994 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001995 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001996
1997 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001998}
1999
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002000status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07002001 ATRACE_CALL();
2002 ALOGV("%s: E", __FUNCTION__);
2003
2004 Mutex::Autolock il(mInterfaceLock);
2005 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07002006
Emilian Peev811d2952018-05-25 11:08:40 +01002007 // In case the client doesn't include any session parameter, try a
2008 // speculative configuration using the values from the last cached
2009 // default request.
2010 if (sessionParams.isEmpty() &&
2011 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
2012 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
2013 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
2014 mLastTemplateId);
2015 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
2016 operatingMode);
2017 }
2018
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002019 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
2020}
2021
2022status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
2023 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002024 //Filter out any incoming session parameters
2025 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002026 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
2027 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002028 CameraMetadata filteredParams(availableSessionKeys.count);
2029 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2030 filteredParams.getAndLock());
2031 set_camera_metadata_vendor_id(meta, mVendorTagId);
2032 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002033 if (availableSessionKeys.count > 0) {
2034 for (size_t i = 0; i < availableSessionKeys.count; i++) {
2035 camera_metadata_ro_entry entry = params.find(
2036 availableSessionKeys.data.i32[i]);
2037 if (entry.count > 0) {
2038 filteredParams.update(entry);
2039 }
2040 }
2041 }
2042
2043 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07002044}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002045
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002046status_t Camera3Device::getInputBufferProducer(
2047 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002048 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002049 Mutex::Autolock il(mInterfaceLock);
2050 Mutex::Autolock l(mLock);
2051
2052 if (producer == NULL) {
2053 return BAD_VALUE;
2054 } else if (mInputStream == NULL) {
2055 return INVALID_OPERATION;
2056 }
2057
2058 return mInputStream->getInputBufferProducer(producer);
2059}
2060
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002061status_t Camera3Device::createDefaultRequest(int templateId,
2062 CameraMetadata *request) {
2063 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07002064 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002065
2066 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
2067 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07002068 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002069 return BAD_VALUE;
2070 }
2071
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002072 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002074 {
2075 Mutex::Autolock l(mLock);
2076 switch (mStatus) {
2077 case STATUS_ERROR:
2078 CLOGE("Device has encountered a serious error");
2079 return INVALID_OPERATION;
2080 case STATUS_UNINITIALIZED:
2081 CLOGE("Device is not initialized!");
2082 return INVALID_OPERATION;
2083 case STATUS_UNCONFIGURED:
2084 case STATUS_CONFIGURED:
2085 case STATUS_ACTIVE:
2086 // OK
2087 break;
2088 default:
2089 SET_ERR_L("Unexpected status: %d", mStatus);
2090 return INVALID_OPERATION;
2091 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002092
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002093 if (!mRequestTemplateCache[templateId].isEmpty()) {
2094 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002095 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002096 return OK;
2097 }
Zhijun Hea1530f12014-09-14 12:44:20 -07002098 }
2099
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002100 camera_metadata_t *rawRequest;
2101 status_t res = mInterface->constructDefaultRequestSettings(
2102 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002103
2104 {
2105 Mutex::Autolock l(mLock);
2106 if (res == BAD_VALUE) {
2107 ALOGI("%s: template %d is not supported on this camera device",
2108 __FUNCTION__, templateId);
2109 return res;
2110 } else if (res != OK) {
2111 CLOGE("Unable to construct request template %d: %s (%d)",
2112 templateId, strerror(-res), res);
2113 return res;
2114 }
2115
2116 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
2117 mRequestTemplateCache[templateId].acquire(rawRequest);
2118
2119 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002120 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002121 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002122 return OK;
2123}
2124
2125status_t Camera3Device::waitUntilDrained() {
2126 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002127 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002128 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002130
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002131 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07002132}
2133
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002134status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135 switch (mStatus) {
2136 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002137 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002138 ALOGV("%s: Already idle", __FUNCTION__);
2139 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002140 case STATUS_CONFIGURED:
2141 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002142 case STATUS_ERROR:
2143 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002144 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002145 break;
2146 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002147 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002148 return INVALID_OPERATION;
2149 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002150 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
2151 maxExpectedDuration);
2152 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07002153 if (res != OK) {
2154 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
2155 res);
2156 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002157 return res;
2158}
2159
Ruben Brunk183f0562015-08-12 12:55:02 -07002160
2161void Camera3Device::internalUpdateStatusLocked(Status status) {
2162 mStatus = status;
2163 mRecentStatusUpdates.add(mStatus);
2164 mStatusChanged.broadcast();
2165}
2166
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002167void Camera3Device::pauseStateNotify(bool enable) {
2168 Mutex::Autolock il(mInterfaceLock);
2169 Mutex::Autolock l(mLock);
2170
2171 mPauseStateNotify = enable;
2172}
2173
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002174// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002175status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08002176 if (mRequestThread.get() != nullptr) {
2177 mRequestThread->setPaused(true);
2178 } else {
2179 return NO_INIT;
2180 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002181
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002182 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2183 maxExpectedDuration);
2184 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002185 if (res != OK) {
2186 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002187 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002188 }
2189
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002190 return res;
2191}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002192
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002193// Resume after internalPauseAndWaitLocked
2194status_t Camera3Device::internalResumeLocked() {
2195 status_t res;
2196
2197 mRequestThread->setPaused(false);
2198
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002199 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2200 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002201 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
2202 if (res != OK) {
2203 SET_ERR_L("Can't transition to active in %f seconds!",
2204 kActiveTimeout/1e9);
2205 }
2206 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002207 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002208}
2209
Ruben Brunk183f0562015-08-12 12:55:02 -07002210status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002211 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07002212
2213 size_t startIndex = 0;
2214 if (mStatusWaiters == 0) {
2215 // Clear the list of recent statuses if there are no existing threads waiting on updates to
2216 // this status list
2217 mRecentStatusUpdates.clear();
2218 } else {
2219 // If other threads are waiting on updates to this status list, set the position of the
2220 // first element that this list will check rather than clearing the list.
2221 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002222 }
2223
Ruben Brunk183f0562015-08-12 12:55:02 -07002224 mStatusWaiters++;
2225
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002226 if (!active && mUseHalBufManager) {
2227 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08002228 if (mStatus == STATUS_ACTIVE) {
2229 mRequestThread->signalPipelineDrain(streamIds);
2230 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002231 mRequestBufferSM.onWaitUntilIdle();
2232 }
2233
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002234 bool stateSeen = false;
2235 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07002236 if (active == (mStatus == STATUS_ACTIVE)) {
2237 // Desired state is current
2238 break;
2239 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002240
2241 res = mStatusChanged.waitRelative(mLock, timeout);
2242 if (res != OK) break;
2243
Ruben Brunk183f0562015-08-12 12:55:02 -07002244 // This is impossible, but if not, could result in subtle deadlocks and invalid state
2245 // transitions.
2246 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
2247 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
2248 __FUNCTION__);
2249
2250 // Encountered desired state since we began waiting
2251 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002252 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
2253 stateSeen = true;
2254 break;
2255 }
2256 }
2257 } while (!stateSeen);
2258
Ruben Brunk183f0562015-08-12 12:55:02 -07002259 mStatusWaiters--;
2260
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002261 return res;
2262}
2263
2264
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002265status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002266 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002267 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002268
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002269 if (listener != NULL && mListener != NULL) {
2270 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
2271 }
2272 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002273 mRequestThread->setNotificationListener(listener);
2274 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002275
2276 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002277}
2278
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002279bool Camera3Device::willNotify3A() {
2280 return false;
2281}
2282
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002283status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002284 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002285 status_t res;
2286 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002287
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002288 while (mResultQueue.empty()) {
2289 res = mResultSignal.waitRelative(mOutputLock, timeout);
2290 if (res == TIMED_OUT) {
2291 return res;
2292 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002293 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
2294 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002295 return res;
2296 }
2297 }
2298 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002299}
2300
Jianing Weicb0652e2014-03-12 18:29:36 -07002301status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002302 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002303 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002304
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002305 if (mResultQueue.empty()) {
2306 return NOT_ENOUGH_DATA;
2307 }
2308
Jianing Weicb0652e2014-03-12 18:29:36 -07002309 if (frame == NULL) {
2310 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
2311 return BAD_VALUE;
2312 }
2313
2314 CaptureResult &result = *(mResultQueue.begin());
2315 frame->mResultExtras = result.mResultExtras;
2316 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002317 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002318 mResultQueue.erase(mResultQueue.begin());
2319
2320 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002321}
2322
2323status_t Camera3Device::triggerAutofocus(uint32_t id) {
2324 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002325 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002326
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002327 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
2328 // Mix-in this trigger into the next request and only the next request.
2329 RequestTrigger trigger[] = {
2330 {
2331 ANDROID_CONTROL_AF_TRIGGER,
2332 ANDROID_CONTROL_AF_TRIGGER_START
2333 },
2334 {
2335 ANDROID_CONTROL_AF_TRIGGER_ID,
2336 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002337 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002338 };
2339
2340 return mRequestThread->queueTrigger(trigger,
2341 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002342}
2343
2344status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2345 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002346 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002347
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002348 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2349 // Mix-in this trigger into the next request and only the next request.
2350 RequestTrigger trigger[] = {
2351 {
2352 ANDROID_CONTROL_AF_TRIGGER,
2353 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2354 },
2355 {
2356 ANDROID_CONTROL_AF_TRIGGER_ID,
2357 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002358 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002359 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002360
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002361 return mRequestThread->queueTrigger(trigger,
2362 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002363}
2364
2365status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2366 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002367 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002368
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002369 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2370 // Mix-in this trigger into the next request and only the next request.
2371 RequestTrigger trigger[] = {
2372 {
2373 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2374 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2375 },
2376 {
2377 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2378 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002379 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002380 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002381
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002382 return mRequestThread->queueTrigger(trigger,
2383 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002384}
2385
Jianing Weicb0652e2014-03-12 18:29:36 -07002386status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002387 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002388 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002389 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002390
Zhijun He7ef20392014-04-21 16:04:17 -07002391 {
2392 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002393
2394 // b/116514106 "disconnect()" can get called twice for the same device. The
2395 // camera device will not be initialized during the second run.
2396 if (mStatus == STATUS_UNINITIALIZED) {
2397 return OK;
2398 }
2399
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002400 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07002401 }
2402
Emilian Peev08dd2452017-04-06 16:55:14 +01002403 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002404}
2405
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002406status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002407 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2408}
2409
2410status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002411 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002412 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002413 Mutex::Autolock il(mInterfaceLock);
2414 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002415
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002416 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2417 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002418 CLOGE("Stream %d does not exist", streamId);
2419 return BAD_VALUE;
2420 }
2421
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002422 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002423 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002424 return BAD_VALUE;
2425 }
2426
2427 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002428 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002429 return BAD_VALUE;
2430 }
2431
Ruben Brunkc78ac262015-08-13 17:58:46 -07002432 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002433}
2434
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002435status_t Camera3Device::tearDown(int streamId) {
2436 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002437 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002438 Mutex::Autolock il(mInterfaceLock);
2439 Mutex::Autolock l(mLock);
2440
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002441 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2442 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002443 CLOGE("Stream %d does not exist", streamId);
2444 return BAD_VALUE;
2445 }
2446
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002447 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2448 CLOGE("Stream %d is a target of a in-progress request", streamId);
2449 return BAD_VALUE;
2450 }
2451
2452 return stream->tearDown();
2453}
2454
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002455status_t Camera3Device::addBufferListenerForStream(int streamId,
2456 wp<Camera3StreamBufferListener> listener) {
2457 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002458 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002459 Mutex::Autolock il(mInterfaceLock);
2460 Mutex::Autolock l(mLock);
2461
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002462 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2463 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002464 CLOGE("Stream %d does not exist", streamId);
2465 return BAD_VALUE;
2466 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002467 stream->addBufferListener(listener);
2468
2469 return OK;
2470}
2471
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002472/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002473 * Methods called by subclasses
2474 */
2475
2476void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002477 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002478 {
2479 // Need mLock to safely update state and synchronize to current
2480 // state of methods in flight.
2481 Mutex::Autolock l(mLock);
2482 // We can get various system-idle notices from the status tracker
2483 // while starting up. Only care about them if we've actually sent
2484 // in some requests recently.
2485 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2486 return;
2487 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002488 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2489 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002490 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002491
2492 // Skip notifying listener if we're doing some user-transparent
2493 // state changes
2494 if (mPauseStateNotify) return;
2495 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002496
2497 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002498 {
2499 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002500 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002501 }
2502 if (idle && listener != NULL) {
2503 listener->notifyIdle();
2504 }
2505}
2506
Shuzhen Wang758c2152017-01-10 18:26:18 -08002507status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002508 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002509 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002510 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2511 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002512
2513 if (surfaceIds == nullptr) {
2514 return BAD_VALUE;
2515 }
2516
Zhijun He5d677d12016-05-29 16:52:39 -07002517 Mutex::Autolock il(mInterfaceLock);
2518 Mutex::Autolock l(mLock);
2519
Shuzhen Wang758c2152017-01-10 18:26:18 -08002520 if (consumers.size() == 0) {
2521 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002522 return BAD_VALUE;
2523 }
2524
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002525 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2526 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002527 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002528 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002529 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002530
2531 // isConsumerConfigurationDeferred will be off after setConsumers
2532 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002533 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002534 if (res != OK) {
2535 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2536 return res;
2537 }
2538
Emilian Peev40ead602017-09-26 15:46:36 +01002539 for (auto &consumer : consumers) {
2540 int id = stream->getSurfaceId(consumer);
2541 if (id < 0) {
2542 CLOGE("Invalid surface id!");
2543 return BAD_VALUE;
2544 }
2545 surfaceIds->push_back(id);
2546 }
2547
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002548 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002549 if (!stream->isConfiguring()) {
2550 CLOGE("Stream %d was already fully configured.", streamId);
2551 return INVALID_OPERATION;
2552 }
Zhijun He5d677d12016-05-29 16:52:39 -07002553
Shuzhen Wang0129d522016-10-30 22:43:41 -07002554 res = stream->finishConfiguration();
2555 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002556 // If finishConfiguration fails due to abandoned surface, do not set
2557 // device to error state.
2558 bool isSurfaceAbandoned =
2559 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2560 if (!isSurfaceAbandoned) {
2561 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2562 stream->getId(), strerror(-res), res);
2563 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002564 return res;
2565 }
Zhijun He5d677d12016-05-29 16:52:39 -07002566 }
2567
2568 return OK;
2569}
2570
Emilian Peev40ead602017-09-26 15:46:36 +01002571status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2572 const std::vector<OutputStreamInfo> &outputInfo,
2573 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2574 Mutex::Autolock il(mInterfaceLock);
2575 Mutex::Autolock l(mLock);
2576
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002577 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2578 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002579 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002580 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002581 }
2582
2583 for (const auto &it : removedSurfaceIds) {
2584 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2585 CLOGE("Shared surface still part of a pending request!");
2586 return -EBUSY;
2587 }
2588 }
2589
Emilian Peev40ead602017-09-26 15:46:36 +01002590 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2591 if (res != OK) {
2592 CLOGE("Stream %d failed to update stream (error %d %s) ",
2593 streamId, res, strerror(-res));
2594 if (res == UNKNOWN_ERROR) {
2595 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2596 __FUNCTION__);
2597 }
2598 return res;
2599 }
2600
2601 return res;
2602}
2603
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002604status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2605 Mutex::Autolock il(mInterfaceLock);
2606 Mutex::Autolock l(mLock);
2607
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002608 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2609 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002610 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2611 return BAD_VALUE;
2612 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002613 return stream->dropBuffers(dropping);
2614}
2615
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002616/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002617 * Camera3Device private methods
2618 */
2619
2620sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002621 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002622 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002623
2624 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002625 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626
2627 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002628 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002629 if (inputStreams.count > 0) {
2630 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002631 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002632 CLOGE("Request references unknown input stream %d",
2633 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002634 return NULL;
2635 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002636
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002637 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002638 SET_ERR_L("%s: input stream %d is not configured!",
2639 __FUNCTION__, mInputStream->getId());
2640 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002641 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002642 // Check if stream prepare is blocking requests.
2643 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002644 CLOGE("Request references an input stream that's being prepared!");
2645 return NULL;
2646 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002647
2648 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002649 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650 }
2651
2652 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002653 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002654 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002655 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002656 return NULL;
2657 }
2658
2659 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002660 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2661 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002662 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002663 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002664 return NULL;
2665 }
Zhijun He5d677d12016-05-29 16:52:39 -07002666 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002667 auto iter = surfaceMap.find(streams.data.i32[i]);
2668 if (iter != surfaceMap.end()) {
2669 const std::vector<size_t>& surfaces = iter->second;
2670 for (const auto& surface : surfaces) {
2671 if (stream->isConsumerConfigurationDeferred(surface)) {
2672 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2673 "due to deferred consumer", stream->getId(), surface);
2674 return NULL;
2675 }
2676 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002677 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002678 }
2679
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002681 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2682 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002683 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002684 // Check if stream prepare is blocking requests.
2685 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002686 CLOGE("Request references an output stream that's being prepared!");
2687 return NULL;
2688 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002689
2690 newRequest->mOutputStreams.push(stream);
2691 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002692 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002693 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002694
2695 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002696}
2697
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002698bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2699 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2700 Size size = mSupportedOpaqueInputSizes[i];
2701 if (size.width == width && size.height == height) {
2702 return true;
2703 }
2704 }
2705
2706 return false;
2707}
2708
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002709void Camera3Device::cancelStreamsConfigurationLocked() {
2710 int res = OK;
2711 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2712 res = mInputStream->cancelConfiguration();
2713 if (res != OK) {
2714 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2715 mInputStream->getId(), strerror(-res), res);
2716 }
2717 }
2718
2719 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002720 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002721 if (outputStream->isConfiguring()) {
2722 res = outputStream->cancelConfiguration();
2723 if (res != OK) {
2724 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2725 outputStream->getId(), strerror(-res), res);
2726 }
2727 }
2728 }
2729
2730 // Return state to that at start of call, so that future configures
2731 // properly clean things up
2732 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2733 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002734
2735 res = mPreparerThread->resume();
2736 if (res != OK) {
2737 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2738 }
2739}
2740
2741bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2742 ATRACE_CALL();
2743 bool ret = false;
2744
2745 Mutex::Autolock il(mInterfaceLock);
2746 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2747
2748 Mutex::Autolock l(mLock);
2749 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2750 if (rc == NO_ERROR) {
2751 mNeedConfig = true;
2752 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2753 if (rc == NO_ERROR) {
2754 ret = true;
2755 mPauseStateNotify = false;
2756 //Moving to active state while holding 'mLock' is important.
2757 //There could be pending calls to 'create-/deleteStream' which
2758 //will trigger another stream configuration while the already
2759 //present streams end up with outstanding buffers that will
2760 //not get drained.
2761 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002762 } else if (rc == DEAD_OBJECT) {
2763 // DEAD_OBJECT can be returned if either the consumer surface is
2764 // abandoned, or the HAL has died.
2765 // - If the HAL has died, configureStreamsLocked call will set
2766 // device to error state,
2767 // - If surface is abandoned, we should not set device to error
2768 // state.
2769 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002770 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002771 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002772 }
2773 } else {
2774 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2775 }
2776
2777 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002778}
2779
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002780status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002781 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 ATRACE_CALL();
2783 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002784
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002785 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002786 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002787 return INVALID_OPERATION;
2788 }
2789
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002790 if (operatingMode < 0) {
2791 CLOGE("Invalid operating mode: %d", operatingMode);
2792 return BAD_VALUE;
2793 }
2794
2795 bool isConstrainedHighSpeed =
2796 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2797 operatingMode;
2798
2799 if (mOperatingMode != operatingMode) {
2800 mNeedConfig = true;
2801 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2802 mOperatingMode = operatingMode;
2803 }
2804
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002805 // In case called from configureStreams, abort queued input buffers not belonging to
2806 // any pending requests.
2807 if (mInputStream != NULL && notifyRequestThread) {
2808 while (true) {
2809 camera3_stream_buffer_t inputBuffer;
2810 status_t res = mInputStream->getInputBuffer(&inputBuffer,
2811 /*respectHalLimit*/ false);
2812 if (res != OK) {
2813 // Exhausted acquiring all input buffers.
2814 break;
2815 }
2816
2817 inputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2818 res = mInputStream->returnInputBuffer(inputBuffer);
2819 if (res != OK) {
2820 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2821 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2822 }
2823 }
2824 }
2825
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002826 if (!mNeedConfig) {
2827 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2828 return OK;
2829 }
2830
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002831 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2832 // adding a dummy stream instead.
2833 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2834 if (mOutputStreams.size() == 0) {
2835 addDummyStreamLocked();
2836 } else {
2837 tryRemoveDummyStreamLocked();
2838 }
2839
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002840 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002841 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002842
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002843 mPreparerThread->pause();
2844
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002846 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002847 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2848
2849 Vector<camera3_stream_t*> streams;
2850 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002851 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002852
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853
2854 if (mInputStream != NULL) {
2855 camera3_stream_t *inputStream;
2856 inputStream = mInputStream->startConfiguration();
2857 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002858 CLOGE("Can't start input stream configuration");
2859 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002860 return INVALID_OPERATION;
2861 }
2862 streams.add(inputStream);
2863 }
2864
2865 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002866
2867 // Don't configure bidi streams twice, nor add them twice to the list
2868 if (mOutputStreams[i].get() ==
2869 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2870
2871 config.num_streams--;
2872 continue;
2873 }
2874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002875 camera3_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002876 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002878 CLOGE("Can't start output stream configuration");
2879 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002880 return INVALID_OPERATION;
2881 }
2882 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002883
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002884 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002885 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2886 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002887 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2888 bufferSizes[k] = static_cast<uint32_t>(
2889 getJpegBufferSize(outputStream->width, outputStream->height));
2890 } else if (outputStream->data_space ==
2891 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2892 bufferSizes[k] = outputStream->width * outputStream->height;
2893 } else {
2894 ALOGW("%s: Blob dataSpace %d not supported",
2895 __FUNCTION__, outputStream->data_space);
2896 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002897 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002898 }
2899
2900 config.streams = streams.editArray();
2901
2902 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002903 // max_buffers, usage, and priv fields, as well as data_space and format
2904 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002905
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002906 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002907 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002908 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002910 if (res == BAD_VALUE) {
2911 // HAL rejected this set of streams as unsupported, clean up config
2912 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002913 CLOGE("Set of requested inputs/outputs not supported by HAL");
2914 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002915 return BAD_VALUE;
2916 } else if (res != OK) {
2917 // Some other kind of error from configure_streams - this is not
2918 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002919 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2920 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 return res;
2922 }
2923
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002924 // Finish all stream configuration immediately.
2925 // TODO: Try to relax this later back to lazy completion, which should be
2926 // faster
2927
Igor Murashkin073f8572013-05-02 14:59:28 -07002928 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002929 bool streamReConfigured = false;
2930 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002931 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002932 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002933 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002934 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002935 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2936 return DEAD_OBJECT;
2937 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002938 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002939 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002940 if (streamReConfigured) {
2941 mInterface->onStreamReConfigured(mInputStream->getId());
2942 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002943 }
2944
2945 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002946 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002947 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002948 bool streamReConfigured = false;
2949 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002950 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002951 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002952 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002953 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002954 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2955 return DEAD_OBJECT;
2956 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002957 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002958 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002959 if (streamReConfigured) {
2960 mInterface->onStreamReConfigured(outputStream->getId());
2961 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002962 }
2963 }
2964
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002965 // Request thread needs to know to avoid using repeat-last-settings protocol
2966 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002967 if (notifyRequestThread) {
2968 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2969 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002970
Zhijun He90f7c372016-08-16 16:19:43 -07002971 char value[PROPERTY_VALUE_MAX];
2972 property_get("camera.fifo.disable", value, "0");
2973 int32_t disableFifo = atoi(value);
2974 if (disableFifo != 1) {
2975 // Boost priority of request thread to SCHED_FIFO.
2976 pid_t requestThreadTid = mRequestThread->getTid();
2977 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002978 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002979 if (res != OK) {
2980 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2981 strerror(-res), res);
2982 } else {
2983 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2984 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002985 }
2986
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002987 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002988 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2989 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2990 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2991 sessionParams.unlock(newSessionParams);
2992 mSessionParams.unlock(currentSessionParams);
2993 if (updateSessionParams) {
2994 mSessionParams = sessionParams;
2995 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002996
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002997 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002998
Ruben Brunk183f0562015-08-12 12:55:02 -07002999 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
3000 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003001
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003002 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003003
Zhijun He0a210512014-07-24 13:45:15 -07003004 // tear down the deleted streams after configure streams.
3005 mDeletedStreams.clear();
3006
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003007 auto rc = mPreparerThread->resume();
3008 if (rc != OK) {
3009 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
3010 return rc;
3011 }
3012
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003013 if (mDummyStreamId == NO_STREAM) {
3014 mRequestBufferSM.onStreamsConfigured();
3015 }
3016
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003017 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003018}
3019
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003020status_t Camera3Device::addDummyStreamLocked() {
3021 ATRACE_CALL();
3022 status_t res;
3023
3024 if (mDummyStreamId != NO_STREAM) {
3025 // Should never be adding a second dummy stream when one is already
3026 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003027 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
3028 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003029 return INVALID_OPERATION;
3030 }
3031
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003032 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003033
3034 sp<Camera3OutputStreamInterface> dummyStream =
3035 new Camera3DummyStream(mNextStreamId);
3036
3037 res = mOutputStreams.add(mNextStreamId, dummyStream);
3038 if (res < 0) {
3039 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
3040 return res;
3041 }
3042
3043 mDummyStreamId = mNextStreamId;
3044 mNextStreamId++;
3045
3046 return OK;
3047}
3048
3049status_t Camera3Device::tryRemoveDummyStreamLocked() {
3050 ATRACE_CALL();
3051 status_t res;
3052
3053 if (mDummyStreamId == NO_STREAM) return OK;
3054 if (mOutputStreams.size() == 1) return OK;
3055
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003056 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003057
3058 // Ok, have a dummy stream and there's at least one other output stream,
3059 // so remove the dummy
3060
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003061 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mDummyStreamId);
3062 if (deletedStream == nullptr) {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003063 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
3064 return INVALID_OPERATION;
3065 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003066 mOutputStreams.remove(mDummyStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003067
3068 // Free up the stream endpoint so that it can be used by some other stream
3069 res = deletedStream->disconnect();
3070 if (res != OK) {
3071 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
3072 // fall through since we want to still list the stream as deleted.
3073 }
3074 mDeletedStreams.add(deletedStream);
3075 mDummyStreamId = NO_STREAM;
3076
3077 return res;
3078}
3079
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003080void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003081 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003082 Mutex::Autolock l(mLock);
3083 va_list args;
3084 va_start(args, fmt);
3085
3086 setErrorStateLockedV(fmt, args);
3087
3088 va_end(args);
3089}
3090
3091void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003092 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003093 Mutex::Autolock l(mLock);
3094 setErrorStateLockedV(fmt, args);
3095}
3096
3097void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
3098 va_list args;
3099 va_start(args, fmt);
3100
3101 setErrorStateLockedV(fmt, args);
3102
3103 va_end(args);
3104}
3105
3106void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003107 // Print out all error messages to log
3108 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003109 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003110
3111 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07003112 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003113
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003114 mErrorCause = errorCause;
3115
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07003116 if (mRequestThread != nullptr) {
3117 mRequestThread->setPaused(true);
3118 }
Ruben Brunk183f0562015-08-12 12:55:02 -07003119 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003120
3121 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003122 sp<NotificationListener> listener = mListener.promote();
3123 if (listener != NULL) {
3124 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003125 CaptureResultExtras());
3126 }
3127
3128 // Save stack trace. View by dumping it later.
3129 CameraTraces::saveTrace();
3130 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003131}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003132
3133/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003134 * In-flight request management
3135 */
3136
Jianing Weicb0652e2014-03-12 18:29:36 -07003137status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07003138 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003139 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003140 std::set<String8>& physicalCameraIds, bool isStillCapture,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003141 bool isZslCapture, const SurfaceMap& outputSurfaces) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003142 ATRACE_CALL();
3143 Mutex::Autolock l(mInFlightLock);
3144
3145 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07003146 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003147 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
3148 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003149 if (res < 0) return res;
3150
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003151 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01003152 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3153 // avoid a deadlock during reprocess requests.
3154 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003155 if (mStatusTracker != nullptr) {
3156 mStatusTracker->markComponentActive(mInFlightStatusId);
3157 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003158 }
3159
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003160 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003161 return OK;
3162}
3163
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003164void Camera3Device::returnOutputBuffers(
3165 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003166 nsecs_t timestamp, bool timestampIncreasing,
3167 const SurfaceMap& outputSurfaces,
3168 const CaptureResultExtras &inResultExtras) {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003169
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003170 for (size_t i = 0; i < numBuffers; i++)
3171 {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003172 if (outputBuffers[i].buffer == nullptr) {
3173 if (!mUseHalBufManager) {
3174 // With HAL buffer management API, HAL sometimes will have to return buffers that
3175 // has not got a output buffer handle filled yet. This is though illegal if HAL
3176 // buffer management API is not being used.
3177 ALOGE("%s: cannot return a null buffer!", __FUNCTION__);
3178 }
3179 continue;
3180 }
3181
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003182 Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream);
3183 int streamId = stream->getId();
3184 const auto& it = outputSurfaces.find(streamId);
3185 status_t res = OK;
3186 if (it != outputSurfaces.end()) {
3187 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003188 outputBuffers[i], timestamp, timestampIncreasing, it->second,
3189 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003190 } else {
3191 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003192 outputBuffers[i], timestamp, timestampIncreasing, std::vector<size_t> (),
3193 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003194 }
3195
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003196 // Note: stream may be deallocated at this point, if this buffer was
3197 // the last reference to it.
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003198 if (res == NO_INIT || res == DEAD_OBJECT) {
3199 ALOGV("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
3200 } else if (res != OK) {
3201 ALOGE("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003202 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003203
3204 // Long processing consumers can cause returnBuffer timeout for shared stream
3205 // If that happens, cancel the buffer and send a buffer error to client
3206 if (it != outputSurfaces.end() && res == TIMED_OUT &&
3207 outputBuffers[i].status == CAMERA3_BUFFER_STATUS_OK) {
3208 // cancel the buffer
3209 camera3_stream_buffer_t sb = outputBuffers[i];
3210 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
Emilian Peev538c90e2018-12-17 18:03:19 +00003211 stream->returnBuffer(sb, /*timestamp*/0, timestampIncreasing, std::vector<size_t> (),
3212 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003213
3214 // notify client buffer error
3215 sp<NotificationListener> listener;
3216 {
3217 Mutex::Autolock l(mOutputLock);
3218 listener = mListener.promote();
3219 }
3220
3221 if (listener != nullptr) {
3222 CaptureResultExtras extras = inResultExtras;
3223 extras.errorStreamId = streamId;
3224 listener->notifyError(
3225 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
3226 extras);
3227 }
3228 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003229 }
3230}
3231
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003232void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003233 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003234 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003235 mInFlightMap.removeItemsAt(idx, 1);
3236
3237 // Indicate idle inFlightMap to the status tracker
3238 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003239 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01003240 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3241 // avoid a deadlock during reprocess requests.
3242 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003243 if (mStatusTracker != nullptr) {
3244 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
3245 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003246 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003247 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003248}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003249
3250void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
3251
3252 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3253 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
3254
3255 nsecs_t sensorTimestamp = request.sensorTimestamp;
3256 nsecs_t shutterTimestamp = request.shutterTimestamp;
3257
3258 // Check if it's okay to remove the request from InFlightMap:
3259 // In the case of a successful request:
3260 // all input and output buffers, all result metadata, shutter callback
3261 // arrived.
3262 // In the case of a unsuccessful request:
3263 // all input and output buffers arrived.
3264 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07003265 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003266 (request.haveResultMetadata && shutterTimestamp != 0))) {
Emilian Peev9dd21f42018-08-03 13:39:29 +01003267 if (request.stillCapture) {
3268 ATRACE_ASYNC_END("still capture", frameNumber);
3269 }
3270
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003271 ATRACE_ASYNC_END("frame capture", frameNumber);
3272
Shuzhen Wang403044a2017-02-26 23:29:04 -08003273 // Sanity check - if sensor timestamp matches shutter timestamp in the
3274 // case of request having callback.
3275 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003276 sensorTimestamp != shutterTimestamp) {
3277 SET_ERR("sensor timestamp (%" PRId64
3278 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
3279 sensorTimestamp, frameNumber, shutterTimestamp);
3280 }
3281
3282 // for an unsuccessful request, it may have pending output buffers to
3283 // return.
3284 assert(request.requestStatus != OK ||
3285 request.pendingOutputBuffers.size() == 0);
3286 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003287 request.pendingOutputBuffers.size(), 0, /*timestampIncreasing*/true,
3288 request.outputSurfaces, request.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003289
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003290 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003291 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
3292 }
3293
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07003294 // Sanity check - if we have too many in-flight frames with long total inflight duration,
3295 // something has likely gone wrong. This might still be legit only if application send in
3296 // a long burst of long exposure requests.
3297 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
3298 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
3299 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
3300 mInFlightMap.size(), mExpectedInflightDuration);
3301 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
3302 kInFlightWarnLimitHighSpeed) {
3303 CLOGW("In-flight list too large for high speed configuration: %zu,"
3304 "total inflight duration %" PRIu64,
3305 mInFlightMap.size(), mExpectedInflightDuration);
3306 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003307 }
3308}
3309
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003310void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003311 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003312 { // First return buffers cached in mInFlightMap
3313 Mutex::Autolock l(mInFlightLock);
3314 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
3315 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3316 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003317 request.pendingOutputBuffers.size(), 0,
3318 /*timestampIncreasing*/true, request.outputSurfaces,
3319 request.resultExtras);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003320 }
3321 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07003322 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003323 }
3324
3325 // Then return all inflight buffers not returned by HAL
3326 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
3327 mInterface->getInflightBufferKeys(&inflightKeys);
3328
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003329 // Inflight buffers for HAL buffer manager
3330 std::vector<uint64_t> inflightRequestBufferKeys;
3331 mInterface->getInflightRequestBufferKeys(&inflightRequestBufferKeys);
3332
3333 // (streamId, frameNumber, buffer_handle_t*) tuple for all inflight buffers.
3334 // frameNumber will be -1 for buffers from HAL buffer manager
3335 std::vector<std::tuple<int32_t, int32_t, buffer_handle_t*>> inflightBuffers;
3336 inflightBuffers.reserve(inflightKeys.size() + inflightRequestBufferKeys.size());
3337
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003338 for (auto& pair : inflightKeys) {
3339 int32_t frameNumber = pair.first;
3340 int32_t streamId = pair.second;
3341 buffer_handle_t* buffer;
3342 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
3343 if (res != OK) {
3344 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
3345 __FUNCTION__, frameNumber, streamId);
3346 continue;
3347 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003348 inflightBuffers.push_back(std::make_tuple(streamId, frameNumber, buffer));
3349 }
3350
3351 for (auto& bufferId : inflightRequestBufferKeys) {
3352 int32_t streamId = -1;
3353 buffer_handle_t* buffer = nullptr;
3354 status_t res = mInterface->popInflightRequestBuffer(bufferId, &buffer, &streamId);
3355 if (res != OK) {
3356 ALOGE("%s: cannot find in-flight buffer %" PRIu64, __FUNCTION__, bufferId);
3357 continue;
3358 }
3359 inflightBuffers.push_back(std::make_tuple(streamId, /*frameNumber*/-1, buffer));
3360 }
3361
3362 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
3363 for (auto& tuple : inflightBuffers) {
3364 status_t res = OK;
3365 int32_t streamId = std::get<0>(tuple);
3366 int32_t frameNumber = std::get<1>(tuple);
3367 buffer_handle_t* buffer = std::get<2>(tuple);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003368
3369 camera3_stream_buffer_t streamBuffer;
3370 streamBuffer.buffer = buffer;
3371 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3372 streamBuffer.acquire_fence = -1;
3373 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003374
3375 // First check if the buffer belongs to deleted stream
3376 bool streamDeleted = false;
3377 for (auto& stream : mDeletedStreams) {
3378 if (streamId == stream->getId()) {
3379 streamDeleted = true;
3380 // Return buffer to deleted stream
3381 camera3_stream* halStream = stream->asHalStream();
3382 streamBuffer.stream = halStream;
3383 switch (halStream->stream_type) {
3384 case CAMERA3_STREAM_OUTPUT:
Emilian Peev538c90e2018-12-17 18:03:19 +00003385 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0,
3386 /*timestampIncreasing*/true, std::vector<size_t> (), frameNumber);
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003387 if (res != OK) {
3388 ALOGE("%s: Can't return output buffer for frame %d to"
3389 " stream %d: %s (%d)", __FUNCTION__,
3390 frameNumber, streamId, strerror(-res), res);
3391 }
3392 break;
3393 case CAMERA3_STREAM_INPUT:
3394 res = stream->returnInputBuffer(streamBuffer);
3395 if (res != OK) {
3396 ALOGE("%s: Can't return input buffer for frame %d to"
3397 " stream %d: %s (%d)", __FUNCTION__,
3398 frameNumber, streamId, strerror(-res), res);
3399 }
3400 break;
3401 default: // Bi-direcitonal stream is deprecated
3402 ALOGE("%s: stream %d has unknown stream type %d",
3403 __FUNCTION__, streamId, halStream->stream_type);
3404 break;
3405 }
3406 break;
3407 }
3408 }
3409 if (streamDeleted) {
3410 continue;
3411 }
3412
3413 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003414 if (streamId == inputStreamId) {
3415 streamBuffer.stream = mInputStream->asHalStream();
3416 res = mInputStream->returnInputBuffer(streamBuffer);
3417 if (res != OK) {
3418 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003419 " stream %d: %s (%d)", __FUNCTION__,
3420 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003421 }
3422 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003423 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
3424 if (stream == nullptr) {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003425 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
3426 continue;
3427 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003428 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003429 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
3430 }
3431 }
3432}
3433
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003434void Camera3Device::insertResultLocked(CaptureResult *result,
3435 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003436 if (result == nullptr) return;
3437
Emilian Peev71c73a22017-03-21 16:35:51 +00003438 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
3439 result->mMetadata.getAndLock());
3440 set_camera_metadata_vendor_id(meta, mVendorTagId);
3441 result->mMetadata.unlock(meta);
3442
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003443 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
3444 (int32_t*)&frameNumber, 1) != OK) {
3445 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
3446 return;
3447 }
3448
3449 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
3450 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
3451 return;
3452 }
3453
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003454 // Update vendor tag id for physical metadata
3455 for (auto& physicalMetadata : result->mPhysicalMetadatas) {
3456 camera_metadata_t *pmeta = const_cast<camera_metadata_t *>(
3457 physicalMetadata.mPhysicalCameraMetadata.getAndLock());
3458 set_camera_metadata_vendor_id(pmeta, mVendorTagId);
3459 physicalMetadata.mPhysicalCameraMetadata.unlock(pmeta);
3460 }
3461
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003462 // Valid result, insert into queue
3463 List<CaptureResult>::iterator queuedResult =
3464 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
3465 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
3466 ", burstId = %" PRId32, __FUNCTION__,
3467 queuedResult->mResultExtras.requestId,
3468 queuedResult->mResultExtras.frameNumber,
3469 queuedResult->mResultExtras.burstId);
3470
3471 mResultSignal.signal();
3472}
3473
3474
3475void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003476 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003477 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003478 Mutex::Autolock l(mOutputLock);
3479
3480 CaptureResult captureResult;
3481 captureResult.mResultExtras = resultExtras;
3482 captureResult.mMetadata = partialResult;
3483
Shuzhen Wang268a1362018-10-16 16:32:59 -07003484 // Fix up result metadata for monochrome camera.
3485 status_t res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3486 if (res != OK) {
3487 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3488 return;
3489 }
3490
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003491 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003492}
3493
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003494
3495void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
3496 CaptureResultExtras &resultExtras,
3497 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003498 uint32_t frameNumber,
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003499 bool reprocess, bool zslStillCapture,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003500 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003501 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003502 if (pendingMetadata.isEmpty())
3503 return;
3504
3505 Mutex::Autolock l(mOutputLock);
3506
3507 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003508 if (reprocess) {
3509 if (frameNumber < mNextReprocessResultFrameNumber) {
3510 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003511 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003512 frameNumber, mNextReprocessResultFrameNumber);
3513 return;
3514 }
3515 mNextReprocessResultFrameNumber = frameNumber + 1;
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003516 } else if (zslStillCapture) {
3517 if (frameNumber < mNextZslStillResultFrameNumber) {
3518 SET_ERR("Out-of-order ZSL still capture result metadata submitted! "
3519 "(got frame number %d, expecting %d)",
3520 frameNumber, mNextZslStillResultFrameNumber);
3521 return;
3522 }
3523 mNextZslStillResultFrameNumber = frameNumber + 1;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003524 } else {
3525 if (frameNumber < mNextResultFrameNumber) {
3526 SET_ERR("Out-of-order capture result metadata submitted! "
3527 "(got frame number %d, expecting %d)",
3528 frameNumber, mNextResultFrameNumber);
3529 return;
3530 }
3531 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003532 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003533
3534 CaptureResult captureResult;
3535 captureResult.mResultExtras = resultExtras;
3536 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003537 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003538
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003539 // Append any previous partials to form a complete result
3540 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
3541 captureResult.mMetadata.append(collectedPartialResult);
3542 }
3543
3544 captureResult.mMetadata.sort();
3545
3546 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003547 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3548 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003549 SET_ERR("No timestamp provided by HAL for frame %d!",
3550 frameNumber);
3551 return;
3552 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003553 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3554 camera_metadata_entry timestamp =
3555 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3556 if (timestamp.count == 0) {
3557 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
3558 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
3559 return;
3560 }
3561 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003562
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003563 // Fix up some result metadata to account for HAL-level distortion correction
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003564 status_t res =
3565 mDistortionMappers[mId.c_str()].correctCaptureResult(&captureResult.mMetadata);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003566 if (res != OK) {
3567 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
3568 frameNumber, strerror(res), res);
3569 return;
3570 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003571 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3572 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
3573 if (mDistortionMappers.find(cameraId8.c_str()) == mDistortionMappers.end()) {
3574 continue;
3575 }
3576 res = mDistortionMappers[cameraId8.c_str()].correctCaptureResult(
3577 &physicalMetadata.mPhysicalCameraMetadata);
3578 if (res != OK) {
3579 SET_ERR("Unable to correct physical capture result metadata for frame %d: %s (%d)",
3580 frameNumber, strerror(res), res);
3581 return;
3582 }
3583 }
3584
Shuzhen Wang268a1362018-10-16 16:32:59 -07003585 // Fix up result metadata for monochrome camera.
3586 res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3587 if (res != OK) {
3588 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3589 return;
3590 }
3591 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3592 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
3593 res = fixupMonochromeTags(mPhysicalDeviceInfoMap.at(cameraId8.c_str()),
3594 physicalMetadata.mPhysicalCameraMetadata);
3595 if (res != OK) {
3596 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3597 return;
3598 }
3599 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003600
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003601 std::unordered_map<std::string, CameraMetadata> monitoredPhysicalMetadata;
3602 for (auto& m : physicalMetadatas) {
3603 monitoredPhysicalMetadata.emplace(String8(m.mPhysicalCameraId).string(),
3604 CameraMetadata(m.mPhysicalCameraMetadata));
3605 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003606 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003607 frameNumber, timestamp.data.i64[0], captureResult.mMetadata,
3608 monitoredPhysicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003609
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003610 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003611}
3612
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003613/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003614 * Camera HAL device callback methods
3615 */
3616
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003617void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003618 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003619
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003620 status_t res;
3621
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003622 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003623 if (result->result == NULL && result->num_output_buffers == 0 &&
3624 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003625 SET_ERR("No result data provided by HAL for frame %d",
3626 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003627 return;
3628 }
Zhijun He204e3292014-07-14 17:09:23 -07003629
Zhijun He204e3292014-07-14 17:09:23 -07003630 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003631 result->result != NULL &&
3632 result->partial_result != 1) {
3633 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3634 " if partial result is not supported",
3635 frameNumber, result->partial_result);
3636 return;
3637 }
3638
3639 bool isPartialResult = false;
3640 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003641 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003642
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003643 // Get shutter timestamp and resultExtras from list of in-flight requests,
3644 // where it was added by the shutter notification for this frame. If the
3645 // shutter timestamp isn't received yet, append the output buffers to the
3646 // in-flight request and they will be returned when the shutter timestamp
3647 // arrives. Update the in-flight status and remove the in-flight entry if
3648 // all result data and shutter timestamp have been received.
3649 nsecs_t shutterTimestamp = 0;
3650
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003651 {
3652 Mutex::Autolock l(mInFlightLock);
3653 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3654 if (idx == NAME_NOT_FOUND) {
3655 SET_ERR("Unknown frame number for capture result: %d",
3656 frameNumber);
3657 return;
3658 }
3659 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003660 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3661 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003662 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003663 __FUNCTION__, request.resultExtras.requestId,
3664 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003665 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003666 // Always update the partial count to the latest one if it's not 0
3667 // (buffers only). When framework aggregates adjacent partial results
3668 // into one, the latest partial count will be used.
3669 if (result->partial_result != 0)
3670 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003671
3672 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003673 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003674 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3675 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3676 " the range of [1, %d] when metadata is included in the result",
3677 frameNumber, result->partial_result, mNumPartialResults);
3678 return;
3679 }
3680 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003681 if (isPartialResult && result->num_physcam_metadata) {
3682 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3683 " physical camera result", frameNumber);
3684 return;
3685 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003686 if (isPartialResult) {
3687 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003688 }
3689
Shuzhen Wang4a472662017-02-26 23:29:04 -08003690 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003691 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003692 sendPartialCaptureResult(result->result, request.resultExtras,
3693 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003694 }
3695 }
3696
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003697 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003698 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003699
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003700 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003701 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003702 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003703 SET_ERR("Expected physical Camera metadata count %d not equal to actual count %d",
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003704 request.physicalCameraIds.size(), result->num_physcam_metadata);
3705 return;
3706 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003707 if (request.haveResultMetadata) {
3708 SET_ERR("Called multiple times with metadata for frame %d",
3709 frameNumber);
3710 return;
3711 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003712 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3713 String8 physicalId(result->physcam_ids[i]);
3714 std::set<String8>::iterator cameraIdIter =
3715 request.physicalCameraIds.find(physicalId);
3716 if (cameraIdIter != request.physicalCameraIds.end()) {
3717 request.physicalCameraIds.erase(cameraIdIter);
3718 } else {
3719 SET_ERR("Total result for frame %d has already returned for camera %s",
3720 frameNumber, physicalId.c_str());
3721 return;
3722 }
3723 }
Zhijun He204e3292014-07-14 17:09:23 -07003724 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003725 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003726 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003727 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003728 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003729 request.haveResultMetadata = true;
3730 }
3731
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003732 uint32_t numBuffersReturned = result->num_output_buffers;
3733 if (result->input_buffer != NULL) {
3734 if (hasInputBufferInRequest) {
3735 numBuffersReturned += 1;
3736 } else {
3737 ALOGW("%s: Input buffer should be NULL if there is no input"
3738 " buffer sent in the request",
3739 __FUNCTION__);
3740 }
3741 }
3742 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003743 if (request.numBuffersLeft < 0) {
3744 SET_ERR("Too many buffers returned for frame %d",
3745 frameNumber);
3746 return;
3747 }
3748
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003749 camera_metadata_ro_entry_t entry;
3750 res = find_camera_metadata_ro_entry(result->result,
3751 ANDROID_SENSOR_TIMESTAMP, &entry);
3752 if (res == OK && entry.count == 1) {
3753 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003754 }
3755
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003756 // If shutter event isn't received yet, append the output buffers to
3757 // the in-flight request. Otherwise, return the output buffers to
3758 // streams.
3759 if (shutterTimestamp == 0) {
3760 request.pendingOutputBuffers.appendArray(result->output_buffers,
3761 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003762 } else {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003763 bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003764 returnOutputBuffers(result->output_buffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003765 result->num_output_buffers, shutterTimestamp, timestampIncreasing,
3766 request.outputSurfaces, request.resultExtras);
Igor Murashkind2c90692013-04-02 12:32:32 -07003767 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003768
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003769 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003770 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3771 CameraMetadata physicalMetadata;
3772 physicalMetadata.append(result->physcam_metadata[i]);
3773 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3774 physicalMetadata});
3775 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003776 if (shutterTimestamp == 0) {
3777 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003778 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang268a1362018-10-16 16:32:59 -07003779 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003780 CameraMetadata metadata;
3781 metadata = result->result;
3782 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003783 collectedPartialResult, frameNumber,
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003784 hasInputBufferInRequest, request.zslCapture && request.stillCapture,
3785 request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003786 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003787 }
3788
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003789 removeInFlightRequestIfReadyLocked(idx);
3790 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003791
Zhijun Hef0d962a2014-06-30 10:24:11 -07003792 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003793 if (hasInputBufferInRequest) {
3794 Camera3Stream *stream =
3795 Camera3Stream::cast(result->input_buffer->stream);
3796 res = stream->returnInputBuffer(*(result->input_buffer));
3797 // Note: stream may be deallocated at this point, if this buffer was the
3798 // last reference to it.
3799 if (res != OK) {
3800 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3801 " its stream:%s (%d)", __FUNCTION__,
3802 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003803 }
3804 } else {
3805 ALOGW("%s: Input buffer should be NULL if there is no input"
3806 " buffer sent in the request, skipping input buffer return.",
3807 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003808 }
3809 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003810}
3811
3812void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003813 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003814 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003815 {
3816 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003817 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003818 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003819
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003820 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003821 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003822 return;
3823 }
3824
3825 switch (msg->type) {
3826 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003827 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003828 break;
3829 }
3830 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003831 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003832 break;
3833 }
3834 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003835 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003836 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003837 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003838}
3839
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003840void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003841 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003842 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003843 // Map camera HAL error codes to ICameraDeviceCallback error codes
3844 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003845 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003846 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003847 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003848 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003849 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003850 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003851 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003852 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003853 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003854 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003855 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003856 };
3857
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003858 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003859 ((msg.error_code >= 0) &&
3860 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3861 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003862 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003863
3864 int streamId = 0;
Emilian Peevedec62d2019-03-19 17:59:24 -07003865 String16 physicalCameraId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003866 if (msg.error_stream != NULL) {
3867 Camera3Stream *stream =
3868 Camera3Stream::cast(msg.error_stream);
3869 streamId = stream->getId();
Emilian Peevedec62d2019-03-19 17:59:24 -07003870 physicalCameraId = String16(stream->physicalCameraId());
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003871 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003872 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3873 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003874 streamId, msg.error_code);
3875
3876 CaptureResultExtras resultExtras;
3877 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003878 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003879 // SET_ERR calls notifyError
3880 SET_ERR("Camera HAL reported serious device error");
3881 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003882 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3883 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3884 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003885 {
3886 Mutex::Autolock l(mInFlightLock);
3887 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3888 if (idx >= 0) {
3889 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3890 r.requestStatus = msg.error_code;
3891 resultExtras = r.resultExtras;
Emilian Peevedec62d2019-03-19 17:59:24 -07003892 bool logicalDeviceResultError = false;
3893 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3894 errorCode) {
3895 if (physicalCameraId.size() > 0) {
3896 String8 cameraId(physicalCameraId);
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003897 auto iter = r.physicalCameraIds.find(cameraId);
3898 if (iter == r.physicalCameraIds.end()) {
Emilian Peevedec62d2019-03-19 17:59:24 -07003899 ALOGE("%s: Reported result failure for physical camera device: %s "
3900 " which is not part of the respective request!",
3901 __FUNCTION__, cameraId.string());
3902 break;
3903 }
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003904 r.physicalCameraIds.erase(iter);
Emilian Peevedec62d2019-03-19 17:59:24 -07003905 resultExtras.errorPhysicalCameraId = physicalCameraId;
3906 } else {
3907 logicalDeviceResultError = true;
3908 }
3909 }
3910
3911 if (logicalDeviceResultError
Shuzhen Wang20f57342017-08-24 15:39:05 -07003912 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3913 errorCode) {
3914 r.skipResultMetadata = true;
3915 }
Emilian Peevedec62d2019-03-19 17:59:24 -07003916 if (logicalDeviceResultError) {
Emilian Peevba0fac32017-03-30 09:05:34 +01003917 // In case of missing result check whether the buffers
3918 // returned. If they returned, then remove inflight
3919 // request.
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003920 // TODO: should we call this for ERROR_CAMERA_REQUEST as well?
3921 // otherwise we are depending on HAL to send the buffers back after
3922 // calling notifyError. Not sure if that's in the spec.
Emilian Peevba0fac32017-03-30 09:05:34 +01003923 removeInFlightRequestIfReadyLocked(idx);
3924 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003925 } else {
3926 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003927 ALOGE("Camera %s: %s: cannot find in-flight request on "
3928 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003929 resultExtras.frameNumber);
3930 }
3931 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003932 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003933 if (listener != NULL) {
3934 listener->notifyError(errorCode, resultExtras);
3935 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003936 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003937 }
3938 break;
3939 default:
3940 // SET_ERR calls notifyError
3941 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3942 break;
3943 }
3944}
3945
3946void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003947 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003948 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003949 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003950
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003951 // Set timestamp for the request in the in-flight tracking
3952 // and get the request ID to send upstream
3953 {
3954 Mutex::Autolock l(mInFlightLock);
3955 idx = mInFlightMap.indexOfKey(msg.frame_number);
3956 if (idx >= 0) {
3957 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003958
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003959 // Verify ordering of shutter notifications
3960 {
3961 Mutex::Autolock l(mOutputLock);
3962 // TODO: need to track errors for tighter bounds on expected frame number.
3963 if (r.hasInputBuffer) {
3964 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003965 SET_ERR("Reprocess shutter notification out-of-order. Expected "
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003966 "notification for frame %d, got frame %d",
3967 mNextReprocessShutterFrameNumber, msg.frame_number);
3968 return;
3969 }
3970 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003971 } else if (r.zslCapture && r.stillCapture) {
3972 if (msg.frame_number < mNextZslStillShutterFrameNumber) {
3973 SET_ERR("ZSL still capture shutter notification out-of-order. Expected "
3974 "notification for frame %d, got frame %d",
3975 mNextZslStillShutterFrameNumber, msg.frame_number);
3976 return;
3977 }
3978 mNextZslStillShutterFrameNumber = msg.frame_number + 1;
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003979 } else {
3980 if (msg.frame_number < mNextShutterFrameNumber) {
3981 SET_ERR("Shutter notification out-of-order. Expected "
3982 "notification for frame %d, got frame %d",
3983 mNextShutterFrameNumber, msg.frame_number);
3984 return;
3985 }
3986 mNextShutterFrameNumber = msg.frame_number + 1;
3987 }
3988 }
3989
Shuzhen Wang4a472662017-02-26 23:29:04 -08003990 r.shutterTimestamp = msg.timestamp;
3991 if (r.hasCallback) {
3992 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003993 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003994 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003995 // Call listener, if any
3996 if (listener != NULL) {
3997 listener->notifyShutter(r.resultExtras, msg.timestamp);
3998 }
3999 // send pending result and buffers
4000 sendCaptureResult(r.pendingMetadata, r.resultExtras,
4001 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5ee99842019-04-12 11:55:48 -07004002 r.hasInputBuffer, r.zslCapture && r.stillCapture,
4003 r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004004 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004005 bool timestampIncreasing = !(r.zslCapture || r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004006 returnOutputBuffers(r.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004007 r.pendingOutputBuffers.size(), r.shutterTimestamp, timestampIncreasing,
4008 r.outputSurfaces, r.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004009 r.pendingOutputBuffers.clear();
4010
4011 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004012 }
4013 }
4014 if (idx < 0) {
4015 SET_ERR("Shutter notification for non-existent frame number %d",
4016 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004017 }
4018}
4019
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004020CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07004021 ALOGV("%s", __FUNCTION__);
4022
Igor Murashkin1e479c02013-09-06 16:55:14 -07004023 CameraMetadata retVal;
4024
4025 if (mRequestThread != NULL) {
4026 retVal = mRequestThread->getLatestRequest();
4027 }
4028
Igor Murashkin1e479c02013-09-06 16:55:14 -07004029 return retVal;
4030}
4031
Jianing Weicb0652e2014-03-12 18:29:36 -07004032
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07004033void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004034 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
4035 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata) {
4036
4037 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
4038 physicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07004039}
4040
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004041/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004042 * HalInterface inner class methods
4043 */
4044
Yifan Hongf79b5542017-04-11 14:44:25 -07004045Camera3Device::HalInterface::HalInterface(
4046 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004047 std::shared_ptr<RequestMetadataQueue> queue,
4048 bool useHalBufManager) :
Yifan Hongf79b5542017-04-11 14:44:25 -07004049 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004050 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08004051 mUseHalBufManager(useHalBufManager),
4052 mIsReconfigurationQuerySupported(true) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004053 // Check with hardware service manager if we can downcast these interfaces
4054 // Somewhat expensive, so cache the results at startup
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004055 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
4056 if (castResult_3_5.isOk()) {
4057 mHidlSession_3_5 = castResult_3_5;
4058 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004059 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4060 if (castResult_3_4.isOk()) {
4061 mHidlSession_3_4 = castResult_3_4;
4062 }
4063 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
4064 if (castResult_3_3.isOk()) {
4065 mHidlSession_3_3 = castResult_3_3;
4066 }
4067}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004068
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004069Camera3Device::HalInterface::HalInterface() : mUseHalBufManager(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004070
4071Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07004072 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004073 mRequestMetadataQueue(other.mRequestMetadataQueue),
4074 mUseHalBufManager(other.mUseHalBufManager) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004075
4076bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004077 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004078}
4079
4080void Camera3Device::HalInterface::clear() {
Emilian Peev644a3e12018-11-23 13:52:39 +00004081 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00004082 mHidlSession_3_4.clear();
4083 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004084 mHidlSession.clear();
4085}
4086
4087status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
4088 camera3_request_template_t templateId,
4089 /*out*/ camera_metadata_t **requestTemplate) {
4090 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
4091 if (!valid()) return INVALID_OPERATION;
4092 status_t res = OK;
4093
Emilian Peev31abd0a2017-05-11 18:37:46 +01004094 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004095
4096 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01004097 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004098 status = s;
4099 if (status == common::V1_0::Status::OK) {
4100 const camera_metadata *r =
4101 reinterpret_cast<const camera_metadata_t*>(request.data());
4102 size_t expectedSize = request.size();
4103 int ret = validate_camera_metadata_structure(r, &expectedSize);
4104 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
4105 *requestTemplate = clone_camera_metadata(r);
4106 if (*requestTemplate == nullptr) {
4107 ALOGE("%s: Unable to clone camera metadata received from HAL",
4108 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004109 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004110 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004111 } else {
4112 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
4113 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004114 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004115 }
4116 };
4117 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004118 RequestTemplate id;
4119 switch (templateId) {
4120 case CAMERA3_TEMPLATE_PREVIEW:
4121 id = RequestTemplate::PREVIEW;
4122 break;
4123 case CAMERA3_TEMPLATE_STILL_CAPTURE:
4124 id = RequestTemplate::STILL_CAPTURE;
4125 break;
4126 case CAMERA3_TEMPLATE_VIDEO_RECORD:
4127 id = RequestTemplate::VIDEO_RECORD;
4128 break;
4129 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
4130 id = RequestTemplate::VIDEO_SNAPSHOT;
4131 break;
4132 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
4133 id = RequestTemplate::ZERO_SHUTTER_LAG;
4134 break;
4135 case CAMERA3_TEMPLATE_MANUAL:
4136 id = RequestTemplate::MANUAL;
4137 break;
4138 default:
4139 // Unknown template ID, or this HAL is too old to support it
4140 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004141 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004142 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004143
Emilian Peev31abd0a2017-05-11 18:37:46 +01004144 if (!err.isOk()) {
4145 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4146 res = DEAD_OBJECT;
4147 } else {
4148 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004149 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004150
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004151 return res;
4152}
4153
Emilian Peev4ec17882019-01-24 17:16:58 -08004154bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
4155 CameraMetadata& newSessionParams) {
4156 // We do reconfiguration by default;
4157 bool ret = true;
4158 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
4159 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
4160 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
4161 oldSessionParams.getAndLock());
4162 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
4163 newSessionParams.getAndLock());
4164 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
4165 get_camera_metadata_size(oldSessioMeta));
4166 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
4167 get_camera_metadata_size(newSessioMeta));
4168 hardware::camera::common::V1_0::Status callStatus;
4169 bool required;
4170 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
4171 bool requiredFlag) {
4172 callStatus = s;
4173 required = requiredFlag;
4174 };
4175 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
4176 oldSessionParams.unlock(oldSessioMeta);
4177 newSessionParams.unlock(newSessioMeta);
4178 if (err.isOk()) {
4179 switch (callStatus) {
4180 case hardware::camera::common::V1_0::Status::OK:
4181 ret = required;
4182 break;
4183 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
4184 mIsReconfigurationQuerySupported = false;
4185 ret = true;
4186 break;
4187 default:
4188 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
4189 ret = true;
4190 }
4191 } else {
4192 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
4193 ret = true;
4194 }
4195 }
4196
4197 return ret;
4198}
4199
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004200status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00004201 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004202 ATRACE_NAME("CameraHal::configureStreams");
4203 if (!valid()) return INVALID_OPERATION;
4204 status_t res = OK;
4205
Emilian Peev31abd0a2017-05-11 18:37:46 +01004206 // Convert stream config to HIDL
4207 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004208 device::V3_2::StreamConfiguration requestedConfiguration3_2;
4209 device::V3_4::StreamConfiguration requestedConfiguration3_4;
4210 requestedConfiguration3_2.streams.resize(config->num_streams);
4211 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004212 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004213 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
4214 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01004215 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004216
Emilian Peev31abd0a2017-05-11 18:37:46 +01004217 Camera3Stream* cam3stream = Camera3Stream::cast(src);
4218 cam3stream->setBufferFreedListener(this);
4219 int streamId = cam3stream->getId();
4220 StreamType streamType;
4221 switch (src->stream_type) {
4222 case CAMERA3_STREAM_OUTPUT:
4223 streamType = StreamType::OUTPUT;
4224 break;
4225 case CAMERA3_STREAM_INPUT:
4226 streamType = StreamType::INPUT;
4227 break;
4228 default:
4229 ALOGE("%s: Stream %d: Unsupported stream type %d",
4230 __FUNCTION__, streamId, config->streams[i]->stream_type);
4231 return BAD_VALUE;
4232 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004233 dst3_2.id = streamId;
4234 dst3_2.streamType = streamType;
4235 dst3_2.width = src->width;
4236 dst3_2.height = src->height;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004237 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004238 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Shuzhen Wang92653952019-05-07 15:11:43 -07004239 // For HidlSession version 3.5 or newer, the format and dataSpace sent
4240 // to HAL are original, not the overriden ones.
4241 if (mHidlSession_3_5 != nullptr) {
4242 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
4243 cam3stream->getOriginalFormat() : src->format);
4244 dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
4245 cam3stream->getOriginalDataSpace() : src->data_space);
4246 } else {
4247 dst3_2.format = mapToPixelFormat(src->format);
4248 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
4249 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004250 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00004251 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004252 if (src->physical_camera_id != nullptr) {
4253 dst3_4.physicalCameraId = src->physical_camera_id;
4254 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004255
4256 activeStreams.insert(streamId);
4257 // Create Buffer ID map if necessary
4258 if (mBufferIdMaps.count(streamId) == 0) {
4259 mBufferIdMaps.emplace(streamId, BufferIdMap{});
4260 }
4261 }
4262 // remove BufferIdMap for deleted streams
4263 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
4264 int streamId = it->first;
4265 bool active = activeStreams.count(streamId) > 0;
4266 if (!active) {
4267 it = mBufferIdMaps.erase(it);
4268 } else {
4269 ++it;
4270 }
4271 }
4272
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004273 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004274 res = mapToStreamConfigurationMode(
4275 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004276 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004277 if (res != OK) {
4278 return res;
4279 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004280 requestedConfiguration3_2.operationMode = operationMode;
4281 requestedConfiguration3_4.operationMode = operationMode;
4282 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004283 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
4284 get_camera_metadata_size(sessionParams));
4285
Emilian Peev31abd0a2017-05-11 18:37:46 +01004286 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004287 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004288 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004289 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004290
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004291 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004292 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
4293 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004294 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004295 };
4296
4297 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
4298 (hardware::Return<void>& err) -> status_t {
4299 if (!err.isOk()) {
4300 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4301 return DEAD_OBJECT;
4302 }
4303 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
4304 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
4305 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
4306 }
4307 return OK;
4308 };
4309
Shuzhen Wang92653952019-05-07 15:11:43 -07004310 // See which version of HAL we have
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004311 if (mHidlSession_3_5 != nullptr) {
4312 ALOGV("%s: v3.5 device found", __FUNCTION__);
4313 device::V3_5::StreamConfiguration requestedConfiguration3_5;
4314 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
4315 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
4316 auto err = mHidlSession_3_5->configureStreams_3_5(
4317 requestedConfiguration3_5, configStream34Cb);
4318 res = postprocConfigStream34(err);
4319 if (res != OK) {
4320 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004321 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004322 } else if (mHidlSession_3_4 != nullptr) {
4323 // We do; use v3.4 for the call
4324 ALOGV("%s: v3.4 device found", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004325 auto err = mHidlSession_3_4->configureStreams_3_4(
4326 requestedConfiguration3_4, configStream34Cb);
4327 res = postprocConfigStream34(err);
4328 if (res != OK) {
4329 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004330 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004331 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004332 // We do; use v3.3 for the call
4333 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004334 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01004335 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004336 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004337 finalConfiguration = halConfiguration;
4338 status = s;
4339 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004340 if (!err.isOk()) {
4341 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4342 return DEAD_OBJECT;
4343 }
4344 } else {
4345 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
4346 ALOGV("%s: v3.2 device found", __FUNCTION__);
4347 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004348 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004349 [&status, &finalConfiguration_3_2]
4350 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
4351 finalConfiguration_3_2 = halConfiguration;
4352 status = s;
4353 });
4354 if (!err.isOk()) {
4355 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4356 return DEAD_OBJECT;
4357 }
4358 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
4359 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
4360 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
4361 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004362 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004363 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004364 }
4365
4366 if (status != common::V1_0::Status::OK ) {
4367 return CameraProviderManager::mapToStatusT(status);
4368 }
4369
4370 // And convert output stream configuration from HIDL
4371
4372 for (size_t i = 0; i < config->num_streams; i++) {
4373 camera3_stream_t *dst = config->streams[i];
4374 int streamId = Camera3Stream::cast(dst)->getId();
4375
4376 // Start scan at i, with the assumption that the stream order matches
4377 size_t realIdx = i;
4378 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004379 size_t halStreamCount = finalConfiguration.streams.size();
4380 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004381 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004382 found = true;
4383 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004384 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004385 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004386 }
4387 if (!found) {
4388 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
4389 __FUNCTION__, streamId);
4390 return INVALID_OPERATION;
4391 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004392 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004393
Emilian Peev710c1422017-08-30 11:19:38 +01004394 Camera3Stream* dstStream = Camera3Stream::cast(dst);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004395 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
4396 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
4397
Yin-Chia Yeh90667662019-07-01 15:45:00 -07004398 if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Shuzhen Wang92653952019-05-07 15:11:43 -07004399 dstStream->setFormatOverride(false);
4400 dstStream->setDataSpaceOverride(false);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004401 if (dst->format != overrideFormat) {
4402 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
4403 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004404 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004405 if (dst->data_space != overrideDataSpace) {
4406 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
4407 streamId, dst->format);
4408 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004409 } else {
Shuzhen Wang92653952019-05-07 15:11:43 -07004410 bool needFormatOverride =
4411 requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
4412 bool needDataspaceOverride =
4413 requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004414 // Override allowed with IMPLEMENTATION_DEFINED
Shuzhen Wang92653952019-05-07 15:11:43 -07004415 dstStream->setFormatOverride(needFormatOverride);
4416 dstStream->setDataSpaceOverride(needDataspaceOverride);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004417 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004418 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004419 }
4420
Emilian Peev31abd0a2017-05-11 18:37:46 +01004421 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004422 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004423 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004424 __FUNCTION__, streamId);
4425 return INVALID_OPERATION;
4426 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004427 dstStream->setUsage(
4428 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01004429 } else {
4430 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004431 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004432 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
4433 __FUNCTION__, streamId);
4434 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004435 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004436 dstStream->setUsage(
4437 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004438 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004439 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004440 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004441
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004442 return res;
4443}
4444
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004445status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004446 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004447 /*out*/std::vector<native_handle_t*>* handlesCreated,
4448 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004449 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004450 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
4451 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
4452 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004453 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004454 }
4455
4456 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07004457
4458 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004459
4460 {
4461 std::lock_guard<std::mutex> lock(mInflightLock);
4462 if (request->input_buffer != nullptr) {
4463 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
4464 buffer_handle_t buf = *(request->input_buffer->buffer);
4465 auto pair = getBufferId(buf, streamId);
4466 bool isNewBuffer = pair.first;
4467 uint64_t bufferId = pair.second;
4468 captureRequest->inputBuffer.streamId = streamId;
4469 captureRequest->inputBuffer.bufferId = bufferId;
4470 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
4471 captureRequest->inputBuffer.status = BufferStatus::OK;
4472 native_handle_t *acquireFence = nullptr;
4473 if (request->input_buffer->acquire_fence != -1) {
4474 acquireFence = native_handle_create(1,0);
4475 acquireFence->data[0] = request->input_buffer->acquire_fence;
4476 handlesCreated->push_back(acquireFence);
4477 }
4478 captureRequest->inputBuffer.acquireFence = acquireFence;
4479 captureRequest->inputBuffer.releaseFence = nullptr;
4480
4481 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004482 request->input_buffer->buffer);
4483 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004484 } else {
4485 captureRequest->inputBuffer.streamId = -1;
4486 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
4487 }
4488
4489 captureRequest->outputBuffers.resize(request->num_output_buffers);
4490 for (size_t i = 0; i < request->num_output_buffers; i++) {
4491 const camera3_stream_buffer_t *src = request->output_buffers + i;
4492 StreamBuffer &dst = captureRequest->outputBuffers[i];
4493 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004494 if (src->buffer != nullptr) {
4495 buffer_handle_t buf = *(src->buffer);
4496 auto pair = getBufferId(buf, streamId);
4497 bool isNewBuffer = pair.first;
4498 dst.bufferId = pair.second;
4499 dst.buffer = isNewBuffer ? buf : nullptr;
4500 native_handle_t *acquireFence = nullptr;
4501 if (src->acquire_fence != -1) {
4502 acquireFence = native_handle_create(1,0);
4503 acquireFence->data[0] = src->acquire_fence;
4504 handlesCreated->push_back(acquireFence);
4505 }
4506 dst.acquireFence = acquireFence;
4507 } else if (mUseHalBufManager) {
4508 // HAL buffer management path
4509 dst.bufferId = BUFFER_ID_NO_BUFFER;
4510 dst.buffer = nullptr;
4511 dst.acquireFence = nullptr;
4512 } else {
4513 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
4514 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004515 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004516 dst.streamId = streamId;
4517 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004518 dst.releaseFence = nullptr;
4519
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004520 // Output buffers are empty when using HAL buffer manager
4521 if (!mUseHalBufManager) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004522 pushInflightBufferLocked(captureRequest->frameNumber, streamId, src->buffer);
4523 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004524 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004525 }
4526 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004527 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004528}
4529
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004530void Camera3Device::HalInterface::cleanupNativeHandles(
4531 std::vector<native_handle_t*> *handles, bool closeFd) {
4532 if (handles == nullptr) {
4533 return;
4534 }
4535 if (closeFd) {
4536 for (auto& handle : *handles) {
4537 native_handle_close(handle);
4538 }
4539 }
4540 for (auto& handle : *handles) {
4541 native_handle_delete(handle);
4542 }
4543 handles->clear();
4544 return;
4545}
4546
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004547status_t Camera3Device::HalInterface::processBatchCaptureRequests(
4548 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
4549 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
4550 if (!valid()) return INVALID_OPERATION;
4551
Emilian Peevaebbe412018-01-15 13:53:24 +00004552 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
4553 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4554 if (castResult_3_4.isOk()) {
4555 hidlSession_3_4 = castResult_3_4;
4556 }
4557
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004558 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00004559 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004560 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00004561 if (hidlSession_3_4 != nullptr) {
4562 captureRequests_3_4.resize(batchSize);
4563 } else {
4564 captureRequests.resize(batchSize);
4565 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004566 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004567 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004568
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004569 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004570 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004571 if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004572 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004573 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00004574 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004575 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
4576 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004577 }
4578 if (res != OK) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004579 popInflightBuffers(inflightBuffers);
4580 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004581 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00004582 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004583 }
4584
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004585 std::vector<device::V3_2::BufferCache> cachesToRemove;
4586 {
4587 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4588 for (auto& pair : mFreedBuffers) {
4589 // The stream might have been removed since onBufferFreed
4590 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
4591 cachesToRemove.push_back({pair.first, pair.second});
4592 }
4593 }
4594 mFreedBuffers.clear();
4595 }
4596
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004597 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
4598 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07004599
4600 // Write metadata to FMQ.
4601 for (size_t i = 0; i < batchSize; i++) {
4602 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00004603 device::V3_2::CaptureRequest* captureRequest;
4604 if (hidlSession_3_4 != nullptr) {
4605 captureRequest = &captureRequests_3_4[i].v3_2;
4606 } else {
4607 captureRequest = &captureRequests[i];
4608 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004609
4610 if (request->settings != nullptr) {
4611 size_t settingsSize = get_camera_metadata_size(request->settings);
4612 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4613 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
4614 captureRequest->settings.resize(0);
4615 captureRequest->fmqSettingsSize = settingsSize;
4616 } else {
4617 if (mRequestMetadataQueue != nullptr) {
4618 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4619 }
4620 captureRequest->settings.setToExternal(
4621 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
4622 get_camera_metadata_size(request->settings));
4623 captureRequest->fmqSettingsSize = 0u;
4624 }
4625 } else {
4626 // A null request settings maps to a size-0 CameraMetadata
4627 captureRequest->settings.resize(0);
4628 captureRequest->fmqSettingsSize = 0u;
4629 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004630
4631 if (hidlSession_3_4 != nullptr) {
4632 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
4633 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00004634 if (request->physcam_settings != nullptr) {
4635 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
4636 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4637 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
4638 settingsSize)) {
4639 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
4640 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
4641 settingsSize;
4642 } else {
4643 if (mRequestMetadataQueue != nullptr) {
4644 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4645 }
4646 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
4647 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
4648 request->physcam_settings[j])),
4649 get_camera_metadata_size(request->physcam_settings[j]));
4650 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00004651 }
Emilian Peev00420d22018-02-05 21:33:13 +00004652 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00004653 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00004654 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00004655 }
4656 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
4657 request->physcam_id[j];
4658 }
4659 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004660 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004661
4662 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004663 auto resultCallback =
4664 [&status, &numRequestProcessed] (auto s, uint32_t n) {
4665 status = s;
4666 *numRequestProcessed = n;
4667 };
Emilian Peevaebbe412018-01-15 13:53:24 +00004668 if (hidlSession_3_4 != nullptr) {
4669 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004670 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004671 } else {
4672 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004673 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004674 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004675 if (!err.isOk()) {
4676 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004677 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004678 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004679
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004680 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
4681 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
4682 __FUNCTION__, *numRequestProcessed, batchSize);
4683 status = common::V1_0::Status::INTERNAL_ERROR;
4684 }
4685
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004686 res = CameraProviderManager::mapToStatusT(status);
4687 if (res == OK) {
4688 if (mHidlSession->isRemote()) {
4689 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
4690 // sent to camera HAL processes)
4691 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
4692 } else {
4693 // In passthrough mode the FDs are now owned by HAL
4694 cleanupNativeHandles(&handlesCreated);
4695 }
4696 } else {
4697 popInflightBuffers(inflightBuffers);
4698 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004699 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004700 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004701}
4702
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004703status_t Camera3Device::HalInterface::flush() {
4704 ATRACE_NAME("CameraHal::flush");
4705 if (!valid()) return INVALID_OPERATION;
4706 status_t res = OK;
4707
Emilian Peev31abd0a2017-05-11 18:37:46 +01004708 auto err = mHidlSession->flush();
4709 if (!err.isOk()) {
4710 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4711 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004712 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004713 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004714 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004715
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004716 return res;
4717}
4718
Emilian Peev31abd0a2017-05-11 18:37:46 +01004719status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004720 ATRACE_NAME("CameraHal::dump");
4721 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004722
Emilian Peev31abd0a2017-05-11 18:37:46 +01004723 // Handled by CameraProviderManager::dump
4724
4725 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004726}
4727
4728status_t Camera3Device::HalInterface::close() {
4729 ATRACE_NAME("CameraHal::close()");
4730 if (!valid()) return INVALID_OPERATION;
4731 status_t res = OK;
4732
Emilian Peev31abd0a2017-05-11 18:37:46 +01004733 auto err = mHidlSession->close();
4734 // Interface will be dead shortly anyway, so don't log errors
4735 if (!err.isOk()) {
4736 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004737 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004738
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004739 return res;
4740}
4741
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004742void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
4743 ATRACE_NAME("CameraHal::signalPipelineDrain");
4744 if (!valid() || mHidlSession_3_5 == nullptr) {
4745 ALOGE("%s called on invalid camera!", __FUNCTION__);
4746 return;
4747 }
4748
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004749 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004750 if (!err.isOk()) {
4751 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4752 return;
4753 }
4754}
4755
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004756void Camera3Device::HalInterface::getInflightBufferKeys(
4757 std::vector<std::pair<int32_t, int32_t>>* out) {
4758 std::lock_guard<std::mutex> lock(mInflightLock);
4759 out->clear();
4760 out->reserve(mInflightBufferMap.size());
4761 for (auto& pair : mInflightBufferMap) {
4762 uint64_t key = pair.first;
4763 int32_t streamId = key & 0xFFFFFFFF;
4764 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
4765 out->push_back(std::make_pair(frameNumber, streamId));
4766 }
4767 return;
4768}
4769
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004770void Camera3Device::HalInterface::getInflightRequestBufferKeys(
4771 std::vector<uint64_t>* out) {
4772 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4773 out->clear();
4774 out->reserve(mRequestedBuffers.size());
4775 for (auto& pair : mRequestedBuffers) {
4776 out->push_back(pair.first);
4777 }
4778 return;
4779}
4780
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004781status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004782 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004783 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004784 mInflightBufferMap[key] = buffer;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004785 return OK;
4786}
4787
4788status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004789 int32_t frameNumber, int32_t streamId,
4790 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004791 std::lock_guard<std::mutex> lock(mInflightLock);
4792
4793 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4794 auto it = mInflightBufferMap.find(key);
4795 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004796 if (buffer != nullptr) {
4797 *buffer = it->second;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004798 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004799 mInflightBufferMap.erase(it);
4800 return OK;
4801}
4802
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004803void Camera3Device::HalInterface::popInflightBuffers(
4804 const std::vector<std::pair<int32_t, int32_t>>& buffers) {
4805 for (const auto& pair : buffers) {
4806 int32_t frameNumber = pair.first;
4807 int32_t streamId = pair.second;
4808 popInflightBuffer(frameNumber, streamId, nullptr);
4809 }
4810}
4811
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004812status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004813 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004814 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004815 auto pair = mRequestedBuffers.insert({bufferId, {streamId, buf}});
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004816 if (!pair.second) {
4817 ALOGE("%s: bufId %" PRIu64 " is already inflight!",
4818 __FUNCTION__, bufferId);
4819 return BAD_VALUE;
4820 }
4821 return OK;
4822}
4823
4824// Find and pop a buffer_handle_t based on bufferId
4825status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004826 uint64_t bufferId,
4827 /*out*/ buffer_handle_t** buffer,
4828 /*optional out*/ int32_t* streamId) {
4829 if (buffer == nullptr) {
4830 ALOGE("%s: buffer (%p) must not be null", __FUNCTION__, buffer);
4831 return BAD_VALUE;
4832 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004833 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4834 auto it = mRequestedBuffers.find(bufferId);
4835 if (it == mRequestedBuffers.end()) {
4836 ALOGE("%s: bufId %" PRIu64 " is not inflight!",
4837 __FUNCTION__, bufferId);
4838 return BAD_VALUE;
4839 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004840 *buffer = it->second.second;
4841 if (streamId != nullptr) {
4842 *streamId = it->second.first;
4843 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004844 mRequestedBuffers.erase(it);
4845 return OK;
4846}
4847
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004848std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4849 const buffer_handle_t& buf, int streamId) {
4850 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4851
4852 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4853 auto it = bIdMap.find(buf);
4854 if (it == bIdMap.end()) {
4855 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004856 ALOGV("stream %d now have %zu buffer caches, buf %p",
4857 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004858 return std::make_pair(true, mNextBufferId - 1);
4859 } else {
4860 return std::make_pair(false, it->second);
4861 }
4862}
4863
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004864void Camera3Device::HalInterface::onBufferFreed(
4865 int streamId, const native_handle_t* handle) {
4866 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4867 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4868 auto mapIt = mBufferIdMaps.find(streamId);
4869 if (mapIt == mBufferIdMaps.end()) {
4870 // streamId might be from a deleted stream here
4871 ALOGI("%s: stream %d has been removed",
4872 __FUNCTION__, streamId);
4873 return;
4874 }
4875 BufferIdMap& bIdMap = mapIt->second;
4876 auto it = bIdMap.find(handle);
4877 if (it == bIdMap.end()) {
4878 ALOGW("%s: cannot find buffer %p in stream %d",
4879 __FUNCTION__, handle, streamId);
4880 return;
4881 } else {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004882 bufferId = it->second;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004883 bIdMap.erase(it);
4884 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4885 __FUNCTION__, streamId, bIdMap.size(), handle);
4886 }
4887 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4888}
4889
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004890void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
4891 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4892 auto mapIt = mBufferIdMaps.find(streamId);
4893 if (mapIt == mBufferIdMaps.end()) {
4894 ALOGE("%s: streamId %d not found!", __FUNCTION__, streamId);
4895 return;
4896 }
4897
4898 BufferIdMap& bIdMap = mapIt->second;
4899 for (const auto& it : bIdMap) {
4900 uint64_t bufferId = it.second;
4901 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4902 }
4903 bIdMap.clear();
4904}
4905
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004906/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004907 * RequestThread inner class methods
4908 */
4909
4910Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004911 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004912 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
4913 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004914 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004915 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004916 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004917 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004918 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004919 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004920 mReconfigured(false),
4921 mDoPause(false),
4922 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004923 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004924 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004925 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004926 mCurrentAfTriggerId(0),
4927 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004928 mRepeatingLastFrameNumber(
4929 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004930 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004931 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004932 mRequestLatency(kRequestLatencyBinSize),
4933 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004934 mLatestSessionParams(sessionParamKeys.size()),
4935 mUseHalBufManager(useHalBufManager) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004936 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004937}
4938
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004939Camera3Device::RequestThread::~RequestThread() {}
4940
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004941void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004942 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004943 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004944 Mutex::Autolock l(mRequestLock);
4945 mListener = listener;
4946}
4947
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004948void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4949 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004950 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004951 Mutex::Autolock l(mRequestLock);
4952 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004953 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004954 // Prepare video stream for high speed recording.
4955 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004956 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004957}
4958
Jianing Wei90e59c92014-03-12 18:29:36 -07004959status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004960 List<sp<CaptureRequest> > &requests,
4961 /*out*/
4962 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004963 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004964 Mutex::Autolock l(mRequestLock);
4965 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4966 ++it) {
4967 mRequestQueue.push_back(*it);
4968 }
4969
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004970 if (lastFrameNumber != NULL) {
4971 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4972 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4973 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4974 *lastFrameNumber);
4975 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004976
Jianing Wei90e59c92014-03-12 18:29:36 -07004977 unpauseForNewRequests();
4978
4979 return OK;
4980}
4981
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004982
4983status_t Camera3Device::RequestThread::queueTrigger(
4984 RequestTrigger trigger[],
4985 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004986 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004987 Mutex::Autolock l(mTriggerMutex);
4988 status_t ret;
4989
4990 for (size_t i = 0; i < count; ++i) {
4991 ret = queueTriggerLocked(trigger[i]);
4992
4993 if (ret != OK) {
4994 return ret;
4995 }
4996 }
4997
4998 return OK;
4999}
5000
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005001const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
5002 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005003 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005004 if (d != nullptr) return d->mId;
5005 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005006}
5007
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005008status_t Camera3Device::RequestThread::queueTriggerLocked(
5009 RequestTrigger trigger) {
5010
5011 uint32_t tag = trigger.metadataTag;
5012 ssize_t index = mTriggerMap.indexOfKey(tag);
5013
5014 switch (trigger.getTagType()) {
5015 case TYPE_BYTE:
5016 // fall-through
5017 case TYPE_INT32:
5018 break;
5019 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005020 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
5021 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005022 return INVALID_OPERATION;
5023 }
5024
5025 /**
5026 * Collect only the latest trigger, since we only have 1 field
5027 * in the request settings per trigger tag, and can't send more than 1
5028 * trigger per request.
5029 */
5030 if (index != NAME_NOT_FOUND) {
5031 mTriggerMap.editValueAt(index) = trigger;
5032 } else {
5033 mTriggerMap.add(tag, trigger);
5034 }
5035
5036 return OK;
5037}
5038
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005039status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005040 const RequestList &requests,
5041 /*out*/
5042 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005043 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005044 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005045 if (lastFrameNumber != NULL) {
5046 *lastFrameNumber = mRepeatingLastFrameNumber;
5047 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005048 mRepeatingRequests.clear();
5049 mRepeatingRequests.insert(mRepeatingRequests.begin(),
5050 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005051
5052 unpauseForNewRequests();
5053
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005054 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005055 return OK;
5056}
5057
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07005058bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005059 if (mRepeatingRequests.empty()) {
5060 return false;
5061 }
5062 int32_t requestId = requestIn->mResultExtras.requestId;
5063 const RequestList &repeatRequests = mRepeatingRequests;
5064 // All repeating requests are guaranteed to have same id so only check first quest
5065 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
5066 return (firstRequest->mResultExtras.requestId == requestId);
5067}
5068
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005069status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005070 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005071 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005072 return clearRepeatingRequestsLocked(lastFrameNumber);
5073
5074}
5075
5076status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005077 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005078 if (lastFrameNumber != NULL) {
5079 *lastFrameNumber = mRepeatingLastFrameNumber;
5080 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005081 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005082 return OK;
5083}
5084
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005085status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005086 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005087 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005088 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005089 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005090
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005091 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005092
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005093 // Send errors for all requests pending in the request queue, including
5094 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005095 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005096 if (listener != NULL) {
5097 for (RequestList::iterator it = mRequestQueue.begin();
5098 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005099 // Abort the input buffers for reprocess requests.
5100 if ((*it)->mInputStream != NULL) {
5101 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07005102 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
5103 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005104 if (res != OK) {
5105 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
5106 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5107 } else {
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07005108 inputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005109 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
5110 if (res != OK) {
5111 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
5112 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5113 }
5114 }
5115 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005116 // Set the frame number this request would have had, if it
5117 // had been submitted; this frame number will not be reused.
5118 // The requestId and burstId fields were set when the request was
5119 // submitted originally (in convertMetadataListToRequestListLocked)
5120 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005121 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005122 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005123 }
5124 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005125 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08005126
5127 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005128 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005129 if (lastFrameNumber != NULL) {
5130 *lastFrameNumber = mRepeatingLastFrameNumber;
5131 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005132 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005133 return OK;
5134}
5135
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005136status_t Camera3Device::RequestThread::flush() {
5137 ATRACE_CALL();
5138 Mutex::Autolock l(mFlushLock);
5139
Emilian Peev08dd2452017-04-06 16:55:14 +01005140 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005141}
5142
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005143void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005144 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005145 Mutex::Autolock l(mPauseLock);
5146 mDoPause = paused;
5147 mDoPauseSignal.signal();
5148}
5149
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005150status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
5151 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005152 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005153 Mutex::Autolock l(mLatestRequestMutex);
5154 status_t res;
5155 while (mLatestRequestId != requestId) {
5156 nsecs_t startTime = systemTime();
5157
5158 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
5159 if (res != OK) return res;
5160
5161 timeout -= (systemTime() - startTime);
5162 }
5163
5164 return OK;
5165}
5166
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005167void Camera3Device::RequestThread::requestExit() {
5168 // Call parent to set up shutdown
5169 Thread::requestExit();
5170 // The exit from any possible waits
5171 mDoPauseSignal.signal();
5172 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07005173
5174 mRequestLatency.log("ProcessCaptureRequest latency histogram");
5175 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005176}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005177
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005178void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005179 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005180 bool surfaceAbandoned = false;
5181 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005182 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005183 {
5184 Mutex::Autolock l(mRequestLock);
5185 // Check all streams needed by repeating requests are still valid. Otherwise, stop
5186 // repeating requests.
5187 for (const auto& request : mRepeatingRequests) {
5188 for (const auto& s : request->mOutputStreams) {
5189 if (s->isAbandoned()) {
5190 surfaceAbandoned = true;
5191 clearRepeatingRequestsLocked(&lastFrameNumber);
5192 break;
5193 }
5194 }
5195 if (surfaceAbandoned) {
5196 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005197 }
5198 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005199 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005200 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005201
5202 if (listener != NULL && surfaceAbandoned) {
5203 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005204 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005205}
5206
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005207bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005208 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005209 status_t res;
5210 size_t batchSize = mNextRequests.size();
5211 std::vector<camera3_capture_request_t*> requests(batchSize);
5212 uint32_t numRequestProcessed = 0;
5213 for (size_t i = 0; i < batchSize; i++) {
5214 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07005215 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005216 }
5217
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005218 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
5219
5220 bool triggerRemoveFailed = false;
5221 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
5222 for (size_t i = 0; i < numRequestProcessed; i++) {
5223 NextRequest& nextRequest = mNextRequests.editItemAt(i);
5224 nextRequest.submitted = true;
5225
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005226 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00005227
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005228 if (!triggerRemoveFailed) {
5229 // Remove any previously queued triggers (after unlock)
5230 status_t removeTriggerRes = removeTriggers(mPrevRequest);
5231 if (removeTriggerRes != OK) {
5232 triggerRemoveFailed = true;
5233 triggerFailedRequest = nextRequest;
5234 }
5235 }
5236 }
5237
5238 if (triggerRemoveFailed) {
5239 SET_ERR("RequestThread: Unable to remove triggers "
5240 "(capture request %d, HAL device: %s (%d)",
5241 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
5242 cleanUpFailedRequests(/*sendRequestError*/ false);
5243 return false;
5244 }
5245
5246 if (res != OK) {
5247 // Should only get a failure here for malformed requests or device-level
5248 // errors, so consider all errors fatal. Bad metadata failures should
5249 // come through notify.
5250 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
5251 mNextRequests[numRequestProcessed].halRequest.frame_number,
5252 strerror(-res), res);
5253 cleanUpFailedRequests(/*sendRequestError*/ false);
5254 return false;
5255 }
5256 return true;
5257}
5258
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005259nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
5260 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
5261 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5262 find_camera_metadata_ro_entry(request,
5263 ANDROID_CONTROL_AE_MODE,
5264 &e);
5265 if (e.count == 0) return maxExpectedDuration;
5266
5267 switch (e.data.u8[0]) {
5268 case ANDROID_CONTROL_AE_MODE_OFF:
5269 find_camera_metadata_ro_entry(request,
5270 ANDROID_SENSOR_EXPOSURE_TIME,
5271 &e);
5272 if (e.count > 0) {
5273 maxExpectedDuration = e.data.i64[0];
5274 }
5275 find_camera_metadata_ro_entry(request,
5276 ANDROID_SENSOR_FRAME_DURATION,
5277 &e);
5278 if (e.count > 0) {
5279 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
5280 }
5281 break;
5282 default:
5283 find_camera_metadata_ro_entry(request,
5284 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
5285 &e);
5286 if (e.count > 1) {
5287 maxExpectedDuration = 1e9 / e.data.u8[0];
5288 }
5289 break;
5290 }
5291
5292 return maxExpectedDuration;
5293}
5294
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005295bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
5296 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
5297 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
5298 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
5299 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
5300 return true;
5301 }
5302
5303 return false;
5304}
5305
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005306void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
5307 // Update the latest request sent to HAL
5308 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
5309 Mutex::Autolock al(mLatestRequestMutex);
5310
5311 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
5312 mLatestRequest.acquire(cloned);
5313
5314 mLatestPhysicalRequest.clear();
5315 for (uint32_t i = 0; i < nextRequest.halRequest.num_physcam_settings; i++) {
5316 cloned = clone_camera_metadata(nextRequest.halRequest.physcam_settings[i]);
5317 mLatestPhysicalRequest.emplace(nextRequest.halRequest.physcam_id[i],
5318 CameraMetadata(cloned));
5319 }
5320
5321 sp<Camera3Device> parent = mParent.promote();
5322 if (parent != NULL) {
5323 parent->monitorMetadata(TagMonitor::REQUEST,
5324 nextRequest.halRequest.frame_number,
5325 0, mLatestRequest, mLatestPhysicalRequest);
5326 }
5327 }
5328
5329 if (nextRequest.halRequest.settings != NULL) {
5330 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
5331 nextRequest.halRequest.settings);
5332 }
5333
5334 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
5335}
5336
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005337bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
5338 ATRACE_CALL();
5339 bool updatesDetected = false;
5340
Emilian Peev4ec17882019-01-24 17:16:58 -08005341 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005342 for (auto tag : mSessionParamKeys) {
5343 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005344 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005345
5346 if (entry.count > 0) {
5347 bool isDifferent = false;
5348 if (lastEntry.count > 0) {
5349 // Have a last value, compare to see if changed
5350 if (lastEntry.type == entry.type &&
5351 lastEntry.count == entry.count) {
5352 // Same type and count, compare values
5353 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
5354 size_t entryBytes = bytesPerValue * lastEntry.count;
5355 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
5356 if (cmp != 0) {
5357 isDifferent = true;
5358 }
5359 } else {
5360 // Count or type has changed
5361 isDifferent = true;
5362 }
5363 } else {
5364 // No last entry, so always consider to be different
5365 isDifferent = true;
5366 }
5367
5368 if (isDifferent) {
5369 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005370 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
5371 updatesDetected = true;
5372 }
Emilian Peev4ec17882019-01-24 17:16:58 -08005373 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005374 }
5375 } else if (lastEntry.count > 0) {
5376 // Value has been removed
5377 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005378 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005379 updatesDetected = true;
5380 }
5381 }
5382
Emilian Peev4ec17882019-01-24 17:16:58 -08005383 bool reconfigureRequired;
5384 if (updatesDetected) {
5385 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
5386 updatedParams);
5387 mLatestSessionParams = updatedParams;
5388 } else {
5389 reconfigureRequired = false;
5390 }
5391
5392 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005393}
5394
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005395bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005396 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005397 status_t res;
5398
5399 // Handle paused state.
5400 if (waitIfPaused()) {
5401 return true;
5402 }
5403
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005404 // Wait for the next batch of requests.
5405 waitForNextRequestBatch();
5406 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005407 return true;
5408 }
5409
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005410 // Get the latest request ID, if any
5411 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005412 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00005413 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005414 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005415 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005416 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005417 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
5418 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005419 }
5420
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005421 // 'mNextRequests' will at this point contain either a set of HFR batched requests
5422 // or a single request from streaming or burst. In either case the first element
5423 // should contain the latest camera settings that we need to check for any session
5424 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00005425 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005426 res = OK;
5427
5428 //Input stream buffers are already acquired at this point so an input stream
5429 //will not be able to move to idle state unless we force it.
5430 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5431 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
5432 if (res != OK) {
5433 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
5434 cleanUpFailedRequests(/*sendRequestError*/ false);
5435 return false;
5436 }
5437 }
5438
5439 if (res == OK) {
5440 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5441 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08005442 sp<Camera3Device> parent = mParent.promote();
5443 if (parent != nullptr) {
5444 parent->pauseStateNotify(true);
5445 }
5446
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005447 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5448
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005449 if (parent != nullptr) {
5450 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
5451 }
5452
5453 statusTracker->markComponentActive(mStatusId);
5454 setPaused(false);
5455 }
5456
5457 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5458 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
5459 if (res != OK) {
5460 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
5461 cleanUpFailedRequests(/*sendRequestError*/ false);
5462 return false;
5463 }
5464 }
5465 }
5466 }
5467
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005468 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005469 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005470 if (res == TIMED_OUT) {
5471 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005472 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005473 // Check if any stream is abandoned.
5474 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005475 return true;
5476 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005477 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005478 return false;
5479 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005480
Zhijun Hecc27e112013-10-03 16:12:43 -07005481 // Inform waitUntilRequestProcessed thread of a new request ID
5482 {
5483 Mutex::Autolock al(mLatestRequestMutex);
5484
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005485 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07005486 mLatestRequestSignal.signal();
5487 }
5488
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005489 // Submit a batch of requests to HAL.
5490 // Use flush lock only when submitting multilple requests in a batch.
5491 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
5492 // which may take a long time to finish so synchronizing flush() and
5493 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
5494 // For now, only synchronize for high speed recording and we should figure something out for
5495 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005496 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07005497
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005498 if (useFlushLock) {
5499 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005500 }
5501
Zhijun Hef0645c12016-08-02 00:58:11 -07005502 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005503 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07005504
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005505 sp<Camera3Device> parent = mParent.promote();
5506 if (parent != nullptr) {
5507 parent->mRequestBufferSM.onSubmittingRequest();
5508 }
5509
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005510 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07005511 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07005512 submitRequestSuccess = sendRequestsBatch();
5513
Shuzhen Wang686f6442017-06-20 16:16:04 -07005514 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
5515 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07005516
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005517 if (useFlushLock) {
5518 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005519 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005520
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005521 // Unset as current request
5522 {
5523 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005524 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005525 }
5526
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005527 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005528}
5529
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005530status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005531 ATRACE_CALL();
5532
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005533 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005534 for (size_t i = 0; i < mNextRequests.size(); i++) {
5535 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005536 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5537 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5538 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5539
5540 // Prepare a request to HAL
5541 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
5542
5543 // Insert any queued triggers (before metadata is locked)
5544 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005545 if (res < 0) {
5546 SET_ERR("RequestThread: Unable to insert triggers "
5547 "(capture request %d, HAL device: %s (%d)",
5548 halRequest->frame_number, strerror(-res), res);
5549 return INVALID_OPERATION;
5550 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005551
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005552 int triggerCount = res;
5553 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
5554 mPrevTriggers = triggerCount;
5555
5556 // If the request is the same as last, or we had triggers last time
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005557 bool newRequest = (mPrevRequest != captureRequest || triggersMixedIn) &&
5558 // Request settings are all the same within one batch, so only treat the first
5559 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07005560 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00005561 if (newRequest) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005562 /**
5563 * HAL workaround:
5564 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
5565 */
5566 res = addDummyTriggerIds(captureRequest);
5567 if (res != OK) {
5568 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
5569 "(capture request %d, HAL device: %s (%d)",
5570 halRequest->frame_number, strerror(-res), res);
5571 return INVALID_OPERATION;
5572 }
5573
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005574 {
5575 // Correct metadata regions for distortion correction if enabled
5576 sp<Camera3Device> parent = mParent.promote();
5577 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07005578 List<PhysicalCameraSettings>::iterator it;
5579 for (it = captureRequest->mSettingsList.begin();
5580 it != captureRequest->mSettingsList.end(); it++) {
5581 if (parent->mDistortionMappers.find(it->cameraId) ==
5582 parent->mDistortionMappers.end()) {
5583 continue;
5584 }
5585 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
5586 &(it->metadata));
5587 if (res != OK) {
5588 SET_ERR("RequestThread: Unable to correct capture requests "
5589 "for lens distortion for request %d: %s (%d)",
5590 halRequest->frame_number, strerror(-res), res);
5591 return INVALID_OPERATION;
5592 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005593 }
5594 }
5595 }
5596
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005597 /**
5598 * The request should be presorted so accesses in HAL
5599 * are O(logn). Sidenote, sorting a sorted metadata is nop.
5600 */
Emilian Peevaebbe412018-01-15 13:53:24 +00005601 captureRequest->mSettingsList.begin()->metadata.sort();
5602 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005603 mPrevRequest = captureRequest;
5604 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
5605
5606 IF_ALOGV() {
5607 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5608 find_camera_metadata_ro_entry(
5609 halRequest->settings,
5610 ANDROID_CONTROL_AF_TRIGGER,
5611 &e
5612 );
5613 if (e.count > 0) {
5614 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
5615 __FUNCTION__,
5616 halRequest->frame_number,
5617 e.data.u8[0]);
5618 }
5619 }
5620 } else {
5621 // leave request.settings NULL to indicate 'reuse latest given'
5622 ALOGVV("%s: Request settings are REUSED",
5623 __FUNCTION__);
5624 }
5625
Emilian Peevaebbe412018-01-15 13:53:24 +00005626 if (captureRequest->mSettingsList.size() > 1) {
5627 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
5628 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00005629 if (newRequest) {
5630 halRequest->physcam_settings =
5631 new const camera_metadata* [halRequest->num_physcam_settings];
5632 } else {
5633 halRequest->physcam_settings = nullptr;
5634 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005635 auto it = ++captureRequest->mSettingsList.begin();
5636 size_t i = 0;
5637 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
5638 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00005639 if (newRequest) {
5640 it->metadata.sort();
5641 halRequest->physcam_settings[i] = it->metadata.getAndLock();
5642 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005643 }
5644 }
5645
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005646 uint32_t totalNumBuffers = 0;
5647
5648 // Fill in buffers
5649 if (captureRequest->mInputStream != NULL) {
5650 halRequest->input_buffer = &captureRequest->mInputBuffer;
5651 totalNumBuffers += 1;
5652 } else {
5653 halRequest->input_buffer = NULL;
5654 }
5655
5656 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
5657 captureRequest->mOutputStreams.size());
5658 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005659 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07005660
5661 sp<Camera3Device> parent = mParent.promote();
5662 if (parent == NULL) {
5663 // Should not happen, and nowhere to send errors to, so just log it
5664 CLOGE("RequestThread: Parent is gone");
5665 return INVALID_OPERATION;
5666 }
5667 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
5668
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005669 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005670 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005671 sp<Camera3OutputStreamInterface> outputStream =
5672 captureRequest->mOutputStreams.editItemAt(j);
5673 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005674
5675 // Prepare video buffers for high speed recording on the first video request.
5676 if (mPrepareVideoStream && outputStream->isVideoStream()) {
5677 // Only try to prepare video stream on the first video request.
5678 mPrepareVideoStream = false;
5679
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005680 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
5681 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005682 while (res == NOT_ENOUGH_DATA) {
5683 res = outputStream->prepareNextBuffer();
5684 }
5685 if (res != OK) {
5686 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
5687 __FUNCTION__, strerror(-res), res);
5688 outputStream->cancelPrepare();
5689 }
5690 }
5691
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005692 std::vector<size_t> uniqueSurfaceIds;
5693 res = outputStream->getUniqueSurfaceIds(
5694 captureRequest->mOutputSurfaces[streamId],
5695 &uniqueSurfaceIds);
5696 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
5697 if (res != OK && res != INVALID_OPERATION) {
5698 ALOGE("%s: failed to query stream %d unique surface IDs",
5699 __FUNCTION__, streamId);
5700 return res;
5701 }
5702 if (res == OK) {
5703 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
5704 }
5705
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005706 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005707 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005708 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005709 return TIMED_OUT;
5710 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005711 // HAL will request buffer through requestStreamBuffer API
5712 camera3_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
5713 buffer.stream = outputStream->asHalStream();
5714 buffer.buffer = nullptr;
5715 buffer.status = CAMERA3_BUFFER_STATUS_OK;
5716 buffer.acquire_fence = -1;
5717 buffer.release_fence = -1;
5718 } else {
5719 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
5720 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005721 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005722 if (res != OK) {
5723 // Can't get output buffer from gralloc queue - this could be due to
5724 // abandoned queue or other consumer misbehavior, so not a fatal
5725 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005726 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005727 " %s (%d)", strerror(-res), res);
5728
5729 return TIMED_OUT;
5730 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005731 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08005732
5733 {
5734 sp<Camera3Device> parent = mParent.promote();
5735 if (parent != nullptr) {
5736 const String8& streamCameraId = outputStream->getPhysicalCameraId();
5737 for (const auto& settings : captureRequest->mSettingsList) {
5738 if ((streamCameraId.isEmpty() &&
5739 parent->getId() == settings.cameraId.c_str()) ||
5740 streamCameraId == settings.cameraId.c_str()) {
5741 outputStream->fireBufferRequestForFrameNumber(
5742 captureRequest->mResultExtras.frameNumber,
5743 settings.metadata);
5744 }
5745 }
5746 }
5747 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07005748
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005749 String8 physicalCameraId = outputStream->getPhysicalCameraId();
5750
5751 if (!physicalCameraId.isEmpty()) {
5752 // Physical stream isn't supported for input request.
5753 if (halRequest->input_buffer) {
5754 CLOGE("Physical stream is not supported for input request");
5755 return INVALID_OPERATION;
5756 }
5757 requestedPhysicalCameras.insert(physicalCameraId);
5758 }
5759 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005760 }
5761 totalNumBuffers += halRequest->num_output_buffers;
5762
5763 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08005764 // If this request list is for constrained high speed recording (not
5765 // preview), and the current request is not the last one in the batch,
5766 // do not send callback to the app.
5767 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005768 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08005769 hasCallback = false;
5770 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005771 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005772 bool isZslCapture = false;
Emilian Peev9dd21f42018-08-03 13:39:29 +01005773 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
5774 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5775 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
5776 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
5777 isStillCapture = true;
5778 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
5779 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005780
5781 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
5782 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
5783 isZslCapture = true;
5784 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005785 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005786 res = parent->registerInFlight(halRequest->frame_number,
5787 totalNumBuffers, captureRequest->mResultExtras,
5788 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005789 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005790 calculateMaxExpectedDuration(halRequest->settings),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005791 requestedPhysicalCameras, isStillCapture, isZslCapture,
5792 (mUseHalBufManager) ? uniqueSurfaceIdMap :
5793 SurfaceMap{});
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005794 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
5795 ", burstId = %" PRId32 ".",
5796 __FUNCTION__,
5797 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
5798 captureRequest->mResultExtras.burstId);
5799 if (res != OK) {
5800 SET_ERR("RequestThread: Unable to register new in-flight request:"
5801 " %s (%d)", strerror(-res), res);
5802 return INVALID_OPERATION;
5803 }
5804 }
5805
5806 return OK;
5807}
5808
Igor Murashkin1e479c02013-09-06 16:55:14 -07005809CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005810 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005811 Mutex::Autolock al(mLatestRequestMutex);
5812
5813 ALOGV("RequestThread::%s", __FUNCTION__);
5814
5815 return mLatestRequest;
5816}
5817
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005818bool Camera3Device::RequestThread::isStreamPending(
5819 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005820 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005821 Mutex::Autolock l(mRequestLock);
5822
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005823 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005824 if (!nextRequest.submitted) {
5825 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5826 if (stream == s) return true;
5827 }
5828 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005829 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005830 }
5831
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005832 for (const auto& request : mRequestQueue) {
5833 for (const auto& s : request->mOutputStreams) {
5834 if (stream == s) return true;
5835 }
5836 if (stream == request->mInputStream) return true;
5837 }
5838
5839 for (const auto& request : mRepeatingRequests) {
5840 for (const auto& s : request->mOutputStreams) {
5841 if (stream == s) return true;
5842 }
5843 if (stream == request->mInputStream) return true;
5844 }
5845
5846 return false;
5847}
Jianing Weicb0652e2014-03-12 18:29:36 -07005848
Emilian Peev40ead602017-09-26 15:46:36 +01005849bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5850 ATRACE_CALL();
5851 Mutex::Autolock l(mRequestLock);
5852
5853 for (const auto& nextRequest : mNextRequests) {
5854 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5855 if (s.first == streamId) {
5856 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5857 if (it != s.second.end()) {
5858 return true;
5859 }
5860 }
5861 }
5862 }
5863
5864 for (const auto& request : mRequestQueue) {
5865 for (const auto& s : request->mOutputSurfaces) {
5866 if (s.first == streamId) {
5867 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5868 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005869 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005870 }
5871 }
5872 }
5873 }
5874
5875 for (const auto& request : mRepeatingRequests) {
5876 for (const auto& s : request->mOutputSurfaces) {
5877 if (s.first == streamId) {
5878 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5879 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005880 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005881 }
5882 }
5883 }
5884 }
5885
5886 return false;
5887}
5888
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005889void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
5890 if (!mUseHalBufManager) {
5891 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
5892 return;
5893 }
5894
5895 Mutex::Autolock pl(mPauseLock);
5896 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005897 mInterface->signalPipelineDrain(streamIds);
5898 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005899 }
5900 // If request thread is still busy, wait until paused then notify HAL
5901 mNotifyPipelineDrain = true;
5902 mStreamIdsToBeDrained = streamIds;
5903}
5904
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005905nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005906 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005907 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005908 return mExpectedInflightDuration > kMinInflightDuration ?
5909 mExpectedInflightDuration : kMinInflightDuration;
5910}
5911
Emilian Peevaebbe412018-01-15 13:53:24 +00005912void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
5913 camera3_capture_request_t *halRequest) {
5914 if ((request == nullptr) || (halRequest == nullptr)) {
5915 ALOGE("%s: Invalid request!", __FUNCTION__);
5916 return;
5917 }
5918
5919 if (halRequest->num_physcam_settings > 0) {
5920 if (halRequest->physcam_id != nullptr) {
5921 delete [] halRequest->physcam_id;
5922 halRequest->physcam_id = nullptr;
5923 }
5924 if (halRequest->physcam_settings != nullptr) {
5925 auto it = ++(request->mSettingsList.begin());
5926 size_t i = 0;
5927 for (; it != request->mSettingsList.end(); it++, i++) {
5928 it->metadata.unlock(halRequest->physcam_settings[i]);
5929 }
5930 delete [] halRequest->physcam_settings;
5931 halRequest->physcam_settings = nullptr;
5932 }
5933 }
5934}
5935
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005936void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5937 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005938 return;
5939 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005940
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005941 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005942 // Skip the ones that have been submitted successfully.
5943 if (nextRequest.submitted) {
5944 continue;
5945 }
5946
5947 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5948 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5949 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5950
5951 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005952 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005953 }
5954
Emilian Peevaebbe412018-01-15 13:53:24 +00005955 cleanupPhysicalSettings(captureRequest, halRequest);
5956
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005957 if (captureRequest->mInputStream != NULL) {
5958 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5959 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5960 }
5961
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005962 // No output buffer can be returned when using HAL buffer manager
5963 if (!mUseHalBufManager) {
5964 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5965 //Buffers that failed processing could still have
5966 //valid acquire fence.
5967 int acquireFence = (*outputBuffers)[i].acquire_fence;
5968 if (0 <= acquireFence) {
5969 close(acquireFence);
5970 outputBuffers->editItemAt(i).acquire_fence = -1;
5971 }
5972 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5973 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
5974 /*timestampIncreasing*/true, std::vector<size_t> (),
5975 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005976 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005977 }
5978
5979 if (sendRequestError) {
5980 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005981 sp<NotificationListener> listener = mListener.promote();
5982 if (listener != NULL) {
5983 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005984 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005985 captureRequest->mResultExtras);
5986 }
5987 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005988
5989 // Remove yet-to-be submitted inflight request from inflightMap
5990 {
5991 sp<Camera3Device> parent = mParent.promote();
5992 if (parent != NULL) {
5993 Mutex::Autolock l(parent->mInFlightLock);
5994 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5995 if (idx >= 0) {
5996 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5997 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5998 parent->removeInFlightMapEntryLocked(idx);
5999 }
6000 }
6001 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006002 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07006003
6004 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006005 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006006}
6007
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006008void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006009 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006010 // Optimized a bit for the simple steady-state case (single repeating
6011 // request), to avoid putting that request in the queue temporarily.
6012 Mutex::Autolock l(mRequestLock);
6013
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006014 assert(mNextRequests.empty());
6015
6016 NextRequest nextRequest;
6017 nextRequest.captureRequest = waitForNextRequestLocked();
6018 if (nextRequest.captureRequest == nullptr) {
6019 return;
6020 }
6021
6022 nextRequest.halRequest = camera3_capture_request_t();
6023 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006024 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006025
6026 // Wait for additional requests
6027 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
6028
6029 for (size_t i = 1; i < batchSize; i++) {
6030 NextRequest additionalRequest;
6031 additionalRequest.captureRequest = waitForNextRequestLocked();
6032 if (additionalRequest.captureRequest == nullptr) {
6033 break;
6034 }
6035
6036 additionalRequest.halRequest = camera3_capture_request_t();
6037 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006038 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006039 }
6040
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006041 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006042 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006043 mNextRequests.size(), batchSize);
6044 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006045 }
6046
6047 return;
6048}
6049
6050sp<Camera3Device::CaptureRequest>
6051 Camera3Device::RequestThread::waitForNextRequestLocked() {
6052 status_t res;
6053 sp<CaptureRequest> nextRequest;
6054
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006055 while (mRequestQueue.empty()) {
6056 if (!mRepeatingRequests.empty()) {
6057 // Always atomically enqueue all requests in a repeating request
6058 // list. Guarantees a complete in-sequence set of captures to
6059 // application.
6060 const RequestList &requests = mRepeatingRequests;
6061 RequestList::const_iterator firstRequest =
6062 requests.begin();
6063 nextRequest = *firstRequest;
6064 mRequestQueue.insert(mRequestQueue.end(),
6065 ++firstRequest,
6066 requests.end());
6067 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07006068
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006069 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07006070
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006071 break;
6072 }
6073
6074 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
6075
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006076 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
6077 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006078 Mutex::Autolock pl(mPauseLock);
6079 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006080 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006081 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006082 if (mNotifyPipelineDrain) {
6083 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6084 mNotifyPipelineDrain = false;
6085 mStreamIdsToBeDrained.clear();
6086 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006087 // Let the tracker know
6088 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6089 if (statusTracker != 0) {
6090 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6091 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006092 sp<Camera3Device> parent = mParent.promote();
6093 if (parent != nullptr) {
6094 parent->mRequestBufferSM.onRequestThreadPaused();
6095 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006096 }
6097 // Stop waiting for now and let thread management happen
6098 return NULL;
6099 }
6100 }
6101
6102 if (nextRequest == NULL) {
6103 // Don't have a repeating request already in hand, so queue
6104 // must have an entry now.
6105 RequestList::iterator firstRequest =
6106 mRequestQueue.begin();
6107 nextRequest = *firstRequest;
6108 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07006109 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
6110 sp<NotificationListener> listener = mListener.promote();
6111 if (listener != NULL) {
6112 listener->notifyRequestQueueEmpty();
6113 }
6114 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006115 }
6116
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006117 // In case we've been unpaused by setPaused clearing mDoPause, need to
6118 // update internal pause state (capture/setRepeatingRequest unpause
6119 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006120 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006121 if (mPaused) {
6122 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
6123 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6124 if (statusTracker != 0) {
6125 statusTracker->markComponentActive(mStatusId);
6126 }
6127 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006128 mPaused = false;
6129
6130 // Check if we've reconfigured since last time, and reset the preview
6131 // request if so. Can't use 'NULL request == repeat' across configure calls.
6132 if (mReconfigured) {
6133 mPrevRequest.clear();
6134 mReconfigured = false;
6135 }
6136
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006137 if (nextRequest != NULL) {
6138 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006139 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
6140 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006141
6142 // Since RequestThread::clear() removes buffers from the input stream,
6143 // get the right buffer here before unlocking mRequestLock
6144 if (nextRequest->mInputStream != NULL) {
6145 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
6146 if (res != OK) {
6147 // Can't get input buffer from gralloc queue - this could be due to
6148 // disconnected queue or other producer misbehavior, so not a fatal
6149 // error
6150 ALOGE("%s: Can't get input buffer, skipping request:"
6151 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006152
6153 sp<NotificationListener> listener = mListener.promote();
6154 if (listener != NULL) {
6155 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08006156 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006157 nextRequest->mResultExtras);
6158 }
6159 return NULL;
6160 }
6161 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006162 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07006163
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006164 return nextRequest;
6165}
6166
6167bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006168 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006169 status_t res;
6170 Mutex::Autolock l(mPauseLock);
6171 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006172 if (mPaused == false) {
6173 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006174 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006175 if (mNotifyPipelineDrain) {
6176 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6177 mNotifyPipelineDrain = false;
6178 mStreamIdsToBeDrained.clear();
6179 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006180 // Let the tracker know
6181 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6182 if (statusTracker != 0) {
6183 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6184 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006185 sp<Camera3Device> parent = mParent.promote();
6186 if (parent != nullptr) {
6187 parent->mRequestBufferSM.onRequestThreadPaused();
6188 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006189 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006190
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006191 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006192 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006193 return true;
6194 }
6195 }
6196 // We don't set mPaused to false here, because waitForNextRequest needs
6197 // to further manage the paused state in case of starvation.
6198 return false;
6199}
6200
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006201void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006202 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006203 // With work to do, mark thread as unpaused.
6204 // If paused by request (setPaused), don't resume, to avoid
6205 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006206 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006207 Mutex::Autolock p(mPauseLock);
6208 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006209 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
6210 if (mPaused) {
6211 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6212 if (statusTracker != 0) {
6213 statusTracker->markComponentActive(mStatusId);
6214 }
6215 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006216 mPaused = false;
6217 }
6218}
6219
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07006220void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
6221 sp<Camera3Device> parent = mParent.promote();
6222 if (parent != NULL) {
6223 va_list args;
6224 va_start(args, fmt);
6225
6226 parent->setErrorStateV(fmt, args);
6227
6228 va_end(args);
6229 }
6230}
6231
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006232status_t Camera3Device::RequestThread::insertTriggers(
6233 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006234 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006235 Mutex::Autolock al(mTriggerMutex);
6236
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006237 sp<Camera3Device> parent = mParent.promote();
6238 if (parent == NULL) {
6239 CLOGE("RequestThread: Parent is gone");
6240 return DEAD_OBJECT;
6241 }
6242
Emilian Peevaebbe412018-01-15 13:53:24 +00006243 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006244 size_t count = mTriggerMap.size();
6245
6246 for (size_t i = 0; i < count; ++i) {
6247 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006248 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006249
6250 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
6251 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
6252 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006253 if (isAeTrigger) {
6254 request->mResultExtras.precaptureTriggerId = triggerId;
6255 mCurrentPreCaptureTriggerId = triggerId;
6256 } else {
6257 request->mResultExtras.afTriggerId = triggerId;
6258 mCurrentAfTriggerId = triggerId;
6259 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01006260 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006261 }
6262
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006263 camera_metadata_entry entry = metadata.find(tag);
6264
6265 if (entry.count > 0) {
6266 /**
6267 * Already has an entry for this trigger in the request.
6268 * Rewrite it with our requested trigger value.
6269 */
6270 RequestTrigger oldTrigger = trigger;
6271
6272 oldTrigger.entryValue = entry.data.u8[0];
6273
6274 mTriggerReplacedMap.add(tag, oldTrigger);
6275 } else {
6276 /**
6277 * More typical, no trigger entry, so we just add it
6278 */
6279 mTriggerRemovedMap.add(tag, trigger);
6280 }
6281
6282 status_t res;
6283
6284 switch (trigger.getTagType()) {
6285 case TYPE_BYTE: {
6286 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6287 res = metadata.update(tag,
6288 &entryValue,
6289 /*count*/1);
6290 break;
6291 }
6292 case TYPE_INT32:
6293 res = metadata.update(tag,
6294 &trigger.entryValue,
6295 /*count*/1);
6296 break;
6297 default:
6298 ALOGE("%s: Type not supported: 0x%x",
6299 __FUNCTION__,
6300 trigger.getTagType());
6301 return INVALID_OPERATION;
6302 }
6303
6304 if (res != OK) {
6305 ALOGE("%s: Failed to update request metadata with trigger tag %s"
6306 ", value %d", __FUNCTION__, trigger.getTagName(),
6307 trigger.entryValue);
6308 return res;
6309 }
6310
6311 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
6312 trigger.getTagName(),
6313 trigger.entryValue);
6314 }
6315
6316 mTriggerMap.clear();
6317
6318 return count;
6319}
6320
6321status_t Camera3Device::RequestThread::removeTriggers(
6322 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006323 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006324 Mutex::Autolock al(mTriggerMutex);
6325
Emilian Peevaebbe412018-01-15 13:53:24 +00006326 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006327
6328 /**
6329 * Replace all old entries with their old values.
6330 */
6331 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
6332 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
6333
6334 status_t res;
6335
6336 uint32_t tag = trigger.metadataTag;
6337 switch (trigger.getTagType()) {
6338 case TYPE_BYTE: {
6339 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6340 res = metadata.update(tag,
6341 &entryValue,
6342 /*count*/1);
6343 break;
6344 }
6345 case TYPE_INT32:
6346 res = metadata.update(tag,
6347 &trigger.entryValue,
6348 /*count*/1);
6349 break;
6350 default:
6351 ALOGE("%s: Type not supported: 0x%x",
6352 __FUNCTION__,
6353 trigger.getTagType());
6354 return INVALID_OPERATION;
6355 }
6356
6357 if (res != OK) {
6358 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
6359 ", trigger value %d", __FUNCTION__,
6360 trigger.getTagName(), trigger.entryValue);
6361 return res;
6362 }
6363 }
6364 mTriggerReplacedMap.clear();
6365
6366 /**
6367 * Remove all new entries.
6368 */
6369 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
6370 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
6371 status_t res = metadata.erase(trigger.metadataTag);
6372
6373 if (res != OK) {
6374 ALOGE("%s: Failed to erase metadata with trigger tag %s"
6375 ", trigger value %d", __FUNCTION__,
6376 trigger.getTagName(), trigger.entryValue);
6377 return res;
6378 }
6379 }
6380 mTriggerRemovedMap.clear();
6381
6382 return OK;
6383}
6384
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006385status_t Camera3Device::RequestThread::addDummyTriggerIds(
6386 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006387 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006388 static const int32_t dummyTriggerId = 1;
6389 status_t res;
6390
Emilian Peevaebbe412018-01-15 13:53:24 +00006391 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006392
6393 // If AF trigger is active, insert a dummy AF trigger ID if none already
6394 // exists
6395 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
6396 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
6397 if (afTrigger.count > 0 &&
6398 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
6399 afId.count == 0) {
6400 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
6401 if (res != OK) return res;
6402 }
6403
6404 // If AE precapture trigger is active, insert a dummy precapture trigger ID
6405 // if none already exists
6406 camera_metadata_entry pcTrigger =
6407 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
6408 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
6409 if (pcTrigger.count > 0 &&
6410 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
6411 pcId.count == 0) {
6412 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
6413 &dummyTriggerId, 1);
6414 if (res != OK) return res;
6415 }
6416
6417 return OK;
6418}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006419
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006420/**
6421 * PreparerThread inner class methods
6422 */
6423
6424Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07006425 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006426 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006427}
6428
6429Camera3Device::PreparerThread::~PreparerThread() {
6430 Thread::requestExitAndWait();
6431 if (mCurrentStream != nullptr) {
6432 mCurrentStream->cancelPrepare();
6433 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6434 mCurrentStream.clear();
6435 }
6436 clear();
6437}
6438
Ruben Brunkc78ac262015-08-13 17:58:46 -07006439status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006440 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006441 status_t res;
6442
6443 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006444 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006445
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006446 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006447 if (res == OK) {
6448 // No preparation needed, fire listener right off
6449 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006450 if (listener != NULL) {
6451 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006452 }
6453 return OK;
6454 } else if (res != NOT_ENOUGH_DATA) {
6455 return res;
6456 }
6457
6458 // Need to prepare, start up thread if necessary
6459 if (!mActive) {
6460 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
6461 // isn't running
6462 Thread::requestExitAndWait();
6463 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6464 if (res != OK) {
6465 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006466 if (listener != NULL) {
6467 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006468 }
6469 return res;
6470 }
6471 mCancelNow = false;
6472 mActive = true;
6473 ALOGV("%s: Preparer stream started", __FUNCTION__);
6474 }
6475
6476 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006477 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006478 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
6479
6480 return OK;
6481}
6482
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006483void Camera3Device::PreparerThread::pause() {
6484 ATRACE_CALL();
6485
6486 Mutex::Autolock l(mLock);
6487
6488 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
6489 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
6490 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
6491 int currentMaxCount = mCurrentMaxCount;
6492 mPendingStreams.clear();
6493 mCancelNow = true;
6494 while (mActive) {
6495 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
6496 if (res == TIMED_OUT) {
6497 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
6498 return;
6499 } else if (res != OK) {
6500 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
6501 return;
6502 }
6503 }
6504
6505 //Check whether the prepare thread was able to complete the current
6506 //stream. In case work is still pending emplace it along with the rest
6507 //of the streams in the pending list.
6508 if (currentStream != nullptr) {
6509 if (!mCurrentPrepareComplete) {
6510 pendingStreams.emplace(currentMaxCount, currentStream);
6511 }
6512 }
6513
6514 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
6515 for (const auto& it : mPendingStreams) {
6516 it.second->cancelPrepare();
6517 }
6518}
6519
6520status_t Camera3Device::PreparerThread::resume() {
6521 ATRACE_CALL();
6522 status_t res;
6523
6524 Mutex::Autolock l(mLock);
6525 sp<NotificationListener> listener = mListener.promote();
6526
6527 if (mActive) {
6528 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
6529 return NO_INIT;
6530 }
6531
6532 auto it = mPendingStreams.begin();
6533 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006534 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006535 if (res == OK) {
6536 if (listener != NULL) {
6537 listener->notifyPrepared(it->second->getId());
6538 }
6539 it = mPendingStreams.erase(it);
6540 } else if (res != NOT_ENOUGH_DATA) {
6541 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
6542 res, strerror(-res));
6543 it = mPendingStreams.erase(it);
6544 } else {
6545 it++;
6546 }
6547 }
6548
6549 if (mPendingStreams.empty()) {
6550 return OK;
6551 }
6552
6553 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6554 if (res != OK) {
6555 ALOGE("%s: Unable to start preparer stream: %d (%s)",
6556 __FUNCTION__, res, strerror(-res));
6557 return res;
6558 }
6559 mCancelNow = false;
6560 mActive = true;
6561 ALOGV("%s: Preparer stream started", __FUNCTION__);
6562
6563 return OK;
6564}
6565
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006566status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006567 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006568 Mutex::Autolock l(mLock);
6569
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006570 for (const auto& it : mPendingStreams) {
6571 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006572 }
6573 mPendingStreams.clear();
6574 mCancelNow = true;
6575
6576 return OK;
6577}
6578
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006579void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006580 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006581 Mutex::Autolock l(mLock);
6582 mListener = listener;
6583}
6584
6585bool Camera3Device::PreparerThread::threadLoop() {
6586 status_t res;
6587 {
6588 Mutex::Autolock l(mLock);
6589 if (mCurrentStream == nullptr) {
6590 // End thread if done with work
6591 if (mPendingStreams.empty()) {
6592 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
6593 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
6594 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
6595 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006596 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006597 return false;
6598 }
6599
6600 // Get next stream to prepare
6601 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006602 mCurrentStream = it->second;
6603 mCurrentMaxCount = it->first;
6604 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006605 mPendingStreams.erase(it);
6606 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
6607 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
6608 } else if (mCancelNow) {
6609 mCurrentStream->cancelPrepare();
6610 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6611 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
6612 mCurrentStream.clear();
6613 mCancelNow = false;
6614 return true;
6615 }
6616 }
6617
6618 res = mCurrentStream->prepareNextBuffer();
6619 if (res == NOT_ENOUGH_DATA) return true;
6620 if (res != OK) {
6621 // Something bad happened; try to recover by cancelling prepare and
6622 // signalling listener anyway
6623 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
6624 mCurrentStream->getId(), res, strerror(-res));
6625 mCurrentStream->cancelPrepare();
6626 }
6627
6628 // This stream has finished, notify listener
6629 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006630 sp<NotificationListener> listener = mListener.promote();
6631 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006632 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
6633 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006634 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006635 }
6636
6637 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6638 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006639 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006640
6641 return true;
6642}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006643
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006644status_t Camera3Device::RequestBufferStateMachine::initialize(
6645 sp<camera3::StatusTracker> statusTracker) {
6646 if (statusTracker == nullptr) {
6647 ALOGE("%s: statusTracker is null", __FUNCTION__);
6648 return BAD_VALUE;
6649 }
6650
6651 std::lock_guard<std::mutex> lock(mLock);
6652 mStatusTracker = statusTracker;
6653 mRequestBufferStatusId = statusTracker->addComponent();
6654 return OK;
6655}
6656
6657bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
6658 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006659 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006660 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006661 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006662 return true;
6663 }
6664 return false;
6665}
6666
6667void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
6668 std::lock_guard<std::mutex> lock(mLock);
6669 if (!mRequestBufferOngoing) {
6670 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
6671 return;
6672 }
6673 mRequestBufferOngoing = false;
6674 if (mStatus == RB_STATUS_PENDING_STOP) {
6675 checkSwitchToStopLocked();
6676 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006677 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006678}
6679
6680void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
6681 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006682 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006683 return;
6684}
6685
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006686void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006687 std::lock_guard<std::mutex> lock(mLock);
6688 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006689 // inflight map register actually happens in prepareHalRequest now, but it is close enough
6690 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006691 mInflightMapEmpty = false;
6692 if (mStatus == RB_STATUS_STOPPED) {
6693 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006694 }
6695 return;
6696}
6697
6698void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
6699 std::lock_guard<std::mutex> lock(mLock);
6700 mRequestThreadPaused = true;
6701 if (mStatus == RB_STATUS_PENDING_STOP) {
6702 checkSwitchToStopLocked();
6703 }
6704 return;
6705}
6706
6707void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
6708 std::lock_guard<std::mutex> lock(mLock);
6709 mInflightMapEmpty = true;
6710 if (mStatus == RB_STATUS_PENDING_STOP) {
6711 checkSwitchToStopLocked();
6712 }
6713 return;
6714}
6715
6716void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6717 std::lock_guard<std::mutex> lock(mLock);
6718 if (!checkSwitchToStopLocked()) {
6719 mStatus = RB_STATUS_PENDING_STOP;
6720 }
6721 return;
6722}
6723
6724void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6725 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6726 if (statusTracker != nullptr) {
6727 if (active) {
6728 statusTracker->markComponentActive(mRequestBufferStatusId);
6729 } else {
6730 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6731 }
6732 }
6733}
6734
6735bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6736 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6737 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006738 return true;
6739 }
6740 return false;
6741}
6742
Shuzhen Wang268a1362018-10-16 16:32:59 -07006743status_t Camera3Device::fixupMonochromeTags(const CameraMetadata& deviceInfo,
6744 CameraMetadata& resultMetadata) {
6745 status_t res = OK;
6746 if (!mNeedFixupMonochromeTags) {
6747 return res;
6748 }
6749
6750 // Remove tags that are not applicable to monochrome camera.
6751 int32_t tagsToRemove[] = {
6752 ANDROID_SENSOR_GREEN_SPLIT,
6753 ANDROID_SENSOR_NEUTRAL_COLOR_POINT,
6754 ANDROID_COLOR_CORRECTION_MODE,
6755 ANDROID_COLOR_CORRECTION_TRANSFORM,
6756 ANDROID_COLOR_CORRECTION_GAINS,
6757 };
6758 for (auto tag : tagsToRemove) {
6759 res = resultMetadata.erase(tag);
6760 if (res != OK) {
6761 ALOGE("%s: Failed to remove tag %d for monochrome camera", __FUNCTION__, tag);
6762 return res;
6763 }
6764 }
6765
6766 // ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL
6767 camera_metadata_entry blEntry = resultMetadata.find(ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL);
6768 for (size_t i = 1; i < blEntry.count; i++) {
6769 blEntry.data.f[i] = blEntry.data.f[0];
6770 }
6771
6772 // ANDROID_SENSOR_NOISE_PROFILE
6773 camera_metadata_entry npEntry = resultMetadata.find(ANDROID_SENSOR_NOISE_PROFILE);
6774 if (npEntry.count > 0 && npEntry.count % 2 == 0) {
6775 double np[] = {npEntry.data.d[0], npEntry.data.d[1]};
6776 res = resultMetadata.update(ANDROID_SENSOR_NOISE_PROFILE, np, 2);
6777 if (res != OK) {
6778 ALOGE("%s: Failed to update SENSOR_NOISE_PROFILE: %s (%d)",
6779 __FUNCTION__, strerror(-res), res);
6780 return res;
6781 }
6782 }
6783
6784 // ANDROID_STATISTICS_LENS_SHADING_MAP
6785 camera_metadata_ro_entry lsSizeEntry = deviceInfo.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
6786 camera_metadata_entry lsEntry = resultMetadata.find(ANDROID_STATISTICS_LENS_SHADING_MAP);
6787 if (lsSizeEntry.count == 2 && lsEntry.count > 0
6788 && (int32_t)lsEntry.count == 4 * lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]) {
6789 for (int32_t i = 0; i < lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]; i++) {
6790 lsEntry.data.f[4*i+1] = lsEntry.data.f[4*i];
6791 lsEntry.data.f[4*i+2] = lsEntry.data.f[4*i];
6792 lsEntry.data.f[4*i+3] = lsEntry.data.f[4*i];
6793 }
6794 }
6795
6796 // ANDROID_TONEMAP_CURVE_BLUE
6797 // ANDROID_TONEMAP_CURVE_GREEN
6798 // ANDROID_TONEMAP_CURVE_RED
6799 camera_metadata_entry tcbEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_BLUE);
6800 camera_metadata_entry tcgEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_GREEN);
6801 camera_metadata_entry tcrEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_RED);
6802 if (tcbEntry.count > 0
6803 && tcbEntry.count == tcgEntry.count
6804 && tcbEntry.count == tcrEntry.count) {
6805 for (size_t i = 0; i < tcbEntry.count; i++) {
6806 tcbEntry.data.f[i] = tcrEntry.data.f[i];
6807 tcgEntry.data.f[i] = tcrEntry.data.f[i];
6808 }
6809 }
6810
6811 return res;
6812}
6813
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006814}; // namespace android