blob: 306f9b65b79b1c5c763e646eb888369ccc0265c1 [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
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Shuzhen Wang5c22c152017-12-31 17:12:25 -080042#include <utility>
43
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080044#include <utils/Log.h>
45#include <utils/Trace.h>
46#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070047#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070048
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049#include <android/hardware/camera2/ICameraDeviceUser.h>
50
Igor Murashkinff3e31d2013-10-23 16:40:06 -070051#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070052#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070053#include "device3/Camera3Device.h"
54#include "device3/Camera3OutputStream.h"
55#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070056#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070057#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070058#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070059#include "utils/CameraThreadState.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080061#include <tuple>
62
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080063using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080064using namespace android::hardware::camera;
65using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080066
67namespace android {
68
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080069Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080070 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080071 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070072 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070073 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070074 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070075 mUsePartialResult(false),
76 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080077 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070078 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070079 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070080 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070081 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000082 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010083 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070084 mLastTemplateId(-1),
85 mNeedFixupMonochromeTags(false)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086{
87 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080088 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089}
90
91Camera3Device::~Camera3Device()
92{
93 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080094 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -070095 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096}
97
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080098const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080099 return mId;
100}
101
Emilian Peevbd8c5032018-02-14 23:05:40 +0000102status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800103 ATRACE_CALL();
104 Mutex::Autolock il(mInterfaceLock);
105 Mutex::Autolock l(mLock);
106
107 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
108 if (mStatus != STATUS_UNINITIALIZED) {
109 CLOGE("Already initialized!");
110 return INVALID_OPERATION;
111 }
112 if (manager == nullptr) return INVALID_OPERATION;
113
114 sp<ICameraDeviceSession> session;
115 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800116 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800117 /*out*/ &session);
118 ATRACE_END();
119 if (res != OK) {
120 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
121 return res;
122 }
123
Steven Moreland5ff9c912017-03-09 23:13:00 -0800124 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800125 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700126 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800127 session->close();
128 return res;
129 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800130
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700131 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700132 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700133 if (isLogical) {
134 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700135 res = manager->getCameraCharacteristics(
136 physicalId, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700137 if (res != OK) {
138 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
139 physicalId.c_str(), strerror(-res), res);
140 session->close();
141 return res;
142 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700143
144 if (DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId])) {
145 mDistortionMappers[physicalId].setupStaticInfo(mPhysicalDeviceInfoMap[physicalId]);
146 if (res != OK) {
147 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
148 "correction", physicalId.c_str());
149 session->close();
150 return res;
151 }
152 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700153 }
154 }
155
Yifan Hongf79b5542017-04-11 14:44:25 -0700156 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700157 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
158 [&queue](const auto& descriptor) {
159 queue = std::make_shared<RequestMetadataQueue>(descriptor);
160 if (!queue->isValid() || queue->availableToWrite() <= 0) {
161 ALOGE("HAL returns empty request metadata fmq, not use it");
162 queue = nullptr;
163 // don't use the queue onwards.
164 }
165 });
166 if (!requestQueueRet.isOk()) {
167 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
168 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700169 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700170 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700171
172 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700173 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700174 [&resQueue](const auto& descriptor) {
175 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
176 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700177 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700178 resQueue = nullptr;
179 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700180 }
181 });
182 if (!resultQueueRet.isOk()) {
183 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
184 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700185 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700186 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700187 IF_ALOGV() {
188 session->interfaceChain([](
189 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
190 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800191 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700192 ALOGV(" %s", iface.c_str());
193 }
194 });
195 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700196
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800197 camera_metadata_entry bufMgrMode =
198 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
199 if (bufMgrMode.count > 0) {
200 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
201 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
202 }
203
204 mInterface = new HalInterface(session, queue, mUseHalBufManager);
Emilian Peev71c73a22017-03-21 16:35:51 +0000205 std::string providerType;
206 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000207 mTagMonitor.initialize(mVendorTagId);
208 if (!monitorTags.isEmpty()) {
209 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
210 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800211
Shuzhen Wang268a1362018-10-16 16:32:59 -0700212 // Metadata tags needs fixup for monochrome camera device version less
213 // than 3.5.
214 hardware::hidl_version maxVersion{0,0};
215 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
216 if (res != OK) {
217 ALOGE("%s: Error in getting camera device version id: %s (%d)",
218 __FUNCTION__, strerror(-res), res);
219 return res;
220 }
221 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
222 maxVersion.get_major(), maxVersion.get_minor());
223
224 bool isMonochrome = false;
225 camera_metadata_entry_t entry = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
226 for (size_t i = 0; i < entry.count; i++) {
227 uint8_t capability = entry.data.u8[i];
228 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
229 isMonochrome = true;
230 }
231 }
232 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
233
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800234 return initializeCommonLocked();
235}
236
237status_t Camera3Device::initializeCommonLocked() {
238
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700239 /** Start up status tracker thread */
240 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800241 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700242 if (res != OK) {
243 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
244 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800245 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 mStatusTracker.clear();
247 return res;
248 }
249
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700250 /** Register in-flight map to the status tracker */
251 mInFlightStatusId = mStatusTracker->addComponent();
252
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700253 if (mUseHalBufManager) {
254 res = mRequestBufferSM.initialize(mStatusTracker);
255 if (res != OK) {
256 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
257 strerror(-res), res);
258 mInterface->close();
259 mStatusTracker.clear();
260 return res;
261 }
262 }
263
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800264 /** Create buffer manager */
265 mBufferManager = new Camera3BufferManager();
266
267 Vector<int32_t> sessionParamKeys;
268 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
269 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
270 if (sessionKeysEntry.count > 0) {
271 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
272 }
273
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700274 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700275 mRequestThread = new RequestThread(
276 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800277 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800278 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700279 SET_ERR_L("Unable to start request queue thread: %s (%d)",
280 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800281 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283 return res;
284 }
285
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700286 mPreparerThread = new PreparerThread();
287
Ruben Brunk183f0562015-08-12 12:55:02 -0700288 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700290 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700291 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800293
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800294 // Measure the clock domain offset between camera and video/hw_composer
295 camera_metadata_entry timestampSource =
296 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
297 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
298 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
299 mTimestampOffset = getMonoToBoottimeOffset();
300 }
301
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700302 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100303 camera_metadata_entry partialResultsCount =
304 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
305 if (partialResultsCount.count > 0) {
306 mNumPartialResults = partialResultsCount.data.i32[0];
307 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700308 }
309
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700310 camera_metadata_entry configs =
311 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
312 for (uint32_t i = 0; i < configs.count; i += 4) {
313 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
314 configs.data.i32[i + 3] ==
315 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
316 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
317 configs.data.i32[i + 2]));
318 }
319 }
320
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700321 if (DistortionMapper::isDistortionSupported(mDeviceInfo)) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700322 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700323 if (res != OK) {
324 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
325 return res;
326 }
327 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800328 return OK;
329}
330
331status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700332 return disconnectImpl();
333}
334
335status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800336 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700337 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800338
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700339 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800340
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700341 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700342 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700343 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700344 {
345 Mutex::Autolock l(mLock);
346 if (mStatus == STATUS_UNINITIALIZED) return res;
347
348 if (mStatus == STATUS_ACTIVE ||
349 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
350 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700351 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700352 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700353 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700354 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700355 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700356 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700357 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
358 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359 // Continue to close device even in case of error
360 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700361 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800362 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800363
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700364 if (mStatus == STATUS_ERROR) {
365 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700366 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700367
368 if (mStatusTracker != NULL) {
369 mStatusTracker->requestExit();
370 }
371
372 if (mRequestThread != NULL) {
373 mRequestThread->requestExit();
374 }
375
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700376 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
377 for (size_t i = 0; i < mOutputStreams.size(); i++) {
378 streams.push_back(mOutputStreams[i]);
379 }
380 if (mInputStream != nullptr) {
381 streams.push_back(mInputStream);
382 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700383 }
384
385 // Joining done without holding mLock, otherwise deadlocks may ensue
386 // as the threads try to access parent state
387 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
388 // HAL may be in a bad state, so waiting for request thread
389 // (which may be stuck in the HAL processCaptureRequest call)
390 // could be dangerous.
391 mRequestThread->join();
392 }
393
394 if (mStatusTracker != NULL) {
395 mStatusTracker->join();
396 }
397
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800398 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700399 {
400 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800401 mRequestThread.clear();
Emilian Peev2843c362018-09-26 08:49:40 +0100402 Mutex::Autolock stLock(mTrackerLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700403 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800404 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700405 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700407 // Call close without internal mutex held, as the HAL close may need to
408 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800409 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700410
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700411 flushInflightRequests();
412
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700413 {
414 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800415 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700416 mOutputStreams.clear();
417 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700418 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700419 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700420 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700421 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800422
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700423 for (auto& weakStream : streams) {
424 sp<Camera3StreamInterface> stream = weakStream.promote();
425 if (stream != nullptr) {
426 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
427 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
428 }
429 }
430
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700431 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700432 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800433}
434
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700435// For dumping/debugging only -
436// try to acquire a lock a few times, eventually give up to proceed with
437// debug/dump operations
438bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
439 bool gotLock = false;
440 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
441 if (lock.tryLock() == NO_ERROR) {
442 gotLock = true;
443 break;
444 } else {
445 usleep(kDumpSleepDuration);
446 }
447 }
448 return gotLock;
449}
450
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700451Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
452 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100453 const int STREAM_CONFIGURATION_SIZE = 4;
454 const int STREAM_FORMAT_OFFSET = 0;
455 const int STREAM_WIDTH_OFFSET = 1;
456 const int STREAM_HEIGHT_OFFSET = 2;
457 const int STREAM_IS_INPUT_OFFSET = 3;
458 camera_metadata_ro_entry_t availableStreamConfigs =
459 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
460 if (availableStreamConfigs.count == 0 ||
461 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
462 return Size(0, 0);
463 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700464
Emilian Peev08dd2452017-04-06 16:55:14 +0100465 // Get max jpeg size (area-wise).
466 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
467 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
468 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
469 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
470 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
471 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
472 && format == HAL_PIXEL_FORMAT_BLOB &&
473 (width * height > maxJpegWidth * maxJpegHeight)) {
474 maxJpegWidth = width;
475 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700476 }
477 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100478
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700479 return Size(maxJpegWidth, maxJpegHeight);
480}
481
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800482nsecs_t Camera3Device::getMonoToBoottimeOffset() {
483 // try three times to get the clock offset, choose the one
484 // with the minimum gap in measurements.
485 const int tries = 3;
486 nsecs_t bestGap, measured;
487 for (int i = 0; i < tries; ++i) {
488 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
489 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
490 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
491 const nsecs_t gap = tmono2 - tmono;
492 if (i == 0 || gap < bestGap) {
493 bestGap = gap;
494 measured = tbase - ((tmono + tmono2) >> 1);
495 }
496 }
497 return measured;
498}
499
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800500hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
501 int frameworkFormat) {
502 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
503}
504
505DataspaceFlags Camera3Device::mapToHidlDataspace(
506 android_dataspace dataSpace) {
507 return dataSpace;
508}
509
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700510BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100511 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700512 return usage;
513}
514
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800515StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
516 switch (rotation) {
517 case CAMERA3_STREAM_ROTATION_0:
518 return StreamRotation::ROTATION_0;
519 case CAMERA3_STREAM_ROTATION_90:
520 return StreamRotation::ROTATION_90;
521 case CAMERA3_STREAM_ROTATION_180:
522 return StreamRotation::ROTATION_180;
523 case CAMERA3_STREAM_ROTATION_270:
524 return StreamRotation::ROTATION_270;
525 }
526 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
527 return StreamRotation::ROTATION_0;
528}
529
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800530status_t Camera3Device::mapToStreamConfigurationMode(
531 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
532 if (mode == nullptr) return BAD_VALUE;
533 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
534 switch(operationMode) {
535 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
536 *mode = StreamConfigurationMode::NORMAL_MODE;
537 break;
538 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
539 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
540 break;
541 default:
542 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
543 return BAD_VALUE;
544 }
545 } else {
546 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800547 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800548 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800549}
550
551camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
552 switch (status) {
553 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
554 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
555 }
556 return CAMERA3_BUFFER_STATUS_ERROR;
557}
558
559int Camera3Device::mapToFrameworkFormat(
560 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
561 return static_cast<uint32_t>(pixelFormat);
562}
563
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700564android_dataspace Camera3Device::mapToFrameworkDataspace(
565 DataspaceFlags dataSpace) {
566 return static_cast<android_dataspace>(dataSpace);
567}
568
Emilian Peev050f5dc2017-05-18 14:43:56 +0100569uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700570 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700571 return usage;
572}
573
Emilian Peev050f5dc2017-05-18 14:43:56 +0100574uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700575 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700576 return usage;
577}
578
Zhijun Hef7da0962014-04-24 13:27:56 -0700579ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700580 // Get max jpeg size (area-wise).
581 Size maxJpegResolution = getMaxJpegResolution();
582 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800583 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
584 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700585 return BAD_VALUE;
586 }
587
Zhijun Hef7da0962014-04-24 13:27:56 -0700588 // Get max jpeg buffer size
589 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700590 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
591 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800592 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
593 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700594 return BAD_VALUE;
595 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700596 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800597 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700598
599 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700600 float scaleFactor = ((float) (width * height)) /
601 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800602 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
603 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700604 if (jpegBufferSize > maxJpegBufferSize) {
605 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700606 }
607
608 return jpegBufferSize;
609}
610
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700611ssize_t Camera3Device::getPointCloudBufferSize() const {
612 const int FLOATS_PER_POINT=4;
613 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
614 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800615 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
616 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700617 return BAD_VALUE;
618 }
619 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
620 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
621 return maxBytesForPointCloud;
622}
623
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800624ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800625 const int PER_CONFIGURATION_SIZE = 3;
626 const int WIDTH_OFFSET = 0;
627 const int HEIGHT_OFFSET = 1;
628 const int SIZE_OFFSET = 2;
629 camera_metadata_ro_entry rawOpaqueSizes =
630 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800631 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800632 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800633 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
634 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800635 return BAD_VALUE;
636 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700637
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800638 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
639 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
640 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
641 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
642 }
643 }
644
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800645 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
646 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800647 return BAD_VALUE;
648}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700649
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
651 ATRACE_CALL();
652 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653
654 // Try to lock, but continue in case of failure (to avoid blocking in
655 // deadlocks)
656 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
657 bool gotLock = tryLockSpinRightRound(mLock);
658
659 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800660 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
661 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800663 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
664 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800666 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700667
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800668 String16 templatesOption("-t");
669 int n = args.size();
670 for (int i = 0; i < n; i++) {
671 if (args[i] == templatesOption) {
672 dumpTemplates = true;
673 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000674 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700675 if (i + 1 < n) {
676 String8 monitorTags = String8(args[i + 1]);
677 if (monitorTags == "off") {
678 mTagMonitor.disableMonitoring();
679 } else {
680 mTagMonitor.parseTagsToMonitor(monitorTags);
681 }
682 } else {
683 mTagMonitor.disableMonitoring();
684 }
685 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800686 }
687
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800688 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800689
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800690 const char *status =
691 mStatus == STATUS_ERROR ? "ERROR" :
692 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700693 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
694 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695 mStatus == STATUS_ACTIVE ? "ACTIVE" :
696 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700697
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800698 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700699 if (mStatus == STATUS_ERROR) {
700 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
701 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800702 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800703 const char *mode =
704 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
705 mOperatingMode == static_cast<int>(
706 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
707 "CUSTOM";
708 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709
710 if (mInputStream != NULL) {
711 write(fd, lines.string(), lines.size());
712 mInputStream->dump(fd, args);
713 } else {
714 lines.appendFormat(" No input stream.\n");
715 write(fd, lines.string(), lines.size());
716 }
717 for (size_t i = 0; i < mOutputStreams.size(); i++) {
718 mOutputStreams[i]->dump(fd,args);
719 }
720
Zhijun He431503c2016-03-07 17:30:16 -0800721 if (mBufferManager != NULL) {
722 lines = String8(" Camera3 Buffer Manager:\n");
723 write(fd, lines.string(), lines.size());
724 mBufferManager->dump(fd, args);
725 }
Zhijun He125684a2015-12-26 15:07:30 -0800726
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700727 lines = String8(" In-flight requests:\n");
728 if (mInFlightMap.size() == 0) {
729 lines.append(" None\n");
730 } else {
731 for (size_t i = 0; i < mInFlightMap.size(); i++) {
732 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700733 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700734 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800735 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700736 r.numBuffersLeft);
737 }
738 }
739 write(fd, lines.string(), lines.size());
740
Shuzhen Wang686f6442017-06-20 16:16:04 -0700741 if (mRequestThread != NULL) {
742 mRequestThread->dumpCaptureRequestLatency(fd,
743 " ProcessCaptureRequest latency histogram:");
744 }
745
Igor Murashkin1e479c02013-09-06 16:55:14 -0700746 {
747 lines = String8(" Last request sent:\n");
748 write(fd, lines.string(), lines.size());
749
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700750 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700751 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
752 }
753
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800754 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800755 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800756 "TEMPLATE_PREVIEW",
757 "TEMPLATE_STILL_CAPTURE",
758 "TEMPLATE_VIDEO_RECORD",
759 "TEMPLATE_VIDEO_SNAPSHOT",
760 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800761 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800762 };
763
764 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800765 camera_metadata_t *templateRequest = nullptr;
766 mInterface->constructDefaultRequestSettings(
767 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800768 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800769 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800770 lines.append(" Not supported\n");
771 write(fd, lines.string(), lines.size());
772 } else {
773 write(fd, lines.string(), lines.size());
774 dump_indented_camera_metadata(templateRequest,
775 fd, /*verbosity*/2, /*indentation*/8);
776 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800777 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800778 }
779 }
780
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700781 mTagMonitor.dumpMonitoredMetadata(fd);
782
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800783 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800784 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800785 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800786 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800787 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800788
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 if (gotLock) mLock.unlock();
790 if (gotInterfaceLock) mInterfaceLock.unlock();
791
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800792 return OK;
793}
794
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700795const CameraMetadata& Camera3Device::info(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800796 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800797 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
798 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700799 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 mStatus == STATUS_ERROR ?
801 "when in error state" : "before init");
802 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700803 if (physicalId.isEmpty()) {
804 return mDeviceInfo;
805 } else {
806 std::string id(physicalId.c_str());
807 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
808 return mPhysicalDeviceInfoMap.at(id);
809 } else {
810 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
811 return mDeviceInfo;
812 }
813 }
814}
815
816const CameraMetadata& Camera3Device::info() const {
817 String8 emptyId;
818 return info(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800819}
820
Jianing Wei90e59c92014-03-12 18:29:36 -0700821status_t Camera3Device::checkStatusOkToCaptureLocked() {
822 switch (mStatus) {
823 case STATUS_ERROR:
824 CLOGE("Device has encountered a serious error");
825 return INVALID_OPERATION;
826 case STATUS_UNINITIALIZED:
827 CLOGE("Device not initialized");
828 return INVALID_OPERATION;
829 case STATUS_UNCONFIGURED:
830 case STATUS_CONFIGURED:
831 case STATUS_ACTIVE:
832 // OK
833 break;
834 default:
835 SET_ERR_L("Unexpected status: %d", mStatus);
836 return INVALID_OPERATION;
837 }
838 return OK;
839}
840
841status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000842 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700843 const std::list<const SurfaceMap> &surfaceMaps,
844 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700845 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700846 if (requestList == NULL) {
847 CLOGE("requestList cannot be NULL.");
848 return BAD_VALUE;
849 }
850
Jianing Weicb0652e2014-03-12 18:29:36 -0700851 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000852 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700853 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
854 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
855 ++metadataIt, ++surfaceMapIt) {
856 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 if (newRequest == 0) {
858 CLOGE("Can't create capture request");
859 return BAD_VALUE;
860 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700861
Shuzhen Wang9d066012016-09-30 11:30:20 -0700862 newRequest->mRepeating = repeating;
863
Jianing Weicb0652e2014-03-12 18:29:36 -0700864 // Setup burst Id and request Id
865 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000866 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
867 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700868 CLOGE("RequestID entry exists; but must not be empty in metadata");
869 return BAD_VALUE;
870 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000871 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
872 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700873 } else {
874 CLOGE("RequestID does not exist in metadata");
875 return BAD_VALUE;
876 }
877
Jianing Wei90e59c92014-03-12 18:29:36 -0700878 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700879
880 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700881 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
883 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
884 return BAD_VALUE;
885 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700886
887 // Setup batch size if this is a high speed video recording request.
888 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
889 auto firstRequest = requestList->begin();
890 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
891 if (outputStream->isVideoStream()) {
892 (*firstRequest)->mBatchSize = requestList->size();
893 break;
894 }
895 }
896 }
897
Jianing Wei90e59c92014-03-12 18:29:36 -0700898 return OK;
899}
900
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800901status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800903
Emilian Peevaebbe412018-01-15 13:53:24 +0000904 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700905 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000906 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700907
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800908 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700909}
910
Emilian Peevaebbe412018-01-15 13:53:24 +0000911void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700912 std::list<const SurfaceMap>& surfaceMaps,
913 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000914 PhysicalCameraSettingsList requestList;
915 requestList.push_back({std::string(getId().string()), request});
916 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700917
918 SurfaceMap surfaceMap;
919 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
920 // With no surface list passed in, stream and surface will have 1-to-1
921 // mapping. So the surface index is 0 for each stream in the surfaceMap.
922 for (size_t i = 0; i < streams.count; i++) {
923 surfaceMap[streams.data.i32[i]].push_back(0);
924 }
925 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926}
927
Jianing Wei90e59c92014-03-12 18:29:36 -0700928status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000929 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930 const std::list<const SurfaceMap> &surfaceMaps,
931 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700932 /*out*/
933 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700934 ATRACE_CALL();
935 Mutex::Autolock il(mInterfaceLock);
936 Mutex::Autolock l(mLock);
937
938 status_t res = checkStatusOkToCaptureLocked();
939 if (res != OK) {
940 // error logged by previous call
941 return res;
942 }
943
944 RequestList requestList;
945
Shuzhen Wang0129d522016-10-30 22:43:41 -0700946 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
947 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700948 if (res != OK) {
949 // error logged by previous call
950 return res;
951 }
952
953 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700954 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700955 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700956 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700957 }
958
959 if (res == OK) {
960 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
961 if (res != OK) {
962 SET_ERR_L("Can't transition to active in %f seconds!",
963 kActiveTimeout/1e9);
964 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800965 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700966 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700967 } else {
968 CLOGE("Cannot queue request. Impossible.");
969 return BAD_VALUE;
970 }
971
972 return res;
973}
974
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700975hardware::Return<void> Camera3Device::requestStreamBuffers(
976 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
977 requestStreamBuffers_cb _hidl_cb) {
978 using hardware::camera::device::V3_5::BufferRequestStatus;
979 using hardware::camera::device::V3_5::StreamBufferRet;
980 using hardware::camera::device::V3_5::StreamBufferRequestError;
981
982 std::lock_guard<std::mutex> lock(mRequestBufferInterfaceLock);
983
984 hardware::hidl_vec<StreamBufferRet> bufRets;
985 if (!mUseHalBufManager) {
986 ALOGE("%s: Camera %s does not support HAL buffer management",
987 __FUNCTION__, mId.string());
988 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
989 return hardware::Void();
990 }
991
992 SortedVector<int32_t> streamIds;
993 ssize_t sz = streamIds.setCapacity(bufReqs.size());
994 if (sz < 0 || static_cast<size_t>(sz) != bufReqs.size()) {
995 ALOGE("%s: failed to allocate memory for %zu buffer requests",
996 __FUNCTION__, bufReqs.size());
997 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
998 return hardware::Void();
999 }
1000
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001001 if (bufReqs.size() > mOutputStreams.size()) {
1002 ALOGE("%s: too many buffer requests (%zu > # of output streams %zu)",
1003 __FUNCTION__, bufReqs.size(), mOutputStreams.size());
1004 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1005 return hardware::Void();
1006 }
1007
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001008 // Check for repeated streamId
1009 for (const auto& bufReq : bufReqs) {
1010 if (streamIds.indexOf(bufReq.streamId) != NAME_NOT_FOUND) {
1011 ALOGE("%s: Stream %d appear multiple times in buffer requests",
1012 __FUNCTION__, bufReq.streamId);
1013 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
1014 return hardware::Void();
1015 }
1016 streamIds.add(bufReq.streamId);
1017 }
1018
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001019 if (!mRequestBufferSM.startRequestBuffer()) {
1020 ALOGE("%s: request buffer disallowed while camera service is configuring",
1021 __FUNCTION__);
1022 _hidl_cb(BufferRequestStatus::FAILED_CONFIGURING, bufRets);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001023 return hardware::Void();
1024 }
1025
1026 bufRets.resize(bufReqs.size());
1027
1028 bool allReqsSucceeds = true;
1029 bool oneReqSucceeds = false;
1030 for (size_t i = 0; i < bufReqs.size(); i++) {
1031 const auto& bufReq = bufReqs[i];
1032 auto& bufRet = bufRets[i];
1033 int32_t streamId = bufReq.streamId;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001034 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.get(streamId);
1035 if (outputStream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001036 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
1037 hardware::hidl_vec<StreamBufferRet> emptyBufRets;
1038 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, emptyBufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001039 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001040 return hardware::Void();
1041 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001042
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001043 if (outputStream->isAbandoned()) {
1044 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
1045 allReqsSucceeds = false;
1046 continue;
1047 }
1048
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001049 bufRet.streamId = streamId;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001050 size_t handOutBufferCount = outputStream->getOutstandingBuffersCount();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001051 uint32_t numBuffersRequested = bufReq.numBuffersRequested;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001052 size_t totalHandout = handOutBufferCount + numBuffersRequested;
1053 uint32_t maxBuffers = outputStream->asHalStream()->max_buffers;
1054 if (totalHandout > maxBuffers) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001055 // Not able to allocate enough buffer. Exit early for this stream
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001056 ALOGE("%s: request too much buffers for stream %d: at HAL: %zu + requesting: %d"
1057 " > max: %d", __FUNCTION__, streamId, handOutBufferCount,
1058 numBuffersRequested, maxBuffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001059 bufRet.val.error(StreamBufferRequestError::MAX_BUFFER_EXCEEDED);
1060 allReqsSucceeds = false;
1061 continue;
1062 }
1063
1064 hardware::hidl_vec<StreamBuffer> tmpRetBuffers(numBuffersRequested);
1065 bool currentReqSucceeds = true;
1066 std::vector<camera3_stream_buffer_t> streamBuffers(numBuffersRequested);
1067 size_t numAllocatedBuffers = 0;
1068 size_t numPushedInflightBuffers = 0;
1069 for (size_t b = 0; b < numBuffersRequested; b++) {
1070 camera3_stream_buffer_t& sb = streamBuffers[b];
1071 // Since this method can run concurrently with request thread
1072 // We need to update the wait duration everytime we call getbuffer
1073 nsecs_t waitDuration = kBaseGetBufferWait + getExpectedInFlightDuration();
1074 status_t res = outputStream->getBuffer(&sb, waitDuration);
1075 if (res != OK) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001076 if (res == NO_INIT || res == DEAD_OBJECT) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001077 ALOGV("%s: Can't get output buffer for stream %d: %s (%d)",
1078 __FUNCTION__, streamId, strerror(-res), res);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001079 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001080 } else {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07001081 ALOGE("%s: Can't get output buffer for stream %d: %s (%d)",
1082 __FUNCTION__, streamId, strerror(-res), res);
1083 if (res == TIMED_OUT || res == NO_MEMORY) {
1084 bufRet.val.error(StreamBufferRequestError::NO_BUFFER_AVAILABLE);
1085 } else {
1086 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1087 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001088 }
1089 currentReqSucceeds = false;
1090 break;
1091 }
1092 numAllocatedBuffers++;
1093
1094 buffer_handle_t *buffer = sb.buffer;
1095 auto pair = mInterface->getBufferId(*buffer, streamId);
1096 bool isNewBuffer = pair.first;
1097 uint64_t bufferId = pair.second;
1098 StreamBuffer& hBuf = tmpRetBuffers[b];
1099
1100 hBuf.streamId = streamId;
1101 hBuf.bufferId = bufferId;
1102 hBuf.buffer = (isNewBuffer) ? *buffer : nullptr;
1103 hBuf.status = BufferStatus::OK;
1104 hBuf.releaseFence = nullptr;
1105
1106 native_handle_t *acquireFence = nullptr;
1107 if (sb.acquire_fence != -1) {
1108 acquireFence = native_handle_create(1,0);
1109 acquireFence->data[0] = sb.acquire_fence;
1110 }
1111 hBuf.acquireFence.setTo(acquireFence, /*shouldOwn*/true);
1112 hBuf.releaseFence = nullptr;
1113
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08001114 res = mInterface->pushInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001115 if (res != OK) {
1116 ALOGE("%s: Can't get register request buffers for stream %d: %s (%d)",
1117 __FUNCTION__, streamId, strerror(-res), res);
1118 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1119 currentReqSucceeds = false;
1120 break;
1121 }
1122 numPushedInflightBuffers++;
1123 }
1124 if (currentReqSucceeds) {
1125 bufRet.val.buffers(std::move(tmpRetBuffers));
1126 oneReqSucceeds = true;
1127 } else {
1128 allReqsSucceeds = false;
1129 for (size_t b = 0; b < numPushedInflightBuffers; b++) {
1130 StreamBuffer& hBuf = tmpRetBuffers[b];
1131 buffer_handle_t* buffer;
1132 status_t res = mInterface->popInflightRequestBuffer(hBuf.bufferId, &buffer);
1133 if (res != OK) {
1134 SET_ERR("%s: popInflightRequestBuffer failed for stream %d: %s (%d)",
1135 __FUNCTION__, streamId, strerror(-res), res);
1136 }
1137 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001138 for (size_t b = 0; b < numAllocatedBuffers; b++) {
1139 camera3_stream_buffer_t& sb = streamBuffers[b];
1140 sb.acquire_fence = -1;
1141 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
1142 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001143 returnOutputBuffers(streamBuffers.data(), numAllocatedBuffers, 0);
1144 }
1145 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001146
1147 _hidl_cb(allReqsSucceeds ? BufferRequestStatus::OK :
1148 oneReqSucceeds ? BufferRequestStatus::FAILED_PARTIAL :
1149 BufferRequestStatus::FAILED_UNKNOWN,
1150 bufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001151 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001152 return hardware::Void();
1153}
1154
1155hardware::Return<void> Camera3Device::returnStreamBuffers(
1156 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
1157 if (!mUseHalBufManager) {
1158 ALOGE("%s: Camera %s does not support HAL buffer managerment",
1159 __FUNCTION__, mId.string());
1160 return hardware::Void();
1161 }
1162
1163 for (const auto& buf : buffers) {
1164 if (buf.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
1165 ALOGE("%s: cannot return a buffer without bufferId", __FUNCTION__);
1166 continue;
1167 }
1168
1169 buffer_handle_t* buffer;
1170 status_t res = mInterface->popInflightRequestBuffer(buf.bufferId, &buffer);
1171
1172 if (res != OK) {
1173 ALOGE("%s: cannot find in-flight buffer %" PRIu64 " for stream %d",
1174 __FUNCTION__, buf.bufferId, buf.streamId);
1175 continue;
1176 }
1177
1178 camera3_stream_buffer_t streamBuffer;
1179 streamBuffer.buffer = buffer;
1180 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
1181 streamBuffer.acquire_fence = -1;
1182 streamBuffer.release_fence = -1;
1183
1184 if (buf.releaseFence == nullptr) {
1185 streamBuffer.release_fence = -1;
1186 } else if (buf.releaseFence->numFds == 1) {
1187 streamBuffer.release_fence = dup(buf.releaseFence->data[0]);
1188 } else {
1189 ALOGE("%s: Invalid release fence, fd count is %d, not 1",
1190 __FUNCTION__, buf.releaseFence->numFds);
1191 continue;
1192 }
1193
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001194 sp<Camera3StreamInterface> stream = mOutputStreams.get(buf.streamId);
1195 if (stream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001196 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, buf.streamId);
1197 continue;
1198 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001199 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001200 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
1201 }
1202 return hardware::Void();
1203}
1204
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001205hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001206 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001207 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001208 // Ideally we should grab mLock, but that can lead to deadlock, and
1209 // it's not super important to get up to date value of mStatus for this
1210 // warning print, hence skipping the lock here
1211 if (mStatus == STATUS_ERROR) {
1212 // Per API contract, HAL should act as closed after device error
1213 // But mStatus can be set to error by framework as well, so just log
1214 // a warning here.
1215 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001216 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001217
1218 if (mProcessCaptureResultLock.tryLock() != OK) {
1219 // This should never happen; it indicates a wrong client implementation
1220 // that doesn't follow the contract. But, we can be tolerant here.
1221 ALOGE("%s: callback overlapped! waiting 1s...",
1222 __FUNCTION__);
1223 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1224 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1225 __FUNCTION__);
1226 // really don't know what to do, so bail out.
1227 return hardware::Void();
1228 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001229 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001230 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001231 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001232 }
1233 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001234 return hardware::Void();
1235}
1236
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001237// Only one processCaptureResult should be called at a time, so
1238// the locks won't block. The locks are present here simply to enforce this.
1239hardware::Return<void> Camera3Device::processCaptureResult(
1240 const hardware::hidl_vec<
1241 hardware::camera::device::V3_2::CaptureResult>& results) {
1242 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1243
1244 // Ideally we should grab mLock, but that can lead to deadlock, and
1245 // it's not super important to get up to date value of mStatus for this
1246 // warning print, hence skipping the lock here
1247 if (mStatus == STATUS_ERROR) {
1248 // Per API contract, HAL should act as closed after device error
1249 // But mStatus can be set to error by framework as well, so just log
1250 // a warning here.
1251 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1252 }
1253
1254 if (mProcessCaptureResultLock.tryLock() != OK) {
1255 // This should never happen; it indicates a wrong client implementation
1256 // that doesn't follow the contract. But, we can be tolerant here.
1257 ALOGE("%s: callback overlapped! waiting 1s...",
1258 __FUNCTION__);
1259 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1260 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1261 __FUNCTION__);
1262 // really don't know what to do, so bail out.
1263 return hardware::Void();
1264 }
1265 }
1266 for (const auto& result : results) {
1267 processOneCaptureResultLocked(result, noPhysMetadata);
1268 }
1269 mProcessCaptureResultLock.unlock();
1270 return hardware::Void();
1271}
1272
1273status_t Camera3Device::readOneCameraMetadataLocked(
1274 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
1275 const hardware::camera::device::V3_2::CameraMetadata& result) {
1276 if (fmqResultSize > 0) {
1277 resultMetadata.resize(fmqResultSize);
1278 if (mResultMetadataQueue == nullptr) {
1279 return NO_MEMORY; // logged in initialize()
1280 }
1281 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
1282 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
1283 __FUNCTION__, fmqResultSize);
1284 return INVALID_OPERATION;
1285 }
1286 } else {
1287 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
1288 result.size());
1289 }
1290
1291 if (resultMetadata.size() != 0) {
1292 status_t res;
1293 const camera_metadata_t* metadata =
1294 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
1295 size_t expected_metadata_size = resultMetadata.size();
1296 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
1297 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
1298 __FUNCTION__, strerror(-res), res);
1299 return INVALID_OPERATION;
1300 }
1301 }
1302
1303 return OK;
1304}
1305
Yifan Honga640c5a2017-04-12 16:30:31 -07001306void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001307 const hardware::camera::device::V3_2::CaptureResult& result,
1308 const hardware::hidl_vec<
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001309 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadata) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001310 camera3_capture_result r;
1311 status_t res;
1312 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -07001313
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001314 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -07001315 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001316 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
1317 if (res != OK) {
1318 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1319 __FUNCTION__, result.frameNumber);
1320 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001321 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001322 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001323
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001324 // Read and validate physical camera metadata
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001325 size_t physResultCount = physicalCameraMetadata.size();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001326 std::vector<const char*> physCamIds(physResultCount);
1327 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1328 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1329 physResultMetadata.resize(physResultCount);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001330 for (size_t i = 0; i < physicalCameraMetadata.size(); i++) {
1331 res = readOneCameraMetadataLocked(physicalCameraMetadata[i].fmqMetadataSize,
1332 physResultMetadata[i], physicalCameraMetadata[i].metadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001333 if (res != OK) {
1334 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1335 __FUNCTION__, result.frameNumber,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001336 physicalCameraMetadata[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001337 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001338 }
Shuzhen Wangc2cba122018-05-17 18:10:24 -07001339 physCamIds[i] = physicalCameraMetadata[i].physicalCameraId.c_str();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001340 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1341 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001342 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001343 r.num_physcam_metadata = physResultCount;
1344 r.physcam_ids = physCamIds.data();
1345 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001346
1347 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1348 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1349 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1350 auto& bDst = outputBuffers[i];
1351 const StreamBuffer &bSrc = result.outputBuffers[i];
1352
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001353 sp<Camera3StreamInterface> stream = mOutputStreams.get(bSrc.streamId);
1354 if (stream == nullptr) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001355 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1356 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001357 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001358 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001359 bDst.stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001360
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001361 bool noBufferReturned = false;
1362 buffer_handle_t *buffer = nullptr;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001363 if (mUseHalBufManager) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001364 // This is suspicious most of the time but can be correct during flush where HAL
1365 // has to return capture result before a buffer is requested
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001366 if (bSrc.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001367 if (bSrc.status == BufferStatus::OK) {
1368 ALOGE("%s: Frame %d: Buffer %zu: No bufferId for stream %d",
1369 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
1370 // Still proceeds so other buffers can be returned
1371 }
1372 noBufferReturned = true;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001373 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001374 if (noBufferReturned) {
1375 res = OK;
1376 } else {
1377 res = mInterface->popInflightRequestBuffer(bSrc.bufferId, &buffer);
1378 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001379 } else {
1380 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
1381 }
1382
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001383 if (res != OK) {
1384 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1385 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001386 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001387 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001388
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001389 bDst.buffer = buffer;
1390 bDst.status = mapHidlBufferStatus(bSrc.status);
1391 bDst.acquire_fence = -1;
1392 if (bSrc.releaseFence == nullptr) {
1393 bDst.release_fence = -1;
1394 } else if (bSrc.releaseFence->numFds == 1) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001395 if (noBufferReturned) {
1396 ALOGE("%s: got releaseFence without output buffer!", __FUNCTION__);
1397 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001398 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1399 } else {
1400 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1401 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001402 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001403 }
1404 }
1405 r.num_output_buffers = outputBuffers.size();
1406 r.output_buffers = outputBuffers.data();
1407
1408 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001409 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001410 r.input_buffer = nullptr;
1411 } else {
1412 if (mInputStream->getId() != result.inputBuffer.streamId) {
1413 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1414 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001415 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001416 }
1417 inputBuffer.stream = mInputStream->asHalStream();
1418 buffer_handle_t *buffer;
1419 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1420 &buffer);
1421 if (res != OK) {
1422 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1423 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001424 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001425 }
1426 inputBuffer.buffer = buffer;
1427 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1428 inputBuffer.acquire_fence = -1;
1429 if (result.inputBuffer.releaseFence == nullptr) {
1430 inputBuffer.release_fence = -1;
1431 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1432 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1433 } else {
1434 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1435 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001436 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001437 }
1438 r.input_buffer = &inputBuffer;
1439 }
1440
1441 r.partial_result = result.partialResult;
1442
1443 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001444}
1445
1446hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001447 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001448 // Ideally we should grab mLock, but that can lead to deadlock, and
1449 // it's not super important to get up to date value of mStatus for this
1450 // warning print, hence skipping the lock here
1451 if (mStatus == STATUS_ERROR) {
1452 // Per API contract, HAL should act as closed after device error
1453 // But mStatus can be set to error by framework as well, so just log
1454 // a warning here.
1455 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001456 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001457
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001458 for (const auto& msg : msgs) {
1459 notify(msg);
1460 }
1461 return hardware::Void();
1462}
1463
1464void Camera3Device::notify(
1465 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1466
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001467 camera3_notify_msg m;
1468 switch (msg.type) {
1469 case MsgType::ERROR:
1470 m.type = CAMERA3_MSG_ERROR;
1471 m.message.error.frame_number = msg.msg.error.frameNumber;
1472 if (msg.msg.error.errorStreamId >= 0) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001473 sp<Camera3StreamInterface> stream = mOutputStreams.get(msg.msg.error.errorStreamId);
1474 if (stream == nullptr) {
1475 ALOGE("%s: Frame %d: Invalid error stream id %d", __FUNCTION__,
1476 m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001477 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001478 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001479 m.message.error.error_stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001480 } else {
1481 m.message.error.error_stream = nullptr;
1482 }
1483 switch (msg.msg.error.errorCode) {
1484 case ErrorCode::ERROR_DEVICE:
1485 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1486 break;
1487 case ErrorCode::ERROR_REQUEST:
1488 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1489 break;
1490 case ErrorCode::ERROR_RESULT:
1491 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1492 break;
1493 case ErrorCode::ERROR_BUFFER:
1494 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1495 break;
1496 }
1497 break;
1498 case MsgType::SHUTTER:
1499 m.type = CAMERA3_MSG_SHUTTER;
1500 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1501 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1502 break;
1503 }
1504 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001505}
1506
Emilian Peevaebbe412018-01-15 13:53:24 +00001507status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001508 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001509 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001510 ATRACE_CALL();
1511
Emilian Peevaebbe412018-01-15 13:53:24 +00001512 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001513}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001514
Jianing Weicb0652e2014-03-12 18:29:36 -07001515status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1516 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001517 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518
Emilian Peevaebbe412018-01-15 13:53:24 +00001519 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001520 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001521 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001522
Emilian Peevaebbe412018-01-15 13:53:24 +00001523 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001524 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525}
1526
Emilian Peevaebbe412018-01-15 13:53:24 +00001527status_t Camera3Device::setStreamingRequestList(
1528 const List<const PhysicalCameraSettingsList> &requestsList,
1529 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001530 ATRACE_CALL();
1531
Emilian Peevaebbe412018-01-15 13:53:24 +00001532 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001533}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001534
1535sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001536 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 status_t res;
1538
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001539 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001540 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1541 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001542 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1543 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001544 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001546 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001547 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001548 } else if (mStatus == STATUS_UNCONFIGURED) {
1549 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001550 CLOGE("No streams configured");
1551 return NULL;
1552 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 }
1554
Shuzhen Wang0129d522016-10-30 22:43:41 -07001555 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001556 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001557}
1558
Jianing Weicb0652e2014-03-12 18:29:36 -07001559status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001560 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001561 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001563
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001564 switch (mStatus) {
1565 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001566 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 return INVALID_OPERATION;
1568 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001569 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001571 case STATUS_UNCONFIGURED:
1572 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 case STATUS_ACTIVE:
1574 // OK
1575 break;
1576 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001577 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 return INVALID_OPERATION;
1579 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001580 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001581
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001582 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001583}
1584
1585status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1586 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001588
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001589 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001590}
1591
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001592status_t Camera3Device::createInputStream(
1593 uint32_t width, uint32_t height, int format, int *id) {
1594 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001595 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001596 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001597 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001598 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1599 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001600
1601 status_t res;
1602 bool wasActive = false;
1603
1604 switch (mStatus) {
1605 case STATUS_ERROR:
1606 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1607 return INVALID_OPERATION;
1608 case STATUS_UNINITIALIZED:
1609 ALOGE("%s: Device not initialized", __FUNCTION__);
1610 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001611 case STATUS_UNCONFIGURED:
1612 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001613 // OK
1614 break;
1615 case STATUS_ACTIVE:
1616 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001617 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001618 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001619 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001620 return res;
1621 }
1622 wasActive = true;
1623 break;
1624 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001626 return INVALID_OPERATION;
1627 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001628 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001629
1630 if (mInputStream != 0) {
1631 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1632 return INVALID_OPERATION;
1633 }
1634
1635 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1636 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001637 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001638
1639 mInputStream = newStream;
1640
1641 *id = mNextStreamId++;
1642
1643 // Continue captures if active at start
1644 if (wasActive) {
1645 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001646 // Reuse current operating mode and session parameters for new stream config
1647 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001648 if (res != OK) {
1649 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1650 __FUNCTION__, mNextStreamId, strerror(-res), res);
1651 return res;
1652 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001653 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001654 }
1655
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001656 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001657 return OK;
1658}
1659
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001660status_t Camera3Device::StreamSet::add(
1661 int streamId, sp<camera3::Camera3OutputStreamInterface> stream) {
1662 if (stream == nullptr) {
1663 ALOGE("%s: cannot add null stream", __FUNCTION__);
1664 return BAD_VALUE;
1665 }
1666 std::lock_guard<std::mutex> lock(mLock);
1667 return mData.add(streamId, stream);
1668}
1669
1670ssize_t Camera3Device::StreamSet::remove(int streamId) {
1671 std::lock_guard<std::mutex> lock(mLock);
1672 return mData.removeItem(streamId);
1673}
1674
1675sp<camera3::Camera3OutputStreamInterface>
1676Camera3Device::StreamSet::get(int streamId) {
1677 std::lock_guard<std::mutex> lock(mLock);
1678 ssize_t idx = mData.indexOfKey(streamId);
1679 if (idx == NAME_NOT_FOUND) {
1680 return nullptr;
1681 }
1682 return mData.editValueAt(idx);
1683}
1684
1685sp<camera3::Camera3OutputStreamInterface>
1686Camera3Device::StreamSet::operator[] (size_t index) {
1687 std::lock_guard<std::mutex> lock(mLock);
1688 return mData.editValueAt(index);
1689}
1690
1691size_t Camera3Device::StreamSet::size() const {
1692 std::lock_guard<std::mutex> lock(mLock);
1693 return mData.size();
1694}
1695
1696void Camera3Device::StreamSet::clear() {
1697 std::lock_guard<std::mutex> lock(mLock);
1698 return mData.clear();
1699}
1700
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07001701std::vector<int> Camera3Device::StreamSet::getStreamIds() {
1702 std::lock_guard<std::mutex> lock(mLock);
1703 std::vector<int> streamIds(mData.size());
1704 for (size_t i = 0; i < mData.size(); i++) {
1705 streamIds[i] = mData.keyAt(i);
1706 }
1707 return streamIds;
1708}
1709
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001710status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001711 uint32_t width, uint32_t height, int format,
1712 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001713 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001714 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001715 ATRACE_CALL();
1716
1717 if (consumer == nullptr) {
1718 ALOGE("%s: consumer must not be null", __FUNCTION__);
1719 return BAD_VALUE;
1720 }
1721
1722 std::vector<sp<Surface>> consumers;
1723 consumers.push_back(consumer);
1724
1725 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001726 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1727 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001728}
1729
1730status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1731 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1732 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001733 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001734 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001735 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001736
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001737 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001738 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001739 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001740 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001741 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1742 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1743 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001744
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001745 status_t res;
1746 bool wasActive = false;
1747
1748 switch (mStatus) {
1749 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001750 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751 return INVALID_OPERATION;
1752 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001753 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001754 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001755 case STATUS_UNCONFIGURED:
1756 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001757 // OK
1758 break;
1759 case STATUS_ACTIVE:
1760 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001761 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001762 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001763 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001764 return res;
1765 }
1766 wasActive = true;
1767 break;
1768 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001769 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001770 return INVALID_OPERATION;
1771 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001772 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773
1774 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001775
Shuzhen Wang0129d522016-10-30 22:43:41 -07001776 if (consumers.size() == 0 && !hasDeferredConsumer) {
1777 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1778 return BAD_VALUE;
1779 }
Zhijun He5d677d12016-05-29 16:52:39 -07001780
Shuzhen Wang0129d522016-10-30 22:43:41 -07001781 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001782 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1783 return BAD_VALUE;
1784 }
1785
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001786 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001787 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001788 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001789 blobBufferSize = getPointCloudBufferSize();
1790 if (blobBufferSize <= 0) {
1791 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1792 return BAD_VALUE;
1793 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001794 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1795 blobBufferSize = width * height;
1796 } else {
1797 blobBufferSize = getJpegBufferSize(width, height);
1798 if (blobBufferSize <= 0) {
1799 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1800 return BAD_VALUE;
1801 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001802 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001803 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001804 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001805 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001806 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1807 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1808 if (rawOpaqueBufferSize <= 0) {
1809 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1810 return BAD_VALUE;
1811 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001812 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001813 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001814 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001815 } else if (isShared) {
1816 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1817 width, height, format, consumerUsage, dataSpace, rotation,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001818 mTimestampOffset, physicalCameraId, streamSetId,
1819 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001820 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001821 newStream = new Camera3OutputStream(mNextStreamId,
1822 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001823 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001824 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001825 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001826 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001827 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001828 }
Emilian Peev40ead602017-09-26 15:46:36 +01001829
1830 size_t consumerCount = consumers.size();
1831 for (size_t i = 0; i < consumerCount; i++) {
1832 int id = newStream->getSurfaceId(consumers[i]);
1833 if (id < 0) {
1834 SET_ERR_L("Invalid surface id");
1835 return BAD_VALUE;
1836 }
1837 if (surfaceIds != nullptr) {
1838 surfaceIds->push_back(id);
1839 }
1840 }
1841
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001842 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001843
Emilian Peev08dd2452017-04-06 16:55:14 +01001844 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001845
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001846 res = mOutputStreams.add(mNextStreamId, newStream);
1847 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001848 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001849 return res;
1850 }
1851
1852 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001853 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001854
1855 // Continue captures if active at start
1856 if (wasActive) {
1857 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001858 // Reuse current operating mode and session parameters for new stream config
1859 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001860 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001861 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1862 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001863 return res;
1864 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001865 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001867 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001868 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001869}
1870
Emilian Peev710c1422017-08-30 11:19:38 +01001871status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001872 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001873 if (nullptr == streamInfo) {
1874 return BAD_VALUE;
1875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001876 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001877 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001878
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001879 switch (mStatus) {
1880 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001881 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882 return INVALID_OPERATION;
1883 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001884 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001885 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 case STATUS_UNCONFIGURED:
1887 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001888 case STATUS_ACTIVE:
1889 // OK
1890 break;
1891 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001892 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001893 return INVALID_OPERATION;
1894 }
1895
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001896 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1897 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001898 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001899 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001900 }
1901
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001902 streamInfo->width = stream->getWidth();
1903 streamInfo->height = stream->getHeight();
1904 streamInfo->format = stream->getFormat();
1905 streamInfo->dataSpace = stream->getDataSpace();
1906 streamInfo->formatOverridden = stream->isFormatOverridden();
1907 streamInfo->originalFormat = stream->getOriginalFormat();
1908 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1909 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001910 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001911}
1912
1913status_t Camera3Device::setStreamTransform(int id,
1914 int transform) {
1915 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001916 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001917 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001918
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919 switch (mStatus) {
1920 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001921 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001922 return INVALID_OPERATION;
1923 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001924 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001925 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001926 case STATUS_UNCONFIGURED:
1927 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928 case STATUS_ACTIVE:
1929 // OK
1930 break;
1931 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001932 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001933 return INVALID_OPERATION;
1934 }
1935
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001936 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1937 if (stream == nullptr) {
1938 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001939 return BAD_VALUE;
1940 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001941 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001942}
1943
1944status_t Camera3Device::deleteStream(int id) {
1945 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001946 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001947 Mutex::Autolock l(mLock);
1948 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001949
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001950 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001951
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001952 // CameraDevice semantics require device to already be idle before
1953 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001954 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001955 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001956 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957 }
1958
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001959 if (mStatus == STATUS_ERROR) {
1960 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1961 __FUNCTION__, mId.string());
1962 return -EBUSY;
1963 }
1964
Igor Murashkin2fba5842013-04-22 14:03:54 -07001965 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001966 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001967 if (mInputStream != NULL && id == mInputStream->getId()) {
1968 deletedStream = mInputStream;
1969 mInputStream.clear();
1970 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001971 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001972 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001973 return BAD_VALUE;
1974 }
Zhijun He5f446352014-01-22 09:49:33 -08001975 }
1976
1977 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001978 if (stream != nullptr) {
1979 deletedStream = stream;
1980 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981 }
1982
1983 // Free up the stream endpoint so that it can be used by some other stream
1984 res = deletedStream->disconnect();
1985 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001986 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001987 // fall through since we want to still list the stream as deleted.
1988 }
1989 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001990 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001991
1992 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001993}
1994
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001995status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001996 ATRACE_CALL();
1997 ALOGV("%s: E", __FUNCTION__);
1998
1999 Mutex::Autolock il(mInterfaceLock);
2000 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07002001
Emilian Peev811d2952018-05-25 11:08:40 +01002002 // In case the client doesn't include any session parameter, try a
2003 // speculative configuration using the values from the last cached
2004 // default request.
2005 if (sessionParams.isEmpty() &&
2006 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
2007 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
2008 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
2009 mLastTemplateId);
2010 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
2011 operatingMode);
2012 }
2013
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002014 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
2015}
2016
2017status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
2018 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002019 //Filter out any incoming session parameters
2020 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002021 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
2022 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002023 CameraMetadata filteredParams(availableSessionKeys.count);
2024 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2025 filteredParams.getAndLock());
2026 set_camera_metadata_vendor_id(meta, mVendorTagId);
2027 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002028 if (availableSessionKeys.count > 0) {
2029 for (size_t i = 0; i < availableSessionKeys.count; i++) {
2030 camera_metadata_ro_entry entry = params.find(
2031 availableSessionKeys.data.i32[i]);
2032 if (entry.count > 0) {
2033 filteredParams.update(entry);
2034 }
2035 }
2036 }
2037
2038 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07002039}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002040
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002041status_t Camera3Device::getInputBufferProducer(
2042 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002043 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002044 Mutex::Autolock il(mInterfaceLock);
2045 Mutex::Autolock l(mLock);
2046
2047 if (producer == NULL) {
2048 return BAD_VALUE;
2049 } else if (mInputStream == NULL) {
2050 return INVALID_OPERATION;
2051 }
2052
2053 return mInputStream->getInputBufferProducer(producer);
2054}
2055
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002056status_t Camera3Device::createDefaultRequest(int templateId,
2057 CameraMetadata *request) {
2058 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07002059 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002060
2061 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
2062 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07002063 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002064 return BAD_VALUE;
2065 }
2066
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002067 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002068
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002069 {
2070 Mutex::Autolock l(mLock);
2071 switch (mStatus) {
2072 case STATUS_ERROR:
2073 CLOGE("Device has encountered a serious error");
2074 return INVALID_OPERATION;
2075 case STATUS_UNINITIALIZED:
2076 CLOGE("Device is not initialized!");
2077 return INVALID_OPERATION;
2078 case STATUS_UNCONFIGURED:
2079 case STATUS_CONFIGURED:
2080 case STATUS_ACTIVE:
2081 // OK
2082 break;
2083 default:
2084 SET_ERR_L("Unexpected status: %d", mStatus);
2085 return INVALID_OPERATION;
2086 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002087
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002088 if (!mRequestTemplateCache[templateId].isEmpty()) {
2089 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002090 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002091 return OK;
2092 }
Zhijun Hea1530f12014-09-14 12:44:20 -07002093 }
2094
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002095 camera_metadata_t *rawRequest;
2096 status_t res = mInterface->constructDefaultRequestSettings(
2097 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002098
2099 {
2100 Mutex::Autolock l(mLock);
2101 if (res == BAD_VALUE) {
2102 ALOGI("%s: template %d is not supported on this camera device",
2103 __FUNCTION__, templateId);
2104 return res;
2105 } else if (res != OK) {
2106 CLOGE("Unable to construct request template %d: %s (%d)",
2107 templateId, strerror(-res), res);
2108 return res;
2109 }
2110
2111 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
2112 mRequestTemplateCache[templateId].acquire(rawRequest);
2113
2114 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002115 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002116 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002117 return OK;
2118}
2119
2120status_t Camera3Device::waitUntilDrained() {
2121 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002122 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002123 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002124 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002125
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002126 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07002127}
2128
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002129status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002130 switch (mStatus) {
2131 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002132 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002133 ALOGV("%s: Already idle", __FUNCTION__);
2134 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002135 case STATUS_CONFIGURED:
2136 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002137 case STATUS_ERROR:
2138 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002139 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140 break;
2141 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002142 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002143 return INVALID_OPERATION;
2144 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002145 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
2146 maxExpectedDuration);
2147 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07002148 if (res != OK) {
2149 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
2150 res);
2151 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002152 return res;
2153}
2154
Ruben Brunk183f0562015-08-12 12:55:02 -07002155
2156void Camera3Device::internalUpdateStatusLocked(Status status) {
2157 mStatus = status;
2158 mRecentStatusUpdates.add(mStatus);
2159 mStatusChanged.broadcast();
2160}
2161
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002162void Camera3Device::pauseStateNotify(bool enable) {
2163 Mutex::Autolock il(mInterfaceLock);
2164 Mutex::Autolock l(mLock);
2165
2166 mPauseStateNotify = enable;
2167}
2168
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002169// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002170status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08002171 if (mRequestThread.get() != nullptr) {
2172 mRequestThread->setPaused(true);
2173 } else {
2174 return NO_INIT;
2175 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002176
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002177 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2178 maxExpectedDuration);
2179 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002180 if (res != OK) {
2181 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002182 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002183 }
2184
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002185 return res;
2186}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002187
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002188// Resume after internalPauseAndWaitLocked
2189status_t Camera3Device::internalResumeLocked() {
2190 status_t res;
2191
2192 mRequestThread->setPaused(false);
2193
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002194 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2195 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002196 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
2197 if (res != OK) {
2198 SET_ERR_L("Can't transition to active in %f seconds!",
2199 kActiveTimeout/1e9);
2200 }
2201 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002202 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002203}
2204
Ruben Brunk183f0562015-08-12 12:55:02 -07002205status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002206 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07002207
2208 size_t startIndex = 0;
2209 if (mStatusWaiters == 0) {
2210 // Clear the list of recent statuses if there are no existing threads waiting on updates to
2211 // this status list
2212 mRecentStatusUpdates.clear();
2213 } else {
2214 // If other threads are waiting on updates to this status list, set the position of the
2215 // first element that this list will check rather than clearing the list.
2216 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002217 }
2218
Ruben Brunk183f0562015-08-12 12:55:02 -07002219 mStatusWaiters++;
2220
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002221 if (!active && mUseHalBufManager) {
2222 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08002223 if (mStatus == STATUS_ACTIVE) {
2224 mRequestThread->signalPipelineDrain(streamIds);
2225 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002226 mRequestBufferSM.onWaitUntilIdle();
2227 }
2228
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002229 bool stateSeen = false;
2230 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07002231 if (active == (mStatus == STATUS_ACTIVE)) {
2232 // Desired state is current
2233 break;
2234 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002235
2236 res = mStatusChanged.waitRelative(mLock, timeout);
2237 if (res != OK) break;
2238
Ruben Brunk183f0562015-08-12 12:55:02 -07002239 // This is impossible, but if not, could result in subtle deadlocks and invalid state
2240 // transitions.
2241 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
2242 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
2243 __FUNCTION__);
2244
2245 // Encountered desired state since we began waiting
2246 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002247 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
2248 stateSeen = true;
2249 break;
2250 }
2251 }
2252 } while (!stateSeen);
2253
Ruben Brunk183f0562015-08-12 12:55:02 -07002254 mStatusWaiters--;
2255
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002256 return res;
2257}
2258
2259
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002260status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002261 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002262 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002263
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002264 if (listener != NULL && mListener != NULL) {
2265 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
2266 }
2267 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002268 mRequestThread->setNotificationListener(listener);
2269 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002270
2271 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002272}
2273
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002274bool Camera3Device::willNotify3A() {
2275 return false;
2276}
2277
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002278status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002279 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002280 status_t res;
2281 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002282
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002283 while (mResultQueue.empty()) {
2284 res = mResultSignal.waitRelative(mOutputLock, timeout);
2285 if (res == TIMED_OUT) {
2286 return res;
2287 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002288 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
2289 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002290 return res;
2291 }
2292 }
2293 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002294}
2295
Jianing Weicb0652e2014-03-12 18:29:36 -07002296status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002297 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002298 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002299
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002300 if (mResultQueue.empty()) {
2301 return NOT_ENOUGH_DATA;
2302 }
2303
Jianing Weicb0652e2014-03-12 18:29:36 -07002304 if (frame == NULL) {
2305 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
2306 return BAD_VALUE;
2307 }
2308
2309 CaptureResult &result = *(mResultQueue.begin());
2310 frame->mResultExtras = result.mResultExtras;
2311 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002312 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002313 mResultQueue.erase(mResultQueue.begin());
2314
2315 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002316}
2317
2318status_t Camera3Device::triggerAutofocus(uint32_t id) {
2319 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002320 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002321
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002322 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
2323 // Mix-in this trigger into the next request and only the next request.
2324 RequestTrigger trigger[] = {
2325 {
2326 ANDROID_CONTROL_AF_TRIGGER,
2327 ANDROID_CONTROL_AF_TRIGGER_START
2328 },
2329 {
2330 ANDROID_CONTROL_AF_TRIGGER_ID,
2331 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002332 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002333 };
2334
2335 return mRequestThread->queueTrigger(trigger,
2336 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002337}
2338
2339status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2340 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002341 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002342
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002343 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2344 // Mix-in this trigger into the next request and only the next request.
2345 RequestTrigger trigger[] = {
2346 {
2347 ANDROID_CONTROL_AF_TRIGGER,
2348 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2349 },
2350 {
2351 ANDROID_CONTROL_AF_TRIGGER_ID,
2352 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002353 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002354 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002355
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002356 return mRequestThread->queueTrigger(trigger,
2357 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002358}
2359
2360status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2361 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002362 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002363
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002364 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2365 // Mix-in this trigger into the next request and only the next request.
2366 RequestTrigger trigger[] = {
2367 {
2368 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2369 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2370 },
2371 {
2372 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2373 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002374 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002375 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002376
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002377 return mRequestThread->queueTrigger(trigger,
2378 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002379}
2380
Jianing Weicb0652e2014-03-12 18:29:36 -07002381status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002382 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002383 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002384 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002385
Zhijun He7ef20392014-04-21 16:04:17 -07002386 {
2387 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002388
2389 // b/116514106 "disconnect()" can get called twice for the same device. The
2390 // camera device will not be initialized during the second run.
2391 if (mStatus == STATUS_UNINITIALIZED) {
2392 return OK;
2393 }
2394
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002395 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07002396 }
2397
Emilian Peev08dd2452017-04-06 16:55:14 +01002398 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002399}
2400
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002401status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002402 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2403}
2404
2405status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002406 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002407 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002408 Mutex::Autolock il(mInterfaceLock);
2409 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002410
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002411 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2412 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002413 CLOGE("Stream %d does not exist", streamId);
2414 return BAD_VALUE;
2415 }
2416
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002417 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002418 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002419 return BAD_VALUE;
2420 }
2421
2422 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002423 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002424 return BAD_VALUE;
2425 }
2426
Ruben Brunkc78ac262015-08-13 17:58:46 -07002427 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002428}
2429
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002430status_t Camera3Device::tearDown(int streamId) {
2431 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002432 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002433 Mutex::Autolock il(mInterfaceLock);
2434 Mutex::Autolock l(mLock);
2435
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002436 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2437 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002438 CLOGE("Stream %d does not exist", streamId);
2439 return BAD_VALUE;
2440 }
2441
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002442 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2443 CLOGE("Stream %d is a target of a in-progress request", streamId);
2444 return BAD_VALUE;
2445 }
2446
2447 return stream->tearDown();
2448}
2449
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002450status_t Camera3Device::addBufferListenerForStream(int streamId,
2451 wp<Camera3StreamBufferListener> listener) {
2452 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002453 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002454 Mutex::Autolock il(mInterfaceLock);
2455 Mutex::Autolock l(mLock);
2456
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002457 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2458 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002459 CLOGE("Stream %d does not exist", streamId);
2460 return BAD_VALUE;
2461 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002462 stream->addBufferListener(listener);
2463
2464 return OK;
2465}
2466
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002467/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002468 * Methods called by subclasses
2469 */
2470
2471void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002472 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002473 {
2474 // Need mLock to safely update state and synchronize to current
2475 // state of methods in flight.
2476 Mutex::Autolock l(mLock);
2477 // We can get various system-idle notices from the status tracker
2478 // while starting up. Only care about them if we've actually sent
2479 // in some requests recently.
2480 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2481 return;
2482 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002483 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2484 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002485 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002486
2487 // Skip notifying listener if we're doing some user-transparent
2488 // state changes
2489 if (mPauseStateNotify) return;
2490 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002491
2492 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002493 {
2494 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002495 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002496 }
2497 if (idle && listener != NULL) {
2498 listener->notifyIdle();
2499 }
2500}
2501
Shuzhen Wang758c2152017-01-10 18:26:18 -08002502status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002503 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002504 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002505 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2506 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002507
2508 if (surfaceIds == nullptr) {
2509 return BAD_VALUE;
2510 }
2511
Zhijun He5d677d12016-05-29 16:52:39 -07002512 Mutex::Autolock il(mInterfaceLock);
2513 Mutex::Autolock l(mLock);
2514
Shuzhen Wang758c2152017-01-10 18:26:18 -08002515 if (consumers.size() == 0) {
2516 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002517 return BAD_VALUE;
2518 }
2519
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002520 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2521 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002522 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002523 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002524 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002525
2526 // isConsumerConfigurationDeferred will be off after setConsumers
2527 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002528 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002529 if (res != OK) {
2530 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2531 return res;
2532 }
2533
Emilian Peev40ead602017-09-26 15:46:36 +01002534 for (auto &consumer : consumers) {
2535 int id = stream->getSurfaceId(consumer);
2536 if (id < 0) {
2537 CLOGE("Invalid surface id!");
2538 return BAD_VALUE;
2539 }
2540 surfaceIds->push_back(id);
2541 }
2542
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002543 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002544 if (!stream->isConfiguring()) {
2545 CLOGE("Stream %d was already fully configured.", streamId);
2546 return INVALID_OPERATION;
2547 }
Zhijun He5d677d12016-05-29 16:52:39 -07002548
Shuzhen Wang0129d522016-10-30 22:43:41 -07002549 res = stream->finishConfiguration();
2550 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002551 // If finishConfiguration fails due to abandoned surface, do not set
2552 // device to error state.
2553 bool isSurfaceAbandoned =
2554 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2555 if (!isSurfaceAbandoned) {
2556 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2557 stream->getId(), strerror(-res), res);
2558 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002559 return res;
2560 }
Zhijun He5d677d12016-05-29 16:52:39 -07002561 }
2562
2563 return OK;
2564}
2565
Emilian Peev40ead602017-09-26 15:46:36 +01002566status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2567 const std::vector<OutputStreamInfo> &outputInfo,
2568 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2569 Mutex::Autolock il(mInterfaceLock);
2570 Mutex::Autolock l(mLock);
2571
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002572 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2573 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002574 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002575 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002576 }
2577
2578 for (const auto &it : removedSurfaceIds) {
2579 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2580 CLOGE("Shared surface still part of a pending request!");
2581 return -EBUSY;
2582 }
2583 }
2584
Emilian Peev40ead602017-09-26 15:46:36 +01002585 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2586 if (res != OK) {
2587 CLOGE("Stream %d failed to update stream (error %d %s) ",
2588 streamId, res, strerror(-res));
2589 if (res == UNKNOWN_ERROR) {
2590 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2591 __FUNCTION__);
2592 }
2593 return res;
2594 }
2595
2596 return res;
2597}
2598
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002599status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2600 Mutex::Autolock il(mInterfaceLock);
2601 Mutex::Autolock l(mLock);
2602
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002603 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2604 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002605 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2606 return BAD_VALUE;
2607 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002608 return stream->dropBuffers(dropping);
2609}
2610
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002611/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612 * Camera3Device private methods
2613 */
2614
2615sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002616 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002617 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002618
2619 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002620 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002621
2622 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002623 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002624 if (inputStreams.count > 0) {
2625 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002626 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002627 CLOGE("Request references unknown input stream %d",
2628 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002629 return NULL;
2630 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002632 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002633 SET_ERR_L("%s: input stream %d is not configured!",
2634 __FUNCTION__, mInputStream->getId());
2635 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002636 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002637 // Check if stream prepare is blocking requests.
2638 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002639 CLOGE("Request references an input stream that's being prepared!");
2640 return NULL;
2641 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002642
2643 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002644 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002645 }
2646
2647 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002648 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002649 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002650 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002651 return NULL;
2652 }
2653
2654 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002655 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2656 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002657 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002658 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002659 return NULL;
2660 }
Zhijun He5d677d12016-05-29 16:52:39 -07002661 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002662 auto iter = surfaceMap.find(streams.data.i32[i]);
2663 if (iter != surfaceMap.end()) {
2664 const std::vector<size_t>& surfaces = iter->second;
2665 for (const auto& surface : surfaces) {
2666 if (stream->isConsumerConfigurationDeferred(surface)) {
2667 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2668 "due to deferred consumer", stream->getId(), surface);
2669 return NULL;
2670 }
2671 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002672 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002673 }
2674
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002675 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002676 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2677 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002678 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002679 // Check if stream prepare is blocking requests.
2680 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002681 CLOGE("Request references an output stream that's being prepared!");
2682 return NULL;
2683 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684
2685 newRequest->mOutputStreams.push(stream);
2686 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002687 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002688 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002689
2690 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002691}
2692
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002693bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2694 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2695 Size size = mSupportedOpaqueInputSizes[i];
2696 if (size.width == width && size.height == height) {
2697 return true;
2698 }
2699 }
2700
2701 return false;
2702}
2703
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002704void Camera3Device::cancelStreamsConfigurationLocked() {
2705 int res = OK;
2706 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2707 res = mInputStream->cancelConfiguration();
2708 if (res != OK) {
2709 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2710 mInputStream->getId(), strerror(-res), res);
2711 }
2712 }
2713
2714 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002715 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002716 if (outputStream->isConfiguring()) {
2717 res = outputStream->cancelConfiguration();
2718 if (res != OK) {
2719 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2720 outputStream->getId(), strerror(-res), res);
2721 }
2722 }
2723 }
2724
2725 // Return state to that at start of call, so that future configures
2726 // properly clean things up
2727 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2728 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002729
2730 res = mPreparerThread->resume();
2731 if (res != OK) {
2732 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2733 }
2734}
2735
2736bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2737 ATRACE_CALL();
2738 bool ret = false;
2739
2740 Mutex::Autolock il(mInterfaceLock);
2741 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2742
2743 Mutex::Autolock l(mLock);
2744 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2745 if (rc == NO_ERROR) {
2746 mNeedConfig = true;
2747 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2748 if (rc == NO_ERROR) {
2749 ret = true;
2750 mPauseStateNotify = false;
2751 //Moving to active state while holding 'mLock' is important.
2752 //There could be pending calls to 'create-/deleteStream' which
2753 //will trigger another stream configuration while the already
2754 //present streams end up with outstanding buffers that will
2755 //not get drained.
2756 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002757 } else if (rc == DEAD_OBJECT) {
2758 // DEAD_OBJECT can be returned if either the consumer surface is
2759 // abandoned, or the HAL has died.
2760 // - If the HAL has died, configureStreamsLocked call will set
2761 // device to error state,
2762 // - If surface is abandoned, we should not set device to error
2763 // state.
2764 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002765 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002766 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002767 }
2768 } else {
2769 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2770 }
2771
2772 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002773}
2774
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002775status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002776 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002777 ATRACE_CALL();
2778 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002779
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002780 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002781 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002782 return INVALID_OPERATION;
2783 }
2784
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002785 if (operatingMode < 0) {
2786 CLOGE("Invalid operating mode: %d", operatingMode);
2787 return BAD_VALUE;
2788 }
2789
2790 bool isConstrainedHighSpeed =
2791 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2792 operatingMode;
2793
2794 if (mOperatingMode != operatingMode) {
2795 mNeedConfig = true;
2796 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2797 mOperatingMode = operatingMode;
2798 }
2799
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002800 if (!mNeedConfig) {
2801 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2802 return OK;
2803 }
2804
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002805 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2806 // adding a dummy stream instead.
2807 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2808 if (mOutputStreams.size() == 0) {
2809 addDummyStreamLocked();
2810 } else {
2811 tryRemoveDummyStreamLocked();
2812 }
2813
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002814 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002815 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002816
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002817 mPreparerThread->pause();
2818
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002819 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002820 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002821 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2822
2823 Vector<camera3_stream_t*> streams;
2824 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002825 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002826
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002827
2828 if (mInputStream != NULL) {
2829 camera3_stream_t *inputStream;
2830 inputStream = mInputStream->startConfiguration();
2831 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002832 CLOGE("Can't start input stream configuration");
2833 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002834 return INVALID_OPERATION;
2835 }
2836 streams.add(inputStream);
2837 }
2838
2839 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002840
2841 // Don't configure bidi streams twice, nor add them twice to the list
2842 if (mOutputStreams[i].get() ==
2843 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2844
2845 config.num_streams--;
2846 continue;
2847 }
2848
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002849 camera3_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002850 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002851 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002852 CLOGE("Can't start output stream configuration");
2853 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002854 return INVALID_OPERATION;
2855 }
2856 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002857
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002858 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002859 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2860 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002861 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2862 bufferSizes[k] = static_cast<uint32_t>(
2863 getJpegBufferSize(outputStream->width, outputStream->height));
2864 } else if (outputStream->data_space ==
2865 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2866 bufferSizes[k] = outputStream->width * outputStream->height;
2867 } else {
2868 ALOGW("%s: Blob dataSpace %d not supported",
2869 __FUNCTION__, outputStream->data_space);
2870 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002871 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002872 }
2873
2874 config.streams = streams.editArray();
2875
2876 // Do the HAL configuration; will potentially touch stream
2877 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002878
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002879 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002880 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002881 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002882
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002883 if (res == BAD_VALUE) {
2884 // HAL rejected this set of streams as unsupported, clean up config
2885 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002886 CLOGE("Set of requested inputs/outputs not supported by HAL");
2887 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002888 return BAD_VALUE;
2889 } else if (res != OK) {
2890 // Some other kind of error from configure_streams - this is not
2891 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002892 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2893 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002894 return res;
2895 }
2896
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002897 // Finish all stream configuration immediately.
2898 // TODO: Try to relax this later back to lazy completion, which should be
2899 // faster
2900
Igor Murashkin073f8572013-05-02 14:59:28 -07002901 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002902 bool streamReConfigured = false;
2903 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002904 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002905 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002906 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002907 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002908 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2909 return DEAD_OBJECT;
2910 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002911 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002912 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002913 if (streamReConfigured) {
2914 mInterface->onStreamReConfigured(mInputStream->getId());
2915 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002916 }
2917
2918 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002919 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002920 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002921 bool streamReConfigured = false;
2922 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002923 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002924 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002925 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002926 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002927 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2928 return DEAD_OBJECT;
2929 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002930 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002931 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002932 if (streamReConfigured) {
2933 mInterface->onStreamReConfigured(outputStream->getId());
2934 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002935 }
2936 }
2937
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002938 // Request thread needs to know to avoid using repeat-last-settings protocol
2939 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002940 if (notifyRequestThread) {
2941 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2942 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002943
Zhijun He90f7c372016-08-16 16:19:43 -07002944 char value[PROPERTY_VALUE_MAX];
2945 property_get("camera.fifo.disable", value, "0");
2946 int32_t disableFifo = atoi(value);
2947 if (disableFifo != 1) {
2948 // Boost priority of request thread to SCHED_FIFO.
2949 pid_t requestThreadTid = mRequestThread->getTid();
2950 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002951 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002952 if (res != OK) {
2953 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2954 strerror(-res), res);
2955 } else {
2956 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2957 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002958 }
2959
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002960 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002961 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2962 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2963 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2964 sessionParams.unlock(newSessionParams);
2965 mSessionParams.unlock(currentSessionParams);
2966 if (updateSessionParams) {
2967 mSessionParams = sessionParams;
2968 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002969
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002970 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002971
Ruben Brunk183f0562015-08-12 12:55:02 -07002972 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2973 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002974
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002975 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002976
Zhijun He0a210512014-07-24 13:45:15 -07002977 // tear down the deleted streams after configure streams.
2978 mDeletedStreams.clear();
2979
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002980 auto rc = mPreparerThread->resume();
2981 if (rc != OK) {
2982 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2983 return rc;
2984 }
2985
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002986 if (mDummyStreamId == NO_STREAM) {
2987 mRequestBufferSM.onStreamsConfigured();
2988 }
2989
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002990 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002991}
2992
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002993status_t Camera3Device::addDummyStreamLocked() {
2994 ATRACE_CALL();
2995 status_t res;
2996
2997 if (mDummyStreamId != NO_STREAM) {
2998 // Should never be adding a second dummy stream when one is already
2999 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003000 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
3001 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003002 return INVALID_OPERATION;
3003 }
3004
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003005 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003006
3007 sp<Camera3OutputStreamInterface> dummyStream =
3008 new Camera3DummyStream(mNextStreamId);
3009
3010 res = mOutputStreams.add(mNextStreamId, dummyStream);
3011 if (res < 0) {
3012 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
3013 return res;
3014 }
3015
3016 mDummyStreamId = mNextStreamId;
3017 mNextStreamId++;
3018
3019 return OK;
3020}
3021
3022status_t Camera3Device::tryRemoveDummyStreamLocked() {
3023 ATRACE_CALL();
3024 status_t res;
3025
3026 if (mDummyStreamId == NO_STREAM) return OK;
3027 if (mOutputStreams.size() == 1) return OK;
3028
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003029 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003030
3031 // Ok, have a dummy stream and there's at least one other output stream,
3032 // so remove the dummy
3033
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003034 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mDummyStreamId);
3035 if (deletedStream == nullptr) {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003036 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
3037 return INVALID_OPERATION;
3038 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003039 mOutputStreams.remove(mDummyStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07003040
3041 // Free up the stream endpoint so that it can be used by some other stream
3042 res = deletedStream->disconnect();
3043 if (res != OK) {
3044 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
3045 // fall through since we want to still list the stream as deleted.
3046 }
3047 mDeletedStreams.add(deletedStream);
3048 mDummyStreamId = NO_STREAM;
3049
3050 return res;
3051}
3052
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003053void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003054 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003055 Mutex::Autolock l(mLock);
3056 va_list args;
3057 va_start(args, fmt);
3058
3059 setErrorStateLockedV(fmt, args);
3060
3061 va_end(args);
3062}
3063
3064void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003065 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003066 Mutex::Autolock l(mLock);
3067 setErrorStateLockedV(fmt, args);
3068}
3069
3070void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
3071 va_list args;
3072 va_start(args, fmt);
3073
3074 setErrorStateLockedV(fmt, args);
3075
3076 va_end(args);
3077}
3078
3079void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003080 // Print out all error messages to log
3081 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003082 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003083
3084 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07003085 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003086
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003087 mErrorCause = errorCause;
3088
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07003089 if (mRequestThread != nullptr) {
3090 mRequestThread->setPaused(true);
3091 }
Ruben Brunk183f0562015-08-12 12:55:02 -07003092 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003093
3094 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003095 sp<NotificationListener> listener = mListener.promote();
3096 if (listener != NULL) {
3097 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003098 CaptureResultExtras());
3099 }
3100
3101 // Save stack trace. View by dumping it later.
3102 CameraTraces::saveTrace();
3103 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003104}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003105
3106/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003107 * In-flight request management
3108 */
3109
Jianing Weicb0652e2014-03-12 18:29:36 -07003110status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07003111 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003112 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003113 std::set<String8>& physicalCameraIds, bool isStillCapture,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003114 bool isZslCapture, const SurfaceMap& outputSurfaces) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003115 ATRACE_CALL();
3116 Mutex::Autolock l(mInFlightLock);
3117
3118 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07003119 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003120 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
3121 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003122 if (res < 0) return res;
3123
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003124 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01003125 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3126 // avoid a deadlock during reprocess requests.
3127 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003128 if (mStatusTracker != nullptr) {
3129 mStatusTracker->markComponentActive(mInFlightStatusId);
3130 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003131 }
3132
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003133 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003134 return OK;
3135}
3136
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003137void Camera3Device::returnOutputBuffers(
3138 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003139 nsecs_t timestamp, bool timestampIncreasing,
3140 const SurfaceMap& outputSurfaces,
3141 const CaptureResultExtras &inResultExtras) {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003142
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003143 for (size_t i = 0; i < numBuffers; i++)
3144 {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003145 if (outputBuffers[i].buffer == nullptr) {
3146 if (!mUseHalBufManager) {
3147 // With HAL buffer management API, HAL sometimes will have to return buffers that
3148 // has not got a output buffer handle filled yet. This is though illegal if HAL
3149 // buffer management API is not being used.
3150 ALOGE("%s: cannot return a null buffer!", __FUNCTION__);
3151 }
3152 continue;
3153 }
3154
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003155 Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream);
3156 int streamId = stream->getId();
3157 const auto& it = outputSurfaces.find(streamId);
3158 status_t res = OK;
3159 if (it != outputSurfaces.end()) {
3160 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003161 outputBuffers[i], timestamp, timestampIncreasing, it->second,
3162 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003163 } else {
3164 res = stream->returnBuffer(
Emilian Peev538c90e2018-12-17 18:03:19 +00003165 outputBuffers[i], timestamp, timestampIncreasing, std::vector<size_t> (),
3166 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003167 }
3168
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003169 // Note: stream may be deallocated at this point, if this buffer was
3170 // the last reference to it.
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003171 if (res == NO_INIT || res == DEAD_OBJECT) {
3172 ALOGV("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
3173 } else if (res != OK) {
3174 ALOGE("Can't return buffer to its stream: %s (%d)", strerror(-res), res);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003175 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003176
3177 // Long processing consumers can cause returnBuffer timeout for shared stream
3178 // If that happens, cancel the buffer and send a buffer error to client
3179 if (it != outputSurfaces.end() && res == TIMED_OUT &&
3180 outputBuffers[i].status == CAMERA3_BUFFER_STATUS_OK) {
3181 // cancel the buffer
3182 camera3_stream_buffer_t sb = outputBuffers[i];
3183 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
Emilian Peev538c90e2018-12-17 18:03:19 +00003184 stream->returnBuffer(sb, /*timestamp*/0, timestampIncreasing, std::vector<size_t> (),
3185 inResultExtras.frameNumber);
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003186
3187 // notify client buffer error
3188 sp<NotificationListener> listener;
3189 {
3190 Mutex::Autolock l(mOutputLock);
3191 listener = mListener.promote();
3192 }
3193
3194 if (listener != nullptr) {
3195 CaptureResultExtras extras = inResultExtras;
3196 extras.errorStreamId = streamId;
3197 listener->notifyError(
3198 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
3199 extras);
3200 }
3201 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003202 }
3203}
3204
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003205void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003206 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003207 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003208 mInFlightMap.removeItemsAt(idx, 1);
3209
3210 // Indicate idle inFlightMap to the status tracker
3211 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003212 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01003213 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3214 // avoid a deadlock during reprocess requests.
3215 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003216 if (mStatusTracker != nullptr) {
3217 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
3218 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003219 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003220 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003221}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003222
3223void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
3224
3225 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3226 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
3227
3228 nsecs_t sensorTimestamp = request.sensorTimestamp;
3229 nsecs_t shutterTimestamp = request.shutterTimestamp;
3230
3231 // Check if it's okay to remove the request from InFlightMap:
3232 // In the case of a successful request:
3233 // all input and output buffers, all result metadata, shutter callback
3234 // arrived.
3235 // In the case of a unsuccessful request:
3236 // all input and output buffers arrived.
3237 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07003238 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003239 (request.haveResultMetadata && shutterTimestamp != 0))) {
Emilian Peev9dd21f42018-08-03 13:39:29 +01003240 if (request.stillCapture) {
3241 ATRACE_ASYNC_END("still capture", frameNumber);
3242 }
3243
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003244 ATRACE_ASYNC_END("frame capture", frameNumber);
3245
Shuzhen Wang403044a2017-02-26 23:29:04 -08003246 // Sanity check - if sensor timestamp matches shutter timestamp in the
3247 // case of request having callback.
3248 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003249 sensorTimestamp != shutterTimestamp) {
3250 SET_ERR("sensor timestamp (%" PRId64
3251 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
3252 sensorTimestamp, frameNumber, shutterTimestamp);
3253 }
3254
3255 // for an unsuccessful request, it may have pending output buffers to
3256 // return.
3257 assert(request.requestStatus != OK ||
3258 request.pendingOutputBuffers.size() == 0);
3259 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003260 request.pendingOutputBuffers.size(), 0, /*timestampIncreasing*/true,
3261 request.outputSurfaces, request.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003262
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003263 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003264 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
3265 }
3266
3267 // Sanity check - if we have too many in-flight frames, something has
3268 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07003269 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003270 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07003271 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
3272 kInFlightWarnLimitHighSpeed) {
3273 CLOGE("In-flight list too large for high speed configuration: %zu",
3274 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003275 }
3276}
3277
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003278void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003279 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003280 { // First return buffers cached in mInFlightMap
3281 Mutex::Autolock l(mInFlightLock);
3282 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
3283 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3284 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003285 request.pendingOutputBuffers.size(), 0,
3286 /*timestampIncreasing*/true, request.outputSurfaces,
3287 request.resultExtras);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003288 }
3289 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07003290 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003291 }
3292
3293 // Then return all inflight buffers not returned by HAL
3294 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
3295 mInterface->getInflightBufferKeys(&inflightKeys);
3296
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003297 // Inflight buffers for HAL buffer manager
3298 std::vector<uint64_t> inflightRequestBufferKeys;
3299 mInterface->getInflightRequestBufferKeys(&inflightRequestBufferKeys);
3300
3301 // (streamId, frameNumber, buffer_handle_t*) tuple for all inflight buffers.
3302 // frameNumber will be -1 for buffers from HAL buffer manager
3303 std::vector<std::tuple<int32_t, int32_t, buffer_handle_t*>> inflightBuffers;
3304 inflightBuffers.reserve(inflightKeys.size() + inflightRequestBufferKeys.size());
3305
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003306 for (auto& pair : inflightKeys) {
3307 int32_t frameNumber = pair.first;
3308 int32_t streamId = pair.second;
3309 buffer_handle_t* buffer;
3310 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
3311 if (res != OK) {
3312 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
3313 __FUNCTION__, frameNumber, streamId);
3314 continue;
3315 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003316 inflightBuffers.push_back(std::make_tuple(streamId, frameNumber, buffer));
3317 }
3318
3319 for (auto& bufferId : inflightRequestBufferKeys) {
3320 int32_t streamId = -1;
3321 buffer_handle_t* buffer = nullptr;
3322 status_t res = mInterface->popInflightRequestBuffer(bufferId, &buffer, &streamId);
3323 if (res != OK) {
3324 ALOGE("%s: cannot find in-flight buffer %" PRIu64, __FUNCTION__, bufferId);
3325 continue;
3326 }
3327 inflightBuffers.push_back(std::make_tuple(streamId, /*frameNumber*/-1, buffer));
3328 }
3329
3330 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
3331 for (auto& tuple : inflightBuffers) {
3332 status_t res = OK;
3333 int32_t streamId = std::get<0>(tuple);
3334 int32_t frameNumber = std::get<1>(tuple);
3335 buffer_handle_t* buffer = std::get<2>(tuple);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003336
3337 camera3_stream_buffer_t streamBuffer;
3338 streamBuffer.buffer = buffer;
3339 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3340 streamBuffer.acquire_fence = -1;
3341 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003342
3343 // First check if the buffer belongs to deleted stream
3344 bool streamDeleted = false;
3345 for (auto& stream : mDeletedStreams) {
3346 if (streamId == stream->getId()) {
3347 streamDeleted = true;
3348 // Return buffer to deleted stream
3349 camera3_stream* halStream = stream->asHalStream();
3350 streamBuffer.stream = halStream;
3351 switch (halStream->stream_type) {
3352 case CAMERA3_STREAM_OUTPUT:
Emilian Peev538c90e2018-12-17 18:03:19 +00003353 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0,
3354 /*timestampIncreasing*/true, std::vector<size_t> (), frameNumber);
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003355 if (res != OK) {
3356 ALOGE("%s: Can't return output buffer for frame %d to"
3357 " stream %d: %s (%d)", __FUNCTION__,
3358 frameNumber, streamId, strerror(-res), res);
3359 }
3360 break;
3361 case CAMERA3_STREAM_INPUT:
3362 res = stream->returnInputBuffer(streamBuffer);
3363 if (res != OK) {
3364 ALOGE("%s: Can't return input buffer for frame %d to"
3365 " stream %d: %s (%d)", __FUNCTION__,
3366 frameNumber, streamId, strerror(-res), res);
3367 }
3368 break;
3369 default: // Bi-direcitonal stream is deprecated
3370 ALOGE("%s: stream %d has unknown stream type %d",
3371 __FUNCTION__, streamId, halStream->stream_type);
3372 break;
3373 }
3374 break;
3375 }
3376 }
3377 if (streamDeleted) {
3378 continue;
3379 }
3380
3381 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003382 if (streamId == inputStreamId) {
3383 streamBuffer.stream = mInputStream->asHalStream();
3384 res = mInputStream->returnInputBuffer(streamBuffer);
3385 if (res != OK) {
3386 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003387 " stream %d: %s (%d)", __FUNCTION__,
3388 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003389 }
3390 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003391 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
3392 if (stream == nullptr) {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003393 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
3394 continue;
3395 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003396 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003397 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
3398 }
3399 }
3400}
3401
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003402void Camera3Device::insertResultLocked(CaptureResult *result,
3403 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003404 if (result == nullptr) return;
3405
Emilian Peev71c73a22017-03-21 16:35:51 +00003406 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
3407 result->mMetadata.getAndLock());
3408 set_camera_metadata_vendor_id(meta, mVendorTagId);
3409 result->mMetadata.unlock(meta);
3410
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003411 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
3412 (int32_t*)&frameNumber, 1) != OK) {
3413 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
3414 return;
3415 }
3416
3417 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
3418 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
3419 return;
3420 }
3421
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003422 // Update vendor tag id for physical metadata
3423 for (auto& physicalMetadata : result->mPhysicalMetadatas) {
3424 camera_metadata_t *pmeta = const_cast<camera_metadata_t *>(
3425 physicalMetadata.mPhysicalCameraMetadata.getAndLock());
3426 set_camera_metadata_vendor_id(pmeta, mVendorTagId);
3427 physicalMetadata.mPhysicalCameraMetadata.unlock(pmeta);
3428 }
3429
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003430 // Valid result, insert into queue
3431 List<CaptureResult>::iterator queuedResult =
3432 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
3433 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
3434 ", burstId = %" PRId32, __FUNCTION__,
3435 queuedResult->mResultExtras.requestId,
3436 queuedResult->mResultExtras.frameNumber,
3437 queuedResult->mResultExtras.burstId);
3438
3439 mResultSignal.signal();
3440}
3441
3442
3443void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003444 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003445 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003446 Mutex::Autolock l(mOutputLock);
3447
3448 CaptureResult captureResult;
3449 captureResult.mResultExtras = resultExtras;
3450 captureResult.mMetadata = partialResult;
3451
Shuzhen Wang268a1362018-10-16 16:32:59 -07003452 // Fix up result metadata for monochrome camera.
3453 status_t res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3454 if (res != OK) {
3455 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3456 return;
3457 }
3458
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003459 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003460}
3461
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003462
3463void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
3464 CaptureResultExtras &resultExtras,
3465 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003466 uint32_t frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003467 bool reprocess,
3468 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003469 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003470 if (pendingMetadata.isEmpty())
3471 return;
3472
3473 Mutex::Autolock l(mOutputLock);
3474
3475 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003476 if (reprocess) {
3477 if (frameNumber < mNextReprocessResultFrameNumber) {
3478 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003479 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003480 frameNumber, mNextReprocessResultFrameNumber);
3481 return;
3482 }
3483 mNextReprocessResultFrameNumber = frameNumber + 1;
3484 } else {
3485 if (frameNumber < mNextResultFrameNumber) {
3486 SET_ERR("Out-of-order capture result metadata submitted! "
3487 "(got frame number %d, expecting %d)",
3488 frameNumber, mNextResultFrameNumber);
3489 return;
3490 }
3491 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003492 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003493
3494 CaptureResult captureResult;
3495 captureResult.mResultExtras = resultExtras;
3496 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003497 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003498
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003499 // Append any previous partials to form a complete result
3500 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
3501 captureResult.mMetadata.append(collectedPartialResult);
3502 }
3503
3504 captureResult.mMetadata.sort();
3505
3506 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003507 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3508 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003509 SET_ERR("No timestamp provided by HAL for frame %d!",
3510 frameNumber);
3511 return;
3512 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003513 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3514 camera_metadata_entry timestamp =
3515 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3516 if (timestamp.count == 0) {
3517 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
3518 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
3519 return;
3520 }
3521 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003522
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003523 // Fix up some result metadata to account for HAL-level distortion correction
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003524 status_t res =
3525 mDistortionMappers[mId.c_str()].correctCaptureResult(&captureResult.mMetadata);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003526 if (res != OK) {
3527 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
3528 frameNumber, strerror(res), res);
3529 return;
3530 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003531 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3532 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
3533 if (mDistortionMappers.find(cameraId8.c_str()) == mDistortionMappers.end()) {
3534 continue;
3535 }
3536 res = mDistortionMappers[cameraId8.c_str()].correctCaptureResult(
3537 &physicalMetadata.mPhysicalCameraMetadata);
3538 if (res != OK) {
3539 SET_ERR("Unable to correct physical capture result metadata for frame %d: %s (%d)",
3540 frameNumber, strerror(res), res);
3541 return;
3542 }
3543 }
3544
Shuzhen Wang268a1362018-10-16 16:32:59 -07003545 // Fix up result metadata for monochrome camera.
3546 res = fixupMonochromeTags(mDeviceInfo, captureResult.mMetadata);
3547 if (res != OK) {
3548 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3549 return;
3550 }
3551 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3552 String8 cameraId8(physicalMetadata.mPhysicalCameraId);
3553 res = fixupMonochromeTags(mPhysicalDeviceInfoMap.at(cameraId8.c_str()),
3554 physicalMetadata.mPhysicalCameraMetadata);
3555 if (res != OK) {
3556 SET_ERR("Failed to override result metadata: %s (%d)", strerror(-res), res);
3557 return;
3558 }
3559 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003560
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003561 std::unordered_map<std::string, CameraMetadata> monitoredPhysicalMetadata;
3562 for (auto& m : physicalMetadatas) {
3563 monitoredPhysicalMetadata.emplace(String8(m.mPhysicalCameraId).string(),
3564 CameraMetadata(m.mPhysicalCameraMetadata));
3565 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003566 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003567 frameNumber, timestamp.data.i64[0], captureResult.mMetadata,
3568 monitoredPhysicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003569
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003570 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003571}
3572
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003573/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003574 * Camera HAL device callback methods
3575 */
3576
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003577void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003578 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003579
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003580 status_t res;
3581
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003582 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003583 if (result->result == NULL && result->num_output_buffers == 0 &&
3584 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003585 SET_ERR("No result data provided by HAL for frame %d",
3586 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003587 return;
3588 }
Zhijun He204e3292014-07-14 17:09:23 -07003589
Zhijun He204e3292014-07-14 17:09:23 -07003590 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003591 result->result != NULL &&
3592 result->partial_result != 1) {
3593 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3594 " if partial result is not supported",
3595 frameNumber, result->partial_result);
3596 return;
3597 }
3598
3599 bool isPartialResult = false;
3600 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003601 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003602
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003603 // Get shutter timestamp and resultExtras from list of in-flight requests,
3604 // where it was added by the shutter notification for this frame. If the
3605 // shutter timestamp isn't received yet, append the output buffers to the
3606 // in-flight request and they will be returned when the shutter timestamp
3607 // arrives. Update the in-flight status and remove the in-flight entry if
3608 // all result data and shutter timestamp have been received.
3609 nsecs_t shutterTimestamp = 0;
3610
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003611 {
3612 Mutex::Autolock l(mInFlightLock);
3613 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3614 if (idx == NAME_NOT_FOUND) {
3615 SET_ERR("Unknown frame number for capture result: %d",
3616 frameNumber);
3617 return;
3618 }
3619 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003620 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3621 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003622 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003623 __FUNCTION__, request.resultExtras.requestId,
3624 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003625 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003626 // Always update the partial count to the latest one if it's not 0
3627 // (buffers only). When framework aggregates adjacent partial results
3628 // into one, the latest partial count will be used.
3629 if (result->partial_result != 0)
3630 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003631
3632 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003633 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003634 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3635 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3636 " the range of [1, %d] when metadata is included in the result",
3637 frameNumber, result->partial_result, mNumPartialResults);
3638 return;
3639 }
3640 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003641 if (isPartialResult && result->num_physcam_metadata) {
3642 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3643 " physical camera result", frameNumber);
3644 return;
3645 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003646 if (isPartialResult) {
3647 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003648 }
3649
Shuzhen Wang4a472662017-02-26 23:29:04 -08003650 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003651 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003652 sendPartialCaptureResult(result->result, request.resultExtras,
3653 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003654 }
3655 }
3656
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003657 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003658 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003659
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003660 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003661 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003662 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
3663 SET_ERR("Requested physical Camera Ids %d not equal to number of metadata %d",
3664 request.physicalCameraIds.size(), result->num_physcam_metadata);
3665 return;
3666 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003667 if (request.haveResultMetadata) {
3668 SET_ERR("Called multiple times with metadata for frame %d",
3669 frameNumber);
3670 return;
3671 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003672 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3673 String8 physicalId(result->physcam_ids[i]);
3674 std::set<String8>::iterator cameraIdIter =
3675 request.physicalCameraIds.find(physicalId);
3676 if (cameraIdIter != request.physicalCameraIds.end()) {
3677 request.physicalCameraIds.erase(cameraIdIter);
3678 } else {
3679 SET_ERR("Total result for frame %d has already returned for camera %s",
3680 frameNumber, physicalId.c_str());
3681 return;
3682 }
3683 }
Zhijun He204e3292014-07-14 17:09:23 -07003684 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003685 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003686 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003687 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003688 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003689 request.haveResultMetadata = true;
3690 }
3691
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003692 uint32_t numBuffersReturned = result->num_output_buffers;
3693 if (result->input_buffer != NULL) {
3694 if (hasInputBufferInRequest) {
3695 numBuffersReturned += 1;
3696 } else {
3697 ALOGW("%s: Input buffer should be NULL if there is no input"
3698 " buffer sent in the request",
3699 __FUNCTION__);
3700 }
3701 }
3702 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003703 if (request.numBuffersLeft < 0) {
3704 SET_ERR("Too many buffers returned for frame %d",
3705 frameNumber);
3706 return;
3707 }
3708
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003709 camera_metadata_ro_entry_t entry;
3710 res = find_camera_metadata_ro_entry(result->result,
3711 ANDROID_SENSOR_TIMESTAMP, &entry);
3712 if (res == OK && entry.count == 1) {
3713 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003714 }
3715
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003716 // If shutter event isn't received yet, append the output buffers to
3717 // the in-flight request. Otherwise, return the output buffers to
3718 // streams.
3719 if (shutterTimestamp == 0) {
3720 request.pendingOutputBuffers.appendArray(result->output_buffers,
3721 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003722 } else {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003723 bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003724 returnOutputBuffers(result->output_buffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003725 result->num_output_buffers, shutterTimestamp, timestampIncreasing,
3726 request.outputSurfaces, request.resultExtras);
Igor Murashkind2c90692013-04-02 12:32:32 -07003727 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003728
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003729 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003730 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3731 CameraMetadata physicalMetadata;
3732 physicalMetadata.append(result->physcam_metadata[i]);
3733 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3734 physicalMetadata});
3735 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003736 if (shutterTimestamp == 0) {
3737 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003738 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang268a1362018-10-16 16:32:59 -07003739 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003740 CameraMetadata metadata;
3741 metadata = result->result;
3742 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003743 collectedPartialResult, frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003744 hasInputBufferInRequest, request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003745 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003746 }
3747
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003748 removeInFlightRequestIfReadyLocked(idx);
3749 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003750
Zhijun Hef0d962a2014-06-30 10:24:11 -07003751 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003752 if (hasInputBufferInRequest) {
3753 Camera3Stream *stream =
3754 Camera3Stream::cast(result->input_buffer->stream);
3755 res = stream->returnInputBuffer(*(result->input_buffer));
3756 // Note: stream may be deallocated at this point, if this buffer was the
3757 // last reference to it.
3758 if (res != OK) {
3759 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3760 " its stream:%s (%d)", __FUNCTION__,
3761 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003762 }
3763 } else {
3764 ALOGW("%s: Input buffer should be NULL if there is no input"
3765 " buffer sent in the request, skipping input buffer return.",
3766 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003767 }
3768 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003769}
3770
3771void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003772 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003773 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003774 {
3775 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003776 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003777 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003778
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003779 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003780 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003781 return;
3782 }
3783
3784 switch (msg->type) {
3785 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003786 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003787 break;
3788 }
3789 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003790 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003791 break;
3792 }
3793 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003794 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003795 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003796 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003797}
3798
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003799void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003800 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003801 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003802 // Map camera HAL error codes to ICameraDeviceCallback error codes
3803 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003804 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003805 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003806 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003807 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003808 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003809 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003810 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003811 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003812 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003813 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003814 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003815 };
3816
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003817 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003818 ((msg.error_code >= 0) &&
3819 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3820 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003821 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003822
3823 int streamId = 0;
Emilian Peevedec62d2019-03-19 17:59:24 -07003824 String16 physicalCameraId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003825 if (msg.error_stream != NULL) {
3826 Camera3Stream *stream =
3827 Camera3Stream::cast(msg.error_stream);
3828 streamId = stream->getId();
Emilian Peevedec62d2019-03-19 17:59:24 -07003829 physicalCameraId = String16(stream->physicalCameraId());
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003830 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003831 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3832 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003833 streamId, msg.error_code);
3834
3835 CaptureResultExtras resultExtras;
3836 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003837 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003838 // SET_ERR calls notifyError
3839 SET_ERR("Camera HAL reported serious device error");
3840 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003841 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3842 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3843 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003844 {
3845 Mutex::Autolock l(mInFlightLock);
3846 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3847 if (idx >= 0) {
3848 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3849 r.requestStatus = msg.error_code;
3850 resultExtras = r.resultExtras;
Emilian Peevedec62d2019-03-19 17:59:24 -07003851 bool logicalDeviceResultError = false;
3852 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3853 errorCode) {
3854 if (physicalCameraId.size() > 0) {
3855 String8 cameraId(physicalCameraId);
3856 if (r.physicalCameraIds.find(cameraId) == r.physicalCameraIds.end()) {
3857 ALOGE("%s: Reported result failure for physical camera device: %s "
3858 " which is not part of the respective request!",
3859 __FUNCTION__, cameraId.string());
3860 break;
3861 }
3862 resultExtras.errorPhysicalCameraId = physicalCameraId;
3863 } else {
3864 logicalDeviceResultError = true;
3865 }
3866 }
3867
3868 if (logicalDeviceResultError
Shuzhen Wang20f57342017-08-24 15:39:05 -07003869 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3870 errorCode) {
3871 r.skipResultMetadata = true;
3872 }
Emilian Peevedec62d2019-03-19 17:59:24 -07003873 if (logicalDeviceResultError) {
Emilian Peevba0fac32017-03-30 09:05:34 +01003874 // In case of missing result check whether the buffers
3875 // returned. If they returned, then remove inflight
3876 // request.
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003877 // TODO: should we call this for ERROR_CAMERA_REQUEST as well?
3878 // otherwise we are depending on HAL to send the buffers back after
3879 // calling notifyError. Not sure if that's in the spec.
Emilian Peevba0fac32017-03-30 09:05:34 +01003880 removeInFlightRequestIfReadyLocked(idx);
3881 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003882 } else {
3883 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003884 ALOGE("Camera %s: %s: cannot find in-flight request on "
3885 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003886 resultExtras.frameNumber);
3887 }
3888 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003889 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003890 if (listener != NULL) {
3891 listener->notifyError(errorCode, resultExtras);
3892 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003893 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003894 }
3895 break;
3896 default:
3897 // SET_ERR calls notifyError
3898 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3899 break;
3900 }
3901}
3902
3903void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003904 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003905 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003906 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003907
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003908 // Set timestamp for the request in the in-flight tracking
3909 // and get the request ID to send upstream
3910 {
3911 Mutex::Autolock l(mInFlightLock);
3912 idx = mInFlightMap.indexOfKey(msg.frame_number);
3913 if (idx >= 0) {
3914 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003915
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003916 // Verify ordering of shutter notifications
3917 {
3918 Mutex::Autolock l(mOutputLock);
3919 // TODO: need to track errors for tighter bounds on expected frame number.
3920 if (r.hasInputBuffer) {
3921 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3922 SET_ERR("Shutter notification out-of-order. Expected "
3923 "notification for frame %d, got frame %d",
3924 mNextReprocessShutterFrameNumber, msg.frame_number);
3925 return;
3926 }
3927 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3928 } else {
3929 if (msg.frame_number < mNextShutterFrameNumber) {
3930 SET_ERR("Shutter notification out-of-order. Expected "
3931 "notification for frame %d, got frame %d",
3932 mNextShutterFrameNumber, msg.frame_number);
3933 return;
3934 }
3935 mNextShutterFrameNumber = msg.frame_number + 1;
3936 }
3937 }
3938
Shuzhen Wang4a472662017-02-26 23:29:04 -08003939 r.shutterTimestamp = msg.timestamp;
3940 if (r.hasCallback) {
3941 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003942 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003943 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003944 // Call listener, if any
3945 if (listener != NULL) {
3946 listener->notifyShutter(r.resultExtras, msg.timestamp);
3947 }
3948 // send pending result and buffers
3949 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3950 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003951 r.hasInputBuffer, r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003952 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003953 bool timestampIncreasing = !(r.zslCapture || r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003954 returnOutputBuffers(r.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003955 r.pendingOutputBuffers.size(), r.shutterTimestamp, timestampIncreasing,
3956 r.outputSurfaces, r.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003957 r.pendingOutputBuffers.clear();
3958
3959 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003960 }
3961 }
3962 if (idx < 0) {
3963 SET_ERR("Shutter notification for non-existent frame number %d",
3964 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003965 }
3966}
3967
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003968CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003969 ALOGV("%s", __FUNCTION__);
3970
Igor Murashkin1e479c02013-09-06 16:55:14 -07003971 CameraMetadata retVal;
3972
3973 if (mRequestThread != NULL) {
3974 retVal = mRequestThread->getLatestRequest();
3975 }
3976
Igor Murashkin1e479c02013-09-06 16:55:14 -07003977 return retVal;
3978}
3979
Jianing Weicb0652e2014-03-12 18:29:36 -07003980
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003981void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003982 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
3983 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata) {
3984
3985 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
3986 physicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003987}
3988
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003989/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003990 * HalInterface inner class methods
3991 */
3992
Yifan Hongf79b5542017-04-11 14:44:25 -07003993Camera3Device::HalInterface::HalInterface(
3994 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003995 std::shared_ptr<RequestMetadataQueue> queue,
3996 bool useHalBufManager) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003997 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003998 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08003999 mUseHalBufManager(useHalBufManager),
4000 mIsReconfigurationQuerySupported(true) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004001 // Check with hardware service manager if we can downcast these interfaces
4002 // Somewhat expensive, so cache the results at startup
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004003 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
4004 if (castResult_3_5.isOk()) {
4005 mHidlSession_3_5 = castResult_3_5;
4006 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004007 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4008 if (castResult_3_4.isOk()) {
4009 mHidlSession_3_4 = castResult_3_4;
4010 }
4011 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
4012 if (castResult_3_3.isOk()) {
4013 mHidlSession_3_3 = castResult_3_3;
4014 }
4015}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004016
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004017Camera3Device::HalInterface::HalInterface() : mUseHalBufManager(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004018
4019Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07004020 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004021 mRequestMetadataQueue(other.mRequestMetadataQueue),
4022 mUseHalBufManager(other.mUseHalBufManager) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004023
4024bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004025 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004026}
4027
4028void Camera3Device::HalInterface::clear() {
Emilian Peev644a3e12018-11-23 13:52:39 +00004029 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00004030 mHidlSession_3_4.clear();
4031 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004032 mHidlSession.clear();
4033}
4034
4035status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
4036 camera3_request_template_t templateId,
4037 /*out*/ camera_metadata_t **requestTemplate) {
4038 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
4039 if (!valid()) return INVALID_OPERATION;
4040 status_t res = OK;
4041
Emilian Peev31abd0a2017-05-11 18:37:46 +01004042 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004043
4044 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01004045 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004046 status = s;
4047 if (status == common::V1_0::Status::OK) {
4048 const camera_metadata *r =
4049 reinterpret_cast<const camera_metadata_t*>(request.data());
4050 size_t expectedSize = request.size();
4051 int ret = validate_camera_metadata_structure(r, &expectedSize);
4052 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
4053 *requestTemplate = clone_camera_metadata(r);
4054 if (*requestTemplate == nullptr) {
4055 ALOGE("%s: Unable to clone camera metadata received from HAL",
4056 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004057 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004058 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004059 } else {
4060 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
4061 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004062 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004063 }
4064 };
4065 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004066 RequestTemplate id;
4067 switch (templateId) {
4068 case CAMERA3_TEMPLATE_PREVIEW:
4069 id = RequestTemplate::PREVIEW;
4070 break;
4071 case CAMERA3_TEMPLATE_STILL_CAPTURE:
4072 id = RequestTemplate::STILL_CAPTURE;
4073 break;
4074 case CAMERA3_TEMPLATE_VIDEO_RECORD:
4075 id = RequestTemplate::VIDEO_RECORD;
4076 break;
4077 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
4078 id = RequestTemplate::VIDEO_SNAPSHOT;
4079 break;
4080 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
4081 id = RequestTemplate::ZERO_SHUTTER_LAG;
4082 break;
4083 case CAMERA3_TEMPLATE_MANUAL:
4084 id = RequestTemplate::MANUAL;
4085 break;
4086 default:
4087 // Unknown template ID, or this HAL is too old to support it
4088 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004089 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08004090 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004091
Emilian Peev31abd0a2017-05-11 18:37:46 +01004092 if (!err.isOk()) {
4093 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4094 res = DEAD_OBJECT;
4095 } else {
4096 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004097 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004098
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004099 return res;
4100}
4101
Emilian Peev4ec17882019-01-24 17:16:58 -08004102bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
4103 CameraMetadata& newSessionParams) {
4104 // We do reconfiguration by default;
4105 bool ret = true;
4106 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
4107 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
4108 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
4109 oldSessionParams.getAndLock());
4110 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
4111 newSessionParams.getAndLock());
4112 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
4113 get_camera_metadata_size(oldSessioMeta));
4114 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
4115 get_camera_metadata_size(newSessioMeta));
4116 hardware::camera::common::V1_0::Status callStatus;
4117 bool required;
4118 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
4119 bool requiredFlag) {
4120 callStatus = s;
4121 required = requiredFlag;
4122 };
4123 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
4124 oldSessionParams.unlock(oldSessioMeta);
4125 newSessionParams.unlock(newSessioMeta);
4126 if (err.isOk()) {
4127 switch (callStatus) {
4128 case hardware::camera::common::V1_0::Status::OK:
4129 ret = required;
4130 break;
4131 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
4132 mIsReconfigurationQuerySupported = false;
4133 ret = true;
4134 break;
4135 default:
4136 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
4137 ret = true;
4138 }
4139 } else {
4140 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
4141 ret = true;
4142 }
4143 }
4144
4145 return ret;
4146}
4147
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004148status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00004149 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004150 ATRACE_NAME("CameraHal::configureStreams");
4151 if (!valid()) return INVALID_OPERATION;
4152 status_t res = OK;
4153
Emilian Peev31abd0a2017-05-11 18:37:46 +01004154 // Convert stream config to HIDL
4155 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004156 device::V3_2::StreamConfiguration requestedConfiguration3_2;
4157 device::V3_4::StreamConfiguration requestedConfiguration3_4;
4158 requestedConfiguration3_2.streams.resize(config->num_streams);
4159 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004160 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004161 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
4162 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01004163 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004164
Emilian Peev31abd0a2017-05-11 18:37:46 +01004165 Camera3Stream* cam3stream = Camera3Stream::cast(src);
4166 cam3stream->setBufferFreedListener(this);
4167 int streamId = cam3stream->getId();
4168 StreamType streamType;
4169 switch (src->stream_type) {
4170 case CAMERA3_STREAM_OUTPUT:
4171 streamType = StreamType::OUTPUT;
4172 break;
4173 case CAMERA3_STREAM_INPUT:
4174 streamType = StreamType::INPUT;
4175 break;
4176 default:
4177 ALOGE("%s: Stream %d: Unsupported stream type %d",
4178 __FUNCTION__, streamId, config->streams[i]->stream_type);
4179 return BAD_VALUE;
4180 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004181 dst3_2.id = streamId;
4182 dst3_2.streamType = streamType;
4183 dst3_2.width = src->width;
4184 dst3_2.height = src->height;
4185 dst3_2.format = mapToPixelFormat(src->format);
4186 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
4187 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
4188 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
4189 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00004190 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004191 if (src->physical_camera_id != nullptr) {
4192 dst3_4.physicalCameraId = src->physical_camera_id;
4193 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004194
4195 activeStreams.insert(streamId);
4196 // Create Buffer ID map if necessary
4197 if (mBufferIdMaps.count(streamId) == 0) {
4198 mBufferIdMaps.emplace(streamId, BufferIdMap{});
4199 }
4200 }
4201 // remove BufferIdMap for deleted streams
4202 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
4203 int streamId = it->first;
4204 bool active = activeStreams.count(streamId) > 0;
4205 if (!active) {
4206 it = mBufferIdMaps.erase(it);
4207 } else {
4208 ++it;
4209 }
4210 }
4211
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004212 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004213 res = mapToStreamConfigurationMode(
4214 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004215 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004216 if (res != OK) {
4217 return res;
4218 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004219 requestedConfiguration3_2.operationMode = operationMode;
4220 requestedConfiguration3_4.operationMode = operationMode;
4221 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004222 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
4223 get_camera_metadata_size(sessionParams));
4224
Emilian Peev31abd0a2017-05-11 18:37:46 +01004225 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004226 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004227 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004228 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004229
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004230 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004231 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
4232 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004233 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004234 };
4235
4236 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
4237 (hardware::Return<void>& err) -> status_t {
4238 if (!err.isOk()) {
4239 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4240 return DEAD_OBJECT;
4241 }
4242 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
4243 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
4244 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
4245 }
4246 return OK;
4247 };
4248
4249 // See if we have v3.4 or v3.3 HAL
4250 if (mHidlSession_3_5 != nullptr) {
4251 ALOGV("%s: v3.5 device found", __FUNCTION__);
4252 device::V3_5::StreamConfiguration requestedConfiguration3_5;
4253 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
4254 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
4255 auto err = mHidlSession_3_5->configureStreams_3_5(
4256 requestedConfiguration3_5, configStream34Cb);
4257 res = postprocConfigStream34(err);
4258 if (res != OK) {
4259 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004260 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004261 } else if (mHidlSession_3_4 != nullptr) {
4262 // We do; use v3.4 for the call
4263 ALOGV("%s: v3.4 device found", __FUNCTION__);
4264 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
4265 auto err = mHidlSession_3_4->configureStreams_3_4(
4266 requestedConfiguration3_4, configStream34Cb);
4267 res = postprocConfigStream34(err);
4268 if (res != OK) {
4269 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004270 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004271 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004272 // We do; use v3.3 for the call
4273 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004274 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01004275 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004276 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004277 finalConfiguration = halConfiguration;
4278 status = s;
4279 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004280 if (!err.isOk()) {
4281 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4282 return DEAD_OBJECT;
4283 }
4284 } else {
4285 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
4286 ALOGV("%s: v3.2 device found", __FUNCTION__);
4287 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004288 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004289 [&status, &finalConfiguration_3_2]
4290 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
4291 finalConfiguration_3_2 = halConfiguration;
4292 status = s;
4293 });
4294 if (!err.isOk()) {
4295 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4296 return DEAD_OBJECT;
4297 }
4298 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
4299 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
4300 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
4301 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004302 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004303 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004304 }
4305
4306 if (status != common::V1_0::Status::OK ) {
4307 return CameraProviderManager::mapToStatusT(status);
4308 }
4309
4310 // And convert output stream configuration from HIDL
4311
4312 for (size_t i = 0; i < config->num_streams; i++) {
4313 camera3_stream_t *dst = config->streams[i];
4314 int streamId = Camera3Stream::cast(dst)->getId();
4315
4316 // Start scan at i, with the assumption that the stream order matches
4317 size_t realIdx = i;
4318 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004319 size_t halStreamCount = finalConfiguration.streams.size();
4320 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004321 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004322 found = true;
4323 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004324 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07004325 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004326 }
4327 if (!found) {
4328 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
4329 __FUNCTION__, streamId);
4330 return INVALID_OPERATION;
4331 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004332 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004333
Emilian Peev710c1422017-08-30 11:19:38 +01004334 Camera3Stream* dstStream = Camera3Stream::cast(dst);
4335 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004336 dstStream->setDataSpaceOverride(false);
4337 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
4338 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
4339
Emilian Peev31abd0a2017-05-11 18:37:46 +01004340 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
4341 if (dst->format != overrideFormat) {
4342 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
4343 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004344 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004345 if (dst->data_space != overrideDataSpace) {
4346 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
4347 streamId, dst->format);
4348 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004349 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01004350 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004351 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
4352
Emilian Peev31abd0a2017-05-11 18:37:46 +01004353 // Override allowed with IMPLEMENTATION_DEFINED
4354 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004355 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004356 }
4357
Emilian Peev31abd0a2017-05-11 18:37:46 +01004358 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004359 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004360 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004361 __FUNCTION__, streamId);
4362 return INVALID_OPERATION;
4363 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004364 dstStream->setUsage(
4365 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01004366 } else {
4367 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004368 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004369 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
4370 __FUNCTION__, streamId);
4371 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004372 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004373 dstStream->setUsage(
4374 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004375 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004376 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004377 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004378
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004379 return res;
4380}
4381
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004382status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004383 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004384 /*out*/std::vector<native_handle_t*>* handlesCreated,
4385 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004386 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004387 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
4388 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
4389 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004390 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004391 }
4392
4393 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07004394
4395 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004396
4397 {
4398 std::lock_guard<std::mutex> lock(mInflightLock);
4399 if (request->input_buffer != nullptr) {
4400 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
4401 buffer_handle_t buf = *(request->input_buffer->buffer);
4402 auto pair = getBufferId(buf, streamId);
4403 bool isNewBuffer = pair.first;
4404 uint64_t bufferId = pair.second;
4405 captureRequest->inputBuffer.streamId = streamId;
4406 captureRequest->inputBuffer.bufferId = bufferId;
4407 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
4408 captureRequest->inputBuffer.status = BufferStatus::OK;
4409 native_handle_t *acquireFence = nullptr;
4410 if (request->input_buffer->acquire_fence != -1) {
4411 acquireFence = native_handle_create(1,0);
4412 acquireFence->data[0] = request->input_buffer->acquire_fence;
4413 handlesCreated->push_back(acquireFence);
4414 }
4415 captureRequest->inputBuffer.acquireFence = acquireFence;
4416 captureRequest->inputBuffer.releaseFence = nullptr;
4417
4418 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004419 request->input_buffer->buffer);
4420 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004421 } else {
4422 captureRequest->inputBuffer.streamId = -1;
4423 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
4424 }
4425
4426 captureRequest->outputBuffers.resize(request->num_output_buffers);
4427 for (size_t i = 0; i < request->num_output_buffers; i++) {
4428 const camera3_stream_buffer_t *src = request->output_buffers + i;
4429 StreamBuffer &dst = captureRequest->outputBuffers[i];
4430 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004431 if (src->buffer != nullptr) {
4432 buffer_handle_t buf = *(src->buffer);
4433 auto pair = getBufferId(buf, streamId);
4434 bool isNewBuffer = pair.first;
4435 dst.bufferId = pair.second;
4436 dst.buffer = isNewBuffer ? buf : nullptr;
4437 native_handle_t *acquireFence = nullptr;
4438 if (src->acquire_fence != -1) {
4439 acquireFence = native_handle_create(1,0);
4440 acquireFence->data[0] = src->acquire_fence;
4441 handlesCreated->push_back(acquireFence);
4442 }
4443 dst.acquireFence = acquireFence;
4444 } else if (mUseHalBufManager) {
4445 // HAL buffer management path
4446 dst.bufferId = BUFFER_ID_NO_BUFFER;
4447 dst.buffer = nullptr;
4448 dst.acquireFence = nullptr;
4449 } else {
4450 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
4451 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004452 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004453 dst.streamId = streamId;
4454 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004455 dst.releaseFence = nullptr;
4456
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004457 // Output buffers are empty when using HAL buffer manager
4458 if (!mUseHalBufManager) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004459 pushInflightBufferLocked(captureRequest->frameNumber, streamId, src->buffer);
4460 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08004461 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004462 }
4463 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004464 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004465}
4466
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004467void Camera3Device::HalInterface::cleanupNativeHandles(
4468 std::vector<native_handle_t*> *handles, bool closeFd) {
4469 if (handles == nullptr) {
4470 return;
4471 }
4472 if (closeFd) {
4473 for (auto& handle : *handles) {
4474 native_handle_close(handle);
4475 }
4476 }
4477 for (auto& handle : *handles) {
4478 native_handle_delete(handle);
4479 }
4480 handles->clear();
4481 return;
4482}
4483
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004484status_t Camera3Device::HalInterface::processBatchCaptureRequests(
4485 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
4486 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
4487 if (!valid()) return INVALID_OPERATION;
4488
Emilian Peevaebbe412018-01-15 13:53:24 +00004489 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
4490 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4491 if (castResult_3_4.isOk()) {
4492 hidlSession_3_4 = castResult_3_4;
4493 }
4494
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004495 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00004496 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004497 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00004498 if (hidlSession_3_4 != nullptr) {
4499 captureRequests_3_4.resize(batchSize);
4500 } else {
4501 captureRequests.resize(batchSize);
4502 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004503 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004504 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004505
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004506 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004507 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004508 if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004509 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004510 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00004511 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004512 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
4513 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004514 }
4515 if (res != OK) {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004516 popInflightBuffers(inflightBuffers);
4517 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004518 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00004519 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004520 }
4521
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004522 std::vector<device::V3_2::BufferCache> cachesToRemove;
4523 {
4524 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4525 for (auto& pair : mFreedBuffers) {
4526 // The stream might have been removed since onBufferFreed
4527 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
4528 cachesToRemove.push_back({pair.first, pair.second});
4529 }
4530 }
4531 mFreedBuffers.clear();
4532 }
4533
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004534 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
4535 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07004536
4537 // Write metadata to FMQ.
4538 for (size_t i = 0; i < batchSize; i++) {
4539 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00004540 device::V3_2::CaptureRequest* captureRequest;
4541 if (hidlSession_3_4 != nullptr) {
4542 captureRequest = &captureRequests_3_4[i].v3_2;
4543 } else {
4544 captureRequest = &captureRequests[i];
4545 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004546
4547 if (request->settings != nullptr) {
4548 size_t settingsSize = get_camera_metadata_size(request->settings);
4549 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4550 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
4551 captureRequest->settings.resize(0);
4552 captureRequest->fmqSettingsSize = settingsSize;
4553 } else {
4554 if (mRequestMetadataQueue != nullptr) {
4555 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4556 }
4557 captureRequest->settings.setToExternal(
4558 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
4559 get_camera_metadata_size(request->settings));
4560 captureRequest->fmqSettingsSize = 0u;
4561 }
4562 } else {
4563 // A null request settings maps to a size-0 CameraMetadata
4564 captureRequest->settings.resize(0);
4565 captureRequest->fmqSettingsSize = 0u;
4566 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004567
4568 if (hidlSession_3_4 != nullptr) {
4569 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
4570 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00004571 if (request->physcam_settings != nullptr) {
4572 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
4573 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4574 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
4575 settingsSize)) {
4576 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
4577 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
4578 settingsSize;
4579 } else {
4580 if (mRequestMetadataQueue != nullptr) {
4581 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4582 }
4583 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
4584 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
4585 request->physcam_settings[j])),
4586 get_camera_metadata_size(request->physcam_settings[j]));
4587 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00004588 }
Emilian Peev00420d22018-02-05 21:33:13 +00004589 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00004590 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00004591 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00004592 }
4593 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
4594 request->physcam_id[j];
4595 }
4596 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004597 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004598
4599 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004600 auto resultCallback =
4601 [&status, &numRequestProcessed] (auto s, uint32_t n) {
4602 status = s;
4603 *numRequestProcessed = n;
4604 };
Emilian Peevaebbe412018-01-15 13:53:24 +00004605 if (hidlSession_3_4 != nullptr) {
4606 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004607 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004608 } else {
4609 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004610 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004611 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004612 if (!err.isOk()) {
4613 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004614 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004615 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004616
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004617 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
4618 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
4619 __FUNCTION__, *numRequestProcessed, batchSize);
4620 status = common::V1_0::Status::INTERNAL_ERROR;
4621 }
4622
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004623 res = CameraProviderManager::mapToStatusT(status);
4624 if (res == OK) {
4625 if (mHidlSession->isRemote()) {
4626 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
4627 // sent to camera HAL processes)
4628 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
4629 } else {
4630 // In passthrough mode the FDs are now owned by HAL
4631 cleanupNativeHandles(&handlesCreated);
4632 }
4633 } else {
4634 popInflightBuffers(inflightBuffers);
4635 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004636 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004637 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004638}
4639
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004640status_t Camera3Device::HalInterface::flush() {
4641 ATRACE_NAME("CameraHal::flush");
4642 if (!valid()) return INVALID_OPERATION;
4643 status_t res = OK;
4644
Emilian Peev31abd0a2017-05-11 18:37:46 +01004645 auto err = mHidlSession->flush();
4646 if (!err.isOk()) {
4647 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4648 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004649 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004650 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004651 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004652
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004653 return res;
4654}
4655
Emilian Peev31abd0a2017-05-11 18:37:46 +01004656status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004657 ATRACE_NAME("CameraHal::dump");
4658 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004659
Emilian Peev31abd0a2017-05-11 18:37:46 +01004660 // Handled by CameraProviderManager::dump
4661
4662 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004663}
4664
4665status_t Camera3Device::HalInterface::close() {
4666 ATRACE_NAME("CameraHal::close()");
4667 if (!valid()) return INVALID_OPERATION;
4668 status_t res = OK;
4669
Emilian Peev31abd0a2017-05-11 18:37:46 +01004670 auto err = mHidlSession->close();
4671 // Interface will be dead shortly anyway, so don't log errors
4672 if (!err.isOk()) {
4673 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004674 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004675
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004676 return res;
4677}
4678
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004679void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
4680 ATRACE_NAME("CameraHal::signalPipelineDrain");
4681 if (!valid() || mHidlSession_3_5 == nullptr) {
4682 ALOGE("%s called on invalid camera!", __FUNCTION__);
4683 return;
4684 }
4685
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004686 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004687 if (!err.isOk()) {
4688 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4689 return;
4690 }
4691}
4692
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004693void Camera3Device::HalInterface::getInflightBufferKeys(
4694 std::vector<std::pair<int32_t, int32_t>>* out) {
4695 std::lock_guard<std::mutex> lock(mInflightLock);
4696 out->clear();
4697 out->reserve(mInflightBufferMap.size());
4698 for (auto& pair : mInflightBufferMap) {
4699 uint64_t key = pair.first;
4700 int32_t streamId = key & 0xFFFFFFFF;
4701 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
4702 out->push_back(std::make_pair(frameNumber, streamId));
4703 }
4704 return;
4705}
4706
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004707void Camera3Device::HalInterface::getInflightRequestBufferKeys(
4708 std::vector<uint64_t>* out) {
4709 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4710 out->clear();
4711 out->reserve(mRequestedBuffers.size());
4712 for (auto& pair : mRequestedBuffers) {
4713 out->push_back(pair.first);
4714 }
4715 return;
4716}
4717
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004718status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004719 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004720 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004721 mInflightBufferMap[key] = buffer;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004722 return OK;
4723}
4724
4725status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004726 int32_t frameNumber, int32_t streamId,
4727 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004728 std::lock_guard<std::mutex> lock(mInflightLock);
4729
4730 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4731 auto it = mInflightBufferMap.find(key);
4732 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004733 if (buffer != nullptr) {
4734 *buffer = it->second;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004735 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004736 mInflightBufferMap.erase(it);
4737 return OK;
4738}
4739
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004740void Camera3Device::HalInterface::popInflightBuffers(
4741 const std::vector<std::pair<int32_t, int32_t>>& buffers) {
4742 for (const auto& pair : buffers) {
4743 int32_t frameNumber = pair.first;
4744 int32_t streamId = pair.second;
4745 popInflightBuffer(frameNumber, streamId, nullptr);
4746 }
4747}
4748
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004749status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004750 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004751 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004752 auto pair = mRequestedBuffers.insert({bufferId, {streamId, buf}});
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004753 if (!pair.second) {
4754 ALOGE("%s: bufId %" PRIu64 " is already inflight!",
4755 __FUNCTION__, bufferId);
4756 return BAD_VALUE;
4757 }
4758 return OK;
4759}
4760
4761// Find and pop a buffer_handle_t based on bufferId
4762status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004763 uint64_t bufferId,
4764 /*out*/ buffer_handle_t** buffer,
4765 /*optional out*/ int32_t* streamId) {
4766 if (buffer == nullptr) {
4767 ALOGE("%s: buffer (%p) must not be null", __FUNCTION__, buffer);
4768 return BAD_VALUE;
4769 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004770 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4771 auto it = mRequestedBuffers.find(bufferId);
4772 if (it == mRequestedBuffers.end()) {
4773 ALOGE("%s: bufId %" PRIu64 " is not inflight!",
4774 __FUNCTION__, bufferId);
4775 return BAD_VALUE;
4776 }
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004777 *buffer = it->second.second;
4778 if (streamId != nullptr) {
4779 *streamId = it->second.first;
4780 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004781 mRequestedBuffers.erase(it);
4782 return OK;
4783}
4784
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004785std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4786 const buffer_handle_t& buf, int streamId) {
4787 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4788
4789 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4790 auto it = bIdMap.find(buf);
4791 if (it == bIdMap.end()) {
4792 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004793 ALOGV("stream %d now have %zu buffer caches, buf %p",
4794 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004795 return std::make_pair(true, mNextBufferId - 1);
4796 } else {
4797 return std::make_pair(false, it->second);
4798 }
4799}
4800
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004801void Camera3Device::HalInterface::onBufferFreed(
4802 int streamId, const native_handle_t* handle) {
4803 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4804 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4805 auto mapIt = mBufferIdMaps.find(streamId);
4806 if (mapIt == mBufferIdMaps.end()) {
4807 // streamId might be from a deleted stream here
4808 ALOGI("%s: stream %d has been removed",
4809 __FUNCTION__, streamId);
4810 return;
4811 }
4812 BufferIdMap& bIdMap = mapIt->second;
4813 auto it = bIdMap.find(handle);
4814 if (it == bIdMap.end()) {
4815 ALOGW("%s: cannot find buffer %p in stream %d",
4816 __FUNCTION__, handle, streamId);
4817 return;
4818 } else {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004819 bufferId = it->second;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004820 bIdMap.erase(it);
4821 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4822 __FUNCTION__, streamId, bIdMap.size(), handle);
4823 }
4824 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4825}
4826
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004827void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
4828 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4829 auto mapIt = mBufferIdMaps.find(streamId);
4830 if (mapIt == mBufferIdMaps.end()) {
4831 ALOGE("%s: streamId %d not found!", __FUNCTION__, streamId);
4832 return;
4833 }
4834
4835 BufferIdMap& bIdMap = mapIt->second;
4836 for (const auto& it : bIdMap) {
4837 uint64_t bufferId = it.second;
4838 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4839 }
4840 bIdMap.clear();
4841}
4842
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004843/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004844 * RequestThread inner class methods
4845 */
4846
4847Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004848 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004849 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
4850 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004851 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004852 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004853 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004854 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004855 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004856 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004857 mReconfigured(false),
4858 mDoPause(false),
4859 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004860 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004861 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004862 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004863 mCurrentAfTriggerId(0),
4864 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004865 mRepeatingLastFrameNumber(
4866 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004867 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004868 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004869 mRequestLatency(kRequestLatencyBinSize),
4870 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004871 mLatestSessionParams(sessionParamKeys.size()),
4872 mUseHalBufManager(useHalBufManager) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004873 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004874}
4875
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004876Camera3Device::RequestThread::~RequestThread() {}
4877
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004878void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004879 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004880 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004881 Mutex::Autolock l(mRequestLock);
4882 mListener = listener;
4883}
4884
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004885void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4886 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004887 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004888 Mutex::Autolock l(mRequestLock);
4889 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004890 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004891 // Prepare video stream for high speed recording.
4892 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004893 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004894}
4895
Jianing Wei90e59c92014-03-12 18:29:36 -07004896status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004897 List<sp<CaptureRequest> > &requests,
4898 /*out*/
4899 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004900 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004901 Mutex::Autolock l(mRequestLock);
4902 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4903 ++it) {
4904 mRequestQueue.push_back(*it);
4905 }
4906
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004907 if (lastFrameNumber != NULL) {
4908 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4909 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4910 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4911 *lastFrameNumber);
4912 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004913
Jianing Wei90e59c92014-03-12 18:29:36 -07004914 unpauseForNewRequests();
4915
4916 return OK;
4917}
4918
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004919
4920status_t Camera3Device::RequestThread::queueTrigger(
4921 RequestTrigger trigger[],
4922 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004923 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004924 Mutex::Autolock l(mTriggerMutex);
4925 status_t ret;
4926
4927 for (size_t i = 0; i < count; ++i) {
4928 ret = queueTriggerLocked(trigger[i]);
4929
4930 if (ret != OK) {
4931 return ret;
4932 }
4933 }
4934
4935 return OK;
4936}
4937
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004938const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4939 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004940 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004941 if (d != nullptr) return d->mId;
4942 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004943}
4944
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004945status_t Camera3Device::RequestThread::queueTriggerLocked(
4946 RequestTrigger trigger) {
4947
4948 uint32_t tag = trigger.metadataTag;
4949 ssize_t index = mTriggerMap.indexOfKey(tag);
4950
4951 switch (trigger.getTagType()) {
4952 case TYPE_BYTE:
4953 // fall-through
4954 case TYPE_INT32:
4955 break;
4956 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004957 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4958 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004959 return INVALID_OPERATION;
4960 }
4961
4962 /**
4963 * Collect only the latest trigger, since we only have 1 field
4964 * in the request settings per trigger tag, and can't send more than 1
4965 * trigger per request.
4966 */
4967 if (index != NAME_NOT_FOUND) {
4968 mTriggerMap.editValueAt(index) = trigger;
4969 } else {
4970 mTriggerMap.add(tag, trigger);
4971 }
4972
4973 return OK;
4974}
4975
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004976status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004977 const RequestList &requests,
4978 /*out*/
4979 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004980 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004981 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004982 if (lastFrameNumber != NULL) {
4983 *lastFrameNumber = mRepeatingLastFrameNumber;
4984 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004985 mRepeatingRequests.clear();
4986 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4987 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004988
4989 unpauseForNewRequests();
4990
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004991 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004992 return OK;
4993}
4994
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004995bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004996 if (mRepeatingRequests.empty()) {
4997 return false;
4998 }
4999 int32_t requestId = requestIn->mResultExtras.requestId;
5000 const RequestList &repeatRequests = mRepeatingRequests;
5001 // All repeating requests are guaranteed to have same id so only check first quest
5002 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
5003 return (firstRequest->mResultExtras.requestId == requestId);
5004}
5005
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005006status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005007 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005008 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005009 return clearRepeatingRequestsLocked(lastFrameNumber);
5010
5011}
5012
5013status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005014 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005015 if (lastFrameNumber != NULL) {
5016 *lastFrameNumber = mRepeatingLastFrameNumber;
5017 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005018 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005019 return OK;
5020}
5021
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005022status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005023 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005024 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005025 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005026 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005027
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005028 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005029
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005030 // Send errors for all requests pending in the request queue, including
5031 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005032 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005033 if (listener != NULL) {
5034 for (RequestList::iterator it = mRequestQueue.begin();
5035 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005036 // Abort the input buffers for reprocess requests.
5037 if ((*it)->mInputStream != NULL) {
5038 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07005039 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
5040 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005041 if (res != OK) {
5042 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
5043 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5044 } else {
5045 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
5046 if (res != OK) {
5047 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
5048 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
5049 }
5050 }
5051 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005052 // Set the frame number this request would have had, if it
5053 // had been submitted; this frame number will not be reused.
5054 // The requestId and burstId fields were set when the request was
5055 // submitted originally (in convertMetadataListToRequestListLocked)
5056 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005057 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07005058 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07005059 }
5060 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005061 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08005062
5063 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005064 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005065 if (lastFrameNumber != NULL) {
5066 *lastFrameNumber = mRepeatingLastFrameNumber;
5067 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005068 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07005069 return OK;
5070}
5071
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005072status_t Camera3Device::RequestThread::flush() {
5073 ATRACE_CALL();
5074 Mutex::Autolock l(mFlushLock);
5075
Emilian Peev08dd2452017-04-06 16:55:14 +01005076 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005077}
5078
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005079void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005080 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005081 Mutex::Autolock l(mPauseLock);
5082 mDoPause = paused;
5083 mDoPauseSignal.signal();
5084}
5085
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005086status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
5087 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005088 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005089 Mutex::Autolock l(mLatestRequestMutex);
5090 status_t res;
5091 while (mLatestRequestId != requestId) {
5092 nsecs_t startTime = systemTime();
5093
5094 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
5095 if (res != OK) return res;
5096
5097 timeout -= (systemTime() - startTime);
5098 }
5099
5100 return OK;
5101}
5102
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005103void Camera3Device::RequestThread::requestExit() {
5104 // Call parent to set up shutdown
5105 Thread::requestExit();
5106 // The exit from any possible waits
5107 mDoPauseSignal.signal();
5108 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07005109
5110 mRequestLatency.log("ProcessCaptureRequest latency histogram");
5111 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005112}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005113
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005114void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005115 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005116 bool surfaceAbandoned = false;
5117 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005118 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005119 {
5120 Mutex::Autolock l(mRequestLock);
5121 // Check all streams needed by repeating requests are still valid. Otherwise, stop
5122 // repeating requests.
5123 for (const auto& request : mRepeatingRequests) {
5124 for (const auto& s : request->mOutputStreams) {
5125 if (s->isAbandoned()) {
5126 surfaceAbandoned = true;
5127 clearRepeatingRequestsLocked(&lastFrameNumber);
5128 break;
5129 }
5130 }
5131 if (surfaceAbandoned) {
5132 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005133 }
5134 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005135 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005136 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005137
5138 if (listener != NULL && surfaceAbandoned) {
5139 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07005140 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005141}
5142
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005143bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005144 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005145 status_t res;
5146 size_t batchSize = mNextRequests.size();
5147 std::vector<camera3_capture_request_t*> requests(batchSize);
5148 uint32_t numRequestProcessed = 0;
5149 for (size_t i = 0; i < batchSize; i++) {
5150 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07005151 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005152 }
5153
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005154 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
5155
5156 bool triggerRemoveFailed = false;
5157 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
5158 for (size_t i = 0; i < numRequestProcessed; i++) {
5159 NextRequest& nextRequest = mNextRequests.editItemAt(i);
5160 nextRequest.submitted = true;
5161
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005162 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00005163
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005164 if (!triggerRemoveFailed) {
5165 // Remove any previously queued triggers (after unlock)
5166 status_t removeTriggerRes = removeTriggers(mPrevRequest);
5167 if (removeTriggerRes != OK) {
5168 triggerRemoveFailed = true;
5169 triggerFailedRequest = nextRequest;
5170 }
5171 }
5172 }
5173
5174 if (triggerRemoveFailed) {
5175 SET_ERR("RequestThread: Unable to remove triggers "
5176 "(capture request %d, HAL device: %s (%d)",
5177 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
5178 cleanUpFailedRequests(/*sendRequestError*/ false);
5179 return false;
5180 }
5181
5182 if (res != OK) {
5183 // Should only get a failure here for malformed requests or device-level
5184 // errors, so consider all errors fatal. Bad metadata failures should
5185 // come through notify.
5186 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
5187 mNextRequests[numRequestProcessed].halRequest.frame_number,
5188 strerror(-res), res);
5189 cleanUpFailedRequests(/*sendRequestError*/ false);
5190 return false;
5191 }
5192 return true;
5193}
5194
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005195nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
5196 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
5197 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5198 find_camera_metadata_ro_entry(request,
5199 ANDROID_CONTROL_AE_MODE,
5200 &e);
5201 if (e.count == 0) return maxExpectedDuration;
5202
5203 switch (e.data.u8[0]) {
5204 case ANDROID_CONTROL_AE_MODE_OFF:
5205 find_camera_metadata_ro_entry(request,
5206 ANDROID_SENSOR_EXPOSURE_TIME,
5207 &e);
5208 if (e.count > 0) {
5209 maxExpectedDuration = e.data.i64[0];
5210 }
5211 find_camera_metadata_ro_entry(request,
5212 ANDROID_SENSOR_FRAME_DURATION,
5213 &e);
5214 if (e.count > 0) {
5215 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
5216 }
5217 break;
5218 default:
5219 find_camera_metadata_ro_entry(request,
5220 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
5221 &e);
5222 if (e.count > 1) {
5223 maxExpectedDuration = 1e9 / e.data.u8[0];
5224 }
5225 break;
5226 }
5227
5228 return maxExpectedDuration;
5229}
5230
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005231bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
5232 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
5233 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
5234 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
5235 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
5236 return true;
5237 }
5238
5239 return false;
5240}
5241
Shuzhen Wangc2cba122018-05-17 18:10:24 -07005242void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
5243 // Update the latest request sent to HAL
5244 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
5245 Mutex::Autolock al(mLatestRequestMutex);
5246
5247 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
5248 mLatestRequest.acquire(cloned);
5249
5250 mLatestPhysicalRequest.clear();
5251 for (uint32_t i = 0; i < nextRequest.halRequest.num_physcam_settings; i++) {
5252 cloned = clone_camera_metadata(nextRequest.halRequest.physcam_settings[i]);
5253 mLatestPhysicalRequest.emplace(nextRequest.halRequest.physcam_id[i],
5254 CameraMetadata(cloned));
5255 }
5256
5257 sp<Camera3Device> parent = mParent.promote();
5258 if (parent != NULL) {
5259 parent->monitorMetadata(TagMonitor::REQUEST,
5260 nextRequest.halRequest.frame_number,
5261 0, mLatestRequest, mLatestPhysicalRequest);
5262 }
5263 }
5264
5265 if (nextRequest.halRequest.settings != NULL) {
5266 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
5267 nextRequest.halRequest.settings);
5268 }
5269
5270 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
5271}
5272
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005273bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
5274 ATRACE_CALL();
5275 bool updatesDetected = false;
5276
Emilian Peev4ec17882019-01-24 17:16:58 -08005277 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005278 for (auto tag : mSessionParamKeys) {
5279 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005280 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005281
5282 if (entry.count > 0) {
5283 bool isDifferent = false;
5284 if (lastEntry.count > 0) {
5285 // Have a last value, compare to see if changed
5286 if (lastEntry.type == entry.type &&
5287 lastEntry.count == entry.count) {
5288 // Same type and count, compare values
5289 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
5290 size_t entryBytes = bytesPerValue * lastEntry.count;
5291 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
5292 if (cmp != 0) {
5293 isDifferent = true;
5294 }
5295 } else {
5296 // Count or type has changed
5297 isDifferent = true;
5298 }
5299 } else {
5300 // No last entry, so always consider to be different
5301 isDifferent = true;
5302 }
5303
5304 if (isDifferent) {
5305 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005306 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
5307 updatesDetected = true;
5308 }
Emilian Peev4ec17882019-01-24 17:16:58 -08005309 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005310 }
5311 } else if (lastEntry.count > 0) {
5312 // Value has been removed
5313 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08005314 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005315 updatesDetected = true;
5316 }
5317 }
5318
Emilian Peev4ec17882019-01-24 17:16:58 -08005319 bool reconfigureRequired;
5320 if (updatesDetected) {
5321 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
5322 updatedParams);
5323 mLatestSessionParams = updatedParams;
5324 } else {
5325 reconfigureRequired = false;
5326 }
5327
5328 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005329}
5330
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005331bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005332 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005333 status_t res;
5334
5335 // Handle paused state.
5336 if (waitIfPaused()) {
5337 return true;
5338 }
5339
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005340 // Wait for the next batch of requests.
5341 waitForNextRequestBatch();
5342 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005343 return true;
5344 }
5345
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005346 // Get the latest request ID, if any
5347 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005348 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00005349 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005350 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005351 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005352 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005353 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
5354 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005355 }
5356
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005357 // 'mNextRequests' will at this point contain either a set of HFR batched requests
5358 // or a single request from streaming or burst. In either case the first element
5359 // should contain the latest camera settings that we need to check for any session
5360 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00005361 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005362 res = OK;
5363
5364 //Input stream buffers are already acquired at this point so an input stream
5365 //will not be able to move to idle state unless we force it.
5366 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5367 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
5368 if (res != OK) {
5369 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
5370 cleanUpFailedRequests(/*sendRequestError*/ false);
5371 return false;
5372 }
5373 }
5374
5375 if (res == OK) {
5376 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5377 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08005378 sp<Camera3Device> parent = mParent.promote();
5379 if (parent != nullptr) {
5380 parent->pauseStateNotify(true);
5381 }
5382
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005383 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5384
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005385 if (parent != nullptr) {
5386 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
5387 }
5388
5389 statusTracker->markComponentActive(mStatusId);
5390 setPaused(false);
5391 }
5392
5393 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5394 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
5395 if (res != OK) {
5396 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
5397 cleanUpFailedRequests(/*sendRequestError*/ false);
5398 return false;
5399 }
5400 }
5401 }
5402 }
5403
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005404 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005405 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005406 if (res == TIMED_OUT) {
5407 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005408 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005409 // Check if any stream is abandoned.
5410 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005411 return true;
5412 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005413 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005414 return false;
5415 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005416
Zhijun Hecc27e112013-10-03 16:12:43 -07005417 // Inform waitUntilRequestProcessed thread of a new request ID
5418 {
5419 Mutex::Autolock al(mLatestRequestMutex);
5420
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005421 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07005422 mLatestRequestSignal.signal();
5423 }
5424
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005425 // Submit a batch of requests to HAL.
5426 // Use flush lock only when submitting multilple requests in a batch.
5427 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
5428 // which may take a long time to finish so synchronizing flush() and
5429 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
5430 // For now, only synchronize for high speed recording and we should figure something out for
5431 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005432 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07005433
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005434 if (useFlushLock) {
5435 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005436 }
5437
Zhijun Hef0645c12016-08-02 00:58:11 -07005438 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005439 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07005440
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005441 sp<Camera3Device> parent = mParent.promote();
5442 if (parent != nullptr) {
5443 parent->mRequestBufferSM.onSubmittingRequest();
5444 }
5445
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005446 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07005447 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07005448 submitRequestSuccess = sendRequestsBatch();
5449
Shuzhen Wang686f6442017-06-20 16:16:04 -07005450 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
5451 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07005452
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005453 if (useFlushLock) {
5454 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005455 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005456
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005457 // Unset as current request
5458 {
5459 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005460 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005461 }
5462
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005463 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005464}
5465
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005466status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005467 ATRACE_CALL();
5468
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005469 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005470 for (size_t i = 0; i < mNextRequests.size(); i++) {
5471 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005472 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5473 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5474 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5475
5476 // Prepare a request to HAL
5477 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
5478
5479 // Insert any queued triggers (before metadata is locked)
5480 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005481 if (res < 0) {
5482 SET_ERR("RequestThread: Unable to insert triggers "
5483 "(capture request %d, HAL device: %s (%d)",
5484 halRequest->frame_number, strerror(-res), res);
5485 return INVALID_OPERATION;
5486 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005487
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005488 int triggerCount = res;
5489 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
5490 mPrevTriggers = triggerCount;
5491
5492 // If the request is the same as last, or we had triggers last time
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005493 bool newRequest = (mPrevRequest != captureRequest || triggersMixedIn) &&
5494 // Request settings are all the same within one batch, so only treat the first
5495 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07005496 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00005497 if (newRequest) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005498 /**
5499 * HAL workaround:
5500 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
5501 */
5502 res = addDummyTriggerIds(captureRequest);
5503 if (res != OK) {
5504 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
5505 "(capture request %d, HAL device: %s (%d)",
5506 halRequest->frame_number, strerror(-res), res);
5507 return INVALID_OPERATION;
5508 }
5509
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005510 {
5511 // Correct metadata regions for distortion correction if enabled
5512 sp<Camera3Device> parent = mParent.promote();
5513 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07005514 List<PhysicalCameraSettings>::iterator it;
5515 for (it = captureRequest->mSettingsList.begin();
5516 it != captureRequest->mSettingsList.end(); it++) {
5517 if (parent->mDistortionMappers.find(it->cameraId) ==
5518 parent->mDistortionMappers.end()) {
5519 continue;
5520 }
5521 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
5522 &(it->metadata));
5523 if (res != OK) {
5524 SET_ERR("RequestThread: Unable to correct capture requests "
5525 "for lens distortion for request %d: %s (%d)",
5526 halRequest->frame_number, strerror(-res), res);
5527 return INVALID_OPERATION;
5528 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005529 }
5530 }
5531 }
5532
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005533 /**
5534 * The request should be presorted so accesses in HAL
5535 * are O(logn). Sidenote, sorting a sorted metadata is nop.
5536 */
Emilian Peevaebbe412018-01-15 13:53:24 +00005537 captureRequest->mSettingsList.begin()->metadata.sort();
5538 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005539 mPrevRequest = captureRequest;
5540 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
5541
5542 IF_ALOGV() {
5543 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5544 find_camera_metadata_ro_entry(
5545 halRequest->settings,
5546 ANDROID_CONTROL_AF_TRIGGER,
5547 &e
5548 );
5549 if (e.count > 0) {
5550 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
5551 __FUNCTION__,
5552 halRequest->frame_number,
5553 e.data.u8[0]);
5554 }
5555 }
5556 } else {
5557 // leave request.settings NULL to indicate 'reuse latest given'
5558 ALOGVV("%s: Request settings are REUSED",
5559 __FUNCTION__);
5560 }
5561
Emilian Peevaebbe412018-01-15 13:53:24 +00005562 if (captureRequest->mSettingsList.size() > 1) {
5563 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
5564 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00005565 if (newRequest) {
5566 halRequest->physcam_settings =
5567 new const camera_metadata* [halRequest->num_physcam_settings];
5568 } else {
5569 halRequest->physcam_settings = nullptr;
5570 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005571 auto it = ++captureRequest->mSettingsList.begin();
5572 size_t i = 0;
5573 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
5574 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00005575 if (newRequest) {
5576 it->metadata.sort();
5577 halRequest->physcam_settings[i] = it->metadata.getAndLock();
5578 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005579 }
5580 }
5581
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005582 uint32_t totalNumBuffers = 0;
5583
5584 // Fill in buffers
5585 if (captureRequest->mInputStream != NULL) {
5586 halRequest->input_buffer = &captureRequest->mInputBuffer;
5587 totalNumBuffers += 1;
5588 } else {
5589 halRequest->input_buffer = NULL;
5590 }
5591
5592 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
5593 captureRequest->mOutputStreams.size());
5594 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005595 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07005596
5597 sp<Camera3Device> parent = mParent.promote();
5598 if (parent == NULL) {
5599 // Should not happen, and nowhere to send errors to, so just log it
5600 CLOGE("RequestThread: Parent is gone");
5601 return INVALID_OPERATION;
5602 }
5603 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
5604
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005605 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005606 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005607 sp<Camera3OutputStreamInterface> outputStream =
5608 captureRequest->mOutputStreams.editItemAt(j);
5609 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005610
5611 // Prepare video buffers for high speed recording on the first video request.
5612 if (mPrepareVideoStream && outputStream->isVideoStream()) {
5613 // Only try to prepare video stream on the first video request.
5614 mPrepareVideoStream = false;
5615
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005616 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
5617 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005618 while (res == NOT_ENOUGH_DATA) {
5619 res = outputStream->prepareNextBuffer();
5620 }
5621 if (res != OK) {
5622 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
5623 __FUNCTION__, strerror(-res), res);
5624 outputStream->cancelPrepare();
5625 }
5626 }
5627
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005628 std::vector<size_t> uniqueSurfaceIds;
5629 res = outputStream->getUniqueSurfaceIds(
5630 captureRequest->mOutputSurfaces[streamId],
5631 &uniqueSurfaceIds);
5632 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
5633 if (res != OK && res != INVALID_OPERATION) {
5634 ALOGE("%s: failed to query stream %d unique surface IDs",
5635 __FUNCTION__, streamId);
5636 return res;
5637 }
5638 if (res == OK) {
5639 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
5640 }
5641
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005642 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005643 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005644 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005645 return TIMED_OUT;
5646 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005647 // HAL will request buffer through requestStreamBuffer API
5648 camera3_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
5649 buffer.stream = outputStream->asHalStream();
5650 buffer.buffer = nullptr;
5651 buffer.status = CAMERA3_BUFFER_STATUS_OK;
5652 buffer.acquire_fence = -1;
5653 buffer.release_fence = -1;
5654 } else {
5655 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
5656 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005657 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005658 if (res != OK) {
5659 // Can't get output buffer from gralloc queue - this could be due to
5660 // abandoned queue or other consumer misbehavior, so not a fatal
5661 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005662 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005663 " %s (%d)", strerror(-res), res);
5664
5665 return TIMED_OUT;
5666 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005667 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08005668
5669 {
5670 sp<Camera3Device> parent = mParent.promote();
5671 if (parent != nullptr) {
5672 const String8& streamCameraId = outputStream->getPhysicalCameraId();
5673 for (const auto& settings : captureRequest->mSettingsList) {
5674 if ((streamCameraId.isEmpty() &&
5675 parent->getId() == settings.cameraId.c_str()) ||
5676 streamCameraId == settings.cameraId.c_str()) {
5677 outputStream->fireBufferRequestForFrameNumber(
5678 captureRequest->mResultExtras.frameNumber,
5679 settings.metadata);
5680 }
5681 }
5682 }
5683 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07005684
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005685 String8 physicalCameraId = outputStream->getPhysicalCameraId();
5686
5687 if (!physicalCameraId.isEmpty()) {
5688 // Physical stream isn't supported for input request.
5689 if (halRequest->input_buffer) {
5690 CLOGE("Physical stream is not supported for input request");
5691 return INVALID_OPERATION;
5692 }
5693 requestedPhysicalCameras.insert(physicalCameraId);
5694 }
5695 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005696 }
5697 totalNumBuffers += halRequest->num_output_buffers;
5698
5699 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08005700 // If this request list is for constrained high speed recording (not
5701 // preview), and the current request is not the last one in the batch,
5702 // do not send callback to the app.
5703 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005704 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08005705 hasCallback = false;
5706 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005707 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005708 bool isZslCapture = false;
Emilian Peev9dd21f42018-08-03 13:39:29 +01005709 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
5710 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5711 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
5712 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
5713 isStillCapture = true;
5714 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
5715 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005716
5717 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
5718 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
5719 isZslCapture = true;
5720 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005721 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005722 res = parent->registerInFlight(halRequest->frame_number,
5723 totalNumBuffers, captureRequest->mResultExtras,
5724 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005725 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005726 calculateMaxExpectedDuration(halRequest->settings),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005727 requestedPhysicalCameras, isStillCapture, isZslCapture,
5728 (mUseHalBufManager) ? uniqueSurfaceIdMap :
5729 SurfaceMap{});
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005730 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
5731 ", burstId = %" PRId32 ".",
5732 __FUNCTION__,
5733 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
5734 captureRequest->mResultExtras.burstId);
5735 if (res != OK) {
5736 SET_ERR("RequestThread: Unable to register new in-flight request:"
5737 " %s (%d)", strerror(-res), res);
5738 return INVALID_OPERATION;
5739 }
5740 }
5741
5742 return OK;
5743}
5744
Igor Murashkin1e479c02013-09-06 16:55:14 -07005745CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005746 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005747 Mutex::Autolock al(mLatestRequestMutex);
5748
5749 ALOGV("RequestThread::%s", __FUNCTION__);
5750
5751 return mLatestRequest;
5752}
5753
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005754bool Camera3Device::RequestThread::isStreamPending(
5755 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005756 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005757 Mutex::Autolock l(mRequestLock);
5758
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005759 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005760 if (!nextRequest.submitted) {
5761 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5762 if (stream == s) return true;
5763 }
5764 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005765 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005766 }
5767
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005768 for (const auto& request : mRequestQueue) {
5769 for (const auto& s : request->mOutputStreams) {
5770 if (stream == s) return true;
5771 }
5772 if (stream == request->mInputStream) return true;
5773 }
5774
5775 for (const auto& request : mRepeatingRequests) {
5776 for (const auto& s : request->mOutputStreams) {
5777 if (stream == s) return true;
5778 }
5779 if (stream == request->mInputStream) return true;
5780 }
5781
5782 return false;
5783}
Jianing Weicb0652e2014-03-12 18:29:36 -07005784
Emilian Peev40ead602017-09-26 15:46:36 +01005785bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5786 ATRACE_CALL();
5787 Mutex::Autolock l(mRequestLock);
5788
5789 for (const auto& nextRequest : mNextRequests) {
5790 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5791 if (s.first == streamId) {
5792 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5793 if (it != s.second.end()) {
5794 return true;
5795 }
5796 }
5797 }
5798 }
5799
5800 for (const auto& request : mRequestQueue) {
5801 for (const auto& s : request->mOutputSurfaces) {
5802 if (s.first == streamId) {
5803 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5804 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005805 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005806 }
5807 }
5808 }
5809 }
5810
5811 for (const auto& request : mRepeatingRequests) {
5812 for (const auto& s : request->mOutputSurfaces) {
5813 if (s.first == streamId) {
5814 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5815 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005816 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005817 }
5818 }
5819 }
5820 }
5821
5822 return false;
5823}
5824
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005825void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
5826 if (!mUseHalBufManager) {
5827 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
5828 return;
5829 }
5830
5831 Mutex::Autolock pl(mPauseLock);
5832 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005833 mInterface->signalPipelineDrain(streamIds);
5834 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005835 }
5836 // If request thread is still busy, wait until paused then notify HAL
5837 mNotifyPipelineDrain = true;
5838 mStreamIdsToBeDrained = streamIds;
5839}
5840
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005841nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005842 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005843 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005844 return mExpectedInflightDuration > kMinInflightDuration ?
5845 mExpectedInflightDuration : kMinInflightDuration;
5846}
5847
Emilian Peevaebbe412018-01-15 13:53:24 +00005848void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
5849 camera3_capture_request_t *halRequest) {
5850 if ((request == nullptr) || (halRequest == nullptr)) {
5851 ALOGE("%s: Invalid request!", __FUNCTION__);
5852 return;
5853 }
5854
5855 if (halRequest->num_physcam_settings > 0) {
5856 if (halRequest->physcam_id != nullptr) {
5857 delete [] halRequest->physcam_id;
5858 halRequest->physcam_id = nullptr;
5859 }
5860 if (halRequest->physcam_settings != nullptr) {
5861 auto it = ++(request->mSettingsList.begin());
5862 size_t i = 0;
5863 for (; it != request->mSettingsList.end(); it++, i++) {
5864 it->metadata.unlock(halRequest->physcam_settings[i]);
5865 }
5866 delete [] halRequest->physcam_settings;
5867 halRequest->physcam_settings = nullptr;
5868 }
5869 }
5870}
5871
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005872void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5873 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005874 return;
5875 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005876
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005877 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005878 // Skip the ones that have been submitted successfully.
5879 if (nextRequest.submitted) {
5880 continue;
5881 }
5882
5883 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5884 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5885 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5886
5887 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005888 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005889 }
5890
Emilian Peevaebbe412018-01-15 13:53:24 +00005891 cleanupPhysicalSettings(captureRequest, halRequest);
5892
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005893 if (captureRequest->mInputStream != NULL) {
5894 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5895 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5896 }
5897
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005898 // No output buffer can be returned when using HAL buffer manager
5899 if (!mUseHalBufManager) {
5900 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5901 //Buffers that failed processing could still have
5902 //valid acquire fence.
5903 int acquireFence = (*outputBuffers)[i].acquire_fence;
5904 if (0 <= acquireFence) {
5905 close(acquireFence);
5906 outputBuffers->editItemAt(i).acquire_fence = -1;
5907 }
5908 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5909 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
5910 /*timestampIncreasing*/true, std::vector<size_t> (),
5911 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005912 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005913 }
5914
5915 if (sendRequestError) {
5916 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005917 sp<NotificationListener> listener = mListener.promote();
5918 if (listener != NULL) {
5919 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005920 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005921 captureRequest->mResultExtras);
5922 }
5923 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005924
5925 // Remove yet-to-be submitted inflight request from inflightMap
5926 {
5927 sp<Camera3Device> parent = mParent.promote();
5928 if (parent != NULL) {
5929 Mutex::Autolock l(parent->mInFlightLock);
5930 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5931 if (idx >= 0) {
5932 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5933 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5934 parent->removeInFlightMapEntryLocked(idx);
5935 }
5936 }
5937 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005938 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005939
5940 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005941 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005942}
5943
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005944void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005945 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005946 // Optimized a bit for the simple steady-state case (single repeating
5947 // request), to avoid putting that request in the queue temporarily.
5948 Mutex::Autolock l(mRequestLock);
5949
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005950 assert(mNextRequests.empty());
5951
5952 NextRequest nextRequest;
5953 nextRequest.captureRequest = waitForNextRequestLocked();
5954 if (nextRequest.captureRequest == nullptr) {
5955 return;
5956 }
5957
5958 nextRequest.halRequest = camera3_capture_request_t();
5959 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005960 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005961
5962 // Wait for additional requests
5963 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5964
5965 for (size_t i = 1; i < batchSize; i++) {
5966 NextRequest additionalRequest;
5967 additionalRequest.captureRequest = waitForNextRequestLocked();
5968 if (additionalRequest.captureRequest == nullptr) {
5969 break;
5970 }
5971
5972 additionalRequest.halRequest = camera3_capture_request_t();
5973 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005974 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005975 }
5976
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005977 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005978 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005979 mNextRequests.size(), batchSize);
5980 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005981 }
5982
5983 return;
5984}
5985
5986sp<Camera3Device::CaptureRequest>
5987 Camera3Device::RequestThread::waitForNextRequestLocked() {
5988 status_t res;
5989 sp<CaptureRequest> nextRequest;
5990
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005991 while (mRequestQueue.empty()) {
5992 if (!mRepeatingRequests.empty()) {
5993 // Always atomically enqueue all requests in a repeating request
5994 // list. Guarantees a complete in-sequence set of captures to
5995 // application.
5996 const RequestList &requests = mRepeatingRequests;
5997 RequestList::const_iterator firstRequest =
5998 requests.begin();
5999 nextRequest = *firstRequest;
6000 mRequestQueue.insert(mRequestQueue.end(),
6001 ++firstRequest,
6002 requests.end());
6003 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07006004
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006005 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07006006
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006007 break;
6008 }
6009
6010 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
6011
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006012 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
6013 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006014 Mutex::Autolock pl(mPauseLock);
6015 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006016 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006017 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006018 if (mNotifyPipelineDrain) {
6019 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6020 mNotifyPipelineDrain = false;
6021 mStreamIdsToBeDrained.clear();
6022 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006023 // Let the tracker know
6024 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6025 if (statusTracker != 0) {
6026 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6027 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006028 sp<Camera3Device> parent = mParent.promote();
6029 if (parent != nullptr) {
6030 parent->mRequestBufferSM.onRequestThreadPaused();
6031 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006032 }
6033 // Stop waiting for now and let thread management happen
6034 return NULL;
6035 }
6036 }
6037
6038 if (nextRequest == NULL) {
6039 // Don't have a repeating request already in hand, so queue
6040 // must have an entry now.
6041 RequestList::iterator firstRequest =
6042 mRequestQueue.begin();
6043 nextRequest = *firstRequest;
6044 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07006045 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
6046 sp<NotificationListener> listener = mListener.promote();
6047 if (listener != NULL) {
6048 listener->notifyRequestQueueEmpty();
6049 }
6050 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006051 }
6052
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006053 // In case we've been unpaused by setPaused clearing mDoPause, need to
6054 // update internal pause state (capture/setRepeatingRequest unpause
6055 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006056 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006057 if (mPaused) {
6058 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
6059 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6060 if (statusTracker != 0) {
6061 statusTracker->markComponentActive(mStatusId);
6062 }
6063 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006064 mPaused = false;
6065
6066 // Check if we've reconfigured since last time, and reset the preview
6067 // request if so. Can't use 'NULL request == repeat' across configure calls.
6068 if (mReconfigured) {
6069 mPrevRequest.clear();
6070 mReconfigured = false;
6071 }
6072
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006073 if (nextRequest != NULL) {
6074 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006075 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
6076 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006077
6078 // Since RequestThread::clear() removes buffers from the input stream,
6079 // get the right buffer here before unlocking mRequestLock
6080 if (nextRequest->mInputStream != NULL) {
6081 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
6082 if (res != OK) {
6083 // Can't get input buffer from gralloc queue - this could be due to
6084 // disconnected queue or other producer misbehavior, so not a fatal
6085 // error
6086 ALOGE("%s: Can't get input buffer, skipping request:"
6087 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006088
6089 sp<NotificationListener> listener = mListener.promote();
6090 if (listener != NULL) {
6091 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08006092 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07006093 nextRequest->mResultExtras);
6094 }
6095 return NULL;
6096 }
6097 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07006098 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07006099
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006100 return nextRequest;
6101}
6102
6103bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006104 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006105 status_t res;
6106 Mutex::Autolock l(mPauseLock);
6107 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006108 if (mPaused == false) {
6109 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006110 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07006111 if (mNotifyPipelineDrain) {
6112 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
6113 mNotifyPipelineDrain = false;
6114 mStreamIdsToBeDrained.clear();
6115 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08006116 // Let the tracker know
6117 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6118 if (statusTracker != 0) {
6119 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
6120 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006121 sp<Camera3Device> parent = mParent.promote();
6122 if (parent != nullptr) {
6123 parent->mRequestBufferSM.onRequestThreadPaused();
6124 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006125 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006126
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006127 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006128 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08006129 return true;
6130 }
6131 }
6132 // We don't set mPaused to false here, because waitForNextRequest needs
6133 // to further manage the paused state in case of starvation.
6134 return false;
6135}
6136
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006137void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006138 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006139 // With work to do, mark thread as unpaused.
6140 // If paused by request (setPaused), don't resume, to avoid
6141 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006142 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006143 Mutex::Autolock p(mPauseLock);
6144 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07006145 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
6146 if (mPaused) {
6147 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6148 if (statusTracker != 0) {
6149 statusTracker->markComponentActive(mStatusId);
6150 }
6151 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07006152 mPaused = false;
6153 }
6154}
6155
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07006156void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
6157 sp<Camera3Device> parent = mParent.promote();
6158 if (parent != NULL) {
6159 va_list args;
6160 va_start(args, fmt);
6161
6162 parent->setErrorStateV(fmt, args);
6163
6164 va_end(args);
6165 }
6166}
6167
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006168status_t Camera3Device::RequestThread::insertTriggers(
6169 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006170 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006171 Mutex::Autolock al(mTriggerMutex);
6172
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006173 sp<Camera3Device> parent = mParent.promote();
6174 if (parent == NULL) {
6175 CLOGE("RequestThread: Parent is gone");
6176 return DEAD_OBJECT;
6177 }
6178
Emilian Peevaebbe412018-01-15 13:53:24 +00006179 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006180 size_t count = mTriggerMap.size();
6181
6182 for (size_t i = 0; i < count; ++i) {
6183 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006184 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006185
6186 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
6187 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
6188 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07006189 if (isAeTrigger) {
6190 request->mResultExtras.precaptureTriggerId = triggerId;
6191 mCurrentPreCaptureTriggerId = triggerId;
6192 } else {
6193 request->mResultExtras.afTriggerId = triggerId;
6194 mCurrentAfTriggerId = triggerId;
6195 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01006196 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07006197 }
6198
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006199 camera_metadata_entry entry = metadata.find(tag);
6200
6201 if (entry.count > 0) {
6202 /**
6203 * Already has an entry for this trigger in the request.
6204 * Rewrite it with our requested trigger value.
6205 */
6206 RequestTrigger oldTrigger = trigger;
6207
6208 oldTrigger.entryValue = entry.data.u8[0];
6209
6210 mTriggerReplacedMap.add(tag, oldTrigger);
6211 } else {
6212 /**
6213 * More typical, no trigger entry, so we just add it
6214 */
6215 mTriggerRemovedMap.add(tag, trigger);
6216 }
6217
6218 status_t res;
6219
6220 switch (trigger.getTagType()) {
6221 case TYPE_BYTE: {
6222 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6223 res = metadata.update(tag,
6224 &entryValue,
6225 /*count*/1);
6226 break;
6227 }
6228 case TYPE_INT32:
6229 res = metadata.update(tag,
6230 &trigger.entryValue,
6231 /*count*/1);
6232 break;
6233 default:
6234 ALOGE("%s: Type not supported: 0x%x",
6235 __FUNCTION__,
6236 trigger.getTagType());
6237 return INVALID_OPERATION;
6238 }
6239
6240 if (res != OK) {
6241 ALOGE("%s: Failed to update request metadata with trigger tag %s"
6242 ", value %d", __FUNCTION__, trigger.getTagName(),
6243 trigger.entryValue);
6244 return res;
6245 }
6246
6247 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
6248 trigger.getTagName(),
6249 trigger.entryValue);
6250 }
6251
6252 mTriggerMap.clear();
6253
6254 return count;
6255}
6256
6257status_t Camera3Device::RequestThread::removeTriggers(
6258 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006259 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006260 Mutex::Autolock al(mTriggerMutex);
6261
Emilian Peevaebbe412018-01-15 13:53:24 +00006262 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006263
6264 /**
6265 * Replace all old entries with their old values.
6266 */
6267 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
6268 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
6269
6270 status_t res;
6271
6272 uint32_t tag = trigger.metadataTag;
6273 switch (trigger.getTagType()) {
6274 case TYPE_BYTE: {
6275 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6276 res = metadata.update(tag,
6277 &entryValue,
6278 /*count*/1);
6279 break;
6280 }
6281 case TYPE_INT32:
6282 res = metadata.update(tag,
6283 &trigger.entryValue,
6284 /*count*/1);
6285 break;
6286 default:
6287 ALOGE("%s: Type not supported: 0x%x",
6288 __FUNCTION__,
6289 trigger.getTagType());
6290 return INVALID_OPERATION;
6291 }
6292
6293 if (res != OK) {
6294 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
6295 ", trigger value %d", __FUNCTION__,
6296 trigger.getTagName(), trigger.entryValue);
6297 return res;
6298 }
6299 }
6300 mTriggerReplacedMap.clear();
6301
6302 /**
6303 * Remove all new entries.
6304 */
6305 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
6306 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
6307 status_t res = metadata.erase(trigger.metadataTag);
6308
6309 if (res != OK) {
6310 ALOGE("%s: Failed to erase metadata with trigger tag %s"
6311 ", trigger value %d", __FUNCTION__,
6312 trigger.getTagName(), trigger.entryValue);
6313 return res;
6314 }
6315 }
6316 mTriggerRemovedMap.clear();
6317
6318 return OK;
6319}
6320
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006321status_t Camera3Device::RequestThread::addDummyTriggerIds(
6322 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006323 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006324 static const int32_t dummyTriggerId = 1;
6325 status_t res;
6326
Emilian Peevaebbe412018-01-15 13:53:24 +00006327 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006328
6329 // If AF trigger is active, insert a dummy AF trigger ID if none already
6330 // exists
6331 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
6332 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
6333 if (afTrigger.count > 0 &&
6334 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
6335 afId.count == 0) {
6336 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
6337 if (res != OK) return res;
6338 }
6339
6340 // If AE precapture trigger is active, insert a dummy precapture trigger ID
6341 // if none already exists
6342 camera_metadata_entry pcTrigger =
6343 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
6344 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
6345 if (pcTrigger.count > 0 &&
6346 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
6347 pcId.count == 0) {
6348 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
6349 &dummyTriggerId, 1);
6350 if (res != OK) return res;
6351 }
6352
6353 return OK;
6354}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006355
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006356/**
6357 * PreparerThread inner class methods
6358 */
6359
6360Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07006361 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006362 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006363}
6364
6365Camera3Device::PreparerThread::~PreparerThread() {
6366 Thread::requestExitAndWait();
6367 if (mCurrentStream != nullptr) {
6368 mCurrentStream->cancelPrepare();
6369 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6370 mCurrentStream.clear();
6371 }
6372 clear();
6373}
6374
Ruben Brunkc78ac262015-08-13 17:58:46 -07006375status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006376 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006377 status_t res;
6378
6379 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006380 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006381
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006382 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006383 if (res == OK) {
6384 // No preparation needed, fire listener right off
6385 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006386 if (listener != NULL) {
6387 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006388 }
6389 return OK;
6390 } else if (res != NOT_ENOUGH_DATA) {
6391 return res;
6392 }
6393
6394 // Need to prepare, start up thread if necessary
6395 if (!mActive) {
6396 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
6397 // isn't running
6398 Thread::requestExitAndWait();
6399 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6400 if (res != OK) {
6401 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006402 if (listener != NULL) {
6403 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006404 }
6405 return res;
6406 }
6407 mCancelNow = false;
6408 mActive = true;
6409 ALOGV("%s: Preparer stream started", __FUNCTION__);
6410 }
6411
6412 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006413 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006414 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
6415
6416 return OK;
6417}
6418
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006419void Camera3Device::PreparerThread::pause() {
6420 ATRACE_CALL();
6421
6422 Mutex::Autolock l(mLock);
6423
6424 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
6425 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
6426 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
6427 int currentMaxCount = mCurrentMaxCount;
6428 mPendingStreams.clear();
6429 mCancelNow = true;
6430 while (mActive) {
6431 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
6432 if (res == TIMED_OUT) {
6433 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
6434 return;
6435 } else if (res != OK) {
6436 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
6437 return;
6438 }
6439 }
6440
6441 //Check whether the prepare thread was able to complete the current
6442 //stream. In case work is still pending emplace it along with the rest
6443 //of the streams in the pending list.
6444 if (currentStream != nullptr) {
6445 if (!mCurrentPrepareComplete) {
6446 pendingStreams.emplace(currentMaxCount, currentStream);
6447 }
6448 }
6449
6450 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
6451 for (const auto& it : mPendingStreams) {
6452 it.second->cancelPrepare();
6453 }
6454}
6455
6456status_t Camera3Device::PreparerThread::resume() {
6457 ATRACE_CALL();
6458 status_t res;
6459
6460 Mutex::Autolock l(mLock);
6461 sp<NotificationListener> listener = mListener.promote();
6462
6463 if (mActive) {
6464 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
6465 return NO_INIT;
6466 }
6467
6468 auto it = mPendingStreams.begin();
6469 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006470 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006471 if (res == OK) {
6472 if (listener != NULL) {
6473 listener->notifyPrepared(it->second->getId());
6474 }
6475 it = mPendingStreams.erase(it);
6476 } else if (res != NOT_ENOUGH_DATA) {
6477 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
6478 res, strerror(-res));
6479 it = mPendingStreams.erase(it);
6480 } else {
6481 it++;
6482 }
6483 }
6484
6485 if (mPendingStreams.empty()) {
6486 return OK;
6487 }
6488
6489 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6490 if (res != OK) {
6491 ALOGE("%s: Unable to start preparer stream: %d (%s)",
6492 __FUNCTION__, res, strerror(-res));
6493 return res;
6494 }
6495 mCancelNow = false;
6496 mActive = true;
6497 ALOGV("%s: Preparer stream started", __FUNCTION__);
6498
6499 return OK;
6500}
6501
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006502status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006503 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006504 Mutex::Autolock l(mLock);
6505
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006506 for (const auto& it : mPendingStreams) {
6507 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006508 }
6509 mPendingStreams.clear();
6510 mCancelNow = true;
6511
6512 return OK;
6513}
6514
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006515void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006516 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006517 Mutex::Autolock l(mLock);
6518 mListener = listener;
6519}
6520
6521bool Camera3Device::PreparerThread::threadLoop() {
6522 status_t res;
6523 {
6524 Mutex::Autolock l(mLock);
6525 if (mCurrentStream == nullptr) {
6526 // End thread if done with work
6527 if (mPendingStreams.empty()) {
6528 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
6529 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
6530 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
6531 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006532 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006533 return false;
6534 }
6535
6536 // Get next stream to prepare
6537 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006538 mCurrentStream = it->second;
6539 mCurrentMaxCount = it->first;
6540 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006541 mPendingStreams.erase(it);
6542 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
6543 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
6544 } else if (mCancelNow) {
6545 mCurrentStream->cancelPrepare();
6546 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6547 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
6548 mCurrentStream.clear();
6549 mCancelNow = false;
6550 return true;
6551 }
6552 }
6553
6554 res = mCurrentStream->prepareNextBuffer();
6555 if (res == NOT_ENOUGH_DATA) return true;
6556 if (res != OK) {
6557 // Something bad happened; try to recover by cancelling prepare and
6558 // signalling listener anyway
6559 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
6560 mCurrentStream->getId(), res, strerror(-res));
6561 mCurrentStream->cancelPrepare();
6562 }
6563
6564 // This stream has finished, notify listener
6565 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006566 sp<NotificationListener> listener = mListener.promote();
6567 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006568 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
6569 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006570 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006571 }
6572
6573 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6574 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006575 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006576
6577 return true;
6578}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006579
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006580status_t Camera3Device::RequestBufferStateMachine::initialize(
6581 sp<camera3::StatusTracker> statusTracker) {
6582 if (statusTracker == nullptr) {
6583 ALOGE("%s: statusTracker is null", __FUNCTION__);
6584 return BAD_VALUE;
6585 }
6586
6587 std::lock_guard<std::mutex> lock(mLock);
6588 mStatusTracker = statusTracker;
6589 mRequestBufferStatusId = statusTracker->addComponent();
6590 return OK;
6591}
6592
6593bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
6594 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006595 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006596 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006597 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006598 return true;
6599 }
6600 return false;
6601}
6602
6603void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
6604 std::lock_guard<std::mutex> lock(mLock);
6605 if (!mRequestBufferOngoing) {
6606 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
6607 return;
6608 }
6609 mRequestBufferOngoing = false;
6610 if (mStatus == RB_STATUS_PENDING_STOP) {
6611 checkSwitchToStopLocked();
6612 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006613 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006614}
6615
6616void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
6617 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006618 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006619 return;
6620}
6621
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006622void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006623 std::lock_guard<std::mutex> lock(mLock);
6624 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006625 // inflight map register actually happens in prepareHalRequest now, but it is close enough
6626 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006627 mInflightMapEmpty = false;
6628 if (mStatus == RB_STATUS_STOPPED) {
6629 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006630 }
6631 return;
6632}
6633
6634void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
6635 std::lock_guard<std::mutex> lock(mLock);
6636 mRequestThreadPaused = true;
6637 if (mStatus == RB_STATUS_PENDING_STOP) {
6638 checkSwitchToStopLocked();
6639 }
6640 return;
6641}
6642
6643void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
6644 std::lock_guard<std::mutex> lock(mLock);
6645 mInflightMapEmpty = true;
6646 if (mStatus == RB_STATUS_PENDING_STOP) {
6647 checkSwitchToStopLocked();
6648 }
6649 return;
6650}
6651
6652void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6653 std::lock_guard<std::mutex> lock(mLock);
6654 if (!checkSwitchToStopLocked()) {
6655 mStatus = RB_STATUS_PENDING_STOP;
6656 }
6657 return;
6658}
6659
6660void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6661 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6662 if (statusTracker != nullptr) {
6663 if (active) {
6664 statusTracker->markComponentActive(mRequestBufferStatusId);
6665 } else {
6666 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6667 }
6668 }
6669}
6670
6671bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6672 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6673 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006674 return true;
6675 }
6676 return false;
6677}
6678
Shuzhen Wang268a1362018-10-16 16:32:59 -07006679status_t Camera3Device::fixupMonochromeTags(const CameraMetadata& deviceInfo,
6680 CameraMetadata& resultMetadata) {
6681 status_t res = OK;
6682 if (!mNeedFixupMonochromeTags) {
6683 return res;
6684 }
6685
6686 // Remove tags that are not applicable to monochrome camera.
6687 int32_t tagsToRemove[] = {
6688 ANDROID_SENSOR_GREEN_SPLIT,
6689 ANDROID_SENSOR_NEUTRAL_COLOR_POINT,
6690 ANDROID_COLOR_CORRECTION_MODE,
6691 ANDROID_COLOR_CORRECTION_TRANSFORM,
6692 ANDROID_COLOR_CORRECTION_GAINS,
6693 };
6694 for (auto tag : tagsToRemove) {
6695 res = resultMetadata.erase(tag);
6696 if (res != OK) {
6697 ALOGE("%s: Failed to remove tag %d for monochrome camera", __FUNCTION__, tag);
6698 return res;
6699 }
6700 }
6701
6702 // ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL
6703 camera_metadata_entry blEntry = resultMetadata.find(ANDROID_SENSOR_DYNAMIC_BLACK_LEVEL);
6704 for (size_t i = 1; i < blEntry.count; i++) {
6705 blEntry.data.f[i] = blEntry.data.f[0];
6706 }
6707
6708 // ANDROID_SENSOR_NOISE_PROFILE
6709 camera_metadata_entry npEntry = resultMetadata.find(ANDROID_SENSOR_NOISE_PROFILE);
6710 if (npEntry.count > 0 && npEntry.count % 2 == 0) {
6711 double np[] = {npEntry.data.d[0], npEntry.data.d[1]};
6712 res = resultMetadata.update(ANDROID_SENSOR_NOISE_PROFILE, np, 2);
6713 if (res != OK) {
6714 ALOGE("%s: Failed to update SENSOR_NOISE_PROFILE: %s (%d)",
6715 __FUNCTION__, strerror(-res), res);
6716 return res;
6717 }
6718 }
6719
6720 // ANDROID_STATISTICS_LENS_SHADING_MAP
6721 camera_metadata_ro_entry lsSizeEntry = deviceInfo.find(ANDROID_LENS_INFO_SHADING_MAP_SIZE);
6722 camera_metadata_entry lsEntry = resultMetadata.find(ANDROID_STATISTICS_LENS_SHADING_MAP);
6723 if (lsSizeEntry.count == 2 && lsEntry.count > 0
6724 && (int32_t)lsEntry.count == 4 * lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]) {
6725 for (int32_t i = 0; i < lsSizeEntry.data.i32[0] * lsSizeEntry.data.i32[1]; i++) {
6726 lsEntry.data.f[4*i+1] = lsEntry.data.f[4*i];
6727 lsEntry.data.f[4*i+2] = lsEntry.data.f[4*i];
6728 lsEntry.data.f[4*i+3] = lsEntry.data.f[4*i];
6729 }
6730 }
6731
6732 // ANDROID_TONEMAP_CURVE_BLUE
6733 // ANDROID_TONEMAP_CURVE_GREEN
6734 // ANDROID_TONEMAP_CURVE_RED
6735 camera_metadata_entry tcbEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_BLUE);
6736 camera_metadata_entry tcgEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_GREEN);
6737 camera_metadata_entry tcrEntry = resultMetadata.find(ANDROID_TONEMAP_CURVE_RED);
6738 if (tcbEntry.count > 0
6739 && tcbEntry.count == tcgEntry.count
6740 && tcbEntry.count == tcrEntry.count) {
6741 for (size_t i = 0; i < tcbEntry.count; i++) {
6742 tcbEntry.data.f[i] = tcrEntry.data.f[i];
6743 tcgEntry.data.f[i] = tcrEntry.data.f[i];
6744 }
6745 }
6746
6747 return res;
6748}
6749
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006750}; // namespace android