blob: 09b1f026233e9dc76be974dc3cfda5b4996047b3 [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"
Yin-Chia Yehb978c382019-10-30 00:22:37 -070061#include "device3/Camera3OfflineSession.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080064
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080065#include <tuple>
66
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080068using namespace android::hardware::camera;
69using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070
71namespace android {
72
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080073Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080074 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080075 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070076 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070077 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070078 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070079 mUsePartialResult(false),
80 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080081 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070082 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070083 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070084 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070085 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070086 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070087 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000088 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010089 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070090 mLastTemplateId(-1),
91 mNeedFixupMonochromeTags(false)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092{
93 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080094 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095}
96
97Camera3Device::~Camera3Device()
98{
99 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800100 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700101 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102}
103
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800104const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800105 return mId;
106}
107
Emilian Peevbd8c5032018-02-14 23:05:40 +0000108status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800109 ATRACE_CALL();
110 Mutex::Autolock il(mInterfaceLock);
111 Mutex::Autolock l(mLock);
112
113 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
114 if (mStatus != STATUS_UNINITIALIZED) {
115 CLOGE("Already initialized!");
116 return INVALID_OPERATION;
117 }
118 if (manager == nullptr) return INVALID_OPERATION;
119
120 sp<ICameraDeviceSession> session;
121 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800122 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 /*out*/ &session);
124 ATRACE_END();
125 if (res != OK) {
126 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
127 return res;
128 }
129
Steven Moreland5ff9c912017-03-09 23:13:00 -0800130 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800131 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700132 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800133 session->close();
134 return res;
135 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800136 mSupportNativeZoomRatio = manager->supportNativeZoomRatio(mId.string());
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800137
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700138 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700139 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700140 if (isLogical) {
141 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700142 res = manager->getCameraCharacteristics(
143 physicalId, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700144 if (res != OK) {
145 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
146 physicalId.c_str(), strerror(-res), res);
147 session->close();
148 return res;
149 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700150
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800151 bool usePrecorrectArray =
152 DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId]);
153 if (usePrecorrectArray) {
154 res = mDistortionMappers[physicalId].setupStaticInfo(
155 mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700156 if (res != OK) {
157 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
158 "correction", physicalId.c_str());
159 session->close();
160 return res;
161 }
162 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800163
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800164 mZoomRatioMappers[physicalId] = ZoomRatioMapper(
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800165 &mPhysicalDeviceInfoMap[physicalId],
166 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700167 }
168 }
169
Yifan Hongf79b5542017-04-11 14:44:25 -0700170 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700171 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
172 [&queue](const auto& descriptor) {
173 queue = std::make_shared<RequestMetadataQueue>(descriptor);
174 if (!queue->isValid() || queue->availableToWrite() <= 0) {
175 ALOGE("HAL returns empty request metadata fmq, not use it");
176 queue = nullptr;
177 // don't use the queue onwards.
178 }
179 });
180 if (!requestQueueRet.isOk()) {
181 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
182 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700183 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700184 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700185
186 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700187 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700188 [&resQueue](const auto& descriptor) {
189 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
190 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700191 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700192 resQueue = nullptr;
193 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700194 }
195 });
196 if (!resultQueueRet.isOk()) {
197 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
198 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700199 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700200 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700201 IF_ALOGV() {
202 session->interfaceChain([](
203 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
204 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800205 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700206 ALOGV(" %s", iface.c_str());
207 }
208 });
209 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700210
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800211 camera_metadata_entry bufMgrMode =
212 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
213 if (bufMgrMode.count > 0) {
214 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
215 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
216 }
217
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700218 camera_metadata_entry_t capabilities = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
219 for (size_t i = 0; i < capabilities.count; i++) {
220 uint8_t capability = capabilities.data.u8[i];
221 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_OFFLINE_PROCESSING) {
222 mSupportOfflineProcessing = true;
223 }
224 }
225
226 mInterface = new HalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
Emilian Peev71c73a22017-03-21 16:35:51 +0000227 std::string providerType;
228 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000229 mTagMonitor.initialize(mVendorTagId);
230 if (!monitorTags.isEmpty()) {
231 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
232 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800233
Shuzhen Wang268a1362018-10-16 16:32:59 -0700234 // Metadata tags needs fixup for monochrome camera device version less
235 // than 3.5.
236 hardware::hidl_version maxVersion{0,0};
237 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
238 if (res != OK) {
239 ALOGE("%s: Error in getting camera device version id: %s (%d)",
240 __FUNCTION__, strerror(-res), res);
241 return res;
242 }
243 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
244 maxVersion.get_major(), maxVersion.get_minor());
245
246 bool isMonochrome = false;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700247 for (size_t i = 0; i < capabilities.count; i++) {
248 uint8_t capability = capabilities.data.u8[i];
Shuzhen Wang268a1362018-10-16 16:32:59 -0700249 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
250 isMonochrome = true;
251 }
252 }
253 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
254
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800255 return initializeCommonLocked();
256}
257
258status_t Camera3Device::initializeCommonLocked() {
259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 /** Start up status tracker thread */
261 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800262 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 if (res != OK) {
264 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
265 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800266 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 mStatusTracker.clear();
268 return res;
269 }
270
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700271 /** Register in-flight map to the status tracker */
272 mInFlightStatusId = mStatusTracker->addComponent();
273
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700274 if (mUseHalBufManager) {
275 res = mRequestBufferSM.initialize(mStatusTracker);
276 if (res != OK) {
277 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
278 strerror(-res), res);
279 mInterface->close();
280 mStatusTracker.clear();
281 return res;
282 }
283 }
284
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800285 /** Create buffer manager */
286 mBufferManager = new Camera3BufferManager();
287
288 Vector<int32_t> sessionParamKeys;
289 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
290 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
291 if (sessionKeysEntry.count > 0) {
292 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
293 }
294
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700295 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700296 mRequestThread = new RequestThread(
297 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800298 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800299 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700300 SET_ERR_L("Unable to start request queue thread: %s (%d)",
301 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800302 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800304 return res;
305 }
306
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700307 mPreparerThread = new PreparerThread();
308
Ruben Brunk183f0562015-08-12 12:55:02 -0700309 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700311 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700312 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800314
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800315 // Measure the clock domain offset between camera and video/hw_composer
316 camera_metadata_entry timestampSource =
317 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
318 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
319 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
320 mTimestampOffset = getMonoToBoottimeOffset();
321 }
322
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700323 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100324 camera_metadata_entry partialResultsCount =
325 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
326 if (partialResultsCount.count > 0) {
327 mNumPartialResults = partialResultsCount.data.i32[0];
328 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700329 }
330
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700331 camera_metadata_entry configs =
332 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
333 for (uint32_t i = 0; i < configs.count; i += 4) {
334 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
335 configs.data.i32[i + 3] ==
336 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
337 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
338 configs.data.i32[i + 2]));
339 }
340 }
341
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800342 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
343 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700344 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700345 if (res != OK) {
346 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
347 return res;
348 }
349 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800350
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800351 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800352 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800353
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800354 return OK;
355}
356
357status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700358 return disconnectImpl();
359}
360
361status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800362 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700363 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800364
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700365 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800366
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700367 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700368 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700369 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700370 {
371 Mutex::Autolock l(mLock);
372 if (mStatus == STATUS_UNINITIALIZED) return res;
373
374 if (mStatus == STATUS_ACTIVE ||
375 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
376 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700377 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700378 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700379 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700380 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700381 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700383 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
384 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700385 // Continue to close device even in case of error
386 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700387 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800388 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800389
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700390 if (mStatus == STATUS_ERROR) {
391 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700392 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700393
394 if (mStatusTracker != NULL) {
395 mStatusTracker->requestExit();
396 }
397
398 if (mRequestThread != NULL) {
399 mRequestThread->requestExit();
400 }
401
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700402 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
403 for (size_t i = 0; i < mOutputStreams.size(); i++) {
404 streams.push_back(mOutputStreams[i]);
405 }
406 if (mInputStream != nullptr) {
407 streams.push_back(mInputStream);
408 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700409 }
410
411 // Joining done without holding mLock, otherwise deadlocks may ensue
412 // as the threads try to access parent state
413 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
414 // HAL may be in a bad state, so waiting for request thread
415 // (which may be stuck in the HAL processCaptureRequest call)
416 // could be dangerous.
417 mRequestThread->join();
418 }
419
420 if (mStatusTracker != NULL) {
421 mStatusTracker->join();
422 }
423
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800424 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700425 {
426 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800427 mRequestThread.clear();
Emilian Peev2843c362018-09-26 08:49:40 +0100428 Mutex::Autolock stLock(mTrackerLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700429 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800430 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700431 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800432
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700433 // Call close without internal mutex held, as the HAL close may need to
434 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800435 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700436
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700437 flushInflightRequests();
438
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700439 {
440 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800441 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700442 mOutputStreams.clear();
443 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700444 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700445 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700446 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700447 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700449 for (auto& weakStream : streams) {
450 sp<Camera3StreamInterface> stream = weakStream.promote();
451 if (stream != nullptr) {
452 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
453 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
454 }
455 }
456
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700457 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700458 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800459}
460
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700461// For dumping/debugging only -
462// try to acquire a lock a few times, eventually give up to proceed with
463// debug/dump operations
464bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
465 bool gotLock = false;
466 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
467 if (lock.tryLock() == NO_ERROR) {
468 gotLock = true;
469 break;
470 } else {
471 usleep(kDumpSleepDuration);
472 }
473 }
474 return gotLock;
475}
476
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700477Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
478 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100479 const int STREAM_CONFIGURATION_SIZE = 4;
480 const int STREAM_FORMAT_OFFSET = 0;
481 const int STREAM_WIDTH_OFFSET = 1;
482 const int STREAM_HEIGHT_OFFSET = 2;
483 const int STREAM_IS_INPUT_OFFSET = 3;
484 camera_metadata_ro_entry_t availableStreamConfigs =
485 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
486 if (availableStreamConfigs.count == 0 ||
487 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
488 return Size(0, 0);
489 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700490
Emilian Peev08dd2452017-04-06 16:55:14 +0100491 // Get max jpeg size (area-wise).
492 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
493 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
494 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
495 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
496 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
497 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
498 && format == HAL_PIXEL_FORMAT_BLOB &&
499 (width * height > maxJpegWidth * maxJpegHeight)) {
500 maxJpegWidth = width;
501 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700502 }
503 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100504
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700505 return Size(maxJpegWidth, maxJpegHeight);
506}
507
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800508nsecs_t Camera3Device::getMonoToBoottimeOffset() {
509 // try three times to get the clock offset, choose the one
510 // with the minimum gap in measurements.
511 const int tries = 3;
512 nsecs_t bestGap, measured;
513 for (int i = 0; i < tries; ++i) {
514 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
515 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
516 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
517 const nsecs_t gap = tmono2 - tmono;
518 if (i == 0 || gap < bestGap) {
519 bestGap = gap;
520 measured = tbase - ((tmono + tmono2) >> 1);
521 }
522 }
523 return measured;
524}
525
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800526hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
527 int frameworkFormat) {
528 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
529}
530
531DataspaceFlags Camera3Device::mapToHidlDataspace(
532 android_dataspace dataSpace) {
533 return dataSpace;
534}
535
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700536BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100537 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700538 return usage;
539}
540
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800541StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
542 switch (rotation) {
543 case CAMERA3_STREAM_ROTATION_0:
544 return StreamRotation::ROTATION_0;
545 case CAMERA3_STREAM_ROTATION_90:
546 return StreamRotation::ROTATION_90;
547 case CAMERA3_STREAM_ROTATION_180:
548 return StreamRotation::ROTATION_180;
549 case CAMERA3_STREAM_ROTATION_270:
550 return StreamRotation::ROTATION_270;
551 }
552 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
553 return StreamRotation::ROTATION_0;
554}
555
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800556status_t Camera3Device::mapToStreamConfigurationMode(
557 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
558 if (mode == nullptr) return BAD_VALUE;
559 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
560 switch(operationMode) {
561 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
562 *mode = StreamConfigurationMode::NORMAL_MODE;
563 break;
564 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
565 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
566 break;
567 default:
568 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
569 return BAD_VALUE;
570 }
571 } else {
572 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800573 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800574 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800575}
576
577camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
578 switch (status) {
579 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
580 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
581 }
582 return CAMERA3_BUFFER_STATUS_ERROR;
583}
584
585int Camera3Device::mapToFrameworkFormat(
586 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
587 return static_cast<uint32_t>(pixelFormat);
588}
589
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700590android_dataspace Camera3Device::mapToFrameworkDataspace(
591 DataspaceFlags dataSpace) {
592 return static_cast<android_dataspace>(dataSpace);
593}
594
Emilian Peev050f5dc2017-05-18 14:43:56 +0100595uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700596 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700597 return usage;
598}
599
Emilian Peev050f5dc2017-05-18 14:43:56 +0100600uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700601 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700602 return usage;
603}
604
Zhijun Hef7da0962014-04-24 13:27:56 -0700605ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700606 // Get max jpeg size (area-wise).
607 Size maxJpegResolution = getMaxJpegResolution();
608 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
610 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700611 return BAD_VALUE;
612 }
613
Zhijun Hef7da0962014-04-24 13:27:56 -0700614 // Get max jpeg buffer size
615 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700616 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
617 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800618 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
619 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700620 return BAD_VALUE;
621 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700622 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800623 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700624
625 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700626 float scaleFactor = ((float) (width * height)) /
627 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800628 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
629 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700630 if (jpegBufferSize > maxJpegBufferSize) {
631 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700632 }
633
634 return jpegBufferSize;
635}
636
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700637ssize_t Camera3Device::getPointCloudBufferSize() const {
638 const int FLOATS_PER_POINT=4;
639 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
640 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800641 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
642 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700643 return BAD_VALUE;
644 }
645 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
646 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
647 return maxBytesForPointCloud;
648}
649
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800650ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800651 const int PER_CONFIGURATION_SIZE = 3;
652 const int WIDTH_OFFSET = 0;
653 const int HEIGHT_OFFSET = 1;
654 const int SIZE_OFFSET = 2;
655 camera_metadata_ro_entry rawOpaqueSizes =
656 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800657 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800658 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800659 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
660 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800661 return BAD_VALUE;
662 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700663
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800664 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
665 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
666 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
667 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
668 }
669 }
670
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800671 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
672 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800673 return BAD_VALUE;
674}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700675
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800676status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
677 ATRACE_CALL();
678 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679
680 // Try to lock, but continue in case of failure (to avoid blocking in
681 // deadlocks)
682 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
683 bool gotLock = tryLockSpinRightRound(mLock);
684
685 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800686 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
687 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700688 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800689 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
690 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800692 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700693
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800694 String16 templatesOption("-t");
695 int n = args.size();
696 for (int i = 0; i < n; i++) {
697 if (args[i] == templatesOption) {
698 dumpTemplates = true;
699 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000700 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700701 if (i + 1 < n) {
702 String8 monitorTags = String8(args[i + 1]);
703 if (monitorTags == "off") {
704 mTagMonitor.disableMonitoring();
705 } else {
706 mTagMonitor.parseTagsToMonitor(monitorTags);
707 }
708 } else {
709 mTagMonitor.disableMonitoring();
710 }
711 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800712 }
713
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800716 const char *status =
717 mStatus == STATUS_ERROR ? "ERROR" :
718 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700719 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
720 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800721 mStatus == STATUS_ACTIVE ? "ACTIVE" :
722 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700725 if (mStatus == STATUS_ERROR) {
726 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
727 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800729 const char *mode =
730 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
731 mOperatingMode == static_cast<int>(
732 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
733 "CUSTOM";
734 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735
736 if (mInputStream != NULL) {
737 write(fd, lines.string(), lines.size());
738 mInputStream->dump(fd, args);
739 } else {
740 lines.appendFormat(" No input stream.\n");
741 write(fd, lines.string(), lines.size());
742 }
743 for (size_t i = 0; i < mOutputStreams.size(); i++) {
744 mOutputStreams[i]->dump(fd,args);
745 }
746
Zhijun He431503c2016-03-07 17:30:16 -0800747 if (mBufferManager != NULL) {
748 lines = String8(" Camera3 Buffer Manager:\n");
749 write(fd, lines.string(), lines.size());
750 mBufferManager->dump(fd, args);
751 }
Zhijun He125684a2015-12-26 15:07:30 -0800752
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700753 lines = String8(" In-flight requests:\n");
754 if (mInFlightMap.size() == 0) {
755 lines.append(" None\n");
756 } else {
757 for (size_t i = 0; i < mInFlightMap.size(); i++) {
758 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700759 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700760 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800761 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700762 r.numBuffersLeft);
763 }
764 }
765 write(fd, lines.string(), lines.size());
766
Shuzhen Wang686f6442017-06-20 16:16:04 -0700767 if (mRequestThread != NULL) {
768 mRequestThread->dumpCaptureRequestLatency(fd,
769 " ProcessCaptureRequest latency histogram:");
770 }
771
Igor Murashkin1e479c02013-09-06 16:55:14 -0700772 {
773 lines = String8(" Last request sent:\n");
774 write(fd, lines.string(), lines.size());
775
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700777 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
778 }
779
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800780 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800781 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800782 "TEMPLATE_PREVIEW",
783 "TEMPLATE_STILL_CAPTURE",
784 "TEMPLATE_VIDEO_RECORD",
785 "TEMPLATE_VIDEO_SNAPSHOT",
786 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800787 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800788 };
789
790 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800791 camera_metadata_t *templateRequest = nullptr;
792 mInterface->constructDefaultRequestSettings(
793 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800794 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800795 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800796 lines.append(" Not supported\n");
797 write(fd, lines.string(), lines.size());
798 } else {
799 write(fd, lines.string(), lines.size());
800 dump_indented_camera_metadata(templateRequest,
801 fd, /*verbosity*/2, /*indentation*/8);
802 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800803 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800804 }
805 }
806
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700807 mTagMonitor.dumpMonitoredMetadata(fd);
808
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800809 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800810 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800812 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 if (gotLock) mLock.unlock();
816 if (gotInterfaceLock) mInterfaceLock.unlock();
817
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800818 return OK;
819}
820
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700821const CameraMetadata& Camera3Device::info(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800822 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
824 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700825 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 mStatus == STATUS_ERROR ?
827 "when in error state" : "before init");
828 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700829 if (physicalId.isEmpty()) {
830 return mDeviceInfo;
831 } else {
832 std::string id(physicalId.c_str());
833 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
834 return mPhysicalDeviceInfoMap.at(id);
835 } else {
836 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
837 return mDeviceInfo;
838 }
839 }
840}
841
842const CameraMetadata& Camera3Device::info() const {
843 String8 emptyId;
844 return info(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845}
846
Jianing Wei90e59c92014-03-12 18:29:36 -0700847status_t Camera3Device::checkStatusOkToCaptureLocked() {
848 switch (mStatus) {
849 case STATUS_ERROR:
850 CLOGE("Device has encountered a serious error");
851 return INVALID_OPERATION;
852 case STATUS_UNINITIALIZED:
853 CLOGE("Device not initialized");
854 return INVALID_OPERATION;
855 case STATUS_UNCONFIGURED:
856 case STATUS_CONFIGURED:
857 case STATUS_ACTIVE:
858 // OK
859 break;
860 default:
861 SET_ERR_L("Unexpected status: %d", mStatus);
862 return INVALID_OPERATION;
863 }
864 return OK;
865}
866
867status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000868 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700869 const std::list<const SurfaceMap> &surfaceMaps,
870 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700871 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700872 if (requestList == NULL) {
873 CLOGE("requestList cannot be NULL.");
874 return BAD_VALUE;
875 }
876
Jianing Weicb0652e2014-03-12 18:29:36 -0700877 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000878 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700879 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
880 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
881 ++metadataIt, ++surfaceMapIt) {
882 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700883 if (newRequest == 0) {
884 CLOGE("Can't create capture request");
885 return BAD_VALUE;
886 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700887
Shuzhen Wang9d066012016-09-30 11:30:20 -0700888 newRequest->mRepeating = repeating;
889
Jianing Weicb0652e2014-03-12 18:29:36 -0700890 // Setup burst Id and request Id
891 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000892 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
893 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700894 CLOGE("RequestID entry exists; but must not be empty in metadata");
895 return BAD_VALUE;
896 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000897 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
898 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700899 } else {
900 CLOGE("RequestID does not exist in metadata");
901 return BAD_VALUE;
902 }
903
Jianing Wei90e59c92014-03-12 18:29:36 -0700904 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700905
906 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700907 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700908 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
909 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
910 return BAD_VALUE;
911 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700912
913 // Setup batch size if this is a high speed video recording request.
914 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
915 auto firstRequest = requestList->begin();
916 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
917 if (outputStream->isVideoStream()) {
918 (*firstRequest)->mBatchSize = requestList->size();
919 break;
920 }
921 }
922 }
923
Jianing Wei90e59c92014-03-12 18:29:36 -0700924 return OK;
925}
926
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800927status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800928 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800929
Emilian Peevaebbe412018-01-15 13:53:24 +0000930 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700931 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000932 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700933
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800934 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700935}
936
Emilian Peevaebbe412018-01-15 13:53:24 +0000937void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700938 std::list<const SurfaceMap>& surfaceMaps,
939 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000940 PhysicalCameraSettingsList requestList;
941 requestList.push_back({std::string(getId().string()), request});
942 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700943
944 SurfaceMap surfaceMap;
945 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
946 // With no surface list passed in, stream and surface will have 1-to-1
947 // mapping. So the surface index is 0 for each stream in the surfaceMap.
948 for (size_t i = 0; i < streams.count; i++) {
949 surfaceMap[streams.data.i32[i]].push_back(0);
950 }
951 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800952}
953
Jianing Wei90e59c92014-03-12 18:29:36 -0700954status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000955 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700956 const std::list<const SurfaceMap> &surfaceMaps,
957 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700958 /*out*/
959 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700960 ATRACE_CALL();
961 Mutex::Autolock il(mInterfaceLock);
962 Mutex::Autolock l(mLock);
963
964 status_t res = checkStatusOkToCaptureLocked();
965 if (res != OK) {
966 // error logged by previous call
967 return res;
968 }
969
970 RequestList requestList;
971
Shuzhen Wang0129d522016-10-30 22:43:41 -0700972 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
973 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700974 if (res != OK) {
975 // error logged by previous call
976 return res;
977 }
978
979 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700980 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700981 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700982 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700983 }
984
985 if (res == OK) {
986 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
987 if (res != OK) {
988 SET_ERR_L("Can't transition to active in %f seconds!",
989 kActiveTimeout/1e9);
990 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800991 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700992 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700993 } else {
994 CLOGE("Cannot queue request. Impossible.");
995 return BAD_VALUE;
996 }
997
998 return res;
999}
1000
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001001hardware::Return<void> Camera3Device::requestStreamBuffers(
1002 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
1003 requestStreamBuffers_cb _hidl_cb) {
1004 using hardware::camera::device::V3_5::BufferRequestStatus;
1005 using hardware::camera::device::V3_5::StreamBufferRet;
1006 using hardware::camera::device::V3_5::StreamBufferRequestError;
1007
1008 std::lock_guard<std::mutex> lock(mRequestBufferInterfaceLock);
1009
1010 hardware::hidl_vec<StreamBufferRet> bufRets;
1011 if (!mUseHalBufManager) {
1012 ALOGE("%s: Camera %s does not support HAL buffer management",
1013 __FUNCTION__, mId.string());
1014 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1015 return hardware::Void();
1016 }
1017
1018 SortedVector<int32_t> streamIds;
1019 ssize_t sz = streamIds.setCapacity(bufReqs.size());
1020 if (sz < 0 || static_cast<size_t>(sz) != bufReqs.size()) {
1021 ALOGE("%s: failed to allocate memory for %zu buffer requests",
1022 __FUNCTION__, bufReqs.size());
1023 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1024 return hardware::Void();
1025 }
1026
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001027 if (bufReqs.size() > mOutputStreams.size()) {
1028 ALOGE("%s: too many buffer requests (%zu > # of output streams %zu)",
1029 __FUNCTION__, bufReqs.size(), mOutputStreams.size());
1030 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1031 return hardware::Void();
1032 }
1033
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001034 // Check for repeated streamId
1035 for (const auto& bufReq : bufReqs) {
1036 if (streamIds.indexOf(bufReq.streamId) != NAME_NOT_FOUND) {
1037 ALOGE("%s: Stream %d appear multiple times in buffer requests",
1038 __FUNCTION__, bufReq.streamId);
1039 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1040 return hardware::Void();
1041 }
1042 streamIds.add(bufReq.streamId);
1043 }
1044
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001045 if (!mRequestBufferSM.startRequestBuffer()) {
1046 ALOGE("%s: request buffer disallowed while camera service is configuring",
1047 __FUNCTION__);
1048 _hidl_cb(BufferRequestStatus::FAILED_CONFIGURING, bufRets);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001049 return hardware::Void();
1050 }
1051
1052 bufRets.resize(bufReqs.size());
1053
1054 bool allReqsSucceeds = true;
1055 bool oneReqSucceeds = false;
1056 for (size_t i = 0; i < bufReqs.size(); i++) {
1057 const auto& bufReq = bufReqs[i];
1058 auto& bufRet = bufRets[i];
1059 int32_t streamId = bufReq.streamId;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001060 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.get(streamId);
1061 if (outputStream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001062 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
1063 hardware::hidl_vec<StreamBufferRet> emptyBufRets;
1064 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, emptyBufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001065 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001066 return hardware::Void();
1067 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001068
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001069 if (outputStream->isAbandoned()) {
1070 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
1071 allReqsSucceeds = false;
1072 continue;
1073 }
1074
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001075 bufRet.streamId = streamId;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001076 size_t handOutBufferCount = outputStream->getOutstandingBuffersCount();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001077 uint32_t numBuffersRequested = bufReq.numBuffersRequested;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001078 size_t totalHandout = handOutBufferCount + numBuffersRequested;
1079 uint32_t maxBuffers = outputStream->asHalStream()->max_buffers;
1080 if (totalHandout > maxBuffers) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001081 // Not able to allocate enough buffer. Exit early for this stream
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001082 ALOGE("%s: request too much buffers for stream %d: at HAL: %zu + requesting: %d"
1083 " > max: %d", __FUNCTION__, streamId, handOutBufferCount,
1084 numBuffersRequested, maxBuffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001085 bufRet.val.error(StreamBufferRequestError::MAX_BUFFER_EXCEEDED);
1086 allReqsSucceeds = false;
1087 continue;
1088 }
1089
1090 hardware::hidl_vec<StreamBuffer> tmpRetBuffers(numBuffersRequested);
1091 bool currentReqSucceeds = true;
1092 std::vector<camera3_stream_buffer_t> streamBuffers(numBuffersRequested);
1093 size_t numAllocatedBuffers = 0;
1094 size_t numPushedInflightBuffers = 0;
1095 for (size_t b = 0; b < numBuffersRequested; b++) {
1096 camera3_stream_buffer_t& sb = streamBuffers[b];
1097 // Since this method can run concurrently with request thread
1098 // We need to update the wait duration everytime we call getbuffer
1099 nsecs_t waitDuration = kBaseGetBufferWait + getExpectedInFlightDuration();
1100 status_t res = outputStream->getBuffer(&sb, waitDuration);
1101 if (res != OK) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001102 if (res == NO_INIT || res == DEAD_OBJECT) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001103 ALOGV("%s: Can't get output buffer for stream %d: %s (%d)",
1104 __FUNCTION__, streamId, strerror(-res), res);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001105 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001106 } else {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001107 ALOGE("%s: Can't get output buffer for stream %d: %s (%d)",
1108 __FUNCTION__, streamId, strerror(-res), res);
1109 if (res == TIMED_OUT || res == NO_MEMORY) {
1110 bufRet.val.error(StreamBufferRequestError::NO_BUFFER_AVAILABLE);
1111 } else {
1112 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1113 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001114 }
1115 currentReqSucceeds = false;
1116 break;
1117 }
1118 numAllocatedBuffers++;
1119
1120 buffer_handle_t *buffer = sb.buffer;
1121 auto pair = mInterface->getBufferId(*buffer, streamId);
1122 bool isNewBuffer = pair.first;
1123 uint64_t bufferId = pair.second;
1124 StreamBuffer& hBuf = tmpRetBuffers[b];
1125
1126 hBuf.streamId = streamId;
1127 hBuf.bufferId = bufferId;
1128 hBuf.buffer = (isNewBuffer) ? *buffer : nullptr;
1129 hBuf.status = BufferStatus::OK;
1130 hBuf.releaseFence = nullptr;
1131
1132 native_handle_t *acquireFence = nullptr;
1133 if (sb.acquire_fence != -1) {
1134 acquireFence = native_handle_create(1,0);
1135 acquireFence->data[0] = sb.acquire_fence;
1136 }
1137 hBuf.acquireFence.setTo(acquireFence, /*shouldOwn*/true);
1138 hBuf.releaseFence = nullptr;
1139
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08001140 res = mInterface->pushInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001141 if (res != OK) {
1142 ALOGE("%s: Can't get register request buffers for stream %d: %s (%d)",
1143 __FUNCTION__, streamId, strerror(-res), res);
1144 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1145 currentReqSucceeds = false;
1146 break;
1147 }
1148 numPushedInflightBuffers++;
1149 }
1150 if (currentReqSucceeds) {
1151 bufRet.val.buffers(std::move(tmpRetBuffers));
1152 oneReqSucceeds = true;
1153 } else {
1154 allReqsSucceeds = false;
1155 for (size_t b = 0; b < numPushedInflightBuffers; b++) {
1156 StreamBuffer& hBuf = tmpRetBuffers[b];
1157 buffer_handle_t* buffer;
1158 status_t res = mInterface->popInflightRequestBuffer(hBuf.bufferId, &buffer);
1159 if (res != OK) {
1160 SET_ERR("%s: popInflightRequestBuffer failed for stream %d: %s (%d)",
1161 __FUNCTION__, streamId, strerror(-res), res);
1162 }
1163 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001164 for (size_t b = 0; b < numAllocatedBuffers; b++) {
1165 camera3_stream_buffer_t& sb = streamBuffers[b];
1166 sb.acquire_fence = -1;
1167 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
1168 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001169 returnOutputBuffers(streamBuffers.data(), numAllocatedBuffers, 0);
1170 }
1171 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001172
1173 _hidl_cb(allReqsSucceeds ? BufferRequestStatus::OK :
1174 oneReqSucceeds ? BufferRequestStatus::FAILED_PARTIAL :
1175 BufferRequestStatus::FAILED_UNKNOWN,
1176 bufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001177 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001178 return hardware::Void();
1179}
1180
1181hardware::Return<void> Camera3Device::returnStreamBuffers(
1182 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
1183 if (!mUseHalBufManager) {
1184 ALOGE("%s: Camera %s does not support HAL buffer managerment",
1185 __FUNCTION__, mId.string());
1186 return hardware::Void();
1187 }
1188
1189 for (const auto& buf : buffers) {
1190 if (buf.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
1191 ALOGE("%s: cannot return a buffer without bufferId", __FUNCTION__);
1192 continue;
1193 }
1194
1195 buffer_handle_t* buffer;
1196 status_t res = mInterface->popInflightRequestBuffer(buf.bufferId, &buffer);
1197
1198 if (res != OK) {
1199 ALOGE("%s: cannot find in-flight buffer %" PRIu64 " for stream %d",
1200 __FUNCTION__, buf.bufferId, buf.streamId);
1201 continue;
1202 }
1203
1204 camera3_stream_buffer_t streamBuffer;
1205 streamBuffer.buffer = buffer;
1206 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
1207 streamBuffer.acquire_fence = -1;
1208 streamBuffer.release_fence = -1;
1209
1210 if (buf.releaseFence == nullptr) {
1211 streamBuffer.release_fence = -1;
1212 } else if (buf.releaseFence->numFds == 1) {
1213 streamBuffer.release_fence = dup(buf.releaseFence->data[0]);
1214 } else {
1215 ALOGE("%s: Invalid release fence, fd count is %d, not 1",
1216 __FUNCTION__, buf.releaseFence->numFds);
1217 continue;
1218 }
1219
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001220 sp<Camera3StreamInterface> stream = mOutputStreams.get(buf.streamId);
1221 if (stream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001222 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, buf.streamId);
1223 continue;
1224 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001225 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001226 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
1227 }
1228 return hardware::Void();
1229}
1230
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001231hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001232 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001233 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001234 // Ideally we should grab mLock, but that can lead to deadlock, and
1235 // it's not super important to get up to date value of mStatus for this
1236 // warning print, hence skipping the lock here
1237 if (mStatus == STATUS_ERROR) {
1238 // Per API contract, HAL should act as closed after device error
1239 // But mStatus can be set to error by framework as well, so just log
1240 // a warning here.
1241 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001242 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001243
1244 if (mProcessCaptureResultLock.tryLock() != OK) {
1245 // This should never happen; it indicates a wrong client implementation
1246 // that doesn't follow the contract. But, we can be tolerant here.
1247 ALOGE("%s: callback overlapped! waiting 1s...",
1248 __FUNCTION__);
1249 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1250 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1251 __FUNCTION__);
1252 // really don't know what to do, so bail out.
1253 return hardware::Void();
1254 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001255 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001256 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001257 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001258 }
1259 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001260 return hardware::Void();
1261}
1262
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001263// Only one processCaptureResult should be called at a time, so
1264// the locks won't block. The locks are present here simply to enforce this.
1265hardware::Return<void> Camera3Device::processCaptureResult(
1266 const hardware::hidl_vec<
1267 hardware::camera::device::V3_2::CaptureResult>& results) {
1268 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1269
1270 // Ideally we should grab mLock, but that can lead to deadlock, and
1271 // it's not super important to get up to date value of mStatus for this
1272 // warning print, hence skipping the lock here
1273 if (mStatus == STATUS_ERROR) {
1274 // Per API contract, HAL should act as closed after device error
1275 // But mStatus can be set to error by framework as well, so just log
1276 // a warning here.
1277 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1278 }
1279
1280 if (mProcessCaptureResultLock.tryLock() != OK) {
1281 // This should never happen; it indicates a wrong client implementation
1282 // that doesn't follow the contract. But, we can be tolerant here.
1283 ALOGE("%s: callback overlapped! waiting 1s...",
1284 __FUNCTION__);
1285 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1286 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1287 __FUNCTION__);
1288 // really don't know what to do, so bail out.
1289 return hardware::Void();
1290 }
1291 }
1292 for (const auto& result : results) {
1293 processOneCaptureResultLocked(result, noPhysMetadata);
1294 }
1295 mProcessCaptureResultLock.unlock();
1296 return hardware::Void();
1297}
1298
1299status_t Camera3Device::readOneCameraMetadataLocked(
1300 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
1301 const hardware::camera::device::V3_2::CameraMetadata& result) {
1302 if (fmqResultSize > 0) {
1303 resultMetadata.resize(fmqResultSize);
1304 if (mResultMetadataQueue == nullptr) {
1305 return NO_MEMORY; // logged in initialize()
1306 }
1307 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
1308 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
1309 __FUNCTION__, fmqResultSize);
1310 return INVALID_OPERATION;
1311 }
1312 } else {
1313 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
1314 result.size());
1315 }
1316
1317 if (resultMetadata.size() != 0) {
1318 status_t res;
1319 const camera_metadata_t* metadata =
1320 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
1321 size_t expected_metadata_size = resultMetadata.size();
1322 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
1323 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
1324 __FUNCTION__, strerror(-res), res);
1325 return INVALID_OPERATION;
1326 }
1327 }
1328
1329 return OK;
1330}
1331
Yifan Honga640c5a2017-04-12 16:30:31 -07001332void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001333 const hardware::camera::device::V3_2::CaptureResult& result,
1334 const hardware::hidl_vec<
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001335 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001336 camera3_capture_result r;
1337 status_t res;
1338 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -07001339
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001340 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -07001341 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001342 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
1343 if (res != OK) {
1344 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1345 __FUNCTION__, result.frameNumber);
1346 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001347 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001348 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001349
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001350 // Read and validate physical camera metadata
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001351 size_t physResultCount = physicalCameraMetadata.size();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001352 std::vector<const char*> physCamIds(physResultCount);
1353 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1354 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1355 physResultMetadata.resize(physResultCount);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001356 for (size_t i = 0; i < physicalCameraMetadata.size(); i++) {
1357 res = readOneCameraMetadataLocked(physicalCameraMetadata[i].fmqMetadataSize,
1358 physResultMetadata[i], physicalCameraMetadata[i].metadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001359 if (res != OK) {
1360 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1361 __FUNCTION__, result.frameNumber,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001362 physicalCameraMetadata[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001363 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001364 }
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001365 physCamIds[i] = physicalCameraMetadata[i].physicalCameraId.c_str();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001366 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1367 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001368 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001369 r.num_physcam_metadata = physResultCount;
1370 r.physcam_ids = physCamIds.data();
1371 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001372
1373 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1374 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1375 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1376 auto& bDst = outputBuffers[i];
1377 const StreamBuffer &bSrc = result.outputBuffers[i];
1378
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001379 sp<Camera3StreamInterface> stream = mOutputStreams.get(bSrc.streamId);
1380 if (stream == nullptr) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001381 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1382 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001383 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001384 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001385 bDst.stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001386
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001387 bool noBufferReturned = false;
1388 buffer_handle_t *buffer = nullptr;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001389 if (mUseHalBufManager) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001390 // This is suspicious most of the time but can be correct during flush where HAL
1391 // has to return capture result before a buffer is requested
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001392 if (bSrc.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001393 if (bSrc.status == BufferStatus::OK) {
1394 ALOGE("%s: Frame %d: Buffer %zu: No bufferId for stream %d",
1395 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
1396 // Still proceeds so other buffers can be returned
1397 }
1398 noBufferReturned = true;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001399 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001400 if (noBufferReturned) {
1401 res = OK;
1402 } else {
1403 res = mInterface->popInflightRequestBuffer(bSrc.bufferId, &buffer);
1404 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001405 } else {
1406 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
1407 }
1408
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001409 if (res != OK) {
1410 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1411 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001412 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001413 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001414
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001415 bDst.buffer = buffer;
1416 bDst.status = mapHidlBufferStatus(bSrc.status);
1417 bDst.acquire_fence = -1;
1418 if (bSrc.releaseFence == nullptr) {
1419 bDst.release_fence = -1;
1420 } else if (bSrc.releaseFence->numFds == 1) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001421 if (noBufferReturned) {
1422 ALOGE("%s: got releaseFence without output buffer!", __FUNCTION__);
1423 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001424 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1425 } else {
1426 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1427 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001428 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001429 }
1430 }
1431 r.num_output_buffers = outputBuffers.size();
1432 r.output_buffers = outputBuffers.data();
1433
1434 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001435 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001436 r.input_buffer = nullptr;
1437 } else {
1438 if (mInputStream->getId() != result.inputBuffer.streamId) {
1439 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1440 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001441 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001442 }
1443 inputBuffer.stream = mInputStream->asHalStream();
1444 buffer_handle_t *buffer;
1445 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1446 &buffer);
1447 if (res != OK) {
1448 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1449 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001450 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001451 }
1452 inputBuffer.buffer = buffer;
1453 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1454 inputBuffer.acquire_fence = -1;
1455 if (result.inputBuffer.releaseFence == nullptr) {
1456 inputBuffer.release_fence = -1;
1457 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1458 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1459 } else {
1460 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1461 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001462 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001463 }
1464 r.input_buffer = &inputBuffer;
1465 }
1466
1467 r.partial_result = result.partialResult;
1468
1469 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001470}
1471
1472hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001473 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001474 // Ideally we should grab mLock, but that can lead to deadlock, and
1475 // it's not super important to get up to date value of mStatus for this
1476 // warning print, hence skipping the lock here
1477 if (mStatus == STATUS_ERROR) {
1478 // Per API contract, HAL should act as closed after device error
1479 // But mStatus can be set to error by framework as well, so just log
1480 // a warning here.
1481 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001482 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001483
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001484 for (const auto& msg : msgs) {
1485 notify(msg);
1486 }
1487 return hardware::Void();
1488}
1489
1490void Camera3Device::notify(
1491 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1492
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001493 camera3_notify_msg m;
1494 switch (msg.type) {
1495 case MsgType::ERROR:
1496 m.type = CAMERA3_MSG_ERROR;
1497 m.message.error.frame_number = msg.msg.error.frameNumber;
1498 if (msg.msg.error.errorStreamId >= 0) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001499 sp<Camera3StreamInterface> stream = mOutputStreams.get(msg.msg.error.errorStreamId);
1500 if (stream == nullptr) {
1501 ALOGE("%s: Frame %d: Invalid error stream id %d", __FUNCTION__,
1502 m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001503 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001504 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001505 m.message.error.error_stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001506 } else {
1507 m.message.error.error_stream = nullptr;
1508 }
1509 switch (msg.msg.error.errorCode) {
1510 case ErrorCode::ERROR_DEVICE:
1511 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1512 break;
1513 case ErrorCode::ERROR_REQUEST:
1514 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1515 break;
1516 case ErrorCode::ERROR_RESULT:
1517 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1518 break;
1519 case ErrorCode::ERROR_BUFFER:
1520 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1521 break;
1522 }
1523 break;
1524 case MsgType::SHUTTER:
1525 m.type = CAMERA3_MSG_SHUTTER;
1526 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1527 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1528 break;
1529 }
1530 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001531}
1532
Emilian Peevaebbe412018-01-15 13:53:24 +00001533status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001534 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001535 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001536 ATRACE_CALL();
1537
Emilian Peevaebbe412018-01-15 13:53:24 +00001538 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001539}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001540
Jianing Weicb0652e2014-03-12 18:29:36 -07001541status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1542 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001543 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544
Emilian Peevaebbe412018-01-15 13:53:24 +00001545 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001546 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001547 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001548
Emilian Peevaebbe412018-01-15 13:53:24 +00001549 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001550 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551}
1552
Emilian Peevaebbe412018-01-15 13:53:24 +00001553status_t Camera3Device::setStreamingRequestList(
1554 const List<const PhysicalCameraSettingsList> &requestsList,
1555 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001556 ATRACE_CALL();
1557
Emilian Peevaebbe412018-01-15 13:53:24 +00001558 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001559}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001560
1561sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001562 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001563 status_t res;
1564
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001566 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1567 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001568 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1569 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001570 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001572 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001574 } else if (mStatus == STATUS_UNCONFIGURED) {
1575 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576 CLOGE("No streams configured");
1577 return NULL;
1578 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579 }
1580
Shuzhen Wang0129d522016-10-30 22:43:41 -07001581 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583}
1584
Jianing Weicb0652e2014-03-12 18:29:36 -07001585status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001586 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590 switch (mStatus) {
1591 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001592 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001593 return INVALID_OPERATION;
1594 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001595 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001596 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001597 case STATUS_UNCONFIGURED:
1598 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001599 case STATUS_ACTIVE:
1600 // OK
1601 break;
1602 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001603 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001604 return INVALID_OPERATION;
1605 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001606 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001607
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001608 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001609}
1610
1611status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1612 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001613 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001614
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001615 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001616}
1617
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001618status_t Camera3Device::createInputStream(
1619 uint32_t width, uint32_t height, int format, int *id) {
1620 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001621 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001622 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001623 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001624 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1625 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001626
1627 status_t res;
1628 bool wasActive = false;
1629
1630 switch (mStatus) {
1631 case STATUS_ERROR:
1632 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1633 return INVALID_OPERATION;
1634 case STATUS_UNINITIALIZED:
1635 ALOGE("%s: Device not initialized", __FUNCTION__);
1636 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001637 case STATUS_UNCONFIGURED:
1638 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001639 // OK
1640 break;
1641 case STATUS_ACTIVE:
1642 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001643 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001644 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001645 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001646 return res;
1647 }
1648 wasActive = true;
1649 break;
1650 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001651 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001652 return INVALID_OPERATION;
1653 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001654 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001655
1656 if (mInputStream != 0) {
1657 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1658 return INVALID_OPERATION;
1659 }
1660
1661 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1662 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001663 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001664
1665 mInputStream = newStream;
1666
1667 *id = mNextStreamId++;
1668
1669 // Continue captures if active at start
1670 if (wasActive) {
1671 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001672 // Reuse current operating mode and session parameters for new stream config
1673 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001674 if (res != OK) {
1675 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1676 __FUNCTION__, mNextStreamId, strerror(-res), res);
1677 return res;
1678 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001679 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001680 }
1681
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001682 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001683 return OK;
1684}
1685
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001686status_t Camera3Device::StreamSet::add(
1687 int streamId, sp<camera3::Camera3OutputStreamInterface> stream) {
1688 if (stream == nullptr) {
1689 ALOGE("%s: cannot add null stream", __FUNCTION__);
1690 return BAD_VALUE;
1691 }
1692 std::lock_guard<std::mutex> lock(mLock);
1693 return mData.add(streamId, stream);
1694}
1695
1696ssize_t Camera3Device::StreamSet::remove(int streamId) {
1697 std::lock_guard<std::mutex> lock(mLock);
1698 return mData.removeItem(streamId);
1699}
1700
1701sp<camera3::Camera3OutputStreamInterface>
1702Camera3Device::StreamSet::get(int streamId) {
1703 std::lock_guard<std::mutex> lock(mLock);
1704 ssize_t idx = mData.indexOfKey(streamId);
1705 if (idx == NAME_NOT_FOUND) {
1706 return nullptr;
1707 }
1708 return mData.editValueAt(idx);
1709}
1710
1711sp<camera3::Camera3OutputStreamInterface>
1712Camera3Device::StreamSet::operator[] (size_t index) {
1713 std::lock_guard<std::mutex> lock(mLock);
1714 return mData.editValueAt(index);
1715}
1716
1717size_t Camera3Device::StreamSet::size() const {
1718 std::lock_guard<std::mutex> lock(mLock);
1719 return mData.size();
1720}
1721
1722void Camera3Device::StreamSet::clear() {
1723 std::lock_guard<std::mutex> lock(mLock);
1724 return mData.clear();
1725}
1726
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07001727std::vector<int> Camera3Device::StreamSet::getStreamIds() {
1728 std::lock_guard<std::mutex> lock(mLock);
1729 std::vector<int> streamIds(mData.size());
1730 for (size_t i = 0; i < mData.size(); i++) {
1731 streamIds[i] = mData.keyAt(i);
1732 }
1733 return streamIds;
1734}
1735
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001736status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001737 uint32_t width, uint32_t height, int format,
1738 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001739 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001740 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001741 ATRACE_CALL();
1742
1743 if (consumer == nullptr) {
1744 ALOGE("%s: consumer must not be null", __FUNCTION__);
1745 return BAD_VALUE;
1746 }
1747
1748 std::vector<sp<Surface>> consumers;
1749 consumers.push_back(consumer);
1750
1751 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001752 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1753 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001754}
1755
1756status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1757 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1758 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001759 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001760 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001761 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001762
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001763 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001764 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001765 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001766 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001767 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1768 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1769 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001770
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001771 status_t res;
1772 bool wasActive = false;
1773
1774 switch (mStatus) {
1775 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001776 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001777 return INVALID_OPERATION;
1778 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001779 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001780 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001781 case STATUS_UNCONFIGURED:
1782 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001783 // OK
1784 break;
1785 case STATUS_ACTIVE:
1786 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001787 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001789 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790 return res;
1791 }
1792 wasActive = true;
1793 break;
1794 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001795 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001796 return INVALID_OPERATION;
1797 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001798 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001799
1800 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001801
Shuzhen Wang0129d522016-10-30 22:43:41 -07001802 if (consumers.size() == 0 && !hasDeferredConsumer) {
1803 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1804 return BAD_VALUE;
1805 }
Zhijun He5d677d12016-05-29 16:52:39 -07001806
Shuzhen Wang0129d522016-10-30 22:43:41 -07001807 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001808 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1809 return BAD_VALUE;
1810 }
1811
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001812 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001813 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001814 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001815 blobBufferSize = getPointCloudBufferSize();
1816 if (blobBufferSize <= 0) {
1817 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1818 return BAD_VALUE;
1819 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001820 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1821 blobBufferSize = width * height;
1822 } else {
1823 blobBufferSize = getJpegBufferSize(width, height);
1824 if (blobBufferSize <= 0) {
1825 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1826 return BAD_VALUE;
1827 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001828 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001829 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001830 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001831 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001832 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1833 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1834 if (rawOpaqueBufferSize <= 0) {
1835 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1836 return BAD_VALUE;
1837 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001838 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001839 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001840 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001841 } else if (isShared) {
1842 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1843 width, height, format, consumerUsage, dataSpace, rotation,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001844 mTimestampOffset, physicalCameraId, streamSetId,
1845 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001846 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001847 newStream = new Camera3OutputStream(mNextStreamId,
1848 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001849 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001850 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001851 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001852 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001853 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854 }
Emilian Peev40ead602017-09-26 15:46:36 +01001855
1856 size_t consumerCount = consumers.size();
1857 for (size_t i = 0; i < consumerCount; i++) {
1858 int id = newStream->getSurfaceId(consumers[i]);
1859 if (id < 0) {
1860 SET_ERR_L("Invalid surface id");
1861 return BAD_VALUE;
1862 }
1863 if (surfaceIds != nullptr) {
1864 surfaceIds->push_back(id);
1865 }
1866 }
1867
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001868 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869
Emilian Peev08dd2452017-04-06 16:55:14 +01001870 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001871
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001872 res = mOutputStreams.add(mNextStreamId, newStream);
1873 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001874 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001875 return res;
1876 }
1877
1878 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001879 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001880
1881 // Continue captures if active at start
1882 if (wasActive) {
1883 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001884 // Reuse current operating mode and session parameters for new stream config
1885 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001886 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001887 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1888 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001889 return res;
1890 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001891 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001892 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001893 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001894 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001895}
1896
Emilian Peev710c1422017-08-30 11:19:38 +01001897status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001898 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001899 if (nullptr == streamInfo) {
1900 return BAD_VALUE;
1901 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001902 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001903 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001904
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001905 switch (mStatus) {
1906 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001907 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001908 return INVALID_OPERATION;
1909 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001910 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001911 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001912 case STATUS_UNCONFIGURED:
1913 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001914 case STATUS_ACTIVE:
1915 // OK
1916 break;
1917 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001918 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919 return INVALID_OPERATION;
1920 }
1921
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001922 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1923 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001924 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001925 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001926 }
1927
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001928 streamInfo->width = stream->getWidth();
1929 streamInfo->height = stream->getHeight();
1930 streamInfo->format = stream->getFormat();
1931 streamInfo->dataSpace = stream->getDataSpace();
1932 streamInfo->formatOverridden = stream->isFormatOverridden();
1933 streamInfo->originalFormat = stream->getOriginalFormat();
1934 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1935 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001936 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001937}
1938
1939status_t Camera3Device::setStreamTransform(int id,
1940 int transform) {
1941 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001942 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001943 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001944
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001945 switch (mStatus) {
1946 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001947 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001948 return INVALID_OPERATION;
1949 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001950 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001951 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 case STATUS_UNCONFIGURED:
1953 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954 case STATUS_ACTIVE:
1955 // OK
1956 break;
1957 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001958 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001959 return INVALID_OPERATION;
1960 }
1961
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001962 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1963 if (stream == nullptr) {
1964 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965 return BAD_VALUE;
1966 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001967 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001968}
1969
1970status_t Camera3Device::deleteStream(int id) {
1971 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001972 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001973 Mutex::Autolock l(mLock);
1974 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001975
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001976 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001977
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001978 // CameraDevice semantics require device to already be idle before
1979 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001980 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001981 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001982 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001983 }
1984
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001985 if (mStatus == STATUS_ERROR) {
1986 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1987 __FUNCTION__, mId.string());
1988 return -EBUSY;
1989 }
1990
Igor Murashkin2fba5842013-04-22 14:03:54 -07001991 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001992 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001993 if (mInputStream != NULL && id == mInputStream->getId()) {
1994 deletedStream = mInputStream;
1995 mInputStream.clear();
1996 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001997 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001998 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001999 return BAD_VALUE;
2000 }
Zhijun He5f446352014-01-22 09:49:33 -08002001 }
2002
2003 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002004 if (stream != nullptr) {
2005 deletedStream = stream;
2006 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002007 }
2008
2009 // Free up the stream endpoint so that it can be used by some other stream
2010 res = deletedStream->disconnect();
2011 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002012 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 // fall through since we want to still list the stream as deleted.
2014 }
2015 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002016 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002017
2018 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002019}
2020
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002021status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07002022 ATRACE_CALL();
2023 ALOGV("%s: E", __FUNCTION__);
2024
2025 Mutex::Autolock il(mInterfaceLock);
2026 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07002027
Emilian Peev811d2952018-05-25 11:08:40 +01002028 // In case the client doesn't include any session parameter, try a
2029 // speculative configuration using the values from the last cached
2030 // default request.
2031 if (sessionParams.isEmpty() &&
2032 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
2033 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
2034 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
2035 mLastTemplateId);
2036 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
2037 operatingMode);
2038 }
2039
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002040 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
2041}
2042
2043status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
2044 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002045 //Filter out any incoming session parameters
2046 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002047 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
2048 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002049 CameraMetadata filteredParams(availableSessionKeys.count);
2050 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2051 filteredParams.getAndLock());
2052 set_camera_metadata_vendor_id(meta, mVendorTagId);
2053 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002054 if (availableSessionKeys.count > 0) {
2055 for (size_t i = 0; i < availableSessionKeys.count; i++) {
2056 camera_metadata_ro_entry entry = params.find(
2057 availableSessionKeys.data.i32[i]);
2058 if (entry.count > 0) {
2059 filteredParams.update(entry);
2060 }
2061 }
2062 }
2063
2064 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07002065}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002066
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002067status_t Camera3Device::getInputBufferProducer(
2068 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002069 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002070 Mutex::Autolock il(mInterfaceLock);
2071 Mutex::Autolock l(mLock);
2072
2073 if (producer == NULL) {
2074 return BAD_VALUE;
2075 } else if (mInputStream == NULL) {
2076 return INVALID_OPERATION;
2077 }
2078
2079 return mInputStream->getInputBufferProducer(producer);
2080}
2081
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002082status_t Camera3Device::createDefaultRequest(int templateId,
2083 CameraMetadata *request) {
2084 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07002085 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002086
2087 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
2088 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07002089 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002090 return BAD_VALUE;
2091 }
2092
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002093 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002094
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002095 {
2096 Mutex::Autolock l(mLock);
2097 switch (mStatus) {
2098 case STATUS_ERROR:
2099 CLOGE("Device has encountered a serious error");
2100 return INVALID_OPERATION;
2101 case STATUS_UNINITIALIZED:
2102 CLOGE("Device is not initialized!");
2103 return INVALID_OPERATION;
2104 case STATUS_UNCONFIGURED:
2105 case STATUS_CONFIGURED:
2106 case STATUS_ACTIVE:
2107 // OK
2108 break;
2109 default:
2110 SET_ERR_L("Unexpected status: %d", mStatus);
2111 return INVALID_OPERATION;
2112 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002113
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002114 if (!mRequestTemplateCache[templateId].isEmpty()) {
2115 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002116 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002117 return OK;
2118 }
Zhijun Hea1530f12014-09-14 12:44:20 -07002119 }
2120
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002121 camera_metadata_t *rawRequest;
2122 status_t res = mInterface->constructDefaultRequestSettings(
2123 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002124
2125 {
2126 Mutex::Autolock l(mLock);
2127 if (res == BAD_VALUE) {
2128 ALOGI("%s: template %d is not supported on this camera device",
2129 __FUNCTION__, templateId);
2130 return res;
2131 } else if (res != OK) {
2132 CLOGE("Unable to construct request template %d: %s (%d)",
2133 templateId, strerror(-res), res);
2134 return res;
2135 }
2136
2137 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
2138 mRequestTemplateCache[templateId].acquire(rawRequest);
2139
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08002140 // Override the template request with zoomRatioMapper
2141 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
2142 &mRequestTemplateCache[templateId]);
2143 if (res != OK) {
2144 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
2145 templateId, strerror(-res), res);
2146 return res;
2147 }
2148
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002149 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002150 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002151 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002152 return OK;
2153}
2154
2155status_t Camera3Device::waitUntilDrained() {
2156 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002157 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002158 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002159 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002160
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002161 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07002162}
2163
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002164status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002165 switch (mStatus) {
2166 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002167 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002168 ALOGV("%s: Already idle", __FUNCTION__);
2169 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002170 case STATUS_CONFIGURED:
2171 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002172 case STATUS_ERROR:
2173 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002174 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002175 break;
2176 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002177 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002178 return INVALID_OPERATION;
2179 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002180 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
2181 maxExpectedDuration);
2182 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07002183 if (res != OK) {
2184 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
2185 res);
2186 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002187 return res;
2188}
2189
Ruben Brunk183f0562015-08-12 12:55:02 -07002190
2191void Camera3Device::internalUpdateStatusLocked(Status status) {
2192 mStatus = status;
2193 mRecentStatusUpdates.add(mStatus);
2194 mStatusChanged.broadcast();
2195}
2196
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002197void Camera3Device::pauseStateNotify(bool enable) {
2198 Mutex::Autolock il(mInterfaceLock);
2199 Mutex::Autolock l(mLock);
2200
2201 mPauseStateNotify = enable;
2202}
2203
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002204// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002205status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08002206 if (mRequestThread.get() != nullptr) {
2207 mRequestThread->setPaused(true);
2208 } else {
2209 return NO_INIT;
2210 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002211
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002212 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2213 maxExpectedDuration);
2214 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002215 if (res != OK) {
2216 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002217 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002218 }
2219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002220 return res;
2221}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002222
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002223// Resume after internalPauseAndWaitLocked
2224status_t Camera3Device::internalResumeLocked() {
2225 status_t res;
2226
2227 mRequestThread->setPaused(false);
2228
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002229 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2230 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002231 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
2232 if (res != OK) {
2233 SET_ERR_L("Can't transition to active in %f seconds!",
2234 kActiveTimeout/1e9);
2235 }
2236 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002237 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002238}
2239
Ruben Brunk183f0562015-08-12 12:55:02 -07002240status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002241 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07002242
2243 size_t startIndex = 0;
2244 if (mStatusWaiters == 0) {
2245 // Clear the list of recent statuses if there are no existing threads waiting on updates to
2246 // this status list
2247 mRecentStatusUpdates.clear();
2248 } else {
2249 // If other threads are waiting on updates to this status list, set the position of the
2250 // first element that this list will check rather than clearing the list.
2251 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002252 }
2253
Ruben Brunk183f0562015-08-12 12:55:02 -07002254 mStatusWaiters++;
2255
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002256 if (!active && mUseHalBufManager) {
2257 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08002258 if (mStatus == STATUS_ACTIVE) {
2259 mRequestThread->signalPipelineDrain(streamIds);
2260 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002261 mRequestBufferSM.onWaitUntilIdle();
2262 }
2263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002264 bool stateSeen = false;
2265 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07002266 if (active == (mStatus == STATUS_ACTIVE)) {
2267 // Desired state is current
2268 break;
2269 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002270
2271 res = mStatusChanged.waitRelative(mLock, timeout);
2272 if (res != OK) break;
2273
Ruben Brunk183f0562015-08-12 12:55:02 -07002274 // This is impossible, but if not, could result in subtle deadlocks and invalid state
2275 // transitions.
2276 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
2277 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
2278 __FUNCTION__);
2279
2280 // Encountered desired state since we began waiting
2281 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002282 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
2283 stateSeen = true;
2284 break;
2285 }
2286 }
2287 } while (!stateSeen);
2288
Ruben Brunk183f0562015-08-12 12:55:02 -07002289 mStatusWaiters--;
2290
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002291 return res;
2292}
2293
2294
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002295status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002296 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002297 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002298
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002299 if (listener != NULL && mListener != NULL) {
2300 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
2301 }
2302 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002303 mRequestThread->setNotificationListener(listener);
2304 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002305
2306 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002307}
2308
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002309bool Camera3Device::willNotify3A() {
2310 return false;
2311}
2312
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002313status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002314 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002315 status_t res;
2316 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002317
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002318 while (mResultQueue.empty()) {
2319 res = mResultSignal.waitRelative(mOutputLock, timeout);
2320 if (res == TIMED_OUT) {
2321 return res;
2322 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002323 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
2324 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002325 return res;
2326 }
2327 }
2328 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002329}
2330
Jianing Weicb0652e2014-03-12 18:29:36 -07002331status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002332 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002333 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002334
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002335 if (mResultQueue.empty()) {
2336 return NOT_ENOUGH_DATA;
2337 }
2338
Jianing Weicb0652e2014-03-12 18:29:36 -07002339 if (frame == NULL) {
2340 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
2341 return BAD_VALUE;
2342 }
2343
2344 CaptureResult &result = *(mResultQueue.begin());
2345 frame->mResultExtras = result.mResultExtras;
2346 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002347 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002348 mResultQueue.erase(mResultQueue.begin());
2349
2350 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002351}
2352
2353status_t Camera3Device::triggerAutofocus(uint32_t id) {
2354 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002355 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002356
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002357 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
2358 // Mix-in this trigger into the next request and only the next request.
2359 RequestTrigger trigger[] = {
2360 {
2361 ANDROID_CONTROL_AF_TRIGGER,
2362 ANDROID_CONTROL_AF_TRIGGER_START
2363 },
2364 {
2365 ANDROID_CONTROL_AF_TRIGGER_ID,
2366 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002367 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002368 };
2369
2370 return mRequestThread->queueTrigger(trigger,
2371 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002372}
2373
2374status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2375 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002376 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002377
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002378 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2379 // Mix-in this trigger into the next request and only the next request.
2380 RequestTrigger trigger[] = {
2381 {
2382 ANDROID_CONTROL_AF_TRIGGER,
2383 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2384 },
2385 {
2386 ANDROID_CONTROL_AF_TRIGGER_ID,
2387 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002388 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002389 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002390
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002391 return mRequestThread->queueTrigger(trigger,
2392 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002393}
2394
2395status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2396 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002397 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002398
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002399 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2400 // Mix-in this trigger into the next request and only the next request.
2401 RequestTrigger trigger[] = {
2402 {
2403 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2404 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2405 },
2406 {
2407 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2408 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002409 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002410 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002411
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002412 return mRequestThread->queueTrigger(trigger,
2413 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002414}
2415
Jianing Weicb0652e2014-03-12 18:29:36 -07002416status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002417 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002418 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002419 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002420
Zhijun He7ef20392014-04-21 16:04:17 -07002421 {
2422 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002423
2424 // b/116514106 "disconnect()" can get called twice for the same device. The
2425 // camera device will not be initialized during the second run.
2426 if (mStatus == STATUS_UNINITIALIZED) {
2427 return OK;
2428 }
2429
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002430 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07002431 }
2432
Emilian Peev08dd2452017-04-06 16:55:14 +01002433 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002434}
2435
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002436status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002437 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2438}
2439
2440status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002441 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002442 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002443 Mutex::Autolock il(mInterfaceLock);
2444 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002445
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002446 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2447 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002448 CLOGE("Stream %d does not exist", streamId);
2449 return BAD_VALUE;
2450 }
2451
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002452 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002453 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002454 return BAD_VALUE;
2455 }
2456
2457 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002458 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002459 return BAD_VALUE;
2460 }
2461
Ruben Brunkc78ac262015-08-13 17:58:46 -07002462 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002463}
2464
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002465status_t Camera3Device::tearDown(int streamId) {
2466 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002467 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002468 Mutex::Autolock il(mInterfaceLock);
2469 Mutex::Autolock l(mLock);
2470
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002471 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2472 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002473 CLOGE("Stream %d does not exist", streamId);
2474 return BAD_VALUE;
2475 }
2476
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002477 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2478 CLOGE("Stream %d is a target of a in-progress request", streamId);
2479 return BAD_VALUE;
2480 }
2481
2482 return stream->tearDown();
2483}
2484
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002485status_t Camera3Device::addBufferListenerForStream(int streamId,
2486 wp<Camera3StreamBufferListener> listener) {
2487 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002488 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002489 Mutex::Autolock il(mInterfaceLock);
2490 Mutex::Autolock l(mLock);
2491
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002492 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2493 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002494 CLOGE("Stream %d does not exist", streamId);
2495 return BAD_VALUE;
2496 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002497 stream->addBufferListener(listener);
2498
2499 return OK;
2500}
2501
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002502/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002503 * Methods called by subclasses
2504 */
2505
2506void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002507 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002508 {
2509 // Need mLock to safely update state and synchronize to current
2510 // state of methods in flight.
2511 Mutex::Autolock l(mLock);
2512 // We can get various system-idle notices from the status tracker
2513 // while starting up. Only care about them if we've actually sent
2514 // in some requests recently.
2515 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2516 return;
2517 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002518 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2519 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002520 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002521
2522 // Skip notifying listener if we're doing some user-transparent
2523 // state changes
2524 if (mPauseStateNotify) return;
2525 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002526
2527 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002528 {
2529 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002530 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002531 }
2532 if (idle && listener != NULL) {
2533 listener->notifyIdle();
2534 }
2535}
2536
Shuzhen Wang758c2152017-01-10 18:26:18 -08002537status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002538 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002539 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002540 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2541 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002542
2543 if (surfaceIds == nullptr) {
2544 return BAD_VALUE;
2545 }
2546
Zhijun He5d677d12016-05-29 16:52:39 -07002547 Mutex::Autolock il(mInterfaceLock);
2548 Mutex::Autolock l(mLock);
2549
Shuzhen Wang758c2152017-01-10 18:26:18 -08002550 if (consumers.size() == 0) {
2551 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002552 return BAD_VALUE;
2553 }
2554
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002555 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2556 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002557 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002558 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002559 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002560
2561 // isConsumerConfigurationDeferred will be off after setConsumers
2562 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002563 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002564 if (res != OK) {
2565 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2566 return res;
2567 }
2568
Emilian Peev40ead602017-09-26 15:46:36 +01002569 for (auto &consumer : consumers) {
2570 int id = stream->getSurfaceId(consumer);
2571 if (id < 0) {
2572 CLOGE("Invalid surface id!");
2573 return BAD_VALUE;
2574 }
2575 surfaceIds->push_back(id);
2576 }
2577
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002578 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002579 if (!stream->isConfiguring()) {
2580 CLOGE("Stream %d was already fully configured.", streamId);
2581 return INVALID_OPERATION;
2582 }
Zhijun He5d677d12016-05-29 16:52:39 -07002583
Shuzhen Wang0129d522016-10-30 22:43:41 -07002584 res = stream->finishConfiguration();
2585 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002586 // If finishConfiguration fails due to abandoned surface, do not set
2587 // device to error state.
2588 bool isSurfaceAbandoned =
2589 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2590 if (!isSurfaceAbandoned) {
2591 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2592 stream->getId(), strerror(-res), res);
2593 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002594 return res;
2595 }
Zhijun He5d677d12016-05-29 16:52:39 -07002596 }
2597
2598 return OK;
2599}
2600
Emilian Peev40ead602017-09-26 15:46:36 +01002601status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2602 const std::vector<OutputStreamInfo> &outputInfo,
2603 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2604 Mutex::Autolock il(mInterfaceLock);
2605 Mutex::Autolock l(mLock);
2606
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002607 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2608 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002609 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002610 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002611 }
2612
2613 for (const auto &it : removedSurfaceIds) {
2614 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2615 CLOGE("Shared surface still part of a pending request!");
2616 return -EBUSY;
2617 }
2618 }
2619
Emilian Peev40ead602017-09-26 15:46:36 +01002620 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2621 if (res != OK) {
2622 CLOGE("Stream %d failed to update stream (error %d %s) ",
2623 streamId, res, strerror(-res));
2624 if (res == UNKNOWN_ERROR) {
2625 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2626 __FUNCTION__);
2627 }
2628 return res;
2629 }
2630
2631 return res;
2632}
2633
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002634status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2635 Mutex::Autolock il(mInterfaceLock);
2636 Mutex::Autolock l(mLock);
2637
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002638 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2639 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002640 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2641 return BAD_VALUE;
2642 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002643 return stream->dropBuffers(dropping);
2644}
2645
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002646/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002647 * Camera3Device private methods
2648 */
2649
2650sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002651 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002652 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002653
2654 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002655 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002656
2657 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002658 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002659 if (inputStreams.count > 0) {
2660 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002661 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002662 CLOGE("Request references unknown input stream %d",
2663 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002664 return NULL;
2665 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002666
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002667 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002668 SET_ERR_L("%s: input stream %d is not configured!",
2669 __FUNCTION__, mInputStream->getId());
2670 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002671 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002672 // Check if stream prepare is blocking requests.
2673 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002674 CLOGE("Request references an input stream that's being prepared!");
2675 return NULL;
2676 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002677
2678 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002679 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 }
2681
2682 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002683 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002685 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002686 return NULL;
2687 }
2688
2689 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002690 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2691 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002692 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002693 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002694 return NULL;
2695 }
Zhijun He5d677d12016-05-29 16:52:39 -07002696 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002697 auto iter = surfaceMap.find(streams.data.i32[i]);
2698 if (iter != surfaceMap.end()) {
2699 const std::vector<size_t>& surfaces = iter->second;
2700 for (const auto& surface : surfaces) {
2701 if (stream->isConsumerConfigurationDeferred(surface)) {
2702 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2703 "due to deferred consumer", stream->getId(), surface);
2704 return NULL;
2705 }
2706 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002707 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002708 }
2709
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002711 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2712 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002713 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002714 // Check if stream prepare is blocking requests.
2715 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002716 CLOGE("Request references an output stream that's being prepared!");
2717 return NULL;
2718 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002719
2720 newRequest->mOutputStreams.push(stream);
2721 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002722 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002723 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002724
2725 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002726}
2727
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002728bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2729 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2730 Size size = mSupportedOpaqueInputSizes[i];
2731 if (size.width == width && size.height == height) {
2732 return true;
2733 }
2734 }
2735
2736 return false;
2737}
2738
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002739void Camera3Device::cancelStreamsConfigurationLocked() {
2740 int res = OK;
2741 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2742 res = mInputStream->cancelConfiguration();
2743 if (res != OK) {
2744 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2745 mInputStream->getId(), strerror(-res), res);
2746 }
2747 }
2748
2749 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002750 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002751 if (outputStream->isConfiguring()) {
2752 res = outputStream->cancelConfiguration();
2753 if (res != OK) {
2754 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2755 outputStream->getId(), strerror(-res), res);
2756 }
2757 }
2758 }
2759
2760 // Return state to that at start of call, so that future configures
2761 // properly clean things up
2762 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2763 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002764
2765 res = mPreparerThread->resume();
2766 if (res != OK) {
2767 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2768 }
2769}
2770
2771bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2772 ATRACE_CALL();
2773 bool ret = false;
2774
2775 Mutex::Autolock il(mInterfaceLock);
2776 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2777
2778 Mutex::Autolock l(mLock);
2779 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2780 if (rc == NO_ERROR) {
2781 mNeedConfig = true;
2782 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2783 if (rc == NO_ERROR) {
2784 ret = true;
2785 mPauseStateNotify = false;
2786 //Moving to active state while holding 'mLock' is important.
2787 //There could be pending calls to 'create-/deleteStream' which
2788 //will trigger another stream configuration while the already
2789 //present streams end up with outstanding buffers that will
2790 //not get drained.
2791 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002792 } else if (rc == DEAD_OBJECT) {
2793 // DEAD_OBJECT can be returned if either the consumer surface is
2794 // abandoned, or the HAL has died.
2795 // - If the HAL has died, configureStreamsLocked call will set
2796 // device to error state,
2797 // - If surface is abandoned, we should not set device to error
2798 // state.
2799 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002800 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002801 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002802 }
2803 } else {
2804 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2805 }
2806
2807 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002808}
2809
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002810status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002811 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002812 ATRACE_CALL();
2813 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002815 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002816 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002817 return INVALID_OPERATION;
2818 }
2819
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002820 if (operatingMode < 0) {
2821 CLOGE("Invalid operating mode: %d", operatingMode);
2822 return BAD_VALUE;
2823 }
2824
2825 bool isConstrainedHighSpeed =
2826 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2827 operatingMode;
2828
2829 if (mOperatingMode != operatingMode) {
2830 mNeedConfig = true;
2831 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2832 mOperatingMode = operatingMode;
2833 }
2834
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002835 // In case called from configureStreams, abort queued input buffers not belonging to
2836 // any pending requests.
2837 if (mInputStream != NULL && notifyRequestThread) {
2838 while (true) {
2839 camera3_stream_buffer_t inputBuffer;
2840 status_t res = mInputStream->getInputBuffer(&inputBuffer,
2841 /*respectHalLimit*/ false);
2842 if (res != OK) {
2843 // Exhausted acquiring all input buffers.
2844 break;
2845 }
2846
2847 inputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2848 res = mInputStream->returnInputBuffer(inputBuffer);
2849 if (res != OK) {
2850 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2851 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2852 }
2853 }
2854 }
2855
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002856 if (!mNeedConfig) {
2857 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2858 return OK;
2859 }
2860
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002861 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2862 // adding a dummy stream instead.
2863 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2864 if (mOutputStreams.size() == 0) {
2865 addDummyStreamLocked();
2866 } else {
2867 tryRemoveDummyStreamLocked();
2868 }
2869
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002870 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002871 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002872
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002873 mPreparerThread->pause();
2874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002875 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002876 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002877 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2878
2879 Vector<camera3_stream_t*> streams;
2880 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002881 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002882
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002883
2884 if (mInputStream != NULL) {
2885 camera3_stream_t *inputStream;
2886 inputStream = mInputStream->startConfiguration();
2887 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002888 CLOGE("Can't start input stream configuration");
2889 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890 return INVALID_OPERATION;
2891 }
2892 streams.add(inputStream);
2893 }
2894
2895 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002896
2897 // Don't configure bidi streams twice, nor add them twice to the list
2898 if (mOutputStreams[i].get() ==
2899 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2900
2901 config.num_streams--;
2902 continue;
2903 }
2904
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002905 camera3_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002906 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002907 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002908 CLOGE("Can't start output stream configuration");
2909 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002910 return INVALID_OPERATION;
2911 }
2912 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002913
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002914 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002915 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2916 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002917 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2918 bufferSizes[k] = static_cast<uint32_t>(
2919 getJpegBufferSize(outputStream->width, outputStream->height));
2920 } else if (outputStream->data_space ==
2921 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2922 bufferSizes[k] = outputStream->width * outputStream->height;
2923 } else {
2924 ALOGW("%s: Blob dataSpace %d not supported",
2925 __FUNCTION__, outputStream->data_space);
2926 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002927 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 }
2929
2930 config.streams = streams.editArray();
2931
2932 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002933 // max_buffers, usage, and priv fields, as well as data_space and format
2934 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002935
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002936 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002937 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002938 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002939
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002940 if (res == BAD_VALUE) {
2941 // HAL rejected this set of streams as unsupported, clean up config
2942 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002943 CLOGE("Set of requested inputs/outputs not supported by HAL");
2944 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002945 return BAD_VALUE;
2946 } else if (res != OK) {
2947 // Some other kind of error from configure_streams - this is not
2948 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002949 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2950 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002951 return res;
2952 }
2953
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002954 // Finish all stream configuration immediately.
2955 // TODO: Try to relax this later back to lazy completion, which should be
2956 // faster
2957
Igor Murashkin073f8572013-05-02 14:59:28 -07002958 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002959 bool streamReConfigured = false;
2960 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002961 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002962 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002963 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002964 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002965 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2966 return DEAD_OBJECT;
2967 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002968 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002969 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002970 if (streamReConfigured) {
2971 mInterface->onStreamReConfigured(mInputStream->getId());
2972 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002973 }
2974
2975 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002976 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002977 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002978 bool streamReConfigured = false;
2979 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002980 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002981 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002982 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002983 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002984 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2985 return DEAD_OBJECT;
2986 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002987 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002988 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002989 if (streamReConfigured) {
2990 mInterface->onStreamReConfigured(outputStream->getId());
2991 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002992 }
2993 }
2994
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002995 // Request thread needs to know to avoid using repeat-last-settings protocol
2996 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002997 if (notifyRequestThread) {
2998 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2999 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003000
Zhijun He90f7c372016-08-16 16:19:43 -07003001 char value[PROPERTY_VALUE_MAX];
3002 property_get("camera.fifo.disable", value, "0");
3003 int32_t disableFifo = atoi(value);
3004 if (disableFifo != 1) {
3005 // Boost priority of request thread to SCHED_FIFO.
3006 pid_t requestThreadTid = mRequestThread->getTid();
3007 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08003008 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07003009 if (res != OK) {
3010 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
3011 strerror(-res), res);
3012 } else {
3013 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
3014 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07003015 }
3016
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003017 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003018 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
3019 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
3020 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
3021 sessionParams.unlock(newSessionParams);
3022 mSessionParams.unlock(currentSessionParams);
3023 if (updateSessionParams) {
3024 mSessionParams = sessionParams;
3025 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003026
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07003027 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003028
Ruben Brunk183f0562015-08-12 12:55:02 -07003029 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
3030 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003031
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003032 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003033
Zhijun He0a210512014-07-24 13:45:15 -07003034 // tear down the deleted streams after configure streams.
3035 mDeletedStreams.clear();
3036
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003037 auto rc = mPreparerThread->resume();
3038 if (rc != OK) {
3039 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
3040 return rc;
3041 }
3042
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003043 if (mDummyStreamId == NO_STREAM) {
3044 mRequestBufferSM.onStreamsConfigured();
3045 }
3046
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003047 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003048}
3049
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003050status_t Camera3Device::addDummyStreamLocked() {
3051 ATRACE_CALL();
3052 status_t res;
3053
3054 if (mDummyStreamId != NO_STREAM) {
3055 // Should never be adding a second dummy stream when one is already
3056 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003057 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
3058 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003059 return INVALID_OPERATION;
3060 }
3061
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003062 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003063
3064 sp<Camera3OutputStreamInterface> dummyStream =
3065 new Camera3DummyStream(mNextStreamId);
3066
3067 res = mOutputStreams.add(mNextStreamId, dummyStream);
3068 if (res < 0) {
3069 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
3070 return res;
3071 }
3072
3073 mDummyStreamId = mNextStreamId;
3074 mNextStreamId++;
3075
3076 return OK;
3077}
3078
3079status_t Camera3Device::tryRemoveDummyStreamLocked() {
3080 ATRACE_CALL();
3081 status_t res;
3082
3083 if (mDummyStreamId == NO_STREAM) return OK;
3084 if (mOutputStreams.size() == 1) return OK;
3085
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003086 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003087
3088 // Ok, have a dummy stream and there's at least one other output stream,
3089 // so remove the dummy
3090
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003091 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mDummyStreamId);
3092 if (deletedStream == nullptr) {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003093 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
3094 return INVALID_OPERATION;
3095 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003096 mOutputStreams.remove(mDummyStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003097
3098 // Free up the stream endpoint so that it can be used by some other stream
3099 res = deletedStream->disconnect();
3100 if (res != OK) {
3101 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
3102 // fall through since we want to still list the stream as deleted.
3103 }
3104 mDeletedStreams.add(deletedStream);
3105 mDummyStreamId = NO_STREAM;
3106
3107 return res;
3108}
3109
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003110void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003111 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003112 Mutex::Autolock l(mLock);
3113 va_list args;
3114 va_start(args, fmt);
3115
3116 setErrorStateLockedV(fmt, args);
3117
3118 va_end(args);
3119}
3120
3121void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003122 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003123 Mutex::Autolock l(mLock);
3124 setErrorStateLockedV(fmt, args);
3125}
3126
3127void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
3128 va_list args;
3129 va_start(args, fmt);
3130
3131 setErrorStateLockedV(fmt, args);
3132
3133 va_end(args);
3134}
3135
3136void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003137 // Print out all error messages to log
3138 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003139 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003140
3141 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07003142 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003143
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003144 mErrorCause = errorCause;
3145
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07003146 if (mRequestThread != nullptr) {
3147 mRequestThread->setPaused(true);
3148 }
Ruben Brunk183f0562015-08-12 12:55:02 -07003149 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003150
3151 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003152 sp<NotificationListener> listener = mListener.promote();
3153 if (listener != NULL) {
3154 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003155 CaptureResultExtras());
3156 }
3157
3158 // Save stack trace. View by dumping it later.
3159 CameraTraces::saveTrace();
3160 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003161}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003162
3163/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003164 * In-flight request management
3165 */
3166
Jianing Weicb0652e2014-03-12 18:29:36 -07003167status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07003168 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003169 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003170 std::set<String8>& physicalCameraIds, bool isStillCapture,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003171 bool isZslCapture, const std::set<std::string>& cameraIdsWithZoom,
3172 const SurfaceMap& outputSurfaces) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003173 ATRACE_CALL();
3174 Mutex::Autolock l(mInFlightLock);
3175
3176 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07003177 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003178 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003179 cameraIdsWithZoom, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003180 if (res < 0) return res;
3181
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003182 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01003183 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3184 // avoid a deadlock during reprocess requests.
3185 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003186 if (mStatusTracker != nullptr) {
3187 mStatusTracker->markComponentActive(mInFlightStatusId);
3188 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003189 }
3190
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003191 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003192 return OK;
3193}
3194
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003195void Camera3Device::returnOutputBuffers(
3196 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003197 nsecs_t timestamp, bool timestampIncreasing,
3198 const SurfaceMap& outputSurfaces,
3199 const CaptureResultExtras &inResultExtras) {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003200
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003201 for (size_t i = 0; i < numBuffers; i++)
3202 {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003203 if (outputBuffers[i].buffer == nullptr) {
3204 if (!mUseHalBufManager) {
3205 // With HAL buffer management API, HAL sometimes will have to return buffers that
3206 // has not got a output buffer handle filled yet. This is though illegal if HAL
3207 // buffer management API is not being used.
3208 ALOGE("%s: cannot return a null buffer!", __FUNCTION__);
3209 }
3210 continue;
3211 }
3212
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003213 Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream);
3214 int streamId = stream->getId();
3215 const auto& it = outputSurfaces.find(streamId);
3216 status_t res = OK;
3217 if (it != outputSurfaces.end()) {
3218 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003219 outputBuffers[i], timestamp, timestampIncreasing, it->second,
3220 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003221 } else {
3222 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003223 outputBuffers[i], timestamp, timestampIncreasing, std::vector<size_t> (),
3224 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003225 }
3226
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003227 // Note: stream may be deallocated at this point, if this buffer was
3228 // the last reference to it.
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003229 if (res == NO_INIT || res == DEAD_OBJECT) {
3230 ALOGV("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
3231 } else if (res != OK) {
3232 ALOGE("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003233 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003234
3235 // Long processing consumers can cause returnBuffer timeout for shared stream
3236 // If that happens, cancel the buffer and send a buffer error to client
3237 if (it != outputSurfaces.end() && res == TIMED_OUT &&
3238 outputBuffers[i].status == CAMERA3_BUFFER_STATUS_OK) {
3239 // cancel the buffer
3240 camera3_stream_buffer_t sb = outputBuffers[i];
3241 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
Emilian Peev538c90e2018-12-17 18:03:19 +00003242 stream->returnBuffer(sb, /*timestamp*/0, timestampIncreasing, std::vector<size_t> (),
3243 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003244
3245 // notify client buffer error
3246 sp<NotificationListener> listener;
3247 {
3248 Mutex::Autolock l(mOutputLock);
3249 listener = mListener.promote();
3250 }
3251
3252 if (listener != nullptr) {
3253 CaptureResultExtras extras = inResultExtras;
3254 extras.errorStreamId = streamId;
3255 listener->notifyError(
3256 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
3257 extras);
3258 }
3259 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003260 }
3261}
3262
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003263void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003264 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003265 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003266 mInFlightMap.removeItemsAt(idx, 1);
3267
3268 // Indicate idle inFlightMap to the status tracker
3269 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003270 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01003271 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3272 // avoid a deadlock during reprocess requests.
3273 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003274 if (mStatusTracker != nullptr) {
3275 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
3276 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003277 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003278 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003279}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003280
3281void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
3282
3283 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3284 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
3285
3286 nsecs_t sensorTimestamp = request.sensorTimestamp;
3287 nsecs_t shutterTimestamp = request.shutterTimestamp;
3288
3289 // Check if it's okay to remove the request from InFlightMap:
3290 // In the case of a successful request:
3291 // all input and output buffers, all result metadata, shutter callback
3292 // arrived.
3293 // In the case of a unsuccessful request:
3294 // all input and output buffers arrived.
3295 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07003296 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003297 (request.haveResultMetadata && shutterTimestamp != 0))) {
Emilian Peev9dd21f42018-08-03 13:39:29 +01003298 if (request.stillCapture) {
3299 ATRACE_ASYNC_END("still capture", frameNumber);
3300 }
3301
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003302 ATRACE_ASYNC_END("frame capture", frameNumber);
3303
Shuzhen Wang403044a2017-02-26 23:29:04 -08003304 // Sanity check - if sensor timestamp matches shutter timestamp in the
3305 // case of request having callback.
3306 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003307 sensorTimestamp != shutterTimestamp) {
3308 SET_ERR("sensor timestamp (%" PRId64
3309 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
3310 sensorTimestamp, frameNumber, shutterTimestamp);
3311 }
3312
3313 // for an unsuccessful request, it may have pending output buffers to
3314 // return.
3315 assert(request.requestStatus != OK ||
3316 request.pendingOutputBuffers.size() == 0);
3317 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003318 request.pendingOutputBuffers.size(), 0, /*timestampIncreasing*/true,
3319 request.outputSurfaces, request.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003320
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003321 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003322 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
3323 }
3324
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07003325 // Sanity check - if we have too many in-flight frames with long total inflight duration,
3326 // something has likely gone wrong. This might still be legit only if application send in
3327 // a long burst of long exposure requests.
3328 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
3329 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
3330 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
3331 mInFlightMap.size(), mExpectedInflightDuration);
3332 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
3333 kInFlightWarnLimitHighSpeed) {
3334 CLOGW("In-flight list too large for high speed configuration: %zu,"
3335 "total inflight duration %" PRIu64,
3336 mInFlightMap.size(), mExpectedInflightDuration);
3337 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003338 }
3339}
3340
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003341void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003342 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003343 { // First return buffers cached in mInFlightMap
3344 Mutex::Autolock l(mInFlightLock);
3345 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
3346 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3347 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003348 request.pendingOutputBuffers.size(), 0,
3349 /*timestampIncreasing*/true, request.outputSurfaces,
3350 request.resultExtras);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003351 }
3352 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07003353 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003354 }
3355
3356 // Then return all inflight buffers not returned by HAL
3357 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
3358 mInterface->getInflightBufferKeys(&inflightKeys);
3359
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003360 // Inflight buffers for HAL buffer manager
3361 std::vector<uint64_t> inflightRequestBufferKeys;
3362 mInterface->getInflightRequestBufferKeys(&inflightRequestBufferKeys);
3363
3364 // (streamId, frameNumber, buffer_handle_t*) tuple for all inflight buffers.
3365 // frameNumber will be -1 for buffers from HAL buffer manager
3366 std::vector<std::tuple<int32_t, int32_t, buffer_handle_t*>> inflightBuffers;
3367 inflightBuffers.reserve(inflightKeys.size() + inflightRequestBufferKeys.size());
3368
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003369 for (auto& pair : inflightKeys) {
3370 int32_t frameNumber = pair.first;
3371 int32_t streamId = pair.second;
3372 buffer_handle_t* buffer;
3373 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
3374 if (res != OK) {
3375 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
3376 __FUNCTION__, frameNumber, streamId);
3377 continue;
3378 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003379 inflightBuffers.push_back(std::make_tuple(streamId, frameNumber, buffer));
3380 }
3381
3382 for (auto& bufferId : inflightRequestBufferKeys) {
3383 int32_t streamId = -1;
3384 buffer_handle_t* buffer = nullptr;
3385 status_t res = mInterface->popInflightRequestBuffer(bufferId, &buffer, &streamId);
3386 if (res != OK) {
3387 ALOGE("%s: cannot find in-flight buffer %" PRIu64, __FUNCTION__, bufferId);
3388 continue;
3389 }
3390 inflightBuffers.push_back(std::make_tuple(streamId, /*frameNumber*/-1, buffer));
3391 }
3392
3393 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
3394 for (auto& tuple : inflightBuffers) {
3395 status_t res = OK;
3396 int32_t streamId = std::get<0>(tuple);
3397 int32_t frameNumber = std::get<1>(tuple);
3398 buffer_handle_t* buffer = std::get<2>(tuple);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003399
3400 camera3_stream_buffer_t streamBuffer;
3401 streamBuffer.buffer = buffer;
3402 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3403 streamBuffer.acquire_fence = -1;
3404 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003405
3406 // First check if the buffer belongs to deleted stream
3407 bool streamDeleted = false;
3408 for (auto& stream : mDeletedStreams) {
3409 if (streamId == stream->getId()) {
3410 streamDeleted = true;
3411 // Return buffer to deleted stream
3412 camera3_stream* halStream = stream->asHalStream();
3413 streamBuffer.stream = halStream;
3414 switch (halStream->stream_type) {
3415 case CAMERA3_STREAM_OUTPUT:
Emilian Peev538c90e2018-12-17 18:03:19 +00003416 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0,
3417 /*timestampIncreasing*/true, std::vector<size_t> (), frameNumber);
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003418 if (res != OK) {
3419 ALOGE("%s: Can't return output buffer for frame %d to"
3420 " stream %d: %s (%d)", __FUNCTION__,
3421 frameNumber, streamId, strerror(-res), res);
3422 }
3423 break;
3424 case CAMERA3_STREAM_INPUT:
3425 res = stream->returnInputBuffer(streamBuffer);
3426 if (res != OK) {
3427 ALOGE("%s: Can't return input buffer for frame %d to"
3428 " stream %d: %s (%d)", __FUNCTION__,
3429 frameNumber, streamId, strerror(-res), res);
3430 }
3431 break;
3432 default: // Bi-direcitonal stream is deprecated
3433 ALOGE("%s: stream %d has unknown stream type %d",
3434 __FUNCTION__, streamId, halStream->stream_type);
3435 break;
3436 }
3437 break;
3438 }
3439 }
3440 if (streamDeleted) {
3441 continue;
3442 }
3443
3444 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003445 if (streamId == inputStreamId) {
3446 streamBuffer.stream = mInputStream->asHalStream();
3447 res = mInputStream->returnInputBuffer(streamBuffer);
3448 if (res != OK) {
3449 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003450 " stream %d: %s (%d)", __FUNCTION__,
3451 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003452 }
3453 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003454 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
3455 if (stream == nullptr) {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003456 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
3457 continue;
3458 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003459 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003460 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
3461 }
3462 }
3463}
3464
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003465void Camera3Device::insertResultLocked(CaptureResult *result,
3466 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003467 if (result == nullptr) return;
3468
Emilian Peev71c73a22017-03-21 16:35:51 +00003469 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
3470 result->mMetadata.getAndLock());
3471 set_camera_metadata_vendor_id(meta, mVendorTagId);
3472 result->mMetadata.unlock(meta);
3473
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003474 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
3475 (int32_t*)&frameNumber, 1) != OK) {
3476 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
3477 return;
3478 }
3479
3480 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
3481 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
3482 return;
3483 }
3484
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003485 // Update vendor tag id for physical metadata
3486 for (auto& physicalMetadata : result->mPhysicalMetadatas) {
3487 camera_metadata_t *pmeta = const_cast<camera_metadata_t *>(
3488 physicalMetadata.mPhysicalCameraMetadata.getAndLock());
3489 set_camera_metadata_vendor_id(pmeta, mVendorTagId);
3490 physicalMetadata.mPhysicalCameraMetadata.unlock(pmeta);
3491 }
3492
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003493 // Valid result, insert into queue
3494 List<CaptureResult>::iterator queuedResult =
3495 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
3496 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
3497 ", burstId = %" PRId32, __FUNCTION__,
3498 queuedResult->mResultExtras.requestId,
3499 queuedResult->mResultExtras.frameNumber,
3500 queuedResult->mResultExtras.burstId);
3501
3502 mResultSignal.signal();
3503}
3504
3505
3506void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003507 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003508 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003509 Mutex::Autolock l(mOutputLock);
3510
3511 CaptureResult captureResult;
3512 captureResult.mResultExtras = resultExtras;
3513 captureResult.mMetadata = partialResult;
3514
Shuzhen Wang268a1362018-10-16 16:32:59 -07003515 // Fix up result metadata for monochrome camera.
3516 status_t res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3517 if (res != OK) {
3518 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3519 return;
3520 }
3521
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003522 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003523}
3524
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003525
3526void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
3527 CaptureResultExtras &resultExtras,
3528 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003529 uint32_t frameNumber,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003530 bool reprocess, bool zslStillCapture, const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003531 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003532 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003533 if (pendingMetadata.isEmpty())
3534 return;
3535
3536 Mutex::Autolock l(mOutputLock);
3537
3538 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003539 if (reprocess) {
3540 if (frameNumber < mNextReprocessResultFrameNumber) {
3541 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003542 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003543 frameNumber, mNextReprocessResultFrameNumber);
3544 return;
3545 }
3546 mNextReprocessResultFrameNumber = frameNumber + 1;
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003547 } else if (zslStillCapture) {
3548 if (frameNumber < mNextZslStillResultFrameNumber) {
3549 SET_ERR("Out-of-order ZSL still capture result metadata submitted! "
3550 "(got frame number %d, expecting %d)",
3551 frameNumber, mNextZslStillResultFrameNumber);
3552 return;
3553 }
3554 mNextZslStillResultFrameNumber = frameNumber + 1;
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003555 } else {
3556 if (frameNumber < mNextResultFrameNumber) {
3557 SET_ERR("Out-of-order capture result metadata submitted! "
3558 "(got frame number %d, expecting %d)",
3559 frameNumber, mNextResultFrameNumber);
3560 return;
3561 }
3562 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003563 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003564
3565 CaptureResult captureResult;
3566 captureResult.mResultExtras = resultExtras;
3567 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003568 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003569
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003570 // Append any previous partials to form a complete result
3571 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
3572 captureResult.mMetadata.append(collectedPartialResult);
3573 }
3574
3575 captureResult.mMetadata.sort();
3576
3577 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003578 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3579 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003580 SET_ERR("No timestamp provided by HAL for frame %d!",
3581 frameNumber);
3582 return;
3583 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003584 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3585 camera_metadata_entry timestamp =
3586 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3587 if (timestamp.count == 0) {
3588 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
3589 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
3590 return;
3591 }
3592 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003593
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003594 // Fix up some result metadata to account for HAL-level distortion correction
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003595 status_t res =
3596 mDistortionMappers[mId.c_str()].correctCaptureResult(&captureResult.mMetadata);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003597 if (res != OK) {
3598 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003599 frameNumber, strerror(-res), res);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003600 return;
3601 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003602
3603 // Fix up result metadata to account for zoom ratio availabilities between
3604 // HAL and app.
3605 bool zoomRatioIs1 = cameraIdsWithZoom.find(mId.c_str()) == cameraIdsWithZoom.end();
3606 res = mZoomRatioMappers[mId.c_str()].updateCaptureResult(
3607 &captureResult.mMetadata, zoomRatioIs1);
3608 if (res != OK) {
3609 SET_ERR("Failed to update capture result zoom ratio metadata for frame %d: %s (%d)",
3610 frameNumber, strerror(-res), res);
3611 return;
3612 }
3613
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003614 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3615 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003616 if (mDistortionMappers.find(cameraId8.c_str()) != mDistortionMappers.end()) {
3617 res = mDistortionMappers[cameraId8.c_str()].correctCaptureResult(
3618 &physicalMetadata.mPhysicalCameraMetadata);
3619 if (res != OK) {
3620 SET_ERR("Unable to correct physical capture result metadata for frame %d: %s (%d)",
3621 frameNumber, strerror(-res), res);
3622 return;
3623 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003624 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003625
3626 zoomRatioIs1 = cameraIdsWithZoom.find(cameraId8.c_str()) == cameraIdsWithZoom.end();
3627 res = mZoomRatioMappers[cameraId8.c_str()].updateCaptureResult(
3628 &physicalMetadata.mPhysicalCameraMetadata, zoomRatioIs1);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003629 if (res != OK) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003630 SET_ERR("Failed to update camera %s's physical zoom ratio metadata for "
3631 "frame %d: %s(%d)", cameraId8.c_str(), frameNumber, strerror(-res), res);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003632 return;
3633 }
3634 }
3635
Shuzhen Wang268a1362018-10-16 16:32:59 -07003636 // Fix up result metadata for monochrome camera.
3637 res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3638 if (res != OK) {
3639 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3640 return;
3641 }
3642 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3643 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
3644 res = fixupMonochromeTags(mPhysicalDeviceInfoMap.at(cameraId8.c_str()),
3645 physicalMetadata.mPhysicalCameraMetadata);
3646 if (res != OK) {
3647 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3648 return;
3649 }
3650 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003651
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003652 std::unordered_map<std::string, CameraMetadata> monitoredPhysicalMetadata;
3653 for (auto& m : physicalMetadatas) {
3654 monitoredPhysicalMetadata.emplace(String8(m.mPhysicalCameraId).string(),
3655 CameraMetadata(m.mPhysicalCameraMetadata));
3656 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003657 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003658 frameNumber, timestamp.data.i64[0], captureResult.mMetadata,
3659 monitoredPhysicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003660
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003661 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003662}
3663
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003664/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003665 * Camera HAL device callback methods
3666 */
3667
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003668void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003669 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003670
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003671 status_t res;
3672
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003673 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003674 if (result->result == NULL && result->num_output_buffers == 0 &&
3675 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003676 SET_ERR("No result data provided by HAL for frame %d",
3677 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003678 return;
3679 }
Zhijun He204e3292014-07-14 17:09:23 -07003680
Zhijun He204e3292014-07-14 17:09:23 -07003681 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003682 result->result != NULL &&
3683 result->partial_result != 1) {
3684 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3685 " if partial result is not supported",
3686 frameNumber, result->partial_result);
3687 return;
3688 }
3689
3690 bool isPartialResult = false;
3691 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003692 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003693
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003694 // Get shutter timestamp and resultExtras from list of in-flight requests,
3695 // where it was added by the shutter notification for this frame. If the
3696 // shutter timestamp isn't received yet, append the output buffers to the
3697 // in-flight request and they will be returned when the shutter timestamp
3698 // arrives. Update the in-flight status and remove the in-flight entry if
3699 // all result data and shutter timestamp have been received.
3700 nsecs_t shutterTimestamp = 0;
3701
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003702 {
3703 Mutex::Autolock l(mInFlightLock);
3704 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3705 if (idx == NAME_NOT_FOUND) {
3706 SET_ERR("Unknown frame number for capture result: %d",
3707 frameNumber);
3708 return;
3709 }
3710 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003711 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3712 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003713 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003714 __FUNCTION__, request.resultExtras.requestId,
3715 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003716 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003717 // Always update the partial count to the latest one if it's not 0
3718 // (buffers only). When framework aggregates adjacent partial results
3719 // into one, the latest partial count will be used.
3720 if (result->partial_result != 0)
3721 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003722
3723 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003724 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003725 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3726 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3727 " the range of [1, %d] when metadata is included in the result",
3728 frameNumber, result->partial_result, mNumPartialResults);
3729 return;
3730 }
3731 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003732 if (isPartialResult && result->num_physcam_metadata) {
3733 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3734 " physical camera result", frameNumber);
3735 return;
3736 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003737 if (isPartialResult) {
3738 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003739 }
3740
Shuzhen Wang4a472662017-02-26 23:29:04 -08003741 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003742 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003743 sendPartialCaptureResult(result->result, request.resultExtras,
3744 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003745 }
3746 }
3747
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003748 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003749 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003750
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003751 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003752 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003753 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003754 SET_ERR("Expected physical Camera metadata count %d not equal to actual count %d",
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003755 request.physicalCameraIds.size(), result->num_physcam_metadata);
3756 return;
3757 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003758 if (request.haveResultMetadata) {
3759 SET_ERR("Called multiple times with metadata for frame %d",
3760 frameNumber);
3761 return;
3762 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003763 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3764 String8 physicalId(result->physcam_ids[i]);
3765 std::set<String8>::iterator cameraIdIter =
3766 request.physicalCameraIds.find(physicalId);
3767 if (cameraIdIter != request.physicalCameraIds.end()) {
3768 request.physicalCameraIds.erase(cameraIdIter);
3769 } else {
3770 SET_ERR("Total result for frame %d has already returned for camera %s",
3771 frameNumber, physicalId.c_str());
3772 return;
3773 }
3774 }
Zhijun He204e3292014-07-14 17:09:23 -07003775 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003776 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003777 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003778 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003779 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003780 request.haveResultMetadata = true;
3781 }
3782
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003783 uint32_t numBuffersReturned = result->num_output_buffers;
3784 if (result->input_buffer != NULL) {
3785 if (hasInputBufferInRequest) {
3786 numBuffersReturned += 1;
3787 } else {
3788 ALOGW("%s: Input buffer should be NULL if there is no input"
3789 " buffer sent in the request",
3790 __FUNCTION__);
3791 }
3792 }
3793 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003794 if (request.numBuffersLeft < 0) {
3795 SET_ERR("Too many buffers returned for frame %d",
3796 frameNumber);
3797 return;
3798 }
3799
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003800 camera_metadata_ro_entry_t entry;
3801 res = find_camera_metadata_ro_entry(result->result,
3802 ANDROID_SENSOR_TIMESTAMP, &entry);
3803 if (res == OK && entry.count == 1) {
3804 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003805 }
3806
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003807 // If shutter event isn't received yet, append the output buffers to
3808 // the in-flight request. Otherwise, return the output buffers to
3809 // streams.
3810 if (shutterTimestamp == 0) {
3811 request.pendingOutputBuffers.appendArray(result->output_buffers,
3812 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003813 } else {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003814 bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003815 returnOutputBuffers(result->output_buffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003816 result->num_output_buffers, shutterTimestamp, timestampIncreasing,
3817 request.outputSurfaces, request.resultExtras);
Igor Murashkind2c90692013-04-02 12:32:32 -07003818 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003819
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003820 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003821 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3822 CameraMetadata physicalMetadata;
3823 physicalMetadata.append(result->physcam_metadata[i]);
3824 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3825 physicalMetadata});
3826 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003827 if (shutterTimestamp == 0) {
3828 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003829 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang268a1362018-10-16 16:32:59 -07003830 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003831 CameraMetadata metadata;
3832 metadata = result->result;
3833 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003834 collectedPartialResult, frameNumber,
Shuzhen Wang5ee99842019-04-12 11:55:48 -07003835 hasInputBufferInRequest, request.zslCapture && request.stillCapture,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003836 request.cameraIdsWithZoom, request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003837 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003838 }
3839
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003840 removeInFlightRequestIfReadyLocked(idx);
3841 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003842
Zhijun Hef0d962a2014-06-30 10:24:11 -07003843 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003844 if (hasInputBufferInRequest) {
3845 Camera3Stream *stream =
3846 Camera3Stream::cast(result->input_buffer->stream);
3847 res = stream->returnInputBuffer(*(result->input_buffer));
3848 // Note: stream may be deallocated at this point, if this buffer was the
3849 // last reference to it.
3850 if (res != OK) {
3851 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3852 " its stream:%s (%d)", __FUNCTION__,
3853 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003854 }
3855 } else {
3856 ALOGW("%s: Input buffer should be NULL if there is no input"
3857 " buffer sent in the request, skipping input buffer return.",
3858 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003859 }
3860 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003861}
3862
3863void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003864 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003865 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003866 {
3867 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003868 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003869 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003870
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003871 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003872 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003873 return;
3874 }
3875
3876 switch (msg->type) {
3877 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003878 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003879 break;
3880 }
3881 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003882 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003883 break;
3884 }
3885 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003886 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003887 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003888 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003889}
3890
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003891void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003892 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003893 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003894 // Map camera HAL error codes to ICameraDeviceCallback error codes
3895 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003896 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003897 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003898 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003899 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003900 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003901 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003902 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003903 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003904 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003905 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003906 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003907 };
3908
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003909 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003910 ((msg.error_code >= 0) &&
3911 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3912 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003913 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003914
3915 int streamId = 0;
Emilian Peevedec62d2019-03-19 17:59:24 -07003916 String16 physicalCameraId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003917 if (msg.error_stream != NULL) {
3918 Camera3Stream *stream =
3919 Camera3Stream::cast(msg.error_stream);
3920 streamId = stream->getId();
Emilian Peevedec62d2019-03-19 17:59:24 -07003921 physicalCameraId = String16(stream->physicalCameraId());
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003922 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003923 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3924 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003925 streamId, msg.error_code);
3926
3927 CaptureResultExtras resultExtras;
3928 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003929 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003930 // SET_ERR calls notifyError
3931 SET_ERR("Camera HAL reported serious device error");
3932 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003933 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3934 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3935 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003936 {
3937 Mutex::Autolock l(mInFlightLock);
3938 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3939 if (idx >= 0) {
3940 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3941 r.requestStatus = msg.error_code;
3942 resultExtras = r.resultExtras;
Emilian Peevedec62d2019-03-19 17:59:24 -07003943 bool logicalDeviceResultError = false;
3944 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3945 errorCode) {
3946 if (physicalCameraId.size() > 0) {
3947 String8 cameraId(physicalCameraId);
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003948 auto iter = r.physicalCameraIds.find(cameraId);
3949 if (iter == r.physicalCameraIds.end()) {
Emilian Peevedec62d2019-03-19 17:59:24 -07003950 ALOGE("%s: Reported result failure for physical camera device: %s "
3951 " which is not part of the respective request!",
3952 __FUNCTION__, cameraId.string());
3953 break;
3954 }
Shuzhen Wang468dbb82019-07-31 11:35:33 -07003955 r.physicalCameraIds.erase(iter);
Emilian Peevedec62d2019-03-19 17:59:24 -07003956 resultExtras.errorPhysicalCameraId = physicalCameraId;
3957 } else {
3958 logicalDeviceResultError = true;
3959 }
3960 }
3961
3962 if (logicalDeviceResultError
Shuzhen Wang20f57342017-08-24 15:39:05 -07003963 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3964 errorCode) {
3965 r.skipResultMetadata = true;
3966 }
Emilian Peevedec62d2019-03-19 17:59:24 -07003967 if (logicalDeviceResultError) {
Emilian Peevba0fac32017-03-30 09:05:34 +01003968 // In case of missing result check whether the buffers
3969 // returned. If they returned, then remove inflight
3970 // request.
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003971 // TODO: should we call this for ERROR_CAMERA_REQUEST as well?
3972 // otherwise we are depending on HAL to send the buffers back after
3973 // calling notifyError. Not sure if that's in the spec.
Emilian Peevba0fac32017-03-30 09:05:34 +01003974 removeInFlightRequestIfReadyLocked(idx);
3975 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003976 } else {
3977 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003978 ALOGE("Camera %s: %s: cannot find in-flight request on "
3979 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003980 resultExtras.frameNumber);
3981 }
3982 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003983 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003984 if (listener != NULL) {
3985 listener->notifyError(errorCode, resultExtras);
3986 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003987 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003988 }
3989 break;
3990 default:
3991 // SET_ERR calls notifyError
3992 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3993 break;
3994 }
3995}
3996
3997void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003998 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003999 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004000 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004001
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004002 // Set timestamp for the request in the in-flight tracking
4003 // and get the request ID to send upstream
4004 {
4005 Mutex::Autolock l(mInFlightLock);
4006 idx = mInFlightMap.indexOfKey(msg.frame_number);
4007 if (idx >= 0) {
4008 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004009
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07004010 // Verify ordering of shutter notifications
4011 {
4012 Mutex::Autolock l(mOutputLock);
4013 // TODO: need to track errors for tighter bounds on expected frame number.
4014 if (r.hasInputBuffer) {
4015 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
Shuzhen Wang5ee99842019-04-12 11:55:48 -07004016 SET_ERR("Reprocess shutter notification out-of-order. Expected "
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07004017 "notification for frame %d, got frame %d",
4018 mNextReprocessShutterFrameNumber, msg.frame_number);
4019 return;
4020 }
4021 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
Shuzhen Wang5ee99842019-04-12 11:55:48 -07004022 } else if (r.zslCapture && r.stillCapture) {
4023 if (msg.frame_number < mNextZslStillShutterFrameNumber) {
4024 SET_ERR("ZSL still capture shutter notification out-of-order. Expected "
4025 "notification for frame %d, got frame %d",
4026 mNextZslStillShutterFrameNumber, msg.frame_number);
4027 return;
4028 }
4029 mNextZslStillShutterFrameNumber = msg.frame_number + 1;
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07004030 } else {
4031 if (msg.frame_number < mNextShutterFrameNumber) {
4032 SET_ERR("Shutter notification out-of-order. Expected "
4033 "notification for frame %d, got frame %d",
4034 mNextShutterFrameNumber, msg.frame_number);
4035 return;
4036 }
4037 mNextShutterFrameNumber = msg.frame_number + 1;
4038 }
4039 }
4040
Shuzhen Wang4a472662017-02-26 23:29:04 -08004041 r.shutterTimestamp = msg.timestamp;
4042 if (r.hasCallback) {
4043 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004044 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004045 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08004046 // Call listener, if any
4047 if (listener != NULL) {
4048 listener->notifyShutter(r.resultExtras, msg.timestamp);
4049 }
4050 // send pending result and buffers
4051 sendCaptureResult(r.pendingMetadata, r.resultExtras,
4052 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5ee99842019-04-12 11:55:48 -07004053 r.hasInputBuffer, r.zslCapture && r.stillCapture,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004054 r.cameraIdsWithZoom, r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004055 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004056 bool timestampIncreasing = !(r.zslCapture || r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004057 returnOutputBuffers(r.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004058 r.pendingOutputBuffers.size(), r.shutterTimestamp, timestampIncreasing,
4059 r.outputSurfaces, r.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08004060 r.pendingOutputBuffers.clear();
4061
4062 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004063 }
4064 }
4065 if (idx < 0) {
4066 SET_ERR("Shutter notification for non-existent frame number %d",
4067 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004068 }
4069}
4070
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004071CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07004072 ALOGV("%s", __FUNCTION__);
4073
Igor Murashkin1e479c02013-09-06 16:55:14 -07004074 CameraMetadata retVal;
4075
4076 if (mRequestThread != NULL) {
4077 retVal = mRequestThread->getLatestRequest();
4078 }
4079
Igor Murashkin1e479c02013-09-06 16:55:14 -07004080 return retVal;
4081}
4082
Jianing Weicb0652e2014-03-12 18:29:36 -07004083
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07004084void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004085 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
4086 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata) {
4087
4088 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
4089 physicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07004090}
4091
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004092/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004093 * HalInterface inner class methods
4094 */
4095
Yifan Hongf79b5542017-04-11 14:44:25 -07004096Camera3Device::HalInterface::HalInterface(
4097 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004098 std::shared_ptr<RequestMetadataQueue> queue,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004099 bool useHalBufManager, bool supportOfflineProcessing) :
Yifan Hongf79b5542017-04-11 14:44:25 -07004100 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004101 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08004102 mUseHalBufManager(useHalBufManager),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004103 mIsReconfigurationQuerySupported(true),
4104 mSupportOfflineProcessing(supportOfflineProcessing) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004105 // Check with hardware service manager if we can downcast these interfaces
4106 // Somewhat expensive, so cache the results at startup
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004107 auto castResult_3_6 = device::V3_6::ICameraDeviceSession::castFrom(mHidlSession);
4108 if (castResult_3_6.isOk()) {
4109 mHidlSession_3_6 = castResult_3_6;
4110 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004111 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
4112 if (castResult_3_5.isOk()) {
4113 mHidlSession_3_5 = castResult_3_5;
4114 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004115 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4116 if (castResult_3_4.isOk()) {
4117 mHidlSession_3_4 = castResult_3_4;
4118 }
4119 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
4120 if (castResult_3_3.isOk()) {
4121 mHidlSession_3_3 = castResult_3_3;
4122 }
4123}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004124
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004125Camera3Device::HalInterface::HalInterface() :
4126 mUseHalBufManager(false),
4127 mSupportOfflineProcessing(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004128
4129Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07004130 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004131 mRequestMetadataQueue(other.mRequestMetadataQueue),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004132 mUseHalBufManager(other.mUseHalBufManager),
4133 mSupportOfflineProcessing(other.mSupportOfflineProcessing) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004134
4135bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004136 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004137}
4138
4139void Camera3Device::HalInterface::clear() {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004140 mHidlSession_3_6.clear();
Emilian Peev644a3e12018-11-23 13:52:39 +00004141 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00004142 mHidlSession_3_4.clear();
4143 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004144 mHidlSession.clear();
4145}
4146
4147status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
4148 camera3_request_template_t templateId,
4149 /*out*/ camera_metadata_t **requestTemplate) {
4150 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
4151 if (!valid()) return INVALID_OPERATION;
4152 status_t res = OK;
4153
Emilian Peev31abd0a2017-05-11 18:37:46 +01004154 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004155
4156 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01004157 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004158 status = s;
4159 if (status == common::V1_0::Status::OK) {
4160 const camera_metadata *r =
4161 reinterpret_cast<const camera_metadata_t*>(request.data());
4162 size_t expectedSize = request.size();
4163 int ret = validate_camera_metadata_structure(r, &expectedSize);
4164 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
4165 *requestTemplate = clone_camera_metadata(r);
4166 if (*requestTemplate == nullptr) {
4167 ALOGE("%s: Unable to clone camera metadata received from HAL",
4168 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004169 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004170 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004171 } else {
4172 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
4173 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004174 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004175 }
4176 };
4177 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004178 RequestTemplate id;
4179 switch (templateId) {
4180 case CAMERA3_TEMPLATE_PREVIEW:
4181 id = RequestTemplate::PREVIEW;
4182 break;
4183 case CAMERA3_TEMPLATE_STILL_CAPTURE:
4184 id = RequestTemplate::STILL_CAPTURE;
4185 break;
4186 case CAMERA3_TEMPLATE_VIDEO_RECORD:
4187 id = RequestTemplate::VIDEO_RECORD;
4188 break;
4189 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
4190 id = RequestTemplate::VIDEO_SNAPSHOT;
4191 break;
4192 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
4193 id = RequestTemplate::ZERO_SHUTTER_LAG;
4194 break;
4195 case CAMERA3_TEMPLATE_MANUAL:
4196 id = RequestTemplate::MANUAL;
4197 break;
4198 default:
4199 // Unknown template ID, or this HAL is too old to support it
4200 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004201 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004202 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004203
Emilian Peev31abd0a2017-05-11 18:37:46 +01004204 if (!err.isOk()) {
4205 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4206 res = DEAD_OBJECT;
4207 } else {
4208 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004209 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004210
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004211 return res;
4212}
4213
Emilian Peev4ec17882019-01-24 17:16:58 -08004214bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
4215 CameraMetadata& newSessionParams) {
4216 // We do reconfiguration by default;
4217 bool ret = true;
4218 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
4219 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
4220 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
4221 oldSessionParams.getAndLock());
4222 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
4223 newSessionParams.getAndLock());
4224 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
4225 get_camera_metadata_size(oldSessioMeta));
4226 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
4227 get_camera_metadata_size(newSessioMeta));
4228 hardware::camera::common::V1_0::Status callStatus;
4229 bool required;
4230 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
4231 bool requiredFlag) {
4232 callStatus = s;
4233 required = requiredFlag;
4234 };
4235 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
4236 oldSessionParams.unlock(oldSessioMeta);
4237 newSessionParams.unlock(newSessioMeta);
4238 if (err.isOk()) {
4239 switch (callStatus) {
4240 case hardware::camera::common::V1_0::Status::OK:
4241 ret = required;
4242 break;
4243 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
4244 mIsReconfigurationQuerySupported = false;
4245 ret = true;
4246 break;
4247 default:
4248 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
4249 ret = true;
4250 }
4251 } else {
4252 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
4253 ret = true;
4254 }
4255 }
4256
4257 return ret;
4258}
4259
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004260status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00004261 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004262 ATRACE_NAME("CameraHal::configureStreams");
4263 if (!valid()) return INVALID_OPERATION;
4264 status_t res = OK;
4265
Emilian Peev31abd0a2017-05-11 18:37:46 +01004266 // Convert stream config to HIDL
4267 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004268 device::V3_2::StreamConfiguration requestedConfiguration3_2;
4269 device::V3_4::StreamConfiguration requestedConfiguration3_4;
4270 requestedConfiguration3_2.streams.resize(config->num_streams);
4271 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004272 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004273 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
4274 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01004275 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004276
Emilian Peev31abd0a2017-05-11 18:37:46 +01004277 Camera3Stream* cam3stream = Camera3Stream::cast(src);
4278 cam3stream->setBufferFreedListener(this);
4279 int streamId = cam3stream->getId();
4280 StreamType streamType;
4281 switch (src->stream_type) {
4282 case CAMERA3_STREAM_OUTPUT:
4283 streamType = StreamType::OUTPUT;
4284 break;
4285 case CAMERA3_STREAM_INPUT:
4286 streamType = StreamType::INPUT;
4287 break;
4288 default:
4289 ALOGE("%s: Stream %d: Unsupported stream type %d",
4290 __FUNCTION__, streamId, config->streams[i]->stream_type);
4291 return BAD_VALUE;
4292 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004293 dst3_2.id = streamId;
4294 dst3_2.streamType = streamType;
4295 dst3_2.width = src->width;
4296 dst3_2.height = src->height;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004297 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004298 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Shuzhen Wang92653952019-05-07 15:11:43 -07004299 // For HidlSession version 3.5 or newer, the format and dataSpace sent
4300 // to HAL are original, not the overriden ones.
4301 if (mHidlSession_3_5 != nullptr) {
4302 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
4303 cam3stream->getOriginalFormat() : src->format);
4304 dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
4305 cam3stream->getOriginalDataSpace() : src->data_space);
4306 } else {
4307 dst3_2.format = mapToPixelFormat(src->format);
4308 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
4309 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004310 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00004311 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004312 if (src->physical_camera_id != nullptr) {
4313 dst3_4.physicalCameraId = src->physical_camera_id;
4314 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004315
4316 activeStreams.insert(streamId);
4317 // Create Buffer ID map if necessary
4318 if (mBufferIdMaps.count(streamId) == 0) {
4319 mBufferIdMaps.emplace(streamId, BufferIdMap{});
4320 }
4321 }
4322 // remove BufferIdMap for deleted streams
4323 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
4324 int streamId = it->first;
4325 bool active = activeStreams.count(streamId) > 0;
4326 if (!active) {
4327 it = mBufferIdMaps.erase(it);
4328 } else {
4329 ++it;
4330 }
4331 }
4332
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004333 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004334 res = mapToStreamConfigurationMode(
4335 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004336 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004337 if (res != OK) {
4338 return res;
4339 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004340 requestedConfiguration3_2.operationMode = operationMode;
4341 requestedConfiguration3_4.operationMode = operationMode;
4342 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004343 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
4344 get_camera_metadata_size(sessionParams));
4345
Emilian Peev31abd0a2017-05-11 18:37:46 +01004346 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004347 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004348 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004349 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004350
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004351 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004352 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
4353 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004354 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004355 };
4356
4357 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
4358 (hardware::Return<void>& err) -> status_t {
4359 if (!err.isOk()) {
4360 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4361 return DEAD_OBJECT;
4362 }
4363 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
4364 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
4365 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
4366 }
4367 return OK;
4368 };
4369
Shuzhen Wang92653952019-05-07 15:11:43 -07004370 // See which version of HAL we have
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004371 if (mHidlSession_3_5 != nullptr) {
4372 ALOGV("%s: v3.5 device found", __FUNCTION__);
4373 device::V3_5::StreamConfiguration requestedConfiguration3_5;
4374 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
4375 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
4376 auto err = mHidlSession_3_5->configureStreams_3_5(
4377 requestedConfiguration3_5, configStream34Cb);
4378 res = postprocConfigStream34(err);
4379 if (res != OK) {
4380 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004381 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004382 } else if (mHidlSession_3_4 != nullptr) {
4383 // We do; use v3.4 for the call
4384 ALOGV("%s: v3.4 device found", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004385 auto err = mHidlSession_3_4->configureStreams_3_4(
4386 requestedConfiguration3_4, configStream34Cb);
4387 res = postprocConfigStream34(err);
4388 if (res != OK) {
4389 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004390 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004391 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004392 // We do; use v3.3 for the call
4393 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004394 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01004395 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004396 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004397 finalConfiguration = halConfiguration;
4398 status = s;
4399 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004400 if (!err.isOk()) {
4401 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4402 return DEAD_OBJECT;
4403 }
4404 } else {
4405 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
4406 ALOGV("%s: v3.2 device found", __FUNCTION__);
4407 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004408 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004409 [&status, &finalConfiguration_3_2]
4410 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
4411 finalConfiguration_3_2 = halConfiguration;
4412 status = s;
4413 });
4414 if (!err.isOk()) {
4415 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4416 return DEAD_OBJECT;
4417 }
4418 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
4419 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
4420 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
4421 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004422 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004423 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004424 }
4425
4426 if (status != common::V1_0::Status::OK ) {
4427 return CameraProviderManager::mapToStatusT(status);
4428 }
4429
4430 // And convert output stream configuration from HIDL
4431
4432 for (size_t i = 0; i < config->num_streams; i++) {
4433 camera3_stream_t *dst = config->streams[i];
4434 int streamId = Camera3Stream::cast(dst)->getId();
4435
4436 // Start scan at i, with the assumption that the stream order matches
4437 size_t realIdx = i;
4438 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004439 size_t halStreamCount = finalConfiguration.streams.size();
4440 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004441 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004442 found = true;
4443 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004444 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004445 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004446 }
4447 if (!found) {
4448 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
4449 __FUNCTION__, streamId);
4450 return INVALID_OPERATION;
4451 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004452 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004453
Emilian Peev710c1422017-08-30 11:19:38 +01004454 Camera3Stream* dstStream = Camera3Stream::cast(dst);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004455 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
4456 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
4457
Yin-Chia Yeh90667662019-07-01 15:45:00 -07004458 if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Shuzhen Wang92653952019-05-07 15:11:43 -07004459 dstStream->setFormatOverride(false);
4460 dstStream->setDataSpaceOverride(false);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004461 if (dst->format != overrideFormat) {
4462 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
4463 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004464 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004465 if (dst->data_space != overrideDataSpace) {
4466 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
4467 streamId, dst->format);
4468 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004469 } else {
Shuzhen Wang92653952019-05-07 15:11:43 -07004470 bool needFormatOverride =
4471 requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
4472 bool needDataspaceOverride =
4473 requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004474 // Override allowed with IMPLEMENTATION_DEFINED
Shuzhen Wang92653952019-05-07 15:11:43 -07004475 dstStream->setFormatOverride(needFormatOverride);
4476 dstStream->setDataSpaceOverride(needDataspaceOverride);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004477 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004478 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004479 }
4480
Emilian Peev31abd0a2017-05-11 18:37:46 +01004481 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004482 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004483 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004484 __FUNCTION__, streamId);
4485 return INVALID_OPERATION;
4486 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004487 dstStream->setUsage(
4488 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01004489 } else {
4490 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004491 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004492 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
4493 __FUNCTION__, streamId);
4494 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004495 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004496 dstStream->setUsage(
4497 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004498 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004499 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004500 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004501
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004502 return res;
4503}
4504
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004505status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004506 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004507 /*out*/std::vector<native_handle_t*>* handlesCreated,
4508 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004509 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004510 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
4511 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
4512 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004513 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004514 }
4515
4516 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07004517
4518 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004519
4520 {
4521 std::lock_guard<std::mutex> lock(mInflightLock);
4522 if (request->input_buffer != nullptr) {
4523 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
4524 buffer_handle_t buf = *(request->input_buffer->buffer);
4525 auto pair = getBufferId(buf, streamId);
4526 bool isNewBuffer = pair.first;
4527 uint64_t bufferId = pair.second;
4528 captureRequest->inputBuffer.streamId = streamId;
4529 captureRequest->inputBuffer.bufferId = bufferId;
4530 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
4531 captureRequest->inputBuffer.status = BufferStatus::OK;
4532 native_handle_t *acquireFence = nullptr;
4533 if (request->input_buffer->acquire_fence != -1) {
4534 acquireFence = native_handle_create(1,0);
4535 acquireFence->data[0] = request->input_buffer->acquire_fence;
4536 handlesCreated->push_back(acquireFence);
4537 }
4538 captureRequest->inputBuffer.acquireFence = acquireFence;
4539 captureRequest->inputBuffer.releaseFence = nullptr;
4540
4541 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004542 request->input_buffer->buffer);
4543 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004544 } else {
4545 captureRequest->inputBuffer.streamId = -1;
4546 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
4547 }
4548
4549 captureRequest->outputBuffers.resize(request->num_output_buffers);
4550 for (size_t i = 0; i < request->num_output_buffers; i++) {
4551 const camera3_stream_buffer_t *src = request->output_buffers + i;
4552 StreamBuffer &dst = captureRequest->outputBuffers[i];
4553 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004554 if (src->buffer != nullptr) {
4555 buffer_handle_t buf = *(src->buffer);
4556 auto pair = getBufferId(buf, streamId);
4557 bool isNewBuffer = pair.first;
4558 dst.bufferId = pair.second;
4559 dst.buffer = isNewBuffer ? buf : nullptr;
4560 native_handle_t *acquireFence = nullptr;
4561 if (src->acquire_fence != -1) {
4562 acquireFence = native_handle_create(1,0);
4563 acquireFence->data[0] = src->acquire_fence;
4564 handlesCreated->push_back(acquireFence);
4565 }
4566 dst.acquireFence = acquireFence;
4567 } else if (mUseHalBufManager) {
4568 // HAL buffer management path
4569 dst.bufferId = BUFFER_ID_NO_BUFFER;
4570 dst.buffer = nullptr;
4571 dst.acquireFence = nullptr;
4572 } else {
4573 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
4574 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004575 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004576 dst.streamId = streamId;
4577 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004578 dst.releaseFence = nullptr;
4579
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004580 // Output buffers are empty when using HAL buffer manager
4581 if (!mUseHalBufManager) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004582 pushInflightBufferLocked(captureRequest->frameNumber, streamId, src->buffer);
4583 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004584 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004585 }
4586 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004587 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004588}
4589
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004590void Camera3Device::HalInterface::cleanupNativeHandles(
4591 std::vector<native_handle_t*> *handles, bool closeFd) {
4592 if (handles == nullptr) {
4593 return;
4594 }
4595 if (closeFd) {
4596 for (auto& handle : *handles) {
4597 native_handle_close(handle);
4598 }
4599 }
4600 for (auto& handle : *handles) {
4601 native_handle_delete(handle);
4602 }
4603 handles->clear();
4604 return;
4605}
4606
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004607status_t Camera3Device::HalInterface::processBatchCaptureRequests(
4608 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
4609 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
4610 if (!valid()) return INVALID_OPERATION;
4611
Emilian Peevaebbe412018-01-15 13:53:24 +00004612 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
4613 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4614 if (castResult_3_4.isOk()) {
4615 hidlSession_3_4 = castResult_3_4;
4616 }
4617
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004618 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00004619 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004620 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00004621 if (hidlSession_3_4 != nullptr) {
4622 captureRequests_3_4.resize(batchSize);
4623 } else {
4624 captureRequests.resize(batchSize);
4625 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004626 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004627 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004628
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004629 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004630 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004631 if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004632 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004633 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00004634 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004635 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
4636 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004637 }
4638 if (res != OK) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004639 popInflightBuffers(inflightBuffers);
4640 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004641 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00004642 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004643 }
4644
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004645 std::vector<device::V3_2::BufferCache> cachesToRemove;
4646 {
4647 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4648 for (auto& pair : mFreedBuffers) {
4649 // The stream might have been removed since onBufferFreed
4650 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
4651 cachesToRemove.push_back({pair.first, pair.second});
4652 }
4653 }
4654 mFreedBuffers.clear();
4655 }
4656
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004657 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
4658 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07004659
4660 // Write metadata to FMQ.
4661 for (size_t i = 0; i < batchSize; i++) {
4662 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00004663 device::V3_2::CaptureRequest* captureRequest;
4664 if (hidlSession_3_4 != nullptr) {
4665 captureRequest = &captureRequests_3_4[i].v3_2;
4666 } else {
4667 captureRequest = &captureRequests[i];
4668 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004669
4670 if (request->settings != nullptr) {
4671 size_t settingsSize = get_camera_metadata_size(request->settings);
4672 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4673 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
4674 captureRequest->settings.resize(0);
4675 captureRequest->fmqSettingsSize = settingsSize;
4676 } else {
4677 if (mRequestMetadataQueue != nullptr) {
4678 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4679 }
4680 captureRequest->settings.setToExternal(
4681 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
4682 get_camera_metadata_size(request->settings));
4683 captureRequest->fmqSettingsSize = 0u;
4684 }
4685 } else {
4686 // A null request settings maps to a size-0 CameraMetadata
4687 captureRequest->settings.resize(0);
4688 captureRequest->fmqSettingsSize = 0u;
4689 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004690
4691 if (hidlSession_3_4 != nullptr) {
4692 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
4693 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00004694 if (request->physcam_settings != nullptr) {
4695 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
4696 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4697 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
4698 settingsSize)) {
4699 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
4700 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
4701 settingsSize;
4702 } else {
4703 if (mRequestMetadataQueue != nullptr) {
4704 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4705 }
4706 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
4707 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
4708 request->physcam_settings[j])),
4709 get_camera_metadata_size(request->physcam_settings[j]));
4710 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00004711 }
Emilian Peev00420d22018-02-05 21:33:13 +00004712 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00004713 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00004714 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00004715 }
4716 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
4717 request->physcam_id[j];
4718 }
4719 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004720 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004721
4722 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004723 auto resultCallback =
4724 [&status, &numRequestProcessed] (auto s, uint32_t n) {
4725 status = s;
4726 *numRequestProcessed = n;
4727 };
Emilian Peevaebbe412018-01-15 13:53:24 +00004728 if (hidlSession_3_4 != nullptr) {
4729 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004730 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004731 } else {
4732 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004733 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004734 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004735 if (!err.isOk()) {
4736 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004737 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004738 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004739
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004740 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
4741 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
4742 __FUNCTION__, *numRequestProcessed, batchSize);
4743 status = common::V1_0::Status::INTERNAL_ERROR;
4744 }
4745
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004746 res = CameraProviderManager::mapToStatusT(status);
4747 if (res == OK) {
4748 if (mHidlSession->isRemote()) {
4749 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
4750 // sent to camera HAL processes)
4751 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
4752 } else {
4753 // In passthrough mode the FDs are now owned by HAL
4754 cleanupNativeHandles(&handlesCreated);
4755 }
4756 } else {
4757 popInflightBuffers(inflightBuffers);
4758 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004759 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004760 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004761}
4762
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004763status_t Camera3Device::HalInterface::flush() {
4764 ATRACE_NAME("CameraHal::flush");
4765 if (!valid()) return INVALID_OPERATION;
4766 status_t res = OK;
4767
Emilian Peev31abd0a2017-05-11 18:37:46 +01004768 auto err = mHidlSession->flush();
4769 if (!err.isOk()) {
4770 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4771 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004772 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004773 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004774 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004775
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004776 return res;
4777}
4778
Emilian Peev31abd0a2017-05-11 18:37:46 +01004779status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004780 ATRACE_NAME("CameraHal::dump");
4781 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004782
Emilian Peev31abd0a2017-05-11 18:37:46 +01004783 // Handled by CameraProviderManager::dump
4784
4785 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004786}
4787
4788status_t Camera3Device::HalInterface::close() {
4789 ATRACE_NAME("CameraHal::close()");
4790 if (!valid()) return INVALID_OPERATION;
4791 status_t res = OK;
4792
Emilian Peev31abd0a2017-05-11 18:37:46 +01004793 auto err = mHidlSession->close();
4794 // Interface will be dead shortly anyway, so don't log errors
4795 if (!err.isOk()) {
4796 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004797 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004798
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004799 return res;
4800}
4801
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004802void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
4803 ATRACE_NAME("CameraHal::signalPipelineDrain");
4804 if (!valid() || mHidlSession_3_5 == nullptr) {
4805 ALOGE("%s called on invalid camera!", __FUNCTION__);
4806 return;
4807 }
4808
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004809 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004810 if (!err.isOk()) {
4811 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4812 return;
4813 }
4814}
4815
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004816status_t Camera3Device::HalInterface::switchToOffline(
4817 const std::vector<int32_t>& streamsToKeep,
4818 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
4819 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession) {
4820 ATRACE_NAME("CameraHal::switchToOffline");
4821 if (!valid() || mHidlSession_3_6 == nullptr) {
4822 ALOGE("%s called on invalid camera!", __FUNCTION__);
4823 return INVALID_OPERATION;
4824 }
4825
4826 if (offlineSessionInfo == nullptr || offlineSession == nullptr) {
4827 ALOGE("%s: offlineSessionInfo and offlineSession must not be null!", __FUNCTION__);
4828 return INVALID_OPERATION;
4829 }
4830
4831 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
4832
4833 auto resultCallback =
4834 [&status, &offlineSessionInfo, &offlineSession] (auto s, auto info, auto session) {
4835 status = s;
4836 *offlineSessionInfo = info;
4837 *offlineSession = session;
4838 };
4839 auto err = mHidlSession_3_6->switchToOffline(streamsToKeep, resultCallback);
4840
4841 if (!err.isOk()) {
4842 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4843 return DEAD_OBJECT;
4844 }
4845 return CameraProviderManager::mapToStatusT(status);
4846}
4847
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004848void Camera3Device::HalInterface::getInflightBufferKeys(
4849 std::vector<std::pair<int32_t, int32_t>>* out) {
4850 std::lock_guard<std::mutex> lock(mInflightLock);
4851 out->clear();
4852 out->reserve(mInflightBufferMap.size());
4853 for (auto& pair : mInflightBufferMap) {
4854 uint64_t key = pair.first;
4855 int32_t streamId = key & 0xFFFFFFFF;
4856 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
4857 out->push_back(std::make_pair(frameNumber, streamId));
4858 }
4859 return;
4860}
4861
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004862void Camera3Device::HalInterface::getInflightRequestBufferKeys(
4863 std::vector<uint64_t>* out) {
4864 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4865 out->clear();
4866 out->reserve(mRequestedBuffers.size());
4867 for (auto& pair : mRequestedBuffers) {
4868 out->push_back(pair.first);
4869 }
4870 return;
4871}
4872
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004873status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004874 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004875 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004876 mInflightBufferMap[key] = buffer;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004877 return OK;
4878}
4879
4880status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004881 int32_t frameNumber, int32_t streamId,
4882 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004883 std::lock_guard<std::mutex> lock(mInflightLock);
4884
4885 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4886 auto it = mInflightBufferMap.find(key);
4887 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004888 if (buffer != nullptr) {
4889 *buffer = it->second;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004890 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004891 mInflightBufferMap.erase(it);
4892 return OK;
4893}
4894
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004895void Camera3Device::HalInterface::popInflightBuffers(
4896 const std::vector<std::pair<int32_t, int32_t>>& buffers) {
4897 for (const auto& pair : buffers) {
4898 int32_t frameNumber = pair.first;
4899 int32_t streamId = pair.second;
4900 popInflightBuffer(frameNumber, streamId, nullptr);
4901 }
4902}
4903
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004904status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004905 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004906 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004907 auto pair = mRequestedBuffers.insert({bufferId, {streamId, buf}});
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004908 if (!pair.second) {
4909 ALOGE("%s: bufId %" PRIu64 " is already inflight!",
4910 __FUNCTION__, bufferId);
4911 return BAD_VALUE;
4912 }
4913 return OK;
4914}
4915
4916// Find and pop a buffer_handle_t based on bufferId
4917status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004918 uint64_t bufferId,
4919 /*out*/ buffer_handle_t** buffer,
4920 /*optional out*/ int32_t* streamId) {
4921 if (buffer == nullptr) {
4922 ALOGE("%s: buffer (%p) must not be null", __FUNCTION__, buffer);
4923 return BAD_VALUE;
4924 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004925 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4926 auto it = mRequestedBuffers.find(bufferId);
4927 if (it == mRequestedBuffers.end()) {
4928 ALOGE("%s: bufId %" PRIu64 " is not inflight!",
4929 __FUNCTION__, bufferId);
4930 return BAD_VALUE;
4931 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004932 *buffer = it->second.second;
4933 if (streamId != nullptr) {
4934 *streamId = it->second.first;
4935 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004936 mRequestedBuffers.erase(it);
4937 return OK;
4938}
4939
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004940std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4941 const buffer_handle_t& buf, int streamId) {
4942 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4943
4944 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4945 auto it = bIdMap.find(buf);
4946 if (it == bIdMap.end()) {
4947 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004948 ALOGV("stream %d now have %zu buffer caches, buf %p",
4949 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004950 return std::make_pair(true, mNextBufferId - 1);
4951 } else {
4952 return std::make_pair(false, it->second);
4953 }
4954}
4955
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004956void Camera3Device::HalInterface::onBufferFreed(
4957 int streamId, const native_handle_t* handle) {
4958 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4959 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4960 auto mapIt = mBufferIdMaps.find(streamId);
4961 if (mapIt == mBufferIdMaps.end()) {
4962 // streamId might be from a deleted stream here
4963 ALOGI("%s: stream %d has been removed",
4964 __FUNCTION__, streamId);
4965 return;
4966 }
4967 BufferIdMap& bIdMap = mapIt->second;
4968 auto it = bIdMap.find(handle);
4969 if (it == bIdMap.end()) {
4970 ALOGW("%s: cannot find buffer %p in stream %d",
4971 __FUNCTION__, handle, streamId);
4972 return;
4973 } else {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004974 bufferId = it->second;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004975 bIdMap.erase(it);
4976 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4977 __FUNCTION__, streamId, bIdMap.size(), handle);
4978 }
4979 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4980}
4981
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004982void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
4983 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4984 auto mapIt = mBufferIdMaps.find(streamId);
4985 if (mapIt == mBufferIdMaps.end()) {
4986 ALOGE("%s: streamId %d not found!", __FUNCTION__, streamId);
4987 return;
4988 }
4989
4990 BufferIdMap& bIdMap = mapIt->second;
4991 for (const auto& it : bIdMap) {
4992 uint64_t bufferId = it.second;
4993 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4994 }
4995 bIdMap.clear();
4996}
4997
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004998/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004999 * RequestThread inner class methods
5000 */
5001
5002Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005003 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005004 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
5005 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005006 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005007 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005008 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005009 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005010 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005011 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005012 mReconfigured(false),
5013 mDoPause(false),
5014 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005015 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005016 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07005017 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005018 mCurrentAfTriggerId(0),
5019 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005020 mRepeatingLastFrameNumber(
5021 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07005022 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005023 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005024 mRequestLatency(kRequestLatencyBinSize),
5025 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005026 mLatestSessionParams(sessionParamKeys.size()),
5027 mUseHalBufManager(useHalBufManager) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005028 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005029}
5030
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005031Camera3Device::RequestThread::~RequestThread() {}
5032
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005033void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005034 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005035 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005036 Mutex::Autolock l(mRequestLock);
5037 mListener = listener;
5038}
5039
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005040void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
5041 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005042 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005043 Mutex::Autolock l(mRequestLock);
5044 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005045 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005046 // Prepare video stream for high speed recording.
5047 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005048 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005049}
5050
Jianing Wei90e59c92014-03-12 18:29:36 -07005051status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005052 List<sp<CaptureRequest> > &requests,
5053 /*out*/
5054 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005055 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07005056 Mutex::Autolock l(mRequestLock);
5057 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
5058 ++it) {
5059 mRequestQueue.push_back(*it);
5060 }
5061
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005062 if (lastFrameNumber != NULL) {
5063 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
5064 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
5065 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
5066 *lastFrameNumber);
5067 }
Jianing Weicb0652e2014-03-12 18:29:36 -07005068
Jianing Wei90e59c92014-03-12 18:29:36 -07005069 unpauseForNewRequests();
5070
5071 return OK;
5072}
5073
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005074
5075status_t Camera3Device::RequestThread::queueTrigger(
5076 RequestTrigger trigger[],
5077 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005078 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005079 Mutex::Autolock l(mTriggerMutex);
5080 status_t ret;
5081
5082 for (size_t i = 0; i < count; ++i) {
5083 ret = queueTriggerLocked(trigger[i]);
5084
5085 if (ret != OK) {
5086 return ret;
5087 }
5088 }
5089
5090 return OK;
5091}
5092
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005093const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
5094 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005095 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08005096 if (d != nullptr) return d->mId;
5097 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005098}
5099
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005100status_t Camera3Device::RequestThread::queueTriggerLocked(
5101 RequestTrigger trigger) {
5102
5103 uint32_t tag = trigger.metadataTag;
5104 ssize_t index = mTriggerMap.indexOfKey(tag);
5105
5106 switch (trigger.getTagType()) {
5107 case TYPE_BYTE:
5108 // fall-through
5109 case TYPE_INT32:
5110 break;
5111 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005112 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
5113 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005114 return INVALID_OPERATION;
5115 }
5116
5117 /**
5118 * Collect only the latest trigger, since we only have 1 field
5119 * in the request settings per trigger tag, and can't send more than 1
5120 * trigger per request.
5121 */
5122 if (index != NAME_NOT_FOUND) {
5123 mTriggerMap.editValueAt(index) = trigger;
5124 } else {
5125 mTriggerMap.add(tag, trigger);
5126 }
5127
5128 return OK;
5129}
5130
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005131status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005132 const RequestList &requests,
5133 /*out*/
5134 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005135 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005136 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005137 if (lastFrameNumber != NULL) {
5138 *lastFrameNumber = mRepeatingLastFrameNumber;
5139 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005140 mRepeatingRequests.clear();
5141 mRepeatingRequests.insert(mRepeatingRequests.begin(),
5142 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005143
5144 unpauseForNewRequests();
5145
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005146 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005147 return OK;
5148}
5149
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07005150bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005151 if (mRepeatingRequests.empty()) {
5152 return false;
5153 }
5154 int32_t requestId = requestIn->mResultExtras.requestId;
5155 const RequestList &repeatRequests = mRepeatingRequests;
5156 // All repeating requests are guaranteed to have same id so only check first quest
5157 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
5158 return (firstRequest->mResultExtras.requestId == requestId);
5159}
5160
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005161status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005162 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005163 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005164 return clearRepeatingRequestsLocked(lastFrameNumber);
5165
5166}
5167
5168status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005169 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005170 if (lastFrameNumber != NULL) {
5171 *lastFrameNumber = mRepeatingLastFrameNumber;
5172 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005173 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005174 return OK;
5175}
5176
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005177status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005178 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005179 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005180 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005181 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005182
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005183 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005184
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005185 // Send errors for all requests pending in the request queue, including
5186 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005187 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005188 if (listener != NULL) {
5189 for (RequestList::iterator it = mRequestQueue.begin();
5190 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005191 // Abort the input buffers for reprocess requests.
5192 if ((*it)->mInputStream != NULL) {
5193 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07005194 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
5195 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005196 if (res != OK) {
5197 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
5198 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5199 } else {
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07005200 inputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005201 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
5202 if (res != OK) {
5203 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
5204 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5205 }
5206 }
5207 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005208 // Set the frame number this request would have had, if it
5209 // had been submitted; this frame number will not be reused.
5210 // The requestId and burstId fields were set when the request was
5211 // submitted originally (in convertMetadataListToRequestListLocked)
5212 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005213 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005214 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005215 }
5216 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005217 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08005218
5219 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005220 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005221 if (lastFrameNumber != NULL) {
5222 *lastFrameNumber = mRepeatingLastFrameNumber;
5223 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005224 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Emilian Peev8dae54c2019-12-02 15:17:17 -08005225 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005226 return OK;
5227}
5228
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005229status_t Camera3Device::RequestThread::flush() {
5230 ATRACE_CALL();
5231 Mutex::Autolock l(mFlushLock);
5232
Emilian Peev08dd2452017-04-06 16:55:14 +01005233 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005234}
5235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005236void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005237 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005238 Mutex::Autolock l(mPauseLock);
5239 mDoPause = paused;
5240 mDoPauseSignal.signal();
5241}
5242
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005243status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
5244 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005245 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005246 Mutex::Autolock l(mLatestRequestMutex);
5247 status_t res;
5248 while (mLatestRequestId != requestId) {
5249 nsecs_t startTime = systemTime();
5250
5251 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
5252 if (res != OK) return res;
5253
5254 timeout -= (systemTime() - startTime);
5255 }
5256
5257 return OK;
5258}
5259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005260void Camera3Device::RequestThread::requestExit() {
5261 // Call parent to set up shutdown
5262 Thread::requestExit();
5263 // The exit from any possible waits
5264 mDoPauseSignal.signal();
5265 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07005266
5267 mRequestLatency.log("ProcessCaptureRequest latency histogram");
5268 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005269}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005270
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005271void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005272 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005273 bool surfaceAbandoned = false;
5274 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005275 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005276 {
5277 Mutex::Autolock l(mRequestLock);
5278 // Check all streams needed by repeating requests are still valid. Otherwise, stop
5279 // repeating requests.
5280 for (const auto& request : mRepeatingRequests) {
5281 for (const auto& s : request->mOutputStreams) {
5282 if (s->isAbandoned()) {
5283 surfaceAbandoned = true;
5284 clearRepeatingRequestsLocked(&lastFrameNumber);
5285 break;
5286 }
5287 }
5288 if (surfaceAbandoned) {
5289 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005290 }
5291 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005292 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005293 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005294
5295 if (listener != NULL && surfaceAbandoned) {
5296 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005297 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005298}
5299
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005300bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005301 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005302 status_t res;
5303 size_t batchSize = mNextRequests.size();
5304 std::vector<camera3_capture_request_t*> requests(batchSize);
5305 uint32_t numRequestProcessed = 0;
5306 for (size_t i = 0; i < batchSize; i++) {
5307 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07005308 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005309 }
5310
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005311 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
5312
5313 bool triggerRemoveFailed = false;
5314 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
5315 for (size_t i = 0; i < numRequestProcessed; i++) {
5316 NextRequest& nextRequest = mNextRequests.editItemAt(i);
5317 nextRequest.submitted = true;
5318
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005319 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00005320
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005321 if (!triggerRemoveFailed) {
5322 // Remove any previously queued triggers (after unlock)
5323 status_t removeTriggerRes = removeTriggers(mPrevRequest);
5324 if (removeTriggerRes != OK) {
5325 triggerRemoveFailed = true;
5326 triggerFailedRequest = nextRequest;
5327 }
5328 }
5329 }
5330
5331 if (triggerRemoveFailed) {
5332 SET_ERR("RequestThread: Unable to remove triggers "
5333 "(capture request %d, HAL device: %s (%d)",
5334 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
5335 cleanUpFailedRequests(/*sendRequestError*/ false);
5336 return false;
5337 }
5338
5339 if (res != OK) {
5340 // Should only get a failure here for malformed requests or device-level
5341 // errors, so consider all errors fatal. Bad metadata failures should
5342 // come through notify.
5343 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
5344 mNextRequests[numRequestProcessed].halRequest.frame_number,
5345 strerror(-res), res);
5346 cleanUpFailedRequests(/*sendRequestError*/ false);
5347 return false;
5348 }
5349 return true;
5350}
5351
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005352nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
5353 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
5354 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5355 find_camera_metadata_ro_entry(request,
5356 ANDROID_CONTROL_AE_MODE,
5357 &e);
5358 if (e.count == 0) return maxExpectedDuration;
5359
5360 switch (e.data.u8[0]) {
5361 case ANDROID_CONTROL_AE_MODE_OFF:
5362 find_camera_metadata_ro_entry(request,
5363 ANDROID_SENSOR_EXPOSURE_TIME,
5364 &e);
5365 if (e.count > 0) {
5366 maxExpectedDuration = e.data.i64[0];
5367 }
5368 find_camera_metadata_ro_entry(request,
5369 ANDROID_SENSOR_FRAME_DURATION,
5370 &e);
5371 if (e.count > 0) {
5372 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
5373 }
5374 break;
5375 default:
5376 find_camera_metadata_ro_entry(request,
5377 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
5378 &e);
5379 if (e.count > 1) {
5380 maxExpectedDuration = 1e9 / e.data.u8[0];
5381 }
5382 break;
5383 }
5384
5385 return maxExpectedDuration;
5386}
5387
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005388bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
5389 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
5390 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
5391 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
5392 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
5393 return true;
5394 }
5395
5396 return false;
5397}
5398
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005399void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
5400 // Update the latest request sent to HAL
5401 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
5402 Mutex::Autolock al(mLatestRequestMutex);
5403
5404 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
5405 mLatestRequest.acquire(cloned);
5406
5407 mLatestPhysicalRequest.clear();
5408 for (uint32_t i = 0; i < nextRequest.halRequest.num_physcam_settings; i++) {
5409 cloned = clone_camera_metadata(nextRequest.halRequest.physcam_settings[i]);
5410 mLatestPhysicalRequest.emplace(nextRequest.halRequest.physcam_id[i],
5411 CameraMetadata(cloned));
5412 }
5413
5414 sp<Camera3Device> parent = mParent.promote();
5415 if (parent != NULL) {
5416 parent->monitorMetadata(TagMonitor::REQUEST,
5417 nextRequest.halRequest.frame_number,
5418 0, mLatestRequest, mLatestPhysicalRequest);
5419 }
5420 }
5421
5422 if (nextRequest.halRequest.settings != NULL) {
5423 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
5424 nextRequest.halRequest.settings);
5425 }
5426
5427 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
5428}
5429
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005430bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
5431 ATRACE_CALL();
5432 bool updatesDetected = false;
5433
Emilian Peev4ec17882019-01-24 17:16:58 -08005434 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005435 for (auto tag : mSessionParamKeys) {
5436 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005437 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005438
5439 if (entry.count > 0) {
5440 bool isDifferent = false;
5441 if (lastEntry.count > 0) {
5442 // Have a last value, compare to see if changed
5443 if (lastEntry.type == entry.type &&
5444 lastEntry.count == entry.count) {
5445 // Same type and count, compare values
5446 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
5447 size_t entryBytes = bytesPerValue * lastEntry.count;
5448 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
5449 if (cmp != 0) {
5450 isDifferent = true;
5451 }
5452 } else {
5453 // Count or type has changed
5454 isDifferent = true;
5455 }
5456 } else {
5457 // No last entry, so always consider to be different
5458 isDifferent = true;
5459 }
5460
5461 if (isDifferent) {
5462 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005463 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
5464 updatesDetected = true;
5465 }
Emilian Peev4ec17882019-01-24 17:16:58 -08005466 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005467 }
5468 } else if (lastEntry.count > 0) {
5469 // Value has been removed
5470 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005471 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005472 updatesDetected = true;
5473 }
5474 }
5475
Emilian Peev4ec17882019-01-24 17:16:58 -08005476 bool reconfigureRequired;
5477 if (updatesDetected) {
5478 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
5479 updatedParams);
5480 mLatestSessionParams = updatedParams;
5481 } else {
5482 reconfigureRequired = false;
5483 }
5484
5485 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005486}
5487
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005488bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005489 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005490 status_t res;
5491
5492 // Handle paused state.
5493 if (waitIfPaused()) {
5494 return true;
5495 }
5496
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005497 // Wait for the next batch of requests.
5498 waitForNextRequestBatch();
5499 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005500 return true;
5501 }
5502
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005503 // Get the latest request ID, if any
5504 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005505 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00005506 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005507 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005508 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005509 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005510 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
5511 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005512 }
5513
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005514 // 'mNextRequests' will at this point contain either a set of HFR batched requests
5515 // or a single request from streaming or burst. In either case the first element
5516 // should contain the latest camera settings that we need to check for any session
5517 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00005518 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005519 res = OK;
5520
5521 //Input stream buffers are already acquired at this point so an input stream
5522 //will not be able to move to idle state unless we force it.
5523 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5524 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
5525 if (res != OK) {
5526 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
5527 cleanUpFailedRequests(/*sendRequestError*/ false);
5528 return false;
5529 }
5530 }
5531
5532 if (res == OK) {
5533 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5534 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08005535 sp<Camera3Device> parent = mParent.promote();
5536 if (parent != nullptr) {
5537 parent->pauseStateNotify(true);
5538 }
5539
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005540 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5541
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005542 if (parent != nullptr) {
5543 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
5544 }
5545
5546 statusTracker->markComponentActive(mStatusId);
5547 setPaused(false);
5548 }
5549
5550 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5551 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
5552 if (res != OK) {
5553 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
5554 cleanUpFailedRequests(/*sendRequestError*/ false);
5555 return false;
5556 }
5557 }
5558 }
5559 }
5560
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005561 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005562 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005563 if (res == TIMED_OUT) {
5564 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005565 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005566 // Check if any stream is abandoned.
5567 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005568 return true;
5569 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005570 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005571 return false;
5572 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005573
Zhijun Hecc27e112013-10-03 16:12:43 -07005574 // Inform waitUntilRequestProcessed thread of a new request ID
5575 {
5576 Mutex::Autolock al(mLatestRequestMutex);
5577
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005578 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07005579 mLatestRequestSignal.signal();
5580 }
5581
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005582 // Submit a batch of requests to HAL.
5583 // Use flush lock only when submitting multilple requests in a batch.
5584 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
5585 // which may take a long time to finish so synchronizing flush() and
5586 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
5587 // For now, only synchronize for high speed recording and we should figure something out for
5588 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005589 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07005590
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005591 if (useFlushLock) {
5592 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005593 }
5594
Zhijun Hef0645c12016-08-02 00:58:11 -07005595 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005596 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07005597
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005598 sp<Camera3Device> parent = mParent.promote();
5599 if (parent != nullptr) {
5600 parent->mRequestBufferSM.onSubmittingRequest();
5601 }
5602
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005603 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07005604 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07005605 submitRequestSuccess = sendRequestsBatch();
5606
Shuzhen Wang686f6442017-06-20 16:16:04 -07005607 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
5608 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07005609
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005610 if (useFlushLock) {
5611 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005612 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005613
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005614 // Unset as current request
5615 {
5616 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005617 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005618 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005619 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005620
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005621 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005622}
5623
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005624status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005625 ATRACE_CALL();
5626
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005627 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005628 for (size_t i = 0; i < mNextRequests.size(); i++) {
5629 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005630 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5631 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5632 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5633
5634 // Prepare a request to HAL
5635 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
5636
5637 // Insert any queued triggers (before metadata is locked)
5638 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005639 if (res < 0) {
5640 SET_ERR("RequestThread: Unable to insert triggers "
5641 "(capture request %d, HAL device: %s (%d)",
5642 halRequest->frame_number, strerror(-res), res);
5643 return INVALID_OPERATION;
5644 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005645
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005646 int triggerCount = res;
5647 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
5648 mPrevTriggers = triggerCount;
5649
5650 // If the request is the same as last, or we had triggers last time
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005651 bool newRequest = (mPrevRequest != captureRequest || triggersMixedIn) &&
5652 // Request settings are all the same within one batch, so only treat the first
5653 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07005654 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00005655 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08005656 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005657 /**
5658 * HAL workaround:
5659 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
5660 */
5661 res = addDummyTriggerIds(captureRequest);
5662 if (res != OK) {
5663 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
5664 "(capture request %d, HAL device: %s (%d)",
5665 halRequest->frame_number, strerror(-res), res);
5666 return INVALID_OPERATION;
5667 }
5668
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005669 {
5670 // Correct metadata regions for distortion correction if enabled
5671 sp<Camera3Device> parent = mParent.promote();
5672 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07005673 List<PhysicalCameraSettings>::iterator it;
5674 for (it = captureRequest->mSettingsList.begin();
5675 it != captureRequest->mSettingsList.end(); it++) {
5676 if (parent->mDistortionMappers.find(it->cameraId) ==
5677 parent->mDistortionMappers.end()) {
5678 continue;
5679 }
5680 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
5681 &(it->metadata));
5682 if (res != OK) {
5683 SET_ERR("RequestThread: Unable to correct capture requests "
5684 "for lens distortion for request %d: %s (%d)",
5685 halRequest->frame_number, strerror(-res), res);
5686 return INVALID_OPERATION;
5687 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005688 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08005689
5690 for (it = captureRequest->mSettingsList.begin();
5691 it != captureRequest->mSettingsList.end(); it++) {
5692 if (parent->mZoomRatioMappers.find(it->cameraId) ==
5693 parent->mZoomRatioMappers.end()) {
5694 continue;
5695 }
5696
5697 camera_metadata_entry_t e = it->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
5698 if (e.count > 0 && e.data.f[0] != 1.0f) {
5699 cameraIdsWithZoom.insert(it->cameraId);
5700 }
5701
5702 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
5703 &(it->metadata));
5704 if (res != OK) {
5705 SET_ERR("RequestThread: Unable to correct capture requests "
5706 "for zoom ratio for request %d: %s (%d)",
5707 halRequest->frame_number, strerror(-res), res);
5708 return INVALID_OPERATION;
5709 }
5710 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005711 }
5712 }
5713
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005714 /**
5715 * The request should be presorted so accesses in HAL
5716 * are O(logn). Sidenote, sorting a sorted metadata is nop.
5717 */
Emilian Peevaebbe412018-01-15 13:53:24 +00005718 captureRequest->mSettingsList.begin()->metadata.sort();
5719 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005720 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08005721 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005722 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
5723
5724 IF_ALOGV() {
5725 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5726 find_camera_metadata_ro_entry(
5727 halRequest->settings,
5728 ANDROID_CONTROL_AF_TRIGGER,
5729 &e
5730 );
5731 if (e.count > 0) {
5732 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
5733 __FUNCTION__,
5734 halRequest->frame_number,
5735 e.data.u8[0]);
5736 }
5737 }
5738 } else {
5739 // leave request.settings NULL to indicate 'reuse latest given'
5740 ALOGVV("%s: Request settings are REUSED",
5741 __FUNCTION__);
5742 }
5743
Emilian Peevaebbe412018-01-15 13:53:24 +00005744 if (captureRequest->mSettingsList.size() > 1) {
5745 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
5746 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00005747 if (newRequest) {
5748 halRequest->physcam_settings =
5749 new const camera_metadata* [halRequest->num_physcam_settings];
5750 } else {
5751 halRequest->physcam_settings = nullptr;
5752 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005753 auto it = ++captureRequest->mSettingsList.begin();
5754 size_t i = 0;
5755 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
5756 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00005757 if (newRequest) {
5758 it->metadata.sort();
5759 halRequest->physcam_settings[i] = it->metadata.getAndLock();
5760 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005761 }
5762 }
5763
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005764 uint32_t totalNumBuffers = 0;
5765
5766 // Fill in buffers
5767 if (captureRequest->mInputStream != NULL) {
5768 halRequest->input_buffer = &captureRequest->mInputBuffer;
5769 totalNumBuffers += 1;
5770 } else {
5771 halRequest->input_buffer = NULL;
5772 }
5773
5774 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
5775 captureRequest->mOutputStreams.size());
5776 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005777 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07005778
5779 sp<Camera3Device> parent = mParent.promote();
5780 if (parent == NULL) {
5781 // Should not happen, and nowhere to send errors to, so just log it
5782 CLOGE("RequestThread: Parent is gone");
5783 return INVALID_OPERATION;
5784 }
5785 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
5786
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005787 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005788 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005789 sp<Camera3OutputStreamInterface> outputStream =
5790 captureRequest->mOutputStreams.editItemAt(j);
5791 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005792
5793 // Prepare video buffers for high speed recording on the first video request.
5794 if (mPrepareVideoStream && outputStream->isVideoStream()) {
5795 // Only try to prepare video stream on the first video request.
5796 mPrepareVideoStream = false;
5797
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005798 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
5799 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005800 while (res == NOT_ENOUGH_DATA) {
5801 res = outputStream->prepareNextBuffer();
5802 }
5803 if (res != OK) {
5804 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
5805 __FUNCTION__, strerror(-res), res);
5806 outputStream->cancelPrepare();
5807 }
5808 }
5809
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005810 std::vector<size_t> uniqueSurfaceIds;
5811 res = outputStream->getUniqueSurfaceIds(
5812 captureRequest->mOutputSurfaces[streamId],
5813 &uniqueSurfaceIds);
5814 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
5815 if (res != OK && res != INVALID_OPERATION) {
5816 ALOGE("%s: failed to query stream %d unique surface IDs",
5817 __FUNCTION__, streamId);
5818 return res;
5819 }
5820 if (res == OK) {
5821 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
5822 }
5823
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005824 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005825 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005826 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005827 return TIMED_OUT;
5828 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005829 // HAL will request buffer through requestStreamBuffer API
5830 camera3_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
5831 buffer.stream = outputStream->asHalStream();
5832 buffer.buffer = nullptr;
5833 buffer.status = CAMERA3_BUFFER_STATUS_OK;
5834 buffer.acquire_fence = -1;
5835 buffer.release_fence = -1;
5836 } else {
5837 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
5838 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005839 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005840 if (res != OK) {
5841 // Can't get output buffer from gralloc queue - this could be due to
5842 // abandoned queue or other consumer misbehavior, so not a fatal
5843 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005844 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005845 " %s (%d)", strerror(-res), res);
5846
5847 return TIMED_OUT;
5848 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005849 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08005850
5851 {
5852 sp<Camera3Device> parent = mParent.promote();
5853 if (parent != nullptr) {
5854 const String8& streamCameraId = outputStream->getPhysicalCameraId();
5855 for (const auto& settings : captureRequest->mSettingsList) {
5856 if ((streamCameraId.isEmpty() &&
5857 parent->getId() == settings.cameraId.c_str()) ||
5858 streamCameraId == settings.cameraId.c_str()) {
5859 outputStream->fireBufferRequestForFrameNumber(
5860 captureRequest->mResultExtras.frameNumber,
5861 settings.metadata);
5862 }
5863 }
5864 }
5865 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07005866
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005867 String8 physicalCameraId = outputStream->getPhysicalCameraId();
5868
5869 if (!physicalCameraId.isEmpty()) {
5870 // Physical stream isn't supported for input request.
5871 if (halRequest->input_buffer) {
5872 CLOGE("Physical stream is not supported for input request");
5873 return INVALID_OPERATION;
5874 }
5875 requestedPhysicalCameras.insert(physicalCameraId);
5876 }
5877 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005878 }
5879 totalNumBuffers += halRequest->num_output_buffers;
5880
5881 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08005882 // If this request list is for constrained high speed recording (not
5883 // preview), and the current request is not the last one in the batch,
5884 // do not send callback to the app.
5885 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005886 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08005887 hasCallback = false;
5888 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005889 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005890 bool isZslCapture = false;
Emilian Peev9dd21f42018-08-03 13:39:29 +01005891 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
5892 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5893 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
5894 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
5895 isStillCapture = true;
5896 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
5897 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005898
5899 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
5900 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
5901 isZslCapture = true;
5902 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005903 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005904 res = parent->registerInFlight(halRequest->frame_number,
5905 totalNumBuffers, captureRequest->mResultExtras,
5906 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005907 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005908 calculateMaxExpectedDuration(halRequest->settings),
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08005909 requestedPhysicalCameras, isStillCapture, isZslCapture, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005910 (mUseHalBufManager) ? uniqueSurfaceIdMap :
5911 SurfaceMap{});
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005912 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
5913 ", burstId = %" PRId32 ".",
5914 __FUNCTION__,
5915 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
5916 captureRequest->mResultExtras.burstId);
5917 if (res != OK) {
5918 SET_ERR("RequestThread: Unable to register new in-flight request:"
5919 " %s (%d)", strerror(-res), res);
5920 return INVALID_OPERATION;
5921 }
5922 }
5923
5924 return OK;
5925}
5926
Igor Murashkin1e479c02013-09-06 16:55:14 -07005927CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005928 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005929 Mutex::Autolock al(mLatestRequestMutex);
5930
5931 ALOGV("RequestThread::%s", __FUNCTION__);
5932
5933 return mLatestRequest;
5934}
5935
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005936bool Camera3Device::RequestThread::isStreamPending(
5937 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005938 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005939 Mutex::Autolock l(mRequestLock);
5940
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005941 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005942 if (!nextRequest.submitted) {
5943 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5944 if (stream == s) return true;
5945 }
5946 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005947 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005948 }
5949
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005950 for (const auto& request : mRequestQueue) {
5951 for (const auto& s : request->mOutputStreams) {
5952 if (stream == s) return true;
5953 }
5954 if (stream == request->mInputStream) return true;
5955 }
5956
5957 for (const auto& request : mRepeatingRequests) {
5958 for (const auto& s : request->mOutputStreams) {
5959 if (stream == s) return true;
5960 }
5961 if (stream == request->mInputStream) return true;
5962 }
5963
5964 return false;
5965}
Jianing Weicb0652e2014-03-12 18:29:36 -07005966
Emilian Peev40ead602017-09-26 15:46:36 +01005967bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5968 ATRACE_CALL();
5969 Mutex::Autolock l(mRequestLock);
5970
5971 for (const auto& nextRequest : mNextRequests) {
5972 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5973 if (s.first == streamId) {
5974 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5975 if (it != s.second.end()) {
5976 return true;
5977 }
5978 }
5979 }
5980 }
5981
5982 for (const auto& request : mRequestQueue) {
5983 for (const auto& s : request->mOutputSurfaces) {
5984 if (s.first == streamId) {
5985 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5986 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005987 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005988 }
5989 }
5990 }
5991 }
5992
5993 for (const auto& request : mRepeatingRequests) {
5994 for (const auto& s : request->mOutputSurfaces) {
5995 if (s.first == streamId) {
5996 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5997 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005998 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005999 }
6000 }
6001 }
6002 }
6003
6004 return false;
6005}
6006
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006007void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
6008 if (!mUseHalBufManager) {
6009 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
6010 return;
6011 }
6012
6013 Mutex::Autolock pl(mPauseLock);
6014 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006015 mInterface->signalPipelineDrain(streamIds);
6016 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006017 }
6018 // If request thread is still busy, wait until paused then notify HAL
6019 mNotifyPipelineDrain = true;
6020 mStreamIdsToBeDrained = streamIds;
6021}
6022
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006023status_t Camera3Device::RequestThread::switchToOffline(
6024 const std::vector<int32_t>& streamsToKeep,
6025 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
6026 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession) {
6027 Mutex::Autolock l(mRequestLock);
6028 clearRepeatingRequestsLocked(/*lastFrameNumber*/nullptr);
6029
6030 // Theoretically we should also check for mRepeatingRequests.empty(), but the API interface
6031 // is serialized by mInterfaceLock so skip that check.
6032 bool queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
6033 while (!queueEmpty) {
6034 status_t res = mRequestSubmittedSignal.waitRelative(mRequestLock, kRequestSubmitTimeout);
6035 if (res == TIMED_OUT) {
6036 ALOGE("%s: request thread failed to submit a request within timeout!", __FUNCTION__);
6037 return res;
6038 } else if (res != OK) {
6039 ALOGE("%s: request thread failed to submit a request: %s (%d)!",
6040 __FUNCTION__, strerror(-res), res);
6041 return res;
6042 }
6043 queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
6044 }
6045 return mInterface->switchToOffline(
6046 streamsToKeep, offlineSessionInfo, offlineSession);
6047}
6048
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07006049nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006050 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07006051 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07006052 return mExpectedInflightDuration > kMinInflightDuration ?
6053 mExpectedInflightDuration : kMinInflightDuration;
6054}
6055
Emilian Peevaebbe412018-01-15 13:53:24 +00006056void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
6057 camera3_capture_request_t *halRequest) {
6058 if ((request == nullptr) || (halRequest == nullptr)) {
6059 ALOGE("%s: Invalid request!", __FUNCTION__);
6060 return;
6061 }
6062
6063 if (halRequest->num_physcam_settings > 0) {
6064 if (halRequest->physcam_id != nullptr) {
6065 delete [] halRequest->physcam_id;
6066 halRequest->physcam_id = nullptr;
6067 }
6068 if (halRequest->physcam_settings != nullptr) {
6069 auto it = ++(request->mSettingsList.begin());
6070 size_t i = 0;
6071 for (; it != request->mSettingsList.end(); it++, i++) {
6072 it->metadata.unlock(halRequest->physcam_settings[i]);
6073 }
6074 delete [] halRequest->physcam_settings;
6075 halRequest->physcam_settings = nullptr;
6076 }
6077 }
6078}
6079
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006080void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
6081 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006082 return;
6083 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006084
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006085 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006086 // Skip the ones that have been submitted successfully.
6087 if (nextRequest.submitted) {
6088 continue;
6089 }
6090
6091 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
6092 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
6093 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
6094
6095 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00006096 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006097 }
6098
Emilian Peevaebbe412018-01-15 13:53:24 +00006099 cleanupPhysicalSettings(captureRequest, halRequest);
6100
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006101 if (captureRequest->mInputStream != NULL) {
6102 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
6103 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
6104 }
6105
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08006106 // No output buffer can be returned when using HAL buffer manager
6107 if (!mUseHalBufManager) {
6108 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
6109 //Buffers that failed processing could still have
6110 //valid acquire fence.
6111 int acquireFence = (*outputBuffers)[i].acquire_fence;
6112 if (0 <= acquireFence) {
6113 close(acquireFence);
6114 outputBuffers->editItemAt(i).acquire_fence = -1;
6115 }
6116 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
6117 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
6118 /*timestampIncreasing*/true, std::vector<size_t> (),
6119 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01006120 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006121 }
6122
6123 if (sendRequestError) {
6124 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006125 sp<NotificationListener> listener = mListener.promote();
6126 if (listener != NULL) {
6127 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08006128 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006129 captureRequest->mResultExtras);
6130 }
6131 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07006132
6133 // Remove yet-to-be submitted inflight request from inflightMap
6134 {
6135 sp<Camera3Device> parent = mParent.promote();
6136 if (parent != NULL) {
6137 Mutex::Autolock l(parent->mInFlightLock);
6138 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
6139 if (idx >= 0) {
6140 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
6141 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
6142 parent->removeInFlightMapEntryLocked(idx);
6143 }
6144 }
6145 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006146 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07006147
6148 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006149 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006150}
6151
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006152void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006153 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006154 // Optimized a bit for the simple steady-state case (single repeating
6155 // request), to avoid putting that request in the queue temporarily.
6156 Mutex::Autolock l(mRequestLock);
6157
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006158 assert(mNextRequests.empty());
6159
6160 NextRequest nextRequest;
6161 nextRequest.captureRequest = waitForNextRequestLocked();
6162 if (nextRequest.captureRequest == nullptr) {
6163 return;
6164 }
6165
6166 nextRequest.halRequest = camera3_capture_request_t();
6167 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006168 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006169
6170 // Wait for additional requests
6171 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
6172
6173 for (size_t i = 1; i < batchSize; i++) {
6174 NextRequest additionalRequest;
6175 additionalRequest.captureRequest = waitForNextRequestLocked();
6176 if (additionalRequest.captureRequest == nullptr) {
6177 break;
6178 }
6179
6180 additionalRequest.halRequest = camera3_capture_request_t();
6181 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006182 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006183 }
6184
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006185 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006186 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07006187 mNextRequests.size(), batchSize);
6188 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07006189 }
6190
6191 return;
6192}
6193
6194sp<Camera3Device::CaptureRequest>
6195 Camera3Device::RequestThread::waitForNextRequestLocked() {
6196 status_t res;
6197 sp<CaptureRequest> nextRequest;
6198
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006199 while (mRequestQueue.empty()) {
6200 if (!mRepeatingRequests.empty()) {
6201 // Always atomically enqueue all requests in a repeating request
6202 // list. Guarantees a complete in-sequence set of captures to
6203 // application.
6204 const RequestList &requests = mRepeatingRequests;
6205 RequestList::const_iterator firstRequest =
6206 requests.begin();
6207 nextRequest = *firstRequest;
6208 mRequestQueue.insert(mRequestQueue.end(),
6209 ++firstRequest,
6210 requests.end());
6211 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07006212
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006213 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07006214
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006215 break;
6216 }
6217
6218 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
6219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006220 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
6221 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006222 Mutex::Autolock pl(mPauseLock);
6223 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006224 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006225 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006226 if (mNotifyPipelineDrain) {
6227 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6228 mNotifyPipelineDrain = false;
6229 mStreamIdsToBeDrained.clear();
6230 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006231 // Let the tracker know
6232 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6233 if (statusTracker != 0) {
6234 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6235 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006236 sp<Camera3Device> parent = mParent.promote();
6237 if (parent != nullptr) {
6238 parent->mRequestBufferSM.onRequestThreadPaused();
6239 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006240 }
6241 // Stop waiting for now and let thread management happen
6242 return NULL;
6243 }
6244 }
6245
6246 if (nextRequest == NULL) {
6247 // Don't have a repeating request already in hand, so queue
6248 // must have an entry now.
6249 RequestList::iterator firstRequest =
6250 mRequestQueue.begin();
6251 nextRequest = *firstRequest;
6252 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07006253 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
6254 sp<NotificationListener> listener = mListener.promote();
6255 if (listener != NULL) {
6256 listener->notifyRequestQueueEmpty();
6257 }
6258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006259 }
6260
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006261 // In case we've been unpaused by setPaused clearing mDoPause, need to
6262 // update internal pause state (capture/setRepeatingRequest unpause
6263 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006264 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006265 if (mPaused) {
6266 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
6267 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6268 if (statusTracker != 0) {
6269 statusTracker->markComponentActive(mStatusId);
6270 }
6271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006272 mPaused = false;
6273
6274 // Check if we've reconfigured since last time, and reset the preview
6275 // request if so. Can't use 'NULL request == repeat' across configure calls.
6276 if (mReconfigured) {
6277 mPrevRequest.clear();
6278 mReconfigured = false;
6279 }
6280
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006281 if (nextRequest != NULL) {
6282 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006283 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
6284 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006285
6286 // Since RequestThread::clear() removes buffers from the input stream,
6287 // get the right buffer here before unlocking mRequestLock
6288 if (nextRequest->mInputStream != NULL) {
6289 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
6290 if (res != OK) {
6291 // Can't get input buffer from gralloc queue - this could be due to
6292 // disconnected queue or other producer misbehavior, so not a fatal
6293 // error
6294 ALOGE("%s: Can't get input buffer, skipping request:"
6295 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006296
6297 sp<NotificationListener> listener = mListener.promote();
6298 if (listener != NULL) {
6299 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08006300 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006301 nextRequest->mResultExtras);
6302 }
6303 return NULL;
6304 }
6305 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006306 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07006307
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006308 return nextRequest;
6309}
6310
6311bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006312 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006313 status_t res;
6314 Mutex::Autolock l(mPauseLock);
6315 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006316 if (mPaused == false) {
6317 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006318 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006319 if (mNotifyPipelineDrain) {
6320 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6321 mNotifyPipelineDrain = false;
6322 mStreamIdsToBeDrained.clear();
6323 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006324 // Let the tracker know
6325 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6326 if (statusTracker != 0) {
6327 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6328 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006329 sp<Camera3Device> parent = mParent.promote();
6330 if (parent != nullptr) {
6331 parent->mRequestBufferSM.onRequestThreadPaused();
6332 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006333 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006334
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006335 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006336 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006337 return true;
6338 }
6339 }
6340 // We don't set mPaused to false here, because waitForNextRequest needs
6341 // to further manage the paused state in case of starvation.
6342 return false;
6343}
6344
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006345void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006346 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006347 // With work to do, mark thread as unpaused.
6348 // If paused by request (setPaused), don't resume, to avoid
6349 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006350 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006351 Mutex::Autolock p(mPauseLock);
6352 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006353 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
6354 if (mPaused) {
6355 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6356 if (statusTracker != 0) {
6357 statusTracker->markComponentActive(mStatusId);
6358 }
6359 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006360 mPaused = false;
6361 }
6362}
6363
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07006364void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
6365 sp<Camera3Device> parent = mParent.promote();
6366 if (parent != NULL) {
6367 va_list args;
6368 va_start(args, fmt);
6369
6370 parent->setErrorStateV(fmt, args);
6371
6372 va_end(args);
6373 }
6374}
6375
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006376status_t Camera3Device::RequestThread::insertTriggers(
6377 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006378 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006379 Mutex::Autolock al(mTriggerMutex);
6380
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006381 sp<Camera3Device> parent = mParent.promote();
6382 if (parent == NULL) {
6383 CLOGE("RequestThread: Parent is gone");
6384 return DEAD_OBJECT;
6385 }
6386
Emilian Peevaebbe412018-01-15 13:53:24 +00006387 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006388 size_t count = mTriggerMap.size();
6389
6390 for (size_t i = 0; i < count; ++i) {
6391 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006392 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006393
6394 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
6395 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
6396 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006397 if (isAeTrigger) {
6398 request->mResultExtras.precaptureTriggerId = triggerId;
6399 mCurrentPreCaptureTriggerId = triggerId;
6400 } else {
6401 request->mResultExtras.afTriggerId = triggerId;
6402 mCurrentAfTriggerId = triggerId;
6403 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01006404 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006405 }
6406
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006407 camera_metadata_entry entry = metadata.find(tag);
6408
6409 if (entry.count > 0) {
6410 /**
6411 * Already has an entry for this trigger in the request.
6412 * Rewrite it with our requested trigger value.
6413 */
6414 RequestTrigger oldTrigger = trigger;
6415
6416 oldTrigger.entryValue = entry.data.u8[0];
6417
6418 mTriggerReplacedMap.add(tag, oldTrigger);
6419 } else {
6420 /**
6421 * More typical, no trigger entry, so we just add it
6422 */
6423 mTriggerRemovedMap.add(tag, trigger);
6424 }
6425
6426 status_t res;
6427
6428 switch (trigger.getTagType()) {
6429 case TYPE_BYTE: {
6430 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6431 res = metadata.update(tag,
6432 &entryValue,
6433 /*count*/1);
6434 break;
6435 }
6436 case TYPE_INT32:
6437 res = metadata.update(tag,
6438 &trigger.entryValue,
6439 /*count*/1);
6440 break;
6441 default:
6442 ALOGE("%s: Type not supported: 0x%x",
6443 __FUNCTION__,
6444 trigger.getTagType());
6445 return INVALID_OPERATION;
6446 }
6447
6448 if (res != OK) {
6449 ALOGE("%s: Failed to update request metadata with trigger tag %s"
6450 ", value %d", __FUNCTION__, trigger.getTagName(),
6451 trigger.entryValue);
6452 return res;
6453 }
6454
6455 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
6456 trigger.getTagName(),
6457 trigger.entryValue);
6458 }
6459
6460 mTriggerMap.clear();
6461
6462 return count;
6463}
6464
6465status_t Camera3Device::RequestThread::removeTriggers(
6466 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006467 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006468 Mutex::Autolock al(mTriggerMutex);
6469
Emilian Peevaebbe412018-01-15 13:53:24 +00006470 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006471
6472 /**
6473 * Replace all old entries with their old values.
6474 */
6475 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
6476 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
6477
6478 status_t res;
6479
6480 uint32_t tag = trigger.metadataTag;
6481 switch (trigger.getTagType()) {
6482 case TYPE_BYTE: {
6483 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6484 res = metadata.update(tag,
6485 &entryValue,
6486 /*count*/1);
6487 break;
6488 }
6489 case TYPE_INT32:
6490 res = metadata.update(tag,
6491 &trigger.entryValue,
6492 /*count*/1);
6493 break;
6494 default:
6495 ALOGE("%s: Type not supported: 0x%x",
6496 __FUNCTION__,
6497 trigger.getTagType());
6498 return INVALID_OPERATION;
6499 }
6500
6501 if (res != OK) {
6502 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
6503 ", trigger value %d", __FUNCTION__,
6504 trigger.getTagName(), trigger.entryValue);
6505 return res;
6506 }
6507 }
6508 mTriggerReplacedMap.clear();
6509
6510 /**
6511 * Remove all new entries.
6512 */
6513 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
6514 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
6515 status_t res = metadata.erase(trigger.metadataTag);
6516
6517 if (res != OK) {
6518 ALOGE("%s: Failed to erase metadata with trigger tag %s"
6519 ", trigger value %d", __FUNCTION__,
6520 trigger.getTagName(), trigger.entryValue);
6521 return res;
6522 }
6523 }
6524 mTriggerRemovedMap.clear();
6525
6526 return OK;
6527}
6528
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006529status_t Camera3Device::RequestThread::addDummyTriggerIds(
6530 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006531 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006532 static const int32_t dummyTriggerId = 1;
6533 status_t res;
6534
Emilian Peevaebbe412018-01-15 13:53:24 +00006535 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006536
6537 // If AF trigger is active, insert a dummy AF trigger ID if none already
6538 // exists
6539 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
6540 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
6541 if (afTrigger.count > 0 &&
6542 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
6543 afId.count == 0) {
6544 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
6545 if (res != OK) return res;
6546 }
6547
6548 // If AE precapture trigger is active, insert a dummy precapture trigger ID
6549 // if none already exists
6550 camera_metadata_entry pcTrigger =
6551 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
6552 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
6553 if (pcTrigger.count > 0 &&
6554 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
6555 pcId.count == 0) {
6556 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
6557 &dummyTriggerId, 1);
6558 if (res != OK) return res;
6559 }
6560
6561 return OK;
6562}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006563
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006564/**
6565 * PreparerThread inner class methods
6566 */
6567
6568Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07006569 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006570 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006571}
6572
6573Camera3Device::PreparerThread::~PreparerThread() {
6574 Thread::requestExitAndWait();
6575 if (mCurrentStream != nullptr) {
6576 mCurrentStream->cancelPrepare();
6577 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6578 mCurrentStream.clear();
6579 }
6580 clear();
6581}
6582
Ruben Brunkc78ac262015-08-13 17:58:46 -07006583status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006584 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006585 status_t res;
6586
6587 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006588 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006589
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006590 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006591 if (res == OK) {
6592 // No preparation needed, fire listener right off
6593 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006594 if (listener != NULL) {
6595 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006596 }
6597 return OK;
6598 } else if (res != NOT_ENOUGH_DATA) {
6599 return res;
6600 }
6601
6602 // Need to prepare, start up thread if necessary
6603 if (!mActive) {
6604 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
6605 // isn't running
6606 Thread::requestExitAndWait();
6607 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6608 if (res != OK) {
6609 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006610 if (listener != NULL) {
6611 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006612 }
6613 return res;
6614 }
6615 mCancelNow = false;
6616 mActive = true;
6617 ALOGV("%s: Preparer stream started", __FUNCTION__);
6618 }
6619
6620 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006621 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006622 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
6623
6624 return OK;
6625}
6626
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006627void Camera3Device::PreparerThread::pause() {
6628 ATRACE_CALL();
6629
6630 Mutex::Autolock l(mLock);
6631
6632 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
6633 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
6634 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
6635 int currentMaxCount = mCurrentMaxCount;
6636 mPendingStreams.clear();
6637 mCancelNow = true;
6638 while (mActive) {
6639 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
6640 if (res == TIMED_OUT) {
6641 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
6642 return;
6643 } else if (res != OK) {
6644 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
6645 return;
6646 }
6647 }
6648
6649 //Check whether the prepare thread was able to complete the current
6650 //stream. In case work is still pending emplace it along with the rest
6651 //of the streams in the pending list.
6652 if (currentStream != nullptr) {
6653 if (!mCurrentPrepareComplete) {
6654 pendingStreams.emplace(currentMaxCount, currentStream);
6655 }
6656 }
6657
6658 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
6659 for (const auto& it : mPendingStreams) {
6660 it.second->cancelPrepare();
6661 }
6662}
6663
6664status_t Camera3Device::PreparerThread::resume() {
6665 ATRACE_CALL();
6666 status_t res;
6667
6668 Mutex::Autolock l(mLock);
6669 sp<NotificationListener> listener = mListener.promote();
6670
6671 if (mActive) {
6672 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
6673 return NO_INIT;
6674 }
6675
6676 auto it = mPendingStreams.begin();
6677 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006678 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006679 if (res == OK) {
6680 if (listener != NULL) {
6681 listener->notifyPrepared(it->second->getId());
6682 }
6683 it = mPendingStreams.erase(it);
6684 } else if (res != NOT_ENOUGH_DATA) {
6685 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
6686 res, strerror(-res));
6687 it = mPendingStreams.erase(it);
6688 } else {
6689 it++;
6690 }
6691 }
6692
6693 if (mPendingStreams.empty()) {
6694 return OK;
6695 }
6696
6697 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6698 if (res != OK) {
6699 ALOGE("%s: Unable to start preparer stream: %d (%s)",
6700 __FUNCTION__, res, strerror(-res));
6701 return res;
6702 }
6703 mCancelNow = false;
6704 mActive = true;
6705 ALOGV("%s: Preparer stream started", __FUNCTION__);
6706
6707 return OK;
6708}
6709
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006710status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006711 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006712 Mutex::Autolock l(mLock);
6713
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006714 for (const auto& it : mPendingStreams) {
6715 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006716 }
6717 mPendingStreams.clear();
6718 mCancelNow = true;
6719
6720 return OK;
6721}
6722
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006723void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006724 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006725 Mutex::Autolock l(mLock);
6726 mListener = listener;
6727}
6728
6729bool Camera3Device::PreparerThread::threadLoop() {
6730 status_t res;
6731 {
6732 Mutex::Autolock l(mLock);
6733 if (mCurrentStream == nullptr) {
6734 // End thread if done with work
6735 if (mPendingStreams.empty()) {
6736 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
6737 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
6738 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
6739 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006740 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006741 return false;
6742 }
6743
6744 // Get next stream to prepare
6745 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006746 mCurrentStream = it->second;
6747 mCurrentMaxCount = it->first;
6748 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006749 mPendingStreams.erase(it);
6750 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
6751 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
6752 } else if (mCancelNow) {
6753 mCurrentStream->cancelPrepare();
6754 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6755 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
6756 mCurrentStream.clear();
6757 mCancelNow = false;
6758 return true;
6759 }
6760 }
6761
6762 res = mCurrentStream->prepareNextBuffer();
6763 if (res == NOT_ENOUGH_DATA) return true;
6764 if (res != OK) {
6765 // Something bad happened; try to recover by cancelling prepare and
6766 // signalling listener anyway
6767 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
6768 mCurrentStream->getId(), res, strerror(-res));
6769 mCurrentStream->cancelPrepare();
6770 }
6771
6772 // This stream has finished, notify listener
6773 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006774 sp<NotificationListener> listener = mListener.promote();
6775 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006776 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
6777 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006778 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006779 }
6780
6781 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6782 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006783 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006784
6785 return true;
6786}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006787
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006788status_t Camera3Device::RequestBufferStateMachine::initialize(
6789 sp<camera3::StatusTracker> statusTracker) {
6790 if (statusTracker == nullptr) {
6791 ALOGE("%s: statusTracker is null", __FUNCTION__);
6792 return BAD_VALUE;
6793 }
6794
6795 std::lock_guard<std::mutex> lock(mLock);
6796 mStatusTracker = statusTracker;
6797 mRequestBufferStatusId = statusTracker->addComponent();
6798 return OK;
6799}
6800
6801bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
6802 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006803 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006804 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006805 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006806 return true;
6807 }
6808 return false;
6809}
6810
6811void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
6812 std::lock_guard<std::mutex> lock(mLock);
6813 if (!mRequestBufferOngoing) {
6814 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
6815 return;
6816 }
6817 mRequestBufferOngoing = false;
6818 if (mStatus == RB_STATUS_PENDING_STOP) {
6819 checkSwitchToStopLocked();
6820 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006821 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006822}
6823
6824void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
6825 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006826 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006827 return;
6828}
6829
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006830void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006831 std::lock_guard<std::mutex> lock(mLock);
6832 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006833 // inflight map register actually happens in prepareHalRequest now, but it is close enough
6834 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006835 mInflightMapEmpty = false;
6836 if (mStatus == RB_STATUS_STOPPED) {
6837 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006838 }
6839 return;
6840}
6841
6842void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
6843 std::lock_guard<std::mutex> lock(mLock);
6844 mRequestThreadPaused = true;
6845 if (mStatus == RB_STATUS_PENDING_STOP) {
6846 checkSwitchToStopLocked();
6847 }
6848 return;
6849}
6850
6851void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
6852 std::lock_guard<std::mutex> lock(mLock);
6853 mInflightMapEmpty = true;
6854 if (mStatus == RB_STATUS_PENDING_STOP) {
6855 checkSwitchToStopLocked();
6856 }
6857 return;
6858}
6859
6860void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6861 std::lock_guard<std::mutex> lock(mLock);
6862 if (!checkSwitchToStopLocked()) {
6863 mStatus = RB_STATUS_PENDING_STOP;
6864 }
6865 return;
6866}
6867
6868void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6869 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6870 if (statusTracker != nullptr) {
6871 if (active) {
6872 statusTracker->markComponentActive(mRequestBufferStatusId);
6873 } else {
6874 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6875 }
6876 }
6877}
6878
6879bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6880 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6881 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006882 return true;
6883 }
6884 return false;
6885}
6886
Shuzhen Wang268a1362018-10-16 16:32:59 -07006887status_t Camera3Device::fixupMonochromeTags(const CameraMetadata& deviceInfo,
6888 CameraMetadata& resultMetadata) {
6889 status_t res = OK;
6890 if (!mNeedFixupMonochromeTags) {
6891 return res;
6892 }
6893
6894 // Remove tags that are not applicable to monochrome camera.
6895 int32_t tagsToRemove[] = {
6896 ANDROID_SENSOR_GREEN_SPLIT,
6897 ANDROID_SENSOR_NEUTRAL_COLOR_POINT,
6898 ANDROID_COLOR_CORRECTION_MODE,
6899 ANDROID_COLOR_CORRECTION_TRANSFORM,
6900 ANDROID_COLOR_CORRECTION_GAINS,
6901 };
6902 for (auto tag : tagsToRemove) {
6903 res = resultMetadata.erase(tag);
6904 if (res != OK) {
6905 ALOGE("%s: Failed to remove tag %d for monochrome camera", __FUNCTION__, tag);
6906 return res;
6907 }
6908 }
6909
6910 // ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL
6911 camera_metadata_entry blEntry = resultMetadata.find(ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL);
6912 for (size_t i = 1; i < blEntry.count; i++) {
6913 blEntry.data.f[i] = blEntry.data.f[0];
6914 }
6915
6916 // ANDROID_SENSOR_NOISE_PROFILE
6917 camera_metadata_entry npEntry = resultMetadata.find(ANDROID_SENSOR_NOISE_PROFILE);
6918 if (npEntry.count > 0 && npEntry.count % 2 == 0) {
6919 double np[] = {npEntry.data.d[0], npEntry.data.d[1]};
6920 res = resultMetadata.update(ANDROID_SENSOR_NOISE_PROFILE, np, 2);
6921 if (res != OK) {
6922 ALOGE("%s: Failed to update SENSOR_NOISE_PROFILE: %s (%d)",
6923 __FUNCTION__, strerror(-res), res);
6924 return res;
6925 }
6926 }
6927
6928 // ANDROID_STATISTICS_LENS_SHADING_MAP
6929 camera_metadata_ro_entry lsSizeEntry = deviceInfo.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
6930 camera_metadata_entry lsEntry = resultMetadata.find(ANDROID_STATISTICS_LENS_SHADING_MAP);
6931 if (lsSizeEntry.count == 2 && lsEntry.count > 0
6932 && (int32_t)lsEntry.count == 4 * lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]) {
6933 for (int32_t i = 0; i < lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]; i++) {
6934 lsEntry.data.f[4*i+1] = lsEntry.data.f[4*i];
6935 lsEntry.data.f[4*i+2] = lsEntry.data.f[4*i];
6936 lsEntry.data.f[4*i+3] = lsEntry.data.f[4*i];
6937 }
6938 }
6939
6940 // ANDROID_TONEMAP_CURVE_BLUE
6941 // ANDROID_TONEMAP_CURVE_GREEN
6942 // ANDROID_TONEMAP_CURVE_RED
6943 camera_metadata_entry tcbEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_BLUE);
6944 camera_metadata_entry tcgEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_GREEN);
6945 camera_metadata_entry tcrEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_RED);
6946 if (tcbEntry.count > 0
6947 && tcbEntry.count == tcgEntry.count
6948 && tcbEntry.count == tcrEntry.count) {
6949 for (size_t i = 0; i < tcbEntry.count; i++) {
6950 tcbEntry.data.f[i] = tcrEntry.data.f[i];
6951 tcgEntry.data.f[i] = tcrEntry.data.f[i];
6952 }
6953 }
6954
6955 return res;
6956}
6957
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006958status_t Camera3Device::switchToOffline(
6959 const std::vector<int32_t>& streamsToKeep,
6960 /*out*/ sp<CameraOfflineSessionBase>* session) {
6961 ATRACE_CALL();
6962 if (session == nullptr) {
6963 ALOGE("%s: session must not be null", __FUNCTION__);
6964 return BAD_VALUE;
6965 }
6966
6967 Mutex::Autolock il(mInterfaceLock);
6968 Mutex::Autolock l(mLock);
6969
6970 // 1. Stop repeating request, wait until request thread submitted all requests, then
6971 // call HAL switchToOffline
6972 hardware::camera::device::V3_6::CameraOfflineSessionInfo offlineSessionInfo;
6973 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession;
6974
6975 mRequestThread->switchToOffline(streamsToKeep, &offlineSessionInfo, &offlineSession);
6976
6977
6978 // 2. Verify offlineSessionInfo
6979 // 3. create Camera3OfflineSession and transfer object ownership
6980 // (streams, inflight requests, buffer caches)
6981 *session = new Camera3OfflineSession(mId);
6982
6983 // 4. Delete unneeded streams
6984 // 5. Verify Camera3Device is in unconfigured state
6985 return OK;
6986}
6987
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006988}; // namespace android