blob: d8fbaf53f97fc4a7392ffc5c0ed6e7d9d7930e9a [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
61using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080062using namespace android::hardware::camera;
63using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064
65namespace android {
66
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080067Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080068 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080069 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070070 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070071 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070072 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070073 mUsePartialResult(false),
74 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080075 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070076 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070077 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070078 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070079 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000080 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010081 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
82 mLastTemplateId(-1)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083{
84 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080085 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086}
87
88Camera3Device::~Camera3Device()
89{
90 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080091 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -070092 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093}
94
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080095const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080096 return mId;
97}
98
Emilian Peevbd8c5032018-02-14 23:05:40 +000099status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800100 ATRACE_CALL();
101 Mutex::Autolock il(mInterfaceLock);
102 Mutex::Autolock l(mLock);
103
104 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
105 if (mStatus != STATUS_UNINITIALIZED) {
106 CLOGE("Already initialized!");
107 return INVALID_OPERATION;
108 }
109 if (manager == nullptr) return INVALID_OPERATION;
110
111 sp<ICameraDeviceSession> session;
112 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800113 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800114 /*out*/ &session);
115 ATRACE_END();
116 if (res != OK) {
117 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
118 return res;
119 }
120
Steven Moreland5ff9c912017-03-09 23:13:00 -0800121 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800122 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700123 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800124 session->close();
125 return res;
126 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800127
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700128 std::vector<std::string> physicalCameraIds;
129 bool isLogical = CameraProviderManager::isLogicalCamera(mDeviceInfo, &physicalCameraIds);
130 if (isLogical) {
131 for (auto& physicalId : physicalCameraIds) {
132 res = manager->getCameraCharacteristics(physicalId, &mPhysicalDeviceInfoMap[physicalId]);
133 if (res != OK) {
134 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
135 physicalId.c_str(), strerror(-res), res);
136 session->close();
137 return res;
138 }
139 }
140 }
141
Yifan Hongf79b5542017-04-11 14:44:25 -0700142 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700143 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
144 [&queue](const auto& descriptor) {
145 queue = std::make_shared<RequestMetadataQueue>(descriptor);
146 if (!queue->isValid() || queue->availableToWrite() <= 0) {
147 ALOGE("HAL returns empty request metadata fmq, not use it");
148 queue = nullptr;
149 // don't use the queue onwards.
150 }
151 });
152 if (!requestQueueRet.isOk()) {
153 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
154 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700155 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700156 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700157
158 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700159 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700160 [&resQueue](const auto& descriptor) {
161 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
162 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700163 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700164 resQueue = nullptr;
165 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700166 }
167 });
168 if (!resultQueueRet.isOk()) {
169 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
170 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700171 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700172 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700173 IF_ALOGV() {
174 session->interfaceChain([](
175 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
176 ALOGV("Session interface chain:");
177 for (auto iface : interfaceChain) {
178 ALOGV(" %s", iface.c_str());
179 }
180 });
181 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700182
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800183 camera_metadata_entry bufMgrMode =
184 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
185 if (bufMgrMode.count > 0) {
186 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
187 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
188 }
189
190 mInterface = new HalInterface(session, queue, mUseHalBufManager);
Emilian Peev71c73a22017-03-21 16:35:51 +0000191 std::string providerType;
192 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000193 mTagMonitor.initialize(mVendorTagId);
194 if (!monitorTags.isEmpty()) {
195 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
196 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800197
198 return initializeCommonLocked();
199}
200
201status_t Camera3Device::initializeCommonLocked() {
202
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700203 /** Start up status tracker thread */
204 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800205 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700206 if (res != OK) {
207 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
208 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800209 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700210 mStatusTracker.clear();
211 return res;
212 }
213
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700214 /** Register in-flight map to the status tracker */
215 mInFlightStatusId = mStatusTracker->addComponent();
216
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700217 if (mUseHalBufManager) {
218 res = mRequestBufferSM.initialize(mStatusTracker);
219 if (res != OK) {
220 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
221 strerror(-res), res);
222 mInterface->close();
223 mStatusTracker.clear();
224 return res;
225 }
226 }
227
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800228 /** Create buffer manager */
229 mBufferManager = new Camera3BufferManager();
230
231 Vector<int32_t> sessionParamKeys;
232 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
233 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
234 if (sessionKeysEntry.count > 0) {
235 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
236 }
237
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700238 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700239 mRequestThread = new RequestThread(
240 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800241 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800242 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700243 SET_ERR_L("Unable to start request queue thread: %s (%d)",
244 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800245 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800246 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 return res;
248 }
249
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700250 mPreparerThread = new PreparerThread();
251
Ruben Brunk183f0562015-08-12 12:55:02 -0700252 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800253 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700254 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700255 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800257
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800258 // Measure the clock domain offset between camera and video/hw_composer
259 camera_metadata_entry timestampSource =
260 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
261 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
262 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
263 mTimestampOffset = getMonoToBoottimeOffset();
264 }
265
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700266 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100267 camera_metadata_entry partialResultsCount =
268 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
269 if (partialResultsCount.count > 0) {
270 mNumPartialResults = partialResultsCount.data.i32[0];
271 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700272 }
273
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700274 camera_metadata_entry configs =
275 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
276 for (uint32_t i = 0; i < configs.count; i += 4) {
277 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
278 configs.data.i32[i + 3] ==
279 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
280 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
281 configs.data.i32[i + 2]));
282 }
283 }
284
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700285 if (DistortionMapper::isDistortionSupported(mDeviceInfo)) {
286 res = mDistortionMapper.setupStaticInfo(mDeviceInfo);
287 if (res != OK) {
288 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
289 return res;
290 }
291 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800292 return OK;
293}
294
295status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700296 return disconnectImpl();
297}
298
299status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700301 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800302
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700303 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700305 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700306 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700307 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 {
309 Mutex::Autolock l(mLock);
310 if (mStatus == STATUS_UNINITIALIZED) return res;
311
312 if (mStatus == STATUS_ACTIVE ||
313 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
314 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700315 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700317 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700318 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700319 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700320 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700321 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
322 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700323 // Continue to close device even in case of error
324 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700328 if (mStatus == STATUS_ERROR) {
329 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700330 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700331
332 if (mStatusTracker != NULL) {
333 mStatusTracker->requestExit();
334 }
335
336 if (mRequestThread != NULL) {
337 mRequestThread->requestExit();
338 }
339
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700340 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
341 for (size_t i = 0; i < mOutputStreams.size(); i++) {
342 streams.push_back(mOutputStreams[i]);
343 }
344 if (mInputStream != nullptr) {
345 streams.push_back(mInputStream);
346 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700347 }
348
349 // Joining done without holding mLock, otherwise deadlocks may ensue
350 // as the threads try to access parent state
351 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
352 // HAL may be in a bad state, so waiting for request thread
353 // (which may be stuck in the HAL processCaptureRequest call)
354 // could be dangerous.
355 mRequestThread->join();
356 }
357
358 if (mStatusTracker != NULL) {
359 mStatusTracker->join();
360 }
361
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800362 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700363 {
364 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800365 mRequestThread.clear();
Emilian Peev2843c362018-09-26 08:49:40 +0100366 Mutex::Autolock stLock(mTrackerLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700367 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800368 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700369 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700371 // Call close without internal mutex held, as the HAL close may need to
372 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800373 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700374
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700375 flushInflightRequests();
376
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700377 {
378 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800379 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700380 mOutputStreams.clear();
381 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700382 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700383 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700384 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700385 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800386
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700387 for (auto& weakStream : streams) {
388 sp<Camera3StreamInterface> stream = weakStream.promote();
389 if (stream != nullptr) {
390 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
391 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
392 }
393 }
394
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700395 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700396 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800397}
398
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700399// For dumping/debugging only -
400// try to acquire a lock a few times, eventually give up to proceed with
401// debug/dump operations
402bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
403 bool gotLock = false;
404 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
405 if (lock.tryLock() == NO_ERROR) {
406 gotLock = true;
407 break;
408 } else {
409 usleep(kDumpSleepDuration);
410 }
411 }
412 return gotLock;
413}
414
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700415Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
416 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100417 const int STREAM_CONFIGURATION_SIZE = 4;
418 const int STREAM_FORMAT_OFFSET = 0;
419 const int STREAM_WIDTH_OFFSET = 1;
420 const int STREAM_HEIGHT_OFFSET = 2;
421 const int STREAM_IS_INPUT_OFFSET = 3;
422 camera_metadata_ro_entry_t availableStreamConfigs =
423 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
424 if (availableStreamConfigs.count == 0 ||
425 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
426 return Size(0, 0);
427 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700428
Emilian Peev08dd2452017-04-06 16:55:14 +0100429 // Get max jpeg size (area-wise).
430 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
431 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
432 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
433 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
434 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
435 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
436 && format == HAL_PIXEL_FORMAT_BLOB &&
437 (width * height > maxJpegWidth * maxJpegHeight)) {
438 maxJpegWidth = width;
439 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700440 }
441 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100442
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700443 return Size(maxJpegWidth, maxJpegHeight);
444}
445
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800446nsecs_t Camera3Device::getMonoToBoottimeOffset() {
447 // try three times to get the clock offset, choose the one
448 // with the minimum gap in measurements.
449 const int tries = 3;
450 nsecs_t bestGap, measured;
451 for (int i = 0; i < tries; ++i) {
452 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
453 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
454 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
455 const nsecs_t gap = tmono2 - tmono;
456 if (i == 0 || gap < bestGap) {
457 bestGap = gap;
458 measured = tbase - ((tmono + tmono2) >> 1);
459 }
460 }
461 return measured;
462}
463
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800464hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
465 int frameworkFormat) {
466 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
467}
468
469DataspaceFlags Camera3Device::mapToHidlDataspace(
470 android_dataspace dataSpace) {
471 return dataSpace;
472}
473
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700474BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100475 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700476 return usage;
477}
478
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800479StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
480 switch (rotation) {
481 case CAMERA3_STREAM_ROTATION_0:
482 return StreamRotation::ROTATION_0;
483 case CAMERA3_STREAM_ROTATION_90:
484 return StreamRotation::ROTATION_90;
485 case CAMERA3_STREAM_ROTATION_180:
486 return StreamRotation::ROTATION_180;
487 case CAMERA3_STREAM_ROTATION_270:
488 return StreamRotation::ROTATION_270;
489 }
490 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
491 return StreamRotation::ROTATION_0;
492}
493
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800494status_t Camera3Device::mapToStreamConfigurationMode(
495 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
496 if (mode == nullptr) return BAD_VALUE;
497 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
498 switch(operationMode) {
499 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
500 *mode = StreamConfigurationMode::NORMAL_MODE;
501 break;
502 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
503 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
504 break;
505 default:
506 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
507 return BAD_VALUE;
508 }
509 } else {
510 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800511 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800512 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800513}
514
515camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
516 switch (status) {
517 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
518 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
519 }
520 return CAMERA3_BUFFER_STATUS_ERROR;
521}
522
523int Camera3Device::mapToFrameworkFormat(
524 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
525 return static_cast<uint32_t>(pixelFormat);
526}
527
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700528android_dataspace Camera3Device::mapToFrameworkDataspace(
529 DataspaceFlags dataSpace) {
530 return static_cast<android_dataspace>(dataSpace);
531}
532
Emilian Peev050f5dc2017-05-18 14:43:56 +0100533uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700534 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700535 return usage;
536}
537
Emilian Peev050f5dc2017-05-18 14:43:56 +0100538uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700539 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700540 return usage;
541}
542
Zhijun Hef7da0962014-04-24 13:27:56 -0700543ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700544 // Get max jpeg size (area-wise).
545 Size maxJpegResolution = getMaxJpegResolution();
546 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800547 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
548 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700549 return BAD_VALUE;
550 }
551
Zhijun Hef7da0962014-04-24 13:27:56 -0700552 // Get max jpeg buffer size
553 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700554 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
555 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800556 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
557 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700558 return BAD_VALUE;
559 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700560 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800561 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700562
563 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700564 float scaleFactor = ((float) (width * height)) /
565 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800566 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
567 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700568 if (jpegBufferSize > maxJpegBufferSize) {
569 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700570 }
571
572 return jpegBufferSize;
573}
574
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700575ssize_t Camera3Device::getPointCloudBufferSize() const {
576 const int FLOATS_PER_POINT=4;
577 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
578 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800579 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
580 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700581 return BAD_VALUE;
582 }
583 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
584 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
585 return maxBytesForPointCloud;
586}
587
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800588ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800589 const int PER_CONFIGURATION_SIZE = 3;
590 const int WIDTH_OFFSET = 0;
591 const int HEIGHT_OFFSET = 1;
592 const int SIZE_OFFSET = 2;
593 camera_metadata_ro_entry rawOpaqueSizes =
594 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800595 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800596 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800597 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
598 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800599 return BAD_VALUE;
600 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700601
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800602 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
603 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
604 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
605 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
606 }
607 }
608
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
610 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800611 return BAD_VALUE;
612}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700613
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800614status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
615 ATRACE_CALL();
616 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617
618 // Try to lock, but continue in case of failure (to avoid blocking in
619 // deadlocks)
620 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
621 bool gotLock = tryLockSpinRightRound(mLock);
622
623 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800624 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
625 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800627 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
628 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800630 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700631
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800632 String16 templatesOption("-t");
633 int n = args.size();
634 for (int i = 0; i < n; i++) {
635 if (args[i] == templatesOption) {
636 dumpTemplates = true;
637 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000638 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700639 if (i + 1 < n) {
640 String8 monitorTags = String8(args[i + 1]);
641 if (monitorTags == "off") {
642 mTagMonitor.disableMonitoring();
643 } else {
644 mTagMonitor.parseTagsToMonitor(monitorTags);
645 }
646 } else {
647 mTagMonitor.disableMonitoring();
648 }
649 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800650 }
651
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800653
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 const char *status =
655 mStatus == STATUS_ERROR ? "ERROR" :
656 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700657 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
658 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 mStatus == STATUS_ACTIVE ? "ACTIVE" :
660 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700661
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700663 if (mStatus == STATUS_ERROR) {
664 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
665 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800667 const char *mode =
668 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
669 mOperatingMode == static_cast<int>(
670 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
671 "CUSTOM";
672 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800673
674 if (mInputStream != NULL) {
675 write(fd, lines.string(), lines.size());
676 mInputStream->dump(fd, args);
677 } else {
678 lines.appendFormat(" No input stream.\n");
679 write(fd, lines.string(), lines.size());
680 }
681 for (size_t i = 0; i < mOutputStreams.size(); i++) {
682 mOutputStreams[i]->dump(fd,args);
683 }
684
Zhijun He431503c2016-03-07 17:30:16 -0800685 if (mBufferManager != NULL) {
686 lines = String8(" Camera3 Buffer Manager:\n");
687 write(fd, lines.string(), lines.size());
688 mBufferManager->dump(fd, args);
689 }
Zhijun He125684a2015-12-26 15:07:30 -0800690
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700691 lines = String8(" In-flight requests:\n");
692 if (mInFlightMap.size() == 0) {
693 lines.append(" None\n");
694 } else {
695 for (size_t i = 0; i < mInFlightMap.size(); i++) {
696 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700697 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700698 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800699 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700700 r.numBuffersLeft);
701 }
702 }
703 write(fd, lines.string(), lines.size());
704
Shuzhen Wang686f6442017-06-20 16:16:04 -0700705 if (mRequestThread != NULL) {
706 mRequestThread->dumpCaptureRequestLatency(fd,
707 " ProcessCaptureRequest latency histogram:");
708 }
709
Igor Murashkin1e479c02013-09-06 16:55:14 -0700710 {
711 lines = String8(" Last request sent:\n");
712 write(fd, lines.string(), lines.size());
713
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700715 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
716 }
717
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800718 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800719 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800720 "TEMPLATE_PREVIEW",
721 "TEMPLATE_STILL_CAPTURE",
722 "TEMPLATE_VIDEO_RECORD",
723 "TEMPLATE_VIDEO_SNAPSHOT",
724 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800725 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800726 };
727
728 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800729 camera_metadata_t *templateRequest = nullptr;
730 mInterface->constructDefaultRequestSettings(
731 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800732 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800733 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800734 lines.append(" Not supported\n");
735 write(fd, lines.string(), lines.size());
736 } else {
737 write(fd, lines.string(), lines.size());
738 dump_indented_camera_metadata(templateRequest,
739 fd, /*verbosity*/2, /*indentation*/8);
740 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800741 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800742 }
743 }
744
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700745 mTagMonitor.dumpMonitoredMetadata(fd);
746
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800747 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800748 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800750 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800751 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800752
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700753 if (gotLock) mLock.unlock();
754 if (gotInterfaceLock) mInterfaceLock.unlock();
755
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800756 return OK;
757}
758
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700759const CameraMetadata& Camera3Device::info(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
762 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700763 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 mStatus == STATUS_ERROR ?
765 "when in error state" : "before init");
766 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700767 if (physicalId.isEmpty()) {
768 return mDeviceInfo;
769 } else {
770 std::string id(physicalId.c_str());
771 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
772 return mPhysicalDeviceInfoMap.at(id);
773 } else {
774 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
775 return mDeviceInfo;
776 }
777 }
778}
779
780const CameraMetadata& Camera3Device::info() const {
781 String8 emptyId;
782 return info(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::checkStatusOkToCaptureLocked() {
786 switch (mStatus) {
787 case STATUS_ERROR:
788 CLOGE("Device has encountered a serious error");
789 return INVALID_OPERATION;
790 case STATUS_UNINITIALIZED:
791 CLOGE("Device not initialized");
792 return INVALID_OPERATION;
793 case STATUS_UNCONFIGURED:
794 case STATUS_CONFIGURED:
795 case STATUS_ACTIVE:
796 // OK
797 break;
798 default:
799 SET_ERR_L("Unexpected status: %d", mStatus);
800 return INVALID_OPERATION;
801 }
802 return OK;
803}
804
805status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000806 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700807 const std::list<const SurfaceMap> &surfaceMaps,
808 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700809 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700810 if (requestList == NULL) {
811 CLOGE("requestList cannot be NULL.");
812 return BAD_VALUE;
813 }
814
Jianing Weicb0652e2014-03-12 18:29:36 -0700815 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000816 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
818 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
819 ++metadataIt, ++surfaceMapIt) {
820 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700821 if (newRequest == 0) {
822 CLOGE("Can't create capture request");
823 return BAD_VALUE;
824 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700825
Shuzhen Wang9d066012016-09-30 11:30:20 -0700826 newRequest->mRepeating = repeating;
827
Jianing Weicb0652e2014-03-12 18:29:36 -0700828 // Setup burst Id and request Id
829 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000830 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
831 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700832 CLOGE("RequestID entry exists; but must not be empty in metadata");
833 return BAD_VALUE;
834 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000835 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
836 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700837 } else {
838 CLOGE("RequestID does not exist in metadata");
839 return BAD_VALUE;
840 }
841
Jianing Wei90e59c92014-03-12 18:29:36 -0700842 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700843
844 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700845 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700846 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
847 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
848 return BAD_VALUE;
849 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700850
851 // Setup batch size if this is a high speed video recording request.
852 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
853 auto firstRequest = requestList->begin();
854 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
855 if (outputStream->isVideoStream()) {
856 (*firstRequest)->mBatchSize = requestList->size();
857 break;
858 }
859 }
860 }
861
Jianing Wei90e59c92014-03-12 18:29:36 -0700862 return OK;
863}
864
Jianing Weicb0652e2014-03-12 18:29:36 -0700865status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800866 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867
Emilian Peevaebbe412018-01-15 13:53:24 +0000868 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700869 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000870 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700871
Emilian Peevaebbe412018-01-15 13:53:24 +0000872 return captureList(requestsList, surfaceMaps, /*lastFrameNumber*/NULL);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700873}
874
Emilian Peevaebbe412018-01-15 13:53:24 +0000875void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700876 std::list<const SurfaceMap>& surfaceMaps,
877 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000878 PhysicalCameraSettingsList requestList;
879 requestList.push_back({std::string(getId().string()), request});
880 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700881
882 SurfaceMap surfaceMap;
883 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
884 // With no surface list passed in, stream and surface will have 1-to-1
885 // mapping. So the surface index is 0 for each stream in the surfaceMap.
886 for (size_t i = 0; i < streams.count; i++) {
887 surfaceMap[streams.data.i32[i]].push_back(0);
888 }
889 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800890}
891
Jianing Wei90e59c92014-03-12 18:29:36 -0700892status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000893 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700894 const std::list<const SurfaceMap> &surfaceMaps,
895 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700896 /*out*/
897 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700898 ATRACE_CALL();
899 Mutex::Autolock il(mInterfaceLock);
900 Mutex::Autolock l(mLock);
901
902 status_t res = checkStatusOkToCaptureLocked();
903 if (res != OK) {
904 // error logged by previous call
905 return res;
906 }
907
908 RequestList requestList;
909
Shuzhen Wang0129d522016-10-30 22:43:41 -0700910 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
911 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700912 if (res != OK) {
913 // error logged by previous call
914 return res;
915 }
916
917 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700918 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700919 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700920 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700921 }
922
923 if (res == OK) {
924 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
925 if (res != OK) {
926 SET_ERR_L("Can't transition to active in %f seconds!",
927 kActiveTimeout/1e9);
928 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800929 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700930 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700931 } else {
932 CLOGE("Cannot queue request. Impossible.");
933 return BAD_VALUE;
934 }
935
936 return res;
937}
938
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700939hardware::Return<void> Camera3Device::requestStreamBuffers(
940 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
941 requestStreamBuffers_cb _hidl_cb) {
942 using hardware::camera::device::V3_5::BufferRequestStatus;
943 using hardware::camera::device::V3_5::StreamBufferRet;
944 using hardware::camera::device::V3_5::StreamBufferRequestError;
945
946 std::lock_guard<std::mutex> lock(mRequestBufferInterfaceLock);
947
948 hardware::hidl_vec<StreamBufferRet> bufRets;
949 if (!mUseHalBufManager) {
950 ALOGE("%s: Camera %s does not support HAL buffer management",
951 __FUNCTION__, mId.string());
952 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
953 return hardware::Void();
954 }
955
956 SortedVector<int32_t> streamIds;
957 ssize_t sz = streamIds.setCapacity(bufReqs.size());
958 if (sz < 0 || static_cast<size_t>(sz) != bufReqs.size()) {
959 ALOGE("%s: failed to allocate memory for %zu buffer requests",
960 __FUNCTION__, bufReqs.size());
961 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
962 return hardware::Void();
963 }
964
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700965 if (bufReqs.size() > mOutputStreams.size()) {
966 ALOGE("%s: too many buffer requests (%zu > # of output streams %zu)",
967 __FUNCTION__, bufReqs.size(), mOutputStreams.size());
968 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
969 return hardware::Void();
970 }
971
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700972 // Check for repeated streamId
973 for (const auto& bufReq : bufReqs) {
974 if (streamIds.indexOf(bufReq.streamId) != NAME_NOT_FOUND) {
975 ALOGE("%s: Stream %d appear multiple times in buffer requests",
976 __FUNCTION__, bufReq.streamId);
977 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, bufRets);
978 return hardware::Void();
979 }
980 streamIds.add(bufReq.streamId);
981 }
982
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700983 if (!mRequestBufferSM.startRequestBuffer()) {
984 ALOGE("%s: request buffer disallowed while camera service is configuring",
985 __FUNCTION__);
986 _hidl_cb(BufferRequestStatus::FAILED_CONFIGURING, bufRets);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700987 return hardware::Void();
988 }
989
990 bufRets.resize(bufReqs.size());
991
992 bool allReqsSucceeds = true;
993 bool oneReqSucceeds = false;
994 for (size_t i = 0; i < bufReqs.size(); i++) {
995 const auto& bufReq = bufReqs[i];
996 auto& bufRet = bufRets[i];
997 int32_t streamId = bufReq.streamId;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -0700998 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.get(streamId);
999 if (outputStream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001000 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
1001 hardware::hidl_vec<StreamBufferRet> emptyBufRets;
1002 _hidl_cb(BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS, emptyBufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001003 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001004 return hardware::Void();
1005 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001006
1007 bufRet.streamId = streamId;
1008 uint32_t numBuffersRequested = bufReq.numBuffersRequested;
1009 size_t totalHandout = outputStream->getOutstandingBuffersCount() + numBuffersRequested;
1010 if (totalHandout > outputStream->asHalStream()->max_buffers) {
1011 // Not able to allocate enough buffer. Exit early for this stream
1012 bufRet.val.error(StreamBufferRequestError::MAX_BUFFER_EXCEEDED);
1013 allReqsSucceeds = false;
1014 continue;
1015 }
1016
1017 hardware::hidl_vec<StreamBuffer> tmpRetBuffers(numBuffersRequested);
1018 bool currentReqSucceeds = true;
1019 std::vector<camera3_stream_buffer_t> streamBuffers(numBuffersRequested);
1020 size_t numAllocatedBuffers = 0;
1021 size_t numPushedInflightBuffers = 0;
1022 for (size_t b = 0; b < numBuffersRequested; b++) {
1023 camera3_stream_buffer_t& sb = streamBuffers[b];
1024 // Since this method can run concurrently with request thread
1025 // We need to update the wait duration everytime we call getbuffer
1026 nsecs_t waitDuration = kBaseGetBufferWait + getExpectedInFlightDuration();
1027 status_t res = outputStream->getBuffer(&sb, waitDuration);
1028 if (res != OK) {
1029 ALOGE("%s: Can't get output buffer for stream %d: %s (%d)",
1030 __FUNCTION__, streamId, strerror(-res), res);
1031 if (res == NO_INIT || res == DEAD_OBJECT) {
1032 bufRet.val.error(StreamBufferRequestError::STREAM_DISCONNECTED);
1033 } else if (res == TIMED_OUT || res == NO_MEMORY) {
1034 bufRet.val.error(StreamBufferRequestError::NO_BUFFER_AVAILABLE);
1035 } else {
1036 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1037 }
1038 currentReqSucceeds = false;
1039 break;
1040 }
1041 numAllocatedBuffers++;
1042
1043 buffer_handle_t *buffer = sb.buffer;
1044 auto pair = mInterface->getBufferId(*buffer, streamId);
1045 bool isNewBuffer = pair.first;
1046 uint64_t bufferId = pair.second;
1047 StreamBuffer& hBuf = tmpRetBuffers[b];
1048
1049 hBuf.streamId = streamId;
1050 hBuf.bufferId = bufferId;
1051 hBuf.buffer = (isNewBuffer) ? *buffer : nullptr;
1052 hBuf.status = BufferStatus::OK;
1053 hBuf.releaseFence = nullptr;
1054
1055 native_handle_t *acquireFence = nullptr;
1056 if (sb.acquire_fence != -1) {
1057 acquireFence = native_handle_create(1,0);
1058 acquireFence->data[0] = sb.acquire_fence;
1059 }
1060 hBuf.acquireFence.setTo(acquireFence, /*shouldOwn*/true);
1061 hBuf.releaseFence = nullptr;
1062
1063 res = mInterface->pushInflightRequestBuffer(bufferId, buffer);
1064 if (res != OK) {
1065 ALOGE("%s: Can't get register request buffers for stream %d: %s (%d)",
1066 __FUNCTION__, streamId, strerror(-res), res);
1067 bufRet.val.error(StreamBufferRequestError::UNKNOWN_ERROR);
1068 currentReqSucceeds = false;
1069 break;
1070 }
1071 numPushedInflightBuffers++;
1072 }
1073 if (currentReqSucceeds) {
1074 bufRet.val.buffers(std::move(tmpRetBuffers));
1075 oneReqSucceeds = true;
1076 } else {
1077 allReqsSucceeds = false;
1078 for (size_t b = 0; b < numPushedInflightBuffers; b++) {
1079 StreamBuffer& hBuf = tmpRetBuffers[b];
1080 buffer_handle_t* buffer;
1081 status_t res = mInterface->popInflightRequestBuffer(hBuf.bufferId, &buffer);
1082 if (res != OK) {
1083 SET_ERR("%s: popInflightRequestBuffer failed for stream %d: %s (%d)",
1084 __FUNCTION__, streamId, strerror(-res), res);
1085 }
1086 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001087 for (size_t b = 0; b < numAllocatedBuffers; b++) {
1088 camera3_stream_buffer_t& sb = streamBuffers[b];
1089 sb.acquire_fence = -1;
1090 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
1091 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001092 returnOutputBuffers(streamBuffers.data(), numAllocatedBuffers, 0);
1093 }
1094 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001095
1096 _hidl_cb(allReqsSucceeds ? BufferRequestStatus::OK :
1097 oneReqSucceeds ? BufferRequestStatus::FAILED_PARTIAL :
1098 BufferRequestStatus::FAILED_UNKNOWN,
1099 bufRets);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001100 mRequestBufferSM.endRequestBuffer();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001101 return hardware::Void();
1102}
1103
1104hardware::Return<void> Camera3Device::returnStreamBuffers(
1105 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
1106 if (!mUseHalBufManager) {
1107 ALOGE("%s: Camera %s does not support HAL buffer managerment",
1108 __FUNCTION__, mId.string());
1109 return hardware::Void();
1110 }
1111
1112 for (const auto& buf : buffers) {
1113 if (buf.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
1114 ALOGE("%s: cannot return a buffer without bufferId", __FUNCTION__);
1115 continue;
1116 }
1117
1118 buffer_handle_t* buffer;
1119 status_t res = mInterface->popInflightRequestBuffer(buf.bufferId, &buffer);
1120
1121 if (res != OK) {
1122 ALOGE("%s: cannot find in-flight buffer %" PRIu64 " for stream %d",
1123 __FUNCTION__, buf.bufferId, buf.streamId);
1124 continue;
1125 }
1126
1127 camera3_stream_buffer_t streamBuffer;
1128 streamBuffer.buffer = buffer;
1129 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
1130 streamBuffer.acquire_fence = -1;
1131 streamBuffer.release_fence = -1;
1132
1133 if (buf.releaseFence == nullptr) {
1134 streamBuffer.release_fence = -1;
1135 } else if (buf.releaseFence->numFds == 1) {
1136 streamBuffer.release_fence = dup(buf.releaseFence->data[0]);
1137 } else {
1138 ALOGE("%s: Invalid release fence, fd count is %d, not 1",
1139 __FUNCTION__, buf.releaseFence->numFds);
1140 continue;
1141 }
1142
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001143 sp<Camera3StreamInterface> stream = mOutputStreams.get(buf.streamId);
1144 if (stream == nullptr) {
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001145 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, buf.streamId);
1146 continue;
1147 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001148 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001149 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
1150 }
1151 return hardware::Void();
1152}
1153
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001154hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001155 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001156 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001157 // Ideally we should grab mLock, but that can lead to deadlock, and
1158 // it's not super important to get up to date value of mStatus for this
1159 // warning print, hence skipping the lock here
1160 if (mStatus == STATUS_ERROR) {
1161 // Per API contract, HAL should act as closed after device error
1162 // But mStatus can be set to error by framework as well, so just log
1163 // a warning here.
1164 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001165 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001166
1167 if (mProcessCaptureResultLock.tryLock() != OK) {
1168 // This should never happen; it indicates a wrong client implementation
1169 // that doesn't follow the contract. But, we can be tolerant here.
1170 ALOGE("%s: callback overlapped! waiting 1s...",
1171 __FUNCTION__);
1172 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1173 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1174 __FUNCTION__);
1175 // really don't know what to do, so bail out.
1176 return hardware::Void();
1177 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001178 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001179 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001180 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001181 }
1182 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001183 return hardware::Void();
1184}
1185
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001186// Only one processCaptureResult should be called at a time, so
1187// the locks won't block. The locks are present here simply to enforce this.
1188hardware::Return<void> Camera3Device::processCaptureResult(
1189 const hardware::hidl_vec<
1190 hardware::camera::device::V3_2::CaptureResult>& results) {
1191 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1192
1193 // Ideally we should grab mLock, but that can lead to deadlock, and
1194 // it's not super important to get up to date value of mStatus for this
1195 // warning print, hence skipping the lock here
1196 if (mStatus == STATUS_ERROR) {
1197 // Per API contract, HAL should act as closed after device error
1198 // But mStatus can be set to error by framework as well, so just log
1199 // a warning here.
1200 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1201 }
1202
1203 if (mProcessCaptureResultLock.tryLock() != OK) {
1204 // This should never happen; it indicates a wrong client implementation
1205 // that doesn't follow the contract. But, we can be tolerant here.
1206 ALOGE("%s: callback overlapped! waiting 1s...",
1207 __FUNCTION__);
1208 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1209 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1210 __FUNCTION__);
1211 // really don't know what to do, so bail out.
1212 return hardware::Void();
1213 }
1214 }
1215 for (const auto& result : results) {
1216 processOneCaptureResultLocked(result, noPhysMetadata);
1217 }
1218 mProcessCaptureResultLock.unlock();
1219 return hardware::Void();
1220}
1221
1222status_t Camera3Device::readOneCameraMetadataLocked(
1223 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
1224 const hardware::camera::device::V3_2::CameraMetadata& result) {
1225 if (fmqResultSize > 0) {
1226 resultMetadata.resize(fmqResultSize);
1227 if (mResultMetadataQueue == nullptr) {
1228 return NO_MEMORY; // logged in initialize()
1229 }
1230 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
1231 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
1232 __FUNCTION__, fmqResultSize);
1233 return INVALID_OPERATION;
1234 }
1235 } else {
1236 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
1237 result.size());
1238 }
1239
1240 if (resultMetadata.size() != 0) {
1241 status_t res;
1242 const camera_metadata_t* metadata =
1243 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
1244 size_t expected_metadata_size = resultMetadata.size();
1245 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
1246 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
1247 __FUNCTION__, strerror(-res), res);
1248 return INVALID_OPERATION;
1249 }
1250 }
1251
1252 return OK;
1253}
1254
Yifan Honga640c5a2017-04-12 16:30:31 -07001255void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001256 const hardware::camera::device::V3_2::CaptureResult& result,
1257 const hardware::hidl_vec<
1258 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadatas) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001259 camera3_capture_result r;
1260 status_t res;
1261 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -07001262
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001263 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -07001264 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001265 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
1266 if (res != OK) {
1267 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1268 __FUNCTION__, result.frameNumber);
1269 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001270 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001271 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001272
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001273 // Read and validate physical camera metadata
1274 size_t physResultCount = physicalCameraMetadatas.size();
1275 std::vector<const char*> physCamIds(physResultCount);
1276 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1277 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1278 physResultMetadata.resize(physResultCount);
1279 for (size_t i = 0; i < physicalCameraMetadatas.size(); i++) {
1280 res = readOneCameraMetadataLocked(physicalCameraMetadatas[i].fmqMetadataSize,
1281 physResultMetadata[i], physicalCameraMetadatas[i].metadata);
1282 if (res != OK) {
1283 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1284 __FUNCTION__, result.frameNumber,
1285 physicalCameraMetadatas[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001286 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001287 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001288 physCamIds[i] = physicalCameraMetadatas[i].physicalCameraId.c_str();
1289 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1290 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001291 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001292 r.num_physcam_metadata = physResultCount;
1293 r.physcam_ids = physCamIds.data();
1294 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001295
1296 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1297 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1298 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1299 auto& bDst = outputBuffers[i];
1300 const StreamBuffer &bSrc = result.outputBuffers[i];
1301
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001302 sp<Camera3StreamInterface> stream = mOutputStreams.get(bSrc.streamId);
1303 if (stream == nullptr) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001304 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1305 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001306 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001307 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001308 bDst.stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001309
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001310 bool noBufferReturned = false;
1311 buffer_handle_t *buffer = nullptr;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001312 if (mUseHalBufManager) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001313 // This is suspicious most of the time but can be correct during flush where HAL
1314 // has to return capture result before a buffer is requested
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001315 if (bSrc.bufferId == HalInterface::BUFFER_ID_NO_BUFFER) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001316 if (bSrc.status == BufferStatus::OK) {
1317 ALOGE("%s: Frame %d: Buffer %zu: No bufferId for stream %d",
1318 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
1319 // Still proceeds so other buffers can be returned
1320 }
1321 noBufferReturned = true;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001322 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001323 if (noBufferReturned) {
1324 res = OK;
1325 } else {
1326 res = mInterface->popInflightRequestBuffer(bSrc.bufferId, &buffer);
1327 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001328 } else {
1329 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
1330 }
1331
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001332 if (res != OK) {
1333 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1334 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001335 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001336 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001337
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001338 bDst.buffer = buffer;
1339 bDst.status = mapHidlBufferStatus(bSrc.status);
1340 bDst.acquire_fence = -1;
1341 if (bSrc.releaseFence == nullptr) {
1342 bDst.release_fence = -1;
1343 } else if (bSrc.releaseFence->numFds == 1) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08001344 if (noBufferReturned) {
1345 ALOGE("%s: got releaseFence without output buffer!", __FUNCTION__);
1346 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001347 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1348 } else {
1349 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1350 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001351 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001352 }
1353 }
1354 r.num_output_buffers = outputBuffers.size();
1355 r.output_buffers = outputBuffers.data();
1356
1357 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001358 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001359 r.input_buffer = nullptr;
1360 } else {
1361 if (mInputStream->getId() != result.inputBuffer.streamId) {
1362 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1363 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001364 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001365 }
1366 inputBuffer.stream = mInputStream->asHalStream();
1367 buffer_handle_t *buffer;
1368 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1369 &buffer);
1370 if (res != OK) {
1371 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1372 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001373 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001374 }
1375 inputBuffer.buffer = buffer;
1376 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1377 inputBuffer.acquire_fence = -1;
1378 if (result.inputBuffer.releaseFence == nullptr) {
1379 inputBuffer.release_fence = -1;
1380 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1381 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1382 } else {
1383 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1384 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001385 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001386 }
1387 r.input_buffer = &inputBuffer;
1388 }
1389
1390 r.partial_result = result.partialResult;
1391
1392 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001393}
1394
1395hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001396 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001397 // Ideally we should grab mLock, but that can lead to deadlock, and
1398 // it's not super important to get up to date value of mStatus for this
1399 // warning print, hence skipping the lock here
1400 if (mStatus == STATUS_ERROR) {
1401 // Per API contract, HAL should act as closed after device error
1402 // But mStatus can be set to error by framework as well, so just log
1403 // a warning here.
1404 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001405 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001406
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001407 for (const auto& msg : msgs) {
1408 notify(msg);
1409 }
1410 return hardware::Void();
1411}
1412
1413void Camera3Device::notify(
1414 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1415
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001416 camera3_notify_msg m;
1417 switch (msg.type) {
1418 case MsgType::ERROR:
1419 m.type = CAMERA3_MSG_ERROR;
1420 m.message.error.frame_number = msg.msg.error.frameNumber;
1421 if (msg.msg.error.errorStreamId >= 0) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001422 sp<Camera3StreamInterface> stream = mOutputStreams.get(msg.msg.error.errorStreamId);
1423 if (stream == nullptr) {
1424 ALOGE("%s: Frame %d: Invalid error stream id %d", __FUNCTION__,
1425 m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001426 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001427 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001428 m.message.error.error_stream = stream->asHalStream();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001429 } else {
1430 m.message.error.error_stream = nullptr;
1431 }
1432 switch (msg.msg.error.errorCode) {
1433 case ErrorCode::ERROR_DEVICE:
1434 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1435 break;
1436 case ErrorCode::ERROR_REQUEST:
1437 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1438 break;
1439 case ErrorCode::ERROR_RESULT:
1440 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1441 break;
1442 case ErrorCode::ERROR_BUFFER:
1443 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1444 break;
1445 }
1446 break;
1447 case MsgType::SHUTTER:
1448 m.type = CAMERA3_MSG_SHUTTER;
1449 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1450 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1451 break;
1452 }
1453 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001454}
1455
Emilian Peevaebbe412018-01-15 13:53:24 +00001456status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001457 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001458 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001459 ATRACE_CALL();
1460
Emilian Peevaebbe412018-01-15 13:53:24 +00001461 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001462}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001463
Jianing Weicb0652e2014-03-12 18:29:36 -07001464status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1465 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001467
Emilian Peevaebbe412018-01-15 13:53:24 +00001468 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001469 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001470 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001471
Emilian Peevaebbe412018-01-15 13:53:24 +00001472 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001473 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474}
1475
Emilian Peevaebbe412018-01-15 13:53:24 +00001476status_t Camera3Device::setStreamingRequestList(
1477 const List<const PhysicalCameraSettingsList> &requestsList,
1478 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001479 ATRACE_CALL();
1480
Emilian Peevaebbe412018-01-15 13:53:24 +00001481 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001482}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483
1484sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001485 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 status_t res;
1487
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001488 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001489 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1490 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001491 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1492 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001493 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001495 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001497 } else if (mStatus == STATUS_UNCONFIGURED) {
1498 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001499 CLOGE("No streams configured");
1500 return NULL;
1501 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001502 }
1503
Shuzhen Wang0129d522016-10-30 22:43:41 -07001504 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001506}
1507
Jianing Weicb0652e2014-03-12 18:29:36 -07001508status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001509 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001510 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001511 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513 switch (mStatus) {
1514 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001515 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001516 return INVALID_OPERATION;
1517 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001518 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520 case STATUS_UNCONFIGURED:
1521 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522 case STATUS_ACTIVE:
1523 // OK
1524 break;
1525 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001526 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 return INVALID_OPERATION;
1528 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001529 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001530
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001531 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001532}
1533
1534status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1535 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001536 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001537
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001538 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539}
1540
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001541status_t Camera3Device::createInputStream(
1542 uint32_t width, uint32_t height, int format, int *id) {
1543 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001544 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001545 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001546 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001547 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1548 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001549
1550 status_t res;
1551 bool wasActive = false;
1552
1553 switch (mStatus) {
1554 case STATUS_ERROR:
1555 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1556 return INVALID_OPERATION;
1557 case STATUS_UNINITIALIZED:
1558 ALOGE("%s: Device not initialized", __FUNCTION__);
1559 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001560 case STATUS_UNCONFIGURED:
1561 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001562 // OK
1563 break;
1564 case STATUS_ACTIVE:
1565 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001566 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001567 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001569 return res;
1570 }
1571 wasActive = true;
1572 break;
1573 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001574 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001575 return INVALID_OPERATION;
1576 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001578
1579 if (mInputStream != 0) {
1580 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1581 return INVALID_OPERATION;
1582 }
1583
1584 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1585 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001586 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001587
1588 mInputStream = newStream;
1589
1590 *id = mNextStreamId++;
1591
1592 // Continue captures if active at start
1593 if (wasActive) {
1594 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001595 // Reuse current operating mode and session parameters for new stream config
1596 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001597 if (res != OK) {
1598 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1599 __FUNCTION__, mNextStreamId, strerror(-res), res);
1600 return res;
1601 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001602 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001603 }
1604
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001605 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001606 return OK;
1607}
1608
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001609status_t Camera3Device::StreamSet::add(
1610 int streamId, sp<camera3::Camera3OutputStreamInterface> stream) {
1611 if (stream == nullptr) {
1612 ALOGE("%s: cannot add null stream", __FUNCTION__);
1613 return BAD_VALUE;
1614 }
1615 std::lock_guard<std::mutex> lock(mLock);
1616 return mData.add(streamId, stream);
1617}
1618
1619ssize_t Camera3Device::StreamSet::remove(int streamId) {
1620 std::lock_guard<std::mutex> lock(mLock);
1621 return mData.removeItem(streamId);
1622}
1623
1624sp<camera3::Camera3OutputStreamInterface>
1625Camera3Device::StreamSet::get(int streamId) {
1626 std::lock_guard<std::mutex> lock(mLock);
1627 ssize_t idx = mData.indexOfKey(streamId);
1628 if (idx == NAME_NOT_FOUND) {
1629 return nullptr;
1630 }
1631 return mData.editValueAt(idx);
1632}
1633
1634sp<camera3::Camera3OutputStreamInterface>
1635Camera3Device::StreamSet::operator[] (size_t index) {
1636 std::lock_guard<std::mutex> lock(mLock);
1637 return mData.editValueAt(index);
1638}
1639
1640size_t Camera3Device::StreamSet::size() const {
1641 std::lock_guard<std::mutex> lock(mLock);
1642 return mData.size();
1643}
1644
1645void Camera3Device::StreamSet::clear() {
1646 std::lock_guard<std::mutex> lock(mLock);
1647 return mData.clear();
1648}
1649
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07001650std::vector<int> Camera3Device::StreamSet::getStreamIds() {
1651 std::lock_guard<std::mutex> lock(mLock);
1652 std::vector<int> streamIds(mData.size());
1653 for (size_t i = 0; i < mData.size(); i++) {
1654 streamIds[i] = mData.keyAt(i);
1655 }
1656 return streamIds;
1657}
1658
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001659status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001660 uint32_t width, uint32_t height, int format,
1661 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001662 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001663 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001664 ATRACE_CALL();
1665
1666 if (consumer == nullptr) {
1667 ALOGE("%s: consumer must not be null", __FUNCTION__);
1668 return BAD_VALUE;
1669 }
1670
1671 std::vector<sp<Surface>> consumers;
1672 consumers.push_back(consumer);
1673
1674 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001675 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1676 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001677}
1678
1679status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1680 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1681 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001682 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001683 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001684 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001685
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001686 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001687 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001688 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001689 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001690 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1691 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1692 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001693
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001694 status_t res;
1695 bool wasActive = false;
1696
1697 switch (mStatus) {
1698 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001699 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001700 return INVALID_OPERATION;
1701 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001702 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001703 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001704 case STATUS_UNCONFIGURED:
1705 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001706 // OK
1707 break;
1708 case STATUS_ACTIVE:
1709 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001710 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001711 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001712 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001713 return res;
1714 }
1715 wasActive = true;
1716 break;
1717 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001718 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001719 return INVALID_OPERATION;
1720 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001721 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001722
1723 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001724
Shuzhen Wang0129d522016-10-30 22:43:41 -07001725 if (consumers.size() == 0 && !hasDeferredConsumer) {
1726 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1727 return BAD_VALUE;
1728 }
Zhijun He5d677d12016-05-29 16:52:39 -07001729
Shuzhen Wang0129d522016-10-30 22:43:41 -07001730 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001731 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1732 return BAD_VALUE;
1733 }
1734
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001735 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001736 ssize_t blobBufferSize;
1737 if (dataSpace != HAL_DATASPACE_DEPTH) {
1738 blobBufferSize = getJpegBufferSize(width, height);
1739 if (blobBufferSize <= 0) {
1740 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1741 return BAD_VALUE;
1742 }
1743 } else {
1744 blobBufferSize = getPointCloudBufferSize();
1745 if (blobBufferSize <= 0) {
1746 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1747 return BAD_VALUE;
1748 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001749 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001750 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001751 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001752 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001753 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1754 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1755 if (rawOpaqueBufferSize <= 0) {
1756 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1757 return BAD_VALUE;
1758 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001759 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001760 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001761 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001762 } else if (isShared) {
1763 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1764 width, height, format, consumerUsage, dataSpace, rotation,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07001765 mTimestampOffset, physicalCameraId, streamSetId,
1766 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001767 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001768 newStream = new Camera3OutputStream(mNextStreamId,
1769 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001770 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001771 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001772 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001773 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001774 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775 }
Emilian Peev40ead602017-09-26 15:46:36 +01001776
1777 size_t consumerCount = consumers.size();
1778 for (size_t i = 0; i < consumerCount; i++) {
1779 int id = newStream->getSurfaceId(consumers[i]);
1780 if (id < 0) {
1781 SET_ERR_L("Invalid surface id");
1782 return BAD_VALUE;
1783 }
1784 if (surfaceIds != nullptr) {
1785 surfaceIds->push_back(id);
1786 }
1787 }
1788
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001789 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790
Emilian Peev08dd2452017-04-06 16:55:14 +01001791 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001792
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001793 res = mOutputStreams.add(mNextStreamId, newStream);
1794 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001795 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001796 return res;
1797 }
1798
1799 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001800 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001801
1802 // Continue captures if active at start
1803 if (wasActive) {
1804 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001805 // Reuse current operating mode and session parameters for new stream config
1806 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001807 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001808 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1809 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001810 return res;
1811 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001812 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001813 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001814 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001815 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001816}
1817
Emilian Peev710c1422017-08-30 11:19:38 +01001818status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001819 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001820 if (nullptr == streamInfo) {
1821 return BAD_VALUE;
1822 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001823 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001824 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001825
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001826 switch (mStatus) {
1827 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001828 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001829 return INVALID_OPERATION;
1830 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001831 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001832 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001833 case STATUS_UNCONFIGURED:
1834 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001835 case STATUS_ACTIVE:
1836 // OK
1837 break;
1838 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001839 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001840 return INVALID_OPERATION;
1841 }
1842
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001843 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1844 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001845 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001846 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001847 }
1848
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001849 streamInfo->width = stream->getWidth();
1850 streamInfo->height = stream->getHeight();
1851 streamInfo->format = stream->getFormat();
1852 streamInfo->dataSpace = stream->getDataSpace();
1853 streamInfo->formatOverridden = stream->isFormatOverridden();
1854 streamInfo->originalFormat = stream->getOriginalFormat();
1855 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1856 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001858}
1859
1860status_t Camera3Device::setStreamTransform(int id,
1861 int transform) {
1862 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001863 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001864 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001865
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 switch (mStatus) {
1867 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001868 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 return INVALID_OPERATION;
1870 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001871 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001872 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001873 case STATUS_UNCONFIGURED:
1874 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001875 case STATUS_ACTIVE:
1876 // OK
1877 break;
1878 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001879 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001880 return INVALID_OPERATION;
1881 }
1882
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001883 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1884 if (stream == nullptr) {
1885 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001886 return BAD_VALUE;
1887 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001888 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001889}
1890
1891status_t Camera3Device::deleteStream(int id) {
1892 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001893 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001894 Mutex::Autolock l(mLock);
1895 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001896
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001897 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001898
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001899 // CameraDevice semantics require device to already be idle before
1900 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001901 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001902 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001903 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001904 }
1905
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001906 if (mStatus == STATUS_ERROR) {
1907 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1908 __FUNCTION__, mId.string());
1909 return -EBUSY;
1910 }
1911
Igor Murashkin2fba5842013-04-22 14:03:54 -07001912 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001913 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001914 if (mInputStream != NULL && id == mInputStream->getId()) {
1915 deletedStream = mInputStream;
1916 mInputStream.clear();
1917 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001918 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001919 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001920 return BAD_VALUE;
1921 }
Zhijun He5f446352014-01-22 09:49:33 -08001922 }
1923
1924 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001925 if (stream != nullptr) {
1926 deletedStream = stream;
1927 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928 }
1929
1930 // Free up the stream endpoint so that it can be used by some other stream
1931 res = deletedStream->disconnect();
1932 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001933 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934 // fall through since we want to still list the stream as deleted.
1935 }
1936 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001937 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001938
1939 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001940}
1941
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001942status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001943 ATRACE_CALL();
1944 ALOGV("%s: E", __FUNCTION__);
1945
1946 Mutex::Autolock il(mInterfaceLock);
1947 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001948
Emilian Peev811d2952018-05-25 11:08:40 +01001949 // In case the client doesn't include any session parameter, try a
1950 // speculative configuration using the values from the last cached
1951 // default request.
1952 if (sessionParams.isEmpty() &&
1953 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
1954 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1955 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1956 mLastTemplateId);
1957 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1958 operatingMode);
1959 }
1960
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001961 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1962}
1963
1964status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1965 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001966 //Filter out any incoming session parameters
1967 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001968 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1969 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001970 CameraMetadata filteredParams(availableSessionKeys.count);
1971 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1972 filteredParams.getAndLock());
1973 set_camera_metadata_vendor_id(meta, mVendorTagId);
1974 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001975 if (availableSessionKeys.count > 0) {
1976 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1977 camera_metadata_ro_entry entry = params.find(
1978 availableSessionKeys.data.i32[i]);
1979 if (entry.count > 0) {
1980 filteredParams.update(entry);
1981 }
1982 }
1983 }
1984
1985 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001986}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001987
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001988status_t Camera3Device::getInputBufferProducer(
1989 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001990 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001991 Mutex::Autolock il(mInterfaceLock);
1992 Mutex::Autolock l(mLock);
1993
1994 if (producer == NULL) {
1995 return BAD_VALUE;
1996 } else if (mInputStream == NULL) {
1997 return INVALID_OPERATION;
1998 }
1999
2000 return mInputStream->getInputBufferProducer(producer);
2001}
2002
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002003status_t Camera3Device::createDefaultRequest(int templateId,
2004 CameraMetadata *request) {
2005 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07002006 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002007
2008 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
2009 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07002010 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08002011 return BAD_VALUE;
2012 }
2013
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002014 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002015
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002016 {
2017 Mutex::Autolock l(mLock);
2018 switch (mStatus) {
2019 case STATUS_ERROR:
2020 CLOGE("Device has encountered a serious error");
2021 return INVALID_OPERATION;
2022 case STATUS_UNINITIALIZED:
2023 CLOGE("Device is not initialized!");
2024 return INVALID_OPERATION;
2025 case STATUS_UNCONFIGURED:
2026 case STATUS_CONFIGURED:
2027 case STATUS_ACTIVE:
2028 // OK
2029 break;
2030 default:
2031 SET_ERR_L("Unexpected status: %d", mStatus);
2032 return INVALID_OPERATION;
2033 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002034
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002035 if (!mRequestTemplateCache[templateId].isEmpty()) {
2036 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002037 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002038 return OK;
2039 }
Zhijun Hea1530f12014-09-14 12:44:20 -07002040 }
2041
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002042 camera_metadata_t *rawRequest;
2043 status_t res = mInterface->constructDefaultRequestSettings(
2044 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002045
2046 {
2047 Mutex::Autolock l(mLock);
2048 if (res == BAD_VALUE) {
2049 ALOGI("%s: template %d is not supported on this camera device",
2050 __FUNCTION__, templateId);
2051 return res;
2052 } else if (res != OK) {
2053 CLOGE("Unable to construct request template %d: %s (%d)",
2054 templateId, strerror(-res), res);
2055 return res;
2056 }
2057
2058 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
2059 mRequestTemplateCache[templateId].acquire(rawRequest);
2060
2061 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01002062 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002063 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002064 return OK;
2065}
2066
2067status_t Camera3Device::waitUntilDrained() {
2068 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002069 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002070 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002072
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002073 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07002074}
2075
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002076status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 switch (mStatus) {
2078 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002079 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002080 ALOGV("%s: Already idle", __FUNCTION__);
2081 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002082 case STATUS_CONFIGURED:
2083 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002084 case STATUS_ERROR:
2085 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002086 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087 break;
2088 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002089 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002090 return INVALID_OPERATION;
2091 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002092 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
2093 maxExpectedDuration);
2094 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07002095 if (res != OK) {
2096 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
2097 res);
2098 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002099 return res;
2100}
2101
Ruben Brunk183f0562015-08-12 12:55:02 -07002102
2103void Camera3Device::internalUpdateStatusLocked(Status status) {
2104 mStatus = status;
2105 mRecentStatusUpdates.add(mStatus);
2106 mStatusChanged.broadcast();
2107}
2108
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002109void Camera3Device::pauseStateNotify(bool enable) {
2110 Mutex::Autolock il(mInterfaceLock);
2111 Mutex::Autolock l(mLock);
2112
2113 mPauseStateNotify = enable;
2114}
2115
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002116// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002117status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002118 mRequestThread->setPaused(true);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002119
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002120 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2121 maxExpectedDuration);
2122 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002123 if (res != OK) {
2124 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002125 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002126 }
2127
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002128 return res;
2129}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002130
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002131// Resume after internalPauseAndWaitLocked
2132status_t Camera3Device::internalResumeLocked() {
2133 status_t res;
2134
2135 mRequestThread->setPaused(false);
2136
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002137 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
2138 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002139 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
2140 if (res != OK) {
2141 SET_ERR_L("Can't transition to active in %f seconds!",
2142 kActiveTimeout/1e9);
2143 }
2144 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002145 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002146}
2147
Ruben Brunk183f0562015-08-12 12:55:02 -07002148status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002149 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07002150
2151 size_t startIndex = 0;
2152 if (mStatusWaiters == 0) {
2153 // Clear the list of recent statuses if there are no existing threads waiting on updates to
2154 // this status list
2155 mRecentStatusUpdates.clear();
2156 } else {
2157 // If other threads are waiting on updates to this status list, set the position of the
2158 // first element that this list will check rather than clearing the list.
2159 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002160 }
2161
Ruben Brunk183f0562015-08-12 12:55:02 -07002162 mStatusWaiters++;
2163
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002164 // Notify HAL to start draining. We need to notify the HalInterface layer
2165 // even when the device is already IDLE, so HalInterface can reject incoming
2166 // requestStreamBuffers call.
2167 if (!active && mUseHalBufManager) {
2168 auto streamIds = mOutputStreams.getStreamIds();
2169 mRequestThread->signalPipelineDrain(streamIds);
2170 mRequestBufferSM.onWaitUntilIdle();
2171 }
2172
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002173 bool stateSeen = false;
2174 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07002175 if (active == (mStatus == STATUS_ACTIVE)) {
2176 // Desired state is current
2177 break;
2178 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002179
2180 res = mStatusChanged.waitRelative(mLock, timeout);
2181 if (res != OK) break;
2182
Ruben Brunk183f0562015-08-12 12:55:02 -07002183 // This is impossible, but if not, could result in subtle deadlocks and invalid state
2184 // transitions.
2185 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
2186 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
2187 __FUNCTION__);
2188
2189 // Encountered desired state since we began waiting
2190 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002191 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
2192 stateSeen = true;
2193 break;
2194 }
2195 }
2196 } while (!stateSeen);
2197
Ruben Brunk183f0562015-08-12 12:55:02 -07002198 mStatusWaiters--;
2199
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002200 return res;
2201}
2202
2203
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002204status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002205 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002206 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002207
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002208 if (listener != NULL && mListener != NULL) {
2209 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
2210 }
2211 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002212 mRequestThread->setNotificationListener(listener);
2213 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002214
2215 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002216}
2217
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002218bool Camera3Device::willNotify3A() {
2219 return false;
2220}
2221
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002222status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002223 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002224 status_t res;
2225 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002226
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002227 while (mResultQueue.empty()) {
2228 res = mResultSignal.waitRelative(mOutputLock, timeout);
2229 if (res == TIMED_OUT) {
2230 return res;
2231 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002232 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
2233 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002234 return res;
2235 }
2236 }
2237 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002238}
2239
Jianing Weicb0652e2014-03-12 18:29:36 -07002240status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002241 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002242 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002243
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002244 if (mResultQueue.empty()) {
2245 return NOT_ENOUGH_DATA;
2246 }
2247
Jianing Weicb0652e2014-03-12 18:29:36 -07002248 if (frame == NULL) {
2249 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
2250 return BAD_VALUE;
2251 }
2252
2253 CaptureResult &result = *(mResultQueue.begin());
2254 frame->mResultExtras = result.mResultExtras;
2255 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002256 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002257 mResultQueue.erase(mResultQueue.begin());
2258
2259 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002260}
2261
2262status_t Camera3Device::triggerAutofocus(uint32_t id) {
2263 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002264 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002265
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002266 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
2267 // Mix-in this trigger into the next request and only the next request.
2268 RequestTrigger trigger[] = {
2269 {
2270 ANDROID_CONTROL_AF_TRIGGER,
2271 ANDROID_CONTROL_AF_TRIGGER_START
2272 },
2273 {
2274 ANDROID_CONTROL_AF_TRIGGER_ID,
2275 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002276 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002277 };
2278
2279 return mRequestThread->queueTrigger(trigger,
2280 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002281}
2282
2283status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2284 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002285 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002286
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002287 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2288 // Mix-in this trigger into the next request and only the next request.
2289 RequestTrigger trigger[] = {
2290 {
2291 ANDROID_CONTROL_AF_TRIGGER,
2292 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2293 },
2294 {
2295 ANDROID_CONTROL_AF_TRIGGER_ID,
2296 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002297 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002298 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002299
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002300 return mRequestThread->queueTrigger(trigger,
2301 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002302}
2303
2304status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2305 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002306 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002307
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002308 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2309 // Mix-in this trigger into the next request and only the next request.
2310 RequestTrigger trigger[] = {
2311 {
2312 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2313 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2314 },
2315 {
2316 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2317 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002318 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002319 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002320
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002321 return mRequestThread->queueTrigger(trigger,
2322 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002323}
2324
Jianing Weicb0652e2014-03-12 18:29:36 -07002325status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002326 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002327 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002328 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002329
Zhijun He7ef20392014-04-21 16:04:17 -07002330 {
2331 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002332
2333 // b/116514106 "disconnect()" can get called twice for the same device. The
2334 // camera device will not be initialized during the second run.
2335 if (mStatus == STATUS_UNINITIALIZED) {
2336 return OK;
2337 }
2338
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002339 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07002340 }
2341
Emilian Peev08dd2452017-04-06 16:55:14 +01002342 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002343}
2344
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002345status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002346 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2347}
2348
2349status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002350 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002351 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002352 Mutex::Autolock il(mInterfaceLock);
2353 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002354
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002355 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2356 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002357 CLOGE("Stream %d does not exist", streamId);
2358 return BAD_VALUE;
2359 }
2360
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002361 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002362 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002363 return BAD_VALUE;
2364 }
2365
2366 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002367 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002368 return BAD_VALUE;
2369 }
2370
Ruben Brunkc78ac262015-08-13 17:58:46 -07002371 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002372}
2373
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002374status_t Camera3Device::tearDown(int streamId) {
2375 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002376 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002377 Mutex::Autolock il(mInterfaceLock);
2378 Mutex::Autolock l(mLock);
2379
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002380 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2381 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002382 CLOGE("Stream %d does not exist", streamId);
2383 return BAD_VALUE;
2384 }
2385
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002386 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2387 CLOGE("Stream %d is a target of a in-progress request", streamId);
2388 return BAD_VALUE;
2389 }
2390
2391 return stream->tearDown();
2392}
2393
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002394status_t Camera3Device::addBufferListenerForStream(int streamId,
2395 wp<Camera3StreamBufferListener> listener) {
2396 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002397 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002398 Mutex::Autolock il(mInterfaceLock);
2399 Mutex::Autolock l(mLock);
2400
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002401 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2402 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002403 CLOGE("Stream %d does not exist", streamId);
2404 return BAD_VALUE;
2405 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002406 stream->addBufferListener(listener);
2407
2408 return OK;
2409}
2410
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002411/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002412 * Methods called by subclasses
2413 */
2414
2415void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002416 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002417 {
2418 // Need mLock to safely update state and synchronize to current
2419 // state of methods in flight.
2420 Mutex::Autolock l(mLock);
2421 // We can get various system-idle notices from the status tracker
2422 // while starting up. Only care about them if we've actually sent
2423 // in some requests recently.
2424 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2425 return;
2426 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002427 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2428 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002429 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002430
2431 // Skip notifying listener if we're doing some user-transparent
2432 // state changes
2433 if (mPauseStateNotify) return;
2434 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002435
2436 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002437 {
2438 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002439 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002440 }
2441 if (idle && listener != NULL) {
2442 listener->notifyIdle();
2443 }
2444}
2445
Shuzhen Wang758c2152017-01-10 18:26:18 -08002446status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002447 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002448 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002449 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2450 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002451
2452 if (surfaceIds == nullptr) {
2453 return BAD_VALUE;
2454 }
2455
Zhijun He5d677d12016-05-29 16:52:39 -07002456 Mutex::Autolock il(mInterfaceLock);
2457 Mutex::Autolock l(mLock);
2458
Shuzhen Wang758c2152017-01-10 18:26:18 -08002459 if (consumers.size() == 0) {
2460 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002461 return BAD_VALUE;
2462 }
2463
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002464 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2465 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002466 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002467 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002468 }
Shuzhen Wang758c2152017-01-10 18:26:18 -08002469 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002470 if (res != OK) {
2471 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2472 return res;
2473 }
2474
Emilian Peev40ead602017-09-26 15:46:36 +01002475 for (auto &consumer : consumers) {
2476 int id = stream->getSurfaceId(consumer);
2477 if (id < 0) {
2478 CLOGE("Invalid surface id!");
2479 return BAD_VALUE;
2480 }
2481 surfaceIds->push_back(id);
2482 }
2483
Shuzhen Wang0129d522016-10-30 22:43:41 -07002484 if (stream->isConsumerConfigurationDeferred()) {
2485 if (!stream->isConfiguring()) {
2486 CLOGE("Stream %d was already fully configured.", streamId);
2487 return INVALID_OPERATION;
2488 }
Zhijun He5d677d12016-05-29 16:52:39 -07002489
Shuzhen Wang0129d522016-10-30 22:43:41 -07002490 res = stream->finishConfiguration();
2491 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002492 // If finishConfiguration fails due to abandoned surface, do not set
2493 // device to error state.
2494 bool isSurfaceAbandoned =
2495 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2496 if (!isSurfaceAbandoned) {
2497 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2498 stream->getId(), strerror(-res), res);
2499 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002500 return res;
2501 }
Zhijun He5d677d12016-05-29 16:52:39 -07002502 }
2503
2504 return OK;
2505}
2506
Emilian Peev40ead602017-09-26 15:46:36 +01002507status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2508 const std::vector<OutputStreamInfo> &outputInfo,
2509 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2510 Mutex::Autolock il(mInterfaceLock);
2511 Mutex::Autolock l(mLock);
2512
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002513 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2514 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002515 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002516 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002517 }
2518
2519 for (const auto &it : removedSurfaceIds) {
2520 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2521 CLOGE("Shared surface still part of a pending request!");
2522 return -EBUSY;
2523 }
2524 }
2525
Emilian Peev40ead602017-09-26 15:46:36 +01002526 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2527 if (res != OK) {
2528 CLOGE("Stream %d failed to update stream (error %d %s) ",
2529 streamId, res, strerror(-res));
2530 if (res == UNKNOWN_ERROR) {
2531 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2532 __FUNCTION__);
2533 }
2534 return res;
2535 }
2536
2537 return res;
2538}
2539
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002540status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2541 Mutex::Autolock il(mInterfaceLock);
2542 Mutex::Autolock l(mLock);
2543
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002544 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2545 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002546 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2547 return BAD_VALUE;
2548 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002549 return stream->dropBuffers(dropping);
2550}
2551
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002552/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002553 * Camera3Device private methods
2554 */
2555
2556sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002557 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002558 ATRACE_CALL();
2559 status_t res;
2560
2561 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002562 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002563
2564 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002565 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002566 if (inputStreams.count > 0) {
2567 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002568 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002569 CLOGE("Request references unknown input stream %d",
2570 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002571 return NULL;
2572 }
2573 // Lazy completion of stream configuration (allocation/registration)
2574 // on first use
2575 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002576 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002577 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002578 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002580 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002581 return NULL;
2582 }
2583 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002584 // Check if stream prepare is blocking requests.
2585 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002586 CLOGE("Request references an input stream that's being prepared!");
2587 return NULL;
2588 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002589
2590 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002591 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002592 }
2593
2594 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002595 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002596 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002597 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002598 return NULL;
2599 }
2600
2601 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002602 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2603 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002604 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002605 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002606 return NULL;
2607 }
Zhijun He5d677d12016-05-29 16:52:39 -07002608 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002609 auto iter = surfaceMap.find(streams.data.i32[i]);
2610 if (iter != surfaceMap.end()) {
2611 const std::vector<size_t>& surfaces = iter->second;
2612 for (const auto& surface : surfaces) {
2613 if (stream->isConsumerConfigurationDeferred(surface)) {
2614 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2615 "due to deferred consumer", stream->getId(), surface);
2616 return NULL;
2617 }
2618 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002619 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002620 }
2621
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002622 // Lazy completion of stream configuration (allocation/registration)
2623 // on first use
2624 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002625 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002627 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2628 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002629 return NULL;
2630 }
2631 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002632 // Check if stream prepare is blocking requests.
2633 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002634 CLOGE("Request references an output stream that's being prepared!");
2635 return NULL;
2636 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002637
2638 newRequest->mOutputStreams.push(stream);
2639 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002640 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002641 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002642
2643 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002644}
2645
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002646bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2647 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2648 Size size = mSupportedOpaqueInputSizes[i];
2649 if (size.width == width && size.height == height) {
2650 return true;
2651 }
2652 }
2653
2654 return false;
2655}
2656
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002657void Camera3Device::cancelStreamsConfigurationLocked() {
2658 int res = OK;
2659 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2660 res = mInputStream->cancelConfiguration();
2661 if (res != OK) {
2662 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2663 mInputStream->getId(), strerror(-res), res);
2664 }
2665 }
2666
2667 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002668 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002669 if (outputStream->isConfiguring()) {
2670 res = outputStream->cancelConfiguration();
2671 if (res != OK) {
2672 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2673 outputStream->getId(), strerror(-res), res);
2674 }
2675 }
2676 }
2677
2678 // Return state to that at start of call, so that future configures
2679 // properly clean things up
2680 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2681 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002682
2683 res = mPreparerThread->resume();
2684 if (res != OK) {
2685 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2686 }
2687}
2688
2689bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2690 ATRACE_CALL();
2691 bool ret = false;
2692
2693 Mutex::Autolock il(mInterfaceLock);
2694 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2695
2696 Mutex::Autolock l(mLock);
2697 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2698 if (rc == NO_ERROR) {
2699 mNeedConfig = true;
2700 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2701 if (rc == NO_ERROR) {
2702 ret = true;
2703 mPauseStateNotify = false;
2704 //Moving to active state while holding 'mLock' is important.
2705 //There could be pending calls to 'create-/deleteStream' which
2706 //will trigger another stream configuration while the already
2707 //present streams end up with outstanding buffers that will
2708 //not get drained.
2709 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002710 } else if (rc == DEAD_OBJECT) {
2711 // DEAD_OBJECT can be returned if either the consumer surface is
2712 // abandoned, or the HAL has died.
2713 // - If the HAL has died, configureStreamsLocked call will set
2714 // device to error state,
2715 // - If surface is abandoned, we should not set device to error
2716 // state.
2717 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002718 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002719 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002720 }
2721 } else {
2722 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2723 }
2724
2725 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002726}
2727
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002728status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002729 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002730 ATRACE_CALL();
2731 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002732
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002733 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002734 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002735 return INVALID_OPERATION;
2736 }
2737
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002738 if (operatingMode < 0) {
2739 CLOGE("Invalid operating mode: %d", operatingMode);
2740 return BAD_VALUE;
2741 }
2742
2743 bool isConstrainedHighSpeed =
2744 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2745 operatingMode;
2746
2747 if (mOperatingMode != operatingMode) {
2748 mNeedConfig = true;
2749 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2750 mOperatingMode = operatingMode;
2751 }
2752
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002753 if (!mNeedConfig) {
2754 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2755 return OK;
2756 }
2757
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002758 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2759 // adding a dummy stream instead.
2760 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2761 if (mOutputStreams.size() == 0) {
2762 addDummyStreamLocked();
2763 } else {
2764 tryRemoveDummyStreamLocked();
2765 }
2766
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002767 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002768 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002770 mPreparerThread->pause();
2771
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002772 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002773 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002774 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2775
2776 Vector<camera3_stream_t*> streams;
2777 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002778 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002779
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002780
2781 if (mInputStream != NULL) {
2782 camera3_stream_t *inputStream;
2783 inputStream = mInputStream->startConfiguration();
2784 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002785 CLOGE("Can't start input stream configuration");
2786 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002787 return INVALID_OPERATION;
2788 }
2789 streams.add(inputStream);
2790 }
2791
2792 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002793
2794 // Don't configure bidi streams twice, nor add them twice to the list
2795 if (mOutputStreams[i].get() ==
2796 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2797
2798 config.num_streams--;
2799 continue;
2800 }
2801
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002802 camera3_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002803 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002804 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002805 CLOGE("Can't start output stream configuration");
2806 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002807 return INVALID_OPERATION;
2808 }
2809 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002810
2811 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB &&
2812 outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
Emilian Peev192ee832018-01-31 14:46:47 +00002813 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2814 // always occupy the initial entry.
2815 bufferSizes[k] = static_cast<uint32_t>(
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002816 getJpegBufferSize(outputStream->width, outputStream->height));
2817 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002818 }
2819
2820 config.streams = streams.editArray();
2821
2822 // Do the HAL configuration; will potentially touch stream
2823 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002824
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002825 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002826 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002827 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002828
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002829 if (res == BAD_VALUE) {
2830 // HAL rejected this set of streams as unsupported, clean up config
2831 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002832 CLOGE("Set of requested inputs/outputs not supported by HAL");
2833 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002834 return BAD_VALUE;
2835 } else if (res != OK) {
2836 // Some other kind of error from configure_streams - this is not
2837 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002838 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2839 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002840 return res;
2841 }
2842
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002843 // Finish all stream configuration immediately.
2844 // TODO: Try to relax this later back to lazy completion, which should be
2845 // faster
2846
Igor Murashkin073f8572013-05-02 14:59:28 -07002847 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002848 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002849 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002850 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002851 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002852 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002853 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2854 return DEAD_OBJECT;
2855 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002856 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002857 }
2858 }
2859
2860 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002861 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002862 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002863 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002864 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002865 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002866 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002867 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002868 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2869 return DEAD_OBJECT;
2870 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002871 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002872 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002873 }
2874 }
2875
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002876 // Request thread needs to know to avoid using repeat-last-settings protocol
2877 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002878 if (notifyRequestThread) {
2879 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2880 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002881
Zhijun He90f7c372016-08-16 16:19:43 -07002882 char value[PROPERTY_VALUE_MAX];
2883 property_get("camera.fifo.disable", value, "0");
2884 int32_t disableFifo = atoi(value);
2885 if (disableFifo != 1) {
2886 // Boost priority of request thread to SCHED_FIFO.
2887 pid_t requestThreadTid = mRequestThread->getTid();
2888 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002889 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002890 if (res != OK) {
2891 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2892 strerror(-res), res);
2893 } else {
2894 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2895 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002896 }
2897
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002898 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002899 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2900 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2901 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2902 sessionParams.unlock(newSessionParams);
2903 mSessionParams.unlock(currentSessionParams);
2904 if (updateSessionParams) {
2905 mSessionParams = sessionParams;
2906 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002907
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002908 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909
Ruben Brunk183f0562015-08-12 12:55:02 -07002910 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2911 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002912
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002913 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002914
Zhijun He0a210512014-07-24 13:45:15 -07002915 // tear down the deleted streams after configure streams.
2916 mDeletedStreams.clear();
2917
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002918 auto rc = mPreparerThread->resume();
2919 if (rc != OK) {
2920 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2921 return rc;
2922 }
2923
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002924 if (mDummyStreamId == NO_STREAM) {
2925 mRequestBufferSM.onStreamsConfigured();
2926 }
2927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002929}
2930
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002931status_t Camera3Device::addDummyStreamLocked() {
2932 ATRACE_CALL();
2933 status_t res;
2934
2935 if (mDummyStreamId != NO_STREAM) {
2936 // Should never be adding a second dummy stream when one is already
2937 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002938 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2939 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002940 return INVALID_OPERATION;
2941 }
2942
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002943 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002944
2945 sp<Camera3OutputStreamInterface> dummyStream =
2946 new Camera3DummyStream(mNextStreamId);
2947
2948 res = mOutputStreams.add(mNextStreamId, dummyStream);
2949 if (res < 0) {
2950 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2951 return res;
2952 }
2953
2954 mDummyStreamId = mNextStreamId;
2955 mNextStreamId++;
2956
2957 return OK;
2958}
2959
2960status_t Camera3Device::tryRemoveDummyStreamLocked() {
2961 ATRACE_CALL();
2962 status_t res;
2963
2964 if (mDummyStreamId == NO_STREAM) return OK;
2965 if (mOutputStreams.size() == 1) return OK;
2966
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002967 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002968
2969 // Ok, have a dummy stream and there's at least one other output stream,
2970 // so remove the dummy
2971
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002972 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mDummyStreamId);
2973 if (deletedStream == nullptr) {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002974 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2975 return INVALID_OPERATION;
2976 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002977 mOutputStreams.remove(mDummyStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002978
2979 // Free up the stream endpoint so that it can be used by some other stream
2980 res = deletedStream->disconnect();
2981 if (res != OK) {
2982 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2983 // fall through since we want to still list the stream as deleted.
2984 }
2985 mDeletedStreams.add(deletedStream);
2986 mDummyStreamId = NO_STREAM;
2987
2988 return res;
2989}
2990
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002991void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002992 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002993 Mutex::Autolock l(mLock);
2994 va_list args;
2995 va_start(args, fmt);
2996
2997 setErrorStateLockedV(fmt, args);
2998
2999 va_end(args);
3000}
3001
3002void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003003 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003004 Mutex::Autolock l(mLock);
3005 setErrorStateLockedV(fmt, args);
3006}
3007
3008void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
3009 va_list args;
3010 va_start(args, fmt);
3011
3012 setErrorStateLockedV(fmt, args);
3013
3014 va_end(args);
3015}
3016
3017void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003018 // Print out all error messages to log
3019 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003020 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003021
3022 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07003023 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003024
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003025 mErrorCause = errorCause;
3026
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07003027 if (mRequestThread != nullptr) {
3028 mRequestThread->setPaused(true);
3029 }
Ruben Brunk183f0562015-08-12 12:55:02 -07003030 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003031
3032 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003033 sp<NotificationListener> listener = mListener.promote();
3034 if (listener != NULL) {
3035 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003036 CaptureResultExtras());
3037 }
3038
3039 // Save stack trace. View by dumping it later.
3040 CameraTraces::saveTrace();
3041 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003042}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003043
3044/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003045 * In-flight request management
3046 */
3047
Jianing Weicb0652e2014-03-12 18:29:36 -07003048status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07003049 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003050 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003051 std::set<String8>& physicalCameraIds, bool isStillCapture,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003052 bool isZslCapture, const SurfaceMap& outputSurfaces) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003053 ATRACE_CALL();
3054 Mutex::Autolock l(mInFlightLock);
3055
3056 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07003057 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003058 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
3059 outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003060 if (res < 0) return res;
3061
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003062 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01003063 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3064 // avoid a deadlock during reprocess requests.
3065 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003066 if (mStatusTracker != nullptr) {
3067 mStatusTracker->markComponentActive(mInFlightStatusId);
3068 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003069 }
3070
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003071 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003072 return OK;
3073}
3074
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003075void Camera3Device::returnOutputBuffers(
3076 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003077 nsecs_t timestamp, bool timestampIncreasing,
3078 const SurfaceMap& outputSurfaces,
3079 const CaptureResultExtras &inResultExtras) {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003080
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003081 for (size_t i = 0; i < numBuffers; i++)
3082 {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003083 if (outputBuffers[i].buffer == nullptr) {
3084 if (!mUseHalBufManager) {
3085 // With HAL buffer management API, HAL sometimes will have to return buffers that
3086 // has not got a output buffer handle filled yet. This is though illegal if HAL
3087 // buffer management API is not being used.
3088 ALOGE("%s: cannot return a null buffer!", __FUNCTION__);
3089 }
3090 continue;
3091 }
3092
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003093 Camera3StreamInterface *stream = Camera3Stream::cast(outputBuffers[i].stream);
3094 int streamId = stream->getId();
3095 const auto& it = outputSurfaces.find(streamId);
3096 status_t res = OK;
3097 if (it != outputSurfaces.end()) {
3098 res = stream->returnBuffer(
3099 outputBuffers[i], timestamp, timestampIncreasing, it->second);
3100 } else {
3101 res = stream->returnBuffer(
3102 outputBuffers[i], timestamp, timestampIncreasing);
3103 }
3104
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003105 // Note: stream may be deallocated at this point, if this buffer was
3106 // the last reference to it.
3107 if (res != OK) {
3108 ALOGE("Can't return buffer to its stream: %s (%d)",
3109 strerror(-res), res);
3110 }
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003111
3112 // Long processing consumers can cause returnBuffer timeout for shared stream
3113 // If that happens, cancel the buffer and send a buffer error to client
3114 if (it != outputSurfaces.end() && res == TIMED_OUT &&
3115 outputBuffers[i].status == CAMERA3_BUFFER_STATUS_OK) {
3116 // cancel the buffer
3117 camera3_stream_buffer_t sb = outputBuffers[i];
3118 sb.status = CAMERA3_BUFFER_STATUS_ERROR;
3119 stream->returnBuffer(sb, /*timestamp*/0, timestampIncreasing);
3120
3121 // notify client buffer error
3122 sp<NotificationListener> listener;
3123 {
3124 Mutex::Autolock l(mOutputLock);
3125 listener = mListener.promote();
3126 }
3127
3128 if (listener != nullptr) {
3129 CaptureResultExtras extras = inResultExtras;
3130 extras.errorStreamId = streamId;
3131 listener->notifyError(
3132 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER,
3133 extras);
3134 }
3135 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003136 }
3137}
3138
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003139void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003140 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003141 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003142 mInFlightMap.removeItemsAt(idx, 1);
3143
3144 // Indicate idle inFlightMap to the status tracker
3145 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003146 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01003147 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3148 // avoid a deadlock during reprocess requests.
3149 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003150 if (mStatusTracker != nullptr) {
3151 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
3152 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003153 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003154 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003155}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003156
3157void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
3158
3159 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3160 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
3161
3162 nsecs_t sensorTimestamp = request.sensorTimestamp;
3163 nsecs_t shutterTimestamp = request.shutterTimestamp;
3164
3165 // Check if it's okay to remove the request from InFlightMap:
3166 // In the case of a successful request:
3167 // all input and output buffers, all result metadata, shutter callback
3168 // arrived.
3169 // In the case of a unsuccessful request:
3170 // all input and output buffers arrived.
3171 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07003172 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003173 (request.haveResultMetadata && shutterTimestamp != 0))) {
Emilian Peev9dd21f42018-08-03 13:39:29 +01003174 if (request.stillCapture) {
3175 ATRACE_ASYNC_END("still capture", frameNumber);
3176 }
3177
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003178 ATRACE_ASYNC_END("frame capture", frameNumber);
3179
Shuzhen Wang403044a2017-02-26 23:29:04 -08003180 // Sanity check - if sensor timestamp matches shutter timestamp in the
3181 // case of request having callback.
3182 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003183 sensorTimestamp != shutterTimestamp) {
3184 SET_ERR("sensor timestamp (%" PRId64
3185 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
3186 sensorTimestamp, frameNumber, shutterTimestamp);
3187 }
3188
3189 // for an unsuccessful request, it may have pending output buffers to
3190 // return.
3191 assert(request.requestStatus != OK ||
3192 request.pendingOutputBuffers.size() == 0);
3193 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003194 request.pendingOutputBuffers.size(), 0, /*timestampIncreasing*/true,
3195 request.outputSurfaces, request.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003196
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003197 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003198 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
3199 }
3200
3201 // Sanity check - if we have too many in-flight frames, something has
3202 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07003203 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003204 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07003205 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
3206 kInFlightWarnLimitHighSpeed) {
3207 CLOGE("In-flight list too large for high speed configuration: %zu",
3208 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003209 }
3210}
3211
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003212void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003213 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003214 { // First return buffers cached in mInFlightMap
3215 Mutex::Autolock l(mInFlightLock);
3216 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
3217 const InFlightRequest &request = mInFlightMap.valueAt(idx);
3218 returnOutputBuffers(request.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003219 request.pendingOutputBuffers.size(), 0,
3220 /*timestampIncreasing*/true, request.outputSurfaces,
3221 request.resultExtras);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003222 }
3223 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07003224 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003225 }
3226
3227 // Then return all inflight buffers not returned by HAL
3228 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
3229 mInterface->getInflightBufferKeys(&inflightKeys);
3230
3231 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
3232 for (auto& pair : inflightKeys) {
3233 int32_t frameNumber = pair.first;
3234 int32_t streamId = pair.second;
3235 buffer_handle_t* buffer;
3236 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
3237 if (res != OK) {
3238 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
3239 __FUNCTION__, frameNumber, streamId);
3240 continue;
3241 }
3242
3243 camera3_stream_buffer_t streamBuffer;
3244 streamBuffer.buffer = buffer;
3245 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
3246 streamBuffer.acquire_fence = -1;
3247 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003248
3249 // First check if the buffer belongs to deleted stream
3250 bool streamDeleted = false;
3251 for (auto& stream : mDeletedStreams) {
3252 if (streamId == stream->getId()) {
3253 streamDeleted = true;
3254 // Return buffer to deleted stream
3255 camera3_stream* halStream = stream->asHalStream();
3256 streamBuffer.stream = halStream;
3257 switch (halStream->stream_type) {
3258 case CAMERA3_STREAM_OUTPUT:
3259 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
3260 if (res != OK) {
3261 ALOGE("%s: Can't return output buffer for frame %d to"
3262 " stream %d: %s (%d)", __FUNCTION__,
3263 frameNumber, streamId, strerror(-res), res);
3264 }
3265 break;
3266 case CAMERA3_STREAM_INPUT:
3267 res = stream->returnInputBuffer(streamBuffer);
3268 if (res != OK) {
3269 ALOGE("%s: Can't return input buffer for frame %d to"
3270 " stream %d: %s (%d)", __FUNCTION__,
3271 frameNumber, streamId, strerror(-res), res);
3272 }
3273 break;
3274 default: // Bi-direcitonal stream is deprecated
3275 ALOGE("%s: stream %d has unknown stream type %d",
3276 __FUNCTION__, streamId, halStream->stream_type);
3277 break;
3278 }
3279 break;
3280 }
3281 }
3282 if (streamDeleted) {
3283 continue;
3284 }
3285
3286 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003287 if (streamId == inputStreamId) {
3288 streamBuffer.stream = mInputStream->asHalStream();
3289 res = mInputStream->returnInputBuffer(streamBuffer);
3290 if (res != OK) {
3291 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003292 " stream %d: %s (%d)", __FUNCTION__,
3293 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003294 }
3295 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003296 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
3297 if (stream == nullptr) {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07003298 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
3299 continue;
3300 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07003301 streamBuffer.stream = stream->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003302 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
3303 }
3304 }
3305}
3306
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003307void Camera3Device::insertResultLocked(CaptureResult *result,
3308 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003309 if (result == nullptr) return;
3310
Emilian Peev71c73a22017-03-21 16:35:51 +00003311 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
3312 result->mMetadata.getAndLock());
3313 set_camera_metadata_vendor_id(meta, mVendorTagId);
3314 result->mMetadata.unlock(meta);
3315
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003316 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
3317 (int32_t*)&frameNumber, 1) != OK) {
3318 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
3319 return;
3320 }
3321
3322 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
3323 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
3324 return;
3325 }
3326
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003327 // Valid result, insert into queue
3328 List<CaptureResult>::iterator queuedResult =
3329 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
3330 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
3331 ", burstId = %" PRId32, __FUNCTION__,
3332 queuedResult->mResultExtras.requestId,
3333 queuedResult->mResultExtras.frameNumber,
3334 queuedResult->mResultExtras.burstId);
3335
3336 mResultSignal.signal();
3337}
3338
3339
3340void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003341 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003342 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003343 Mutex::Autolock l(mOutputLock);
3344
3345 CaptureResult captureResult;
3346 captureResult.mResultExtras = resultExtras;
3347 captureResult.mMetadata = partialResult;
3348
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003349 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003350}
3351
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003352
3353void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
3354 CaptureResultExtras &resultExtras,
3355 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003356 uint32_t frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003357 bool reprocess,
3358 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003359 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003360 if (pendingMetadata.isEmpty())
3361 return;
3362
3363 Mutex::Autolock l(mOutputLock);
3364
3365 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003366 if (reprocess) {
3367 if (frameNumber < mNextReprocessResultFrameNumber) {
3368 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003369 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003370 frameNumber, mNextReprocessResultFrameNumber);
3371 return;
3372 }
3373 mNextReprocessResultFrameNumber = frameNumber + 1;
3374 } else {
3375 if (frameNumber < mNextResultFrameNumber) {
3376 SET_ERR("Out-of-order capture result metadata submitted! "
3377 "(got frame number %d, expecting %d)",
3378 frameNumber, mNextResultFrameNumber);
3379 return;
3380 }
3381 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003382 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003383
3384 CaptureResult captureResult;
3385 captureResult.mResultExtras = resultExtras;
3386 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003387 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003388
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003389 // Append any previous partials to form a complete result
3390 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
3391 captureResult.mMetadata.append(collectedPartialResult);
3392 }
3393
3394 captureResult.mMetadata.sort();
3395
3396 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003397 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3398 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003399 SET_ERR("No timestamp provided by HAL for frame %d!",
3400 frameNumber);
3401 return;
3402 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003403 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3404 camera_metadata_entry timestamp =
3405 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3406 if (timestamp.count == 0) {
3407 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
3408 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
3409 return;
3410 }
3411 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003412
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003413 // Fix up some result metadata to account for HAL-level distortion correction
3414 status_t res = mDistortionMapper.correctCaptureResult(&captureResult.mMetadata);
3415 if (res != OK) {
3416 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
3417 frameNumber, strerror(res), res);
3418 return;
3419 }
3420
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003421 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
3422 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
3423
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003424 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003425}
3426
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003427/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003428 * Camera HAL device callback methods
3429 */
3430
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003431void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003432 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003433
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003434 status_t res;
3435
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003436 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003437 if (result->result == NULL && result->num_output_buffers == 0 &&
3438 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003439 SET_ERR("No result data provided by HAL for frame %d",
3440 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003441 return;
3442 }
Zhijun He204e3292014-07-14 17:09:23 -07003443
Zhijun He204e3292014-07-14 17:09:23 -07003444 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003445 result->result != NULL &&
3446 result->partial_result != 1) {
3447 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3448 " if partial result is not supported",
3449 frameNumber, result->partial_result);
3450 return;
3451 }
3452
3453 bool isPartialResult = false;
3454 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003455 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003456
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003457 // Get shutter timestamp and resultExtras from list of in-flight requests,
3458 // where it was added by the shutter notification for this frame. If the
3459 // shutter timestamp isn't received yet, append the output buffers to the
3460 // in-flight request and they will be returned when the shutter timestamp
3461 // arrives. Update the in-flight status and remove the in-flight entry if
3462 // all result data and shutter timestamp have been received.
3463 nsecs_t shutterTimestamp = 0;
3464
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003465 {
3466 Mutex::Autolock l(mInFlightLock);
3467 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3468 if (idx == NAME_NOT_FOUND) {
3469 SET_ERR("Unknown frame number for capture result: %d",
3470 frameNumber);
3471 return;
3472 }
3473 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003474 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3475 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003476 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003477 __FUNCTION__, request.resultExtras.requestId,
3478 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003479 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003480 // Always update the partial count to the latest one if it's not 0
3481 // (buffers only). When framework aggregates adjacent partial results
3482 // into one, the latest partial count will be used.
3483 if (result->partial_result != 0)
3484 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003485
3486 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003487 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003488 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3489 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3490 " the range of [1, %d] when metadata is included in the result",
3491 frameNumber, result->partial_result, mNumPartialResults);
3492 return;
3493 }
3494 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003495 if (isPartialResult && result->num_physcam_metadata) {
3496 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3497 " physical camera result", frameNumber);
3498 return;
3499 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003500 if (isPartialResult) {
3501 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003502 }
3503
Shuzhen Wang4a472662017-02-26 23:29:04 -08003504 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003505 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003506 sendPartialCaptureResult(result->result, request.resultExtras,
3507 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003508 }
3509 }
3510
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003511 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003512 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003513
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003514 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003515 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003516 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
3517 SET_ERR("Requested physical Camera Ids %d not equal to number of metadata %d",
3518 request.physicalCameraIds.size(), result->num_physcam_metadata);
3519 return;
3520 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003521 if (request.haveResultMetadata) {
3522 SET_ERR("Called multiple times with metadata for frame %d",
3523 frameNumber);
3524 return;
3525 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003526 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3527 String8 physicalId(result->physcam_ids[i]);
3528 std::set<String8>::iterator cameraIdIter =
3529 request.physicalCameraIds.find(physicalId);
3530 if (cameraIdIter != request.physicalCameraIds.end()) {
3531 request.physicalCameraIds.erase(cameraIdIter);
3532 } else {
3533 SET_ERR("Total result for frame %d has already returned for camera %s",
3534 frameNumber, physicalId.c_str());
3535 return;
3536 }
3537 }
Zhijun He204e3292014-07-14 17:09:23 -07003538 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003539 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003540 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003541 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003542 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003543 request.haveResultMetadata = true;
3544 }
3545
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003546 uint32_t numBuffersReturned = result->num_output_buffers;
3547 if (result->input_buffer != NULL) {
3548 if (hasInputBufferInRequest) {
3549 numBuffersReturned += 1;
3550 } else {
3551 ALOGW("%s: Input buffer should be NULL if there is no input"
3552 " buffer sent in the request",
3553 __FUNCTION__);
3554 }
3555 }
3556 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003557 if (request.numBuffersLeft < 0) {
3558 SET_ERR("Too many buffers returned for frame %d",
3559 frameNumber);
3560 return;
3561 }
3562
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003563 camera_metadata_ro_entry_t entry;
3564 res = find_camera_metadata_ro_entry(result->result,
3565 ANDROID_SENSOR_TIMESTAMP, &entry);
3566 if (res == OK && entry.count == 1) {
3567 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003568 }
3569
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003570 // If shutter event isn't received yet, append the output buffers to
3571 // the in-flight request. Otherwise, return the output buffers to
3572 // streams.
3573 if (shutterTimestamp == 0) {
3574 request.pendingOutputBuffers.appendArray(result->output_buffers,
3575 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003576 } else {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003577 bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003578 returnOutputBuffers(result->output_buffers,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003579 result->num_output_buffers, shutterTimestamp, timestampIncreasing,
3580 request.outputSurfaces, request.resultExtras);
Igor Murashkind2c90692013-04-02 12:32:32 -07003581 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003582
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003583 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003584 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3585 CameraMetadata physicalMetadata;
3586 physicalMetadata.append(result->physcam_metadata[i]);
3587 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3588 physicalMetadata});
3589 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003590 if (shutterTimestamp == 0) {
3591 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003592 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003593 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003594 CameraMetadata metadata;
3595 metadata = result->result;
3596 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003597 collectedPartialResult, frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003598 hasInputBufferInRequest, request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003599 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003600 }
3601
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003602 removeInFlightRequestIfReadyLocked(idx);
3603 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003604
Zhijun Hef0d962a2014-06-30 10:24:11 -07003605 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003606 if (hasInputBufferInRequest) {
3607 Camera3Stream *stream =
3608 Camera3Stream::cast(result->input_buffer->stream);
3609 res = stream->returnInputBuffer(*(result->input_buffer));
3610 // Note: stream may be deallocated at this point, if this buffer was the
3611 // last reference to it.
3612 if (res != OK) {
3613 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3614 " its stream:%s (%d)", __FUNCTION__,
3615 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003616 }
3617 } else {
3618 ALOGW("%s: Input buffer should be NULL if there is no input"
3619 " buffer sent in the request, skipping input buffer return.",
3620 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003621 }
3622 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003623}
3624
3625void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003626 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003627 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003628 {
3629 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003630 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003631 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003632
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003633 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003634 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003635 return;
3636 }
3637
3638 switch (msg->type) {
3639 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003640 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003641 break;
3642 }
3643 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003644 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003645 break;
3646 }
3647 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003648 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003649 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003650 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003651}
3652
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003653void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003654 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003655 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003656 // Map camera HAL error codes to ICameraDeviceCallback error codes
3657 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003658 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003659 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003660 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003661 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003662 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003663 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003664 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003665 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003666 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003667 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003668 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003669 };
3670
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003671 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003672 ((msg.error_code >= 0) &&
3673 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3674 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003675 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003676
3677 int streamId = 0;
3678 if (msg.error_stream != NULL) {
3679 Camera3Stream *stream =
3680 Camera3Stream::cast(msg.error_stream);
3681 streamId = stream->getId();
3682 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003683 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3684 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003685 streamId, msg.error_code);
3686
3687 CaptureResultExtras resultExtras;
3688 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003689 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003690 // SET_ERR calls notifyError
3691 SET_ERR("Camera HAL reported serious device error");
3692 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003693 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3694 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3695 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003696 {
3697 Mutex::Autolock l(mInFlightLock);
3698 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3699 if (idx >= 0) {
3700 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3701 r.requestStatus = msg.error_code;
3702 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07003703 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3704 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3705 errorCode) {
3706 r.skipResultMetadata = true;
3707 }
Emilian Peevba0fac32017-03-30 09:05:34 +01003708 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3709 errorCode) {
3710 // In case of missing result check whether the buffers
3711 // returned. If they returned, then remove inflight
3712 // request.
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003713 // TODO: should we call this for ERROR_CAMERA_REQUEST as well?
3714 // otherwise we are depending on HAL to send the buffers back after
3715 // calling notifyError. Not sure if that's in the spec.
Emilian Peevba0fac32017-03-30 09:05:34 +01003716 removeInFlightRequestIfReadyLocked(idx);
3717 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003718 } else {
3719 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003720 ALOGE("Camera %s: %s: cannot find in-flight request on "
3721 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003722 resultExtras.frameNumber);
3723 }
3724 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003725 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003726 if (listener != NULL) {
3727 listener->notifyError(errorCode, resultExtras);
3728 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003729 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003730 }
3731 break;
3732 default:
3733 // SET_ERR calls notifyError
3734 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3735 break;
3736 }
3737}
3738
3739void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003740 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003741 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003742 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003743
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003744 // Set timestamp for the request in the in-flight tracking
3745 // and get the request ID to send upstream
3746 {
3747 Mutex::Autolock l(mInFlightLock);
3748 idx = mInFlightMap.indexOfKey(msg.frame_number);
3749 if (idx >= 0) {
3750 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003751
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003752 // Verify ordering of shutter notifications
3753 {
3754 Mutex::Autolock l(mOutputLock);
3755 // TODO: need to track errors for tighter bounds on expected frame number.
3756 if (r.hasInputBuffer) {
3757 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3758 SET_ERR("Shutter notification out-of-order. Expected "
3759 "notification for frame %d, got frame %d",
3760 mNextReprocessShutterFrameNumber, msg.frame_number);
3761 return;
3762 }
3763 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3764 } else {
3765 if (msg.frame_number < mNextShutterFrameNumber) {
3766 SET_ERR("Shutter notification out-of-order. Expected "
3767 "notification for frame %d, got frame %d",
3768 mNextShutterFrameNumber, msg.frame_number);
3769 return;
3770 }
3771 mNextShutterFrameNumber = msg.frame_number + 1;
3772 }
3773 }
3774
Shuzhen Wang4a472662017-02-26 23:29:04 -08003775 r.shutterTimestamp = msg.timestamp;
3776 if (r.hasCallback) {
3777 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003778 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003779 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003780 // Call listener, if any
3781 if (listener != NULL) {
3782 listener->notifyShutter(r.resultExtras, msg.timestamp);
3783 }
3784 // send pending result and buffers
3785 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3786 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003787 r.hasInputBuffer, r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003788 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003789 bool timestampIncreasing = !(r.zslCapture || r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003790 returnOutputBuffers(r.pendingOutputBuffers.array(),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003791 r.pendingOutputBuffers.size(), r.shutterTimestamp, timestampIncreasing,
3792 r.outputSurfaces, r.resultExtras);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003793 r.pendingOutputBuffers.clear();
3794
3795 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003796 }
3797 }
3798 if (idx < 0) {
3799 SET_ERR("Shutter notification for non-existent frame number %d",
3800 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003801 }
3802}
3803
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003804CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003805 ALOGV("%s", __FUNCTION__);
3806
Igor Murashkin1e479c02013-09-06 16:55:14 -07003807 CameraMetadata retVal;
3808
3809 if (mRequestThread != NULL) {
3810 retVal = mRequestThread->getLatestRequest();
3811 }
3812
Igor Murashkin1e479c02013-09-06 16:55:14 -07003813 return retVal;
3814}
3815
Jianing Weicb0652e2014-03-12 18:29:36 -07003816
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003817void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3818 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3819 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3820}
3821
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003822/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003823 * HalInterface inner class methods
3824 */
3825
Yifan Hongf79b5542017-04-11 14:44:25 -07003826Camera3Device::HalInterface::HalInterface(
3827 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003828 std::shared_ptr<RequestMetadataQueue> queue,
3829 bool useHalBufManager) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003830 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003831 mRequestMetadataQueue(queue),
3832 mUseHalBufManager(useHalBufManager) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003833 // Check with hardware service manager if we can downcast these interfaces
3834 // Somewhat expensive, so cache the results at startup
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003835 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
3836 if (castResult_3_5.isOk()) {
3837 mHidlSession_3_5 = castResult_3_5;
3838 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003839 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3840 if (castResult_3_4.isOk()) {
3841 mHidlSession_3_4 = castResult_3_4;
3842 }
3843 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3844 if (castResult_3_3.isOk()) {
3845 mHidlSession_3_3 = castResult_3_3;
3846 }
3847}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003848
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003849Camera3Device::HalInterface::HalInterface() : mUseHalBufManager(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003850
3851Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003852 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003853 mRequestMetadataQueue(other.mRequestMetadataQueue),
3854 mUseHalBufManager(other.mUseHalBufManager) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003855
3856bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003857 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003858}
3859
3860void Camera3Device::HalInterface::clear() {
Emilian Peev9e740b02018-01-30 18:28:03 +00003861 mHidlSession_3_4.clear();
3862 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003863 mHidlSession.clear();
3864}
3865
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003866bool Camera3Device::HalInterface::supportBatchRequest() {
3867 return mHidlSession != nullptr;
3868}
3869
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003870status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3871 camera3_request_template_t templateId,
3872 /*out*/ camera_metadata_t **requestTemplate) {
3873 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3874 if (!valid()) return INVALID_OPERATION;
3875 status_t res = OK;
3876
Emilian Peev31abd0a2017-05-11 18:37:46 +01003877 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003878
3879 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003880 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003881 status = s;
3882 if (status == common::V1_0::Status::OK) {
3883 const camera_metadata *r =
3884 reinterpret_cast<const camera_metadata_t*>(request.data());
3885 size_t expectedSize = request.size();
3886 int ret = validate_camera_metadata_structure(r, &expectedSize);
3887 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3888 *requestTemplate = clone_camera_metadata(r);
3889 if (*requestTemplate == nullptr) {
3890 ALOGE("%s: Unable to clone camera metadata received from HAL",
3891 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003892 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003893 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003894 } else {
3895 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3896 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003897 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003898 }
3899 };
3900 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003901 RequestTemplate id;
3902 switch (templateId) {
3903 case CAMERA3_TEMPLATE_PREVIEW:
3904 id = RequestTemplate::PREVIEW;
3905 break;
3906 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3907 id = RequestTemplate::STILL_CAPTURE;
3908 break;
3909 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3910 id = RequestTemplate::VIDEO_RECORD;
3911 break;
3912 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3913 id = RequestTemplate::VIDEO_SNAPSHOT;
3914 break;
3915 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3916 id = RequestTemplate::ZERO_SHUTTER_LAG;
3917 break;
3918 case CAMERA3_TEMPLATE_MANUAL:
3919 id = RequestTemplate::MANUAL;
3920 break;
3921 default:
3922 // Unknown template ID, or this HAL is too old to support it
3923 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003924 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003925 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003926
Emilian Peev31abd0a2017-05-11 18:37:46 +01003927 if (!err.isOk()) {
3928 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3929 res = DEAD_OBJECT;
3930 } else {
3931 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003932 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003933
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003934 return res;
3935}
3936
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003937status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00003938 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003939 ATRACE_NAME("CameraHal::configureStreams");
3940 if (!valid()) return INVALID_OPERATION;
3941 status_t res = OK;
3942
Emilian Peev31abd0a2017-05-11 18:37:46 +01003943 // Convert stream config to HIDL
3944 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003945 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3946 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3947 requestedConfiguration3_2.streams.resize(config->num_streams);
3948 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003949 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003950 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3951 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003952 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003953
Emilian Peev31abd0a2017-05-11 18:37:46 +01003954 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3955 cam3stream->setBufferFreedListener(this);
3956 int streamId = cam3stream->getId();
3957 StreamType streamType;
3958 switch (src->stream_type) {
3959 case CAMERA3_STREAM_OUTPUT:
3960 streamType = StreamType::OUTPUT;
3961 break;
3962 case CAMERA3_STREAM_INPUT:
3963 streamType = StreamType::INPUT;
3964 break;
3965 default:
3966 ALOGE("%s: Stream %d: Unsupported stream type %d",
3967 __FUNCTION__, streamId, config->streams[i]->stream_type);
3968 return BAD_VALUE;
3969 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003970 dst3_2.id = streamId;
3971 dst3_2.streamType = streamType;
3972 dst3_2.width = src->width;
3973 dst3_2.height = src->height;
3974 dst3_2.format = mapToPixelFormat(src->format);
3975 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3976 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3977 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3978 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003979 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003980 if (src->physical_camera_id != nullptr) {
3981 dst3_4.physicalCameraId = src->physical_camera_id;
3982 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003983
3984 activeStreams.insert(streamId);
3985 // Create Buffer ID map if necessary
3986 if (mBufferIdMaps.count(streamId) == 0) {
3987 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3988 }
3989 }
3990 // remove BufferIdMap for deleted streams
3991 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3992 int streamId = it->first;
3993 bool active = activeStreams.count(streamId) > 0;
3994 if (!active) {
3995 it = mBufferIdMaps.erase(it);
3996 } else {
3997 ++it;
3998 }
3999 }
4000
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004001 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004002 res = mapToStreamConfigurationMode(
4003 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004004 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01004005 if (res != OK) {
4006 return res;
4007 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004008 requestedConfiguration3_2.operationMode = operationMode;
4009 requestedConfiguration3_4.operationMode = operationMode;
4010 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004011 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
4012 get_camera_metadata_size(sessionParams));
4013
Emilian Peev31abd0a2017-05-11 18:37:46 +01004014 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004015 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004016 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Emilian Peev31abd0a2017-05-11 18:37:46 +01004017 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004018
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004019 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004020 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
4021 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004022 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004023 };
4024
4025 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
4026 (hardware::Return<void>& err) -> status_t {
4027 if (!err.isOk()) {
4028 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4029 return DEAD_OBJECT;
4030 }
4031 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
4032 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
4033 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
4034 }
4035 return OK;
4036 };
4037
4038 // See if we have v3.4 or v3.3 HAL
4039 if (mHidlSession_3_5 != nullptr) {
4040 ALOGV("%s: v3.5 device found", __FUNCTION__);
4041 device::V3_5::StreamConfiguration requestedConfiguration3_5;
4042 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
4043 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
4044 auto err = mHidlSession_3_5->configureStreams_3_5(
4045 requestedConfiguration3_5, configStream34Cb);
4046 res = postprocConfigStream34(err);
4047 if (res != OK) {
4048 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01004049 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004050 } else if (mHidlSession_3_4 != nullptr) {
4051 // We do; use v3.4 for the call
4052 ALOGV("%s: v3.4 device found", __FUNCTION__);
4053 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
4054 auto err = mHidlSession_3_4->configureStreams_3_4(
4055 requestedConfiguration3_4, configStream34Cb);
4056 res = postprocConfigStream34(err);
4057 if (res != OK) {
4058 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004059 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004060 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004061 // We do; use v3.3 for the call
4062 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08004063 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01004064 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004065 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004066 finalConfiguration = halConfiguration;
4067 status = s;
4068 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004069 if (!err.isOk()) {
4070 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4071 return DEAD_OBJECT;
4072 }
4073 } else {
4074 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
4075 ALOGV("%s: v3.2 device found", __FUNCTION__);
4076 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004077 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004078 [&status, &finalConfiguration_3_2]
4079 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
4080 finalConfiguration_3_2 = halConfiguration;
4081 status = s;
4082 });
4083 if (!err.isOk()) {
4084 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4085 return DEAD_OBJECT;
4086 }
4087 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
4088 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
4089 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
4090 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08004091 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004092 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004093 }
4094
4095 if (status != common::V1_0::Status::OK ) {
4096 return CameraProviderManager::mapToStatusT(status);
4097 }
4098
4099 // And convert output stream configuration from HIDL
4100
4101 for (size_t i = 0; i < config->num_streams; i++) {
4102 camera3_stream_t *dst = config->streams[i];
4103 int streamId = Camera3Stream::cast(dst)->getId();
4104
4105 // Start scan at i, with the assumption that the stream order matches
4106 size_t realIdx = i;
4107 bool found = false;
4108 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004109 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004110 found = true;
4111 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004112 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004113 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
4114 }
4115 if (!found) {
4116 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
4117 __FUNCTION__, streamId);
4118 return INVALID_OPERATION;
4119 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004120 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004121
Emilian Peev710c1422017-08-30 11:19:38 +01004122 Camera3Stream* dstStream = Camera3Stream::cast(dst);
4123 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004124 dstStream->setDataSpaceOverride(false);
4125 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
4126 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
4127
Emilian Peev31abd0a2017-05-11 18:37:46 +01004128 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
4129 if (dst->format != overrideFormat) {
4130 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
4131 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004132 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004133 if (dst->data_space != overrideDataSpace) {
4134 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
4135 streamId, dst->format);
4136 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004137 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01004138 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004139 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
4140
Emilian Peev31abd0a2017-05-11 18:37:46 +01004141 // Override allowed with IMPLEMENTATION_DEFINED
4142 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004143 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004144 }
4145
Emilian Peev31abd0a2017-05-11 18:37:46 +01004146 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004147 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004148 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004149 __FUNCTION__, streamId);
4150 return INVALID_OPERATION;
4151 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004152 dstStream->setUsage(
4153 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01004154 } else {
4155 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004156 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004157 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
4158 __FUNCTION__, streamId);
4159 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004160 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004161 dstStream->setUsage(
4162 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004163 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07004164 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004165 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004166
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004167 return res;
4168}
4169
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004170status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004171 /*out*/device::V3_2::CaptureRequest* captureRequest,
4172 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004173 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004174 if (captureRequest == nullptr || handlesCreated == nullptr) {
4175 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
4176 __FUNCTION__, captureRequest, handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004177 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004178 }
4179
4180 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07004181
4182 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004183
4184 {
4185 std::lock_guard<std::mutex> lock(mInflightLock);
4186 if (request->input_buffer != nullptr) {
4187 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
4188 buffer_handle_t buf = *(request->input_buffer->buffer);
4189 auto pair = getBufferId(buf, streamId);
4190 bool isNewBuffer = pair.first;
4191 uint64_t bufferId = pair.second;
4192 captureRequest->inputBuffer.streamId = streamId;
4193 captureRequest->inputBuffer.bufferId = bufferId;
4194 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
4195 captureRequest->inputBuffer.status = BufferStatus::OK;
4196 native_handle_t *acquireFence = nullptr;
4197 if (request->input_buffer->acquire_fence != -1) {
4198 acquireFence = native_handle_create(1,0);
4199 acquireFence->data[0] = request->input_buffer->acquire_fence;
4200 handlesCreated->push_back(acquireFence);
4201 }
4202 captureRequest->inputBuffer.acquireFence = acquireFence;
4203 captureRequest->inputBuffer.releaseFence = nullptr;
4204
4205 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
4206 request->input_buffer->buffer,
4207 request->input_buffer->acquire_fence);
4208 } else {
4209 captureRequest->inputBuffer.streamId = -1;
4210 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
4211 }
4212
4213 captureRequest->outputBuffers.resize(request->num_output_buffers);
4214 for (size_t i = 0; i < request->num_output_buffers; i++) {
4215 const camera3_stream_buffer_t *src = request->output_buffers + i;
4216 StreamBuffer &dst = captureRequest->outputBuffers[i];
4217 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004218 if (src->buffer != nullptr) {
4219 buffer_handle_t buf = *(src->buffer);
4220 auto pair = getBufferId(buf, streamId);
4221 bool isNewBuffer = pair.first;
4222 dst.bufferId = pair.second;
4223 dst.buffer = isNewBuffer ? buf : nullptr;
4224 native_handle_t *acquireFence = nullptr;
4225 if (src->acquire_fence != -1) {
4226 acquireFence = native_handle_create(1,0);
4227 acquireFence->data[0] = src->acquire_fence;
4228 handlesCreated->push_back(acquireFence);
4229 }
4230 dst.acquireFence = acquireFence;
4231 } else if (mUseHalBufManager) {
4232 // HAL buffer management path
4233 dst.bufferId = BUFFER_ID_NO_BUFFER;
4234 dst.buffer = nullptr;
4235 dst.acquireFence = nullptr;
4236 } else {
4237 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
4238 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004239 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004240 dst.streamId = streamId;
4241 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004242 dst.releaseFence = nullptr;
4243
4244 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
4245 src->buffer, src->acquire_fence);
4246 }
4247 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004248 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004249}
4250
4251status_t Camera3Device::HalInterface::processBatchCaptureRequests(
4252 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
4253 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
4254 if (!valid()) return INVALID_OPERATION;
4255
Emilian Peevaebbe412018-01-15 13:53:24 +00004256 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
4257 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
4258 if (castResult_3_4.isOk()) {
4259 hidlSession_3_4 = castResult_3_4;
4260 }
4261
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004262 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00004263 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004264 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00004265 if (hidlSession_3_4 != nullptr) {
4266 captureRequests_3_4.resize(batchSize);
4267 } else {
4268 captureRequests.resize(batchSize);
4269 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004270 std::vector<native_handle_t*> handlesCreated;
4271
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004272 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004273 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004274 if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004275 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Emilian Peevaebbe412018-01-15 13:53:24 +00004276 /*out*/&handlesCreated);
4277 } else {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08004278 res = wrapAsHidlRequest(requests[i],
4279 /*out*/&captureRequests[i], /*out*/&handlesCreated);
4280 }
4281 if (res != OK) {
4282 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00004283 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004284 }
4285
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004286 std::vector<device::V3_2::BufferCache> cachesToRemove;
4287 {
4288 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4289 for (auto& pair : mFreedBuffers) {
4290 // The stream might have been removed since onBufferFreed
4291 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
4292 cachesToRemove.push_back({pair.first, pair.second});
4293 }
4294 }
4295 mFreedBuffers.clear();
4296 }
4297
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004298 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
4299 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07004300
4301 // Write metadata to FMQ.
4302 for (size_t i = 0; i < batchSize; i++) {
4303 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00004304 device::V3_2::CaptureRequest* captureRequest;
4305 if (hidlSession_3_4 != nullptr) {
4306 captureRequest = &captureRequests_3_4[i].v3_2;
4307 } else {
4308 captureRequest = &captureRequests[i];
4309 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004310
4311 if (request->settings != nullptr) {
4312 size_t settingsSize = get_camera_metadata_size(request->settings);
4313 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4314 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
4315 captureRequest->settings.resize(0);
4316 captureRequest->fmqSettingsSize = settingsSize;
4317 } else {
4318 if (mRequestMetadataQueue != nullptr) {
4319 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4320 }
4321 captureRequest->settings.setToExternal(
4322 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
4323 get_camera_metadata_size(request->settings));
4324 captureRequest->fmqSettingsSize = 0u;
4325 }
4326 } else {
4327 // A null request settings maps to a size-0 CameraMetadata
4328 captureRequest->settings.resize(0);
4329 captureRequest->fmqSettingsSize = 0u;
4330 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004331
4332 if (hidlSession_3_4 != nullptr) {
4333 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
4334 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00004335 if (request->physcam_settings != nullptr) {
4336 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
4337 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
4338 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
4339 settingsSize)) {
4340 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
4341 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
4342 settingsSize;
4343 } else {
4344 if (mRequestMetadataQueue != nullptr) {
4345 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
4346 }
4347 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
4348 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
4349 request->physcam_settings[j])),
4350 get_camera_metadata_size(request->physcam_settings[j]));
4351 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00004352 }
Emilian Peev00420d22018-02-05 21:33:13 +00004353 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00004354 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00004355 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00004356 }
4357 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
4358 request->physcam_id[j];
4359 }
4360 }
Yifan Hongf79b5542017-04-11 14:44:25 -07004361 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004362
4363 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004364 auto resultCallback =
4365 [&status, &numRequestProcessed] (auto s, uint32_t n) {
4366 status = s;
4367 *numRequestProcessed = n;
4368 };
Emilian Peevaebbe412018-01-15 13:53:24 +00004369 if (hidlSession_3_4 != nullptr) {
4370 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004371 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004372 } else {
4373 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07004374 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00004375 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07004376 if (!err.isOk()) {
4377 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4378 return DEAD_OBJECT;
4379 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004380 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
4381 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
4382 __FUNCTION__, *numRequestProcessed, batchSize);
4383 status = common::V1_0::Status::INTERNAL_ERROR;
4384 }
4385
4386 for (auto& handle : handlesCreated) {
4387 native_handle_delete(handle);
4388 }
4389 return CameraProviderManager::mapToStatusT(status);
4390}
4391
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004392status_t Camera3Device::HalInterface::processCaptureRequest(
4393 camera3_capture_request_t *request) {
4394 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004395 if (!valid()) return INVALID_OPERATION;
4396 status_t res = OK;
4397
Emilian Peev31abd0a2017-05-11 18:37:46 +01004398 uint32_t numRequestProcessed = 0;
4399 std::vector<camera3_capture_request_t*> requests(1);
4400 requests[0] = request;
4401 res = processBatchCaptureRequests(requests, &numRequestProcessed);
4402
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004403 return res;
4404}
4405
4406status_t Camera3Device::HalInterface::flush() {
4407 ATRACE_NAME("CameraHal::flush");
4408 if (!valid()) return INVALID_OPERATION;
4409 status_t res = OK;
4410
Emilian Peev31abd0a2017-05-11 18:37:46 +01004411 auto err = mHidlSession->flush();
4412 if (!err.isOk()) {
4413 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4414 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004415 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004416 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004417 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004418
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004419 return res;
4420}
4421
Emilian Peev31abd0a2017-05-11 18:37:46 +01004422status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004423 ATRACE_NAME("CameraHal::dump");
4424 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004425
Emilian Peev31abd0a2017-05-11 18:37:46 +01004426 // Handled by CameraProviderManager::dump
4427
4428 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004429}
4430
4431status_t Camera3Device::HalInterface::close() {
4432 ATRACE_NAME("CameraHal::close()");
4433 if (!valid()) return INVALID_OPERATION;
4434 status_t res = OK;
4435
Emilian Peev31abd0a2017-05-11 18:37:46 +01004436 auto err = mHidlSession->close();
4437 // Interface will be dead shortly anyway, so don't log errors
4438 if (!err.isOk()) {
4439 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004440 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004441
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004442 return res;
4443}
4444
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004445void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
4446 ATRACE_NAME("CameraHal::signalPipelineDrain");
4447 if (!valid() || mHidlSession_3_5 == nullptr) {
4448 ALOGE("%s called on invalid camera!", __FUNCTION__);
4449 return;
4450 }
4451
4452 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter);
4453 if (!err.isOk()) {
4454 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4455 return;
4456 }
4457}
4458
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004459void Camera3Device::HalInterface::getInflightBufferKeys(
4460 std::vector<std::pair<int32_t, int32_t>>* out) {
4461 std::lock_guard<std::mutex> lock(mInflightLock);
4462 out->clear();
4463 out->reserve(mInflightBufferMap.size());
4464 for (auto& pair : mInflightBufferMap) {
4465 uint64_t key = pair.first;
4466 int32_t streamId = key & 0xFFFFFFFF;
4467 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
4468 out->push_back(std::make_pair(frameNumber, streamId));
4469 }
4470 return;
4471}
4472
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004473status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004474 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004475 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004476 auto pair = std::make_pair(buffer, acquireFence);
4477 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004478 return OK;
4479}
4480
4481status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004482 int32_t frameNumber, int32_t streamId,
4483 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004484 std::lock_guard<std::mutex> lock(mInflightLock);
4485
4486 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4487 auto it = mInflightBufferMap.find(key);
4488 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004489 auto pair = it->second;
4490 *buffer = pair.first;
4491 int acquireFence = pair.second;
4492 if (acquireFence > 0) {
4493 ::close(acquireFence);
4494 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004495 mInflightBufferMap.erase(it);
4496 return OK;
4497}
4498
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004499status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
4500 uint64_t bufferId, buffer_handle_t* buf) {
4501 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4502 auto pair = mRequestedBuffers.insert({bufferId, buf});
4503 if (!pair.second) {
4504 ALOGE("%s: bufId %" PRIu64 " is already inflight!",
4505 __FUNCTION__, bufferId);
4506 return BAD_VALUE;
4507 }
4508 return OK;
4509}
4510
4511// Find and pop a buffer_handle_t based on bufferId
4512status_t Camera3Device::HalInterface::popInflightRequestBuffer(
4513 uint64_t bufferId, /*out*/ buffer_handle_t **buffer) {
4514 std::lock_guard<std::mutex> lock(mRequestedBuffersLock);
4515 auto it = mRequestedBuffers.find(bufferId);
4516 if (it == mRequestedBuffers.end()) {
4517 ALOGE("%s: bufId %" PRIu64 " is not inflight!",
4518 __FUNCTION__, bufferId);
4519 return BAD_VALUE;
4520 }
4521 *buffer = it->second;
4522 mRequestedBuffers.erase(it);
4523 return OK;
4524}
4525
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004526std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4527 const buffer_handle_t& buf, int streamId) {
4528 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4529
4530 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4531 auto it = bIdMap.find(buf);
4532 if (it == bIdMap.end()) {
4533 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004534 ALOGV("stream %d now have %zu buffer caches, buf %p",
4535 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004536 return std::make_pair(true, mNextBufferId - 1);
4537 } else {
4538 return std::make_pair(false, it->second);
4539 }
4540}
4541
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004542void Camera3Device::HalInterface::onBufferFreed(
4543 int streamId, const native_handle_t* handle) {
4544 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4545 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4546 auto mapIt = mBufferIdMaps.find(streamId);
4547 if (mapIt == mBufferIdMaps.end()) {
4548 // streamId might be from a deleted stream here
4549 ALOGI("%s: stream %d has been removed",
4550 __FUNCTION__, streamId);
4551 return;
4552 }
4553 BufferIdMap& bIdMap = mapIt->second;
4554 auto it = bIdMap.find(handle);
4555 if (it == bIdMap.end()) {
4556 ALOGW("%s: cannot find buffer %p in stream %d",
4557 __FUNCTION__, handle, streamId);
4558 return;
4559 } else {
4560 bufferId = it->second;
4561 bIdMap.erase(it);
4562 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4563 __FUNCTION__, streamId, bIdMap.size(), handle);
4564 }
4565 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4566}
4567
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004568/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004569 * RequestThread inner class methods
4570 */
4571
4572Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004573 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004574 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
4575 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004576 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004577 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004578 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004579 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004580 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004581 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004582 mReconfigured(false),
4583 mDoPause(false),
4584 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004585 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004586 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004587 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004588 mCurrentAfTriggerId(0),
4589 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004590 mRepeatingLastFrameNumber(
4591 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004592 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004593 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004594 mRequestLatency(kRequestLatencyBinSize),
4595 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004596 mLatestSessionParams(sessionParamKeys.size()),
4597 mUseHalBufManager(useHalBufManager) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004598 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004599}
4600
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004601Camera3Device::RequestThread::~RequestThread() {}
4602
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004603void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004604 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004605 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004606 Mutex::Autolock l(mRequestLock);
4607 mListener = listener;
4608}
4609
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004610void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4611 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004612 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004613 Mutex::Autolock l(mRequestLock);
4614 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004615 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004616 // Prepare video stream for high speed recording.
4617 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004618 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004619}
4620
Jianing Wei90e59c92014-03-12 18:29:36 -07004621status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004622 List<sp<CaptureRequest> > &requests,
4623 /*out*/
4624 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004625 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004626 Mutex::Autolock l(mRequestLock);
4627 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4628 ++it) {
4629 mRequestQueue.push_back(*it);
4630 }
4631
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004632 if (lastFrameNumber != NULL) {
4633 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4634 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4635 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4636 *lastFrameNumber);
4637 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004638
Jianing Wei90e59c92014-03-12 18:29:36 -07004639 unpauseForNewRequests();
4640
4641 return OK;
4642}
4643
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004644
4645status_t Camera3Device::RequestThread::queueTrigger(
4646 RequestTrigger trigger[],
4647 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004648 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004649 Mutex::Autolock l(mTriggerMutex);
4650 status_t ret;
4651
4652 for (size_t i = 0; i < count; ++i) {
4653 ret = queueTriggerLocked(trigger[i]);
4654
4655 if (ret != OK) {
4656 return ret;
4657 }
4658 }
4659
4660 return OK;
4661}
4662
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004663const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4664 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004665 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004666 if (d != nullptr) return d->mId;
4667 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004668}
4669
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004670status_t Camera3Device::RequestThread::queueTriggerLocked(
4671 RequestTrigger trigger) {
4672
4673 uint32_t tag = trigger.metadataTag;
4674 ssize_t index = mTriggerMap.indexOfKey(tag);
4675
4676 switch (trigger.getTagType()) {
4677 case TYPE_BYTE:
4678 // fall-through
4679 case TYPE_INT32:
4680 break;
4681 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004682 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4683 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004684 return INVALID_OPERATION;
4685 }
4686
4687 /**
4688 * Collect only the latest trigger, since we only have 1 field
4689 * in the request settings per trigger tag, and can't send more than 1
4690 * trigger per request.
4691 */
4692 if (index != NAME_NOT_FOUND) {
4693 mTriggerMap.editValueAt(index) = trigger;
4694 } else {
4695 mTriggerMap.add(tag, trigger);
4696 }
4697
4698 return OK;
4699}
4700
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004701status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004702 const RequestList &requests,
4703 /*out*/
4704 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004705 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004706 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004707 if (lastFrameNumber != NULL) {
4708 *lastFrameNumber = mRepeatingLastFrameNumber;
4709 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004710 mRepeatingRequests.clear();
4711 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4712 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004713
4714 unpauseForNewRequests();
4715
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004716 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004717 return OK;
4718}
4719
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004720bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004721 if (mRepeatingRequests.empty()) {
4722 return false;
4723 }
4724 int32_t requestId = requestIn->mResultExtras.requestId;
4725 const RequestList &repeatRequests = mRepeatingRequests;
4726 // All repeating requests are guaranteed to have same id so only check first quest
4727 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4728 return (firstRequest->mResultExtras.requestId == requestId);
4729}
4730
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004731status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004732 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004733 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004734 return clearRepeatingRequestsLocked(lastFrameNumber);
4735
4736}
4737
4738status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004739 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004740 if (lastFrameNumber != NULL) {
4741 *lastFrameNumber = mRepeatingLastFrameNumber;
4742 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004743 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004744 return OK;
4745}
4746
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004747status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004748 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004749 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004750 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004751 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004752
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004753 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004754
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004755 // Send errors for all requests pending in the request queue, including
4756 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004757 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004758 if (listener != NULL) {
4759 for (RequestList::iterator it = mRequestQueue.begin();
4760 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004761 // Abort the input buffers for reprocess requests.
4762 if ((*it)->mInputStream != NULL) {
4763 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004764 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4765 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004766 if (res != OK) {
4767 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4768 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4769 } else {
4770 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4771 if (res != OK) {
4772 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4773 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4774 }
4775 }
4776 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004777 // Set the frame number this request would have had, if it
4778 // had been submitted; this frame number will not be reused.
4779 // The requestId and burstId fields were set when the request was
4780 // submitted originally (in convertMetadataListToRequestListLocked)
4781 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004782 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004783 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004784 }
4785 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004786 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004787
4788 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004789 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004790 if (lastFrameNumber != NULL) {
4791 *lastFrameNumber = mRepeatingLastFrameNumber;
4792 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004793 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004794 return OK;
4795}
4796
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004797status_t Camera3Device::RequestThread::flush() {
4798 ATRACE_CALL();
4799 Mutex::Autolock l(mFlushLock);
4800
Emilian Peev08dd2452017-04-06 16:55:14 +01004801 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004802}
4803
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004804void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004805 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004806 Mutex::Autolock l(mPauseLock);
4807 mDoPause = paused;
4808 mDoPauseSignal.signal();
4809}
4810
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004811status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4812 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004813 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004814 Mutex::Autolock l(mLatestRequestMutex);
4815 status_t res;
4816 while (mLatestRequestId != requestId) {
4817 nsecs_t startTime = systemTime();
4818
4819 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4820 if (res != OK) return res;
4821
4822 timeout -= (systemTime() - startTime);
4823 }
4824
4825 return OK;
4826}
4827
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004828void Camera3Device::RequestThread::requestExit() {
4829 // Call parent to set up shutdown
4830 Thread::requestExit();
4831 // The exit from any possible waits
4832 mDoPauseSignal.signal();
4833 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004834
4835 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4836 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004837}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004838
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004839void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004840 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004841 bool surfaceAbandoned = false;
4842 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004843 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004844 {
4845 Mutex::Autolock l(mRequestLock);
4846 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4847 // repeating requests.
4848 for (const auto& request : mRepeatingRequests) {
4849 for (const auto& s : request->mOutputStreams) {
4850 if (s->isAbandoned()) {
4851 surfaceAbandoned = true;
4852 clearRepeatingRequestsLocked(&lastFrameNumber);
4853 break;
4854 }
4855 }
4856 if (surfaceAbandoned) {
4857 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004858 }
4859 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004860 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004861 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004862
4863 if (listener != NULL && surfaceAbandoned) {
4864 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004865 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004866}
4867
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004868bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004869 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004870 status_t res;
4871 size_t batchSize = mNextRequests.size();
4872 std::vector<camera3_capture_request_t*> requests(batchSize);
4873 uint32_t numRequestProcessed = 0;
4874 for (size_t i = 0; i < batchSize; i++) {
4875 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004876 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004877 }
4878
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004879 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4880
4881 bool triggerRemoveFailed = false;
4882 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4883 for (size_t i = 0; i < numRequestProcessed; i++) {
4884 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4885 nextRequest.submitted = true;
4886
4887
4888 // Update the latest request sent to HAL
4889 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4890 Mutex::Autolock al(mLatestRequestMutex);
4891
4892 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4893 mLatestRequest.acquire(cloned);
4894
4895 sp<Camera3Device> parent = mParent.promote();
4896 if (parent != NULL) {
4897 parent->monitorMetadata(TagMonitor::REQUEST,
4898 nextRequest.halRequest.frame_number,
4899 0, mLatestRequest);
4900 }
4901 }
4902
4903 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004904 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4905 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004906 }
4907
Emilian Peevaebbe412018-01-15 13:53:24 +00004908 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4909
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004910 if (!triggerRemoveFailed) {
4911 // Remove any previously queued triggers (after unlock)
4912 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4913 if (removeTriggerRes != OK) {
4914 triggerRemoveFailed = true;
4915 triggerFailedRequest = nextRequest;
4916 }
4917 }
4918 }
4919
4920 if (triggerRemoveFailed) {
4921 SET_ERR("RequestThread: Unable to remove triggers "
4922 "(capture request %d, HAL device: %s (%d)",
4923 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4924 cleanUpFailedRequests(/*sendRequestError*/ false);
4925 return false;
4926 }
4927
4928 if (res != OK) {
4929 // Should only get a failure here for malformed requests or device-level
4930 // errors, so consider all errors fatal. Bad metadata failures should
4931 // come through notify.
4932 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4933 mNextRequests[numRequestProcessed].halRequest.frame_number,
4934 strerror(-res), res);
4935 cleanUpFailedRequests(/*sendRequestError*/ false);
4936 return false;
4937 }
4938 return true;
4939}
4940
4941bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4942 status_t res;
4943
4944 for (auto& nextRequest : mNextRequests) {
4945 // Submit request and block until ready for next one
4946 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4947 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4948
4949 if (res != OK) {
4950 // Should only get a failure here for malformed requests or device-level
4951 // errors, so consider all errors fatal. Bad metadata failures should
4952 // come through notify.
4953 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4954 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4955 res);
4956 cleanUpFailedRequests(/*sendRequestError*/ false);
4957 return false;
4958 }
4959
4960 // Mark that the request has be submitted successfully.
4961 nextRequest.submitted = true;
4962
4963 // Update the latest request sent to HAL
4964 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4965 Mutex::Autolock al(mLatestRequestMutex);
4966
4967 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4968 mLatestRequest.acquire(cloned);
4969
4970 sp<Camera3Device> parent = mParent.promote();
4971 if (parent != NULL) {
4972 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4973 0, mLatestRequest);
4974 }
4975 }
4976
4977 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004978 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4979 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004980 }
4981
Emilian Peevaebbe412018-01-15 13:53:24 +00004982 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4983
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004984 // Remove any previously queued triggers (after unlock)
4985 res = removeTriggers(mPrevRequest);
4986 if (res != OK) {
4987 SET_ERR("RequestThread: Unable to remove triggers "
4988 "(capture request %d, HAL device: %s (%d)",
4989 nextRequest.halRequest.frame_number, strerror(-res), res);
4990 cleanUpFailedRequests(/*sendRequestError*/ false);
4991 return false;
4992 }
4993 }
4994 return true;
4995}
4996
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004997nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4998 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4999 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5000 find_camera_metadata_ro_entry(request,
5001 ANDROID_CONTROL_AE_MODE,
5002 &e);
5003 if (e.count == 0) return maxExpectedDuration;
5004
5005 switch (e.data.u8[0]) {
5006 case ANDROID_CONTROL_AE_MODE_OFF:
5007 find_camera_metadata_ro_entry(request,
5008 ANDROID_SENSOR_EXPOSURE_TIME,
5009 &e);
5010 if (e.count > 0) {
5011 maxExpectedDuration = e.data.i64[0];
5012 }
5013 find_camera_metadata_ro_entry(request,
5014 ANDROID_SENSOR_FRAME_DURATION,
5015 &e);
5016 if (e.count > 0) {
5017 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
5018 }
5019 break;
5020 default:
5021 find_camera_metadata_ro_entry(request,
5022 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
5023 &e);
5024 if (e.count > 1) {
5025 maxExpectedDuration = 1e9 / e.data.u8[0];
5026 }
5027 break;
5028 }
5029
5030 return maxExpectedDuration;
5031}
5032
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005033bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
5034 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
5035 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
5036 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
5037 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
5038 return true;
5039 }
5040
5041 return false;
5042}
5043
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005044bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
5045 ATRACE_CALL();
5046 bool updatesDetected = false;
5047
5048 for (auto tag : mSessionParamKeys) {
5049 camera_metadata_ro_entry entry = settings.find(tag);
5050 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
5051
5052 if (entry.count > 0) {
5053 bool isDifferent = false;
5054 if (lastEntry.count > 0) {
5055 // Have a last value, compare to see if changed
5056 if (lastEntry.type == entry.type &&
5057 lastEntry.count == entry.count) {
5058 // Same type and count, compare values
5059 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
5060 size_t entryBytes = bytesPerValue * lastEntry.count;
5061 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
5062 if (cmp != 0) {
5063 isDifferent = true;
5064 }
5065 } else {
5066 // Count or type has changed
5067 isDifferent = true;
5068 }
5069 } else {
5070 // No last entry, so always consider to be different
5071 isDifferent = true;
5072 }
5073
5074 if (isDifferent) {
5075 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01005076 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
5077 updatesDetected = true;
5078 }
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005079 mLatestSessionParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005080 }
5081 } else if (lastEntry.count > 0) {
5082 // Value has been removed
5083 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
5084 mLatestSessionParams.erase(tag);
5085 updatesDetected = true;
5086 }
5087 }
5088
5089 return updatesDetected;
5090}
5091
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005092bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005093 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005094 status_t res;
5095
5096 // Handle paused state.
5097 if (waitIfPaused()) {
5098 return true;
5099 }
5100
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005101 // Wait for the next batch of requests.
5102 waitForNextRequestBatch();
5103 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005104 return true;
5105 }
5106
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005107 // Get the latest request ID, if any
5108 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005109 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00005110 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005111 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005112 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005113 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005114 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
5115 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005116 }
5117
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005118 // 'mNextRequests' will at this point contain either a set of HFR batched requests
5119 // or a single request from streaming or burst. In either case the first element
5120 // should contain the latest camera settings that we need to check for any session
5121 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00005122 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005123 res = OK;
5124
5125 //Input stream buffers are already acquired at this point so an input stream
5126 //will not be able to move to idle state unless we force it.
5127 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5128 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
5129 if (res != OK) {
5130 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
5131 cleanUpFailedRequests(/*sendRequestError*/ false);
5132 return false;
5133 }
5134 }
5135
5136 if (res == OK) {
5137 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5138 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08005139 sp<Camera3Device> parent = mParent.promote();
5140 if (parent != nullptr) {
5141 parent->pauseStateNotify(true);
5142 }
5143
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005144 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5145
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005146 if (parent != nullptr) {
5147 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
5148 }
5149
5150 statusTracker->markComponentActive(mStatusId);
5151 setPaused(false);
5152 }
5153
5154 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
5155 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
5156 if (res != OK) {
5157 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
5158 cleanUpFailedRequests(/*sendRequestError*/ false);
5159 return false;
5160 }
5161 }
5162 }
5163 }
5164
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005165 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005166 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005167 if (res == TIMED_OUT) {
5168 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005169 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07005170 // Check if any stream is abandoned.
5171 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005172 return true;
5173 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005174 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07005175 return false;
5176 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005177
Zhijun Hecc27e112013-10-03 16:12:43 -07005178 // Inform waitUntilRequestProcessed thread of a new request ID
5179 {
5180 Mutex::Autolock al(mLatestRequestMutex);
5181
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005182 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07005183 mLatestRequestSignal.signal();
5184 }
5185
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005186 // Submit a batch of requests to HAL.
5187 // Use flush lock only when submitting multilple requests in a batch.
5188 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
5189 // which may take a long time to finish so synchronizing flush() and
5190 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
5191 // For now, only synchronize for high speed recording and we should figure something out for
5192 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005193 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07005194
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005195 if (useFlushLock) {
5196 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005197 }
5198
Zhijun Hef0645c12016-08-02 00:58:11 -07005199 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005200 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07005201
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005202 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07005203 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005204 if (mInterface->supportBatchRequest()) {
5205 submitRequestSuccess = sendRequestsBatch();
5206 } else {
5207 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005208 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07005209 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
5210 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07005211
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005212 if (submitRequestSuccess) {
5213 sp<Camera3Device> parent = mParent.promote();
5214 if (parent != nullptr) {
5215 parent->mRequestBufferSM.onRequestSubmitted();
5216 }
5217 }
5218
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005219 if (useFlushLock) {
5220 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005221 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005222
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005223 // Unset as current request
5224 {
5225 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005226 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005227 }
5228
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08005229 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005230}
5231
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005232status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005233 ATRACE_CALL();
5234
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005235 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005236 for (size_t i = 0; i < mNextRequests.size(); i++) {
5237 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005238 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5239 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5240 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5241
5242 // Prepare a request to HAL
5243 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
5244
5245 // Insert any queued triggers (before metadata is locked)
5246 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005247 if (res < 0) {
5248 SET_ERR("RequestThread: Unable to insert triggers "
5249 "(capture request %d, HAL device: %s (%d)",
5250 halRequest->frame_number, strerror(-res), res);
5251 return INVALID_OPERATION;
5252 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005253
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005254 int triggerCount = res;
5255 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
5256 mPrevTriggers = triggerCount;
5257
5258 // If the request is the same as last, or we had triggers last time
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005259 bool newRequest = (mPrevRequest != captureRequest || triggersMixedIn) &&
5260 // Request settings are all the same within one batch, so only treat the first
5261 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07005262 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00005263 if (newRequest) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005264 /**
5265 * HAL workaround:
5266 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
5267 */
5268 res = addDummyTriggerIds(captureRequest);
5269 if (res != OK) {
5270 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
5271 "(capture request %d, HAL device: %s (%d)",
5272 halRequest->frame_number, strerror(-res), res);
5273 return INVALID_OPERATION;
5274 }
5275
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005276 {
5277 // Correct metadata regions for distortion correction if enabled
5278 sp<Camera3Device> parent = mParent.promote();
5279 if (parent != nullptr) {
5280 res = parent->mDistortionMapper.correctCaptureRequest(
5281 &(captureRequest->mSettingsList.begin()->metadata));
5282 if (res != OK) {
5283 SET_ERR("RequestThread: Unable to correct capture requests "
5284 "for lens distortion for request %d: %s (%d)",
5285 halRequest->frame_number, strerror(-res), res);
5286 return INVALID_OPERATION;
5287 }
5288 }
5289 }
5290
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005291 /**
5292 * The request should be presorted so accesses in HAL
5293 * are O(logn). Sidenote, sorting a sorted metadata is nop.
5294 */
Emilian Peevaebbe412018-01-15 13:53:24 +00005295 captureRequest->mSettingsList.begin()->metadata.sort();
5296 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005297 mPrevRequest = captureRequest;
5298 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
5299
5300 IF_ALOGV() {
5301 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5302 find_camera_metadata_ro_entry(
5303 halRequest->settings,
5304 ANDROID_CONTROL_AF_TRIGGER,
5305 &e
5306 );
5307 if (e.count > 0) {
5308 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
5309 __FUNCTION__,
5310 halRequest->frame_number,
5311 e.data.u8[0]);
5312 }
5313 }
5314 } else {
5315 // leave request.settings NULL to indicate 'reuse latest given'
5316 ALOGVV("%s: Request settings are REUSED",
5317 __FUNCTION__);
5318 }
5319
Emilian Peevaebbe412018-01-15 13:53:24 +00005320 if (captureRequest->mSettingsList.size() > 1) {
5321 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
5322 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00005323 if (newRequest) {
5324 halRequest->physcam_settings =
5325 new const camera_metadata* [halRequest->num_physcam_settings];
5326 } else {
5327 halRequest->physcam_settings = nullptr;
5328 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005329 auto it = ++captureRequest->mSettingsList.begin();
5330 size_t i = 0;
5331 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
5332 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00005333 if (newRequest) {
5334 it->metadata.sort();
5335 halRequest->physcam_settings[i] = it->metadata.getAndLock();
5336 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005337 }
5338 }
5339
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005340 uint32_t totalNumBuffers = 0;
5341
5342 // Fill in buffers
5343 if (captureRequest->mInputStream != NULL) {
5344 halRequest->input_buffer = &captureRequest->mInputBuffer;
5345 totalNumBuffers += 1;
5346 } else {
5347 halRequest->input_buffer = NULL;
5348 }
5349
5350 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
5351 captureRequest->mOutputStreams.size());
5352 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005353 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07005354
5355 sp<Camera3Device> parent = mParent.promote();
5356 if (parent == NULL) {
5357 // Should not happen, and nowhere to send errors to, so just log it
5358 CLOGE("RequestThread: Parent is gone");
5359 return INVALID_OPERATION;
5360 }
5361 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
5362
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005363 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005364 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005365 sp<Camera3OutputStreamInterface> outputStream =
5366 captureRequest->mOutputStreams.editItemAt(j);
5367 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005368
5369 // Prepare video buffers for high speed recording on the first video request.
5370 if (mPrepareVideoStream && outputStream->isVideoStream()) {
5371 // Only try to prepare video stream on the first video request.
5372 mPrepareVideoStream = false;
5373
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005374 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
5375 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005376 while (res == NOT_ENOUGH_DATA) {
5377 res = outputStream->prepareNextBuffer();
5378 }
5379 if (res != OK) {
5380 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
5381 __FUNCTION__, strerror(-res), res);
5382 outputStream->cancelPrepare();
5383 }
5384 }
5385
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005386 std::vector<size_t> uniqueSurfaceIds;
5387 res = outputStream->getUniqueSurfaceIds(
5388 captureRequest->mOutputSurfaces[streamId],
5389 &uniqueSurfaceIds);
5390 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
5391 if (res != OK && res != INVALID_OPERATION) {
5392 ALOGE("%s: failed to query stream %d unique surface IDs",
5393 __FUNCTION__, streamId);
5394 return res;
5395 }
5396 if (res == OK) {
5397 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
5398 }
5399
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005400 if (mUseHalBufManager) {
5401 // HAL will request buffer through requestStreamBuffer API
5402 camera3_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
5403 buffer.stream = outputStream->asHalStream();
5404 buffer.buffer = nullptr;
5405 buffer.status = CAMERA3_BUFFER_STATUS_OK;
5406 buffer.acquire_fence = -1;
5407 buffer.release_fence = -1;
5408 } else {
5409 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
5410 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005411 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005412 if (res != OK) {
5413 // Can't get output buffer from gralloc queue - this could be due to
5414 // abandoned queue or other consumer misbehavior, so not a fatal
5415 // error
5416 ALOGE("RequestThread: Can't get output buffer, skipping request:"
5417 " %s (%d)", strerror(-res), res);
5418
5419 return TIMED_OUT;
5420 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005421 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07005422
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005423 String8 physicalCameraId = outputStream->getPhysicalCameraId();
5424
5425 if (!physicalCameraId.isEmpty()) {
5426 // Physical stream isn't supported for input request.
5427 if (halRequest->input_buffer) {
5428 CLOGE("Physical stream is not supported for input request");
5429 return INVALID_OPERATION;
5430 }
5431 requestedPhysicalCameras.insert(physicalCameraId);
5432 }
5433 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005434 }
5435 totalNumBuffers += halRequest->num_output_buffers;
5436
5437 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08005438 // If this request list is for constrained high speed recording (not
5439 // preview), and the current request is not the last one in the batch,
5440 // do not send callback to the app.
5441 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005442 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08005443 hasCallback = false;
5444 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005445 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005446 bool isZslCapture = false;
Emilian Peev9dd21f42018-08-03 13:39:29 +01005447 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
5448 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5449 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
5450 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
5451 isStillCapture = true;
5452 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
5453 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005454
5455 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
5456 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
5457 isZslCapture = true;
5458 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005459 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005460 res = parent->registerInFlight(halRequest->frame_number,
5461 totalNumBuffers, captureRequest->mResultExtras,
5462 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005463 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005464 calculateMaxExpectedDuration(halRequest->settings),
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005465 requestedPhysicalCameras, isStillCapture, isZslCapture,
5466 (mUseHalBufManager) ? uniqueSurfaceIdMap :
5467 SurfaceMap{});
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005468 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
5469 ", burstId = %" PRId32 ".",
5470 __FUNCTION__,
5471 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
5472 captureRequest->mResultExtras.burstId);
5473 if (res != OK) {
5474 SET_ERR("RequestThread: Unable to register new in-flight request:"
5475 " %s (%d)", strerror(-res), res);
5476 return INVALID_OPERATION;
5477 }
5478 }
5479
5480 return OK;
5481}
5482
Igor Murashkin1e479c02013-09-06 16:55:14 -07005483CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005484 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005485 Mutex::Autolock al(mLatestRequestMutex);
5486
5487 ALOGV("RequestThread::%s", __FUNCTION__);
5488
5489 return mLatestRequest;
5490}
5491
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005492bool Camera3Device::RequestThread::isStreamPending(
5493 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005494 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005495 Mutex::Autolock l(mRequestLock);
5496
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005497 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005498 if (!nextRequest.submitted) {
5499 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5500 if (stream == s) return true;
5501 }
5502 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005503 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005504 }
5505
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005506 for (const auto& request : mRequestQueue) {
5507 for (const auto& s : request->mOutputStreams) {
5508 if (stream == s) return true;
5509 }
5510 if (stream == request->mInputStream) return true;
5511 }
5512
5513 for (const auto& request : mRepeatingRequests) {
5514 for (const auto& s : request->mOutputStreams) {
5515 if (stream == s) return true;
5516 }
5517 if (stream == request->mInputStream) return true;
5518 }
5519
5520 return false;
5521}
Jianing Weicb0652e2014-03-12 18:29:36 -07005522
Emilian Peev40ead602017-09-26 15:46:36 +01005523bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5524 ATRACE_CALL();
5525 Mutex::Autolock l(mRequestLock);
5526
5527 for (const auto& nextRequest : mNextRequests) {
5528 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5529 if (s.first == streamId) {
5530 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5531 if (it != s.second.end()) {
5532 return true;
5533 }
5534 }
5535 }
5536 }
5537
5538 for (const auto& request : mRequestQueue) {
5539 for (const auto& s : request->mOutputSurfaces) {
5540 if (s.first == streamId) {
5541 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5542 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005543 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005544 }
5545 }
5546 }
5547 }
5548
5549 for (const auto& request : mRepeatingRequests) {
5550 for (const auto& s : request->mOutputSurfaces) {
5551 if (s.first == streamId) {
5552 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5553 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005554 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005555 }
5556 }
5557 }
5558 }
5559
5560 return false;
5561}
5562
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005563void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
5564 if (!mUseHalBufManager) {
5565 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
5566 return;
5567 }
5568
5569 Mutex::Autolock pl(mPauseLock);
5570 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005571 mInterface->signalPipelineDrain(streamIds);
5572 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005573 }
5574 // If request thread is still busy, wait until paused then notify HAL
5575 mNotifyPipelineDrain = true;
5576 mStreamIdsToBeDrained = streamIds;
5577}
5578
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005579nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005580 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005581 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005582 return mExpectedInflightDuration > kMinInflightDuration ?
5583 mExpectedInflightDuration : kMinInflightDuration;
5584}
5585
Emilian Peevaebbe412018-01-15 13:53:24 +00005586void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
5587 camera3_capture_request_t *halRequest) {
5588 if ((request == nullptr) || (halRequest == nullptr)) {
5589 ALOGE("%s: Invalid request!", __FUNCTION__);
5590 return;
5591 }
5592
5593 if (halRequest->num_physcam_settings > 0) {
5594 if (halRequest->physcam_id != nullptr) {
5595 delete [] halRequest->physcam_id;
5596 halRequest->physcam_id = nullptr;
5597 }
5598 if (halRequest->physcam_settings != nullptr) {
5599 auto it = ++(request->mSettingsList.begin());
5600 size_t i = 0;
5601 for (; it != request->mSettingsList.end(); it++, i++) {
5602 it->metadata.unlock(halRequest->physcam_settings[i]);
5603 }
5604 delete [] halRequest->physcam_settings;
5605 halRequest->physcam_settings = nullptr;
5606 }
5607 }
5608}
5609
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005610void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5611 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005612 return;
5613 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005614
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005615 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005616 // Skip the ones that have been submitted successfully.
5617 if (nextRequest.submitted) {
5618 continue;
5619 }
5620
5621 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5622 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5623 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5624
5625 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005626 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005627 }
5628
Emilian Peevaebbe412018-01-15 13:53:24 +00005629 cleanupPhysicalSettings(captureRequest, halRequest);
5630
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005631 if (captureRequest->mInputStream != NULL) {
5632 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5633 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5634 }
5635
5636 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005637 //Buffers that failed processing could still have
5638 //valid acquire fence.
5639 int acquireFence = (*outputBuffers)[i].acquire_fence;
5640 if (0 <= acquireFence) {
5641 close(acquireFence);
5642 outputBuffers->editItemAt(i).acquire_fence = -1;
5643 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005644 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5645 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
5646 }
5647
5648 if (sendRequestError) {
5649 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005650 sp<NotificationListener> listener = mListener.promote();
5651 if (listener != NULL) {
5652 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005653 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005654 captureRequest->mResultExtras);
5655 }
5656 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005657
5658 // Remove yet-to-be submitted inflight request from inflightMap
5659 {
5660 sp<Camera3Device> parent = mParent.promote();
5661 if (parent != NULL) {
5662 Mutex::Autolock l(parent->mInFlightLock);
5663 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5664 if (idx >= 0) {
5665 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5666 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5667 parent->removeInFlightMapEntryLocked(idx);
5668 }
5669 }
5670 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005671 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005672
5673 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005674 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005675}
5676
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005677void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005678 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005679 // Optimized a bit for the simple steady-state case (single repeating
5680 // request), to avoid putting that request in the queue temporarily.
5681 Mutex::Autolock l(mRequestLock);
5682
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005683 assert(mNextRequests.empty());
5684
5685 NextRequest nextRequest;
5686 nextRequest.captureRequest = waitForNextRequestLocked();
5687 if (nextRequest.captureRequest == nullptr) {
5688 return;
5689 }
5690
5691 nextRequest.halRequest = camera3_capture_request_t();
5692 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005693 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005694
5695 // Wait for additional requests
5696 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5697
5698 for (size_t i = 1; i < batchSize; i++) {
5699 NextRequest additionalRequest;
5700 additionalRequest.captureRequest = waitForNextRequestLocked();
5701 if (additionalRequest.captureRequest == nullptr) {
5702 break;
5703 }
5704
5705 additionalRequest.halRequest = camera3_capture_request_t();
5706 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005707 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005708 }
5709
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005710 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005711 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005712 mNextRequests.size(), batchSize);
5713 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005714 }
5715
5716 return;
5717}
5718
5719sp<Camera3Device::CaptureRequest>
5720 Camera3Device::RequestThread::waitForNextRequestLocked() {
5721 status_t res;
5722 sp<CaptureRequest> nextRequest;
5723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005724 while (mRequestQueue.empty()) {
5725 if (!mRepeatingRequests.empty()) {
5726 // Always atomically enqueue all requests in a repeating request
5727 // list. Guarantees a complete in-sequence set of captures to
5728 // application.
5729 const RequestList &requests = mRepeatingRequests;
5730 RequestList::const_iterator firstRequest =
5731 requests.begin();
5732 nextRequest = *firstRequest;
5733 mRequestQueue.insert(mRequestQueue.end(),
5734 ++firstRequest,
5735 requests.end());
5736 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005737
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005738 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005739
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005740 break;
5741 }
5742
5743 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5744
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005745 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5746 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005747 Mutex::Autolock pl(mPauseLock);
5748 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005749 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005750 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005751 // Let the tracker know
5752 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5753 if (statusTracker != 0) {
5754 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5755 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005756 if (mNotifyPipelineDrain) {
5757 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5758 mNotifyPipelineDrain = false;
5759 mStreamIdsToBeDrained.clear();
5760 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005761 sp<Camera3Device> parent = mParent.promote();
5762 if (parent != nullptr) {
5763 parent->mRequestBufferSM.onRequestThreadPaused();
5764 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005765 }
5766 // Stop waiting for now and let thread management happen
5767 return NULL;
5768 }
5769 }
5770
5771 if (nextRequest == NULL) {
5772 // Don't have a repeating request already in hand, so queue
5773 // must have an entry now.
5774 RequestList::iterator firstRequest =
5775 mRequestQueue.begin();
5776 nextRequest = *firstRequest;
5777 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005778 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5779 sp<NotificationListener> listener = mListener.promote();
5780 if (listener != NULL) {
5781 listener->notifyRequestQueueEmpty();
5782 }
5783 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005784 }
5785
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005786 // In case we've been unpaused by setPaused clearing mDoPause, need to
5787 // update internal pause state (capture/setRepeatingRequest unpause
5788 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005789 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005790 if (mPaused) {
5791 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5792 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5793 if (statusTracker != 0) {
5794 statusTracker->markComponentActive(mStatusId);
5795 }
5796 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005797 mPaused = false;
5798
5799 // Check if we've reconfigured since last time, and reset the preview
5800 // request if so. Can't use 'NULL request == repeat' across configure calls.
5801 if (mReconfigured) {
5802 mPrevRequest.clear();
5803 mReconfigured = false;
5804 }
5805
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005806 if (nextRequest != NULL) {
5807 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005808 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5809 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005810
5811 // Since RequestThread::clear() removes buffers from the input stream,
5812 // get the right buffer here before unlocking mRequestLock
5813 if (nextRequest->mInputStream != NULL) {
5814 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5815 if (res != OK) {
5816 // Can't get input buffer from gralloc queue - this could be due to
5817 // disconnected queue or other producer misbehavior, so not a fatal
5818 // error
5819 ALOGE("%s: Can't get input buffer, skipping request:"
5820 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005821
5822 sp<NotificationListener> listener = mListener.promote();
5823 if (listener != NULL) {
5824 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005825 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005826 nextRequest->mResultExtras);
5827 }
5828 return NULL;
5829 }
5830 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005831 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005832
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005833 return nextRequest;
5834}
5835
5836bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005837 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005838 status_t res;
5839 Mutex::Autolock l(mPauseLock);
5840 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005841 if (mPaused == false) {
5842 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005843 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5844 // Let the tracker know
5845 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5846 if (statusTracker != 0) {
5847 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5848 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005849 if (mNotifyPipelineDrain) {
5850 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5851 mNotifyPipelineDrain = false;
5852 mStreamIdsToBeDrained.clear();
5853 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005854 sp<Camera3Device> parent = mParent.promote();
5855 if (parent != nullptr) {
5856 parent->mRequestBufferSM.onRequestThreadPaused();
5857 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005858 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005859
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005860 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005861 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005862 return true;
5863 }
5864 }
5865 // We don't set mPaused to false here, because waitForNextRequest needs
5866 // to further manage the paused state in case of starvation.
5867 return false;
5868}
5869
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005870void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005871 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005872 // With work to do, mark thread as unpaused.
5873 // If paused by request (setPaused), don't resume, to avoid
5874 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005875 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005876 Mutex::Autolock p(mPauseLock);
5877 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005878 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5879 if (mPaused) {
5880 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5881 if (statusTracker != 0) {
5882 statusTracker->markComponentActive(mStatusId);
5883 }
5884 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005885 mPaused = false;
5886 }
5887}
5888
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005889void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5890 sp<Camera3Device> parent = mParent.promote();
5891 if (parent != NULL) {
5892 va_list args;
5893 va_start(args, fmt);
5894
5895 parent->setErrorStateV(fmt, args);
5896
5897 va_end(args);
5898 }
5899}
5900
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005901status_t Camera3Device::RequestThread::insertTriggers(
5902 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005903 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005904 Mutex::Autolock al(mTriggerMutex);
5905
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005906 sp<Camera3Device> parent = mParent.promote();
5907 if (parent == NULL) {
5908 CLOGE("RequestThread: Parent is gone");
5909 return DEAD_OBJECT;
5910 }
5911
Emilian Peevaebbe412018-01-15 13:53:24 +00005912 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005913 size_t count = mTriggerMap.size();
5914
5915 for (size_t i = 0; i < count; ++i) {
5916 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005917 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005918
5919 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5920 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5921 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005922 if (isAeTrigger) {
5923 request->mResultExtras.precaptureTriggerId = triggerId;
5924 mCurrentPreCaptureTriggerId = triggerId;
5925 } else {
5926 request->mResultExtras.afTriggerId = triggerId;
5927 mCurrentAfTriggerId = triggerId;
5928 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005929 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005930 }
5931
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005932 camera_metadata_entry entry = metadata.find(tag);
5933
5934 if (entry.count > 0) {
5935 /**
5936 * Already has an entry for this trigger in the request.
5937 * Rewrite it with our requested trigger value.
5938 */
5939 RequestTrigger oldTrigger = trigger;
5940
5941 oldTrigger.entryValue = entry.data.u8[0];
5942
5943 mTriggerReplacedMap.add(tag, oldTrigger);
5944 } else {
5945 /**
5946 * More typical, no trigger entry, so we just add it
5947 */
5948 mTriggerRemovedMap.add(tag, trigger);
5949 }
5950
5951 status_t res;
5952
5953 switch (trigger.getTagType()) {
5954 case TYPE_BYTE: {
5955 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5956 res = metadata.update(tag,
5957 &entryValue,
5958 /*count*/1);
5959 break;
5960 }
5961 case TYPE_INT32:
5962 res = metadata.update(tag,
5963 &trigger.entryValue,
5964 /*count*/1);
5965 break;
5966 default:
5967 ALOGE("%s: Type not supported: 0x%x",
5968 __FUNCTION__,
5969 trigger.getTagType());
5970 return INVALID_OPERATION;
5971 }
5972
5973 if (res != OK) {
5974 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5975 ", value %d", __FUNCTION__, trigger.getTagName(),
5976 trigger.entryValue);
5977 return res;
5978 }
5979
5980 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5981 trigger.getTagName(),
5982 trigger.entryValue);
5983 }
5984
5985 mTriggerMap.clear();
5986
5987 return count;
5988}
5989
5990status_t Camera3Device::RequestThread::removeTriggers(
5991 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005992 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005993 Mutex::Autolock al(mTriggerMutex);
5994
Emilian Peevaebbe412018-01-15 13:53:24 +00005995 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005996
5997 /**
5998 * Replace all old entries with their old values.
5999 */
6000 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
6001 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
6002
6003 status_t res;
6004
6005 uint32_t tag = trigger.metadataTag;
6006 switch (trigger.getTagType()) {
6007 case TYPE_BYTE: {
6008 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
6009 res = metadata.update(tag,
6010 &entryValue,
6011 /*count*/1);
6012 break;
6013 }
6014 case TYPE_INT32:
6015 res = metadata.update(tag,
6016 &trigger.entryValue,
6017 /*count*/1);
6018 break;
6019 default:
6020 ALOGE("%s: Type not supported: 0x%x",
6021 __FUNCTION__,
6022 trigger.getTagType());
6023 return INVALID_OPERATION;
6024 }
6025
6026 if (res != OK) {
6027 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
6028 ", trigger value %d", __FUNCTION__,
6029 trigger.getTagName(), trigger.entryValue);
6030 return res;
6031 }
6032 }
6033 mTriggerReplacedMap.clear();
6034
6035 /**
6036 * Remove all new entries.
6037 */
6038 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
6039 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
6040 status_t res = metadata.erase(trigger.metadataTag);
6041
6042 if (res != OK) {
6043 ALOGE("%s: Failed to erase metadata with trigger tag %s"
6044 ", trigger value %d", __FUNCTION__,
6045 trigger.getTagName(), trigger.entryValue);
6046 return res;
6047 }
6048 }
6049 mTriggerRemovedMap.clear();
6050
6051 return OK;
6052}
6053
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006054status_t Camera3Device::RequestThread::addDummyTriggerIds(
6055 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08006056 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006057 static const int32_t dummyTriggerId = 1;
6058 status_t res;
6059
Emilian Peevaebbe412018-01-15 13:53:24 +00006060 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07006061
6062 // If AF trigger is active, insert a dummy AF trigger ID if none already
6063 // exists
6064 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
6065 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
6066 if (afTrigger.count > 0 &&
6067 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
6068 afId.count == 0) {
6069 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
6070 if (res != OK) return res;
6071 }
6072
6073 // If AE precapture trigger is active, insert a dummy precapture trigger ID
6074 // if none already exists
6075 camera_metadata_entry pcTrigger =
6076 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
6077 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
6078 if (pcTrigger.count > 0 &&
6079 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
6080 pcId.count == 0) {
6081 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
6082 &dummyTriggerId, 1);
6083 if (res != OK) return res;
6084 }
6085
6086 return OK;
6087}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006088
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006089/**
6090 * PreparerThread inner class methods
6091 */
6092
6093Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07006094 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006095 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006096}
6097
6098Camera3Device::PreparerThread::~PreparerThread() {
6099 Thread::requestExitAndWait();
6100 if (mCurrentStream != nullptr) {
6101 mCurrentStream->cancelPrepare();
6102 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6103 mCurrentStream.clear();
6104 }
6105 clear();
6106}
6107
Ruben Brunkc78ac262015-08-13 17:58:46 -07006108status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006109 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006110 status_t res;
6111
6112 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006113 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006114
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006115 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006116 if (res == OK) {
6117 // No preparation needed, fire listener right off
6118 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006119 if (listener != NULL) {
6120 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006121 }
6122 return OK;
6123 } else if (res != NOT_ENOUGH_DATA) {
6124 return res;
6125 }
6126
6127 // Need to prepare, start up thread if necessary
6128 if (!mActive) {
6129 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
6130 // isn't running
6131 Thread::requestExitAndWait();
6132 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6133 if (res != OK) {
6134 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006135 if (listener != NULL) {
6136 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006137 }
6138 return res;
6139 }
6140 mCancelNow = false;
6141 mActive = true;
6142 ALOGV("%s: Preparer stream started", __FUNCTION__);
6143 }
6144
6145 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006146 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006147 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
6148
6149 return OK;
6150}
6151
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006152void Camera3Device::PreparerThread::pause() {
6153 ATRACE_CALL();
6154
6155 Mutex::Autolock l(mLock);
6156
6157 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
6158 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
6159 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
6160 int currentMaxCount = mCurrentMaxCount;
6161 mPendingStreams.clear();
6162 mCancelNow = true;
6163 while (mActive) {
6164 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
6165 if (res == TIMED_OUT) {
6166 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
6167 return;
6168 } else if (res != OK) {
6169 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
6170 return;
6171 }
6172 }
6173
6174 //Check whether the prepare thread was able to complete the current
6175 //stream. In case work is still pending emplace it along with the rest
6176 //of the streams in the pending list.
6177 if (currentStream != nullptr) {
6178 if (!mCurrentPrepareComplete) {
6179 pendingStreams.emplace(currentMaxCount, currentStream);
6180 }
6181 }
6182
6183 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
6184 for (const auto& it : mPendingStreams) {
6185 it.second->cancelPrepare();
6186 }
6187}
6188
6189status_t Camera3Device::PreparerThread::resume() {
6190 ATRACE_CALL();
6191 status_t res;
6192
6193 Mutex::Autolock l(mLock);
6194 sp<NotificationListener> listener = mListener.promote();
6195
6196 if (mActive) {
6197 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
6198 return NO_INIT;
6199 }
6200
6201 auto it = mPendingStreams.begin();
6202 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006203 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006204 if (res == OK) {
6205 if (listener != NULL) {
6206 listener->notifyPrepared(it->second->getId());
6207 }
6208 it = mPendingStreams.erase(it);
6209 } else if (res != NOT_ENOUGH_DATA) {
6210 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
6211 res, strerror(-res));
6212 it = mPendingStreams.erase(it);
6213 } else {
6214 it++;
6215 }
6216 }
6217
6218 if (mPendingStreams.empty()) {
6219 return OK;
6220 }
6221
6222 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6223 if (res != OK) {
6224 ALOGE("%s: Unable to start preparer stream: %d (%s)",
6225 __FUNCTION__, res, strerror(-res));
6226 return res;
6227 }
6228 mCancelNow = false;
6229 mActive = true;
6230 ALOGV("%s: Preparer stream started", __FUNCTION__);
6231
6232 return OK;
6233}
6234
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006235status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006236 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006237 Mutex::Autolock l(mLock);
6238
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006239 for (const auto& it : mPendingStreams) {
6240 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006241 }
6242 mPendingStreams.clear();
6243 mCancelNow = true;
6244
6245 return OK;
6246}
6247
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006248void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006249 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006250 Mutex::Autolock l(mLock);
6251 mListener = listener;
6252}
6253
6254bool Camera3Device::PreparerThread::threadLoop() {
6255 status_t res;
6256 {
6257 Mutex::Autolock l(mLock);
6258 if (mCurrentStream == nullptr) {
6259 // End thread if done with work
6260 if (mPendingStreams.empty()) {
6261 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
6262 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
6263 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
6264 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006265 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006266 return false;
6267 }
6268
6269 // Get next stream to prepare
6270 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006271 mCurrentStream = it->second;
6272 mCurrentMaxCount = it->first;
6273 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006274 mPendingStreams.erase(it);
6275 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
6276 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
6277 } else if (mCancelNow) {
6278 mCurrentStream->cancelPrepare();
6279 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6280 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
6281 mCurrentStream.clear();
6282 mCancelNow = false;
6283 return true;
6284 }
6285 }
6286
6287 res = mCurrentStream->prepareNextBuffer();
6288 if (res == NOT_ENOUGH_DATA) return true;
6289 if (res != OK) {
6290 // Something bad happened; try to recover by cancelling prepare and
6291 // signalling listener anyway
6292 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
6293 mCurrentStream->getId(), res, strerror(-res));
6294 mCurrentStream->cancelPrepare();
6295 }
6296
6297 // This stream has finished, notify listener
6298 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006299 sp<NotificationListener> listener = mListener.promote();
6300 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006301 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
6302 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006303 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006304 }
6305
6306 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6307 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006308 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006309
6310 return true;
6311}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006312
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006313status_t Camera3Device::RequestBufferStateMachine::initialize(
6314 sp<camera3::StatusTracker> statusTracker) {
6315 if (statusTracker == nullptr) {
6316 ALOGE("%s: statusTracker is null", __FUNCTION__);
6317 return BAD_VALUE;
6318 }
6319
6320 std::lock_guard<std::mutex> lock(mLock);
6321 mStatusTracker = statusTracker;
6322 mRequestBufferStatusId = statusTracker->addComponent();
6323 return OK;
6324}
6325
6326bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
6327 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006328 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006329 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006330 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006331 return true;
6332 }
6333 return false;
6334}
6335
6336void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
6337 std::lock_guard<std::mutex> lock(mLock);
6338 if (!mRequestBufferOngoing) {
6339 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
6340 return;
6341 }
6342 mRequestBufferOngoing = false;
6343 if (mStatus == RB_STATUS_PENDING_STOP) {
6344 checkSwitchToStopLocked();
6345 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006346 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006347}
6348
6349void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
6350 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006351 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006352 return;
6353}
6354
6355void Camera3Device::RequestBufferStateMachine::onRequestSubmitted() {
6356 std::lock_guard<std::mutex> lock(mLock);
6357 mRequestThreadPaused = false;
6358 mInflightMapEmpty = false;
6359 if (mStatus == RB_STATUS_STOPPED) {
6360 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006361 }
6362 return;
6363}
6364
6365void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
6366 std::lock_guard<std::mutex> lock(mLock);
6367 mRequestThreadPaused = true;
6368 if (mStatus == RB_STATUS_PENDING_STOP) {
6369 checkSwitchToStopLocked();
6370 }
6371 return;
6372}
6373
6374void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
6375 std::lock_guard<std::mutex> lock(mLock);
6376 mInflightMapEmpty = true;
6377 if (mStatus == RB_STATUS_PENDING_STOP) {
6378 checkSwitchToStopLocked();
6379 }
6380 return;
6381}
6382
6383void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6384 std::lock_guard<std::mutex> lock(mLock);
6385 if (!checkSwitchToStopLocked()) {
6386 mStatus = RB_STATUS_PENDING_STOP;
6387 }
6388 return;
6389}
6390
6391void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6392 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6393 if (statusTracker != nullptr) {
6394 if (active) {
6395 statusTracker->markComponentActive(mRequestBufferStatusId);
6396 } else {
6397 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6398 }
6399 }
6400}
6401
6402bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6403 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6404 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006405 return true;
6406 }
6407 return false;
6408}
6409
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006410}; // namespace android