blob: 543914e5a48af6cf892fbfb6d304d8dd284ae27b [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"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080059
60using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080061using namespace android::hardware::camera;
62using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063
64namespace android {
65
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080066Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080067 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080068 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070069 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070070 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070071 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070072 mUsePartialResult(false),
73 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080074 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070076 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070077 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070078 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000079 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010080 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
81 mLastTemplateId(-1)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080082{
83 ATRACE_CALL();
84 camera3_callback_ops::notify = &sNotify;
85 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080086 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080087}
88
89Camera3Device::~Camera3Device()
90{
91 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080092 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 disconnect();
94}
95
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080096const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080097 return mId;
98}
99
Emilian Peevbd8c5032018-02-14 23:05:40 +0000100status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800101 ATRACE_CALL();
102 Mutex::Autolock il(mInterfaceLock);
103 Mutex::Autolock l(mLock);
104
105 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
106 if (mStatus != STATUS_UNINITIALIZED) {
107 CLOGE("Already initialized!");
108 return INVALID_OPERATION;
109 }
110 if (manager == nullptr) return INVALID_OPERATION;
111
112 sp<ICameraDeviceSession> session;
113 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800114 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115 /*out*/ &session);
116 ATRACE_END();
117 if (res != OK) {
118 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
119 return res;
120 }
121
Steven Moreland5ff9c912017-03-09 23:13:00 -0800122 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800123 if (res != OK) {
124 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
125 session->close();
126 return res;
127 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800128
Yifan Hongf79b5542017-04-11 14:44:25 -0700129 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700130 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
131 [&queue](const auto& descriptor) {
132 queue = std::make_shared<RequestMetadataQueue>(descriptor);
133 if (!queue->isValid() || queue->availableToWrite() <= 0) {
134 ALOGE("HAL returns empty request metadata fmq, not use it");
135 queue = nullptr;
136 // don't use the queue onwards.
137 }
138 });
139 if (!requestQueueRet.isOk()) {
140 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
141 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700142 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700143 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700144
145 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700146 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700147 [&resQueue](const auto& descriptor) {
148 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
149 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700150 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700151 resQueue = nullptr;
152 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700153 }
154 });
155 if (!resultQueueRet.isOk()) {
156 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
157 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700158 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700159 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700160 IF_ALOGV() {
161 session->interfaceChain([](
162 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
163 ALOGV("Session interface chain:");
164 for (auto iface : interfaceChain) {
165 ALOGV(" %s", iface.c_str());
166 }
167 });
168 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700169
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700170 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000171 std::string providerType;
172 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000173 mTagMonitor.initialize(mVendorTagId);
174 if (!monitorTags.isEmpty()) {
175 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
176 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800177
178 return initializeCommonLocked();
179}
180
181status_t Camera3Device::initializeCommonLocked() {
182
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 /** Start up status tracker thread */
184 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800185 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700186 if (res != OK) {
187 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
188 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800189 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 mStatusTracker.clear();
191 return res;
192 }
193
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700194 /** Register in-flight map to the status tracker */
195 mInFlightStatusId = mStatusTracker->addComponent();
196
Zhijun He125684a2015-12-26 15:07:30 -0800197 /** Create buffer manager */
198 mBufferManager = new Camera3BufferManager();
199
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000200 Vector<int32_t> sessionParamKeys;
201 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
202 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
203 if (sessionKeysEntry.count > 0) {
204 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
205 }
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700206 /** Start up request queue thread */
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000207 mRequestThread = new RequestThread(this, mStatusTracker, mInterface, sessionParamKeys);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800208 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800209 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700210 SET_ERR_L("Unable to start request queue thread: %s (%d)",
211 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800212 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800213 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800214 return res;
215 }
216
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700217 mPreparerThread = new PreparerThread();
218
Ruben Brunk183f0562015-08-12 12:55:02 -0700219 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800220 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700221 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700222 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800225 // Measure the clock domain offset between camera and video/hw_composer
226 camera_metadata_entry timestampSource =
227 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
228 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
229 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
230 mTimestampOffset = getMonoToBoottimeOffset();
231 }
232
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700233 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100234 camera_metadata_entry partialResultsCount =
235 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
236 if (partialResultsCount.count > 0) {
237 mNumPartialResults = partialResultsCount.data.i32[0];
238 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700239 }
240
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700241 camera_metadata_entry configs =
242 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
243 for (uint32_t i = 0; i < configs.count; i += 4) {
244 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
245 configs.data.i32[i + 3] ==
246 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
247 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
248 configs.data.i32[i + 2]));
249 }
250 }
251
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700252 if (DistortionMapper::isDistortionSupported(mDeviceInfo)) {
253 res = mDistortionMapper.setupStaticInfo(mDeviceInfo);
254 if (res != OK) {
255 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
256 return res;
257 }
258 }
259
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800260 return OK;
261}
262
263status_t Camera3Device::disconnect() {
264 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800266
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700267 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800268
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700269 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700270 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700271 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 {
273 Mutex::Autolock l(mLock);
274 if (mStatus == STATUS_UNINITIALIZED) return res;
275
276 if (mStatus == STATUS_ACTIVE ||
277 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
278 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700279 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700280 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700283 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700285 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
286 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287 // Continue to close device even in case of error
288 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700289 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800290 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 if (mStatus == STATUS_ERROR) {
293 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700294 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700295
296 if (mStatusTracker != NULL) {
297 mStatusTracker->requestExit();
298 }
299
300 if (mRequestThread != NULL) {
301 mRequestThread->requestExit();
302 }
303
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700304 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
305 for (size_t i = 0; i < mOutputStreams.size(); i++) {
306 streams.push_back(mOutputStreams[i]);
307 }
308 if (mInputStream != nullptr) {
309 streams.push_back(mInputStream);
310 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311 }
312
313 // Joining done without holding mLock, otherwise deadlocks may ensue
314 // as the threads try to access parent state
315 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
316 // HAL may be in a bad state, so waiting for request thread
317 // (which may be stuck in the HAL processCaptureRequest call)
318 // could be dangerous.
319 mRequestThread->join();
320 }
321
322 if (mStatusTracker != NULL) {
323 mStatusTracker->join();
324 }
325
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800326 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700327 {
328 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700330 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800331 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700332 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700334 // Call close without internal mutex held, as the HAL close may need to
335 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800336 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700337
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700338 flushInflightRequests();
339
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700340 {
341 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800342 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700343 mOutputStreams.clear();
344 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700345 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700346 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700347 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700348 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800349
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700350 for (auto& weakStream : streams) {
351 sp<Camera3StreamInterface> stream = weakStream.promote();
352 if (stream != nullptr) {
353 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
354 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
355 }
356 }
357
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700358 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700359 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800360}
361
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700362// For dumping/debugging only -
363// try to acquire a lock a few times, eventually give up to proceed with
364// debug/dump operations
365bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
366 bool gotLock = false;
367 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
368 if (lock.tryLock() == NO_ERROR) {
369 gotLock = true;
370 break;
371 } else {
372 usleep(kDumpSleepDuration);
373 }
374 }
375 return gotLock;
376}
377
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700378Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
379 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100380 const int STREAM_CONFIGURATION_SIZE = 4;
381 const int STREAM_FORMAT_OFFSET = 0;
382 const int STREAM_WIDTH_OFFSET = 1;
383 const int STREAM_HEIGHT_OFFSET = 2;
384 const int STREAM_IS_INPUT_OFFSET = 3;
385 camera_metadata_ro_entry_t availableStreamConfigs =
386 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
387 if (availableStreamConfigs.count == 0 ||
388 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
389 return Size(0, 0);
390 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700391
Emilian Peev08dd2452017-04-06 16:55:14 +0100392 // Get max jpeg size (area-wise).
393 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
394 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
395 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
396 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
397 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
398 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
399 && format == HAL_PIXEL_FORMAT_BLOB &&
400 (width * height > maxJpegWidth * maxJpegHeight)) {
401 maxJpegWidth = width;
402 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700403 }
404 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100405
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700406 return Size(maxJpegWidth, maxJpegHeight);
407}
408
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800409nsecs_t Camera3Device::getMonoToBoottimeOffset() {
410 // try three times to get the clock offset, choose the one
411 // with the minimum gap in measurements.
412 const int tries = 3;
413 nsecs_t bestGap, measured;
414 for (int i = 0; i < tries; ++i) {
415 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
416 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
417 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
418 const nsecs_t gap = tmono2 - tmono;
419 if (i == 0 || gap < bestGap) {
420 bestGap = gap;
421 measured = tbase - ((tmono + tmono2) >> 1);
422 }
423 }
424 return measured;
425}
426
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800427hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
428 int frameworkFormat) {
429 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
430}
431
432DataspaceFlags Camera3Device::mapToHidlDataspace(
433 android_dataspace dataSpace) {
434 return dataSpace;
435}
436
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700437BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100438 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700439 return usage;
440}
441
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800442StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
443 switch (rotation) {
444 case CAMERA3_STREAM_ROTATION_0:
445 return StreamRotation::ROTATION_0;
446 case CAMERA3_STREAM_ROTATION_90:
447 return StreamRotation::ROTATION_90;
448 case CAMERA3_STREAM_ROTATION_180:
449 return StreamRotation::ROTATION_180;
450 case CAMERA3_STREAM_ROTATION_270:
451 return StreamRotation::ROTATION_270;
452 }
453 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
454 return StreamRotation::ROTATION_0;
455}
456
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800457status_t Camera3Device::mapToStreamConfigurationMode(
458 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
459 if (mode == nullptr) return BAD_VALUE;
460 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
461 switch(operationMode) {
462 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
463 *mode = StreamConfigurationMode::NORMAL_MODE;
464 break;
465 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
466 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
467 break;
468 default:
469 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
470 return BAD_VALUE;
471 }
472 } else {
473 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800474 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800475 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800476}
477
478camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
479 switch (status) {
480 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
481 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
482 }
483 return CAMERA3_BUFFER_STATUS_ERROR;
484}
485
486int Camera3Device::mapToFrameworkFormat(
487 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
488 return static_cast<uint32_t>(pixelFormat);
489}
490
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700491android_dataspace Camera3Device::mapToFrameworkDataspace(
492 DataspaceFlags dataSpace) {
493 return static_cast<android_dataspace>(dataSpace);
494}
495
Emilian Peev050f5dc2017-05-18 14:43:56 +0100496uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700497 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700498 return usage;
499}
500
Emilian Peev050f5dc2017-05-18 14:43:56 +0100501uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700502 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700503 return usage;
504}
505
Zhijun Hef7da0962014-04-24 13:27:56 -0700506ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700507 // Get max jpeg size (area-wise).
508 Size maxJpegResolution = getMaxJpegResolution();
509 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800510 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
511 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700512 return BAD_VALUE;
513 }
514
Zhijun Hef7da0962014-04-24 13:27:56 -0700515 // Get max jpeg buffer size
516 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700517 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
518 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800519 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
520 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700521 return BAD_VALUE;
522 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700523 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800524 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700525
526 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700527 float scaleFactor = ((float) (width * height)) /
528 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800529 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
530 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700531 if (jpegBufferSize > maxJpegBufferSize) {
532 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700533 }
534
535 return jpegBufferSize;
536}
537
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700538ssize_t Camera3Device::getPointCloudBufferSize() const {
539 const int FLOATS_PER_POINT=4;
540 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
541 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800542 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
543 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700544 return BAD_VALUE;
545 }
546 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
547 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
548 return maxBytesForPointCloud;
549}
550
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800551ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800552 const int PER_CONFIGURATION_SIZE = 3;
553 const int WIDTH_OFFSET = 0;
554 const int HEIGHT_OFFSET = 1;
555 const int SIZE_OFFSET = 2;
556 camera_metadata_ro_entry rawOpaqueSizes =
557 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800558 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800559 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800560 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
561 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800562 return BAD_VALUE;
563 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700564
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800565 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
566 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
567 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
568 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
569 }
570 }
571
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800572 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
573 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800574 return BAD_VALUE;
575}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700576
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800577status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
578 ATRACE_CALL();
579 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700580
581 // Try to lock, but continue in case of failure (to avoid blocking in
582 // deadlocks)
583 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
584 bool gotLock = tryLockSpinRightRound(mLock);
585
586 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800587 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
588 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700589 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800590 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
591 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700592
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800593 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700594
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800595 String16 templatesOption("-t");
596 int n = args.size();
597 for (int i = 0; i < n; i++) {
598 if (args[i] == templatesOption) {
599 dumpTemplates = true;
600 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000601 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700602 if (i + 1 < n) {
603 String8 monitorTags = String8(args[i + 1]);
604 if (monitorTags == "off") {
605 mTagMonitor.disableMonitoring();
606 } else {
607 mTagMonitor.parseTagsToMonitor(monitorTags);
608 }
609 } else {
610 mTagMonitor.disableMonitoring();
611 }
612 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 }
614
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800615 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 const char *status =
618 mStatus == STATUS_ERROR ? "ERROR" :
619 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700620 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
621 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800622 mStatus == STATUS_ACTIVE ? "ACTIVE" :
623 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700624
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700626 if (mStatus == STATUS_ERROR) {
627 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
628 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800630 const char *mode =
631 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
632 mOperatingMode == static_cast<int>(
633 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
634 "CUSTOM";
635 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800636
637 if (mInputStream != NULL) {
638 write(fd, lines.string(), lines.size());
639 mInputStream->dump(fd, args);
640 } else {
641 lines.appendFormat(" No input stream.\n");
642 write(fd, lines.string(), lines.size());
643 }
644 for (size_t i = 0; i < mOutputStreams.size(); i++) {
645 mOutputStreams[i]->dump(fd,args);
646 }
647
Zhijun He431503c2016-03-07 17:30:16 -0800648 if (mBufferManager != NULL) {
649 lines = String8(" Camera3 Buffer Manager:\n");
650 write(fd, lines.string(), lines.size());
651 mBufferManager->dump(fd, args);
652 }
Zhijun He125684a2015-12-26 15:07:30 -0800653
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700654 lines = String8(" In-flight requests:\n");
655 if (mInFlightMap.size() == 0) {
656 lines.append(" None\n");
657 } else {
658 for (size_t i = 0; i < mInFlightMap.size(); i++) {
659 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700660 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700661 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800662 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700663 r.numBuffersLeft);
664 }
665 }
666 write(fd, lines.string(), lines.size());
667
Shuzhen Wang686f6442017-06-20 16:16:04 -0700668 if (mRequestThread != NULL) {
669 mRequestThread->dumpCaptureRequestLatency(fd,
670 " ProcessCaptureRequest latency histogram:");
671 }
672
Igor Murashkin1e479c02013-09-06 16:55:14 -0700673 {
674 lines = String8(" Last request sent:\n");
675 write(fd, lines.string(), lines.size());
676
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700678 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
679 }
680
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800681 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800682 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800683 "TEMPLATE_PREVIEW",
684 "TEMPLATE_STILL_CAPTURE",
685 "TEMPLATE_VIDEO_RECORD",
686 "TEMPLATE_VIDEO_SNAPSHOT",
687 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800688 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800689 };
690
691 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800692 camera_metadata_t *templateRequest = nullptr;
693 mInterface->constructDefaultRequestSettings(
694 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800695 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800696 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800697 lines.append(" Not supported\n");
698 write(fd, lines.string(), lines.size());
699 } else {
700 write(fd, lines.string(), lines.size());
701 dump_indented_camera_metadata(templateRequest,
702 fd, /*verbosity*/2, /*indentation*/8);
703 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800704 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800705 }
706 }
707
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700708 mTagMonitor.dumpMonitoredMetadata(fd);
709
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800710 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800711 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800712 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800713 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716 if (gotLock) mLock.unlock();
717 if (gotInterfaceLock) mInterfaceLock.unlock();
718
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800719 return OK;
720}
721
722const CameraMetadata& Camera3Device::info() const {
723 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
725 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700726 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727 mStatus == STATUS_ERROR ?
728 "when in error state" : "before init");
729 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800730 return mDeviceInfo;
731}
732
Jianing Wei90e59c92014-03-12 18:29:36 -0700733status_t Camera3Device::checkStatusOkToCaptureLocked() {
734 switch (mStatus) {
735 case STATUS_ERROR:
736 CLOGE("Device has encountered a serious error");
737 return INVALID_OPERATION;
738 case STATUS_UNINITIALIZED:
739 CLOGE("Device not initialized");
740 return INVALID_OPERATION;
741 case STATUS_UNCONFIGURED:
742 case STATUS_CONFIGURED:
743 case STATUS_ACTIVE:
744 // OK
745 break;
746 default:
747 SET_ERR_L("Unexpected status: %d", mStatus);
748 return INVALID_OPERATION;
749 }
750 return OK;
751}
752
753status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000754 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700755 const std::list<const SurfaceMap> &surfaceMaps,
756 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700757 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700758 if (requestList == NULL) {
759 CLOGE("requestList cannot be NULL.");
760 return BAD_VALUE;
761 }
762
Jianing Weicb0652e2014-03-12 18:29:36 -0700763 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000764 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
766 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
767 ++metadataIt, ++surfaceMapIt) {
768 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700769 if (newRequest == 0) {
770 CLOGE("Can't create capture request");
771 return BAD_VALUE;
772 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700773
Shuzhen Wang9d066012016-09-30 11:30:20 -0700774 newRequest->mRepeating = repeating;
775
Jianing Weicb0652e2014-03-12 18:29:36 -0700776 // Setup burst Id and request Id
777 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000778 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
779 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700780 CLOGE("RequestID entry exists; but must not be empty in metadata");
781 return BAD_VALUE;
782 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000783 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
784 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700785 } else {
786 CLOGE("RequestID does not exist in metadata");
787 return BAD_VALUE;
788 }
789
Jianing Wei90e59c92014-03-12 18:29:36 -0700790 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700791
792 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700793 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700794 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
795 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
796 return BAD_VALUE;
797 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700798
799 // Setup batch size if this is a high speed video recording request.
800 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
801 auto firstRequest = requestList->begin();
802 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
803 if (outputStream->isVideoStream()) {
804 (*firstRequest)->mBatchSize = requestList->size();
805 break;
806 }
807 }
808 }
809
Jianing Wei90e59c92014-03-12 18:29:36 -0700810 return OK;
811}
812
Jianing Weicb0652e2014-03-12 18:29:36 -0700813status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800814 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800815
Emilian Peevaebbe412018-01-15 13:53:24 +0000816 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000818 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700819
Emilian Peevaebbe412018-01-15 13:53:24 +0000820 return captureList(requestsList, surfaceMaps, /*lastFrameNumber*/NULL);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700821}
822
Emilian Peevaebbe412018-01-15 13:53:24 +0000823void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700824 std::list<const SurfaceMap>& surfaceMaps,
825 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000826 PhysicalCameraSettingsList requestList;
827 requestList.push_back({std::string(getId().string()), request});
828 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700829
830 SurfaceMap surfaceMap;
831 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
832 // With no surface list passed in, stream and surface will have 1-to-1
833 // mapping. So the surface index is 0 for each stream in the surfaceMap.
834 for (size_t i = 0; i < streams.count; i++) {
835 surfaceMap[streams.data.i32[i]].push_back(0);
836 }
837 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800838}
839
Jianing Wei90e59c92014-03-12 18:29:36 -0700840status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000841 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700842 const std::list<const SurfaceMap> &surfaceMaps,
843 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700844 /*out*/
845 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700846 ATRACE_CALL();
847 Mutex::Autolock il(mInterfaceLock);
848 Mutex::Autolock l(mLock);
849
850 status_t res = checkStatusOkToCaptureLocked();
851 if (res != OK) {
852 // error logged by previous call
853 return res;
854 }
855
856 RequestList requestList;
857
Shuzhen Wang0129d522016-10-30 22:43:41 -0700858 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
859 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700860 if (res != OK) {
861 // error logged by previous call
862 return res;
863 }
864
865 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700866 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700867 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700868 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700869 }
870
871 if (res == OK) {
872 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
873 if (res != OK) {
874 SET_ERR_L("Can't transition to active in %f seconds!",
875 kActiveTimeout/1e9);
876 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800877 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700878 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700879 } else {
880 CLOGE("Cannot queue request. Impossible.");
881 return BAD_VALUE;
882 }
883
884 return res;
885}
886
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800887hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800888 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800889 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700890 // Ideally we should grab mLock, but that can lead to deadlock, and
891 // it's not super important to get up to date value of mStatus for this
892 // warning print, hence skipping the lock here
893 if (mStatus == STATUS_ERROR) {
894 // Per API contract, HAL should act as closed after device error
895 // But mStatus can be set to error by framework as well, so just log
896 // a warning here.
897 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700898 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700899
900 if (mProcessCaptureResultLock.tryLock() != OK) {
901 // This should never happen; it indicates a wrong client implementation
902 // that doesn't follow the contract. But, we can be tolerant here.
903 ALOGE("%s: callback overlapped! waiting 1s...",
904 __FUNCTION__);
905 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
906 ALOGE("%s: cannot acquire lock in 1s, dropping results",
907 __FUNCTION__);
908 // really don't know what to do, so bail out.
909 return hardware::Void();
910 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800911 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700912 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800913 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -0700914 }
915 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800916 return hardware::Void();
917}
918
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800919// Only one processCaptureResult should be called at a time, so
920// the locks won't block. The locks are present here simply to enforce this.
921hardware::Return<void> Camera3Device::processCaptureResult(
922 const hardware::hidl_vec<
923 hardware::camera::device::V3_2::CaptureResult>& results) {
924 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
925
926 // Ideally we should grab mLock, but that can lead to deadlock, and
927 // it's not super important to get up to date value of mStatus for this
928 // warning print, hence skipping the lock here
929 if (mStatus == STATUS_ERROR) {
930 // Per API contract, HAL should act as closed after device error
931 // But mStatus can be set to error by framework as well, so just log
932 // a warning here.
933 ALOGW("%s: received capture result in error state.", __FUNCTION__);
934 }
935
936 if (mProcessCaptureResultLock.tryLock() != OK) {
937 // This should never happen; it indicates a wrong client implementation
938 // that doesn't follow the contract. But, we can be tolerant here.
939 ALOGE("%s: callback overlapped! waiting 1s...",
940 __FUNCTION__);
941 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
942 ALOGE("%s: cannot acquire lock in 1s, dropping results",
943 __FUNCTION__);
944 // really don't know what to do, so bail out.
945 return hardware::Void();
946 }
947 }
948 for (const auto& result : results) {
949 processOneCaptureResultLocked(result, noPhysMetadata);
950 }
951 mProcessCaptureResultLock.unlock();
952 return hardware::Void();
953}
954
955status_t Camera3Device::readOneCameraMetadataLocked(
956 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
957 const hardware::camera::device::V3_2::CameraMetadata& result) {
958 if (fmqResultSize > 0) {
959 resultMetadata.resize(fmqResultSize);
960 if (mResultMetadataQueue == nullptr) {
961 return NO_MEMORY; // logged in initialize()
962 }
963 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
964 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
965 __FUNCTION__, fmqResultSize);
966 return INVALID_OPERATION;
967 }
968 } else {
969 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
970 result.size());
971 }
972
973 if (resultMetadata.size() != 0) {
974 status_t res;
975 const camera_metadata_t* metadata =
976 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
977 size_t expected_metadata_size = resultMetadata.size();
978 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
979 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
980 __FUNCTION__, strerror(-res), res);
981 return INVALID_OPERATION;
982 }
983 }
984
985 return OK;
986}
987
Yifan Honga640c5a2017-04-12 16:30:31 -0700988void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800989 const hardware::camera::device::V3_2::CaptureResult& result,
990 const hardware::hidl_vec<
991 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadatas) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800992 camera3_capture_result r;
993 status_t res;
994 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700995
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800996 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -0700997 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800998 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
999 if (res != OK) {
1000 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1001 __FUNCTION__, result.frameNumber);
1002 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001003 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001004 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001005
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001006 // Read and validate physical camera metadata
1007 size_t physResultCount = physicalCameraMetadatas.size();
1008 std::vector<const char*> physCamIds(physResultCount);
1009 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1010 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1011 physResultMetadata.resize(physResultCount);
1012 for (size_t i = 0; i < physicalCameraMetadatas.size(); i++) {
1013 res = readOneCameraMetadataLocked(physicalCameraMetadatas[i].fmqMetadataSize,
1014 physResultMetadata[i], physicalCameraMetadatas[i].metadata);
1015 if (res != OK) {
1016 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1017 __FUNCTION__, result.frameNumber,
1018 physicalCameraMetadatas[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001019 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001020 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001021 physCamIds[i] = physicalCameraMetadatas[i].physicalCameraId.c_str();
1022 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1023 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001024 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001025 r.num_physcam_metadata = physResultCount;
1026 r.physcam_ids = physCamIds.data();
1027 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001028
1029 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1030 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1031 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1032 auto& bDst = outputBuffers[i];
1033 const StreamBuffer &bSrc = result.outputBuffers[i];
1034
1035 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001036 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001037 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1038 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001039 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001040 }
1041 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
1042
1043 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08001044 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001045 if (res != OK) {
1046 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1047 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001048 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001049 }
1050 bDst.buffer = buffer;
1051 bDst.status = mapHidlBufferStatus(bSrc.status);
1052 bDst.acquire_fence = -1;
1053 if (bSrc.releaseFence == nullptr) {
1054 bDst.release_fence = -1;
1055 } else if (bSrc.releaseFence->numFds == 1) {
1056 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1057 } else {
1058 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1059 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001060 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001061 }
1062 }
1063 r.num_output_buffers = outputBuffers.size();
1064 r.output_buffers = outputBuffers.data();
1065
1066 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001067 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001068 r.input_buffer = nullptr;
1069 } else {
1070 if (mInputStream->getId() != result.inputBuffer.streamId) {
1071 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1072 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001073 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001074 }
1075 inputBuffer.stream = mInputStream->asHalStream();
1076 buffer_handle_t *buffer;
1077 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1078 &buffer);
1079 if (res != OK) {
1080 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1081 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001082 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001083 }
1084 inputBuffer.buffer = buffer;
1085 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1086 inputBuffer.acquire_fence = -1;
1087 if (result.inputBuffer.releaseFence == nullptr) {
1088 inputBuffer.release_fence = -1;
1089 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1090 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1091 } else {
1092 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1093 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001094 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001095 }
1096 r.input_buffer = &inputBuffer;
1097 }
1098
1099 r.partial_result = result.partialResult;
1100
1101 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001102}
1103
1104hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001105 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001106 // Ideally we should grab mLock, but that can lead to deadlock, and
1107 // it's not super important to get up to date value of mStatus for this
1108 // warning print, hence skipping the lock here
1109 if (mStatus == STATUS_ERROR) {
1110 // Per API contract, HAL should act as closed after device error
1111 // But mStatus can be set to error by framework as well, so just log
1112 // a warning here.
1113 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001114 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001115
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001116 for (const auto& msg : msgs) {
1117 notify(msg);
1118 }
1119 return hardware::Void();
1120}
1121
1122void Camera3Device::notify(
1123 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1124
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001125 camera3_notify_msg m;
1126 switch (msg.type) {
1127 case MsgType::ERROR:
1128 m.type = CAMERA3_MSG_ERROR;
1129 m.message.error.frame_number = msg.msg.error.frameNumber;
1130 if (msg.msg.error.errorStreamId >= 0) {
1131 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001132 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001133 ALOGE("%s: Frame %d: Invalid error stream id %d",
1134 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001135 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001136 }
1137 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1138 } else {
1139 m.message.error.error_stream = nullptr;
1140 }
1141 switch (msg.msg.error.errorCode) {
1142 case ErrorCode::ERROR_DEVICE:
1143 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1144 break;
1145 case ErrorCode::ERROR_REQUEST:
1146 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1147 break;
1148 case ErrorCode::ERROR_RESULT:
1149 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1150 break;
1151 case ErrorCode::ERROR_BUFFER:
1152 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1153 break;
1154 }
1155 break;
1156 case MsgType::SHUTTER:
1157 m.type = CAMERA3_MSG_SHUTTER;
1158 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1159 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1160 break;
1161 }
1162 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001163}
1164
Emilian Peevaebbe412018-01-15 13:53:24 +00001165status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001166 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001167 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001168 ATRACE_CALL();
1169
Emilian Peevaebbe412018-01-15 13:53:24 +00001170 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001171}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001172
Jianing Weicb0652e2014-03-12 18:29:36 -07001173status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1174 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001175 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176
Emilian Peevaebbe412018-01-15 13:53:24 +00001177 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001178 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001179 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001180
Emilian Peevaebbe412018-01-15 13:53:24 +00001181 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001182 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001183}
1184
Emilian Peevaebbe412018-01-15 13:53:24 +00001185status_t Camera3Device::setStreamingRequestList(
1186 const List<const PhysicalCameraSettingsList> &requestsList,
1187 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001188 ATRACE_CALL();
1189
Emilian Peevaebbe412018-01-15 13:53:24 +00001190 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001191}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192
1193sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001194 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 status_t res;
1196
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001197 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001198 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1199 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001200 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1201 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001202 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001204 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001206 } else if (mStatus == STATUS_UNCONFIGURED) {
1207 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 CLOGE("No streams configured");
1209 return NULL;
1210 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 }
1212
Shuzhen Wang0129d522016-10-30 22:43:41 -07001213 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001215}
1216
Jianing Weicb0652e2014-03-12 18:29:36 -07001217status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001218 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001219 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001220 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 switch (mStatus) {
1223 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001224 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 return INVALID_OPERATION;
1226 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001227 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001229 case STATUS_UNCONFIGURED:
1230 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001231 case STATUS_ACTIVE:
1232 // OK
1233 break;
1234 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001235 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 return INVALID_OPERATION;
1237 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001238 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001239
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001240 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001241}
1242
1243status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1244 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001247 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001248}
1249
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001250status_t Camera3Device::createInputStream(
1251 uint32_t width, uint32_t height, int format, int *id) {
1252 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001253 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001254 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001255 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001256 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1257 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001258
1259 status_t res;
1260 bool wasActive = false;
1261
1262 switch (mStatus) {
1263 case STATUS_ERROR:
1264 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1265 return INVALID_OPERATION;
1266 case STATUS_UNINITIALIZED:
1267 ALOGE("%s: Device not initialized", __FUNCTION__);
1268 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001269 case STATUS_UNCONFIGURED:
1270 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001271 // OK
1272 break;
1273 case STATUS_ACTIVE:
1274 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001275 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001276 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001277 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001278 return res;
1279 }
1280 wasActive = true;
1281 break;
1282 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001284 return INVALID_OPERATION;
1285 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001286 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001287
1288 if (mInputStream != 0) {
1289 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1290 return INVALID_OPERATION;
1291 }
1292
1293 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1294 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001295 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001296
1297 mInputStream = newStream;
1298
1299 *id = mNextStreamId++;
1300
1301 // Continue captures if active at start
1302 if (wasActive) {
1303 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001304 // Reuse current operating mode and session parameters for new stream config
1305 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001306 if (res != OK) {
1307 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1308 __FUNCTION__, mNextStreamId, strerror(-res), res);
1309 return res;
1310 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001311 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001312 }
1313
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001314 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001315 return OK;
1316}
1317
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001318status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001319 uint32_t width, uint32_t height, int format,
1320 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001321 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001322 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001323 ATRACE_CALL();
1324
1325 if (consumer == nullptr) {
1326 ALOGE("%s: consumer must not be null", __FUNCTION__);
1327 return BAD_VALUE;
1328 }
1329
1330 std::vector<sp<Surface>> consumers;
1331 consumers.push_back(consumer);
1332
1333 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001334 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1335 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001336}
1337
1338status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1339 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1340 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001341 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001342 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001343 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001344
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001345 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001346 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001348 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001349 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1350 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1351 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001353 status_t res;
1354 bool wasActive = false;
1355
1356 switch (mStatus) {
1357 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001358 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001359 return INVALID_OPERATION;
1360 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001361 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001362 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 case STATUS_UNCONFIGURED:
1364 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001365 // OK
1366 break;
1367 case STATUS_ACTIVE:
1368 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001369 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001371 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001372 return res;
1373 }
1374 wasActive = true;
1375 break;
1376 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001377 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001378 return INVALID_OPERATION;
1379 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001380 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381
1382 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001383
Shuzhen Wang0129d522016-10-30 22:43:41 -07001384 if (consumers.size() == 0 && !hasDeferredConsumer) {
1385 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1386 return BAD_VALUE;
1387 }
Zhijun He5d677d12016-05-29 16:52:39 -07001388
Shuzhen Wang0129d522016-10-30 22:43:41 -07001389 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001390 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1391 return BAD_VALUE;
1392 }
1393
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001395 ssize_t blobBufferSize;
1396 if (dataSpace != HAL_DATASPACE_DEPTH) {
1397 blobBufferSize = getJpegBufferSize(width, height);
1398 if (blobBufferSize <= 0) {
1399 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1400 return BAD_VALUE;
1401 }
1402 } else {
1403 blobBufferSize = getPointCloudBufferSize();
1404 if (blobBufferSize <= 0) {
1405 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1406 return BAD_VALUE;
1407 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001408 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001409 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001410 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001411 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001412 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1413 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1414 if (rawOpaqueBufferSize <= 0) {
1415 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1416 return BAD_VALUE;
1417 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001418 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001419 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001420 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001421 } else if (isShared) {
1422 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1423 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001424 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001425 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001426 newStream = new Camera3OutputStream(mNextStreamId,
1427 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001428 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001430 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001431 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001432 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433 }
Emilian Peev40ead602017-09-26 15:46:36 +01001434
1435 size_t consumerCount = consumers.size();
1436 for (size_t i = 0; i < consumerCount; i++) {
1437 int id = newStream->getSurfaceId(consumers[i]);
1438 if (id < 0) {
1439 SET_ERR_L("Invalid surface id");
1440 return BAD_VALUE;
1441 }
1442 if (surfaceIds != nullptr) {
1443 surfaceIds->push_back(id);
1444 }
1445 }
1446
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001447 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448
Emilian Peev08dd2452017-04-06 16:55:14 +01001449 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001450
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001451 res = mOutputStreams.add(mNextStreamId, newStream);
1452 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001453 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001454 return res;
1455 }
1456
1457 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001458 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459
1460 // Continue captures if active at start
1461 if (wasActive) {
1462 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001463 // Reuse current operating mode and session parameters for new stream config
1464 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001466 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1467 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001468 return res;
1469 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001472 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001474}
1475
Emilian Peev710c1422017-08-30 11:19:38 +01001476status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001477 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001478 if (nullptr == streamInfo) {
1479 return BAD_VALUE;
1480 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001481 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001483
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484 switch (mStatus) {
1485 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001486 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001487 return INVALID_OPERATION;
1488 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001489 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001490 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001491 case STATUS_UNCONFIGURED:
1492 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001493 case STATUS_ACTIVE:
1494 // OK
1495 break;
1496 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001497 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498 return INVALID_OPERATION;
1499 }
1500
1501 ssize_t idx = mOutputStreams.indexOfKey(id);
1502 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001503 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504 return idx;
1505 }
1506
Emilian Peev710c1422017-08-30 11:19:38 +01001507 streamInfo->width = mOutputStreams[idx]->getWidth();
1508 streamInfo->height = mOutputStreams[idx]->getHeight();
1509 streamInfo->format = mOutputStreams[idx]->getFormat();
1510 streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace();
1511 streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden();
1512 streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat();
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07001513 streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden();
1514 streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001515 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516}
1517
1518status_t Camera3Device::setStreamTransform(int id,
1519 int transform) {
1520 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001521 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001524 switch (mStatus) {
1525 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001526 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 return INVALID_OPERATION;
1528 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001529 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001530 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001531 case STATUS_UNCONFIGURED:
1532 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533 case STATUS_ACTIVE:
1534 // OK
1535 break;
1536 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001537 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538 return INVALID_OPERATION;
1539 }
1540
1541 ssize_t idx = mOutputStreams.indexOfKey(id);
1542 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001543 CLOGE("Stream %d does not exist",
1544 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545 return BAD_VALUE;
1546 }
1547
1548 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001549}
1550
1551status_t Camera3Device::deleteStream(int id) {
1552 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001553 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001554 Mutex::Autolock l(mLock);
1555 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001556
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001557 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 // CameraDevice semantics require device to already be idle before
1560 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001561 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001562 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001563 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001564 }
1565
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001566 if (mStatus == STATUS_ERROR) {
1567 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1568 __FUNCTION__, mId.string());
1569 return -EBUSY;
1570 }
1571
Igor Murashkin2fba5842013-04-22 14:03:54 -07001572 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001573 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001574 if (mInputStream != NULL && id == mInputStream->getId()) {
1575 deletedStream = mInputStream;
1576 mInputStream.clear();
1577 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001578 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001579 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001580 return BAD_VALUE;
1581 }
Zhijun He5f446352014-01-22 09:49:33 -08001582 }
1583
1584 // Delete output stream or the output part of a bi-directional stream.
1585 if (outputStreamIdx != NAME_NOT_FOUND) {
1586 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001587 mOutputStreams.removeItem(id);
1588 }
1589
1590 // Free up the stream endpoint so that it can be used by some other stream
1591 res = deletedStream->disconnect();
1592 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001593 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001594 // fall through since we want to still list the stream as deleted.
1595 }
1596 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001597 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598
1599 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001600}
1601
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001602status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001603 ATRACE_CALL();
1604 ALOGV("%s: E", __FUNCTION__);
1605
1606 Mutex::Autolock il(mInterfaceLock);
1607 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001608
Emilian Peev811d2952018-05-25 11:08:40 +01001609 // In case the client doesn't include any session parameter, try a
1610 // speculative configuration using the values from the last cached
1611 // default request.
1612 if (sessionParams.isEmpty() &&
1613 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
1614 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1615 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1616 mLastTemplateId);
1617 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1618 operatingMode);
1619 }
1620
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001621 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1622}
1623
1624status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1625 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001626 //Filter out any incoming session parameters
1627 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001628 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1629 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001630 CameraMetadata filteredParams(availableSessionKeys.count);
1631 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1632 filteredParams.getAndLock());
1633 set_camera_metadata_vendor_id(meta, mVendorTagId);
1634 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001635 if (availableSessionKeys.count > 0) {
1636 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1637 camera_metadata_ro_entry entry = params.find(
1638 availableSessionKeys.data.i32[i]);
1639 if (entry.count > 0) {
1640 filteredParams.update(entry);
1641 }
1642 }
1643 }
1644
1645 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001646}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001647
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001648status_t Camera3Device::getInputBufferProducer(
1649 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001650 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001651 Mutex::Autolock il(mInterfaceLock);
1652 Mutex::Autolock l(mLock);
1653
1654 if (producer == NULL) {
1655 return BAD_VALUE;
1656 } else if (mInputStream == NULL) {
1657 return INVALID_OPERATION;
1658 }
1659
1660 return mInputStream->getInputBufferProducer(producer);
1661}
1662
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001663status_t Camera3Device::createDefaultRequest(int templateId,
1664 CameraMetadata *request) {
1665 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001666 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001667
1668 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1669 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1670 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1671 return BAD_VALUE;
1672 }
1673
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001674 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001675
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001676 {
1677 Mutex::Autolock l(mLock);
1678 switch (mStatus) {
1679 case STATUS_ERROR:
1680 CLOGE("Device has encountered a serious error");
1681 return INVALID_OPERATION;
1682 case STATUS_UNINITIALIZED:
1683 CLOGE("Device is not initialized!");
1684 return INVALID_OPERATION;
1685 case STATUS_UNCONFIGURED:
1686 case STATUS_CONFIGURED:
1687 case STATUS_ACTIVE:
1688 // OK
1689 break;
1690 default:
1691 SET_ERR_L("Unexpected status: %d", mStatus);
1692 return INVALID_OPERATION;
1693 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001694
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001695 if (!mRequestTemplateCache[templateId].isEmpty()) {
1696 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001697 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001698 return OK;
1699 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001700 }
1701
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001702 camera_metadata_t *rawRequest;
1703 status_t res = mInterface->constructDefaultRequestSettings(
1704 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001705
1706 {
1707 Mutex::Autolock l(mLock);
1708 if (res == BAD_VALUE) {
1709 ALOGI("%s: template %d is not supported on this camera device",
1710 __FUNCTION__, templateId);
1711 return res;
1712 } else if (res != OK) {
1713 CLOGE("Unable to construct request template %d: %s (%d)",
1714 templateId, strerror(-res), res);
1715 return res;
1716 }
1717
1718 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1719 mRequestTemplateCache[templateId].acquire(rawRequest);
1720
1721 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001722 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001723 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001724 return OK;
1725}
1726
1727status_t Camera3Device::waitUntilDrained() {
1728 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001729 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001730 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001731 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001732
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001733 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001734}
1735
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001736status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737 switch (mStatus) {
1738 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001739 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001740 ALOGV("%s: Already idle", __FUNCTION__);
1741 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742 case STATUS_CONFIGURED:
1743 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001744 case STATUS_ERROR:
1745 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001746 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001747 break;
1748 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001749 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001750 return INVALID_OPERATION;
1751 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001752 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1753 maxExpectedDuration);
1754 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001755 if (res != OK) {
1756 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1757 res);
1758 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001759 return res;
1760}
1761
Ruben Brunk183f0562015-08-12 12:55:02 -07001762
1763void Camera3Device::internalUpdateStatusLocked(Status status) {
1764 mStatus = status;
1765 mRecentStatusUpdates.add(mStatus);
1766 mStatusChanged.broadcast();
1767}
1768
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001769void Camera3Device::pauseStateNotify(bool enable) {
1770 Mutex::Autolock il(mInterfaceLock);
1771 Mutex::Autolock l(mLock);
1772
1773 mPauseStateNotify = enable;
1774}
1775
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001776// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001777status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001778 mRequestThread->setPaused(true);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001779
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001780 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1781 maxExpectedDuration);
1782 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001783 if (res != OK) {
1784 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001785 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001786 }
1787
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001788 return res;
1789}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001791// Resume after internalPauseAndWaitLocked
1792status_t Camera3Device::internalResumeLocked() {
1793 status_t res;
1794
1795 mRequestThread->setPaused(false);
1796
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001797 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1798 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001799 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1800 if (res != OK) {
1801 SET_ERR_L("Can't transition to active in %f seconds!",
1802 kActiveTimeout/1e9);
1803 }
1804 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001805 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001806}
1807
Ruben Brunk183f0562015-08-12 12:55:02 -07001808status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001809 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001810
1811 size_t startIndex = 0;
1812 if (mStatusWaiters == 0) {
1813 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1814 // this status list
1815 mRecentStatusUpdates.clear();
1816 } else {
1817 // If other threads are waiting on updates to this status list, set the position of the
1818 // first element that this list will check rather than clearing the list.
1819 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001820 }
1821
Ruben Brunk183f0562015-08-12 12:55:02 -07001822 mStatusWaiters++;
1823
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001824 bool stateSeen = false;
1825 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001826 if (active == (mStatus == STATUS_ACTIVE)) {
1827 // Desired state is current
1828 break;
1829 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001830
1831 res = mStatusChanged.waitRelative(mLock, timeout);
1832 if (res != OK) break;
1833
Ruben Brunk183f0562015-08-12 12:55:02 -07001834 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1835 // transitions.
1836 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1837 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1838 __FUNCTION__);
1839
1840 // Encountered desired state since we began waiting
1841 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001842 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1843 stateSeen = true;
1844 break;
1845 }
1846 }
1847 } while (!stateSeen);
1848
Ruben Brunk183f0562015-08-12 12:55:02 -07001849 mStatusWaiters--;
1850
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001851 return res;
1852}
1853
1854
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001855status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001856 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001857 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001858
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001859 if (listener != NULL && mListener != NULL) {
1860 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1861 }
1862 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001863 mRequestThread->setNotificationListener(listener);
1864 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001865
1866 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001867}
1868
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001869bool Camera3Device::willNotify3A() {
1870 return false;
1871}
1872
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001873status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001874 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001875 status_t res;
1876 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001877
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001878 while (mResultQueue.empty()) {
1879 res = mResultSignal.waitRelative(mOutputLock, timeout);
1880 if (res == TIMED_OUT) {
1881 return res;
1882 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001883 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1884 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001885 return res;
1886 }
1887 }
1888 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001889}
1890
Jianing Weicb0652e2014-03-12 18:29:36 -07001891status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001892 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001893 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001894
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001895 if (mResultQueue.empty()) {
1896 return NOT_ENOUGH_DATA;
1897 }
1898
Jianing Weicb0652e2014-03-12 18:29:36 -07001899 if (frame == NULL) {
1900 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1901 return BAD_VALUE;
1902 }
1903
1904 CaptureResult &result = *(mResultQueue.begin());
1905 frame->mResultExtras = result.mResultExtras;
1906 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001907 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001908 mResultQueue.erase(mResultQueue.begin());
1909
1910 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001911}
1912
1913status_t Camera3Device::triggerAutofocus(uint32_t id) {
1914 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001915 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001916
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001917 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1918 // Mix-in this trigger into the next request and only the next request.
1919 RequestTrigger trigger[] = {
1920 {
1921 ANDROID_CONTROL_AF_TRIGGER,
1922 ANDROID_CONTROL_AF_TRIGGER_START
1923 },
1924 {
1925 ANDROID_CONTROL_AF_TRIGGER_ID,
1926 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001927 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001928 };
1929
1930 return mRequestThread->queueTrigger(trigger,
1931 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001932}
1933
1934status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1935 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001936 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001937
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001938 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1939 // Mix-in this trigger into the next request and only the next request.
1940 RequestTrigger trigger[] = {
1941 {
1942 ANDROID_CONTROL_AF_TRIGGER,
1943 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1944 },
1945 {
1946 ANDROID_CONTROL_AF_TRIGGER_ID,
1947 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001948 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001949 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001950
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001951 return mRequestThread->queueTrigger(trigger,
1952 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001953}
1954
1955status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1956 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001957 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001958
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001959 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1960 // Mix-in this trigger into the next request and only the next request.
1961 RequestTrigger trigger[] = {
1962 {
1963 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1964 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1965 },
1966 {
1967 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1968 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001969 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001970 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001971
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001972 return mRequestThread->queueTrigger(trigger,
1973 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001974}
1975
Jianing Weicb0652e2014-03-12 18:29:36 -07001976status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001977 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001978 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001979 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001980
Zhijun He7ef20392014-04-21 16:04:17 -07001981 {
1982 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001983 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001984 }
1985
Emilian Peev08dd2452017-04-06 16:55:14 +01001986 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001987}
1988
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001989status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001990 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1991}
1992
1993status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001994 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001995 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001996 Mutex::Autolock il(mInterfaceLock);
1997 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001998
1999 sp<Camera3StreamInterface> stream;
2000 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2001 if (outputStreamIdx == NAME_NOT_FOUND) {
2002 CLOGE("Stream %d does not exist", streamId);
2003 return BAD_VALUE;
2004 }
2005
2006 stream = mOutputStreams.editValueAt(outputStreamIdx);
2007
2008 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002009 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002010 return BAD_VALUE;
2011 }
2012
2013 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002014 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002015 return BAD_VALUE;
2016 }
2017
Ruben Brunkc78ac262015-08-13 17:58:46 -07002018 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002019}
2020
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002021status_t Camera3Device::tearDown(int streamId) {
2022 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002023 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002024 Mutex::Autolock il(mInterfaceLock);
2025 Mutex::Autolock l(mLock);
2026
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002027 sp<Camera3StreamInterface> stream;
2028 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2029 if (outputStreamIdx == NAME_NOT_FOUND) {
2030 CLOGE("Stream %d does not exist", streamId);
2031 return BAD_VALUE;
2032 }
2033
2034 stream = mOutputStreams.editValueAt(outputStreamIdx);
2035
2036 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2037 CLOGE("Stream %d is a target of a in-progress request", streamId);
2038 return BAD_VALUE;
2039 }
2040
2041 return stream->tearDown();
2042}
2043
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002044status_t Camera3Device::addBufferListenerForStream(int streamId,
2045 wp<Camera3StreamBufferListener> listener) {
2046 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002047 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002048 Mutex::Autolock il(mInterfaceLock);
2049 Mutex::Autolock l(mLock);
2050
2051 sp<Camera3StreamInterface> stream;
2052 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2053 if (outputStreamIdx == NAME_NOT_FOUND) {
2054 CLOGE("Stream %d does not exist", streamId);
2055 return BAD_VALUE;
2056 }
2057
2058 stream = mOutputStreams.editValueAt(outputStreamIdx);
2059 stream->addBufferListener(listener);
2060
2061 return OK;
2062}
2063
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002065 * Methods called by subclasses
2066 */
2067
2068void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002069 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002070 {
2071 // Need mLock to safely update state and synchronize to current
2072 // state of methods in flight.
2073 Mutex::Autolock l(mLock);
2074 // We can get various system-idle notices from the status tracker
2075 // while starting up. Only care about them if we've actually sent
2076 // in some requests recently.
2077 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2078 return;
2079 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002080 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2081 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002082 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002083
2084 // Skip notifying listener if we're doing some user-transparent
2085 // state changes
2086 if (mPauseStateNotify) return;
2087 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002088
2089 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002090 {
2091 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002092 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002093 }
2094 if (idle && listener != NULL) {
2095 listener->notifyIdle();
2096 }
2097}
2098
Shuzhen Wang758c2152017-01-10 18:26:18 -08002099status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002100 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002101 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002102 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2103 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002104
2105 if (surfaceIds == nullptr) {
2106 return BAD_VALUE;
2107 }
2108
Zhijun He5d677d12016-05-29 16:52:39 -07002109 Mutex::Autolock il(mInterfaceLock);
2110 Mutex::Autolock l(mLock);
2111
Shuzhen Wang758c2152017-01-10 18:26:18 -08002112 if (consumers.size() == 0) {
2113 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002114 return BAD_VALUE;
2115 }
2116
2117 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2118 if (idx == NAME_NOT_FOUND) {
2119 CLOGE("Stream %d is unknown", streamId);
2120 return idx;
2121 }
2122 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002123 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002124 if (res != OK) {
2125 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2126 return res;
2127 }
2128
Emilian Peev40ead602017-09-26 15:46:36 +01002129 for (auto &consumer : consumers) {
2130 int id = stream->getSurfaceId(consumer);
2131 if (id < 0) {
2132 CLOGE("Invalid surface id!");
2133 return BAD_VALUE;
2134 }
2135 surfaceIds->push_back(id);
2136 }
2137
Shuzhen Wang0129d522016-10-30 22:43:41 -07002138 if (stream->isConsumerConfigurationDeferred()) {
2139 if (!stream->isConfiguring()) {
2140 CLOGE("Stream %d was already fully configured.", streamId);
2141 return INVALID_OPERATION;
2142 }
Zhijun He5d677d12016-05-29 16:52:39 -07002143
Shuzhen Wang0129d522016-10-30 22:43:41 -07002144 res = stream->finishConfiguration();
2145 if (res != OK) {
2146 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2147 stream->getId(), strerror(-res), res);
2148 return res;
2149 }
Zhijun He5d677d12016-05-29 16:52:39 -07002150 }
2151
2152 return OK;
2153}
2154
Emilian Peev40ead602017-09-26 15:46:36 +01002155status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2156 const std::vector<OutputStreamInfo> &outputInfo,
2157 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2158 Mutex::Autolock il(mInterfaceLock);
2159 Mutex::Autolock l(mLock);
2160
2161 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2162 if (idx == NAME_NOT_FOUND) {
2163 CLOGE("Stream %d is unknown", streamId);
2164 return idx;
2165 }
2166
2167 for (const auto &it : removedSurfaceIds) {
2168 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2169 CLOGE("Shared surface still part of a pending request!");
2170 return -EBUSY;
2171 }
2172 }
2173
2174 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
2175 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2176 if (res != OK) {
2177 CLOGE("Stream %d failed to update stream (error %d %s) ",
2178 streamId, res, strerror(-res));
2179 if (res == UNKNOWN_ERROR) {
2180 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2181 __FUNCTION__);
2182 }
2183 return res;
2184 }
2185
2186 return res;
2187}
2188
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002189status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2190 Mutex::Autolock il(mInterfaceLock);
2191 Mutex::Autolock l(mLock);
2192
2193 int idx = mOutputStreams.indexOfKey(streamId);
2194 if (idx == NAME_NOT_FOUND) {
2195 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2196 return BAD_VALUE;
2197 }
2198
2199 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx);
2200 return stream->dropBuffers(dropping);
2201}
2202
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002203/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002204 * Camera3Device private methods
2205 */
2206
2207sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002208 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002209 ATRACE_CALL();
2210 status_t res;
2211
2212 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002213 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002214
2215 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002216 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002217 if (inputStreams.count > 0) {
2218 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002219 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002220 CLOGE("Request references unknown input stream %d",
2221 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002222 return NULL;
2223 }
2224 // Lazy completion of stream configuration (allocation/registration)
2225 // on first use
2226 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002227 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002228 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002229 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002230 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002231 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002232 return NULL;
2233 }
2234 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002235 // Check if stream is being prepared
2236 if (mInputStream->isPreparing()) {
2237 CLOGE("Request references an input stream that's being prepared!");
2238 return NULL;
2239 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002240
2241 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002242 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002243 }
2244
2245 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002246 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002247 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002248 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002249 return NULL;
2250 }
2251
2252 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002253 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002254 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002255 CLOGE("Request references unknown stream %d",
2256 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002257 return NULL;
2258 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002259 sp<Camera3OutputStreamInterface> stream =
2260 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002261
Zhijun He5d677d12016-05-29 16:52:39 -07002262 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002263 auto iter = surfaceMap.find(streams.data.i32[i]);
2264 if (iter != surfaceMap.end()) {
2265 const std::vector<size_t>& surfaces = iter->second;
2266 for (const auto& surface : surfaces) {
2267 if (stream->isConsumerConfigurationDeferred(surface)) {
2268 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2269 "due to deferred consumer", stream->getId(), surface);
2270 return NULL;
2271 }
2272 }
2273 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002274 }
2275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002276 // Lazy completion of stream configuration (allocation/registration)
2277 // on first use
2278 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002279 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002280 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002281 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2282 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002283 return NULL;
2284 }
2285 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002286 // Check if stream is being prepared
2287 if (stream->isPreparing()) {
2288 CLOGE("Request references an output stream that's being prepared!");
2289 return NULL;
2290 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002291
2292 newRequest->mOutputStreams.push(stream);
2293 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002294 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002295 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002296
2297 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002298}
2299
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002300bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2301 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2302 Size size = mSupportedOpaqueInputSizes[i];
2303 if (size.width == width && size.height == height) {
2304 return true;
2305 }
2306 }
2307
2308 return false;
2309}
2310
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002311void Camera3Device::cancelStreamsConfigurationLocked() {
2312 int res = OK;
2313 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2314 res = mInputStream->cancelConfiguration();
2315 if (res != OK) {
2316 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2317 mInputStream->getId(), strerror(-res), res);
2318 }
2319 }
2320
2321 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2322 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2323 if (outputStream->isConfiguring()) {
2324 res = outputStream->cancelConfiguration();
2325 if (res != OK) {
2326 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2327 outputStream->getId(), strerror(-res), res);
2328 }
2329 }
2330 }
2331
2332 // Return state to that at start of call, so that future configures
2333 // properly clean things up
2334 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2335 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002336
2337 res = mPreparerThread->resume();
2338 if (res != OK) {
2339 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2340 }
2341}
2342
2343bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2344 ATRACE_CALL();
2345 bool ret = false;
2346
2347 Mutex::Autolock il(mInterfaceLock);
2348 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2349
2350 Mutex::Autolock l(mLock);
2351 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2352 if (rc == NO_ERROR) {
2353 mNeedConfig = true;
2354 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2355 if (rc == NO_ERROR) {
2356 ret = true;
2357 mPauseStateNotify = false;
2358 //Moving to active state while holding 'mLock' is important.
2359 //There could be pending calls to 'create-/deleteStream' which
2360 //will trigger another stream configuration while the already
2361 //present streams end up with outstanding buffers that will
2362 //not get drained.
2363 internalUpdateStatusLocked(STATUS_ACTIVE);
2364 } else {
2365 setErrorStateLocked("%s: Failed to re-configure camera: %d",
2366 __FUNCTION__, rc);
2367 }
2368 } else {
2369 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2370 }
2371
2372 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002373}
2374
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002375status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002376 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 ATRACE_CALL();
2378 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002379
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002380 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002381 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382 return INVALID_OPERATION;
2383 }
2384
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002385 if (operatingMode < 0) {
2386 CLOGE("Invalid operating mode: %d", operatingMode);
2387 return BAD_VALUE;
2388 }
2389
2390 bool isConstrainedHighSpeed =
2391 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2392 operatingMode;
2393
2394 if (mOperatingMode != operatingMode) {
2395 mNeedConfig = true;
2396 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2397 mOperatingMode = operatingMode;
2398 }
2399
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002400 if (!mNeedConfig) {
2401 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2402 return OK;
2403 }
2404
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002405 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2406 // adding a dummy stream instead.
2407 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2408 if (mOutputStreams.size() == 0) {
2409 addDummyStreamLocked();
2410 } else {
2411 tryRemoveDummyStreamLocked();
2412 }
2413
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002414 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002415 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002416
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002417 mPreparerThread->pause();
2418
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002419 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002420 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002421 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2422
2423 Vector<camera3_stream_t*> streams;
2424 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002425 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002426
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002427
2428 if (mInputStream != NULL) {
2429 camera3_stream_t *inputStream;
2430 inputStream = mInputStream->startConfiguration();
2431 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002432 CLOGE("Can't start input stream configuration");
2433 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002434 return INVALID_OPERATION;
2435 }
2436 streams.add(inputStream);
2437 }
2438
2439 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002440
2441 // Don't configure bidi streams twice, nor add them twice to the list
2442 if (mOutputStreams[i].get() ==
2443 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2444
2445 config.num_streams--;
2446 continue;
2447 }
2448
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002449 camera3_stream_t *outputStream;
2450 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2451 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002452 CLOGE("Can't start output stream configuration");
2453 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002454 return INVALID_OPERATION;
2455 }
2456 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002457
2458 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB &&
2459 outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
Emilian Peev192ee832018-01-31 14:46:47 +00002460 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2461 // always occupy the initial entry.
2462 bufferSizes[k] = static_cast<uint32_t>(
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002463 getJpegBufferSize(outputStream->width, outputStream->height));
2464 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002465 }
2466
2467 config.streams = streams.editArray();
2468
2469 // Do the HAL configuration; will potentially touch stream
2470 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002471
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002472 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002473 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002474 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002475
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002476 if (res == BAD_VALUE) {
2477 // HAL rejected this set of streams as unsupported, clean up config
2478 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002479 CLOGE("Set of requested inputs/outputs not supported by HAL");
2480 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002481 return BAD_VALUE;
2482 } else if (res != OK) {
2483 // Some other kind of error from configure_streams - this is not
2484 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002485 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2486 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487 return res;
2488 }
2489
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002490 // Finish all stream configuration immediately.
2491 // TODO: Try to relax this later back to lazy completion, which should be
2492 // faster
2493
Igor Murashkin073f8572013-05-02 14:59:28 -07002494 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002495 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002496 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002497 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002498 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002499 cancelStreamsConfigurationLocked();
2500 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002501 }
2502 }
2503
2504 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002505 sp<Camera3OutputStreamInterface> outputStream =
2506 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002507 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002508 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002509 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002510 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002511 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002512 cancelStreamsConfigurationLocked();
2513 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002514 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002515 }
2516 }
2517
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002518 // Request thread needs to know to avoid using repeat-last-settings protocol
2519 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002520 if (notifyRequestThread) {
2521 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2522 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002523
Zhijun He90f7c372016-08-16 16:19:43 -07002524 char value[PROPERTY_VALUE_MAX];
2525 property_get("camera.fifo.disable", value, "0");
2526 int32_t disableFifo = atoi(value);
2527 if (disableFifo != 1) {
2528 // Boost priority of request thread to SCHED_FIFO.
2529 pid_t requestThreadTid = mRequestThread->getTid();
2530 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002531 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002532 if (res != OK) {
2533 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2534 strerror(-res), res);
2535 } else {
2536 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2537 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002538 }
2539
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002540 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002541 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2542 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2543 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2544 sessionParams.unlock(newSessionParams);
2545 mSessionParams.unlock(currentSessionParams);
2546 if (updateSessionParams) {
2547 mSessionParams = sessionParams;
2548 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002549
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002550 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002551
Ruben Brunk183f0562015-08-12 12:55:02 -07002552 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2553 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002554
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002555 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002556
Zhijun He0a210512014-07-24 13:45:15 -07002557 // tear down the deleted streams after configure streams.
2558 mDeletedStreams.clear();
2559
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002560 auto rc = mPreparerThread->resume();
2561 if (rc != OK) {
2562 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2563 return rc;
2564 }
2565
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002566 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002567}
2568
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002569status_t Camera3Device::addDummyStreamLocked() {
2570 ATRACE_CALL();
2571 status_t res;
2572
2573 if (mDummyStreamId != NO_STREAM) {
2574 // Should never be adding a second dummy stream when one is already
2575 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002576 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2577 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002578 return INVALID_OPERATION;
2579 }
2580
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002581 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002582
2583 sp<Camera3OutputStreamInterface> dummyStream =
2584 new Camera3DummyStream(mNextStreamId);
2585
2586 res = mOutputStreams.add(mNextStreamId, dummyStream);
2587 if (res < 0) {
2588 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2589 return res;
2590 }
2591
2592 mDummyStreamId = mNextStreamId;
2593 mNextStreamId++;
2594
2595 return OK;
2596}
2597
2598status_t Camera3Device::tryRemoveDummyStreamLocked() {
2599 ATRACE_CALL();
2600 status_t res;
2601
2602 if (mDummyStreamId == NO_STREAM) return OK;
2603 if (mOutputStreams.size() == 1) return OK;
2604
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002605 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002606
2607 // Ok, have a dummy stream and there's at least one other output stream,
2608 // so remove the dummy
2609
2610 sp<Camera3StreamInterface> deletedStream;
2611 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2612 if (outputStreamIdx == NAME_NOT_FOUND) {
2613 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2614 return INVALID_OPERATION;
2615 }
2616
2617 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2618 mOutputStreams.removeItemsAt(outputStreamIdx);
2619
2620 // Free up the stream endpoint so that it can be used by some other stream
2621 res = deletedStream->disconnect();
2622 if (res != OK) {
2623 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2624 // fall through since we want to still list the stream as deleted.
2625 }
2626 mDeletedStreams.add(deletedStream);
2627 mDummyStreamId = NO_STREAM;
2628
2629 return res;
2630}
2631
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002632void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002633 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002634 Mutex::Autolock l(mLock);
2635 va_list args;
2636 va_start(args, fmt);
2637
2638 setErrorStateLockedV(fmt, args);
2639
2640 va_end(args);
2641}
2642
2643void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002644 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002645 Mutex::Autolock l(mLock);
2646 setErrorStateLockedV(fmt, args);
2647}
2648
2649void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2650 va_list args;
2651 va_start(args, fmt);
2652
2653 setErrorStateLockedV(fmt, args);
2654
2655 va_end(args);
2656}
2657
2658void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002659 // Print out all error messages to log
2660 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002661 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002662
2663 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002664 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002665
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002666 mErrorCause = errorCause;
2667
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002668 if (mRequestThread != nullptr) {
2669 mRequestThread->setPaused(true);
2670 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002671 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002672
2673 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002674 sp<NotificationListener> listener = mListener.promote();
2675 if (listener != NULL) {
2676 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002677 CaptureResultExtras());
2678 }
2679
2680 // Save stack trace. View by dumping it later.
2681 CameraTraces::saveTrace();
2682 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002683}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002684
2685/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002686 * In-flight request management
2687 */
2688
Jianing Weicb0652e2014-03-12 18:29:36 -07002689status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002690 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002691 bool hasAppCallback, nsecs_t maxExpectedDuration,
2692 std::set<String8>& physicalCameraIds) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002693 ATRACE_CALL();
2694 Mutex::Autolock l(mInFlightLock);
2695
2696 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002697 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002698 hasAppCallback, maxExpectedDuration, physicalCameraIds));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002699 if (res < 0) return res;
2700
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002701 if (mInFlightMap.size() == 1) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002702 // hold mLock to prevent race with disconnect
2703 Mutex::Autolock l(mLock);
2704 if (mStatusTracker != nullptr) {
2705 mStatusTracker->markComponentActive(mInFlightStatusId);
2706 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002707 }
2708
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002709 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002710 return OK;
2711}
2712
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002713void Camera3Device::returnOutputBuffers(
2714 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2715 nsecs_t timestamp) {
2716 for (size_t i = 0; i < numBuffers; i++)
2717 {
2718 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2719 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2720 // Note: stream may be deallocated at this point, if this buffer was
2721 // the last reference to it.
2722 if (res != OK) {
2723 ALOGE("Can't return buffer to its stream: %s (%d)",
2724 strerror(-res), res);
2725 }
2726 }
2727}
2728
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002729void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002730 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002731 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002732 mInFlightMap.removeItemsAt(idx, 1);
2733
2734 // Indicate idle inFlightMap to the status tracker
2735 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002736 // hold mLock to prevent race with disconnect
2737 Mutex::Autolock l(mLock);
2738 if (mStatusTracker != nullptr) {
2739 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2740 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002741 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002742 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002743}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002744
2745void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2746
2747 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2748 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2749
2750 nsecs_t sensorTimestamp = request.sensorTimestamp;
2751 nsecs_t shutterTimestamp = request.shutterTimestamp;
2752
2753 // Check if it's okay to remove the request from InFlightMap:
2754 // In the case of a successful request:
2755 // all input and output buffers, all result metadata, shutter callback
2756 // arrived.
2757 // In the case of a unsuccessful request:
2758 // all input and output buffers arrived.
2759 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07002760 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002761 (request.haveResultMetadata && shutterTimestamp != 0))) {
2762 ATRACE_ASYNC_END("frame capture", frameNumber);
2763
Shuzhen Wang403044a2017-02-26 23:29:04 -08002764 // Sanity check - if sensor timestamp matches shutter timestamp in the
2765 // case of request having callback.
2766 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002767 sensorTimestamp != shutterTimestamp) {
2768 SET_ERR("sensor timestamp (%" PRId64
2769 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2770 sensorTimestamp, frameNumber, shutterTimestamp);
2771 }
2772
2773 // for an unsuccessful request, it may have pending output buffers to
2774 // return.
2775 assert(request.requestStatus != OK ||
2776 request.pendingOutputBuffers.size() == 0);
2777 returnOutputBuffers(request.pendingOutputBuffers.array(),
2778 request.pendingOutputBuffers.size(), 0);
2779
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002780 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002781 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2782 }
2783
2784 // Sanity check - if we have too many in-flight frames, something has
2785 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002786 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002787 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002788 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2789 kInFlightWarnLimitHighSpeed) {
2790 CLOGE("In-flight list too large for high speed configuration: %zu",
2791 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002792 }
2793}
2794
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002795void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002796 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002797 { // First return buffers cached in mInFlightMap
2798 Mutex::Autolock l(mInFlightLock);
2799 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2800 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2801 returnOutputBuffers(request.pendingOutputBuffers.array(),
2802 request.pendingOutputBuffers.size(), 0);
2803 }
2804 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002805 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002806 }
2807
2808 // Then return all inflight buffers not returned by HAL
2809 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2810 mInterface->getInflightBufferKeys(&inflightKeys);
2811
2812 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2813 for (auto& pair : inflightKeys) {
2814 int32_t frameNumber = pair.first;
2815 int32_t streamId = pair.second;
2816 buffer_handle_t* buffer;
2817 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2818 if (res != OK) {
2819 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2820 __FUNCTION__, frameNumber, streamId);
2821 continue;
2822 }
2823
2824 camera3_stream_buffer_t streamBuffer;
2825 streamBuffer.buffer = buffer;
2826 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2827 streamBuffer.acquire_fence = -1;
2828 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002829
2830 // First check if the buffer belongs to deleted stream
2831 bool streamDeleted = false;
2832 for (auto& stream : mDeletedStreams) {
2833 if (streamId == stream->getId()) {
2834 streamDeleted = true;
2835 // Return buffer to deleted stream
2836 camera3_stream* halStream = stream->asHalStream();
2837 streamBuffer.stream = halStream;
2838 switch (halStream->stream_type) {
2839 case CAMERA3_STREAM_OUTPUT:
2840 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2841 if (res != OK) {
2842 ALOGE("%s: Can't return output buffer for frame %d to"
2843 " stream %d: %s (%d)", __FUNCTION__,
2844 frameNumber, streamId, strerror(-res), res);
2845 }
2846 break;
2847 case CAMERA3_STREAM_INPUT:
2848 res = stream->returnInputBuffer(streamBuffer);
2849 if (res != OK) {
2850 ALOGE("%s: Can't return input buffer for frame %d to"
2851 " stream %d: %s (%d)", __FUNCTION__,
2852 frameNumber, streamId, strerror(-res), res);
2853 }
2854 break;
2855 default: // Bi-direcitonal stream is deprecated
2856 ALOGE("%s: stream %d has unknown stream type %d",
2857 __FUNCTION__, streamId, halStream->stream_type);
2858 break;
2859 }
2860 break;
2861 }
2862 }
2863 if (streamDeleted) {
2864 continue;
2865 }
2866
2867 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002868 if (streamId == inputStreamId) {
2869 streamBuffer.stream = mInputStream->asHalStream();
2870 res = mInputStream->returnInputBuffer(streamBuffer);
2871 if (res != OK) {
2872 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002873 " stream %d: %s (%d)", __FUNCTION__,
2874 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002875 }
2876 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002877 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2878 if (idx == NAME_NOT_FOUND) {
2879 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2880 continue;
2881 }
2882 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002883 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2884 }
2885 }
2886}
2887
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002888void Camera3Device::insertResultLocked(CaptureResult *result,
2889 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002890 if (result == nullptr) return;
2891
Emilian Peev71c73a22017-03-21 16:35:51 +00002892 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2893 result->mMetadata.getAndLock());
2894 set_camera_metadata_vendor_id(meta, mVendorTagId);
2895 result->mMetadata.unlock(meta);
2896
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002897 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2898 (int32_t*)&frameNumber, 1) != OK) {
2899 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2900 return;
2901 }
2902
2903 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2904 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2905 return;
2906 }
2907
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002908 // Valid result, insert into queue
2909 List<CaptureResult>::iterator queuedResult =
2910 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2911 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2912 ", burstId = %" PRId32, __FUNCTION__,
2913 queuedResult->mResultExtras.requestId,
2914 queuedResult->mResultExtras.frameNumber,
2915 queuedResult->mResultExtras.burstId);
2916
2917 mResultSignal.signal();
2918}
2919
2920
2921void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002922 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002923 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002924 Mutex::Autolock l(mOutputLock);
2925
2926 CaptureResult captureResult;
2927 captureResult.mResultExtras = resultExtras;
2928 captureResult.mMetadata = partialResult;
2929
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002930 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002931}
2932
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002933
2934void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2935 CaptureResultExtras &resultExtras,
2936 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002937 uint32_t frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002938 bool reprocess,
2939 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002940 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002941 if (pendingMetadata.isEmpty())
2942 return;
2943
2944 Mutex::Autolock l(mOutputLock);
2945
2946 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002947 if (reprocess) {
2948 if (frameNumber < mNextReprocessResultFrameNumber) {
2949 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002950 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002951 frameNumber, mNextReprocessResultFrameNumber);
2952 return;
2953 }
2954 mNextReprocessResultFrameNumber = frameNumber + 1;
2955 } else {
2956 if (frameNumber < mNextResultFrameNumber) {
2957 SET_ERR("Out-of-order capture result metadata submitted! "
2958 "(got frame number %d, expecting %d)",
2959 frameNumber, mNextResultFrameNumber);
2960 return;
2961 }
2962 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002963 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002964
2965 CaptureResult captureResult;
2966 captureResult.mResultExtras = resultExtras;
2967 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002968 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002969
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002970 // Append any previous partials to form a complete result
2971 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2972 captureResult.mMetadata.append(collectedPartialResult);
2973 }
2974
2975 captureResult.mMetadata.sort();
2976
2977 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002978 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2979 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002980 SET_ERR("No timestamp provided by HAL for frame %d!",
2981 frameNumber);
2982 return;
2983 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002984 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
2985 camera_metadata_entry timestamp =
2986 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2987 if (timestamp.count == 0) {
2988 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
2989 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
2990 return;
2991 }
2992 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002993
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07002994 // Fix up some result metadata to account for HAL-level distortion correction
2995 status_t res = mDistortionMapper.correctCaptureResult(&captureResult.mMetadata);
2996 if (res != OK) {
2997 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
2998 frameNumber, strerror(res), res);
2999 return;
3000 }
3001
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003002 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
3003 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
3004
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003005 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003006}
3007
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003008/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003009 * Camera HAL device callback methods
3010 */
3011
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003012void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003013 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003014
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003015 status_t res;
3016
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003017 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003018 if (result->result == NULL && result->num_output_buffers == 0 &&
3019 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003020 SET_ERR("No result data provided by HAL for frame %d",
3021 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003022 return;
3023 }
Zhijun He204e3292014-07-14 17:09:23 -07003024
Zhijun He204e3292014-07-14 17:09:23 -07003025 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003026 result->result != NULL &&
3027 result->partial_result != 1) {
3028 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3029 " if partial result is not supported",
3030 frameNumber, result->partial_result);
3031 return;
3032 }
3033
3034 bool isPartialResult = false;
3035 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003036 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003037
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003038 // Get shutter timestamp and resultExtras from list of in-flight requests,
3039 // where it was added by the shutter notification for this frame. If the
3040 // shutter timestamp isn't received yet, append the output buffers to the
3041 // in-flight request and they will be returned when the shutter timestamp
3042 // arrives. Update the in-flight status and remove the in-flight entry if
3043 // all result data and shutter timestamp have been received.
3044 nsecs_t shutterTimestamp = 0;
3045
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003046 {
3047 Mutex::Autolock l(mInFlightLock);
3048 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3049 if (idx == NAME_NOT_FOUND) {
3050 SET_ERR("Unknown frame number for capture result: %d",
3051 frameNumber);
3052 return;
3053 }
3054 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003055 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3056 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003057 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003058 __FUNCTION__, request.resultExtras.requestId,
3059 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003060 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003061 // Always update the partial count to the latest one if it's not 0
3062 // (buffers only). When framework aggregates adjacent partial results
3063 // into one, the latest partial count will be used.
3064 if (result->partial_result != 0)
3065 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003066
3067 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003068 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003069 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3070 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3071 " the range of [1, %d] when metadata is included in the result",
3072 frameNumber, result->partial_result, mNumPartialResults);
3073 return;
3074 }
3075 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003076 if (isPartialResult && result->num_physcam_metadata) {
3077 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3078 " physical camera result", frameNumber);
3079 return;
3080 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003081 if (isPartialResult) {
3082 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003083 }
3084
Shuzhen Wang4a472662017-02-26 23:29:04 -08003085 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003086 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003087 sendPartialCaptureResult(result->result, request.resultExtras,
3088 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003089 }
3090 }
3091
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003092 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003093 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003094
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003095 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003096 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003097 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
3098 SET_ERR("Requested physical Camera Ids %d not equal to number of metadata %d",
3099 request.physicalCameraIds.size(), result->num_physcam_metadata);
3100 return;
3101 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003102 if (request.haveResultMetadata) {
3103 SET_ERR("Called multiple times with metadata for frame %d",
3104 frameNumber);
3105 return;
3106 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003107 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3108 String8 physicalId(result->physcam_ids[i]);
3109 std::set<String8>::iterator cameraIdIter =
3110 request.physicalCameraIds.find(physicalId);
3111 if (cameraIdIter != request.physicalCameraIds.end()) {
3112 request.physicalCameraIds.erase(cameraIdIter);
3113 } else {
3114 SET_ERR("Total result for frame %d has already returned for camera %s",
3115 frameNumber, physicalId.c_str());
3116 return;
3117 }
3118 }
Zhijun He204e3292014-07-14 17:09:23 -07003119 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003120 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003121 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003122 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003123 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003124 request.haveResultMetadata = true;
3125 }
3126
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003127 uint32_t numBuffersReturned = result->num_output_buffers;
3128 if (result->input_buffer != NULL) {
3129 if (hasInputBufferInRequest) {
3130 numBuffersReturned += 1;
3131 } else {
3132 ALOGW("%s: Input buffer should be NULL if there is no input"
3133 " buffer sent in the request",
3134 __FUNCTION__);
3135 }
3136 }
3137 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003138 if (request.numBuffersLeft < 0) {
3139 SET_ERR("Too many buffers returned for frame %d",
3140 frameNumber);
3141 return;
3142 }
3143
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003144 camera_metadata_ro_entry_t entry;
3145 res = find_camera_metadata_ro_entry(result->result,
3146 ANDROID_SENSOR_TIMESTAMP, &entry);
3147 if (res == OK && entry.count == 1) {
3148 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003149 }
3150
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003151 // If shutter event isn't received yet, append the output buffers to
3152 // the in-flight request. Otherwise, return the output buffers to
3153 // streams.
3154 if (shutterTimestamp == 0) {
3155 request.pendingOutputBuffers.appendArray(result->output_buffers,
3156 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003157 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003158 returnOutputBuffers(result->output_buffers,
3159 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07003160 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003161
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003162 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003163 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3164 CameraMetadata physicalMetadata;
3165 physicalMetadata.append(result->physcam_metadata[i]);
3166 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3167 physicalMetadata});
3168 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003169 if (shutterTimestamp == 0) {
3170 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003171 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003172 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003173 CameraMetadata metadata;
3174 metadata = result->result;
3175 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003176 collectedPartialResult, frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003177 hasInputBufferInRequest, request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003178 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003179 }
3180
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003181 removeInFlightRequestIfReadyLocked(idx);
3182 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003183
Zhijun Hef0d962a2014-06-30 10:24:11 -07003184 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003185 if (hasInputBufferInRequest) {
3186 Camera3Stream *stream =
3187 Camera3Stream::cast(result->input_buffer->stream);
3188 res = stream->returnInputBuffer(*(result->input_buffer));
3189 // Note: stream may be deallocated at this point, if this buffer was the
3190 // last reference to it.
3191 if (res != OK) {
3192 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3193 " its stream:%s (%d)", __FUNCTION__,
3194 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003195 }
3196 } else {
3197 ALOGW("%s: Input buffer should be NULL if there is no input"
3198 " buffer sent in the request, skipping input buffer return.",
3199 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003200 }
3201 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003202}
3203
3204void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003205 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003206 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003207 {
3208 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003209 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003210 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003211
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003212 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003213 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003214 return;
3215 }
3216
3217 switch (msg->type) {
3218 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003219 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003220 break;
3221 }
3222 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003223 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003224 break;
3225 }
3226 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003227 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003228 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003229 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003230}
3231
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003232void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003233 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003234 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003235 // Map camera HAL error codes to ICameraDeviceCallback error codes
3236 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003237 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003238 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003239 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003240 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003241 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003242 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003243 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003244 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003245 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003246 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003247 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003248 };
3249
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003250 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003251 ((msg.error_code >= 0) &&
3252 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3253 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003254 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003255
3256 int streamId = 0;
3257 if (msg.error_stream != NULL) {
3258 Camera3Stream *stream =
3259 Camera3Stream::cast(msg.error_stream);
3260 streamId = stream->getId();
3261 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003262 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3263 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003264 streamId, msg.error_code);
3265
3266 CaptureResultExtras resultExtras;
3267 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003268 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003269 // SET_ERR calls notifyError
3270 SET_ERR("Camera HAL reported serious device error");
3271 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003272 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3273 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3274 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003275 {
3276 Mutex::Autolock l(mInFlightLock);
3277 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3278 if (idx >= 0) {
3279 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3280 r.requestStatus = msg.error_code;
3281 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07003282 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3283 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3284 errorCode) {
3285 r.skipResultMetadata = true;
3286 }
Emilian Peevba0fac32017-03-30 09:05:34 +01003287 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3288 errorCode) {
3289 // In case of missing result check whether the buffers
3290 // returned. If they returned, then remove inflight
3291 // request.
3292 removeInFlightRequestIfReadyLocked(idx);
3293 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003294 } else {
3295 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003296 ALOGE("Camera %s: %s: cannot find in-flight request on "
3297 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003298 resultExtras.frameNumber);
3299 }
3300 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003301 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003302 if (listener != NULL) {
3303 listener->notifyError(errorCode, resultExtras);
3304 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003305 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003306 }
3307 break;
3308 default:
3309 // SET_ERR calls notifyError
3310 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3311 break;
3312 }
3313}
3314
3315void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003316 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003317 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003318 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003319
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003320 // Set timestamp for the request in the in-flight tracking
3321 // and get the request ID to send upstream
3322 {
3323 Mutex::Autolock l(mInFlightLock);
3324 idx = mInFlightMap.indexOfKey(msg.frame_number);
3325 if (idx >= 0) {
3326 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003327
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003328 // Verify ordering of shutter notifications
3329 {
3330 Mutex::Autolock l(mOutputLock);
3331 // TODO: need to track errors for tighter bounds on expected frame number.
3332 if (r.hasInputBuffer) {
3333 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3334 SET_ERR("Shutter notification out-of-order. Expected "
3335 "notification for frame %d, got frame %d",
3336 mNextReprocessShutterFrameNumber, msg.frame_number);
3337 return;
3338 }
3339 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3340 } else {
3341 if (msg.frame_number < mNextShutterFrameNumber) {
3342 SET_ERR("Shutter notification out-of-order. Expected "
3343 "notification for frame %d, got frame %d",
3344 mNextShutterFrameNumber, msg.frame_number);
3345 return;
3346 }
3347 mNextShutterFrameNumber = msg.frame_number + 1;
3348 }
3349 }
3350
Shuzhen Wang4a472662017-02-26 23:29:04 -08003351 r.shutterTimestamp = msg.timestamp;
3352 if (r.hasCallback) {
3353 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003354 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003355 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003356 // Call listener, if any
3357 if (listener != NULL) {
3358 listener->notifyShutter(r.resultExtras, msg.timestamp);
3359 }
3360 // send pending result and buffers
3361 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3362 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003363 r.hasInputBuffer, r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003364 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003365 returnOutputBuffers(r.pendingOutputBuffers.array(),
3366 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3367 r.pendingOutputBuffers.clear();
3368
3369 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003370 }
3371 }
3372 if (idx < 0) {
3373 SET_ERR("Shutter notification for non-existent frame number %d",
3374 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003375 }
3376}
3377
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003378CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003379 ALOGV("%s", __FUNCTION__);
3380
Igor Murashkin1e479c02013-09-06 16:55:14 -07003381 CameraMetadata retVal;
3382
3383 if (mRequestThread != NULL) {
3384 retVal = mRequestThread->getLatestRequest();
3385 }
3386
Igor Murashkin1e479c02013-09-06 16:55:14 -07003387 return retVal;
3388}
3389
Jianing Weicb0652e2014-03-12 18:29:36 -07003390
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003391void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3392 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3393 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3394}
3395
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003396/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003397 * HalInterface inner class methods
3398 */
3399
Yifan Hongf79b5542017-04-11 14:44:25 -07003400Camera3Device::HalInterface::HalInterface(
3401 sp<ICameraDeviceSession> &session,
3402 std::shared_ptr<RequestMetadataQueue> queue) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003403 mHidlSession(session),
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003404 mRequestMetadataQueue(queue) {
3405 // Check with hardware service manager if we can downcast these interfaces
3406 // Somewhat expensive, so cache the results at startup
3407 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3408 if (castResult_3_4.isOk()) {
3409 mHidlSession_3_4 = castResult_3_4;
3410 }
3411 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3412 if (castResult_3_3.isOk()) {
3413 mHidlSession_3_3 = castResult_3_3;
3414 }
3415}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003416
Emilian Peev31abd0a2017-05-11 18:37:46 +01003417Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003418
3419Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003420 mHidlSession(other.mHidlSession),
3421 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003422
3423bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003424 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003425}
3426
3427void Camera3Device::HalInterface::clear() {
Emilian Peev9e740b02018-01-30 18:28:03 +00003428 mHidlSession_3_4.clear();
3429 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003430 mHidlSession.clear();
3431}
3432
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003433bool Camera3Device::HalInterface::supportBatchRequest() {
3434 return mHidlSession != nullptr;
3435}
3436
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003437status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3438 camera3_request_template_t templateId,
3439 /*out*/ camera_metadata_t **requestTemplate) {
3440 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3441 if (!valid()) return INVALID_OPERATION;
3442 status_t res = OK;
3443
Emilian Peev31abd0a2017-05-11 18:37:46 +01003444 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003445
3446 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003447 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003448 status = s;
3449 if (status == common::V1_0::Status::OK) {
3450 const camera_metadata *r =
3451 reinterpret_cast<const camera_metadata_t*>(request.data());
3452 size_t expectedSize = request.size();
3453 int ret = validate_camera_metadata_structure(r, &expectedSize);
3454 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3455 *requestTemplate = clone_camera_metadata(r);
3456 if (*requestTemplate == nullptr) {
3457 ALOGE("%s: Unable to clone camera metadata received from HAL",
3458 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003459 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003460 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003461 } else {
3462 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3463 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003464 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003465 }
3466 };
3467 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003468 RequestTemplate id;
3469 switch (templateId) {
3470 case CAMERA3_TEMPLATE_PREVIEW:
3471 id = RequestTemplate::PREVIEW;
3472 break;
3473 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3474 id = RequestTemplate::STILL_CAPTURE;
3475 break;
3476 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3477 id = RequestTemplate::VIDEO_RECORD;
3478 break;
3479 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3480 id = RequestTemplate::VIDEO_SNAPSHOT;
3481 break;
3482 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3483 id = RequestTemplate::ZERO_SHUTTER_LAG;
3484 break;
3485 case CAMERA3_TEMPLATE_MANUAL:
3486 id = RequestTemplate::MANUAL;
3487 break;
3488 default:
3489 // Unknown template ID, or this HAL is too old to support it
3490 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003491 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003492 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003493
Emilian Peev31abd0a2017-05-11 18:37:46 +01003494 if (!err.isOk()) {
3495 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3496 res = DEAD_OBJECT;
3497 } else {
3498 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003499 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003500
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003501 return res;
3502}
3503
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003504status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00003505 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003506 ATRACE_NAME("CameraHal::configureStreams");
3507 if (!valid()) return INVALID_OPERATION;
3508 status_t res = OK;
3509
Emilian Peev31abd0a2017-05-11 18:37:46 +01003510 // Convert stream config to HIDL
3511 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003512 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3513 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3514 requestedConfiguration3_2.streams.resize(config->num_streams);
3515 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003516 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003517 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3518 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003519 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003520
Emilian Peev31abd0a2017-05-11 18:37:46 +01003521 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3522 cam3stream->setBufferFreedListener(this);
3523 int streamId = cam3stream->getId();
3524 StreamType streamType;
3525 switch (src->stream_type) {
3526 case CAMERA3_STREAM_OUTPUT:
3527 streamType = StreamType::OUTPUT;
3528 break;
3529 case CAMERA3_STREAM_INPUT:
3530 streamType = StreamType::INPUT;
3531 break;
3532 default:
3533 ALOGE("%s: Stream %d: Unsupported stream type %d",
3534 __FUNCTION__, streamId, config->streams[i]->stream_type);
3535 return BAD_VALUE;
3536 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003537 dst3_2.id = streamId;
3538 dst3_2.streamType = streamType;
3539 dst3_2.width = src->width;
3540 dst3_2.height = src->height;
3541 dst3_2.format = mapToPixelFormat(src->format);
3542 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3543 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3544 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3545 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003546 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003547 if (src->physical_camera_id != nullptr) {
3548 dst3_4.physicalCameraId = src->physical_camera_id;
3549 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003550
3551 activeStreams.insert(streamId);
3552 // Create Buffer ID map if necessary
3553 if (mBufferIdMaps.count(streamId) == 0) {
3554 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3555 }
3556 }
3557 // remove BufferIdMap for deleted streams
3558 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3559 int streamId = it->first;
3560 bool active = activeStreams.count(streamId) > 0;
3561 if (!active) {
3562 it = mBufferIdMaps.erase(it);
3563 } else {
3564 ++it;
3565 }
3566 }
3567
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003568 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003569 res = mapToStreamConfigurationMode(
3570 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003571 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003572 if (res != OK) {
3573 return res;
3574 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003575 requestedConfiguration3_2.operationMode = operationMode;
3576 requestedConfiguration3_4.operationMode = operationMode;
3577 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003578 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3579 get_camera_metadata_size(sessionParams));
3580
Emilian Peev31abd0a2017-05-11 18:37:46 +01003581 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003582 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003583 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003584
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003585 // See if we have v3.4 or v3.3 HAL
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003586 if (mHidlSession_3_4 != nullptr) {
3587 // We do; use v3.4 for the call
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003588 ALOGV("%s: v3.4 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003589 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003590 auto err = mHidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003591 [&status, &finalConfiguration3_4]
3592 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3593 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003594 status = s;
3595 });
3596 if (!err.isOk()) {
3597 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3598 return DEAD_OBJECT;
3599 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003600 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3601 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3602 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3603 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003604 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003605 // We do; use v3.3 for the call
3606 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003607 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003608 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003609 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003610 finalConfiguration = halConfiguration;
3611 status = s;
3612 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003613 if (!err.isOk()) {
3614 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3615 return DEAD_OBJECT;
3616 }
3617 } else {
3618 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3619 ALOGV("%s: v3.2 device found", __FUNCTION__);
3620 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003621 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003622 [&status, &finalConfiguration_3_2]
3623 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3624 finalConfiguration_3_2 = halConfiguration;
3625 status = s;
3626 });
3627 if (!err.isOk()) {
3628 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3629 return DEAD_OBJECT;
3630 }
3631 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3632 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3633 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3634 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003635 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003636 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003637 }
3638
3639 if (status != common::V1_0::Status::OK ) {
3640 return CameraProviderManager::mapToStatusT(status);
3641 }
3642
3643 // And convert output stream configuration from HIDL
3644
3645 for (size_t i = 0; i < config->num_streams; i++) {
3646 camera3_stream_t *dst = config->streams[i];
3647 int streamId = Camera3Stream::cast(dst)->getId();
3648
3649 // Start scan at i, with the assumption that the stream order matches
3650 size_t realIdx = i;
3651 bool found = false;
3652 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003653 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003654 found = true;
3655 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003656 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003657 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3658 }
3659 if (!found) {
3660 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3661 __FUNCTION__, streamId);
3662 return INVALID_OPERATION;
3663 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003664 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003665
Emilian Peev710c1422017-08-30 11:19:38 +01003666 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3667 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003668 dstStream->setDataSpaceOverride(false);
3669 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3670 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3671
Emilian Peev31abd0a2017-05-11 18:37:46 +01003672 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3673 if (dst->format != overrideFormat) {
3674 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3675 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003676 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003677 if (dst->data_space != overrideDataSpace) {
3678 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3679 streamId, dst->format);
3680 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003681 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003682 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003683 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3684
Emilian Peev31abd0a2017-05-11 18:37:46 +01003685 // Override allowed with IMPLEMENTATION_DEFINED
3686 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003687 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003688 }
3689
Emilian Peev31abd0a2017-05-11 18:37:46 +01003690 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003691 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003692 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003693 __FUNCTION__, streamId);
3694 return INVALID_OPERATION;
3695 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003696 dstStream->setUsage(
3697 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003698 } else {
3699 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003700 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003701 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3702 __FUNCTION__, streamId);
3703 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003704 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003705 dstStream->setUsage(
3706 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003707 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003708 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003709 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003710
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003711 return res;
3712}
3713
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003714void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3715 /*out*/device::V3_2::CaptureRequest* captureRequest,
3716 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003717 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003718 if (captureRequest == nullptr || handlesCreated == nullptr) {
3719 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3720 __FUNCTION__, captureRequest, handlesCreated);
3721 return;
3722 }
3723
3724 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003725
3726 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003727
3728 {
3729 std::lock_guard<std::mutex> lock(mInflightLock);
3730 if (request->input_buffer != nullptr) {
3731 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3732 buffer_handle_t buf = *(request->input_buffer->buffer);
3733 auto pair = getBufferId(buf, streamId);
3734 bool isNewBuffer = pair.first;
3735 uint64_t bufferId = pair.second;
3736 captureRequest->inputBuffer.streamId = streamId;
3737 captureRequest->inputBuffer.bufferId = bufferId;
3738 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3739 captureRequest->inputBuffer.status = BufferStatus::OK;
3740 native_handle_t *acquireFence = nullptr;
3741 if (request->input_buffer->acquire_fence != -1) {
3742 acquireFence = native_handle_create(1,0);
3743 acquireFence->data[0] = request->input_buffer->acquire_fence;
3744 handlesCreated->push_back(acquireFence);
3745 }
3746 captureRequest->inputBuffer.acquireFence = acquireFence;
3747 captureRequest->inputBuffer.releaseFence = nullptr;
3748
3749 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3750 request->input_buffer->buffer,
3751 request->input_buffer->acquire_fence);
3752 } else {
3753 captureRequest->inputBuffer.streamId = -1;
3754 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3755 }
3756
3757 captureRequest->outputBuffers.resize(request->num_output_buffers);
3758 for (size_t i = 0; i < request->num_output_buffers; i++) {
3759 const camera3_stream_buffer_t *src = request->output_buffers + i;
3760 StreamBuffer &dst = captureRequest->outputBuffers[i];
3761 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3762 buffer_handle_t buf = *(src->buffer);
3763 auto pair = getBufferId(buf, streamId);
3764 bool isNewBuffer = pair.first;
3765 dst.streamId = streamId;
3766 dst.bufferId = pair.second;
3767 dst.buffer = isNewBuffer ? buf : nullptr;
3768 dst.status = BufferStatus::OK;
3769 native_handle_t *acquireFence = nullptr;
3770 if (src->acquire_fence != -1) {
3771 acquireFence = native_handle_create(1,0);
3772 acquireFence->data[0] = src->acquire_fence;
3773 handlesCreated->push_back(acquireFence);
3774 }
3775 dst.acquireFence = acquireFence;
3776 dst.releaseFence = nullptr;
3777
3778 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3779 src->buffer, src->acquire_fence);
3780 }
3781 }
3782}
3783
3784status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3785 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3786 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3787 if (!valid()) return INVALID_OPERATION;
3788
Emilian Peevaebbe412018-01-15 13:53:24 +00003789 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3790 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3791 if (castResult_3_4.isOk()) {
3792 hidlSession_3_4 = castResult_3_4;
3793 }
3794
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003795 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003796 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003797 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00003798 if (hidlSession_3_4 != nullptr) {
3799 captureRequests_3_4.resize(batchSize);
3800 } else {
3801 captureRequests.resize(batchSize);
3802 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003803 std::vector<native_handle_t*> handlesCreated;
3804
3805 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003806 if (hidlSession_3_4 != nullptr) {
3807 wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3808 /*out*/&handlesCreated);
3809 } else {
3810 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3811 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003812 }
3813
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003814 std::vector<device::V3_2::BufferCache> cachesToRemove;
3815 {
3816 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3817 for (auto& pair : mFreedBuffers) {
3818 // The stream might have been removed since onBufferFreed
3819 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3820 cachesToRemove.push_back({pair.first, pair.second});
3821 }
3822 }
3823 mFreedBuffers.clear();
3824 }
3825
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003826 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3827 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003828
3829 // Write metadata to FMQ.
3830 for (size_t i = 0; i < batchSize; i++) {
3831 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003832 device::V3_2::CaptureRequest* captureRequest;
3833 if (hidlSession_3_4 != nullptr) {
3834 captureRequest = &captureRequests_3_4[i].v3_2;
3835 } else {
3836 captureRequest = &captureRequests[i];
3837 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003838
3839 if (request->settings != nullptr) {
3840 size_t settingsSize = get_camera_metadata_size(request->settings);
3841 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3842 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3843 captureRequest->settings.resize(0);
3844 captureRequest->fmqSettingsSize = settingsSize;
3845 } else {
3846 if (mRequestMetadataQueue != nullptr) {
3847 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3848 }
3849 captureRequest->settings.setToExternal(
3850 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3851 get_camera_metadata_size(request->settings));
3852 captureRequest->fmqSettingsSize = 0u;
3853 }
3854 } else {
3855 // A null request settings maps to a size-0 CameraMetadata
3856 captureRequest->settings.resize(0);
3857 captureRequest->fmqSettingsSize = 0u;
3858 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003859
3860 if (hidlSession_3_4 != nullptr) {
3861 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3862 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00003863 if (request->physcam_settings != nullptr) {
3864 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3865 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3866 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3867 settingsSize)) {
3868 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3869 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
3870 settingsSize;
3871 } else {
3872 if (mRequestMetadataQueue != nullptr) {
3873 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3874 }
3875 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3876 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3877 request->physcam_settings[j])),
3878 get_camera_metadata_size(request->physcam_settings[j]));
3879 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00003880 }
Emilian Peev00420d22018-02-05 21:33:13 +00003881 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00003882 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00003883 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00003884 }
3885 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3886 request->physcam_id[j];
3887 }
3888 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003889 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003890
3891 hardware::details::return_status err;
3892 if (hidlSession_3_4 != nullptr) {
3893 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003894 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3895 status = s;
3896 *numRequestProcessed = n;
3897 });
Emilian Peevaebbe412018-01-15 13:53:24 +00003898 } else {
3899 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
3900 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3901 status = s;
3902 *numRequestProcessed = n;
3903 });
3904 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003905 if (!err.isOk()) {
3906 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3907 return DEAD_OBJECT;
3908 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003909 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3910 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3911 __FUNCTION__, *numRequestProcessed, batchSize);
3912 status = common::V1_0::Status::INTERNAL_ERROR;
3913 }
3914
3915 for (auto& handle : handlesCreated) {
3916 native_handle_delete(handle);
3917 }
3918 return CameraProviderManager::mapToStatusT(status);
3919}
3920
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003921status_t Camera3Device::HalInterface::processCaptureRequest(
3922 camera3_capture_request_t *request) {
3923 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003924 if (!valid()) return INVALID_OPERATION;
3925 status_t res = OK;
3926
Emilian Peev31abd0a2017-05-11 18:37:46 +01003927 uint32_t numRequestProcessed = 0;
3928 std::vector<camera3_capture_request_t*> requests(1);
3929 requests[0] = request;
3930 res = processBatchCaptureRequests(requests, &numRequestProcessed);
3931
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003932 return res;
3933}
3934
3935status_t Camera3Device::HalInterface::flush() {
3936 ATRACE_NAME("CameraHal::flush");
3937 if (!valid()) return INVALID_OPERATION;
3938 status_t res = OK;
3939
Emilian Peev31abd0a2017-05-11 18:37:46 +01003940 auto err = mHidlSession->flush();
3941 if (!err.isOk()) {
3942 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3943 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003944 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003945 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003946 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003947
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003948 return res;
3949}
3950
Emilian Peev31abd0a2017-05-11 18:37:46 +01003951status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003952 ATRACE_NAME("CameraHal::dump");
3953 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003954
Emilian Peev31abd0a2017-05-11 18:37:46 +01003955 // Handled by CameraProviderManager::dump
3956
3957 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003958}
3959
3960status_t Camera3Device::HalInterface::close() {
3961 ATRACE_NAME("CameraHal::close()");
3962 if (!valid()) return INVALID_OPERATION;
3963 status_t res = OK;
3964
Emilian Peev31abd0a2017-05-11 18:37:46 +01003965 auto err = mHidlSession->close();
3966 // Interface will be dead shortly anyway, so don't log errors
3967 if (!err.isOk()) {
3968 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003969 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003970
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003971 return res;
3972}
3973
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003974void Camera3Device::HalInterface::getInflightBufferKeys(
3975 std::vector<std::pair<int32_t, int32_t>>* out) {
3976 std::lock_guard<std::mutex> lock(mInflightLock);
3977 out->clear();
3978 out->reserve(mInflightBufferMap.size());
3979 for (auto& pair : mInflightBufferMap) {
3980 uint64_t key = pair.first;
3981 int32_t streamId = key & 0xFFFFFFFF;
3982 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3983 out->push_back(std::make_pair(frameNumber, streamId));
3984 }
3985 return;
3986}
3987
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003988status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003989 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003990 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003991 auto pair = std::make_pair(buffer, acquireFence);
3992 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003993 return OK;
3994}
3995
3996status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003997 int32_t frameNumber, int32_t streamId,
3998 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003999 std::lock_guard<std::mutex> lock(mInflightLock);
4000
4001 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4002 auto it = mInflightBufferMap.find(key);
4003 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004004 auto pair = it->second;
4005 *buffer = pair.first;
4006 int acquireFence = pair.second;
4007 if (acquireFence > 0) {
4008 ::close(acquireFence);
4009 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004010 mInflightBufferMap.erase(it);
4011 return OK;
4012}
4013
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004014std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4015 const buffer_handle_t& buf, int streamId) {
4016 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4017
4018 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4019 auto it = bIdMap.find(buf);
4020 if (it == bIdMap.end()) {
4021 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004022 ALOGV("stream %d now have %zu buffer caches, buf %p",
4023 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004024 return std::make_pair(true, mNextBufferId - 1);
4025 } else {
4026 return std::make_pair(false, it->second);
4027 }
4028}
4029
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004030void Camera3Device::HalInterface::onBufferFreed(
4031 int streamId, const native_handle_t* handle) {
4032 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4033 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4034 auto mapIt = mBufferIdMaps.find(streamId);
4035 if (mapIt == mBufferIdMaps.end()) {
4036 // streamId might be from a deleted stream here
4037 ALOGI("%s: stream %d has been removed",
4038 __FUNCTION__, streamId);
4039 return;
4040 }
4041 BufferIdMap& bIdMap = mapIt->second;
4042 auto it = bIdMap.find(handle);
4043 if (it == bIdMap.end()) {
4044 ALOGW("%s: cannot find buffer %p in stream %d",
4045 __FUNCTION__, handle, streamId);
4046 return;
4047 } else {
4048 bufferId = it->second;
4049 bIdMap.erase(it);
4050 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4051 __FUNCTION__, streamId, bIdMap.size(), handle);
4052 }
4053 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4054}
4055
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004056/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004057 * RequestThread inner class methods
4058 */
4059
4060Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004061 sp<StatusTracker> statusTracker,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004062 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004063 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004064 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004065 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004066 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004067 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004068 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004069 mReconfigured(false),
4070 mDoPause(false),
4071 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004072 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004073 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004074 mCurrentAfTriggerId(0),
4075 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004076 mRepeatingLastFrameNumber(
4077 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004078 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004079 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004080 mRequestLatency(kRequestLatencyBinSize),
4081 mSessionParamKeys(sessionParamKeys),
4082 mLatestSessionParams(sessionParamKeys.size()) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004083 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004084}
4085
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004086Camera3Device::RequestThread::~RequestThread() {}
4087
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004088void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004089 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004090 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004091 Mutex::Autolock l(mRequestLock);
4092 mListener = listener;
4093}
4094
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004095void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4096 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004097 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004098 Mutex::Autolock l(mRequestLock);
4099 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004100 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004101 // Prepare video stream for high speed recording.
4102 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004103 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004104}
4105
Jianing Wei90e59c92014-03-12 18:29:36 -07004106status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004107 List<sp<CaptureRequest> > &requests,
4108 /*out*/
4109 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004110 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004111 Mutex::Autolock l(mRequestLock);
4112 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4113 ++it) {
4114 mRequestQueue.push_back(*it);
4115 }
4116
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004117 if (lastFrameNumber != NULL) {
4118 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4119 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4120 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4121 *lastFrameNumber);
4122 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004123
Jianing Wei90e59c92014-03-12 18:29:36 -07004124 unpauseForNewRequests();
4125
4126 return OK;
4127}
4128
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004129
4130status_t Camera3Device::RequestThread::queueTrigger(
4131 RequestTrigger trigger[],
4132 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004133 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004134 Mutex::Autolock l(mTriggerMutex);
4135 status_t ret;
4136
4137 for (size_t i = 0; i < count; ++i) {
4138 ret = queueTriggerLocked(trigger[i]);
4139
4140 if (ret != OK) {
4141 return ret;
4142 }
4143 }
4144
4145 return OK;
4146}
4147
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004148const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4149 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004150 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004151 if (d != nullptr) return d->mId;
4152 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004153}
4154
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004155status_t Camera3Device::RequestThread::queueTriggerLocked(
4156 RequestTrigger trigger) {
4157
4158 uint32_t tag = trigger.metadataTag;
4159 ssize_t index = mTriggerMap.indexOfKey(tag);
4160
4161 switch (trigger.getTagType()) {
4162 case TYPE_BYTE:
4163 // fall-through
4164 case TYPE_INT32:
4165 break;
4166 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004167 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4168 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004169 return INVALID_OPERATION;
4170 }
4171
4172 /**
4173 * Collect only the latest trigger, since we only have 1 field
4174 * in the request settings per trigger tag, and can't send more than 1
4175 * trigger per request.
4176 */
4177 if (index != NAME_NOT_FOUND) {
4178 mTriggerMap.editValueAt(index) = trigger;
4179 } else {
4180 mTriggerMap.add(tag, trigger);
4181 }
4182
4183 return OK;
4184}
4185
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004186status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004187 const RequestList &requests,
4188 /*out*/
4189 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004190 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004191 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004192 if (lastFrameNumber != NULL) {
4193 *lastFrameNumber = mRepeatingLastFrameNumber;
4194 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004195 mRepeatingRequests.clear();
4196 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4197 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004198
4199 unpauseForNewRequests();
4200
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004201 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004202 return OK;
4203}
4204
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004205bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004206 if (mRepeatingRequests.empty()) {
4207 return false;
4208 }
4209 int32_t requestId = requestIn->mResultExtras.requestId;
4210 const RequestList &repeatRequests = mRepeatingRequests;
4211 // All repeating requests are guaranteed to have same id so only check first quest
4212 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4213 return (firstRequest->mResultExtras.requestId == requestId);
4214}
4215
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004216status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004217 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004218 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004219 return clearRepeatingRequestsLocked(lastFrameNumber);
4220
4221}
4222
4223status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004224 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004225 if (lastFrameNumber != NULL) {
4226 *lastFrameNumber = mRepeatingLastFrameNumber;
4227 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004228 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004229 return OK;
4230}
4231
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004232status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004233 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004234 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004235 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004236 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004237
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004238 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004239
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004240 // Send errors for all requests pending in the request queue, including
4241 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004242 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004243 if (listener != NULL) {
4244 for (RequestList::iterator it = mRequestQueue.begin();
4245 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004246 // Abort the input buffers for reprocess requests.
4247 if ((*it)->mInputStream != NULL) {
4248 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004249 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4250 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004251 if (res != OK) {
4252 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4253 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4254 } else {
4255 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4256 if (res != OK) {
4257 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4258 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4259 }
4260 }
4261 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004262 // Set the frame number this request would have had, if it
4263 // had been submitted; this frame number will not be reused.
4264 // The requestId and burstId fields were set when the request was
4265 // submitted originally (in convertMetadataListToRequestListLocked)
4266 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004267 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004268 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004269 }
4270 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004271 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004272
4273 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004274 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004275 if (lastFrameNumber != NULL) {
4276 *lastFrameNumber = mRepeatingLastFrameNumber;
4277 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004278 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004279 return OK;
4280}
4281
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004282status_t Camera3Device::RequestThread::flush() {
4283 ATRACE_CALL();
4284 Mutex::Autolock l(mFlushLock);
4285
Emilian Peev08dd2452017-04-06 16:55:14 +01004286 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004287}
4288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004289void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004290 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004291 Mutex::Autolock l(mPauseLock);
4292 mDoPause = paused;
4293 mDoPauseSignal.signal();
4294}
4295
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004296status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4297 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004298 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004299 Mutex::Autolock l(mLatestRequestMutex);
4300 status_t res;
4301 while (mLatestRequestId != requestId) {
4302 nsecs_t startTime = systemTime();
4303
4304 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4305 if (res != OK) return res;
4306
4307 timeout -= (systemTime() - startTime);
4308 }
4309
4310 return OK;
4311}
4312
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004313void Camera3Device::RequestThread::requestExit() {
4314 // Call parent to set up shutdown
4315 Thread::requestExit();
4316 // The exit from any possible waits
4317 mDoPauseSignal.signal();
4318 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004319
4320 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4321 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004322}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004323
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004324void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004325 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004326 bool surfaceAbandoned = false;
4327 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004328 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004329 {
4330 Mutex::Autolock l(mRequestLock);
4331 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4332 // repeating requests.
4333 for (const auto& request : mRepeatingRequests) {
4334 for (const auto& s : request->mOutputStreams) {
4335 if (s->isAbandoned()) {
4336 surfaceAbandoned = true;
4337 clearRepeatingRequestsLocked(&lastFrameNumber);
4338 break;
4339 }
4340 }
4341 if (surfaceAbandoned) {
4342 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004343 }
4344 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004345 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004346 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004347
4348 if (listener != NULL && surfaceAbandoned) {
4349 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004350 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004351}
4352
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004353bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004354 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004355 status_t res;
4356 size_t batchSize = mNextRequests.size();
4357 std::vector<camera3_capture_request_t*> requests(batchSize);
4358 uint32_t numRequestProcessed = 0;
4359 for (size_t i = 0; i < batchSize; i++) {
4360 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004361 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004362 }
4363
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004364 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4365
4366 bool triggerRemoveFailed = false;
4367 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4368 for (size_t i = 0; i < numRequestProcessed; i++) {
4369 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4370 nextRequest.submitted = true;
4371
4372
4373 // Update the latest request sent to HAL
4374 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4375 Mutex::Autolock al(mLatestRequestMutex);
4376
4377 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4378 mLatestRequest.acquire(cloned);
4379
4380 sp<Camera3Device> parent = mParent.promote();
4381 if (parent != NULL) {
4382 parent->monitorMetadata(TagMonitor::REQUEST,
4383 nextRequest.halRequest.frame_number,
4384 0, mLatestRequest);
4385 }
4386 }
4387
4388 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004389 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4390 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004391 }
4392
Emilian Peevaebbe412018-01-15 13:53:24 +00004393 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4394
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004395 if (!triggerRemoveFailed) {
4396 // Remove any previously queued triggers (after unlock)
4397 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4398 if (removeTriggerRes != OK) {
4399 triggerRemoveFailed = true;
4400 triggerFailedRequest = nextRequest;
4401 }
4402 }
4403 }
4404
4405 if (triggerRemoveFailed) {
4406 SET_ERR("RequestThread: Unable to remove triggers "
4407 "(capture request %d, HAL device: %s (%d)",
4408 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4409 cleanUpFailedRequests(/*sendRequestError*/ false);
4410 return false;
4411 }
4412
4413 if (res != OK) {
4414 // Should only get a failure here for malformed requests or device-level
4415 // errors, so consider all errors fatal. Bad metadata failures should
4416 // come through notify.
4417 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4418 mNextRequests[numRequestProcessed].halRequest.frame_number,
4419 strerror(-res), res);
4420 cleanUpFailedRequests(/*sendRequestError*/ false);
4421 return false;
4422 }
4423 return true;
4424}
4425
4426bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4427 status_t res;
4428
4429 for (auto& nextRequest : mNextRequests) {
4430 // Submit request and block until ready for next one
4431 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4432 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4433
4434 if (res != OK) {
4435 // Should only get a failure here for malformed requests or device-level
4436 // errors, so consider all errors fatal. Bad metadata failures should
4437 // come through notify.
4438 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4439 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4440 res);
4441 cleanUpFailedRequests(/*sendRequestError*/ false);
4442 return false;
4443 }
4444
4445 // Mark that the request has be submitted successfully.
4446 nextRequest.submitted = true;
4447
4448 // Update the latest request sent to HAL
4449 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4450 Mutex::Autolock al(mLatestRequestMutex);
4451
4452 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4453 mLatestRequest.acquire(cloned);
4454
4455 sp<Camera3Device> parent = mParent.promote();
4456 if (parent != NULL) {
4457 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4458 0, mLatestRequest);
4459 }
4460 }
4461
4462 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004463 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4464 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004465 }
4466
Emilian Peevaebbe412018-01-15 13:53:24 +00004467 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4468
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004469 // Remove any previously queued triggers (after unlock)
4470 res = removeTriggers(mPrevRequest);
4471 if (res != OK) {
4472 SET_ERR("RequestThread: Unable to remove triggers "
4473 "(capture request %d, HAL device: %s (%d)",
4474 nextRequest.halRequest.frame_number, strerror(-res), res);
4475 cleanUpFailedRequests(/*sendRequestError*/ false);
4476 return false;
4477 }
4478 }
4479 return true;
4480}
4481
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004482nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4483 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4484 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4485 find_camera_metadata_ro_entry(request,
4486 ANDROID_CONTROL_AE_MODE,
4487 &e);
4488 if (e.count == 0) return maxExpectedDuration;
4489
4490 switch (e.data.u8[0]) {
4491 case ANDROID_CONTROL_AE_MODE_OFF:
4492 find_camera_metadata_ro_entry(request,
4493 ANDROID_SENSOR_EXPOSURE_TIME,
4494 &e);
4495 if (e.count > 0) {
4496 maxExpectedDuration = e.data.i64[0];
4497 }
4498 find_camera_metadata_ro_entry(request,
4499 ANDROID_SENSOR_FRAME_DURATION,
4500 &e);
4501 if (e.count > 0) {
4502 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4503 }
4504 break;
4505 default:
4506 find_camera_metadata_ro_entry(request,
4507 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4508 &e);
4509 if (e.count > 1) {
4510 maxExpectedDuration = 1e9 / e.data.u8[0];
4511 }
4512 break;
4513 }
4514
4515 return maxExpectedDuration;
4516}
4517
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004518bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4519 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4520 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4521 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4522 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4523 return true;
4524 }
4525
4526 return false;
4527}
4528
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004529bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4530 ATRACE_CALL();
4531 bool updatesDetected = false;
4532
4533 for (auto tag : mSessionParamKeys) {
4534 camera_metadata_ro_entry entry = settings.find(tag);
4535 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4536
4537 if (entry.count > 0) {
4538 bool isDifferent = false;
4539 if (lastEntry.count > 0) {
4540 // Have a last value, compare to see if changed
4541 if (lastEntry.type == entry.type &&
4542 lastEntry.count == entry.count) {
4543 // Same type and count, compare values
4544 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4545 size_t entryBytes = bytesPerValue * lastEntry.count;
4546 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4547 if (cmp != 0) {
4548 isDifferent = true;
4549 }
4550 } else {
4551 // Count or type has changed
4552 isDifferent = true;
4553 }
4554 } else {
4555 // No last entry, so always consider to be different
4556 isDifferent = true;
4557 }
4558
4559 if (isDifferent) {
4560 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004561 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4562 updatesDetected = true;
4563 }
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004564 mLatestSessionParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004565 }
4566 } else if (lastEntry.count > 0) {
4567 // Value has been removed
4568 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4569 mLatestSessionParams.erase(tag);
4570 updatesDetected = true;
4571 }
4572 }
4573
4574 return updatesDetected;
4575}
4576
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004577bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004578 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004579 status_t res;
4580
4581 // Handle paused state.
4582 if (waitIfPaused()) {
4583 return true;
4584 }
4585
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004586 // Wait for the next batch of requests.
4587 waitForNextRequestBatch();
4588 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004589 return true;
4590 }
4591
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004592 // Get the latest request ID, if any
4593 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004594 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004595 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004596 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004597 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004598 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004599 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4600 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004601 }
4602
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004603 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4604 // or a single request from streaming or burst. In either case the first element
4605 // should contain the latest camera settings that we need to check for any session
4606 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004607 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004608 res = OK;
4609
4610 //Input stream buffers are already acquired at this point so an input stream
4611 //will not be able to move to idle state unless we force it.
4612 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4613 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4614 if (res != OK) {
4615 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4616 cleanUpFailedRequests(/*sendRequestError*/ false);
4617 return false;
4618 }
4619 }
4620
4621 if (res == OK) {
4622 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4623 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08004624 sp<Camera3Device> parent = mParent.promote();
4625 if (parent != nullptr) {
4626 parent->pauseStateNotify(true);
4627 }
4628
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004629 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4630
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004631 if (parent != nullptr) {
4632 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4633 }
4634
4635 statusTracker->markComponentActive(mStatusId);
4636 setPaused(false);
4637 }
4638
4639 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4640 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4641 if (res != OK) {
4642 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4643 cleanUpFailedRequests(/*sendRequestError*/ false);
4644 return false;
4645 }
4646 }
4647 }
4648 }
4649
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004650 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004651 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004652 if (res == TIMED_OUT) {
4653 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004654 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004655 // Check if any stream is abandoned.
4656 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004657 return true;
4658 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004659 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004660 return false;
4661 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004662
Zhijun Hecc27e112013-10-03 16:12:43 -07004663 // Inform waitUntilRequestProcessed thread of a new request ID
4664 {
4665 Mutex::Autolock al(mLatestRequestMutex);
4666
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004667 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004668 mLatestRequestSignal.signal();
4669 }
4670
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004671 // Submit a batch of requests to HAL.
4672 // Use flush lock only when submitting multilple requests in a batch.
4673 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4674 // which may take a long time to finish so synchronizing flush() and
4675 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4676 // For now, only synchronize for high speed recording and we should figure something out for
4677 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004678 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004679
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004680 if (useFlushLock) {
4681 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004682 }
4683
Zhijun Hef0645c12016-08-02 00:58:11 -07004684 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004685 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004686
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004687 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004688 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004689 if (mInterface->supportBatchRequest()) {
4690 submitRequestSuccess = sendRequestsBatch();
4691 } else {
4692 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004693 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004694 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4695 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004696
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004697 if (useFlushLock) {
4698 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004699 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004700
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004701 // Unset as current request
4702 {
4703 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004704 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004705 }
4706
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004707 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004708}
4709
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004710status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004711 ATRACE_CALL();
4712
Shuzhen Wang4a472662017-02-26 23:29:04 -08004713 for (size_t i = 0; i < mNextRequests.size(); i++) {
4714 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004715 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4716 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4717 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4718
4719 // Prepare a request to HAL
4720 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4721
4722 // Insert any queued triggers (before metadata is locked)
4723 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004724 if (res < 0) {
4725 SET_ERR("RequestThread: Unable to insert triggers "
4726 "(capture request %d, HAL device: %s (%d)",
4727 halRequest->frame_number, strerror(-res), res);
4728 return INVALID_OPERATION;
4729 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004730
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004731 int triggerCount = res;
4732 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4733 mPrevTriggers = triggerCount;
4734
4735 // If the request is the same as last, or we had triggers last time
Emilian Peev00420d22018-02-05 21:33:13 +00004736 bool newRequest = mPrevRequest != captureRequest || triggersMixedIn;
4737 if (newRequest) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004738 /**
4739 * HAL workaround:
4740 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4741 */
4742 res = addDummyTriggerIds(captureRequest);
4743 if (res != OK) {
4744 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4745 "(capture request %d, HAL device: %s (%d)",
4746 halRequest->frame_number, strerror(-res), res);
4747 return INVALID_OPERATION;
4748 }
4749
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004750 {
4751 // Correct metadata regions for distortion correction if enabled
4752 sp<Camera3Device> parent = mParent.promote();
4753 if (parent != nullptr) {
4754 res = parent->mDistortionMapper.correctCaptureRequest(
4755 &(captureRequest->mSettingsList.begin()->metadata));
4756 if (res != OK) {
4757 SET_ERR("RequestThread: Unable to correct capture requests "
4758 "for lens distortion for request %d: %s (%d)",
4759 halRequest->frame_number, strerror(-res), res);
4760 return INVALID_OPERATION;
4761 }
4762 }
4763 }
4764
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004765 /**
4766 * The request should be presorted so accesses in HAL
4767 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4768 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004769 captureRequest->mSettingsList.begin()->metadata.sort();
4770 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004771 mPrevRequest = captureRequest;
4772 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4773
4774 IF_ALOGV() {
4775 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4776 find_camera_metadata_ro_entry(
4777 halRequest->settings,
4778 ANDROID_CONTROL_AF_TRIGGER,
4779 &e
4780 );
4781 if (e.count > 0) {
4782 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4783 __FUNCTION__,
4784 halRequest->frame_number,
4785 e.data.u8[0]);
4786 }
4787 }
4788 } else {
4789 // leave request.settings NULL to indicate 'reuse latest given'
4790 ALOGVV("%s: Request settings are REUSED",
4791 __FUNCTION__);
4792 }
4793
Emilian Peevaebbe412018-01-15 13:53:24 +00004794 if (captureRequest->mSettingsList.size() > 1) {
4795 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4796 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00004797 if (newRequest) {
4798 halRequest->physcam_settings =
4799 new const camera_metadata* [halRequest->num_physcam_settings];
4800 } else {
4801 halRequest->physcam_settings = nullptr;
4802 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004803 auto it = ++captureRequest->mSettingsList.begin();
4804 size_t i = 0;
4805 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4806 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00004807 if (newRequest) {
4808 it->metadata.sort();
4809 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4810 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004811 }
4812 }
4813
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004814 uint32_t totalNumBuffers = 0;
4815
4816 // Fill in buffers
4817 if (captureRequest->mInputStream != NULL) {
4818 halRequest->input_buffer = &captureRequest->mInputBuffer;
4819 totalNumBuffers += 1;
4820 } else {
4821 halRequest->input_buffer = NULL;
4822 }
4823
4824 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4825 captureRequest->mOutputStreams.size());
4826 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004827 std::set<String8> requestedPhysicalCameras;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004828 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4829 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004830
4831 // Prepare video buffers for high speed recording on the first video request.
4832 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4833 // Only try to prepare video stream on the first video request.
4834 mPrepareVideoStream = false;
4835
4836 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4837 while (res == NOT_ENOUGH_DATA) {
4838 res = outputStream->prepareNextBuffer();
4839 }
4840 if (res != OK) {
4841 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4842 __FUNCTION__, strerror(-res), res);
4843 outputStream->cancelPrepare();
4844 }
4845 }
4846
Shuzhen Wang4a472662017-02-26 23:29:04 -08004847 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4848 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004849 if (res != OK) {
4850 // Can't get output buffer from gralloc queue - this could be due to
4851 // abandoned queue or other consumer misbehavior, so not a fatal
4852 // error
4853 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4854 " %s (%d)", strerror(-res), res);
4855
4856 return TIMED_OUT;
4857 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004858
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004859 String8 physicalCameraId = outputStream->getPhysicalCameraId();
4860
4861 if (!physicalCameraId.isEmpty()) {
4862 // Physical stream isn't supported for input request.
4863 if (halRequest->input_buffer) {
4864 CLOGE("Physical stream is not supported for input request");
4865 return INVALID_OPERATION;
4866 }
4867 requestedPhysicalCameras.insert(physicalCameraId);
4868 }
4869 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004870 }
4871 totalNumBuffers += halRequest->num_output_buffers;
4872
4873 // Log request in the in-flight queue
4874 sp<Camera3Device> parent = mParent.promote();
4875 if (parent == NULL) {
4876 // Should not happen, and nowhere to send errors to, so just log it
4877 CLOGE("RequestThread: Parent is gone");
4878 return INVALID_OPERATION;
4879 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004880
4881 // If this request list is for constrained high speed recording (not
4882 // preview), and the current request is not the last one in the batch,
4883 // do not send callback to the app.
4884 bool hasCallback = true;
4885 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4886 hasCallback = false;
4887 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004888 res = parent->registerInFlight(halRequest->frame_number,
4889 totalNumBuffers, captureRequest->mResultExtras,
4890 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004891 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004892 calculateMaxExpectedDuration(halRequest->settings),
4893 requestedPhysicalCameras);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004894 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4895 ", burstId = %" PRId32 ".",
4896 __FUNCTION__,
4897 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4898 captureRequest->mResultExtras.burstId);
4899 if (res != OK) {
4900 SET_ERR("RequestThread: Unable to register new in-flight request:"
4901 " %s (%d)", strerror(-res), res);
4902 return INVALID_OPERATION;
4903 }
4904 }
4905
4906 return OK;
4907}
4908
Igor Murashkin1e479c02013-09-06 16:55:14 -07004909CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004910 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004911 Mutex::Autolock al(mLatestRequestMutex);
4912
4913 ALOGV("RequestThread::%s", __FUNCTION__);
4914
4915 return mLatestRequest;
4916}
4917
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004918bool Camera3Device::RequestThread::isStreamPending(
4919 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004920 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004921 Mutex::Autolock l(mRequestLock);
4922
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004923 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004924 if (!nextRequest.submitted) {
4925 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4926 if (stream == s) return true;
4927 }
4928 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004929 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004930 }
4931
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004932 for (const auto& request : mRequestQueue) {
4933 for (const auto& s : request->mOutputStreams) {
4934 if (stream == s) return true;
4935 }
4936 if (stream == request->mInputStream) return true;
4937 }
4938
4939 for (const auto& request : mRepeatingRequests) {
4940 for (const auto& s : request->mOutputStreams) {
4941 if (stream == s) return true;
4942 }
4943 if (stream == request->mInputStream) return true;
4944 }
4945
4946 return false;
4947}
Jianing Weicb0652e2014-03-12 18:29:36 -07004948
Emilian Peev40ead602017-09-26 15:46:36 +01004949bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4950 ATRACE_CALL();
4951 Mutex::Autolock l(mRequestLock);
4952
4953 for (const auto& nextRequest : mNextRequests) {
4954 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4955 if (s.first == streamId) {
4956 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4957 if (it != s.second.end()) {
4958 return true;
4959 }
4960 }
4961 }
4962 }
4963
4964 for (const auto& request : mRequestQueue) {
4965 for (const auto& s : request->mOutputSurfaces) {
4966 if (s.first == streamId) {
4967 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4968 if (it != s.second.end()) {
4969 return true;
4970 }
4971 }
4972 }
4973 }
4974
4975 for (const auto& request : mRepeatingRequests) {
4976 for (const auto& s : request->mOutputSurfaces) {
4977 if (s.first == streamId) {
4978 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4979 if (it != s.second.end()) {
4980 return true;
4981 }
4982 }
4983 }
4984 }
4985
4986 return false;
4987}
4988
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004989nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004990 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004991 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004992 return mExpectedInflightDuration > kMinInflightDuration ?
4993 mExpectedInflightDuration : kMinInflightDuration;
4994}
4995
Emilian Peevaebbe412018-01-15 13:53:24 +00004996void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
4997 camera3_capture_request_t *halRequest) {
4998 if ((request == nullptr) || (halRequest == nullptr)) {
4999 ALOGE("%s: Invalid request!", __FUNCTION__);
5000 return;
5001 }
5002
5003 if (halRequest->num_physcam_settings > 0) {
5004 if (halRequest->physcam_id != nullptr) {
5005 delete [] halRequest->physcam_id;
5006 halRequest->physcam_id = nullptr;
5007 }
5008 if (halRequest->physcam_settings != nullptr) {
5009 auto it = ++(request->mSettingsList.begin());
5010 size_t i = 0;
5011 for (; it != request->mSettingsList.end(); it++, i++) {
5012 it->metadata.unlock(halRequest->physcam_settings[i]);
5013 }
5014 delete [] halRequest->physcam_settings;
5015 halRequest->physcam_settings = nullptr;
5016 }
5017 }
5018}
5019
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005020void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5021 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005022 return;
5023 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005024
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005025 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005026 // Skip the ones that have been submitted successfully.
5027 if (nextRequest.submitted) {
5028 continue;
5029 }
5030
5031 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5032 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5033 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5034
5035 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005036 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005037 }
5038
Emilian Peevaebbe412018-01-15 13:53:24 +00005039 cleanupPhysicalSettings(captureRequest, halRequest);
5040
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005041 if (captureRequest->mInputStream != NULL) {
5042 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5043 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5044 }
5045
5046 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005047 //Buffers that failed processing could still have
5048 //valid acquire fence.
5049 int acquireFence = (*outputBuffers)[i].acquire_fence;
5050 if (0 <= acquireFence) {
5051 close(acquireFence);
5052 outputBuffers->editItemAt(i).acquire_fence = -1;
5053 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005054 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5055 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
5056 }
5057
5058 if (sendRequestError) {
5059 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005060 sp<NotificationListener> listener = mListener.promote();
5061 if (listener != NULL) {
5062 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005063 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005064 captureRequest->mResultExtras);
5065 }
5066 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005067
5068 // Remove yet-to-be submitted inflight request from inflightMap
5069 {
5070 sp<Camera3Device> parent = mParent.promote();
5071 if (parent != NULL) {
5072 Mutex::Autolock l(parent->mInFlightLock);
5073 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5074 if (idx >= 0) {
5075 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5076 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5077 parent->removeInFlightMapEntryLocked(idx);
5078 }
5079 }
5080 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005081 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005082
5083 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005084 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005085}
5086
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005087void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005088 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005089 // Optimized a bit for the simple steady-state case (single repeating
5090 // request), to avoid putting that request in the queue temporarily.
5091 Mutex::Autolock l(mRequestLock);
5092
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005093 assert(mNextRequests.empty());
5094
5095 NextRequest nextRequest;
5096 nextRequest.captureRequest = waitForNextRequestLocked();
5097 if (nextRequest.captureRequest == nullptr) {
5098 return;
5099 }
5100
5101 nextRequest.halRequest = camera3_capture_request_t();
5102 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005103 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005104
5105 // Wait for additional requests
5106 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5107
5108 for (size_t i = 1; i < batchSize; i++) {
5109 NextRequest additionalRequest;
5110 additionalRequest.captureRequest = waitForNextRequestLocked();
5111 if (additionalRequest.captureRequest == nullptr) {
5112 break;
5113 }
5114
5115 additionalRequest.halRequest = camera3_capture_request_t();
5116 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005117 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005118 }
5119
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005120 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005121 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005122 mNextRequests.size(), batchSize);
5123 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005124 }
5125
5126 return;
5127}
5128
5129sp<Camera3Device::CaptureRequest>
5130 Camera3Device::RequestThread::waitForNextRequestLocked() {
5131 status_t res;
5132 sp<CaptureRequest> nextRequest;
5133
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005134 while (mRequestQueue.empty()) {
5135 if (!mRepeatingRequests.empty()) {
5136 // Always atomically enqueue all requests in a repeating request
5137 // list. Guarantees a complete in-sequence set of captures to
5138 // application.
5139 const RequestList &requests = mRepeatingRequests;
5140 RequestList::const_iterator firstRequest =
5141 requests.begin();
5142 nextRequest = *firstRequest;
5143 mRequestQueue.insert(mRequestQueue.end(),
5144 ++firstRequest,
5145 requests.end());
5146 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005147
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005148 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005149
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005150 break;
5151 }
5152
5153 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005155 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5156 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005157 Mutex::Autolock pl(mPauseLock);
5158 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005159 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005160 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005161 // Let the tracker know
5162 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5163 if (statusTracker != 0) {
5164 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5165 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005166 }
5167 // Stop waiting for now and let thread management happen
5168 return NULL;
5169 }
5170 }
5171
5172 if (nextRequest == NULL) {
5173 // Don't have a repeating request already in hand, so queue
5174 // must have an entry now.
5175 RequestList::iterator firstRequest =
5176 mRequestQueue.begin();
5177 nextRequest = *firstRequest;
5178 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005179 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5180 sp<NotificationListener> listener = mListener.promote();
5181 if (listener != NULL) {
5182 listener->notifyRequestQueueEmpty();
5183 }
5184 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005185 }
5186
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005187 // In case we've been unpaused by setPaused clearing mDoPause, need to
5188 // update internal pause state (capture/setRepeatingRequest unpause
5189 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005190 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005191 if (mPaused) {
5192 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5193 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5194 if (statusTracker != 0) {
5195 statusTracker->markComponentActive(mStatusId);
5196 }
5197 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005198 mPaused = false;
5199
5200 // Check if we've reconfigured since last time, and reset the preview
5201 // request if so. Can't use 'NULL request == repeat' across configure calls.
5202 if (mReconfigured) {
5203 mPrevRequest.clear();
5204 mReconfigured = false;
5205 }
5206
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005207 if (nextRequest != NULL) {
5208 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005209 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5210 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005211
5212 // Since RequestThread::clear() removes buffers from the input stream,
5213 // get the right buffer here before unlocking mRequestLock
5214 if (nextRequest->mInputStream != NULL) {
5215 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5216 if (res != OK) {
5217 // Can't get input buffer from gralloc queue - this could be due to
5218 // disconnected queue or other producer misbehavior, so not a fatal
5219 // error
5220 ALOGE("%s: Can't get input buffer, skipping request:"
5221 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005222
5223 sp<NotificationListener> listener = mListener.promote();
5224 if (listener != NULL) {
5225 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005226 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005227 nextRequest->mResultExtras);
5228 }
5229 return NULL;
5230 }
5231 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005232 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005234 return nextRequest;
5235}
5236
5237bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005238 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005239 status_t res;
5240 Mutex::Autolock l(mPauseLock);
5241 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005242 if (mPaused == false) {
5243 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005244 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5245 // Let the tracker know
5246 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5247 if (statusTracker != 0) {
5248 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5249 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005250 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005251
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005252 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005253 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005254 return true;
5255 }
5256 }
5257 // We don't set mPaused to false here, because waitForNextRequest needs
5258 // to further manage the paused state in case of starvation.
5259 return false;
5260}
5261
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005262void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005263 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005264 // With work to do, mark thread as unpaused.
5265 // If paused by request (setPaused), don't resume, to avoid
5266 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005267 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005268 Mutex::Autolock p(mPauseLock);
5269 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005270 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5271 if (mPaused) {
5272 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5273 if (statusTracker != 0) {
5274 statusTracker->markComponentActive(mStatusId);
5275 }
5276 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005277 mPaused = false;
5278 }
5279}
5280
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005281void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5282 sp<Camera3Device> parent = mParent.promote();
5283 if (parent != NULL) {
5284 va_list args;
5285 va_start(args, fmt);
5286
5287 parent->setErrorStateV(fmt, args);
5288
5289 va_end(args);
5290 }
5291}
5292
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005293status_t Camera3Device::RequestThread::insertTriggers(
5294 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005295 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005296 Mutex::Autolock al(mTriggerMutex);
5297
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005298 sp<Camera3Device> parent = mParent.promote();
5299 if (parent == NULL) {
5300 CLOGE("RequestThread: Parent is gone");
5301 return DEAD_OBJECT;
5302 }
5303
Emilian Peevaebbe412018-01-15 13:53:24 +00005304 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005305 size_t count = mTriggerMap.size();
5306
5307 for (size_t i = 0; i < count; ++i) {
5308 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005309 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005310
5311 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5312 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5313 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005314 if (isAeTrigger) {
5315 request->mResultExtras.precaptureTriggerId = triggerId;
5316 mCurrentPreCaptureTriggerId = triggerId;
5317 } else {
5318 request->mResultExtras.afTriggerId = triggerId;
5319 mCurrentAfTriggerId = triggerId;
5320 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005321 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005322 }
5323
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005324 camera_metadata_entry entry = metadata.find(tag);
5325
5326 if (entry.count > 0) {
5327 /**
5328 * Already has an entry for this trigger in the request.
5329 * Rewrite it with our requested trigger value.
5330 */
5331 RequestTrigger oldTrigger = trigger;
5332
5333 oldTrigger.entryValue = entry.data.u8[0];
5334
5335 mTriggerReplacedMap.add(tag, oldTrigger);
5336 } else {
5337 /**
5338 * More typical, no trigger entry, so we just add it
5339 */
5340 mTriggerRemovedMap.add(tag, trigger);
5341 }
5342
5343 status_t res;
5344
5345 switch (trigger.getTagType()) {
5346 case TYPE_BYTE: {
5347 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5348 res = metadata.update(tag,
5349 &entryValue,
5350 /*count*/1);
5351 break;
5352 }
5353 case TYPE_INT32:
5354 res = metadata.update(tag,
5355 &trigger.entryValue,
5356 /*count*/1);
5357 break;
5358 default:
5359 ALOGE("%s: Type not supported: 0x%x",
5360 __FUNCTION__,
5361 trigger.getTagType());
5362 return INVALID_OPERATION;
5363 }
5364
5365 if (res != OK) {
5366 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5367 ", value %d", __FUNCTION__, trigger.getTagName(),
5368 trigger.entryValue);
5369 return res;
5370 }
5371
5372 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5373 trigger.getTagName(),
5374 trigger.entryValue);
5375 }
5376
5377 mTriggerMap.clear();
5378
5379 return count;
5380}
5381
5382status_t Camera3Device::RequestThread::removeTriggers(
5383 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005384 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005385 Mutex::Autolock al(mTriggerMutex);
5386
Emilian Peevaebbe412018-01-15 13:53:24 +00005387 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005388
5389 /**
5390 * Replace all old entries with their old values.
5391 */
5392 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5393 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5394
5395 status_t res;
5396
5397 uint32_t tag = trigger.metadataTag;
5398 switch (trigger.getTagType()) {
5399 case TYPE_BYTE: {
5400 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5401 res = metadata.update(tag,
5402 &entryValue,
5403 /*count*/1);
5404 break;
5405 }
5406 case TYPE_INT32:
5407 res = metadata.update(tag,
5408 &trigger.entryValue,
5409 /*count*/1);
5410 break;
5411 default:
5412 ALOGE("%s: Type not supported: 0x%x",
5413 __FUNCTION__,
5414 trigger.getTagType());
5415 return INVALID_OPERATION;
5416 }
5417
5418 if (res != OK) {
5419 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5420 ", trigger value %d", __FUNCTION__,
5421 trigger.getTagName(), trigger.entryValue);
5422 return res;
5423 }
5424 }
5425 mTriggerReplacedMap.clear();
5426
5427 /**
5428 * Remove all new entries.
5429 */
5430 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5431 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5432 status_t res = metadata.erase(trigger.metadataTag);
5433
5434 if (res != OK) {
5435 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5436 ", trigger value %d", __FUNCTION__,
5437 trigger.getTagName(), trigger.entryValue);
5438 return res;
5439 }
5440 }
5441 mTriggerRemovedMap.clear();
5442
5443 return OK;
5444}
5445
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005446status_t Camera3Device::RequestThread::addDummyTriggerIds(
5447 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005448 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005449 static const int32_t dummyTriggerId = 1;
5450 status_t res;
5451
Emilian Peevaebbe412018-01-15 13:53:24 +00005452 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005453
5454 // If AF trigger is active, insert a dummy AF trigger ID if none already
5455 // exists
5456 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5457 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5458 if (afTrigger.count > 0 &&
5459 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5460 afId.count == 0) {
5461 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5462 if (res != OK) return res;
5463 }
5464
5465 // If AE precapture trigger is active, insert a dummy precapture trigger ID
5466 // if none already exists
5467 camera_metadata_entry pcTrigger =
5468 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5469 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5470 if (pcTrigger.count > 0 &&
5471 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5472 pcId.count == 0) {
5473 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5474 &dummyTriggerId, 1);
5475 if (res != OK) return res;
5476 }
5477
5478 return OK;
5479}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005480
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005481/**
5482 * PreparerThread inner class methods
5483 */
5484
5485Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005486 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005487 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005488}
5489
5490Camera3Device::PreparerThread::~PreparerThread() {
5491 Thread::requestExitAndWait();
5492 if (mCurrentStream != nullptr) {
5493 mCurrentStream->cancelPrepare();
5494 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5495 mCurrentStream.clear();
5496 }
5497 clear();
5498}
5499
Ruben Brunkc78ac262015-08-13 17:58:46 -07005500status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005501 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005502 status_t res;
5503
5504 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005505 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005506
Ruben Brunkc78ac262015-08-13 17:58:46 -07005507 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005508 if (res == OK) {
5509 // No preparation needed, fire listener right off
5510 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005511 if (listener != NULL) {
5512 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005513 }
5514 return OK;
5515 } else if (res != NOT_ENOUGH_DATA) {
5516 return res;
5517 }
5518
5519 // Need to prepare, start up thread if necessary
5520 if (!mActive) {
5521 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5522 // isn't running
5523 Thread::requestExitAndWait();
5524 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5525 if (res != OK) {
5526 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005527 if (listener != NULL) {
5528 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005529 }
5530 return res;
5531 }
5532 mCancelNow = false;
5533 mActive = true;
5534 ALOGV("%s: Preparer stream started", __FUNCTION__);
5535 }
5536
5537 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005538 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005539 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5540
5541 return OK;
5542}
5543
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005544void Camera3Device::PreparerThread::pause() {
5545 ATRACE_CALL();
5546
5547 Mutex::Autolock l(mLock);
5548
5549 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5550 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5551 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5552 int currentMaxCount = mCurrentMaxCount;
5553 mPendingStreams.clear();
5554 mCancelNow = true;
5555 while (mActive) {
5556 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5557 if (res == TIMED_OUT) {
5558 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5559 return;
5560 } else if (res != OK) {
5561 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5562 return;
5563 }
5564 }
5565
5566 //Check whether the prepare thread was able to complete the current
5567 //stream. In case work is still pending emplace it along with the rest
5568 //of the streams in the pending list.
5569 if (currentStream != nullptr) {
5570 if (!mCurrentPrepareComplete) {
5571 pendingStreams.emplace(currentMaxCount, currentStream);
5572 }
5573 }
5574
5575 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5576 for (const auto& it : mPendingStreams) {
5577 it.second->cancelPrepare();
5578 }
5579}
5580
5581status_t Camera3Device::PreparerThread::resume() {
5582 ATRACE_CALL();
5583 status_t res;
5584
5585 Mutex::Autolock l(mLock);
5586 sp<NotificationListener> listener = mListener.promote();
5587
5588 if (mActive) {
5589 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5590 return NO_INIT;
5591 }
5592
5593 auto it = mPendingStreams.begin();
5594 for (; it != mPendingStreams.end();) {
5595 res = it->second->startPrepare(it->first);
5596 if (res == OK) {
5597 if (listener != NULL) {
5598 listener->notifyPrepared(it->second->getId());
5599 }
5600 it = mPendingStreams.erase(it);
5601 } else if (res != NOT_ENOUGH_DATA) {
5602 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5603 res, strerror(-res));
5604 it = mPendingStreams.erase(it);
5605 } else {
5606 it++;
5607 }
5608 }
5609
5610 if (mPendingStreams.empty()) {
5611 return OK;
5612 }
5613
5614 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5615 if (res != OK) {
5616 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5617 __FUNCTION__, res, strerror(-res));
5618 return res;
5619 }
5620 mCancelNow = false;
5621 mActive = true;
5622 ALOGV("%s: Preparer stream started", __FUNCTION__);
5623
5624 return OK;
5625}
5626
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005627status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005628 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005629 Mutex::Autolock l(mLock);
5630
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005631 for (const auto& it : mPendingStreams) {
5632 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005633 }
5634 mPendingStreams.clear();
5635 mCancelNow = true;
5636
5637 return OK;
5638}
5639
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005640void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005641 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005642 Mutex::Autolock l(mLock);
5643 mListener = listener;
5644}
5645
5646bool Camera3Device::PreparerThread::threadLoop() {
5647 status_t res;
5648 {
5649 Mutex::Autolock l(mLock);
5650 if (mCurrentStream == nullptr) {
5651 // End thread if done with work
5652 if (mPendingStreams.empty()) {
5653 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5654 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5655 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5656 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005657 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005658 return false;
5659 }
5660
5661 // Get next stream to prepare
5662 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005663 mCurrentStream = it->second;
5664 mCurrentMaxCount = it->first;
5665 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005666 mPendingStreams.erase(it);
5667 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5668 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5669 } else if (mCancelNow) {
5670 mCurrentStream->cancelPrepare();
5671 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5672 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5673 mCurrentStream.clear();
5674 mCancelNow = false;
5675 return true;
5676 }
5677 }
5678
5679 res = mCurrentStream->prepareNextBuffer();
5680 if (res == NOT_ENOUGH_DATA) return true;
5681 if (res != OK) {
5682 // Something bad happened; try to recover by cancelling prepare and
5683 // signalling listener anyway
5684 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5685 mCurrentStream->getId(), res, strerror(-res));
5686 mCurrentStream->cancelPrepare();
5687 }
5688
5689 // This stream has finished, notify listener
5690 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005691 sp<NotificationListener> listener = mListener.promote();
5692 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005693 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5694 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005695 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005696 }
5697
5698 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5699 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005700 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005701
5702 return true;
5703}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005704
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005705/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005706 * Static callback forwarding methods from HAL to instance
5707 */
5708
5709void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5710 const camera3_capture_result *result) {
5711 Camera3Device *d =
5712 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07005713
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005714 d->processCaptureResult(result);
5715}
5716
5717void Camera3Device::sNotify(const camera3_callback_ops *cb,
5718 const camera3_notify_msg *msg) {
5719 Camera3Device *d =
5720 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5721 d->notify(msg);
5722}
5723
5724}; // namespace android