blob: 999e258491e8e46ad8e11948f58660dd32564136 [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
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070055#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080059using namespace android::hardware::camera;
60using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62namespace android {
63
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080065 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080066 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070067 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070069 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070070 mUsePartialResult(false),
71 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070074 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070076 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000077 mListener(NULL),
78 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080079{
80 ATRACE_CALL();
81 camera3_callback_ops::notify = &sNotify;
82 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080083 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084}
85
86Camera3Device::~Camera3Device()
87{
88 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080089 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 disconnect();
91}
92
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080094 return mId;
95}
96
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080097status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
98 ATRACE_CALL();
99 Mutex::Autolock il(mInterfaceLock);
100 Mutex::Autolock l(mLock);
101
102 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
103 if (mStatus != STATUS_UNINITIALIZED) {
104 CLOGE("Already initialized!");
105 return INVALID_OPERATION;
106 }
107 if (manager == nullptr) return INVALID_OPERATION;
108
109 sp<ICameraDeviceSession> session;
110 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800111 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800112 /*out*/ &session);
113 ATRACE_END();
114 if (res != OK) {
115 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
116 return res;
117 }
118
Steven Moreland5ff9c912017-03-09 23:13:00 -0800119 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120 if (res != OK) {
121 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
122 session->close();
123 return res;
124 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800125
Yifan Hongf79b5542017-04-11 14:44:25 -0700126 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700127 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
128 [&queue](const auto& descriptor) {
129 queue = std::make_shared<RequestMetadataQueue>(descriptor);
130 if (!queue->isValid() || queue->availableToWrite() <= 0) {
131 ALOGE("HAL returns empty request metadata fmq, not use it");
132 queue = nullptr;
133 // don't use the queue onwards.
134 }
135 });
136 if (!requestQueueRet.isOk()) {
137 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
138 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700139 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700140 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700141
142 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700143 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700144 [&resQueue](const auto& descriptor) {
145 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
146 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700147 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700148 resQueue = nullptr;
149 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700150 }
151 });
152 if (!resultQueueRet.isOk()) {
153 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
154 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700155 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700156 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700157 IF_ALOGV() {
158 session->interfaceChain([](
159 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
160 ALOGV("Session interface chain:");
161 for (auto iface : interfaceChain) {
162 ALOGV(" %s", iface.c_str());
163 }
164 });
165 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700166
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700167 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000168 std::string providerType;
169 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800170
171 return initializeCommonLocked();
172}
173
174status_t Camera3Device::initializeCommonLocked() {
175
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700176 /** Start up status tracker thread */
177 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800178 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 if (res != OK) {
180 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
181 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800182 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 mStatusTracker.clear();
184 return res;
185 }
186
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700187 /** Register in-flight map to the status tracker */
188 mInFlightStatusId = mStatusTracker->addComponent();
189
Zhijun He125684a2015-12-26 15:07:30 -0800190 /** Create buffer manager */
191 mBufferManager = new Camera3BufferManager();
192
Emilian Peev71c73a22017-03-21 16:35:51 +0000193 mTagMonitor.initialize(mVendorTagId);
194
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000195 Vector<int32_t> sessionParamKeys;
196 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
197 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
198 if (sessionKeysEntry.count > 0) {
199 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
200 }
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700201 /** Start up request queue thread */
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000202 mRequestThread = new RequestThread(this, mStatusTracker, mInterface, sessionParamKeys);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800203 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700205 SET_ERR_L("Unable to start request queue thread: %s (%d)",
206 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800207 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800208 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800209 return res;
210 }
211
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700212 mPreparerThread = new PreparerThread();
213
Ruben Brunk183f0562015-08-12 12:55:02 -0700214 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700216 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700217 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800220 // Measure the clock domain offset between camera and video/hw_composer
221 camera_metadata_entry timestampSource =
222 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
223 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
224 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
225 mTimestampOffset = getMonoToBoottimeOffset();
226 }
227
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700228 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100229 camera_metadata_entry partialResultsCount =
230 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
231 if (partialResultsCount.count > 0) {
232 mNumPartialResults = partialResultsCount.data.i32[0];
233 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700234 }
235
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700236 camera_metadata_entry configs =
237 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
238 for (uint32_t i = 0; i < configs.count; i += 4) {
239 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
240 configs.data.i32[i + 3] ==
241 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
242 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
243 configs.data.i32[i + 2]));
244 }
245 }
246
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 return OK;
248}
249
250status_t Camera3Device::disconnect() {
251 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700252 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800253
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700254 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800255
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700256 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700257 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700258 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 {
260 Mutex::Autolock l(mLock);
261 if (mStatus == STATUS_UNINITIALIZED) return res;
262
263 if (mStatus == STATUS_ACTIVE ||
264 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
265 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700266 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700268 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700270 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700272 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
273 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 // Continue to close device even in case of error
275 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700276 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800278
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700279 if (mStatus == STATUS_ERROR) {
280 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700282
283 if (mStatusTracker != NULL) {
284 mStatusTracker->requestExit();
285 }
286
287 if (mRequestThread != NULL) {
288 mRequestThread->requestExit();
289 }
290
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700291 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
292 for (size_t i = 0; i < mOutputStreams.size(); i++) {
293 streams.push_back(mOutputStreams[i]);
294 }
295 if (mInputStream != nullptr) {
296 streams.push_back(mInputStream);
297 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 }
299
300 // Joining done without holding mLock, otherwise deadlocks may ensue
301 // as the threads try to access parent state
302 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
303 // HAL may be in a bad state, so waiting for request thread
304 // (which may be stuck in the HAL processCaptureRequest call)
305 // could be dangerous.
306 mRequestThread->join();
307 }
308
309 if (mStatusTracker != NULL) {
310 mStatusTracker->join();
311 }
312
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800313 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314 {
315 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800316 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700317 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800318 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700319 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800320
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700321 // Call close without internal mutex held, as the HAL close may need to
322 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800323 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700324
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700325 flushInflightRequests();
326
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700327 {
328 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800329 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700330 mOutputStreams.clear();
331 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700332 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700333 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700334 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700335 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700337 for (auto& weakStream : streams) {
338 sp<Camera3StreamInterface> stream = weakStream.promote();
339 if (stream != nullptr) {
340 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
341 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
342 }
343 }
344
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700345 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700346 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800347}
348
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700349// For dumping/debugging only -
350// try to acquire a lock a few times, eventually give up to proceed with
351// debug/dump operations
352bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
353 bool gotLock = false;
354 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
355 if (lock.tryLock() == NO_ERROR) {
356 gotLock = true;
357 break;
358 } else {
359 usleep(kDumpSleepDuration);
360 }
361 }
362 return gotLock;
363}
364
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700365Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
366 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100367 const int STREAM_CONFIGURATION_SIZE = 4;
368 const int STREAM_FORMAT_OFFSET = 0;
369 const int STREAM_WIDTH_OFFSET = 1;
370 const int STREAM_HEIGHT_OFFSET = 2;
371 const int STREAM_IS_INPUT_OFFSET = 3;
372 camera_metadata_ro_entry_t availableStreamConfigs =
373 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
374 if (availableStreamConfigs.count == 0 ||
375 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
376 return Size(0, 0);
377 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700378
Emilian Peev08dd2452017-04-06 16:55:14 +0100379 // Get max jpeg size (area-wise).
380 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
381 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
382 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
383 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
384 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
385 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
386 && format == HAL_PIXEL_FORMAT_BLOB &&
387 (width * height > maxJpegWidth * maxJpegHeight)) {
388 maxJpegWidth = width;
389 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700390 }
391 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100392
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700393 return Size(maxJpegWidth, maxJpegHeight);
394}
395
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800396nsecs_t Camera3Device::getMonoToBoottimeOffset() {
397 // try three times to get the clock offset, choose the one
398 // with the minimum gap in measurements.
399 const int tries = 3;
400 nsecs_t bestGap, measured;
401 for (int i = 0; i < tries; ++i) {
402 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
403 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
404 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
405 const nsecs_t gap = tmono2 - tmono;
406 if (i == 0 || gap < bestGap) {
407 bestGap = gap;
408 measured = tbase - ((tmono + tmono2) >> 1);
409 }
410 }
411 return measured;
412}
413
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800414hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
415 int frameworkFormat) {
416 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
417}
418
419DataspaceFlags Camera3Device::mapToHidlDataspace(
420 android_dataspace dataSpace) {
421 return dataSpace;
422}
423
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700424BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100425 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700426 return usage;
427}
428
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800429StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
430 switch (rotation) {
431 case CAMERA3_STREAM_ROTATION_0:
432 return StreamRotation::ROTATION_0;
433 case CAMERA3_STREAM_ROTATION_90:
434 return StreamRotation::ROTATION_90;
435 case CAMERA3_STREAM_ROTATION_180:
436 return StreamRotation::ROTATION_180;
437 case CAMERA3_STREAM_ROTATION_270:
438 return StreamRotation::ROTATION_270;
439 }
440 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
441 return StreamRotation::ROTATION_0;
442}
443
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800444status_t Camera3Device::mapToStreamConfigurationMode(
445 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
446 if (mode == nullptr) return BAD_VALUE;
447 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
448 switch(operationMode) {
449 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
450 *mode = StreamConfigurationMode::NORMAL_MODE;
451 break;
452 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
453 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
454 break;
455 default:
456 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
457 return BAD_VALUE;
458 }
459 } else {
460 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800461 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800462 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800463}
464
465camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
466 switch (status) {
467 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
468 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
469 }
470 return CAMERA3_BUFFER_STATUS_ERROR;
471}
472
473int Camera3Device::mapToFrameworkFormat(
474 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
475 return static_cast<uint32_t>(pixelFormat);
476}
477
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700478android_dataspace Camera3Device::mapToFrameworkDataspace(
479 DataspaceFlags dataSpace) {
480 return static_cast<android_dataspace>(dataSpace);
481}
482
Emilian Peev050f5dc2017-05-18 14:43:56 +0100483uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700484 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700485 return usage;
486}
487
Emilian Peev050f5dc2017-05-18 14:43:56 +0100488uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700489 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700490 return usage;
491}
492
Zhijun Hef7da0962014-04-24 13:27:56 -0700493ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700494 // Get max jpeg size (area-wise).
495 Size maxJpegResolution = getMaxJpegResolution();
496 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800497 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
498 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700499 return BAD_VALUE;
500 }
501
Zhijun Hef7da0962014-04-24 13:27:56 -0700502 // Get max jpeg buffer size
503 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700504 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
505 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800506 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
507 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700508 return BAD_VALUE;
509 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700510 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800511 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700512
513 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700514 float scaleFactor = ((float) (width * height)) /
515 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800516 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
517 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700518 if (jpegBufferSize > maxJpegBufferSize) {
519 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700520 }
521
522 return jpegBufferSize;
523}
524
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700525ssize_t Camera3Device::getPointCloudBufferSize() const {
526 const int FLOATS_PER_POINT=4;
527 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
528 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800529 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
530 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700531 return BAD_VALUE;
532 }
533 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
534 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
535 return maxBytesForPointCloud;
536}
537
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800538ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800539 const int PER_CONFIGURATION_SIZE = 3;
540 const int WIDTH_OFFSET = 0;
541 const int HEIGHT_OFFSET = 1;
542 const int SIZE_OFFSET = 2;
543 camera_metadata_ro_entry rawOpaqueSizes =
544 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800545 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800546 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800547 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
548 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800549 return BAD_VALUE;
550 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700551
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800552 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
553 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
554 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
555 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
556 }
557 }
558
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800559 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
560 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800561 return BAD_VALUE;
562}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700563
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800564status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
565 ATRACE_CALL();
566 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700567
568 // Try to lock, but continue in case of failure (to avoid blocking in
569 // deadlocks)
570 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
571 bool gotLock = tryLockSpinRightRound(mLock);
572
573 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800574 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
575 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700576 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800577 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
578 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700579
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800580 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700581
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800582 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700583 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800584 int n = args.size();
585 for (int i = 0; i < n; i++) {
586 if (args[i] == templatesOption) {
587 dumpTemplates = true;
588 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700589 if (args[i] == monitorOption) {
590 if (i + 1 < n) {
591 String8 monitorTags = String8(args[i + 1]);
592 if (monitorTags == "off") {
593 mTagMonitor.disableMonitoring();
594 } else {
595 mTagMonitor.parseTagsToMonitor(monitorTags);
596 }
597 } else {
598 mTagMonitor.disableMonitoring();
599 }
600 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800601 }
602
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800604
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800605 const char *status =
606 mStatus == STATUS_ERROR ? "ERROR" :
607 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
609 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800610 mStatus == STATUS_ACTIVE ? "ACTIVE" :
611 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700614 if (mStatus == STATUS_ERROR) {
615 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
616 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800618 const char *mode =
619 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
620 mOperatingMode == static_cast<int>(
621 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
622 "CUSTOM";
623 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800624
625 if (mInputStream != NULL) {
626 write(fd, lines.string(), lines.size());
627 mInputStream->dump(fd, args);
628 } else {
629 lines.appendFormat(" No input stream.\n");
630 write(fd, lines.string(), lines.size());
631 }
632 for (size_t i = 0; i < mOutputStreams.size(); i++) {
633 mOutputStreams[i]->dump(fd,args);
634 }
635
Zhijun He431503c2016-03-07 17:30:16 -0800636 if (mBufferManager != NULL) {
637 lines = String8(" Camera3 Buffer Manager:\n");
638 write(fd, lines.string(), lines.size());
639 mBufferManager->dump(fd, args);
640 }
Zhijun He125684a2015-12-26 15:07:30 -0800641
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700642 lines = String8(" In-flight requests:\n");
643 if (mInFlightMap.size() == 0) {
644 lines.append(" None\n");
645 } else {
646 for (size_t i = 0; i < mInFlightMap.size(); i++) {
647 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700648 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700649 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800650 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700651 r.numBuffersLeft);
652 }
653 }
654 write(fd, lines.string(), lines.size());
655
Shuzhen Wang686f6442017-06-20 16:16:04 -0700656 if (mRequestThread != NULL) {
657 mRequestThread->dumpCaptureRequestLatency(fd,
658 " ProcessCaptureRequest latency histogram:");
659 }
660
Igor Murashkin1e479c02013-09-06 16:55:14 -0700661 {
662 lines = String8(" Last request sent:\n");
663 write(fd, lines.string(), lines.size());
664
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700666 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
667 }
668
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800669 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800670 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800671 "TEMPLATE_PREVIEW",
672 "TEMPLATE_STILL_CAPTURE",
673 "TEMPLATE_VIDEO_RECORD",
674 "TEMPLATE_VIDEO_SNAPSHOT",
675 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800676 "TEMPLATE_MANUAL",
677 "TEMPLATE_MOTION_TRACKING_PREVIEW",
678 "TEMPALTE_MOTION_TRACKING_BEST"
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800679 };
680
681 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800682 camera_metadata_t *templateRequest = nullptr;
683 mInterface->constructDefaultRequestSettings(
684 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800685 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800686 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800687 lines.append(" Not supported\n");
688 write(fd, lines.string(), lines.size());
689 } else {
690 write(fd, lines.string(), lines.size());
691 dump_indented_camera_metadata(templateRequest,
692 fd, /*verbosity*/2, /*indentation*/8);
693 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800694 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800695 }
696 }
697
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700698 mTagMonitor.dumpMonitoredMetadata(fd);
699
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800700 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800701 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800702 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800703 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800704 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800705
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 if (gotLock) mLock.unlock();
707 if (gotInterfaceLock) mInterfaceLock.unlock();
708
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800709 return OK;
710}
711
712const CameraMetadata& Camera3Device::info() const {
713 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
715 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700716 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800717 mStatus == STATUS_ERROR ?
718 "when in error state" : "before init");
719 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800720 return mDeviceInfo;
721}
722
Jianing Wei90e59c92014-03-12 18:29:36 -0700723status_t Camera3Device::checkStatusOkToCaptureLocked() {
724 switch (mStatus) {
725 case STATUS_ERROR:
726 CLOGE("Device has encountered a serious error");
727 return INVALID_OPERATION;
728 case STATUS_UNINITIALIZED:
729 CLOGE("Device not initialized");
730 return INVALID_OPERATION;
731 case STATUS_UNCONFIGURED:
732 case STATUS_CONFIGURED:
733 case STATUS_ACTIVE:
734 // OK
735 break;
736 default:
737 SET_ERR_L("Unexpected status: %d", mStatus);
738 return INVALID_OPERATION;
739 }
740 return OK;
741}
742
743status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000744 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700745 const std::list<const SurfaceMap> &surfaceMaps,
746 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700747 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700748 if (requestList == NULL) {
749 CLOGE("requestList cannot be NULL.");
750 return BAD_VALUE;
751 }
752
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000754 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700755 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
756 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
757 ++metadataIt, ++surfaceMapIt) {
758 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700759 if (newRequest == 0) {
760 CLOGE("Can't create capture request");
761 return BAD_VALUE;
762 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700763
Shuzhen Wang9d066012016-09-30 11:30:20 -0700764 newRequest->mRepeating = repeating;
765
Jianing Weicb0652e2014-03-12 18:29:36 -0700766 // Setup burst Id and request Id
767 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000768 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
769 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700770 CLOGE("RequestID entry exists; but must not be empty in metadata");
771 return BAD_VALUE;
772 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000773 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
774 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700775 } else {
776 CLOGE("RequestID does not exist in metadata");
777 return BAD_VALUE;
778 }
779
Jianing Wei90e59c92014-03-12 18:29:36 -0700780 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700781
782 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700783 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700784 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
785 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
786 return BAD_VALUE;
787 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700788
789 // Setup batch size if this is a high speed video recording request.
790 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
791 auto firstRequest = requestList->begin();
792 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
793 if (outputStream->isVideoStream()) {
794 (*firstRequest)->mBatchSize = requestList->size();
795 break;
796 }
797 }
798 }
799
Jianing Wei90e59c92014-03-12 18:29:36 -0700800 return OK;
801}
802
Jianing Weicb0652e2014-03-12 18:29:36 -0700803status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800804 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800805
Emilian Peevaebbe412018-01-15 13:53:24 +0000806 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700807 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000808 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700809
Emilian Peevaebbe412018-01-15 13:53:24 +0000810 return captureList(requestsList, surfaceMaps, /*lastFrameNumber*/NULL);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700811}
812
Emilian Peevaebbe412018-01-15 13:53:24 +0000813void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700814 std::list<const SurfaceMap>& surfaceMaps,
815 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000816 PhysicalCameraSettingsList requestList;
817 requestList.push_back({std::string(getId().string()), request});
818 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700819
820 SurfaceMap surfaceMap;
821 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
822 // With no surface list passed in, stream and surface will have 1-to-1
823 // mapping. So the surface index is 0 for each stream in the surfaceMap.
824 for (size_t i = 0; i < streams.count; i++) {
825 surfaceMap[streams.data.i32[i]].push_back(0);
826 }
827 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800828}
829
Jianing Wei90e59c92014-03-12 18:29:36 -0700830status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000831 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 const std::list<const SurfaceMap> &surfaceMaps,
833 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700834 /*out*/
835 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700836 ATRACE_CALL();
837 Mutex::Autolock il(mInterfaceLock);
838 Mutex::Autolock l(mLock);
839
840 status_t res = checkStatusOkToCaptureLocked();
841 if (res != OK) {
842 // error logged by previous call
843 return res;
844 }
845
846 RequestList requestList;
847
Shuzhen Wang0129d522016-10-30 22:43:41 -0700848 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
849 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700850 if (res != OK) {
851 // error logged by previous call
852 return res;
853 }
854
855 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700856 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700858 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700859 }
860
861 if (res == OK) {
862 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
863 if (res != OK) {
864 SET_ERR_L("Can't transition to active in %f seconds!",
865 kActiveTimeout/1e9);
866 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800867 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700868 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700869 } else {
870 CLOGE("Cannot queue request. Impossible.");
871 return BAD_VALUE;
872 }
873
874 return res;
875}
876
Yifan Honga640c5a2017-04-12 16:30:31 -0700877// Only one processCaptureResult should be called at a time, so
878// the locks won't block. The locks are present here simply to enforce this.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800879hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800880 const hardware::hidl_vec<
881 hardware::camera::device::V3_2::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700882 // Ideally we should grab mLock, but that can lead to deadlock, and
883 // it's not super important to get up to date value of mStatus for this
884 // warning print, hence skipping the lock here
885 if (mStatus == STATUS_ERROR) {
886 // Per API contract, HAL should act as closed after device error
887 // But mStatus can be set to error by framework as well, so just log
888 // a warning here.
889 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700890 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700891
892 if (mProcessCaptureResultLock.tryLock() != OK) {
893 // This should never happen; it indicates a wrong client implementation
894 // that doesn't follow the contract. But, we can be tolerant here.
895 ALOGE("%s: callback overlapped! waiting 1s...",
896 __FUNCTION__);
897 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
898 ALOGE("%s: cannot acquire lock in 1s, dropping results",
899 __FUNCTION__);
900 // really don't know what to do, so bail out.
901 return hardware::Void();
902 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800903 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700904 for (const auto& result : results) {
905 processOneCaptureResultLocked(result);
906 }
907 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800908 return hardware::Void();
909}
910
Yifan Honga640c5a2017-04-12 16:30:31 -0700911void Camera3Device::processOneCaptureResultLocked(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800912 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800913 camera3_capture_result r;
914 status_t res;
915 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700916
917 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
918 if (result.fmqResultSize > 0) {
919 resultMetadata.resize(result.fmqResultSize);
920 if (mResultMetadataQueue == nullptr) {
921 return; // logged in initialize()
922 }
923 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
924 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
925 __FUNCTION__, result.frameNumber, result.fmqResultSize);
926 return;
927 }
928 } else {
929 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
930 result.result.size());
931 }
932
933 if (resultMetadata.size() != 0) {
934 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
935 size_t expected_metadata_size = resultMetadata.size();
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800936 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
937 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
938 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800939 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800940 }
941 } else {
942 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800943 }
944
945 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
946 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
947 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
948 auto& bDst = outputBuffers[i];
949 const StreamBuffer &bSrc = result.outputBuffers[i];
950
951 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100952 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800953 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
954 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800955 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800956 }
957 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
958
959 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800960 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800961 if (res != OK) {
962 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
963 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800964 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800965 }
966 bDst.buffer = buffer;
967 bDst.status = mapHidlBufferStatus(bSrc.status);
968 bDst.acquire_fence = -1;
969 if (bSrc.releaseFence == nullptr) {
970 bDst.release_fence = -1;
971 } else if (bSrc.releaseFence->numFds == 1) {
972 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
973 } else {
974 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
975 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800976 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800977 }
978 }
979 r.num_output_buffers = outputBuffers.size();
980 r.output_buffers = outputBuffers.data();
981
982 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800983 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800984 r.input_buffer = nullptr;
985 } else {
986 if (mInputStream->getId() != result.inputBuffer.streamId) {
987 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
988 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800989 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800990 }
991 inputBuffer.stream = mInputStream->asHalStream();
992 buffer_handle_t *buffer;
993 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
994 &buffer);
995 if (res != OK) {
996 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
997 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800998 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800999 }
1000 inputBuffer.buffer = buffer;
1001 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1002 inputBuffer.acquire_fence = -1;
1003 if (result.inputBuffer.releaseFence == nullptr) {
1004 inputBuffer.release_fence = -1;
1005 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1006 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1007 } else {
1008 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1009 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001010 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001011 }
1012 r.input_buffer = &inputBuffer;
1013 }
1014
1015 r.partial_result = result.partialResult;
1016
1017 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001018}
1019
1020hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001021 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001022 // Ideally we should grab mLock, but that can lead to deadlock, and
1023 // it's not super important to get up to date value of mStatus for this
1024 // warning print, hence skipping the lock here
1025 if (mStatus == STATUS_ERROR) {
1026 // Per API contract, HAL should act as closed after device error
1027 // But mStatus can be set to error by framework as well, so just log
1028 // a warning here.
1029 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001030 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001031
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001032 for (const auto& msg : msgs) {
1033 notify(msg);
1034 }
1035 return hardware::Void();
1036}
1037
1038void Camera3Device::notify(
1039 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1040
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001041 camera3_notify_msg m;
1042 switch (msg.type) {
1043 case MsgType::ERROR:
1044 m.type = CAMERA3_MSG_ERROR;
1045 m.message.error.frame_number = msg.msg.error.frameNumber;
1046 if (msg.msg.error.errorStreamId >= 0) {
1047 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001048 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001049 ALOGE("%s: Frame %d: Invalid error stream id %d",
1050 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001051 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001052 }
1053 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1054 } else {
1055 m.message.error.error_stream = nullptr;
1056 }
1057 switch (msg.msg.error.errorCode) {
1058 case ErrorCode::ERROR_DEVICE:
1059 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1060 break;
1061 case ErrorCode::ERROR_REQUEST:
1062 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1063 break;
1064 case ErrorCode::ERROR_RESULT:
1065 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1066 break;
1067 case ErrorCode::ERROR_BUFFER:
1068 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1069 break;
1070 }
1071 break;
1072 case MsgType::SHUTTER:
1073 m.type = CAMERA3_MSG_SHUTTER;
1074 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1075 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1076 break;
1077 }
1078 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001079}
1080
Emilian Peevaebbe412018-01-15 13:53:24 +00001081status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001082 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001083 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001084 ATRACE_CALL();
1085
Emilian Peevaebbe412018-01-15 13:53:24 +00001086 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001087}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001088
Jianing Weicb0652e2014-03-12 18:29:36 -07001089status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1090 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001091 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092
Emilian Peevaebbe412018-01-15 13:53:24 +00001093 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001094 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001095 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001096
Emilian Peevaebbe412018-01-15 13:53:24 +00001097 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001098 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099}
1100
Emilian Peevaebbe412018-01-15 13:53:24 +00001101status_t Camera3Device::setStreamingRequestList(
1102 const List<const PhysicalCameraSettingsList> &requestsList,
1103 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001104 ATRACE_CALL();
1105
Emilian Peevaebbe412018-01-15 13:53:24 +00001106 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001107}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001108
1109sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001110 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 status_t res;
1112
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001113 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001114 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1115 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001116 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1117 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001118 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001120 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001122 } else if (mStatus == STATUS_UNCONFIGURED) {
1123 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 CLOGE("No streams configured");
1125 return NULL;
1126 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127 }
1128
Shuzhen Wang0129d522016-10-30 22:43:41 -07001129 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131}
1132
Jianing Weicb0652e2014-03-12 18:29:36 -07001133status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001134 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 switch (mStatus) {
1139 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001140 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 return INVALID_OPERATION;
1142 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001143 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 case STATUS_UNCONFIGURED:
1146 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001147 case STATUS_ACTIVE:
1148 // OK
1149 break;
1150 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001151 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152 return INVALID_OPERATION;
1153 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001154 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001155
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001156 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157}
1158
1159status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1160 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001162
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001163 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164}
1165
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001166status_t Camera3Device::createInputStream(
1167 uint32_t width, uint32_t height, int format, int *id) {
1168 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001169 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001170 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001171 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001172 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1173 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001174
1175 status_t res;
1176 bool wasActive = false;
1177
1178 switch (mStatus) {
1179 case STATUS_ERROR:
1180 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1181 return INVALID_OPERATION;
1182 case STATUS_UNINITIALIZED:
1183 ALOGE("%s: Device not initialized", __FUNCTION__);
1184 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001185 case STATUS_UNCONFIGURED:
1186 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001187 // OK
1188 break;
1189 case STATUS_ACTIVE:
1190 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001191 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001192 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001193 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001194 return res;
1195 }
1196 wasActive = true;
1197 break;
1198 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001200 return INVALID_OPERATION;
1201 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001202 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001203
1204 if (mInputStream != 0) {
1205 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1206 return INVALID_OPERATION;
1207 }
1208
1209 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1210 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001211 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001212
1213 mInputStream = newStream;
1214
1215 *id = mNextStreamId++;
1216
1217 // Continue captures if active at start
1218 if (wasActive) {
1219 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001220 // Reuse current operating mode and session parameters for new stream config
1221 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001222 if (res != OK) {
1223 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1224 __FUNCTION__, mNextStreamId, strerror(-res), res);
1225 return res;
1226 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001227 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001228 }
1229
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001230 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001231 return OK;
1232}
1233
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001234status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001235 uint32_t width, uint32_t height, int format,
1236 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001237 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001238 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001239 ATRACE_CALL();
1240
1241 if (consumer == nullptr) {
1242 ALOGE("%s: consumer must not be null", __FUNCTION__);
1243 return BAD_VALUE;
1244 }
1245
1246 std::vector<sp<Surface>> consumers;
1247 consumers.push_back(consumer);
1248
1249 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001250 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1251 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001252}
1253
1254status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1255 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1256 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001257 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001258 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001260
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001261 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001262 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001264 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001265 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1266 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1267 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269 status_t res;
1270 bool wasActive = false;
1271
1272 switch (mStatus) {
1273 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 return INVALID_OPERATION;
1276 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 case STATUS_UNCONFIGURED:
1280 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 // OK
1282 break;
1283 case STATUS_ACTIVE:
1284 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001285 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001288 return res;
1289 }
1290 wasActive = true;
1291 break;
1292 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001293 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001294 return INVALID_OPERATION;
1295 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001296 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297
1298 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001299
Shuzhen Wang0129d522016-10-30 22:43:41 -07001300 if (consumers.size() == 0 && !hasDeferredConsumer) {
1301 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1302 return BAD_VALUE;
1303 }
Zhijun He5d677d12016-05-29 16:52:39 -07001304
Shuzhen Wang0129d522016-10-30 22:43:41 -07001305 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001306 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1307 return BAD_VALUE;
1308 }
1309
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001311 ssize_t blobBufferSize;
1312 if (dataSpace != HAL_DATASPACE_DEPTH) {
1313 blobBufferSize = getJpegBufferSize(width, height);
1314 if (blobBufferSize <= 0) {
1315 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1316 return BAD_VALUE;
1317 }
1318 } else {
1319 blobBufferSize = getPointCloudBufferSize();
1320 if (blobBufferSize <= 0) {
1321 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1322 return BAD_VALUE;
1323 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001324 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001325 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001326 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001327 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001328 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1329 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1330 if (rawOpaqueBufferSize <= 0) {
1331 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1332 return BAD_VALUE;
1333 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001334 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001335 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001336 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001337 } else if (isShared) {
1338 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1339 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001340 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001341 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001342 newStream = new Camera3OutputStream(mNextStreamId,
1343 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001344 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001346 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001347 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001348 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001349 }
Emilian Peev40ead602017-09-26 15:46:36 +01001350
1351 size_t consumerCount = consumers.size();
1352 for (size_t i = 0; i < consumerCount; i++) {
1353 int id = newStream->getSurfaceId(consumers[i]);
1354 if (id < 0) {
1355 SET_ERR_L("Invalid surface id");
1356 return BAD_VALUE;
1357 }
1358 if (surfaceIds != nullptr) {
1359 surfaceIds->push_back(id);
1360 }
1361 }
1362
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001363 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364
Emilian Peev08dd2452017-04-06 16:55:14 +01001365 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001366
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 res = mOutputStreams.add(mNextStreamId, newStream);
1368 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 return res;
1371 }
1372
1373 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001374 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001375
1376 // Continue captures if active at start
1377 if (wasActive) {
1378 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001379 // Reuse current operating mode and session parameters for new stream config
1380 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001382 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1383 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 return res;
1385 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001386 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001388 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001390}
1391
Emilian Peev710c1422017-08-30 11:19:38 +01001392status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001394 if (nullptr == streamInfo) {
1395 return BAD_VALUE;
1396 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001398 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 switch (mStatus) {
1401 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001402 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 return INVALID_OPERATION;
1404 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001405 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001407 case STATUS_UNCONFIGURED:
1408 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 case STATUS_ACTIVE:
1410 // OK
1411 break;
1412 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001413 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001414 return INVALID_OPERATION;
1415 }
1416
1417 ssize_t idx = mOutputStreams.indexOfKey(id);
1418 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001419 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001420 return idx;
1421 }
1422
Emilian Peev710c1422017-08-30 11:19:38 +01001423 streamInfo->width = mOutputStreams[idx]->getWidth();
1424 streamInfo->height = mOutputStreams[idx]->getHeight();
1425 streamInfo->format = mOutputStreams[idx]->getFormat();
1426 streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace();
1427 streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden();
1428 streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat();
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07001429 streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden();
1430 streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001431 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001432}
1433
1434status_t Camera3Device::setStreamTransform(int id,
1435 int transform) {
1436 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001437 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001439
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 switch (mStatus) {
1441 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001442 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001443 return INVALID_OPERATION;
1444 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001445 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001446 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001447 case STATUS_UNCONFIGURED:
1448 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001449 case STATUS_ACTIVE:
1450 // OK
1451 break;
1452 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001453 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001454 return INVALID_OPERATION;
1455 }
1456
1457 ssize_t idx = mOutputStreams.indexOfKey(id);
1458 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001459 CLOGE("Stream %d does not exist",
1460 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001461 return BAD_VALUE;
1462 }
1463
1464 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001465}
1466
1467status_t Camera3Device::deleteStream(int id) {
1468 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001469 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001470 Mutex::Autolock l(mLock);
1471 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001472
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001473 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001474
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001475 // CameraDevice semantics require device to already be idle before
1476 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001477 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001478 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001479 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001480 }
1481
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001482 if (mStatus == STATUS_ERROR) {
1483 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1484 __FUNCTION__, mId.string());
1485 return -EBUSY;
1486 }
1487
Igor Murashkin2fba5842013-04-22 14:03:54 -07001488 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001489 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001490 if (mInputStream != NULL && id == mInputStream->getId()) {
1491 deletedStream = mInputStream;
1492 mInputStream.clear();
1493 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001494 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001495 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496 return BAD_VALUE;
1497 }
Zhijun He5f446352014-01-22 09:49:33 -08001498 }
1499
1500 // Delete output stream or the output part of a bi-directional stream.
1501 if (outputStreamIdx != NAME_NOT_FOUND) {
1502 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001503 mOutputStreams.removeItem(id);
1504 }
1505
1506 // Free up the stream endpoint so that it can be used by some other stream
1507 res = deletedStream->disconnect();
1508 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001509 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 // fall through since we want to still list the stream as deleted.
1511 }
1512 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001513 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001514
1515 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001516}
1517
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001518status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001519 ATRACE_CALL();
1520 ALOGV("%s: E", __FUNCTION__);
1521
1522 Mutex::Autolock il(mInterfaceLock);
1523 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001524
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001525 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1526}
1527
1528status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1529 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001530 //Filter out any incoming session parameters
1531 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001532 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1533 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001534 CameraMetadata filteredParams(availableSessionKeys.count);
1535 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1536 filteredParams.getAndLock());
1537 set_camera_metadata_vendor_id(meta, mVendorTagId);
1538 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001539 if (availableSessionKeys.count > 0) {
1540 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1541 camera_metadata_ro_entry entry = params.find(
1542 availableSessionKeys.data.i32[i]);
1543 if (entry.count > 0) {
1544 filteredParams.update(entry);
1545 }
1546 }
1547 }
1548
1549 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001550}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001551
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001552status_t Camera3Device::getInputBufferProducer(
1553 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001554 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001555 Mutex::Autolock il(mInterfaceLock);
1556 Mutex::Autolock l(mLock);
1557
1558 if (producer == NULL) {
1559 return BAD_VALUE;
1560 } else if (mInputStream == NULL) {
1561 return INVALID_OPERATION;
1562 }
1563
1564 return mInputStream->getInputBufferProducer(producer);
1565}
1566
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001567status_t Camera3Device::createDefaultRequest(int templateId,
1568 CameraMetadata *request) {
1569 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001570 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001571
1572 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1573 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1574 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1575 return BAD_VALUE;
1576 }
1577
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001578 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001580 {
1581 Mutex::Autolock l(mLock);
1582 switch (mStatus) {
1583 case STATUS_ERROR:
1584 CLOGE("Device has encountered a serious error");
1585 return INVALID_OPERATION;
1586 case STATUS_UNINITIALIZED:
1587 CLOGE("Device is not initialized!");
1588 return INVALID_OPERATION;
1589 case STATUS_UNCONFIGURED:
1590 case STATUS_CONFIGURED:
1591 case STATUS_ACTIVE:
1592 // OK
1593 break;
1594 default:
1595 SET_ERR_L("Unexpected status: %d", mStatus);
1596 return INVALID_OPERATION;
1597 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001598
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001599 if (!mRequestTemplateCache[templateId].isEmpty()) {
1600 *request = mRequestTemplateCache[templateId];
1601 return OK;
1602 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001603 }
1604
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001605 camera_metadata_t *rawRequest;
1606 status_t res = mInterface->constructDefaultRequestSettings(
1607 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001608
1609 {
1610 Mutex::Autolock l(mLock);
1611 if (res == BAD_VALUE) {
1612 ALOGI("%s: template %d is not supported on this camera device",
1613 __FUNCTION__, templateId);
1614 return res;
1615 } else if (res != OK) {
1616 CLOGE("Unable to construct request template %d: %s (%d)",
1617 templateId, strerror(-res), res);
1618 return res;
1619 }
1620
1621 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1622 mRequestTemplateCache[templateId].acquire(rawRequest);
1623
1624 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001625 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001626 return OK;
1627}
1628
1629status_t Camera3Device::waitUntilDrained() {
1630 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001631 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001632 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001633 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001634
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001635 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001636}
1637
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001638status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001639 switch (mStatus) {
1640 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001641 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001642 ALOGV("%s: Already idle", __FUNCTION__);
1643 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001644 case STATUS_CONFIGURED:
1645 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646 case STATUS_ERROR:
1647 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001648 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001649 break;
1650 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001651 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 return INVALID_OPERATION;
1653 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001654 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1655 maxExpectedDuration);
1656 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001657 if (res != OK) {
1658 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1659 res);
1660 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001661 return res;
1662}
1663
Ruben Brunk183f0562015-08-12 12:55:02 -07001664
1665void Camera3Device::internalUpdateStatusLocked(Status status) {
1666 mStatus = status;
1667 mRecentStatusUpdates.add(mStatus);
1668 mStatusChanged.broadcast();
1669}
1670
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001671void Camera3Device::pauseStateNotify(bool enable) {
1672 Mutex::Autolock il(mInterfaceLock);
1673 Mutex::Autolock l(mLock);
1674
1675 mPauseStateNotify = enable;
1676}
1677
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001678// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001679status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001680 mRequestThread->setPaused(true);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001681
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001682 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1683 maxExpectedDuration);
1684 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001685 if (res != OK) {
1686 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001687 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001688 }
1689
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 return res;
1691}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001693// Resume after internalPauseAndWaitLocked
1694status_t Camera3Device::internalResumeLocked() {
1695 status_t res;
1696
1697 mRequestThread->setPaused(false);
1698
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001699 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1700 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001701 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1702 if (res != OK) {
1703 SET_ERR_L("Can't transition to active in %f seconds!",
1704 kActiveTimeout/1e9);
1705 }
1706 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001707 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001708}
1709
Ruben Brunk183f0562015-08-12 12:55:02 -07001710status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001711 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001712
1713 size_t startIndex = 0;
1714 if (mStatusWaiters == 0) {
1715 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1716 // this status list
1717 mRecentStatusUpdates.clear();
1718 } else {
1719 // If other threads are waiting on updates to this status list, set the position of the
1720 // first element that this list will check rather than clearing the list.
1721 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001722 }
1723
Ruben Brunk183f0562015-08-12 12:55:02 -07001724 mStatusWaiters++;
1725
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001726 bool stateSeen = false;
1727 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001728 if (active == (mStatus == STATUS_ACTIVE)) {
1729 // Desired state is current
1730 break;
1731 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001732
1733 res = mStatusChanged.waitRelative(mLock, timeout);
1734 if (res != OK) break;
1735
Ruben Brunk183f0562015-08-12 12:55:02 -07001736 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1737 // transitions.
1738 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1739 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1740 __FUNCTION__);
1741
1742 // Encountered desired state since we began waiting
1743 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001744 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1745 stateSeen = true;
1746 break;
1747 }
1748 }
1749 } while (!stateSeen);
1750
Ruben Brunk183f0562015-08-12 12:55:02 -07001751 mStatusWaiters--;
1752
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001753 return res;
1754}
1755
1756
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001757status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001758 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001759 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001761 if (listener != NULL && mListener != NULL) {
1762 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1763 }
1764 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001765 mRequestThread->setNotificationListener(listener);
1766 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001767
1768 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001769}
1770
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001771bool Camera3Device::willNotify3A() {
1772 return false;
1773}
1774
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001775status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001776 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001777 status_t res;
1778 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001779
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001780 while (mResultQueue.empty()) {
1781 res = mResultSignal.waitRelative(mOutputLock, timeout);
1782 if (res == TIMED_OUT) {
1783 return res;
1784 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001785 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1786 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001787 return res;
1788 }
1789 }
1790 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001791}
1792
Jianing Weicb0652e2014-03-12 18:29:36 -07001793status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001794 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001795 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001796
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001797 if (mResultQueue.empty()) {
1798 return NOT_ENOUGH_DATA;
1799 }
1800
Jianing Weicb0652e2014-03-12 18:29:36 -07001801 if (frame == NULL) {
1802 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1803 return BAD_VALUE;
1804 }
1805
1806 CaptureResult &result = *(mResultQueue.begin());
1807 frame->mResultExtras = result.mResultExtras;
1808 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001809 mResultQueue.erase(mResultQueue.begin());
1810
1811 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001812}
1813
1814status_t Camera3Device::triggerAutofocus(uint32_t id) {
1815 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001816 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001817
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001818 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1819 // Mix-in this trigger into the next request and only the next request.
1820 RequestTrigger trigger[] = {
1821 {
1822 ANDROID_CONTROL_AF_TRIGGER,
1823 ANDROID_CONTROL_AF_TRIGGER_START
1824 },
1825 {
1826 ANDROID_CONTROL_AF_TRIGGER_ID,
1827 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001828 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001829 };
1830
1831 return mRequestThread->queueTrigger(trigger,
1832 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001833}
1834
1835status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1836 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001837 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001838
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001839 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1840 // Mix-in this trigger into the next request and only the next request.
1841 RequestTrigger trigger[] = {
1842 {
1843 ANDROID_CONTROL_AF_TRIGGER,
1844 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1845 },
1846 {
1847 ANDROID_CONTROL_AF_TRIGGER_ID,
1848 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001849 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001850 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001851
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001852 return mRequestThread->queueTrigger(trigger,
1853 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001854}
1855
1856status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1857 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001858 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001859
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001860 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1861 // Mix-in this trigger into the next request and only the next request.
1862 RequestTrigger trigger[] = {
1863 {
1864 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1865 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1866 },
1867 {
1868 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1869 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001870 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001871 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001872
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001873 return mRequestThread->queueTrigger(trigger,
1874 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001875}
1876
Jianing Weicb0652e2014-03-12 18:29:36 -07001877status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001878 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001879 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001880 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001881
Zhijun He7ef20392014-04-21 16:04:17 -07001882 {
1883 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001884 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001885 }
1886
Emilian Peev08dd2452017-04-06 16:55:14 +01001887 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001888}
1889
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001890status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001891 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1892}
1893
1894status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001895 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001896 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001897 Mutex::Autolock il(mInterfaceLock);
1898 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001899
1900 sp<Camera3StreamInterface> stream;
1901 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1902 if (outputStreamIdx == NAME_NOT_FOUND) {
1903 CLOGE("Stream %d does not exist", streamId);
1904 return BAD_VALUE;
1905 }
1906
1907 stream = mOutputStreams.editValueAt(outputStreamIdx);
1908
1909 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001910 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001911 return BAD_VALUE;
1912 }
1913
1914 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001915 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001916 return BAD_VALUE;
1917 }
1918
Ruben Brunkc78ac262015-08-13 17:58:46 -07001919 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001920}
1921
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001922status_t Camera3Device::tearDown(int streamId) {
1923 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001924 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001925 Mutex::Autolock il(mInterfaceLock);
1926 Mutex::Autolock l(mLock);
1927
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001928 sp<Camera3StreamInterface> stream;
1929 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1930 if (outputStreamIdx == NAME_NOT_FOUND) {
1931 CLOGE("Stream %d does not exist", streamId);
1932 return BAD_VALUE;
1933 }
1934
1935 stream = mOutputStreams.editValueAt(outputStreamIdx);
1936
1937 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1938 CLOGE("Stream %d is a target of a in-progress request", streamId);
1939 return BAD_VALUE;
1940 }
1941
1942 return stream->tearDown();
1943}
1944
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001945status_t Camera3Device::addBufferListenerForStream(int streamId,
1946 wp<Camera3StreamBufferListener> listener) {
1947 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001948 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001949 Mutex::Autolock il(mInterfaceLock);
1950 Mutex::Autolock l(mLock);
1951
1952 sp<Camera3StreamInterface> stream;
1953 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1954 if (outputStreamIdx == NAME_NOT_FOUND) {
1955 CLOGE("Stream %d does not exist", streamId);
1956 return BAD_VALUE;
1957 }
1958
1959 stream = mOutputStreams.editValueAt(outputStreamIdx);
1960 stream->addBufferListener(listener);
1961
1962 return OK;
1963}
1964
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 * Methods called by subclasses
1967 */
1968
1969void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001970 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001971 {
1972 // Need mLock to safely update state and synchronize to current
1973 // state of methods in flight.
1974 Mutex::Autolock l(mLock);
1975 // We can get various system-idle notices from the status tracker
1976 // while starting up. Only care about them if we've actually sent
1977 // in some requests recently.
1978 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1979 return;
1980 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001981 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1982 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001983 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001984
1985 // Skip notifying listener if we're doing some user-transparent
1986 // state changes
1987 if (mPauseStateNotify) return;
1988 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001989
1990 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991 {
1992 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001993 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001994 }
1995 if (idle && listener != NULL) {
1996 listener->notifyIdle();
1997 }
1998}
1999
Shuzhen Wang758c2152017-01-10 18:26:18 -08002000status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002001 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002002 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002003 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2004 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002005
2006 if (surfaceIds == nullptr) {
2007 return BAD_VALUE;
2008 }
2009
Zhijun He5d677d12016-05-29 16:52:39 -07002010 Mutex::Autolock il(mInterfaceLock);
2011 Mutex::Autolock l(mLock);
2012
Shuzhen Wang758c2152017-01-10 18:26:18 -08002013 if (consumers.size() == 0) {
2014 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002015 return BAD_VALUE;
2016 }
2017
2018 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2019 if (idx == NAME_NOT_FOUND) {
2020 CLOGE("Stream %d is unknown", streamId);
2021 return idx;
2022 }
2023 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002024 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002025 if (res != OK) {
2026 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2027 return res;
2028 }
2029
Emilian Peev40ead602017-09-26 15:46:36 +01002030 for (auto &consumer : consumers) {
2031 int id = stream->getSurfaceId(consumer);
2032 if (id < 0) {
2033 CLOGE("Invalid surface id!");
2034 return BAD_VALUE;
2035 }
2036 surfaceIds->push_back(id);
2037 }
2038
Shuzhen Wang0129d522016-10-30 22:43:41 -07002039 if (stream->isConsumerConfigurationDeferred()) {
2040 if (!stream->isConfiguring()) {
2041 CLOGE("Stream %d was already fully configured.", streamId);
2042 return INVALID_OPERATION;
2043 }
Zhijun He5d677d12016-05-29 16:52:39 -07002044
Shuzhen Wang0129d522016-10-30 22:43:41 -07002045 res = stream->finishConfiguration();
2046 if (res != OK) {
2047 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2048 stream->getId(), strerror(-res), res);
2049 return res;
2050 }
Zhijun He5d677d12016-05-29 16:52:39 -07002051 }
2052
2053 return OK;
2054}
2055
Emilian Peev40ead602017-09-26 15:46:36 +01002056status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2057 const std::vector<OutputStreamInfo> &outputInfo,
2058 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2059 Mutex::Autolock il(mInterfaceLock);
2060 Mutex::Autolock l(mLock);
2061
2062 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2063 if (idx == NAME_NOT_FOUND) {
2064 CLOGE("Stream %d is unknown", streamId);
2065 return idx;
2066 }
2067
2068 for (const auto &it : removedSurfaceIds) {
2069 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2070 CLOGE("Shared surface still part of a pending request!");
2071 return -EBUSY;
2072 }
2073 }
2074
2075 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
2076 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2077 if (res != OK) {
2078 CLOGE("Stream %d failed to update stream (error %d %s) ",
2079 streamId, res, strerror(-res));
2080 if (res == UNKNOWN_ERROR) {
2081 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2082 __FUNCTION__);
2083 }
2084 return res;
2085 }
2086
2087 return res;
2088}
2089
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002090status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2091 Mutex::Autolock il(mInterfaceLock);
2092 Mutex::Autolock l(mLock);
2093
2094 int idx = mOutputStreams.indexOfKey(streamId);
2095 if (idx == NAME_NOT_FOUND) {
2096 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2097 return BAD_VALUE;
2098 }
2099
2100 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx);
2101 return stream->dropBuffers(dropping);
2102}
2103
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002104/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002105 * Camera3Device private methods
2106 */
2107
2108sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002109 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110 ATRACE_CALL();
2111 status_t res;
2112
2113 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002114 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002115
2116 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002117 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118 if (inputStreams.count > 0) {
2119 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002120 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002121 CLOGE("Request references unknown input stream %d",
2122 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123 return NULL;
2124 }
2125 // Lazy completion of stream configuration (allocation/registration)
2126 // on first use
2127 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002128 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002130 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002131 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002132 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002133 return NULL;
2134 }
2135 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002136 // Check if stream is being prepared
2137 if (mInputStream->isPreparing()) {
2138 CLOGE("Request references an input stream that's being prepared!");
2139 return NULL;
2140 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141
2142 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002143 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002144 }
2145
2146 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002147 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002148 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002149 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002150 return NULL;
2151 }
2152
2153 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002154 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002155 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002156 CLOGE("Request references unknown stream %d",
2157 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002158 return NULL;
2159 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002160 sp<Camera3OutputStreamInterface> stream =
2161 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002162
Zhijun He5d677d12016-05-29 16:52:39 -07002163 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002164 auto iter = surfaceMap.find(streams.data.i32[i]);
2165 if (iter != surfaceMap.end()) {
2166 const std::vector<size_t>& surfaces = iter->second;
2167 for (const auto& surface : surfaces) {
2168 if (stream->isConsumerConfigurationDeferred(surface)) {
2169 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2170 "due to deferred consumer", stream->getId(), surface);
2171 return NULL;
2172 }
2173 }
2174 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002175 }
2176
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002177 // Lazy completion of stream configuration (allocation/registration)
2178 // on first use
2179 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002180 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002181 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002182 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2183 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002184 return NULL;
2185 }
2186 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002187 // Check if stream is being prepared
2188 if (stream->isPreparing()) {
2189 CLOGE("Request references an output stream that's being prepared!");
2190 return NULL;
2191 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002192
2193 newRequest->mOutputStreams.push(stream);
2194 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002195 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002196 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002197
2198 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002199}
2200
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002201bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2202 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2203 Size size = mSupportedOpaqueInputSizes[i];
2204 if (size.width == width && size.height == height) {
2205 return true;
2206 }
2207 }
2208
2209 return false;
2210}
2211
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002212void Camera3Device::cancelStreamsConfigurationLocked() {
2213 int res = OK;
2214 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2215 res = mInputStream->cancelConfiguration();
2216 if (res != OK) {
2217 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2218 mInputStream->getId(), strerror(-res), res);
2219 }
2220 }
2221
2222 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2223 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2224 if (outputStream->isConfiguring()) {
2225 res = outputStream->cancelConfiguration();
2226 if (res != OK) {
2227 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2228 outputStream->getId(), strerror(-res), res);
2229 }
2230 }
2231 }
2232
2233 // Return state to that at start of call, so that future configures
2234 // properly clean things up
2235 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2236 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002237
2238 res = mPreparerThread->resume();
2239 if (res != OK) {
2240 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2241 }
2242}
2243
2244bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2245 ATRACE_CALL();
2246 bool ret = false;
2247
2248 Mutex::Autolock il(mInterfaceLock);
2249 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2250
2251 Mutex::Autolock l(mLock);
2252 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2253 if (rc == NO_ERROR) {
2254 mNeedConfig = true;
2255 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2256 if (rc == NO_ERROR) {
2257 ret = true;
2258 mPauseStateNotify = false;
2259 //Moving to active state while holding 'mLock' is important.
2260 //There could be pending calls to 'create-/deleteStream' which
2261 //will trigger another stream configuration while the already
2262 //present streams end up with outstanding buffers that will
2263 //not get drained.
2264 internalUpdateStatusLocked(STATUS_ACTIVE);
2265 } else {
2266 setErrorStateLocked("%s: Failed to re-configure camera: %d",
2267 __FUNCTION__, rc);
2268 }
2269 } else {
2270 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2271 }
2272
2273 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002274}
2275
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002276status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002277 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278 ATRACE_CALL();
2279 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002281 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002282 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002283 return INVALID_OPERATION;
2284 }
2285
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002286 if (operatingMode < 0) {
2287 CLOGE("Invalid operating mode: %d", operatingMode);
2288 return BAD_VALUE;
2289 }
2290
2291 bool isConstrainedHighSpeed =
2292 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2293 operatingMode;
2294
2295 if (mOperatingMode != operatingMode) {
2296 mNeedConfig = true;
2297 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2298 mOperatingMode = operatingMode;
2299 }
2300
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002301 if (!mNeedConfig) {
2302 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2303 return OK;
2304 }
2305
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002306 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2307 // adding a dummy stream instead.
2308 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2309 if (mOutputStreams.size() == 0) {
2310 addDummyStreamLocked();
2311 } else {
2312 tryRemoveDummyStreamLocked();
2313 }
2314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002315 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002316 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002317
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002318 mPreparerThread->pause();
2319
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002320 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002321 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002322 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2323
2324 Vector<camera3_stream_t*> streams;
2325 streams.setCapacity(config.num_streams);
2326
2327 if (mInputStream != NULL) {
2328 camera3_stream_t *inputStream;
2329 inputStream = mInputStream->startConfiguration();
2330 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002331 CLOGE("Can't start input stream configuration");
2332 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333 return INVALID_OPERATION;
2334 }
2335 streams.add(inputStream);
2336 }
2337
2338 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002339
2340 // Don't configure bidi streams twice, nor add them twice to the list
2341 if (mOutputStreams[i].get() ==
2342 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2343
2344 config.num_streams--;
2345 continue;
2346 }
2347
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002348 camera3_stream_t *outputStream;
2349 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2350 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002351 CLOGE("Can't start output stream configuration");
2352 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002353 return INVALID_OPERATION;
2354 }
2355 streams.add(outputStream);
2356 }
2357
2358 config.streams = streams.editArray();
2359
2360 // Do the HAL configuration; will potentially touch stream
2361 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002362
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002363 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
2364 res = mInterface->configureStreams(sessionBuffer, &config);
2365 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002366
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002367 if (res == BAD_VALUE) {
2368 // HAL rejected this set of streams as unsupported, clean up config
2369 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002370 CLOGE("Set of requested inputs/outputs not supported by HAL");
2371 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002372 return BAD_VALUE;
2373 } else if (res != OK) {
2374 // Some other kind of error from configure_streams - this is not
2375 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002376 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2377 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002378 return res;
2379 }
2380
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002381 // Finish all stream configuration immediately.
2382 // TODO: Try to relax this later back to lazy completion, which should be
2383 // faster
2384
Igor Murashkin073f8572013-05-02 14:59:28 -07002385 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002386 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002387 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002388 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002389 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002390 cancelStreamsConfigurationLocked();
2391 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002392 }
2393 }
2394
2395 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002396 sp<Camera3OutputStreamInterface> outputStream =
2397 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002398 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002399 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002400 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002401 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002402 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002403 cancelStreamsConfigurationLocked();
2404 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002405 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002406 }
2407 }
2408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 // Request thread needs to know to avoid using repeat-last-settings protocol
2410 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002411 if (notifyRequestThread) {
2412 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2413 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002414
Zhijun He90f7c372016-08-16 16:19:43 -07002415 char value[PROPERTY_VALUE_MAX];
2416 property_get("camera.fifo.disable", value, "0");
2417 int32_t disableFifo = atoi(value);
2418 if (disableFifo != 1) {
2419 // Boost priority of request thread to SCHED_FIFO.
2420 pid_t requestThreadTid = mRequestThread->getTid();
2421 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002422 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002423 if (res != OK) {
2424 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2425 strerror(-res), res);
2426 } else {
2427 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2428 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002429 }
2430
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002431 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002432 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2433 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2434 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2435 sessionParams.unlock(newSessionParams);
2436 mSessionParams.unlock(currentSessionParams);
2437 if (updateSessionParams) {
2438 mSessionParams = sessionParams;
2439 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002440
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002441 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002442
Ruben Brunk183f0562015-08-12 12:55:02 -07002443 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2444 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002445
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002446 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002447
Zhijun He0a210512014-07-24 13:45:15 -07002448 // tear down the deleted streams after configure streams.
2449 mDeletedStreams.clear();
2450
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002451 auto rc = mPreparerThread->resume();
2452 if (rc != OK) {
2453 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2454 return rc;
2455 }
2456
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002457 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002458}
2459
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002460status_t Camera3Device::addDummyStreamLocked() {
2461 ATRACE_CALL();
2462 status_t res;
2463
2464 if (mDummyStreamId != NO_STREAM) {
2465 // Should never be adding a second dummy stream when one is already
2466 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002467 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2468 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002469 return INVALID_OPERATION;
2470 }
2471
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002472 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002473
2474 sp<Camera3OutputStreamInterface> dummyStream =
2475 new Camera3DummyStream(mNextStreamId);
2476
2477 res = mOutputStreams.add(mNextStreamId, dummyStream);
2478 if (res < 0) {
2479 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2480 return res;
2481 }
2482
2483 mDummyStreamId = mNextStreamId;
2484 mNextStreamId++;
2485
2486 return OK;
2487}
2488
2489status_t Camera3Device::tryRemoveDummyStreamLocked() {
2490 ATRACE_CALL();
2491 status_t res;
2492
2493 if (mDummyStreamId == NO_STREAM) return OK;
2494 if (mOutputStreams.size() == 1) return OK;
2495
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002496 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002497
2498 // Ok, have a dummy stream and there's at least one other output stream,
2499 // so remove the dummy
2500
2501 sp<Camera3StreamInterface> deletedStream;
2502 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2503 if (outputStreamIdx == NAME_NOT_FOUND) {
2504 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2505 return INVALID_OPERATION;
2506 }
2507
2508 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2509 mOutputStreams.removeItemsAt(outputStreamIdx);
2510
2511 // Free up the stream endpoint so that it can be used by some other stream
2512 res = deletedStream->disconnect();
2513 if (res != OK) {
2514 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2515 // fall through since we want to still list the stream as deleted.
2516 }
2517 mDeletedStreams.add(deletedStream);
2518 mDummyStreamId = NO_STREAM;
2519
2520 return res;
2521}
2522
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002523void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002524 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002525 Mutex::Autolock l(mLock);
2526 va_list args;
2527 va_start(args, fmt);
2528
2529 setErrorStateLockedV(fmt, args);
2530
2531 va_end(args);
2532}
2533
2534void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002535 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002536 Mutex::Autolock l(mLock);
2537 setErrorStateLockedV(fmt, args);
2538}
2539
2540void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2541 va_list args;
2542 va_start(args, fmt);
2543
2544 setErrorStateLockedV(fmt, args);
2545
2546 va_end(args);
2547}
2548
2549void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002550 // Print out all error messages to log
2551 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002552 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002553
2554 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002555 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002556
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002557 mErrorCause = errorCause;
2558
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002559 if (mRequestThread != nullptr) {
2560 mRequestThread->setPaused(true);
2561 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002562 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002563
2564 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002565 sp<NotificationListener> listener = mListener.promote();
2566 if (listener != NULL) {
2567 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002568 CaptureResultExtras());
2569 }
2570
2571 // Save stack trace. View by dumping it later.
2572 CameraTraces::saveTrace();
2573 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002574}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002575
2576/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002577 * In-flight request management
2578 */
2579
Jianing Weicb0652e2014-03-12 18:29:36 -07002580status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002581 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002582 bool hasAppCallback, nsecs_t maxExpectedDuration) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002583 ATRACE_CALL();
2584 Mutex::Autolock l(mInFlightLock);
2585
2586 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002587 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002588 hasAppCallback, maxExpectedDuration));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002589 if (res < 0) return res;
2590
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002591 if (mInFlightMap.size() == 1) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002592 // hold mLock to prevent race with disconnect
2593 Mutex::Autolock l(mLock);
2594 if (mStatusTracker != nullptr) {
2595 mStatusTracker->markComponentActive(mInFlightStatusId);
2596 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002597 }
2598
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002599 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002600 return OK;
2601}
2602
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002603void Camera3Device::returnOutputBuffers(
2604 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2605 nsecs_t timestamp) {
2606 for (size_t i = 0; i < numBuffers; i++)
2607 {
2608 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2609 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2610 // Note: stream may be deallocated at this point, if this buffer was
2611 // the last reference to it.
2612 if (res != OK) {
2613 ALOGE("Can't return buffer to its stream: %s (%d)",
2614 strerror(-res), res);
2615 }
2616 }
2617}
2618
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002619void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002620 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002621 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002622 mInFlightMap.removeItemsAt(idx, 1);
2623
2624 // Indicate idle inFlightMap to the status tracker
2625 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002626 // hold mLock to prevent race with disconnect
2627 Mutex::Autolock l(mLock);
2628 if (mStatusTracker != nullptr) {
2629 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2630 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002631 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002632 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002633}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002634
2635void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2636
2637 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2638 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2639
2640 nsecs_t sensorTimestamp = request.sensorTimestamp;
2641 nsecs_t shutterTimestamp = request.shutterTimestamp;
2642
2643 // Check if it's okay to remove the request from InFlightMap:
2644 // In the case of a successful request:
2645 // all input and output buffers, all result metadata, shutter callback
2646 // arrived.
2647 // In the case of a unsuccessful request:
2648 // all input and output buffers arrived.
2649 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07002650 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002651 (request.haveResultMetadata && shutterTimestamp != 0))) {
2652 ATRACE_ASYNC_END("frame capture", frameNumber);
2653
Shuzhen Wang403044a2017-02-26 23:29:04 -08002654 // Sanity check - if sensor timestamp matches shutter timestamp in the
2655 // case of request having callback.
2656 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002657 sensorTimestamp != shutterTimestamp) {
2658 SET_ERR("sensor timestamp (%" PRId64
2659 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2660 sensorTimestamp, frameNumber, shutterTimestamp);
2661 }
2662
2663 // for an unsuccessful request, it may have pending output buffers to
2664 // return.
2665 assert(request.requestStatus != OK ||
2666 request.pendingOutputBuffers.size() == 0);
2667 returnOutputBuffers(request.pendingOutputBuffers.array(),
2668 request.pendingOutputBuffers.size(), 0);
2669
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002670 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002671 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2672 }
2673
2674 // Sanity check - if we have too many in-flight frames, something has
2675 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002676 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002677 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002678 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2679 kInFlightWarnLimitHighSpeed) {
2680 CLOGE("In-flight list too large for high speed configuration: %zu",
2681 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002682 }
2683}
2684
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002685void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002686 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002687 { // First return buffers cached in mInFlightMap
2688 Mutex::Autolock l(mInFlightLock);
2689 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2690 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2691 returnOutputBuffers(request.pendingOutputBuffers.array(),
2692 request.pendingOutputBuffers.size(), 0);
2693 }
2694 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002695 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002696 }
2697
2698 // Then return all inflight buffers not returned by HAL
2699 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2700 mInterface->getInflightBufferKeys(&inflightKeys);
2701
2702 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2703 for (auto& pair : inflightKeys) {
2704 int32_t frameNumber = pair.first;
2705 int32_t streamId = pair.second;
2706 buffer_handle_t* buffer;
2707 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2708 if (res != OK) {
2709 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2710 __FUNCTION__, frameNumber, streamId);
2711 continue;
2712 }
2713
2714 camera3_stream_buffer_t streamBuffer;
2715 streamBuffer.buffer = buffer;
2716 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2717 streamBuffer.acquire_fence = -1;
2718 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002719
2720 // First check if the buffer belongs to deleted stream
2721 bool streamDeleted = false;
2722 for (auto& stream : mDeletedStreams) {
2723 if (streamId == stream->getId()) {
2724 streamDeleted = true;
2725 // Return buffer to deleted stream
2726 camera3_stream* halStream = stream->asHalStream();
2727 streamBuffer.stream = halStream;
2728 switch (halStream->stream_type) {
2729 case CAMERA3_STREAM_OUTPUT:
2730 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2731 if (res != OK) {
2732 ALOGE("%s: Can't return output buffer for frame %d to"
2733 " stream %d: %s (%d)", __FUNCTION__,
2734 frameNumber, streamId, strerror(-res), res);
2735 }
2736 break;
2737 case CAMERA3_STREAM_INPUT:
2738 res = stream->returnInputBuffer(streamBuffer);
2739 if (res != OK) {
2740 ALOGE("%s: Can't return input buffer for frame %d to"
2741 " stream %d: %s (%d)", __FUNCTION__,
2742 frameNumber, streamId, strerror(-res), res);
2743 }
2744 break;
2745 default: // Bi-direcitonal stream is deprecated
2746 ALOGE("%s: stream %d has unknown stream type %d",
2747 __FUNCTION__, streamId, halStream->stream_type);
2748 break;
2749 }
2750 break;
2751 }
2752 }
2753 if (streamDeleted) {
2754 continue;
2755 }
2756
2757 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002758 if (streamId == inputStreamId) {
2759 streamBuffer.stream = mInputStream->asHalStream();
2760 res = mInputStream->returnInputBuffer(streamBuffer);
2761 if (res != OK) {
2762 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002763 " stream %d: %s (%d)", __FUNCTION__,
2764 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002765 }
2766 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002767 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2768 if (idx == NAME_NOT_FOUND) {
2769 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2770 continue;
2771 }
2772 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002773 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2774 }
2775 }
2776}
2777
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002778void Camera3Device::insertResultLocked(CaptureResult *result,
2779 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002780 if (result == nullptr) return;
2781
Emilian Peev71c73a22017-03-21 16:35:51 +00002782 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2783 result->mMetadata.getAndLock());
2784 set_camera_metadata_vendor_id(meta, mVendorTagId);
2785 result->mMetadata.unlock(meta);
2786
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002787 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2788 (int32_t*)&frameNumber, 1) != OK) {
2789 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2790 return;
2791 }
2792
2793 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2794 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2795 return;
2796 }
2797
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002798 // Valid result, insert into queue
2799 List<CaptureResult>::iterator queuedResult =
2800 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2801 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2802 ", burstId = %" PRId32, __FUNCTION__,
2803 queuedResult->mResultExtras.requestId,
2804 queuedResult->mResultExtras.frameNumber,
2805 queuedResult->mResultExtras.burstId);
2806
2807 mResultSignal.signal();
2808}
2809
2810
2811void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002812 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002813 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002814 Mutex::Autolock l(mOutputLock);
2815
2816 CaptureResult captureResult;
2817 captureResult.mResultExtras = resultExtras;
2818 captureResult.mMetadata = partialResult;
2819
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002820 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002821}
2822
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002823
2824void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2825 CaptureResultExtras &resultExtras,
2826 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002827 uint32_t frameNumber,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002828 bool reprocess) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002829 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002830 if (pendingMetadata.isEmpty())
2831 return;
2832
2833 Mutex::Autolock l(mOutputLock);
2834
2835 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002836 if (reprocess) {
2837 if (frameNumber < mNextReprocessResultFrameNumber) {
2838 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002839 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002840 frameNumber, mNextReprocessResultFrameNumber);
2841 return;
2842 }
2843 mNextReprocessResultFrameNumber = frameNumber + 1;
2844 } else {
2845 if (frameNumber < mNextResultFrameNumber) {
2846 SET_ERR("Out-of-order capture result metadata submitted! "
2847 "(got frame number %d, expecting %d)",
2848 frameNumber, mNextResultFrameNumber);
2849 return;
2850 }
2851 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002852 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002853
2854 CaptureResult captureResult;
2855 captureResult.mResultExtras = resultExtras;
2856 captureResult.mMetadata = pendingMetadata;
2857
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002858 // Append any previous partials to form a complete result
2859 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2860 captureResult.mMetadata.append(collectedPartialResult);
2861 }
2862
2863 captureResult.mMetadata.sort();
2864
2865 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002866 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2867 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002868 SET_ERR("No timestamp provided by HAL for frame %d!",
2869 frameNumber);
2870 return;
2871 }
2872
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002873 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2874 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2875
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002876 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002877}
2878
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002879/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002880 * Camera HAL device callback methods
2881 */
2882
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002883void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002884 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002885
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002886 status_t res;
2887
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002888 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002889 if (result->result == NULL && result->num_output_buffers == 0 &&
2890 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002891 SET_ERR("No result data provided by HAL for frame %d",
2892 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002893 return;
2894 }
Zhijun He204e3292014-07-14 17:09:23 -07002895
Zhijun He204e3292014-07-14 17:09:23 -07002896 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002897 result->result != NULL &&
2898 result->partial_result != 1) {
2899 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2900 " if partial result is not supported",
2901 frameNumber, result->partial_result);
2902 return;
2903 }
2904
2905 bool isPartialResult = false;
2906 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002907 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002908 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002909
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002910 // Get shutter timestamp and resultExtras from list of in-flight requests,
2911 // where it was added by the shutter notification for this frame. If the
2912 // shutter timestamp isn't received yet, append the output buffers to the
2913 // in-flight request and they will be returned when the shutter timestamp
2914 // arrives. Update the in-flight status and remove the in-flight entry if
2915 // all result data and shutter timestamp have been received.
2916 nsecs_t shutterTimestamp = 0;
2917
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002918 {
2919 Mutex::Autolock l(mInFlightLock);
2920 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2921 if (idx == NAME_NOT_FOUND) {
2922 SET_ERR("Unknown frame number for capture result: %d",
2923 frameNumber);
2924 return;
2925 }
2926 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002927 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2928 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002929 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002930 __FUNCTION__, request.resultExtras.requestId,
2931 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002932 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002933 // Always update the partial count to the latest one if it's not 0
2934 // (buffers only). When framework aggregates adjacent partial results
2935 // into one, the latest partial count will be used.
2936 if (result->partial_result != 0)
2937 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002938
2939 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002940 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002941 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2942 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2943 " the range of [1, %d] when metadata is included in the result",
2944 frameNumber, result->partial_result, mNumPartialResults);
2945 return;
2946 }
2947 isPartialResult = (result->partial_result < mNumPartialResults);
2948 if (isPartialResult) {
2949 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002950 }
2951
Shuzhen Wang4a472662017-02-26 23:29:04 -08002952 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002953 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002954 sendPartialCaptureResult(result->result, request.resultExtras,
2955 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002956 }
2957 }
2958
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002959 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002960 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002961
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002962 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002963 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002964 if (request.haveResultMetadata) {
2965 SET_ERR("Called multiple times with metadata for frame %d",
2966 frameNumber);
2967 return;
2968 }
Zhijun He204e3292014-07-14 17:09:23 -07002969 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002970 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002971 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002972 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002973 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002974 request.haveResultMetadata = true;
2975 }
2976
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002977 uint32_t numBuffersReturned = result->num_output_buffers;
2978 if (result->input_buffer != NULL) {
2979 if (hasInputBufferInRequest) {
2980 numBuffersReturned += 1;
2981 } else {
2982 ALOGW("%s: Input buffer should be NULL if there is no input"
2983 " buffer sent in the request",
2984 __FUNCTION__);
2985 }
2986 }
2987 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002988 if (request.numBuffersLeft < 0) {
2989 SET_ERR("Too many buffers returned for frame %d",
2990 frameNumber);
2991 return;
2992 }
2993
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002994 camera_metadata_ro_entry_t entry;
2995 res = find_camera_metadata_ro_entry(result->result,
2996 ANDROID_SENSOR_TIMESTAMP, &entry);
2997 if (res == OK && entry.count == 1) {
2998 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002999 }
3000
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003001 // If shutter event isn't received yet, append the output buffers to
3002 // the in-flight request. Otherwise, return the output buffers to
3003 // streams.
3004 if (shutterTimestamp == 0) {
3005 request.pendingOutputBuffers.appendArray(result->output_buffers,
3006 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003007 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003008 returnOutputBuffers(result->output_buffers,
3009 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07003010 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003011
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003012 if (result->result != NULL && !isPartialResult) {
3013 if (shutterTimestamp == 0) {
3014 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003015 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003016 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003017 CameraMetadata metadata;
3018 metadata = result->result;
3019 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003020 collectedPartialResult, frameNumber,
3021 hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003022 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003023 }
3024
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003025 removeInFlightRequestIfReadyLocked(idx);
3026 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003027
Zhijun Hef0d962a2014-06-30 10:24:11 -07003028 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003029 if (hasInputBufferInRequest) {
3030 Camera3Stream *stream =
3031 Camera3Stream::cast(result->input_buffer->stream);
3032 res = stream->returnInputBuffer(*(result->input_buffer));
3033 // Note: stream may be deallocated at this point, if this buffer was the
3034 // last reference to it.
3035 if (res != OK) {
3036 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3037 " its stream:%s (%d)", __FUNCTION__,
3038 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003039 }
3040 } else {
3041 ALOGW("%s: Input buffer should be NULL if there is no input"
3042 " buffer sent in the request, skipping input buffer return.",
3043 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003044 }
3045 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003046}
3047
3048void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003049 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003050 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003051 {
3052 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003053 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003054 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003055
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003056 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003057 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003058 return;
3059 }
3060
3061 switch (msg->type) {
3062 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003063 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003064 break;
3065 }
3066 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003067 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003068 break;
3069 }
3070 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003071 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003072 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003073 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003074}
3075
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003076void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003077 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003078 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003079 // Map camera HAL error codes to ICameraDeviceCallback error codes
3080 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003081 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003082 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003083 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003084 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003085 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003086 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003087 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003088 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003089 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003090 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003091 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003092 };
3093
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003094 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003095 ((msg.error_code >= 0) &&
3096 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3097 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003098 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003099
3100 int streamId = 0;
3101 if (msg.error_stream != NULL) {
3102 Camera3Stream *stream =
3103 Camera3Stream::cast(msg.error_stream);
3104 streamId = stream->getId();
3105 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003106 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3107 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003108 streamId, msg.error_code);
3109
3110 CaptureResultExtras resultExtras;
3111 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003112 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003113 // SET_ERR calls notifyError
3114 SET_ERR("Camera HAL reported serious device error");
3115 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003116 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3117 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3118 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003119 {
3120 Mutex::Autolock l(mInFlightLock);
3121 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3122 if (idx >= 0) {
3123 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3124 r.requestStatus = msg.error_code;
3125 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07003126 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3127 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3128 errorCode) {
3129 r.skipResultMetadata = true;
3130 }
Emilian Peevba0fac32017-03-30 09:05:34 +01003131 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3132 errorCode) {
3133 // In case of missing result check whether the buffers
3134 // returned. If they returned, then remove inflight
3135 // request.
3136 removeInFlightRequestIfReadyLocked(idx);
3137 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003138 } else {
3139 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003140 ALOGE("Camera %s: %s: cannot find in-flight request on "
3141 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003142 resultExtras.frameNumber);
3143 }
3144 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003145 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003146 if (listener != NULL) {
3147 listener->notifyError(errorCode, resultExtras);
3148 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003149 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003150 }
3151 break;
3152 default:
3153 // SET_ERR calls notifyError
3154 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3155 break;
3156 }
3157}
3158
3159void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003160 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003161 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003162 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003163
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003164 // Set timestamp for the request in the in-flight tracking
3165 // and get the request ID to send upstream
3166 {
3167 Mutex::Autolock l(mInFlightLock);
3168 idx = mInFlightMap.indexOfKey(msg.frame_number);
3169 if (idx >= 0) {
3170 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003171
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003172 // Verify ordering of shutter notifications
3173 {
3174 Mutex::Autolock l(mOutputLock);
3175 // TODO: need to track errors for tighter bounds on expected frame number.
3176 if (r.hasInputBuffer) {
3177 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3178 SET_ERR("Shutter notification out-of-order. Expected "
3179 "notification for frame %d, got frame %d",
3180 mNextReprocessShutterFrameNumber, msg.frame_number);
3181 return;
3182 }
3183 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3184 } else {
3185 if (msg.frame_number < mNextShutterFrameNumber) {
3186 SET_ERR("Shutter notification out-of-order. Expected "
3187 "notification for frame %d, got frame %d",
3188 mNextShutterFrameNumber, msg.frame_number);
3189 return;
3190 }
3191 mNextShutterFrameNumber = msg.frame_number + 1;
3192 }
3193 }
3194
Shuzhen Wang4a472662017-02-26 23:29:04 -08003195 r.shutterTimestamp = msg.timestamp;
3196 if (r.hasCallback) {
3197 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003198 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003199 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003200 // Call listener, if any
3201 if (listener != NULL) {
3202 listener->notifyShutter(r.resultExtras, msg.timestamp);
3203 }
3204 // send pending result and buffers
3205 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3206 r.collectedPartialResult, msg.frame_number,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003207 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003208 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003209 returnOutputBuffers(r.pendingOutputBuffers.array(),
3210 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3211 r.pendingOutputBuffers.clear();
3212
3213 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003214 }
3215 }
3216 if (idx < 0) {
3217 SET_ERR("Shutter notification for non-existent frame number %d",
3218 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003219 }
3220}
3221
3222
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003223CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003224 ALOGV("%s", __FUNCTION__);
3225
Igor Murashkin1e479c02013-09-06 16:55:14 -07003226 CameraMetadata retVal;
3227
3228 if (mRequestThread != NULL) {
3229 retVal = mRequestThread->getLatestRequest();
3230 }
3231
Igor Murashkin1e479c02013-09-06 16:55:14 -07003232 return retVal;
3233}
3234
Jianing Weicb0652e2014-03-12 18:29:36 -07003235
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003236void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3237 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3238 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3239}
3240
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003241/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003242 * HalInterface inner class methods
3243 */
3244
Yifan Hongf79b5542017-04-11 14:44:25 -07003245Camera3Device::HalInterface::HalInterface(
3246 sp<ICameraDeviceSession> &session,
3247 std::shared_ptr<RequestMetadataQueue> queue) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003248 mHidlSession(session),
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003249 mRequestMetadataQueue(queue) {
3250 // Check with hardware service manager if we can downcast these interfaces
3251 // Somewhat expensive, so cache the results at startup
3252 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3253 if (castResult_3_4.isOk()) {
3254 mHidlSession_3_4 = castResult_3_4;
3255 }
3256 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3257 if (castResult_3_3.isOk()) {
3258 mHidlSession_3_3 = castResult_3_3;
3259 }
3260}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003261
Emilian Peev31abd0a2017-05-11 18:37:46 +01003262Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003263
3264Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003265 mHidlSession(other.mHidlSession),
3266 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003267
3268bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003269 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003270}
3271
3272void Camera3Device::HalInterface::clear() {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003273 mHidlSession.clear();
3274}
3275
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003276bool Camera3Device::HalInterface::supportBatchRequest() {
3277 return mHidlSession != nullptr;
3278}
3279
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003280status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3281 camera3_request_template_t templateId,
3282 /*out*/ camera_metadata_t **requestTemplate) {
3283 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3284 if (!valid()) return INVALID_OPERATION;
3285 status_t res = OK;
3286
Emilian Peev31abd0a2017-05-11 18:37:46 +01003287 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003288
3289 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003290 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003291 status = s;
3292 if (status == common::V1_0::Status::OK) {
3293 const camera_metadata *r =
3294 reinterpret_cast<const camera_metadata_t*>(request.data());
3295 size_t expectedSize = request.size();
3296 int ret = validate_camera_metadata_structure(r, &expectedSize);
3297 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3298 *requestTemplate = clone_camera_metadata(r);
3299 if (*requestTemplate == nullptr) {
3300 ALOGE("%s: Unable to clone camera metadata received from HAL",
3301 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003302 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003303 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003304 } else {
3305 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3306 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003307 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003308 }
3309 };
3310 hardware::Return<void> err;
3311 if (mHidlSession_3_4 != nullptr) {
3312 device::V3_4::RequestTemplate id;
3313 switch (templateId) {
3314 case CAMERA3_TEMPLATE_PREVIEW:
3315 id = device::V3_4::RequestTemplate::PREVIEW;
3316 break;
3317 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3318 id = device::V3_4::RequestTemplate::STILL_CAPTURE;
3319 break;
3320 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3321 id = device::V3_4::RequestTemplate::VIDEO_RECORD;
3322 break;
3323 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3324 id = device::V3_4::RequestTemplate::VIDEO_SNAPSHOT;
3325 break;
3326 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3327 id = device::V3_4::RequestTemplate::ZERO_SHUTTER_LAG;
3328 break;
3329 case CAMERA3_TEMPLATE_MANUAL:
3330 id = device::V3_4::RequestTemplate::MANUAL;
3331 break;
3332 case CAMERA3_TEMPLATE_MOTION_TRACKING_PREVIEW:
3333 id = device::V3_4::RequestTemplate::MOTION_TRACKING_PREVIEW;
3334 break;
3335 case CAMERA3_TEMPLATE_MOTION_TRACKING_BEST:
3336 id = device::V3_4::RequestTemplate::MOTION_TRACKING_BEST;
3337 break;
3338 default:
3339 // Unknown template ID
3340 return BAD_VALUE;
3341 }
3342 err = mHidlSession_3_4->constructDefaultRequestSettings_3_4(id, requestCallback);
3343 } else {
3344 RequestTemplate id;
3345 switch (templateId) {
3346 case CAMERA3_TEMPLATE_PREVIEW:
3347 id = RequestTemplate::PREVIEW;
3348 break;
3349 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3350 id = RequestTemplate::STILL_CAPTURE;
3351 break;
3352 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3353 id = RequestTemplate::VIDEO_RECORD;
3354 break;
3355 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3356 id = RequestTemplate::VIDEO_SNAPSHOT;
3357 break;
3358 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3359 id = RequestTemplate::ZERO_SHUTTER_LAG;
3360 break;
3361 case CAMERA3_TEMPLATE_MANUAL:
3362 id = RequestTemplate::MANUAL;
3363 break;
3364 default:
3365 // Unknown template ID, or this HAL is too old to support it
3366 return BAD_VALUE;
3367 }
3368 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
3369 }
3370
Emilian Peev31abd0a2017-05-11 18:37:46 +01003371 if (!err.isOk()) {
3372 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3373 res = DEAD_OBJECT;
3374 } else {
3375 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003376 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003377
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003378 return res;
3379}
3380
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003381status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
3382 camera3_stream_configuration *config) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003383 ATRACE_NAME("CameraHal::configureStreams");
3384 if (!valid()) return INVALID_OPERATION;
3385 status_t res = OK;
3386
Emilian Peev31abd0a2017-05-11 18:37:46 +01003387 // Convert stream config to HIDL
3388 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003389 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3390 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3391 requestedConfiguration3_2.streams.resize(config->num_streams);
3392 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003393 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003394 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3395 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003396 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003397
Emilian Peev31abd0a2017-05-11 18:37:46 +01003398 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3399 cam3stream->setBufferFreedListener(this);
3400 int streamId = cam3stream->getId();
3401 StreamType streamType;
3402 switch (src->stream_type) {
3403 case CAMERA3_STREAM_OUTPUT:
3404 streamType = StreamType::OUTPUT;
3405 break;
3406 case CAMERA3_STREAM_INPUT:
3407 streamType = StreamType::INPUT;
3408 break;
3409 default:
3410 ALOGE("%s: Stream %d: Unsupported stream type %d",
3411 __FUNCTION__, streamId, config->streams[i]->stream_type);
3412 return BAD_VALUE;
3413 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003414 dst3_2.id = streamId;
3415 dst3_2.streamType = streamType;
3416 dst3_2.width = src->width;
3417 dst3_2.height = src->height;
3418 dst3_2.format = mapToPixelFormat(src->format);
3419 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3420 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3421 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3422 dst3_4.v3_2 = dst3_2;
3423 if (src->physical_camera_id != nullptr) {
3424 dst3_4.physicalCameraId = src->physical_camera_id;
3425 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003426
3427 activeStreams.insert(streamId);
3428 // Create Buffer ID map if necessary
3429 if (mBufferIdMaps.count(streamId) == 0) {
3430 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3431 }
3432 }
3433 // remove BufferIdMap for deleted streams
3434 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3435 int streamId = it->first;
3436 bool active = activeStreams.count(streamId) > 0;
3437 if (!active) {
3438 it = mBufferIdMaps.erase(it);
3439 } else {
3440 ++it;
3441 }
3442 }
3443
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003444 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003445 res = mapToStreamConfigurationMode(
3446 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003447 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003448 if (res != OK) {
3449 return res;
3450 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003451 requestedConfiguration3_2.operationMode = operationMode;
3452 requestedConfiguration3_4.operationMode = operationMode;
3453 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003454 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3455 get_camera_metadata_size(sessionParams));
3456
Emilian Peev31abd0a2017-05-11 18:37:46 +01003457 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003458 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003459 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003460
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003461 // See if we have v3.4 or v3.3 HAL
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003462 if (mHidlSession_3_4 != nullptr) {
3463 // We do; use v3.4 for the call
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003464 ALOGV("%s: v3.4 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003465 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003466 auto err = mHidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003467 [&status, &finalConfiguration3_4]
3468 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3469 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003470 status = s;
3471 });
3472 if (!err.isOk()) {
3473 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3474 return DEAD_OBJECT;
3475 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003476 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3477 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3478 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3479 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003480 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003481 // We do; use v3.3 for the call
3482 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003483 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003484 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003485 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003486 finalConfiguration = halConfiguration;
3487 status = s;
3488 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003489 if (!err.isOk()) {
3490 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3491 return DEAD_OBJECT;
3492 }
3493 } else {
3494 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3495 ALOGV("%s: v3.2 device found", __FUNCTION__);
3496 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003497 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003498 [&status, &finalConfiguration_3_2]
3499 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3500 finalConfiguration_3_2 = halConfiguration;
3501 status = s;
3502 });
3503 if (!err.isOk()) {
3504 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3505 return DEAD_OBJECT;
3506 }
3507 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3508 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3509 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3510 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003511 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003512 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003513 }
3514
3515 if (status != common::V1_0::Status::OK ) {
3516 return CameraProviderManager::mapToStatusT(status);
3517 }
3518
3519 // And convert output stream configuration from HIDL
3520
3521 for (size_t i = 0; i < config->num_streams; i++) {
3522 camera3_stream_t *dst = config->streams[i];
3523 int streamId = Camera3Stream::cast(dst)->getId();
3524
3525 // Start scan at i, with the assumption that the stream order matches
3526 size_t realIdx = i;
3527 bool found = false;
3528 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003529 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003530 found = true;
3531 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003532 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003533 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3534 }
3535 if (!found) {
3536 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3537 __FUNCTION__, streamId);
3538 return INVALID_OPERATION;
3539 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003540 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003541
Emilian Peev710c1422017-08-30 11:19:38 +01003542 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3543 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003544 dstStream->setDataSpaceOverride(false);
3545 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3546 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3547
Emilian Peev31abd0a2017-05-11 18:37:46 +01003548 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3549 if (dst->format != overrideFormat) {
3550 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3551 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003552 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003553 if (dst->data_space != overrideDataSpace) {
3554 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3555 streamId, dst->format);
3556 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003557 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003558 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003559 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3560
Emilian Peev31abd0a2017-05-11 18:37:46 +01003561 // Override allowed with IMPLEMENTATION_DEFINED
3562 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003563 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003564 }
3565
Emilian Peev31abd0a2017-05-11 18:37:46 +01003566 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003567 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003568 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003569 __FUNCTION__, streamId);
3570 return INVALID_OPERATION;
3571 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003572 dstStream->setUsage(
3573 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003574 } else {
3575 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003576 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003577 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3578 __FUNCTION__, streamId);
3579 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003580 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003581 dstStream->setUsage(
3582 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003583 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003584 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003585 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003586
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003587 return res;
3588}
3589
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003590void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3591 /*out*/device::V3_2::CaptureRequest* captureRequest,
3592 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003593 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003594 if (captureRequest == nullptr || handlesCreated == nullptr) {
3595 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3596 __FUNCTION__, captureRequest, handlesCreated);
3597 return;
3598 }
3599
3600 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003601
3602 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003603
3604 {
3605 std::lock_guard<std::mutex> lock(mInflightLock);
3606 if (request->input_buffer != nullptr) {
3607 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3608 buffer_handle_t buf = *(request->input_buffer->buffer);
3609 auto pair = getBufferId(buf, streamId);
3610 bool isNewBuffer = pair.first;
3611 uint64_t bufferId = pair.second;
3612 captureRequest->inputBuffer.streamId = streamId;
3613 captureRequest->inputBuffer.bufferId = bufferId;
3614 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3615 captureRequest->inputBuffer.status = BufferStatus::OK;
3616 native_handle_t *acquireFence = nullptr;
3617 if (request->input_buffer->acquire_fence != -1) {
3618 acquireFence = native_handle_create(1,0);
3619 acquireFence->data[0] = request->input_buffer->acquire_fence;
3620 handlesCreated->push_back(acquireFence);
3621 }
3622 captureRequest->inputBuffer.acquireFence = acquireFence;
3623 captureRequest->inputBuffer.releaseFence = nullptr;
3624
3625 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3626 request->input_buffer->buffer,
3627 request->input_buffer->acquire_fence);
3628 } else {
3629 captureRequest->inputBuffer.streamId = -1;
3630 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3631 }
3632
3633 captureRequest->outputBuffers.resize(request->num_output_buffers);
3634 for (size_t i = 0; i < request->num_output_buffers; i++) {
3635 const camera3_stream_buffer_t *src = request->output_buffers + i;
3636 StreamBuffer &dst = captureRequest->outputBuffers[i];
3637 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3638 buffer_handle_t buf = *(src->buffer);
3639 auto pair = getBufferId(buf, streamId);
3640 bool isNewBuffer = pair.first;
3641 dst.streamId = streamId;
3642 dst.bufferId = pair.second;
3643 dst.buffer = isNewBuffer ? buf : nullptr;
3644 dst.status = BufferStatus::OK;
3645 native_handle_t *acquireFence = nullptr;
3646 if (src->acquire_fence != -1) {
3647 acquireFence = native_handle_create(1,0);
3648 acquireFence->data[0] = src->acquire_fence;
3649 handlesCreated->push_back(acquireFence);
3650 }
3651 dst.acquireFence = acquireFence;
3652 dst.releaseFence = nullptr;
3653
3654 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3655 src->buffer, src->acquire_fence);
3656 }
3657 }
3658}
3659
3660status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3661 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3662 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3663 if (!valid()) return INVALID_OPERATION;
3664
Emilian Peevaebbe412018-01-15 13:53:24 +00003665 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3666 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3667 if (castResult_3_4.isOk()) {
3668 hidlSession_3_4 = castResult_3_4;
3669 }
3670
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003671 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003672 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003673 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00003674 if (hidlSession_3_4 != nullptr) {
3675 captureRequests_3_4.resize(batchSize);
3676 } else {
3677 captureRequests.resize(batchSize);
3678 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003679 std::vector<native_handle_t*> handlesCreated;
3680
3681 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003682 if (hidlSession_3_4 != nullptr) {
3683 wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3684 /*out*/&handlesCreated);
3685 } else {
3686 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3687 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003688 }
3689
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003690 std::vector<device::V3_2::BufferCache> cachesToRemove;
3691 {
3692 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3693 for (auto& pair : mFreedBuffers) {
3694 // The stream might have been removed since onBufferFreed
3695 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3696 cachesToRemove.push_back({pair.first, pair.second});
3697 }
3698 }
3699 mFreedBuffers.clear();
3700 }
3701
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003702 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3703 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003704
3705 // Write metadata to FMQ.
3706 for (size_t i = 0; i < batchSize; i++) {
3707 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003708 device::V3_2::CaptureRequest* captureRequest;
3709 if (hidlSession_3_4 != nullptr) {
3710 captureRequest = &captureRequests_3_4[i].v3_2;
3711 } else {
3712 captureRequest = &captureRequests[i];
3713 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003714
3715 if (request->settings != nullptr) {
3716 size_t settingsSize = get_camera_metadata_size(request->settings);
3717 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3718 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3719 captureRequest->settings.resize(0);
3720 captureRequest->fmqSettingsSize = settingsSize;
3721 } else {
3722 if (mRequestMetadataQueue != nullptr) {
3723 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3724 }
3725 captureRequest->settings.setToExternal(
3726 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3727 get_camera_metadata_size(request->settings));
3728 captureRequest->fmqSettingsSize = 0u;
3729 }
3730 } else {
3731 // A null request settings maps to a size-0 CameraMetadata
3732 captureRequest->settings.resize(0);
3733 captureRequest->fmqSettingsSize = 0u;
3734 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003735
3736 if (hidlSession_3_4 != nullptr) {
3737 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3738 for (size_t j = 0; j < request->num_physcam_settings; j++) {
3739 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3740 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3741 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3742 settingsSize)) {
3743 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3744 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = settingsSize;
3745 } else {
3746 if (mRequestMetadataQueue != nullptr) {
3747 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3748 }
3749 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3750 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3751 request->physcam_settings[j])),
3752 get_camera_metadata_size(request->physcam_settings[j]));
3753 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
3754 }
3755 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3756 request->physcam_id[j];
3757 }
3758 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003759 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003760
3761 hardware::details::return_status err;
3762 if (hidlSession_3_4 != nullptr) {
3763 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003764 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3765 status = s;
3766 *numRequestProcessed = n;
3767 });
Emilian Peevaebbe412018-01-15 13:53:24 +00003768 } else {
3769 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
3770 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3771 status = s;
3772 *numRequestProcessed = n;
3773 });
3774 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003775 if (!err.isOk()) {
3776 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3777 return DEAD_OBJECT;
3778 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003779 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3780 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3781 __FUNCTION__, *numRequestProcessed, batchSize);
3782 status = common::V1_0::Status::INTERNAL_ERROR;
3783 }
3784
3785 for (auto& handle : handlesCreated) {
3786 native_handle_delete(handle);
3787 }
3788 return CameraProviderManager::mapToStatusT(status);
3789}
3790
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003791status_t Camera3Device::HalInterface::processCaptureRequest(
3792 camera3_capture_request_t *request) {
3793 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003794 if (!valid()) return INVALID_OPERATION;
3795 status_t res = OK;
3796
Emilian Peev31abd0a2017-05-11 18:37:46 +01003797 uint32_t numRequestProcessed = 0;
3798 std::vector<camera3_capture_request_t*> requests(1);
3799 requests[0] = request;
3800 res = processBatchCaptureRequests(requests, &numRequestProcessed);
3801
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003802 return res;
3803}
3804
3805status_t Camera3Device::HalInterface::flush() {
3806 ATRACE_NAME("CameraHal::flush");
3807 if (!valid()) return INVALID_OPERATION;
3808 status_t res = OK;
3809
Emilian Peev31abd0a2017-05-11 18:37:46 +01003810 auto err = mHidlSession->flush();
3811 if (!err.isOk()) {
3812 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3813 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003814 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003815 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003816 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003817
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003818 return res;
3819}
3820
Emilian Peev31abd0a2017-05-11 18:37:46 +01003821status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003822 ATRACE_NAME("CameraHal::dump");
3823 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003824
Emilian Peev31abd0a2017-05-11 18:37:46 +01003825 // Handled by CameraProviderManager::dump
3826
3827 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003828}
3829
3830status_t Camera3Device::HalInterface::close() {
3831 ATRACE_NAME("CameraHal::close()");
3832 if (!valid()) return INVALID_OPERATION;
3833 status_t res = OK;
3834
Emilian Peev31abd0a2017-05-11 18:37:46 +01003835 auto err = mHidlSession->close();
3836 // Interface will be dead shortly anyway, so don't log errors
3837 if (!err.isOk()) {
3838 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003839 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003840
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003841 return res;
3842}
3843
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003844void Camera3Device::HalInterface::getInflightBufferKeys(
3845 std::vector<std::pair<int32_t, int32_t>>* out) {
3846 std::lock_guard<std::mutex> lock(mInflightLock);
3847 out->clear();
3848 out->reserve(mInflightBufferMap.size());
3849 for (auto& pair : mInflightBufferMap) {
3850 uint64_t key = pair.first;
3851 int32_t streamId = key & 0xFFFFFFFF;
3852 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3853 out->push_back(std::make_pair(frameNumber, streamId));
3854 }
3855 return;
3856}
3857
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003858status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003859 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003860 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003861 auto pair = std::make_pair(buffer, acquireFence);
3862 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003863 return OK;
3864}
3865
3866status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003867 int32_t frameNumber, int32_t streamId,
3868 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003869 std::lock_guard<std::mutex> lock(mInflightLock);
3870
3871 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3872 auto it = mInflightBufferMap.find(key);
3873 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003874 auto pair = it->second;
3875 *buffer = pair.first;
3876 int acquireFence = pair.second;
3877 if (acquireFence > 0) {
3878 ::close(acquireFence);
3879 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003880 mInflightBufferMap.erase(it);
3881 return OK;
3882}
3883
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003884std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3885 const buffer_handle_t& buf, int streamId) {
3886 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3887
3888 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3889 auto it = bIdMap.find(buf);
3890 if (it == bIdMap.end()) {
3891 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003892 ALOGV("stream %d now have %zu buffer caches, buf %p",
3893 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003894 return std::make_pair(true, mNextBufferId - 1);
3895 } else {
3896 return std::make_pair(false, it->second);
3897 }
3898}
3899
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003900void Camera3Device::HalInterface::onBufferFreed(
3901 int streamId, const native_handle_t* handle) {
3902 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3903 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3904 auto mapIt = mBufferIdMaps.find(streamId);
3905 if (mapIt == mBufferIdMaps.end()) {
3906 // streamId might be from a deleted stream here
3907 ALOGI("%s: stream %d has been removed",
3908 __FUNCTION__, streamId);
3909 return;
3910 }
3911 BufferIdMap& bIdMap = mapIt->second;
3912 auto it = bIdMap.find(handle);
3913 if (it == bIdMap.end()) {
3914 ALOGW("%s: cannot find buffer %p in stream %d",
3915 __FUNCTION__, handle, streamId);
3916 return;
3917 } else {
3918 bufferId = it->second;
3919 bIdMap.erase(it);
3920 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3921 __FUNCTION__, streamId, bIdMap.size(), handle);
3922 }
3923 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3924}
3925
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003926/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003927 * RequestThread inner class methods
3928 */
3929
3930Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003931 sp<StatusTracker> statusTracker,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003932 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003933 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003934 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003935 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003936 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003937 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003938 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003939 mReconfigured(false),
3940 mDoPause(false),
3941 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003942 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003943 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003944 mCurrentAfTriggerId(0),
3945 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003946 mRepeatingLastFrameNumber(
3947 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003948 mPrepareVideoStream(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003949 mRequestLatency(kRequestLatencyBinSize),
3950 mSessionParamKeys(sessionParamKeys),
3951 mLatestSessionParams(sessionParamKeys.size()) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003952 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003953}
3954
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003955Camera3Device::RequestThread::~RequestThread() {}
3956
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003957void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003958 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003959 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003960 Mutex::Autolock l(mRequestLock);
3961 mListener = listener;
3962}
3963
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003964void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
3965 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003966 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003967 Mutex::Autolock l(mRequestLock);
3968 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003969 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003970 // Prepare video stream for high speed recording.
3971 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003972}
3973
Jianing Wei90e59c92014-03-12 18:29:36 -07003974status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003975 List<sp<CaptureRequest> > &requests,
3976 /*out*/
3977 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003978 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003979 Mutex::Autolock l(mRequestLock);
3980 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3981 ++it) {
3982 mRequestQueue.push_back(*it);
3983 }
3984
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003985 if (lastFrameNumber != NULL) {
3986 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3987 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3988 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3989 *lastFrameNumber);
3990 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003991
Jianing Wei90e59c92014-03-12 18:29:36 -07003992 unpauseForNewRequests();
3993
3994 return OK;
3995}
3996
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003997
3998status_t Camera3Device::RequestThread::queueTrigger(
3999 RequestTrigger trigger[],
4000 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004001 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004002 Mutex::Autolock l(mTriggerMutex);
4003 status_t ret;
4004
4005 for (size_t i = 0; i < count; ++i) {
4006 ret = queueTriggerLocked(trigger[i]);
4007
4008 if (ret != OK) {
4009 return ret;
4010 }
4011 }
4012
4013 return OK;
4014}
4015
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004016const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4017 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004018 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004019 if (d != nullptr) return d->mId;
4020 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004021}
4022
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004023status_t Camera3Device::RequestThread::queueTriggerLocked(
4024 RequestTrigger trigger) {
4025
4026 uint32_t tag = trigger.metadataTag;
4027 ssize_t index = mTriggerMap.indexOfKey(tag);
4028
4029 switch (trigger.getTagType()) {
4030 case TYPE_BYTE:
4031 // fall-through
4032 case TYPE_INT32:
4033 break;
4034 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004035 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4036 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004037 return INVALID_OPERATION;
4038 }
4039
4040 /**
4041 * Collect only the latest trigger, since we only have 1 field
4042 * in the request settings per trigger tag, and can't send more than 1
4043 * trigger per request.
4044 */
4045 if (index != NAME_NOT_FOUND) {
4046 mTriggerMap.editValueAt(index) = trigger;
4047 } else {
4048 mTriggerMap.add(tag, trigger);
4049 }
4050
4051 return OK;
4052}
4053
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004054status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004055 const RequestList &requests,
4056 /*out*/
4057 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004058 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004059 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004060 if (lastFrameNumber != NULL) {
4061 *lastFrameNumber = mRepeatingLastFrameNumber;
4062 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004063 mRepeatingRequests.clear();
4064 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4065 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004066
4067 unpauseForNewRequests();
4068
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004069 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004070 return OK;
4071}
4072
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004073bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004074 if (mRepeatingRequests.empty()) {
4075 return false;
4076 }
4077 int32_t requestId = requestIn->mResultExtras.requestId;
4078 const RequestList &repeatRequests = mRepeatingRequests;
4079 // All repeating requests are guaranteed to have same id so only check first quest
4080 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4081 return (firstRequest->mResultExtras.requestId == requestId);
4082}
4083
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004084status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004085 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004086 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004087 return clearRepeatingRequestsLocked(lastFrameNumber);
4088
4089}
4090
4091status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004092 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004093 if (lastFrameNumber != NULL) {
4094 *lastFrameNumber = mRepeatingLastFrameNumber;
4095 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004096 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004097 return OK;
4098}
4099
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004100status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004101 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004102 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004103 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004104 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004105
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004106 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004107
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004108 // Send errors for all requests pending in the request queue, including
4109 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004110 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004111 if (listener != NULL) {
4112 for (RequestList::iterator it = mRequestQueue.begin();
4113 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004114 // Abort the input buffers for reprocess requests.
4115 if ((*it)->mInputStream != NULL) {
4116 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004117 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4118 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004119 if (res != OK) {
4120 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4121 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4122 } else {
4123 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4124 if (res != OK) {
4125 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4126 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4127 }
4128 }
4129 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004130 // Set the frame number this request would have had, if it
4131 // had been submitted; this frame number will not be reused.
4132 // The requestId and burstId fields were set when the request was
4133 // submitted originally (in convertMetadataListToRequestListLocked)
4134 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004135 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004136 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004137 }
4138 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004139 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004140
4141 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004142 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004143 if (lastFrameNumber != NULL) {
4144 *lastFrameNumber = mRepeatingLastFrameNumber;
4145 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004146 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004147 return OK;
4148}
4149
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004150status_t Camera3Device::RequestThread::flush() {
4151 ATRACE_CALL();
4152 Mutex::Autolock l(mFlushLock);
4153
Emilian Peev08dd2452017-04-06 16:55:14 +01004154 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004155}
4156
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004157void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004158 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004159 Mutex::Autolock l(mPauseLock);
4160 mDoPause = paused;
4161 mDoPauseSignal.signal();
4162}
4163
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004164status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4165 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004166 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004167 Mutex::Autolock l(mLatestRequestMutex);
4168 status_t res;
4169 while (mLatestRequestId != requestId) {
4170 nsecs_t startTime = systemTime();
4171
4172 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4173 if (res != OK) return res;
4174
4175 timeout -= (systemTime() - startTime);
4176 }
4177
4178 return OK;
4179}
4180
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004181void Camera3Device::RequestThread::requestExit() {
4182 // Call parent to set up shutdown
4183 Thread::requestExit();
4184 // The exit from any possible waits
4185 mDoPauseSignal.signal();
4186 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004187
4188 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4189 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004190}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004191
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004192void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004193 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004194 bool surfaceAbandoned = false;
4195 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004196 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004197 {
4198 Mutex::Autolock l(mRequestLock);
4199 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4200 // repeating requests.
4201 for (const auto& request : mRepeatingRequests) {
4202 for (const auto& s : request->mOutputStreams) {
4203 if (s->isAbandoned()) {
4204 surfaceAbandoned = true;
4205 clearRepeatingRequestsLocked(&lastFrameNumber);
4206 break;
4207 }
4208 }
4209 if (surfaceAbandoned) {
4210 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004211 }
4212 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004213 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004214 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004215
4216 if (listener != NULL && surfaceAbandoned) {
4217 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004218 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004219}
4220
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004221bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004222 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004223 status_t res;
4224 size_t batchSize = mNextRequests.size();
4225 std::vector<camera3_capture_request_t*> requests(batchSize);
4226 uint32_t numRequestProcessed = 0;
4227 for (size_t i = 0; i < batchSize; i++) {
4228 requests[i] = &mNextRequests.editItemAt(i).halRequest;
4229 }
4230
4231 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
4232 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4233
4234 bool triggerRemoveFailed = false;
4235 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4236 for (size_t i = 0; i < numRequestProcessed; i++) {
4237 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4238 nextRequest.submitted = true;
4239
4240
4241 // Update the latest request sent to HAL
4242 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4243 Mutex::Autolock al(mLatestRequestMutex);
4244
4245 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4246 mLatestRequest.acquire(cloned);
4247
4248 sp<Camera3Device> parent = mParent.promote();
4249 if (parent != NULL) {
4250 parent->monitorMetadata(TagMonitor::REQUEST,
4251 nextRequest.halRequest.frame_number,
4252 0, mLatestRequest);
4253 }
4254 }
4255
4256 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004257 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4258 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004259 }
4260
Emilian Peevaebbe412018-01-15 13:53:24 +00004261 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4262
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004263 if (!triggerRemoveFailed) {
4264 // Remove any previously queued triggers (after unlock)
4265 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4266 if (removeTriggerRes != OK) {
4267 triggerRemoveFailed = true;
4268 triggerFailedRequest = nextRequest;
4269 }
4270 }
4271 }
4272
4273 if (triggerRemoveFailed) {
4274 SET_ERR("RequestThread: Unable to remove triggers "
4275 "(capture request %d, HAL device: %s (%d)",
4276 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4277 cleanUpFailedRequests(/*sendRequestError*/ false);
4278 return false;
4279 }
4280
4281 if (res != OK) {
4282 // Should only get a failure here for malformed requests or device-level
4283 // errors, so consider all errors fatal. Bad metadata failures should
4284 // come through notify.
4285 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4286 mNextRequests[numRequestProcessed].halRequest.frame_number,
4287 strerror(-res), res);
4288 cleanUpFailedRequests(/*sendRequestError*/ false);
4289 return false;
4290 }
4291 return true;
4292}
4293
4294bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4295 status_t res;
4296
4297 for (auto& nextRequest : mNextRequests) {
4298 // Submit request and block until ready for next one
4299 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4300 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4301
4302 if (res != OK) {
4303 // Should only get a failure here for malformed requests or device-level
4304 // errors, so consider all errors fatal. Bad metadata failures should
4305 // come through notify.
4306 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4307 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4308 res);
4309 cleanUpFailedRequests(/*sendRequestError*/ false);
4310 return false;
4311 }
4312
4313 // Mark that the request has be submitted successfully.
4314 nextRequest.submitted = true;
4315
4316 // Update the latest request sent to HAL
4317 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4318 Mutex::Autolock al(mLatestRequestMutex);
4319
4320 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4321 mLatestRequest.acquire(cloned);
4322
4323 sp<Camera3Device> parent = mParent.promote();
4324 if (parent != NULL) {
4325 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4326 0, mLatestRequest);
4327 }
4328 }
4329
4330 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004331 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4332 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004333 }
4334
Emilian Peevaebbe412018-01-15 13:53:24 +00004335 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4336
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004337 // Remove any previously queued triggers (after unlock)
4338 res = removeTriggers(mPrevRequest);
4339 if (res != OK) {
4340 SET_ERR("RequestThread: Unable to remove triggers "
4341 "(capture request %d, HAL device: %s (%d)",
4342 nextRequest.halRequest.frame_number, strerror(-res), res);
4343 cleanUpFailedRequests(/*sendRequestError*/ false);
4344 return false;
4345 }
4346 }
4347 return true;
4348}
4349
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004350nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4351 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4352 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4353 find_camera_metadata_ro_entry(request,
4354 ANDROID_CONTROL_AE_MODE,
4355 &e);
4356 if (e.count == 0) return maxExpectedDuration;
4357
4358 switch (e.data.u8[0]) {
4359 case ANDROID_CONTROL_AE_MODE_OFF:
4360 find_camera_metadata_ro_entry(request,
4361 ANDROID_SENSOR_EXPOSURE_TIME,
4362 &e);
4363 if (e.count > 0) {
4364 maxExpectedDuration = e.data.i64[0];
4365 }
4366 find_camera_metadata_ro_entry(request,
4367 ANDROID_SENSOR_FRAME_DURATION,
4368 &e);
4369 if (e.count > 0) {
4370 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4371 }
4372 break;
4373 default:
4374 find_camera_metadata_ro_entry(request,
4375 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4376 &e);
4377 if (e.count > 1) {
4378 maxExpectedDuration = 1e9 / e.data.u8[0];
4379 }
4380 break;
4381 }
4382
4383 return maxExpectedDuration;
4384}
4385
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004386bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4387 ATRACE_CALL();
4388 bool updatesDetected = false;
4389
4390 for (auto tag : mSessionParamKeys) {
4391 camera_metadata_ro_entry entry = settings.find(tag);
4392 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4393
4394 if (entry.count > 0) {
4395 bool isDifferent = false;
4396 if (lastEntry.count > 0) {
4397 // Have a last value, compare to see if changed
4398 if (lastEntry.type == entry.type &&
4399 lastEntry.count == entry.count) {
4400 // Same type and count, compare values
4401 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4402 size_t entryBytes = bytesPerValue * lastEntry.count;
4403 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4404 if (cmp != 0) {
4405 isDifferent = true;
4406 }
4407 } else {
4408 // Count or type has changed
4409 isDifferent = true;
4410 }
4411 } else {
4412 // No last entry, so always consider to be different
4413 isDifferent = true;
4414 }
4415
4416 if (isDifferent) {
4417 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
4418 mLatestSessionParams.update(entry);
4419 updatesDetected = true;
4420 }
4421 } else if (lastEntry.count > 0) {
4422 // Value has been removed
4423 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4424 mLatestSessionParams.erase(tag);
4425 updatesDetected = true;
4426 }
4427 }
4428
4429 return updatesDetected;
4430}
4431
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004432bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004433 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004434 status_t res;
4435
4436 // Handle paused state.
4437 if (waitIfPaused()) {
4438 return true;
4439 }
4440
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004441 // Wait for the next batch of requests.
4442 waitForNextRequestBatch();
4443 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004444 return true;
4445 }
4446
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004447 // Get the latest request ID, if any
4448 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004449 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004450 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004451 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004452 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004453 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004454 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4455 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004456 }
4457
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004458 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4459 // or a single request from streaming or burst. In either case the first element
4460 // should contain the latest camera settings that we need to check for any session
4461 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004462 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004463 res = OK;
4464
4465 //Input stream buffers are already acquired at this point so an input stream
4466 //will not be able to move to idle state unless we force it.
4467 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4468 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4469 if (res != OK) {
4470 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4471 cleanUpFailedRequests(/*sendRequestError*/ false);
4472 return false;
4473 }
4474 }
4475
4476 if (res == OK) {
4477 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4478 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08004479 sp<Camera3Device> parent = mParent.promote();
4480 if (parent != nullptr) {
4481 parent->pauseStateNotify(true);
4482 }
4483
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004484 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4485
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004486 if (parent != nullptr) {
4487 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4488 }
4489
4490 statusTracker->markComponentActive(mStatusId);
4491 setPaused(false);
4492 }
4493
4494 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4495 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4496 if (res != OK) {
4497 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4498 cleanUpFailedRequests(/*sendRequestError*/ false);
4499 return false;
4500 }
4501 }
4502 }
4503 }
4504
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004505 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004506 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004507 if (res == TIMED_OUT) {
4508 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004509 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004510 // Check if any stream is abandoned.
4511 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004512 return true;
4513 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004514 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004515 return false;
4516 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004517
Zhijun Hecc27e112013-10-03 16:12:43 -07004518 // Inform waitUntilRequestProcessed thread of a new request ID
4519 {
4520 Mutex::Autolock al(mLatestRequestMutex);
4521
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004522 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004523 mLatestRequestSignal.signal();
4524 }
4525
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004526 // Submit a batch of requests to HAL.
4527 // Use flush lock only when submitting multilple requests in a batch.
4528 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4529 // which may take a long time to finish so synchronizing flush() and
4530 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4531 // For now, only synchronize for high speed recording and we should figure something out for
4532 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004533 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004534
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004535 if (useFlushLock) {
4536 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004537 }
4538
Zhijun Hef0645c12016-08-02 00:58:11 -07004539 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004540 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004541
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004542 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004543 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004544 if (mInterface->supportBatchRequest()) {
4545 submitRequestSuccess = sendRequestsBatch();
4546 } else {
4547 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004548 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004549 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4550 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004551
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004552 if (useFlushLock) {
4553 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004554 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004555
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004556 // Unset as current request
4557 {
4558 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004559 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004560 }
4561
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004562 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004563}
4564
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004565status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004566 ATRACE_CALL();
4567
Shuzhen Wang4a472662017-02-26 23:29:04 -08004568 for (size_t i = 0; i < mNextRequests.size(); i++) {
4569 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004570 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4571 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4572 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4573
4574 // Prepare a request to HAL
4575 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4576
4577 // Insert any queued triggers (before metadata is locked)
4578 status_t res = insertTriggers(captureRequest);
4579
4580 if (res < 0) {
4581 SET_ERR("RequestThread: Unable to insert triggers "
4582 "(capture request %d, HAL device: %s (%d)",
4583 halRequest->frame_number, strerror(-res), res);
4584 return INVALID_OPERATION;
4585 }
4586 int triggerCount = res;
4587 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4588 mPrevTriggers = triggerCount;
4589
4590 // If the request is the same as last, or we had triggers last time
4591 if (mPrevRequest != captureRequest || triggersMixedIn) {
4592 /**
4593 * HAL workaround:
4594 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4595 */
4596 res = addDummyTriggerIds(captureRequest);
4597 if (res != OK) {
4598 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4599 "(capture request %d, HAL device: %s (%d)",
4600 halRequest->frame_number, strerror(-res), res);
4601 return INVALID_OPERATION;
4602 }
4603
4604 /**
4605 * The request should be presorted so accesses in HAL
4606 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4607 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004608 captureRequest->mSettingsList.begin()->metadata.sort();
4609 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004610 mPrevRequest = captureRequest;
4611 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4612
4613 IF_ALOGV() {
4614 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4615 find_camera_metadata_ro_entry(
4616 halRequest->settings,
4617 ANDROID_CONTROL_AF_TRIGGER,
4618 &e
4619 );
4620 if (e.count > 0) {
4621 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4622 __FUNCTION__,
4623 halRequest->frame_number,
4624 e.data.u8[0]);
4625 }
4626 }
4627 } else {
4628 // leave request.settings NULL to indicate 'reuse latest given'
4629 ALOGVV("%s: Request settings are REUSED",
4630 __FUNCTION__);
4631 }
4632
Emilian Peevaebbe412018-01-15 13:53:24 +00004633 if (captureRequest->mSettingsList.size() > 1) {
4634 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4635 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
4636 halRequest->physcam_settings =
4637 new const camera_metadata* [halRequest->num_physcam_settings];
4638 auto it = ++captureRequest->mSettingsList.begin();
4639 size_t i = 0;
4640 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4641 halRequest->physcam_id[i] = it->cameraId.c_str();
4642 it->metadata.sort();
4643 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4644 }
4645 }
4646
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004647 uint32_t totalNumBuffers = 0;
4648
4649 // Fill in buffers
4650 if (captureRequest->mInputStream != NULL) {
4651 halRequest->input_buffer = &captureRequest->mInputBuffer;
4652 totalNumBuffers += 1;
4653 } else {
4654 halRequest->input_buffer = NULL;
4655 }
4656
4657 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4658 captureRequest->mOutputStreams.size());
4659 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004660 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4661 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004662
4663 // Prepare video buffers for high speed recording on the first video request.
4664 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4665 // Only try to prepare video stream on the first video request.
4666 mPrepareVideoStream = false;
4667
4668 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4669 while (res == NOT_ENOUGH_DATA) {
4670 res = outputStream->prepareNextBuffer();
4671 }
4672 if (res != OK) {
4673 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4674 __FUNCTION__, strerror(-res), res);
4675 outputStream->cancelPrepare();
4676 }
4677 }
4678
Shuzhen Wang4a472662017-02-26 23:29:04 -08004679 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4680 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004681 if (res != OK) {
4682 // Can't get output buffer from gralloc queue - this could be due to
4683 // abandoned queue or other consumer misbehavior, so not a fatal
4684 // error
4685 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4686 " %s (%d)", strerror(-res), res);
4687
4688 return TIMED_OUT;
4689 }
4690 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004691
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004692 }
4693 totalNumBuffers += halRequest->num_output_buffers;
4694
4695 // Log request in the in-flight queue
4696 sp<Camera3Device> parent = mParent.promote();
4697 if (parent == NULL) {
4698 // Should not happen, and nowhere to send errors to, so just log it
4699 CLOGE("RequestThread: Parent is gone");
4700 return INVALID_OPERATION;
4701 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004702
4703 // If this request list is for constrained high speed recording (not
4704 // preview), and the current request is not the last one in the batch,
4705 // do not send callback to the app.
4706 bool hasCallback = true;
4707 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4708 hasCallback = false;
4709 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004710 res = parent->registerInFlight(halRequest->frame_number,
4711 totalNumBuffers, captureRequest->mResultExtras,
4712 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004713 hasCallback,
4714 calculateMaxExpectedDuration(halRequest->settings));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004715 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4716 ", burstId = %" PRId32 ".",
4717 __FUNCTION__,
4718 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4719 captureRequest->mResultExtras.burstId);
4720 if (res != OK) {
4721 SET_ERR("RequestThread: Unable to register new in-flight request:"
4722 " %s (%d)", strerror(-res), res);
4723 return INVALID_OPERATION;
4724 }
4725 }
4726
4727 return OK;
4728}
4729
Igor Murashkin1e479c02013-09-06 16:55:14 -07004730CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004731 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004732 Mutex::Autolock al(mLatestRequestMutex);
4733
4734 ALOGV("RequestThread::%s", __FUNCTION__);
4735
4736 return mLatestRequest;
4737}
4738
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004739bool Camera3Device::RequestThread::isStreamPending(
4740 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004741 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004742 Mutex::Autolock l(mRequestLock);
4743
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004744 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004745 if (!nextRequest.submitted) {
4746 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4747 if (stream == s) return true;
4748 }
4749 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004750 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004751 }
4752
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004753 for (const auto& request : mRequestQueue) {
4754 for (const auto& s : request->mOutputStreams) {
4755 if (stream == s) return true;
4756 }
4757 if (stream == request->mInputStream) return true;
4758 }
4759
4760 for (const auto& request : mRepeatingRequests) {
4761 for (const auto& s : request->mOutputStreams) {
4762 if (stream == s) return true;
4763 }
4764 if (stream == request->mInputStream) return true;
4765 }
4766
4767 return false;
4768}
Jianing Weicb0652e2014-03-12 18:29:36 -07004769
Emilian Peev40ead602017-09-26 15:46:36 +01004770bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4771 ATRACE_CALL();
4772 Mutex::Autolock l(mRequestLock);
4773
4774 for (const auto& nextRequest : mNextRequests) {
4775 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4776 if (s.first == streamId) {
4777 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4778 if (it != s.second.end()) {
4779 return true;
4780 }
4781 }
4782 }
4783 }
4784
4785 for (const auto& request : mRequestQueue) {
4786 for (const auto& s : request->mOutputSurfaces) {
4787 if (s.first == streamId) {
4788 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4789 if (it != s.second.end()) {
4790 return true;
4791 }
4792 }
4793 }
4794 }
4795
4796 for (const auto& request : mRepeatingRequests) {
4797 for (const auto& s : request->mOutputSurfaces) {
4798 if (s.first == streamId) {
4799 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4800 if (it != s.second.end()) {
4801 return true;
4802 }
4803 }
4804 }
4805 }
4806
4807 return false;
4808}
4809
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004810nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004811 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004812 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004813 return mExpectedInflightDuration > kMinInflightDuration ?
4814 mExpectedInflightDuration : kMinInflightDuration;
4815}
4816
Emilian Peevaebbe412018-01-15 13:53:24 +00004817void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
4818 camera3_capture_request_t *halRequest) {
4819 if ((request == nullptr) || (halRequest == nullptr)) {
4820 ALOGE("%s: Invalid request!", __FUNCTION__);
4821 return;
4822 }
4823
4824 if (halRequest->num_physcam_settings > 0) {
4825 if (halRequest->physcam_id != nullptr) {
4826 delete [] halRequest->physcam_id;
4827 halRequest->physcam_id = nullptr;
4828 }
4829 if (halRequest->physcam_settings != nullptr) {
4830 auto it = ++(request->mSettingsList.begin());
4831 size_t i = 0;
4832 for (; it != request->mSettingsList.end(); it++, i++) {
4833 it->metadata.unlock(halRequest->physcam_settings[i]);
4834 }
4835 delete [] halRequest->physcam_settings;
4836 halRequest->physcam_settings = nullptr;
4837 }
4838 }
4839}
4840
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004841void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4842 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004843 return;
4844 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004845
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004846 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004847 // Skip the ones that have been submitted successfully.
4848 if (nextRequest.submitted) {
4849 continue;
4850 }
4851
4852 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4853 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4854 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4855
4856 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004857 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004858 }
4859
Emilian Peevaebbe412018-01-15 13:53:24 +00004860 cleanupPhysicalSettings(captureRequest, halRequest);
4861
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004862 if (captureRequest->mInputStream != NULL) {
4863 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4864 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4865 }
4866
4867 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004868 //Buffers that failed processing could still have
4869 //valid acquire fence.
4870 int acquireFence = (*outputBuffers)[i].acquire_fence;
4871 if (0 <= acquireFence) {
4872 close(acquireFence);
4873 outputBuffers->editItemAt(i).acquire_fence = -1;
4874 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004875 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4876 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4877 }
4878
4879 if (sendRequestError) {
4880 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004881 sp<NotificationListener> listener = mListener.promote();
4882 if (listener != NULL) {
4883 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004884 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004885 captureRequest->mResultExtras);
4886 }
4887 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004888
4889 // Remove yet-to-be submitted inflight request from inflightMap
4890 {
4891 sp<Camera3Device> parent = mParent.promote();
4892 if (parent != NULL) {
4893 Mutex::Autolock l(parent->mInFlightLock);
4894 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4895 if (idx >= 0) {
4896 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4897 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4898 parent->removeInFlightMapEntryLocked(idx);
4899 }
4900 }
4901 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004902 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004903
4904 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004905 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004906}
4907
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004908void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004909 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004910 // Optimized a bit for the simple steady-state case (single repeating
4911 // request), to avoid putting that request in the queue temporarily.
4912 Mutex::Autolock l(mRequestLock);
4913
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004914 assert(mNextRequests.empty());
4915
4916 NextRequest nextRequest;
4917 nextRequest.captureRequest = waitForNextRequestLocked();
4918 if (nextRequest.captureRequest == nullptr) {
4919 return;
4920 }
4921
4922 nextRequest.halRequest = camera3_capture_request_t();
4923 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004924 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004925
4926 // Wait for additional requests
4927 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4928
4929 for (size_t i = 1; i < batchSize; i++) {
4930 NextRequest additionalRequest;
4931 additionalRequest.captureRequest = waitForNextRequestLocked();
4932 if (additionalRequest.captureRequest == nullptr) {
4933 break;
4934 }
4935
4936 additionalRequest.halRequest = camera3_capture_request_t();
4937 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004938 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004939 }
4940
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004941 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004942 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004943 mNextRequests.size(), batchSize);
4944 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004945 }
4946
4947 return;
4948}
4949
4950sp<Camera3Device::CaptureRequest>
4951 Camera3Device::RequestThread::waitForNextRequestLocked() {
4952 status_t res;
4953 sp<CaptureRequest> nextRequest;
4954
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004955 while (mRequestQueue.empty()) {
4956 if (!mRepeatingRequests.empty()) {
4957 // Always atomically enqueue all requests in a repeating request
4958 // list. Guarantees a complete in-sequence set of captures to
4959 // application.
4960 const RequestList &requests = mRepeatingRequests;
4961 RequestList::const_iterator firstRequest =
4962 requests.begin();
4963 nextRequest = *firstRequest;
4964 mRequestQueue.insert(mRequestQueue.end(),
4965 ++firstRequest,
4966 requests.end());
4967 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004968
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004969 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004970
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004971 break;
4972 }
4973
4974 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4975
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004976 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4977 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004978 Mutex::Autolock pl(mPauseLock);
4979 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004980 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004981 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004982 // Let the tracker know
4983 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4984 if (statusTracker != 0) {
4985 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4986 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004987 }
4988 // Stop waiting for now and let thread management happen
4989 return NULL;
4990 }
4991 }
4992
4993 if (nextRequest == NULL) {
4994 // Don't have a repeating request already in hand, so queue
4995 // must have an entry now.
4996 RequestList::iterator firstRequest =
4997 mRequestQueue.begin();
4998 nextRequest = *firstRequest;
4999 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005000 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5001 sp<NotificationListener> listener = mListener.promote();
5002 if (listener != NULL) {
5003 listener->notifyRequestQueueEmpty();
5004 }
5005 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005006 }
5007
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005008 // In case we've been unpaused by setPaused clearing mDoPause, need to
5009 // update internal pause state (capture/setRepeatingRequest unpause
5010 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005011 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005012 if (mPaused) {
5013 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5014 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5015 if (statusTracker != 0) {
5016 statusTracker->markComponentActive(mStatusId);
5017 }
5018 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005019 mPaused = false;
5020
5021 // Check if we've reconfigured since last time, and reset the preview
5022 // request if so. Can't use 'NULL request == repeat' across configure calls.
5023 if (mReconfigured) {
5024 mPrevRequest.clear();
5025 mReconfigured = false;
5026 }
5027
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005028 if (nextRequest != NULL) {
5029 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005030 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5031 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005032
5033 // Since RequestThread::clear() removes buffers from the input stream,
5034 // get the right buffer here before unlocking mRequestLock
5035 if (nextRequest->mInputStream != NULL) {
5036 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5037 if (res != OK) {
5038 // Can't get input buffer from gralloc queue - this could be due to
5039 // disconnected queue or other producer misbehavior, so not a fatal
5040 // error
5041 ALOGE("%s: Can't get input buffer, skipping request:"
5042 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005043
5044 sp<NotificationListener> listener = mListener.promote();
5045 if (listener != NULL) {
5046 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005047 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005048 nextRequest->mResultExtras);
5049 }
5050 return NULL;
5051 }
5052 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005053 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005054
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005055 return nextRequest;
5056}
5057
5058bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005059 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005060 status_t res;
5061 Mutex::Autolock l(mPauseLock);
5062 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005063 if (mPaused == false) {
5064 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005065 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5066 // Let the tracker know
5067 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5068 if (statusTracker != 0) {
5069 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5070 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005071 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005072
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005073 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005074 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005075 return true;
5076 }
5077 }
5078 // We don't set mPaused to false here, because waitForNextRequest needs
5079 // to further manage the paused state in case of starvation.
5080 return false;
5081}
5082
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005083void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005084 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005085 // With work to do, mark thread as unpaused.
5086 // If paused by request (setPaused), don't resume, to avoid
5087 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005088 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005089 Mutex::Autolock p(mPauseLock);
5090 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005091 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5092 if (mPaused) {
5093 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5094 if (statusTracker != 0) {
5095 statusTracker->markComponentActive(mStatusId);
5096 }
5097 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005098 mPaused = false;
5099 }
5100}
5101
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005102void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5103 sp<Camera3Device> parent = mParent.promote();
5104 if (parent != NULL) {
5105 va_list args;
5106 va_start(args, fmt);
5107
5108 parent->setErrorStateV(fmt, args);
5109
5110 va_end(args);
5111 }
5112}
5113
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005114status_t Camera3Device::RequestThread::insertTriggers(
5115 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005116 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005117 Mutex::Autolock al(mTriggerMutex);
5118
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005119 sp<Camera3Device> parent = mParent.promote();
5120 if (parent == NULL) {
5121 CLOGE("RequestThread: Parent is gone");
5122 return DEAD_OBJECT;
5123 }
5124
Emilian Peevaebbe412018-01-15 13:53:24 +00005125 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005126 size_t count = mTriggerMap.size();
5127
5128 for (size_t i = 0; i < count; ++i) {
5129 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005130 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005131
5132 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5133 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5134 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005135 if (isAeTrigger) {
5136 request->mResultExtras.precaptureTriggerId = triggerId;
5137 mCurrentPreCaptureTriggerId = triggerId;
5138 } else {
5139 request->mResultExtras.afTriggerId = triggerId;
5140 mCurrentAfTriggerId = triggerId;
5141 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005142 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005143 }
5144
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005145 camera_metadata_entry entry = metadata.find(tag);
5146
5147 if (entry.count > 0) {
5148 /**
5149 * Already has an entry for this trigger in the request.
5150 * Rewrite it with our requested trigger value.
5151 */
5152 RequestTrigger oldTrigger = trigger;
5153
5154 oldTrigger.entryValue = entry.data.u8[0];
5155
5156 mTriggerReplacedMap.add(tag, oldTrigger);
5157 } else {
5158 /**
5159 * More typical, no trigger entry, so we just add it
5160 */
5161 mTriggerRemovedMap.add(tag, trigger);
5162 }
5163
5164 status_t res;
5165
5166 switch (trigger.getTagType()) {
5167 case TYPE_BYTE: {
5168 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5169 res = metadata.update(tag,
5170 &entryValue,
5171 /*count*/1);
5172 break;
5173 }
5174 case TYPE_INT32:
5175 res = metadata.update(tag,
5176 &trigger.entryValue,
5177 /*count*/1);
5178 break;
5179 default:
5180 ALOGE("%s: Type not supported: 0x%x",
5181 __FUNCTION__,
5182 trigger.getTagType());
5183 return INVALID_OPERATION;
5184 }
5185
5186 if (res != OK) {
5187 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5188 ", value %d", __FUNCTION__, trigger.getTagName(),
5189 trigger.entryValue);
5190 return res;
5191 }
5192
5193 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5194 trigger.getTagName(),
5195 trigger.entryValue);
5196 }
5197
5198 mTriggerMap.clear();
5199
5200 return count;
5201}
5202
5203status_t Camera3Device::RequestThread::removeTriggers(
5204 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005205 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005206 Mutex::Autolock al(mTriggerMutex);
5207
Emilian Peevaebbe412018-01-15 13:53:24 +00005208 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005209
5210 /**
5211 * Replace all old entries with their old values.
5212 */
5213 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5214 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5215
5216 status_t res;
5217
5218 uint32_t tag = trigger.metadataTag;
5219 switch (trigger.getTagType()) {
5220 case TYPE_BYTE: {
5221 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5222 res = metadata.update(tag,
5223 &entryValue,
5224 /*count*/1);
5225 break;
5226 }
5227 case TYPE_INT32:
5228 res = metadata.update(tag,
5229 &trigger.entryValue,
5230 /*count*/1);
5231 break;
5232 default:
5233 ALOGE("%s: Type not supported: 0x%x",
5234 __FUNCTION__,
5235 trigger.getTagType());
5236 return INVALID_OPERATION;
5237 }
5238
5239 if (res != OK) {
5240 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5241 ", trigger value %d", __FUNCTION__,
5242 trigger.getTagName(), trigger.entryValue);
5243 return res;
5244 }
5245 }
5246 mTriggerReplacedMap.clear();
5247
5248 /**
5249 * Remove all new entries.
5250 */
5251 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5252 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5253 status_t res = metadata.erase(trigger.metadataTag);
5254
5255 if (res != OK) {
5256 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5257 ", trigger value %d", __FUNCTION__,
5258 trigger.getTagName(), trigger.entryValue);
5259 return res;
5260 }
5261 }
5262 mTriggerRemovedMap.clear();
5263
5264 return OK;
5265}
5266
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005267status_t Camera3Device::RequestThread::addDummyTriggerIds(
5268 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005269 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005270 static const int32_t dummyTriggerId = 1;
5271 status_t res;
5272
Emilian Peevaebbe412018-01-15 13:53:24 +00005273 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005274
5275 // If AF trigger is active, insert a dummy AF trigger ID if none already
5276 // exists
5277 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5278 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5279 if (afTrigger.count > 0 &&
5280 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5281 afId.count == 0) {
5282 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5283 if (res != OK) return res;
5284 }
5285
5286 // If AE precapture trigger is active, insert a dummy precapture trigger ID
5287 // if none already exists
5288 camera_metadata_entry pcTrigger =
5289 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5290 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5291 if (pcTrigger.count > 0 &&
5292 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5293 pcId.count == 0) {
5294 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5295 &dummyTriggerId, 1);
5296 if (res != OK) return res;
5297 }
5298
5299 return OK;
5300}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005301
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005302/**
5303 * PreparerThread inner class methods
5304 */
5305
5306Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005307 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005308 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005309}
5310
5311Camera3Device::PreparerThread::~PreparerThread() {
5312 Thread::requestExitAndWait();
5313 if (mCurrentStream != nullptr) {
5314 mCurrentStream->cancelPrepare();
5315 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5316 mCurrentStream.clear();
5317 }
5318 clear();
5319}
5320
Ruben Brunkc78ac262015-08-13 17:58:46 -07005321status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005322 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005323 status_t res;
5324
5325 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005326 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005327
Ruben Brunkc78ac262015-08-13 17:58:46 -07005328 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005329 if (res == OK) {
5330 // No preparation needed, fire listener right off
5331 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005332 if (listener != NULL) {
5333 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005334 }
5335 return OK;
5336 } else if (res != NOT_ENOUGH_DATA) {
5337 return res;
5338 }
5339
5340 // Need to prepare, start up thread if necessary
5341 if (!mActive) {
5342 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5343 // isn't running
5344 Thread::requestExitAndWait();
5345 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5346 if (res != OK) {
5347 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005348 if (listener != NULL) {
5349 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005350 }
5351 return res;
5352 }
5353 mCancelNow = false;
5354 mActive = true;
5355 ALOGV("%s: Preparer stream started", __FUNCTION__);
5356 }
5357
5358 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005359 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005360 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5361
5362 return OK;
5363}
5364
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005365void Camera3Device::PreparerThread::pause() {
5366 ATRACE_CALL();
5367
5368 Mutex::Autolock l(mLock);
5369
5370 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5371 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5372 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5373 int currentMaxCount = mCurrentMaxCount;
5374 mPendingStreams.clear();
5375 mCancelNow = true;
5376 while (mActive) {
5377 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5378 if (res == TIMED_OUT) {
5379 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5380 return;
5381 } else if (res != OK) {
5382 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5383 return;
5384 }
5385 }
5386
5387 //Check whether the prepare thread was able to complete the current
5388 //stream. In case work is still pending emplace it along with the rest
5389 //of the streams in the pending list.
5390 if (currentStream != nullptr) {
5391 if (!mCurrentPrepareComplete) {
5392 pendingStreams.emplace(currentMaxCount, currentStream);
5393 }
5394 }
5395
5396 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5397 for (const auto& it : mPendingStreams) {
5398 it.second->cancelPrepare();
5399 }
5400}
5401
5402status_t Camera3Device::PreparerThread::resume() {
5403 ATRACE_CALL();
5404 status_t res;
5405
5406 Mutex::Autolock l(mLock);
5407 sp<NotificationListener> listener = mListener.promote();
5408
5409 if (mActive) {
5410 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5411 return NO_INIT;
5412 }
5413
5414 auto it = mPendingStreams.begin();
5415 for (; it != mPendingStreams.end();) {
5416 res = it->second->startPrepare(it->first);
5417 if (res == OK) {
5418 if (listener != NULL) {
5419 listener->notifyPrepared(it->second->getId());
5420 }
5421 it = mPendingStreams.erase(it);
5422 } else if (res != NOT_ENOUGH_DATA) {
5423 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5424 res, strerror(-res));
5425 it = mPendingStreams.erase(it);
5426 } else {
5427 it++;
5428 }
5429 }
5430
5431 if (mPendingStreams.empty()) {
5432 return OK;
5433 }
5434
5435 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5436 if (res != OK) {
5437 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5438 __FUNCTION__, res, strerror(-res));
5439 return res;
5440 }
5441 mCancelNow = false;
5442 mActive = true;
5443 ALOGV("%s: Preparer stream started", __FUNCTION__);
5444
5445 return OK;
5446}
5447
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005448status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005449 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005450 Mutex::Autolock l(mLock);
5451
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005452 for (const auto& it : mPendingStreams) {
5453 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005454 }
5455 mPendingStreams.clear();
5456 mCancelNow = true;
5457
5458 return OK;
5459}
5460
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005461void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005462 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005463 Mutex::Autolock l(mLock);
5464 mListener = listener;
5465}
5466
5467bool Camera3Device::PreparerThread::threadLoop() {
5468 status_t res;
5469 {
5470 Mutex::Autolock l(mLock);
5471 if (mCurrentStream == nullptr) {
5472 // End thread if done with work
5473 if (mPendingStreams.empty()) {
5474 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5475 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5476 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5477 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005478 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005479 return false;
5480 }
5481
5482 // Get next stream to prepare
5483 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005484 mCurrentStream = it->second;
5485 mCurrentMaxCount = it->first;
5486 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005487 mPendingStreams.erase(it);
5488 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5489 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5490 } else if (mCancelNow) {
5491 mCurrentStream->cancelPrepare();
5492 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5493 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5494 mCurrentStream.clear();
5495 mCancelNow = false;
5496 return true;
5497 }
5498 }
5499
5500 res = mCurrentStream->prepareNextBuffer();
5501 if (res == NOT_ENOUGH_DATA) return true;
5502 if (res != OK) {
5503 // Something bad happened; try to recover by cancelling prepare and
5504 // signalling listener anyway
5505 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5506 mCurrentStream->getId(), res, strerror(-res));
5507 mCurrentStream->cancelPrepare();
5508 }
5509
5510 // This stream has finished, notify listener
5511 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005512 sp<NotificationListener> listener = mListener.promote();
5513 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005514 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5515 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005516 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005517 }
5518
5519 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5520 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005521 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005522
5523 return true;
5524}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005525
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005526/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005527 * Static callback forwarding methods from HAL to instance
5528 */
5529
5530void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5531 const camera3_capture_result *result) {
5532 Camera3Device *d =
5533 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07005534
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005535 d->processCaptureResult(result);
5536}
5537
5538void Camera3Device::sNotify(const camera3_callback_ops *cb,
5539 const camera3_notify_msg *msg) {
5540 Camera3Device *d =
5541 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5542 d->notify(msg);
5543}
5544
5545}; // namespace android