blob: c443b204c192c86f42d1a6b49c4f21f83fab417e [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
Emilian Peev5fbe0ba2017-10-20 15:45:45 +010058#include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
59
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080061using namespace android::hardware::camera;
62using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063
64namespace android {
65
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080066Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080067 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080068 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070069 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070070 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070071 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070072 mUsePartialResult(false),
73 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080074 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070076 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070077 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070078 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000079 mListener(NULL),
80 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080081{
82 ATRACE_CALL();
83 camera3_callback_ops::notify = &sNotify;
84 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080085 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086}
87
88Camera3Device::~Camera3Device()
89{
90 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080091 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092 disconnect();
93}
94
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080095const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080096 return mId;
97}
98
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080099status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
100 ATRACE_CALL();
101 Mutex::Autolock il(mInterfaceLock);
102 Mutex::Autolock l(mLock);
103
104 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
105 if (mStatus != STATUS_UNINITIALIZED) {
106 CLOGE("Already initialized!");
107 return INVALID_OPERATION;
108 }
109 if (manager == nullptr) return INVALID_OPERATION;
110
111 sp<ICameraDeviceSession> session;
112 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800113 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800114 /*out*/ &session);
115 ATRACE_END();
116 if (res != OK) {
117 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
118 return res;
119 }
120
Steven Moreland5ff9c912017-03-09 23:13:00 -0800121 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800122 if (res != OK) {
123 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
124 session->close();
125 return res;
126 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800127
Yifan Hongf79b5542017-04-11 14:44:25 -0700128 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700129 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
130 [&queue](const auto& descriptor) {
131 queue = std::make_shared<RequestMetadataQueue>(descriptor);
132 if (!queue->isValid() || queue->availableToWrite() <= 0) {
133 ALOGE("HAL returns empty request metadata fmq, not use it");
134 queue = nullptr;
135 // don't use the queue onwards.
136 }
137 });
138 if (!requestQueueRet.isOk()) {
139 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
140 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700141 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700142 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700143
144 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700145 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700146 [&resQueue](const auto& descriptor) {
147 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
148 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700149 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700150 resQueue = nullptr;
151 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700152 }
153 });
154 if (!resultQueueRet.isOk()) {
155 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
156 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700157 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700158 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700159 IF_ALOGV() {
160 session->interfaceChain([](
161 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
162 ALOGV("Session interface chain:");
163 for (auto iface : interfaceChain) {
164 ALOGV(" %s", iface.c_str());
165 }
166 });
167 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700168
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700169 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000170 std::string providerType;
171 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800172
173 return initializeCommonLocked();
174}
175
176status_t Camera3Device::initializeCommonLocked() {
177
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700178 /** Start up status tracker thread */
179 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800180 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700181 if (res != OK) {
182 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
183 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800184 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700185 mStatusTracker.clear();
186 return res;
187 }
188
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700189 /** Register in-flight map to the status tracker */
190 mInFlightStatusId = mStatusTracker->addComponent();
191
Zhijun He125684a2015-12-26 15:07:30 -0800192 /** Create buffer manager */
193 mBufferManager = new Camera3BufferManager();
194
Emilian Peev71c73a22017-03-21 16:35:51 +0000195 mTagMonitor.initialize(mVendorTagId);
196
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000197 Vector<int32_t> sessionParamKeys;
198 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
199 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
200 if (sessionKeysEntry.count > 0) {
201 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
202 }
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700203 /** Start up request queue thread */
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000204 mRequestThread = new RequestThread(this, mStatusTracker, mInterface, sessionParamKeys);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800205 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800206 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700207 SET_ERR_L("Unable to start request queue thread: %s (%d)",
208 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800209 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800210 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800211 return res;
212 }
213
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700214 mPreparerThread = new PreparerThread();
215
Ruben Brunk183f0562015-08-12 12:55:02 -0700216 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800217 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700218 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700219 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700220 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800221
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800222 // Measure the clock domain offset between camera and video/hw_composer
223 camera_metadata_entry timestampSource =
224 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
225 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
226 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
227 mTimestampOffset = getMonoToBoottimeOffset();
228 }
229
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700230 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100231 camera_metadata_entry partialResultsCount =
232 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
233 if (partialResultsCount.count > 0) {
234 mNumPartialResults = partialResultsCount.data.i32[0];
235 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700236 }
237
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700238 camera_metadata_entry configs =
239 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
240 for (uint32_t i = 0; i < configs.count; i += 4) {
241 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
242 configs.data.i32[i + 3] ==
243 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
244 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
245 configs.data.i32[i + 2]));
246 }
247 }
248
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800249 return OK;
250}
251
252status_t Camera3Device::disconnect() {
253 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700254 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800255
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700256 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700258 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700259 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700260 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 {
262 Mutex::Autolock l(mLock);
263 if (mStatus == STATUS_UNINITIALIZED) return res;
264
265 if (mStatus == STATUS_ACTIVE ||
266 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
267 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700268 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700272 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700274 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
275 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276 // Continue to close device even in case of error
277 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 if (mStatus == STATUS_ERROR) {
282 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700283 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284
285 if (mStatusTracker != NULL) {
286 mStatusTracker->requestExit();
287 }
288
289 if (mRequestThread != NULL) {
290 mRequestThread->requestExit();
291 }
292
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700293 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
294 for (size_t i = 0; i < mOutputStreams.size(); i++) {
295 streams.push_back(mOutputStreams[i]);
296 }
297 if (mInputStream != nullptr) {
298 streams.push_back(mInputStream);
299 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700300 }
301
302 // Joining done without holding mLock, otherwise deadlocks may ensue
303 // as the threads try to access parent state
304 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
305 // HAL may be in a bad state, so waiting for request thread
306 // (which may be stuck in the HAL processCaptureRequest call)
307 // could be dangerous.
308 mRequestThread->join();
309 }
310
311 if (mStatusTracker != NULL) {
312 mStatusTracker->join();
313 }
314
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800315 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 {
317 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700319 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800320 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700321 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700323 // Call close without internal mutex held, as the HAL close may need to
324 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800325 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700326
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700327 flushInflightRequests();
328
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700329 {
330 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800331 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700332 mOutputStreams.clear();
333 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700334 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700335 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700336 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700337 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700339 for (auto& weakStream : streams) {
340 sp<Camera3StreamInterface> stream = weakStream.promote();
341 if (stream != nullptr) {
342 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
343 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
344 }
345 }
346
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700347 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700348 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800349}
350
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700351// For dumping/debugging only -
352// try to acquire a lock a few times, eventually give up to proceed with
353// debug/dump operations
354bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
355 bool gotLock = false;
356 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
357 if (lock.tryLock() == NO_ERROR) {
358 gotLock = true;
359 break;
360 } else {
361 usleep(kDumpSleepDuration);
362 }
363 }
364 return gotLock;
365}
366
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700367Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
368 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100369 const int STREAM_CONFIGURATION_SIZE = 4;
370 const int STREAM_FORMAT_OFFSET = 0;
371 const int STREAM_WIDTH_OFFSET = 1;
372 const int STREAM_HEIGHT_OFFSET = 2;
373 const int STREAM_IS_INPUT_OFFSET = 3;
374 camera_metadata_ro_entry_t availableStreamConfigs =
375 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
376 if (availableStreamConfigs.count == 0 ||
377 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
378 return Size(0, 0);
379 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700380
Emilian Peev08dd2452017-04-06 16:55:14 +0100381 // Get max jpeg size (area-wise).
382 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
383 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
384 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
385 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
386 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
387 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
388 && format == HAL_PIXEL_FORMAT_BLOB &&
389 (width * height > maxJpegWidth * maxJpegHeight)) {
390 maxJpegWidth = width;
391 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700392 }
393 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100394
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700395 return Size(maxJpegWidth, maxJpegHeight);
396}
397
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800398nsecs_t Camera3Device::getMonoToBoottimeOffset() {
399 // try three times to get the clock offset, choose the one
400 // with the minimum gap in measurements.
401 const int tries = 3;
402 nsecs_t bestGap, measured;
403 for (int i = 0; i < tries; ++i) {
404 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
405 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
406 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
407 const nsecs_t gap = tmono2 - tmono;
408 if (i == 0 || gap < bestGap) {
409 bestGap = gap;
410 measured = tbase - ((tmono + tmono2) >> 1);
411 }
412 }
413 return measured;
414}
415
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800416hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
417 int frameworkFormat) {
418 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
419}
420
421DataspaceFlags Camera3Device::mapToHidlDataspace(
422 android_dataspace dataSpace) {
423 return dataSpace;
424}
425
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700426BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100427 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700428 return usage;
429}
430
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800431StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
432 switch (rotation) {
433 case CAMERA3_STREAM_ROTATION_0:
434 return StreamRotation::ROTATION_0;
435 case CAMERA3_STREAM_ROTATION_90:
436 return StreamRotation::ROTATION_90;
437 case CAMERA3_STREAM_ROTATION_180:
438 return StreamRotation::ROTATION_180;
439 case CAMERA3_STREAM_ROTATION_270:
440 return StreamRotation::ROTATION_270;
441 }
442 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
443 return StreamRotation::ROTATION_0;
444}
445
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800446status_t Camera3Device::mapToStreamConfigurationMode(
447 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
448 if (mode == nullptr) return BAD_VALUE;
449 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
450 switch(operationMode) {
451 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
452 *mode = StreamConfigurationMode::NORMAL_MODE;
453 break;
454 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
455 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
456 break;
457 default:
458 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
459 return BAD_VALUE;
460 }
461 } else {
462 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800463 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800464 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800465}
466
467camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
468 switch (status) {
469 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
470 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
471 }
472 return CAMERA3_BUFFER_STATUS_ERROR;
473}
474
475int Camera3Device::mapToFrameworkFormat(
476 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
477 return static_cast<uint32_t>(pixelFormat);
478}
479
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700480android_dataspace Camera3Device::mapToFrameworkDataspace(
481 DataspaceFlags dataSpace) {
482 return static_cast<android_dataspace>(dataSpace);
483}
484
Emilian Peev050f5dc2017-05-18 14:43:56 +0100485uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700486 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700487 return usage;
488}
489
Emilian Peev050f5dc2017-05-18 14:43:56 +0100490uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700491 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700492 return usage;
493}
494
Zhijun Hef7da0962014-04-24 13:27:56 -0700495ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700496 // Get max jpeg size (area-wise).
497 Size maxJpegResolution = getMaxJpegResolution();
498 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800499 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
500 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700501 return BAD_VALUE;
502 }
503
Zhijun Hef7da0962014-04-24 13:27:56 -0700504 // Get max jpeg buffer size
505 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700506 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
507 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800508 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
509 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700510 return BAD_VALUE;
511 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700512 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800513 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700514
515 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700516 float scaleFactor = ((float) (width * height)) /
517 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800518 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
519 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700520 if (jpegBufferSize > maxJpegBufferSize) {
521 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700522 }
523
524 return jpegBufferSize;
525}
526
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700527ssize_t Camera3Device::getPointCloudBufferSize() const {
528 const int FLOATS_PER_POINT=4;
529 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
530 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800531 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
532 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700533 return BAD_VALUE;
534 }
535 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
536 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
537 return maxBytesForPointCloud;
538}
539
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800540ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800541 const int PER_CONFIGURATION_SIZE = 3;
542 const int WIDTH_OFFSET = 0;
543 const int HEIGHT_OFFSET = 1;
544 const int SIZE_OFFSET = 2;
545 camera_metadata_ro_entry rawOpaqueSizes =
546 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800547 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800548 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800549 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
550 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800551 return BAD_VALUE;
552 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700553
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800554 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
555 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
556 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
557 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
558 }
559 }
560
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800561 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
562 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800563 return BAD_VALUE;
564}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700565
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800566status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
567 ATRACE_CALL();
568 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700569
570 // Try to lock, but continue in case of failure (to avoid blocking in
571 // deadlocks)
572 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
573 bool gotLock = tryLockSpinRightRound(mLock);
574
575 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800576 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
577 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700578 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800579 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
580 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700581
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800582 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700583
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800584 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700585 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800586 int n = args.size();
587 for (int i = 0; i < n; i++) {
588 if (args[i] == templatesOption) {
589 dumpTemplates = true;
590 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700591 if (args[i] == monitorOption) {
592 if (i + 1 < n) {
593 String8 monitorTags = String8(args[i + 1]);
594 if (monitorTags == "off") {
595 mTagMonitor.disableMonitoring();
596 } else {
597 mTagMonitor.parseTagsToMonitor(monitorTags);
598 }
599 } else {
600 mTagMonitor.disableMonitoring();
601 }
602 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800603 }
604
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800605 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800606
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607 const char *status =
608 mStatus == STATUS_ERROR ? "ERROR" :
609 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700610 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
611 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612 mStatus == STATUS_ACTIVE ? "ACTIVE" :
613 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800615 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700616 if (mStatus == STATUS_ERROR) {
617 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
618 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800619 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800620 const char *mode =
621 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
622 mOperatingMode == static_cast<int>(
623 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
624 "CUSTOM";
625 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626
627 if (mInputStream != NULL) {
628 write(fd, lines.string(), lines.size());
629 mInputStream->dump(fd, args);
630 } else {
631 lines.appendFormat(" No input stream.\n");
632 write(fd, lines.string(), lines.size());
633 }
634 for (size_t i = 0; i < mOutputStreams.size(); i++) {
635 mOutputStreams[i]->dump(fd,args);
636 }
637
Zhijun He431503c2016-03-07 17:30:16 -0800638 if (mBufferManager != NULL) {
639 lines = String8(" Camera3 Buffer Manager:\n");
640 write(fd, lines.string(), lines.size());
641 mBufferManager->dump(fd, args);
642 }
Zhijun He125684a2015-12-26 15:07:30 -0800643
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700644 lines = String8(" In-flight requests:\n");
645 if (mInFlightMap.size() == 0) {
646 lines.append(" None\n");
647 } else {
648 for (size_t i = 0; i < mInFlightMap.size(); i++) {
649 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700650 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700651 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800652 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700653 r.numBuffersLeft);
654 }
655 }
656 write(fd, lines.string(), lines.size());
657
Shuzhen Wang686f6442017-06-20 16:16:04 -0700658 if (mRequestThread != NULL) {
659 mRequestThread->dumpCaptureRequestLatency(fd,
660 " ProcessCaptureRequest latency histogram:");
661 }
662
Igor Murashkin1e479c02013-09-06 16:55:14 -0700663 {
664 lines = String8(" Last request sent:\n");
665 write(fd, lines.string(), lines.size());
666
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700667 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700668 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
669 }
670
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800671 if (dumpTemplates) {
672 const char *templateNames[] = {
673 "TEMPLATE_PREVIEW",
674 "TEMPLATE_STILL_CAPTURE",
675 "TEMPLATE_VIDEO_RECORD",
676 "TEMPLATE_VIDEO_SNAPSHOT",
677 "TEMPLATE_ZERO_SHUTTER_LAG",
678 "TEMPLATE_MANUAL"
679 };
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),
3249 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003250
Emilian Peev31abd0a2017-05-11 18:37:46 +01003251Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003252
3253Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003254 mHidlSession(other.mHidlSession),
3255 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003256
3257bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003258 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003259}
3260
3261void Camera3Device::HalInterface::clear() {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003262 mHidlSession.clear();
3263}
3264
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003265bool Camera3Device::HalInterface::supportBatchRequest() {
3266 return mHidlSession != nullptr;
3267}
3268
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003269status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3270 camera3_request_template_t templateId,
3271 /*out*/ camera_metadata_t **requestTemplate) {
3272 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3273 if (!valid()) return INVALID_OPERATION;
3274 status_t res = OK;
3275
Emilian Peev31abd0a2017-05-11 18:37:46 +01003276 common::V1_0::Status status;
3277 RequestTemplate id;
3278 switch (templateId) {
3279 case CAMERA3_TEMPLATE_PREVIEW:
3280 id = RequestTemplate::PREVIEW;
3281 break;
3282 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3283 id = RequestTemplate::STILL_CAPTURE;
3284 break;
3285 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3286 id = RequestTemplate::VIDEO_RECORD;
3287 break;
3288 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3289 id = RequestTemplate::VIDEO_SNAPSHOT;
3290 break;
3291 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3292 id = RequestTemplate::ZERO_SHUTTER_LAG;
3293 break;
3294 case CAMERA3_TEMPLATE_MANUAL:
3295 id = RequestTemplate::MANUAL;
3296 break;
3297 default:
3298 // Unknown template ID
3299 return BAD_VALUE;
3300 }
3301 auto err = mHidlSession->constructDefaultRequestSettings(id,
3302 [&status, &requestTemplate]
3303 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3304 status = s;
3305 if (status == common::V1_0::Status::OK) {
3306 const camera_metadata *r =
3307 reinterpret_cast<const camera_metadata_t*>(request.data());
3308 size_t expectedSize = request.size();
3309 int ret = validate_camera_metadata_structure(r, &expectedSize);
3310 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3311 *requestTemplate = clone_camera_metadata(r);
3312 if (*requestTemplate == nullptr) {
3313 ALOGE("%s: Unable to clone camera metadata received from HAL",
3314 __FUNCTION__);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003315 status = common::V1_0::Status::INTERNAL_ERROR;
3316 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003317 } else {
3318 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3319 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003320 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003321 }
3322 });
3323 if (!err.isOk()) {
3324 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3325 res = DEAD_OBJECT;
3326 } else {
3327 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003328 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003329
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003330 return res;
3331}
3332
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003333status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
3334 camera3_stream_configuration *config) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003335 ATRACE_NAME("CameraHal::configureStreams");
3336 if (!valid()) return INVALID_OPERATION;
3337 status_t res = OK;
3338
Emilian Peev31abd0a2017-05-11 18:37:46 +01003339 // Convert stream config to HIDL
3340 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003341 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3342 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3343 requestedConfiguration3_2.streams.resize(config->num_streams);
3344 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003345 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003346 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3347 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003348 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003349
Emilian Peev31abd0a2017-05-11 18:37:46 +01003350 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3351 cam3stream->setBufferFreedListener(this);
3352 int streamId = cam3stream->getId();
3353 StreamType streamType;
3354 switch (src->stream_type) {
3355 case CAMERA3_STREAM_OUTPUT:
3356 streamType = StreamType::OUTPUT;
3357 break;
3358 case CAMERA3_STREAM_INPUT:
3359 streamType = StreamType::INPUT;
3360 break;
3361 default:
3362 ALOGE("%s: Stream %d: Unsupported stream type %d",
3363 __FUNCTION__, streamId, config->streams[i]->stream_type);
3364 return BAD_VALUE;
3365 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003366 dst3_2.id = streamId;
3367 dst3_2.streamType = streamType;
3368 dst3_2.width = src->width;
3369 dst3_2.height = src->height;
3370 dst3_2.format = mapToPixelFormat(src->format);
3371 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3372 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3373 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3374 dst3_4.v3_2 = dst3_2;
3375 if (src->physical_camera_id != nullptr) {
3376 dst3_4.physicalCameraId = src->physical_camera_id;
3377 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003378
3379 activeStreams.insert(streamId);
3380 // Create Buffer ID map if necessary
3381 if (mBufferIdMaps.count(streamId) == 0) {
3382 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3383 }
3384 }
3385 // remove BufferIdMap for deleted streams
3386 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3387 int streamId = it->first;
3388 bool active = activeStreams.count(streamId) > 0;
3389 if (!active) {
3390 it = mBufferIdMaps.erase(it);
3391 } else {
3392 ++it;
3393 }
3394 }
3395
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003396 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003397 res = mapToStreamConfigurationMode(
3398 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003399 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003400 if (res != OK) {
3401 return res;
3402 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003403 requestedConfiguration3_2.operationMode = operationMode;
3404 requestedConfiguration3_4.operationMode = operationMode;
3405 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003406 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3407 get_camera_metadata_size(sessionParams));
3408
Emilian Peev31abd0a2017-05-11 18:37:46 +01003409 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003410 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003411 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003412
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003413 // See if we have v3.4 or v3.3 HAL
3414 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003415 sp<device::V3_3::ICameraDeviceSession> hidlSession_3_3;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003416 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3417 if (castResult_3_4.isOk()) {
3418 hidlSession_3_4 = castResult_3_4;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003419 } else {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003420 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3421 if (castResult_3_3.isOk()) {
3422 hidlSession_3_3 = castResult_3_3;
3423 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003424 }
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003425
3426 if (hidlSession_3_4 != nullptr) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003427 // We do; use v3.4 for the call, and construct a v3.4
3428 // HalStreamConfiguration
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003429 ALOGV("%s: v3.4 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003430 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
3431 auto err = hidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
3432 [&status, &finalConfiguration3_4]
3433 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3434 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003435 status = s;
3436 });
3437 if (!err.isOk()) {
3438 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3439 return DEAD_OBJECT;
3440 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003441 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3442 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3443 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3444 }
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003445 } else if (hidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003446 // We do; use v3.3 for the call
3447 ALOGV("%s: v3.3 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003448 auto err = hidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003449 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003450 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003451 finalConfiguration = halConfiguration;
3452 status = s;
3453 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003454 if (!err.isOk()) {
3455 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3456 return DEAD_OBJECT;
3457 }
3458 } else {
3459 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3460 ALOGV("%s: v3.2 device found", __FUNCTION__);
3461 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003462 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003463 [&status, &finalConfiguration_3_2]
3464 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3465 finalConfiguration_3_2 = halConfiguration;
3466 status = s;
3467 });
3468 if (!err.isOk()) {
3469 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3470 return DEAD_OBJECT;
3471 }
3472 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3473 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3474 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3475 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003476 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003477 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003478 }
3479
3480 if (status != common::V1_0::Status::OK ) {
3481 return CameraProviderManager::mapToStatusT(status);
3482 }
3483
3484 // And convert output stream configuration from HIDL
3485
3486 for (size_t i = 0; i < config->num_streams; i++) {
3487 camera3_stream_t *dst = config->streams[i];
3488 int streamId = Camera3Stream::cast(dst)->getId();
3489
3490 // Start scan at i, with the assumption that the stream order matches
3491 size_t realIdx = i;
3492 bool found = false;
3493 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003494 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003495 found = true;
3496 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003497 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003498 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3499 }
3500 if (!found) {
3501 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3502 __FUNCTION__, streamId);
3503 return INVALID_OPERATION;
3504 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003505 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003506
Emilian Peev710c1422017-08-30 11:19:38 +01003507 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3508 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003509 dstStream->setDataSpaceOverride(false);
3510 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3511 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3512
Emilian Peev31abd0a2017-05-11 18:37:46 +01003513 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3514 if (dst->format != overrideFormat) {
3515 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3516 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003517 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003518 if (dst->data_space != overrideDataSpace) {
3519 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3520 streamId, dst->format);
3521 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003522 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003523 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003524 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3525
Emilian Peev31abd0a2017-05-11 18:37:46 +01003526 // Override allowed with IMPLEMENTATION_DEFINED
3527 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003528 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003529 }
3530
Emilian Peev31abd0a2017-05-11 18:37:46 +01003531 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003532 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003533 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003534 __FUNCTION__, streamId);
3535 return INVALID_OPERATION;
3536 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003537 dstStream->setUsage(
3538 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003539 } else {
3540 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003541 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003542 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3543 __FUNCTION__, streamId);
3544 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003545 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003546 dstStream->setUsage(
3547 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003548 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003549 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003550 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003551
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003552 return res;
3553}
3554
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003555void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3556 /*out*/device::V3_2::CaptureRequest* captureRequest,
3557 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003558 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003559 if (captureRequest == nullptr || handlesCreated == nullptr) {
3560 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3561 __FUNCTION__, captureRequest, handlesCreated);
3562 return;
3563 }
3564
3565 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003566
3567 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003568
3569 {
3570 std::lock_guard<std::mutex> lock(mInflightLock);
3571 if (request->input_buffer != nullptr) {
3572 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3573 buffer_handle_t buf = *(request->input_buffer->buffer);
3574 auto pair = getBufferId(buf, streamId);
3575 bool isNewBuffer = pair.first;
3576 uint64_t bufferId = pair.second;
3577 captureRequest->inputBuffer.streamId = streamId;
3578 captureRequest->inputBuffer.bufferId = bufferId;
3579 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3580 captureRequest->inputBuffer.status = BufferStatus::OK;
3581 native_handle_t *acquireFence = nullptr;
3582 if (request->input_buffer->acquire_fence != -1) {
3583 acquireFence = native_handle_create(1,0);
3584 acquireFence->data[0] = request->input_buffer->acquire_fence;
3585 handlesCreated->push_back(acquireFence);
3586 }
3587 captureRequest->inputBuffer.acquireFence = acquireFence;
3588 captureRequest->inputBuffer.releaseFence = nullptr;
3589
3590 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3591 request->input_buffer->buffer,
3592 request->input_buffer->acquire_fence);
3593 } else {
3594 captureRequest->inputBuffer.streamId = -1;
3595 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3596 }
3597
3598 captureRequest->outputBuffers.resize(request->num_output_buffers);
3599 for (size_t i = 0; i < request->num_output_buffers; i++) {
3600 const camera3_stream_buffer_t *src = request->output_buffers + i;
3601 StreamBuffer &dst = captureRequest->outputBuffers[i];
3602 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3603 buffer_handle_t buf = *(src->buffer);
3604 auto pair = getBufferId(buf, streamId);
3605 bool isNewBuffer = pair.first;
3606 dst.streamId = streamId;
3607 dst.bufferId = pair.second;
3608 dst.buffer = isNewBuffer ? buf : nullptr;
3609 dst.status = BufferStatus::OK;
3610 native_handle_t *acquireFence = nullptr;
3611 if (src->acquire_fence != -1) {
3612 acquireFence = native_handle_create(1,0);
3613 acquireFence->data[0] = src->acquire_fence;
3614 handlesCreated->push_back(acquireFence);
3615 }
3616 dst.acquireFence = acquireFence;
3617 dst.releaseFence = nullptr;
3618
3619 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3620 src->buffer, src->acquire_fence);
3621 }
3622 }
3623}
3624
3625status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3626 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3627 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3628 if (!valid()) return INVALID_OPERATION;
3629
Emilian Peevaebbe412018-01-15 13:53:24 +00003630 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3631 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3632 if (castResult_3_4.isOk()) {
3633 hidlSession_3_4 = castResult_3_4;
3634 }
3635
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003636 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003637 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003638 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00003639 if (hidlSession_3_4 != nullptr) {
3640 captureRequests_3_4.resize(batchSize);
3641 } else {
3642 captureRequests.resize(batchSize);
3643 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003644 std::vector<native_handle_t*> handlesCreated;
3645
3646 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003647 if (hidlSession_3_4 != nullptr) {
3648 wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3649 /*out*/&handlesCreated);
3650 } else {
3651 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3652 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003653 }
3654
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003655 std::vector<device::V3_2::BufferCache> cachesToRemove;
3656 {
3657 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3658 for (auto& pair : mFreedBuffers) {
3659 // The stream might have been removed since onBufferFreed
3660 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3661 cachesToRemove.push_back({pair.first, pair.second});
3662 }
3663 }
3664 mFreedBuffers.clear();
3665 }
3666
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003667 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3668 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003669
3670 // Write metadata to FMQ.
3671 for (size_t i = 0; i < batchSize; i++) {
3672 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003673 device::V3_2::CaptureRequest* captureRequest;
3674 if (hidlSession_3_4 != nullptr) {
3675 captureRequest = &captureRequests_3_4[i].v3_2;
3676 } else {
3677 captureRequest = &captureRequests[i];
3678 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003679
3680 if (request->settings != nullptr) {
3681 size_t settingsSize = get_camera_metadata_size(request->settings);
3682 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3683 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3684 captureRequest->settings.resize(0);
3685 captureRequest->fmqSettingsSize = settingsSize;
3686 } else {
3687 if (mRequestMetadataQueue != nullptr) {
3688 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3689 }
3690 captureRequest->settings.setToExternal(
3691 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3692 get_camera_metadata_size(request->settings));
3693 captureRequest->fmqSettingsSize = 0u;
3694 }
3695 } else {
3696 // A null request settings maps to a size-0 CameraMetadata
3697 captureRequest->settings.resize(0);
3698 captureRequest->fmqSettingsSize = 0u;
3699 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003700
3701 if (hidlSession_3_4 != nullptr) {
3702 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3703 for (size_t j = 0; j < request->num_physcam_settings; j++) {
3704 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3705 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3706 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3707 settingsSize)) {
3708 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3709 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = settingsSize;
3710 } else {
3711 if (mRequestMetadataQueue != nullptr) {
3712 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3713 }
3714 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3715 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3716 request->physcam_settings[j])),
3717 get_camera_metadata_size(request->physcam_settings[j]));
3718 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
3719 }
3720 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3721 request->physcam_id[j];
3722 }
3723 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003724 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003725
3726 hardware::details::return_status err;
3727 if (hidlSession_3_4 != nullptr) {
3728 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003729 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3730 status = s;
3731 *numRequestProcessed = n;
3732 });
Emilian Peevaebbe412018-01-15 13:53:24 +00003733 } else {
3734 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
3735 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3736 status = s;
3737 *numRequestProcessed = n;
3738 });
3739 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003740 if (!err.isOk()) {
3741 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3742 return DEAD_OBJECT;
3743 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003744 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3745 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3746 __FUNCTION__, *numRequestProcessed, batchSize);
3747 status = common::V1_0::Status::INTERNAL_ERROR;
3748 }
3749
3750 for (auto& handle : handlesCreated) {
3751 native_handle_delete(handle);
3752 }
3753 return CameraProviderManager::mapToStatusT(status);
3754}
3755
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003756status_t Camera3Device::HalInterface::processCaptureRequest(
3757 camera3_capture_request_t *request) {
3758 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003759 if (!valid()) return INVALID_OPERATION;
3760 status_t res = OK;
3761
Emilian Peev31abd0a2017-05-11 18:37:46 +01003762 uint32_t numRequestProcessed = 0;
3763 std::vector<camera3_capture_request_t*> requests(1);
3764 requests[0] = request;
3765 res = processBatchCaptureRequests(requests, &numRequestProcessed);
3766
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003767 return res;
3768}
3769
3770status_t Camera3Device::HalInterface::flush() {
3771 ATRACE_NAME("CameraHal::flush");
3772 if (!valid()) return INVALID_OPERATION;
3773 status_t res = OK;
3774
Emilian Peev31abd0a2017-05-11 18:37:46 +01003775 auto err = mHidlSession->flush();
3776 if (!err.isOk()) {
3777 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3778 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003779 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003780 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003781 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003782
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003783 return res;
3784}
3785
Emilian Peev31abd0a2017-05-11 18:37:46 +01003786status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003787 ATRACE_NAME("CameraHal::dump");
3788 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003789
Emilian Peev31abd0a2017-05-11 18:37:46 +01003790 // Handled by CameraProviderManager::dump
3791
3792 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003793}
3794
3795status_t Camera3Device::HalInterface::close() {
3796 ATRACE_NAME("CameraHal::close()");
3797 if (!valid()) return INVALID_OPERATION;
3798 status_t res = OK;
3799
Emilian Peev31abd0a2017-05-11 18:37:46 +01003800 auto err = mHidlSession->close();
3801 // Interface will be dead shortly anyway, so don't log errors
3802 if (!err.isOk()) {
3803 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003804 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003805
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003806 return res;
3807}
3808
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003809void Camera3Device::HalInterface::getInflightBufferKeys(
3810 std::vector<std::pair<int32_t, int32_t>>* out) {
3811 std::lock_guard<std::mutex> lock(mInflightLock);
3812 out->clear();
3813 out->reserve(mInflightBufferMap.size());
3814 for (auto& pair : mInflightBufferMap) {
3815 uint64_t key = pair.first;
3816 int32_t streamId = key & 0xFFFFFFFF;
3817 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3818 out->push_back(std::make_pair(frameNumber, streamId));
3819 }
3820 return;
3821}
3822
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003823status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003824 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003825 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003826 auto pair = std::make_pair(buffer, acquireFence);
3827 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003828 return OK;
3829}
3830
3831status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003832 int32_t frameNumber, int32_t streamId,
3833 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003834 std::lock_guard<std::mutex> lock(mInflightLock);
3835
3836 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3837 auto it = mInflightBufferMap.find(key);
3838 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003839 auto pair = it->second;
3840 *buffer = pair.first;
3841 int acquireFence = pair.second;
3842 if (acquireFence > 0) {
3843 ::close(acquireFence);
3844 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003845 mInflightBufferMap.erase(it);
3846 return OK;
3847}
3848
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003849std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3850 const buffer_handle_t& buf, int streamId) {
3851 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3852
3853 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3854 auto it = bIdMap.find(buf);
3855 if (it == bIdMap.end()) {
3856 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003857 ALOGV("stream %d now have %zu buffer caches, buf %p",
3858 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003859 return std::make_pair(true, mNextBufferId - 1);
3860 } else {
3861 return std::make_pair(false, it->second);
3862 }
3863}
3864
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003865void Camera3Device::HalInterface::onBufferFreed(
3866 int streamId, const native_handle_t* handle) {
3867 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3868 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3869 auto mapIt = mBufferIdMaps.find(streamId);
3870 if (mapIt == mBufferIdMaps.end()) {
3871 // streamId might be from a deleted stream here
3872 ALOGI("%s: stream %d has been removed",
3873 __FUNCTION__, streamId);
3874 return;
3875 }
3876 BufferIdMap& bIdMap = mapIt->second;
3877 auto it = bIdMap.find(handle);
3878 if (it == bIdMap.end()) {
3879 ALOGW("%s: cannot find buffer %p in stream %d",
3880 __FUNCTION__, handle, streamId);
3881 return;
3882 } else {
3883 bufferId = it->second;
3884 bIdMap.erase(it);
3885 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3886 __FUNCTION__, streamId, bIdMap.size(), handle);
3887 }
3888 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3889}
3890
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003891/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003892 * RequestThread inner class methods
3893 */
3894
3895Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003896 sp<StatusTracker> statusTracker,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003897 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003898 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003899 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003900 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003901 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003902 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003903 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003904 mReconfigured(false),
3905 mDoPause(false),
3906 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003907 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003908 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003909 mCurrentAfTriggerId(0),
3910 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003911 mRepeatingLastFrameNumber(
3912 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003913 mPrepareVideoStream(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003914 mRequestLatency(kRequestLatencyBinSize),
3915 mSessionParamKeys(sessionParamKeys),
3916 mLatestSessionParams(sessionParamKeys.size()) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003917 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003918}
3919
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003920Camera3Device::RequestThread::~RequestThread() {}
3921
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003922void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003923 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003924 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003925 Mutex::Autolock l(mRequestLock);
3926 mListener = listener;
3927}
3928
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003929void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
3930 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003931 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003932 Mutex::Autolock l(mRequestLock);
3933 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003934 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003935 // Prepare video stream for high speed recording.
3936 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003937}
3938
Jianing Wei90e59c92014-03-12 18:29:36 -07003939status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003940 List<sp<CaptureRequest> > &requests,
3941 /*out*/
3942 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003943 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003944 Mutex::Autolock l(mRequestLock);
3945 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3946 ++it) {
3947 mRequestQueue.push_back(*it);
3948 }
3949
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003950 if (lastFrameNumber != NULL) {
3951 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3952 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3953 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3954 *lastFrameNumber);
3955 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003956
Jianing Wei90e59c92014-03-12 18:29:36 -07003957 unpauseForNewRequests();
3958
3959 return OK;
3960}
3961
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003962
3963status_t Camera3Device::RequestThread::queueTrigger(
3964 RequestTrigger trigger[],
3965 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003966 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003967 Mutex::Autolock l(mTriggerMutex);
3968 status_t ret;
3969
3970 for (size_t i = 0; i < count; ++i) {
3971 ret = queueTriggerLocked(trigger[i]);
3972
3973 if (ret != OK) {
3974 return ret;
3975 }
3976 }
3977
3978 return OK;
3979}
3980
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003981const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3982 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003983 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003984 if (d != nullptr) return d->mId;
3985 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003986}
3987
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003988status_t Camera3Device::RequestThread::queueTriggerLocked(
3989 RequestTrigger trigger) {
3990
3991 uint32_t tag = trigger.metadataTag;
3992 ssize_t index = mTriggerMap.indexOfKey(tag);
3993
3994 switch (trigger.getTagType()) {
3995 case TYPE_BYTE:
3996 // fall-through
3997 case TYPE_INT32:
3998 break;
3999 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004000 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4001 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004002 return INVALID_OPERATION;
4003 }
4004
4005 /**
4006 * Collect only the latest trigger, since we only have 1 field
4007 * in the request settings per trigger tag, and can't send more than 1
4008 * trigger per request.
4009 */
4010 if (index != NAME_NOT_FOUND) {
4011 mTriggerMap.editValueAt(index) = trigger;
4012 } else {
4013 mTriggerMap.add(tag, trigger);
4014 }
4015
4016 return OK;
4017}
4018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004019status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004020 const RequestList &requests,
4021 /*out*/
4022 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004023 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004024 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004025 if (lastFrameNumber != NULL) {
4026 *lastFrameNumber = mRepeatingLastFrameNumber;
4027 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004028 mRepeatingRequests.clear();
4029 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4030 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004031
4032 unpauseForNewRequests();
4033
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004034 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004035 return OK;
4036}
4037
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004038bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004039 if (mRepeatingRequests.empty()) {
4040 return false;
4041 }
4042 int32_t requestId = requestIn->mResultExtras.requestId;
4043 const RequestList &repeatRequests = mRepeatingRequests;
4044 // All repeating requests are guaranteed to have same id so only check first quest
4045 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4046 return (firstRequest->mResultExtras.requestId == requestId);
4047}
4048
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004049status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004050 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004051 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004052 return clearRepeatingRequestsLocked(lastFrameNumber);
4053
4054}
4055
4056status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004057 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004058 if (lastFrameNumber != NULL) {
4059 *lastFrameNumber = mRepeatingLastFrameNumber;
4060 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004061 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004062 return OK;
4063}
4064
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004065status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004066 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004067 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004068 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004069 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004070
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004071 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004072
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004073 // Send errors for all requests pending in the request queue, including
4074 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004075 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004076 if (listener != NULL) {
4077 for (RequestList::iterator it = mRequestQueue.begin();
4078 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004079 // Abort the input buffers for reprocess requests.
4080 if ((*it)->mInputStream != NULL) {
4081 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004082 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4083 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004084 if (res != OK) {
4085 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4086 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4087 } else {
4088 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4089 if (res != OK) {
4090 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4091 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4092 }
4093 }
4094 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004095 // Set the frame number this request would have had, if it
4096 // had been submitted; this frame number will not be reused.
4097 // The requestId and burstId fields were set when the request was
4098 // submitted originally (in convertMetadataListToRequestListLocked)
4099 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004100 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004101 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004102 }
4103 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004104 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004105
4106 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004107 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004108 if (lastFrameNumber != NULL) {
4109 *lastFrameNumber = mRepeatingLastFrameNumber;
4110 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004111 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004112 return OK;
4113}
4114
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004115status_t Camera3Device::RequestThread::flush() {
4116 ATRACE_CALL();
4117 Mutex::Autolock l(mFlushLock);
4118
Emilian Peev08dd2452017-04-06 16:55:14 +01004119 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004120}
4121
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004122void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004123 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004124 Mutex::Autolock l(mPauseLock);
4125 mDoPause = paused;
4126 mDoPauseSignal.signal();
4127}
4128
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004129status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4130 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004131 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004132 Mutex::Autolock l(mLatestRequestMutex);
4133 status_t res;
4134 while (mLatestRequestId != requestId) {
4135 nsecs_t startTime = systemTime();
4136
4137 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4138 if (res != OK) return res;
4139
4140 timeout -= (systemTime() - startTime);
4141 }
4142
4143 return OK;
4144}
4145
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004146void Camera3Device::RequestThread::requestExit() {
4147 // Call parent to set up shutdown
4148 Thread::requestExit();
4149 // The exit from any possible waits
4150 mDoPauseSignal.signal();
4151 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004152
4153 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4154 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004155}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004156
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004157void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004158 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004159 bool surfaceAbandoned = false;
4160 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004161 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004162 {
4163 Mutex::Autolock l(mRequestLock);
4164 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4165 // repeating requests.
4166 for (const auto& request : mRepeatingRequests) {
4167 for (const auto& s : request->mOutputStreams) {
4168 if (s->isAbandoned()) {
4169 surfaceAbandoned = true;
4170 clearRepeatingRequestsLocked(&lastFrameNumber);
4171 break;
4172 }
4173 }
4174 if (surfaceAbandoned) {
4175 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004176 }
4177 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004178 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004179 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004180
4181 if (listener != NULL && surfaceAbandoned) {
4182 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004183 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004184}
4185
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004186bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004187 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004188 status_t res;
4189 size_t batchSize = mNextRequests.size();
4190 std::vector<camera3_capture_request_t*> requests(batchSize);
4191 uint32_t numRequestProcessed = 0;
4192 for (size_t i = 0; i < batchSize; i++) {
4193 requests[i] = &mNextRequests.editItemAt(i).halRequest;
4194 }
4195
4196 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
4197 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4198
4199 bool triggerRemoveFailed = false;
4200 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4201 for (size_t i = 0; i < numRequestProcessed; i++) {
4202 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4203 nextRequest.submitted = true;
4204
4205
4206 // Update the latest request sent to HAL
4207 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4208 Mutex::Autolock al(mLatestRequestMutex);
4209
4210 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4211 mLatestRequest.acquire(cloned);
4212
4213 sp<Camera3Device> parent = mParent.promote();
4214 if (parent != NULL) {
4215 parent->monitorMetadata(TagMonitor::REQUEST,
4216 nextRequest.halRequest.frame_number,
4217 0, mLatestRequest);
4218 }
4219 }
4220
4221 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004222 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4223 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004224 }
4225
Emilian Peevaebbe412018-01-15 13:53:24 +00004226 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4227
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004228 if (!triggerRemoveFailed) {
4229 // Remove any previously queued triggers (after unlock)
4230 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4231 if (removeTriggerRes != OK) {
4232 triggerRemoveFailed = true;
4233 triggerFailedRequest = nextRequest;
4234 }
4235 }
4236 }
4237
4238 if (triggerRemoveFailed) {
4239 SET_ERR("RequestThread: Unable to remove triggers "
4240 "(capture request %d, HAL device: %s (%d)",
4241 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4242 cleanUpFailedRequests(/*sendRequestError*/ false);
4243 return false;
4244 }
4245
4246 if (res != OK) {
4247 // Should only get a failure here for malformed requests or device-level
4248 // errors, so consider all errors fatal. Bad metadata failures should
4249 // come through notify.
4250 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4251 mNextRequests[numRequestProcessed].halRequest.frame_number,
4252 strerror(-res), res);
4253 cleanUpFailedRequests(/*sendRequestError*/ false);
4254 return false;
4255 }
4256 return true;
4257}
4258
4259bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4260 status_t res;
4261
4262 for (auto& nextRequest : mNextRequests) {
4263 // Submit request and block until ready for next one
4264 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4265 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4266
4267 if (res != OK) {
4268 // Should only get a failure here for malformed requests or device-level
4269 // errors, so consider all errors fatal. Bad metadata failures should
4270 // come through notify.
4271 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4272 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4273 res);
4274 cleanUpFailedRequests(/*sendRequestError*/ false);
4275 return false;
4276 }
4277
4278 // Mark that the request has be submitted successfully.
4279 nextRequest.submitted = true;
4280
4281 // Update the latest request sent to HAL
4282 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4283 Mutex::Autolock al(mLatestRequestMutex);
4284
4285 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4286 mLatestRequest.acquire(cloned);
4287
4288 sp<Camera3Device> parent = mParent.promote();
4289 if (parent != NULL) {
4290 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4291 0, mLatestRequest);
4292 }
4293 }
4294
4295 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004296 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4297 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004298 }
4299
Emilian Peevaebbe412018-01-15 13:53:24 +00004300 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4301
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004302 // Remove any previously queued triggers (after unlock)
4303 res = removeTriggers(mPrevRequest);
4304 if (res != OK) {
4305 SET_ERR("RequestThread: Unable to remove triggers "
4306 "(capture request %d, HAL device: %s (%d)",
4307 nextRequest.halRequest.frame_number, strerror(-res), res);
4308 cleanUpFailedRequests(/*sendRequestError*/ false);
4309 return false;
4310 }
4311 }
4312 return true;
4313}
4314
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004315nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4316 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4317 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4318 find_camera_metadata_ro_entry(request,
4319 ANDROID_CONTROL_AE_MODE,
4320 &e);
4321 if (e.count == 0) return maxExpectedDuration;
4322
4323 switch (e.data.u8[0]) {
4324 case ANDROID_CONTROL_AE_MODE_OFF:
4325 find_camera_metadata_ro_entry(request,
4326 ANDROID_SENSOR_EXPOSURE_TIME,
4327 &e);
4328 if (e.count > 0) {
4329 maxExpectedDuration = e.data.i64[0];
4330 }
4331 find_camera_metadata_ro_entry(request,
4332 ANDROID_SENSOR_FRAME_DURATION,
4333 &e);
4334 if (e.count > 0) {
4335 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4336 }
4337 break;
4338 default:
4339 find_camera_metadata_ro_entry(request,
4340 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4341 &e);
4342 if (e.count > 1) {
4343 maxExpectedDuration = 1e9 / e.data.u8[0];
4344 }
4345 break;
4346 }
4347
4348 return maxExpectedDuration;
4349}
4350
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004351bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4352 ATRACE_CALL();
4353 bool updatesDetected = false;
4354
4355 for (auto tag : mSessionParamKeys) {
4356 camera_metadata_ro_entry entry = settings.find(tag);
4357 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4358
4359 if (entry.count > 0) {
4360 bool isDifferent = false;
4361 if (lastEntry.count > 0) {
4362 // Have a last value, compare to see if changed
4363 if (lastEntry.type == entry.type &&
4364 lastEntry.count == entry.count) {
4365 // Same type and count, compare values
4366 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4367 size_t entryBytes = bytesPerValue * lastEntry.count;
4368 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4369 if (cmp != 0) {
4370 isDifferent = true;
4371 }
4372 } else {
4373 // Count or type has changed
4374 isDifferent = true;
4375 }
4376 } else {
4377 // No last entry, so always consider to be different
4378 isDifferent = true;
4379 }
4380
4381 if (isDifferent) {
4382 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
4383 mLatestSessionParams.update(entry);
4384 updatesDetected = true;
4385 }
4386 } else if (lastEntry.count > 0) {
4387 // Value has been removed
4388 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4389 mLatestSessionParams.erase(tag);
4390 updatesDetected = true;
4391 }
4392 }
4393
4394 return updatesDetected;
4395}
4396
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004397bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004398 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004399 status_t res;
4400
4401 // Handle paused state.
4402 if (waitIfPaused()) {
4403 return true;
4404 }
4405
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004406 // Wait for the next batch of requests.
4407 waitForNextRequestBatch();
4408 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004409 return true;
4410 }
4411
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004412 // Get the latest request ID, if any
4413 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004414 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004415 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004416 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004417 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004418 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004419 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4420 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004421 }
4422
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004423 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4424 // or a single request from streaming or burst. In either case the first element
4425 // should contain the latest camera settings that we need to check for any session
4426 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004427 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004428 res = OK;
4429
4430 //Input stream buffers are already acquired at this point so an input stream
4431 //will not be able to move to idle state unless we force it.
4432 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4433 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4434 if (res != OK) {
4435 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4436 cleanUpFailedRequests(/*sendRequestError*/ false);
4437 return false;
4438 }
4439 }
4440
4441 if (res == OK) {
4442 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4443 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08004444 sp<Camera3Device> parent = mParent.promote();
4445 if (parent != nullptr) {
4446 parent->pauseStateNotify(true);
4447 }
4448
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004449 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4450
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004451 if (parent != nullptr) {
4452 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4453 }
4454
4455 statusTracker->markComponentActive(mStatusId);
4456 setPaused(false);
4457 }
4458
4459 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4460 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4461 if (res != OK) {
4462 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4463 cleanUpFailedRequests(/*sendRequestError*/ false);
4464 return false;
4465 }
4466 }
4467 }
4468 }
4469
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004470 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004471 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004472 if (res == TIMED_OUT) {
4473 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004474 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004475 // Check if any stream is abandoned.
4476 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004477 return true;
4478 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004479 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004480 return false;
4481 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004482
Zhijun Hecc27e112013-10-03 16:12:43 -07004483 // Inform waitUntilRequestProcessed thread of a new request ID
4484 {
4485 Mutex::Autolock al(mLatestRequestMutex);
4486
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004487 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004488 mLatestRequestSignal.signal();
4489 }
4490
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004491 // Submit a batch of requests to HAL.
4492 // Use flush lock only when submitting multilple requests in a batch.
4493 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4494 // which may take a long time to finish so synchronizing flush() and
4495 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4496 // For now, only synchronize for high speed recording and we should figure something out for
4497 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004498 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004499
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004500 if (useFlushLock) {
4501 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004502 }
4503
Zhijun Hef0645c12016-08-02 00:58:11 -07004504 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004505 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004506
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004507 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004508 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004509 if (mInterface->supportBatchRequest()) {
4510 submitRequestSuccess = sendRequestsBatch();
4511 } else {
4512 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004513 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004514 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4515 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004516
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004517 if (useFlushLock) {
4518 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004519 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004520
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004521 // Unset as current request
4522 {
4523 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004524 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004525 }
4526
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004527 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004528}
4529
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004530status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004531 ATRACE_CALL();
4532
Shuzhen Wang4a472662017-02-26 23:29:04 -08004533 for (size_t i = 0; i < mNextRequests.size(); i++) {
4534 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004535 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4536 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4537 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4538
4539 // Prepare a request to HAL
4540 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4541
4542 // Insert any queued triggers (before metadata is locked)
4543 status_t res = insertTriggers(captureRequest);
4544
4545 if (res < 0) {
4546 SET_ERR("RequestThread: Unable to insert triggers "
4547 "(capture request %d, HAL device: %s (%d)",
4548 halRequest->frame_number, strerror(-res), res);
4549 return INVALID_OPERATION;
4550 }
4551 int triggerCount = res;
4552 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4553 mPrevTriggers = triggerCount;
4554
4555 // If the request is the same as last, or we had triggers last time
4556 if (mPrevRequest != captureRequest || triggersMixedIn) {
4557 /**
4558 * HAL workaround:
4559 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4560 */
4561 res = addDummyTriggerIds(captureRequest);
4562 if (res != OK) {
4563 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4564 "(capture request %d, HAL device: %s (%d)",
4565 halRequest->frame_number, strerror(-res), res);
4566 return INVALID_OPERATION;
4567 }
4568
4569 /**
4570 * The request should be presorted so accesses in HAL
4571 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4572 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004573 captureRequest->mSettingsList.begin()->metadata.sort();
4574 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004575 mPrevRequest = captureRequest;
4576 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4577
4578 IF_ALOGV() {
4579 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4580 find_camera_metadata_ro_entry(
4581 halRequest->settings,
4582 ANDROID_CONTROL_AF_TRIGGER,
4583 &e
4584 );
4585 if (e.count > 0) {
4586 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4587 __FUNCTION__,
4588 halRequest->frame_number,
4589 e.data.u8[0]);
4590 }
4591 }
4592 } else {
4593 // leave request.settings NULL to indicate 'reuse latest given'
4594 ALOGVV("%s: Request settings are REUSED",
4595 __FUNCTION__);
4596 }
4597
Emilian Peevaebbe412018-01-15 13:53:24 +00004598 if (captureRequest->mSettingsList.size() > 1) {
4599 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4600 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
4601 halRequest->physcam_settings =
4602 new const camera_metadata* [halRequest->num_physcam_settings];
4603 auto it = ++captureRequest->mSettingsList.begin();
4604 size_t i = 0;
4605 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4606 halRequest->physcam_id[i] = it->cameraId.c_str();
4607 it->metadata.sort();
4608 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4609 }
4610 }
4611
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004612 uint32_t totalNumBuffers = 0;
4613
4614 // Fill in buffers
4615 if (captureRequest->mInputStream != NULL) {
4616 halRequest->input_buffer = &captureRequest->mInputBuffer;
4617 totalNumBuffers += 1;
4618 } else {
4619 halRequest->input_buffer = NULL;
4620 }
4621
4622 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4623 captureRequest->mOutputStreams.size());
4624 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004625 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4626 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004627
4628 // Prepare video buffers for high speed recording on the first video request.
4629 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4630 // Only try to prepare video stream on the first video request.
4631 mPrepareVideoStream = false;
4632
4633 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4634 while (res == NOT_ENOUGH_DATA) {
4635 res = outputStream->prepareNextBuffer();
4636 }
4637 if (res != OK) {
4638 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4639 __FUNCTION__, strerror(-res), res);
4640 outputStream->cancelPrepare();
4641 }
4642 }
4643
Shuzhen Wang4a472662017-02-26 23:29:04 -08004644 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4645 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004646 if (res != OK) {
4647 // Can't get output buffer from gralloc queue - this could be due to
4648 // abandoned queue or other consumer misbehavior, so not a fatal
4649 // error
4650 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4651 " %s (%d)", strerror(-res), res);
4652
4653 return TIMED_OUT;
4654 }
4655 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004656
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004657 }
4658 totalNumBuffers += halRequest->num_output_buffers;
4659
4660 // Log request in the in-flight queue
4661 sp<Camera3Device> parent = mParent.promote();
4662 if (parent == NULL) {
4663 // Should not happen, and nowhere to send errors to, so just log it
4664 CLOGE("RequestThread: Parent is gone");
4665 return INVALID_OPERATION;
4666 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004667
4668 // If this request list is for constrained high speed recording (not
4669 // preview), and the current request is not the last one in the batch,
4670 // do not send callback to the app.
4671 bool hasCallback = true;
4672 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4673 hasCallback = false;
4674 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004675 res = parent->registerInFlight(halRequest->frame_number,
4676 totalNumBuffers, captureRequest->mResultExtras,
4677 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004678 hasCallback,
4679 calculateMaxExpectedDuration(halRequest->settings));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004680 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4681 ", burstId = %" PRId32 ".",
4682 __FUNCTION__,
4683 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4684 captureRequest->mResultExtras.burstId);
4685 if (res != OK) {
4686 SET_ERR("RequestThread: Unable to register new in-flight request:"
4687 " %s (%d)", strerror(-res), res);
4688 return INVALID_OPERATION;
4689 }
4690 }
4691
4692 return OK;
4693}
4694
Igor Murashkin1e479c02013-09-06 16:55:14 -07004695CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004696 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004697 Mutex::Autolock al(mLatestRequestMutex);
4698
4699 ALOGV("RequestThread::%s", __FUNCTION__);
4700
4701 return mLatestRequest;
4702}
4703
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004704bool Camera3Device::RequestThread::isStreamPending(
4705 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004706 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004707 Mutex::Autolock l(mRequestLock);
4708
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004709 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004710 if (!nextRequest.submitted) {
4711 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4712 if (stream == s) return true;
4713 }
4714 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004715 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004716 }
4717
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004718 for (const auto& request : mRequestQueue) {
4719 for (const auto& s : request->mOutputStreams) {
4720 if (stream == s) return true;
4721 }
4722 if (stream == request->mInputStream) return true;
4723 }
4724
4725 for (const auto& request : mRepeatingRequests) {
4726 for (const auto& s : request->mOutputStreams) {
4727 if (stream == s) return true;
4728 }
4729 if (stream == request->mInputStream) return true;
4730 }
4731
4732 return false;
4733}
Jianing Weicb0652e2014-03-12 18:29:36 -07004734
Emilian Peev40ead602017-09-26 15:46:36 +01004735bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4736 ATRACE_CALL();
4737 Mutex::Autolock l(mRequestLock);
4738
4739 for (const auto& nextRequest : mNextRequests) {
4740 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4741 if (s.first == streamId) {
4742 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4743 if (it != s.second.end()) {
4744 return true;
4745 }
4746 }
4747 }
4748 }
4749
4750 for (const auto& request : mRequestQueue) {
4751 for (const auto& s : request->mOutputSurfaces) {
4752 if (s.first == streamId) {
4753 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4754 if (it != s.second.end()) {
4755 return true;
4756 }
4757 }
4758 }
4759 }
4760
4761 for (const auto& request : mRepeatingRequests) {
4762 for (const auto& s : request->mOutputSurfaces) {
4763 if (s.first == streamId) {
4764 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4765 if (it != s.second.end()) {
4766 return true;
4767 }
4768 }
4769 }
4770 }
4771
4772 return false;
4773}
4774
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004775nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004776 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004777 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004778 return mExpectedInflightDuration > kMinInflightDuration ?
4779 mExpectedInflightDuration : kMinInflightDuration;
4780}
4781
Emilian Peevaebbe412018-01-15 13:53:24 +00004782void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
4783 camera3_capture_request_t *halRequest) {
4784 if ((request == nullptr) || (halRequest == nullptr)) {
4785 ALOGE("%s: Invalid request!", __FUNCTION__);
4786 return;
4787 }
4788
4789 if (halRequest->num_physcam_settings > 0) {
4790 if (halRequest->physcam_id != nullptr) {
4791 delete [] halRequest->physcam_id;
4792 halRequest->physcam_id = nullptr;
4793 }
4794 if (halRequest->physcam_settings != nullptr) {
4795 auto it = ++(request->mSettingsList.begin());
4796 size_t i = 0;
4797 for (; it != request->mSettingsList.end(); it++, i++) {
4798 it->metadata.unlock(halRequest->physcam_settings[i]);
4799 }
4800 delete [] halRequest->physcam_settings;
4801 halRequest->physcam_settings = nullptr;
4802 }
4803 }
4804}
4805
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004806void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4807 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004808 return;
4809 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004810
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004811 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004812 // Skip the ones that have been submitted successfully.
4813 if (nextRequest.submitted) {
4814 continue;
4815 }
4816
4817 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4818 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4819 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4820
4821 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004822 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004823 }
4824
Emilian Peevaebbe412018-01-15 13:53:24 +00004825 cleanupPhysicalSettings(captureRequest, halRequest);
4826
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004827 if (captureRequest->mInputStream != NULL) {
4828 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4829 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4830 }
4831
4832 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004833 //Buffers that failed processing could still have
4834 //valid acquire fence.
4835 int acquireFence = (*outputBuffers)[i].acquire_fence;
4836 if (0 <= acquireFence) {
4837 close(acquireFence);
4838 outputBuffers->editItemAt(i).acquire_fence = -1;
4839 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004840 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4841 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4842 }
4843
4844 if (sendRequestError) {
4845 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004846 sp<NotificationListener> listener = mListener.promote();
4847 if (listener != NULL) {
4848 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004849 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004850 captureRequest->mResultExtras);
4851 }
4852 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004853
4854 // Remove yet-to-be submitted inflight request from inflightMap
4855 {
4856 sp<Camera3Device> parent = mParent.promote();
4857 if (parent != NULL) {
4858 Mutex::Autolock l(parent->mInFlightLock);
4859 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4860 if (idx >= 0) {
4861 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4862 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4863 parent->removeInFlightMapEntryLocked(idx);
4864 }
4865 }
4866 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004867 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004868
4869 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004870 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004871}
4872
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004873void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004874 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004875 // Optimized a bit for the simple steady-state case (single repeating
4876 // request), to avoid putting that request in the queue temporarily.
4877 Mutex::Autolock l(mRequestLock);
4878
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004879 assert(mNextRequests.empty());
4880
4881 NextRequest nextRequest;
4882 nextRequest.captureRequest = waitForNextRequestLocked();
4883 if (nextRequest.captureRequest == nullptr) {
4884 return;
4885 }
4886
4887 nextRequest.halRequest = camera3_capture_request_t();
4888 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004889 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004890
4891 // Wait for additional requests
4892 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4893
4894 for (size_t i = 1; i < batchSize; i++) {
4895 NextRequest additionalRequest;
4896 additionalRequest.captureRequest = waitForNextRequestLocked();
4897 if (additionalRequest.captureRequest == nullptr) {
4898 break;
4899 }
4900
4901 additionalRequest.halRequest = camera3_capture_request_t();
4902 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004903 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004904 }
4905
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004906 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004907 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004908 mNextRequests.size(), batchSize);
4909 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004910 }
4911
4912 return;
4913}
4914
4915sp<Camera3Device::CaptureRequest>
4916 Camera3Device::RequestThread::waitForNextRequestLocked() {
4917 status_t res;
4918 sp<CaptureRequest> nextRequest;
4919
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004920 while (mRequestQueue.empty()) {
4921 if (!mRepeatingRequests.empty()) {
4922 // Always atomically enqueue all requests in a repeating request
4923 // list. Guarantees a complete in-sequence set of captures to
4924 // application.
4925 const RequestList &requests = mRepeatingRequests;
4926 RequestList::const_iterator firstRequest =
4927 requests.begin();
4928 nextRequest = *firstRequest;
4929 mRequestQueue.insert(mRequestQueue.end(),
4930 ++firstRequest,
4931 requests.end());
4932 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004933
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004934 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004935
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004936 break;
4937 }
4938
4939 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4940
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004941 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4942 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004943 Mutex::Autolock pl(mPauseLock);
4944 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004945 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004946 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004947 // Let the tracker know
4948 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4949 if (statusTracker != 0) {
4950 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4951 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004952 }
4953 // Stop waiting for now and let thread management happen
4954 return NULL;
4955 }
4956 }
4957
4958 if (nextRequest == NULL) {
4959 // Don't have a repeating request already in hand, so queue
4960 // must have an entry now.
4961 RequestList::iterator firstRequest =
4962 mRequestQueue.begin();
4963 nextRequest = *firstRequest;
4964 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004965 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4966 sp<NotificationListener> listener = mListener.promote();
4967 if (listener != NULL) {
4968 listener->notifyRequestQueueEmpty();
4969 }
4970 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004971 }
4972
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004973 // In case we've been unpaused by setPaused clearing mDoPause, need to
4974 // update internal pause state (capture/setRepeatingRequest unpause
4975 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004976 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004977 if (mPaused) {
4978 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4979 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4980 if (statusTracker != 0) {
4981 statusTracker->markComponentActive(mStatusId);
4982 }
4983 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004984 mPaused = false;
4985
4986 // Check if we've reconfigured since last time, and reset the preview
4987 // request if so. Can't use 'NULL request == repeat' across configure calls.
4988 if (mReconfigured) {
4989 mPrevRequest.clear();
4990 mReconfigured = false;
4991 }
4992
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004993 if (nextRequest != NULL) {
4994 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004995 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4996 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004997
4998 // Since RequestThread::clear() removes buffers from the input stream,
4999 // get the right buffer here before unlocking mRequestLock
5000 if (nextRequest->mInputStream != NULL) {
5001 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5002 if (res != OK) {
5003 // Can't get input buffer from gralloc queue - this could be due to
5004 // disconnected queue or other producer misbehavior, so not a fatal
5005 // error
5006 ALOGE("%s: Can't get input buffer, skipping request:"
5007 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005008
5009 sp<NotificationListener> listener = mListener.promote();
5010 if (listener != NULL) {
5011 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005012 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005013 nextRequest->mResultExtras);
5014 }
5015 return NULL;
5016 }
5017 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005018 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005019
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005020 return nextRequest;
5021}
5022
5023bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005024 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005025 status_t res;
5026 Mutex::Autolock l(mPauseLock);
5027 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005028 if (mPaused == false) {
5029 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005030 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5031 // Let the tracker know
5032 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5033 if (statusTracker != 0) {
5034 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5035 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005036 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005037
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005038 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005039 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005040 return true;
5041 }
5042 }
5043 // We don't set mPaused to false here, because waitForNextRequest needs
5044 // to further manage the paused state in case of starvation.
5045 return false;
5046}
5047
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005048void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005049 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005050 // With work to do, mark thread as unpaused.
5051 // If paused by request (setPaused), don't resume, to avoid
5052 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005053 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005054 Mutex::Autolock p(mPauseLock);
5055 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005056 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5057 if (mPaused) {
5058 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5059 if (statusTracker != 0) {
5060 statusTracker->markComponentActive(mStatusId);
5061 }
5062 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005063 mPaused = false;
5064 }
5065}
5066
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005067void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5068 sp<Camera3Device> parent = mParent.promote();
5069 if (parent != NULL) {
5070 va_list args;
5071 va_start(args, fmt);
5072
5073 parent->setErrorStateV(fmt, args);
5074
5075 va_end(args);
5076 }
5077}
5078
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005079status_t Camera3Device::RequestThread::insertTriggers(
5080 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005081 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005082 Mutex::Autolock al(mTriggerMutex);
5083
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005084 sp<Camera3Device> parent = mParent.promote();
5085 if (parent == NULL) {
5086 CLOGE("RequestThread: Parent is gone");
5087 return DEAD_OBJECT;
5088 }
5089
Emilian Peevaebbe412018-01-15 13:53:24 +00005090 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005091 size_t count = mTriggerMap.size();
5092
5093 for (size_t i = 0; i < count; ++i) {
5094 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005095 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005096
5097 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5098 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5099 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005100 if (isAeTrigger) {
5101 request->mResultExtras.precaptureTriggerId = triggerId;
5102 mCurrentPreCaptureTriggerId = triggerId;
5103 } else {
5104 request->mResultExtras.afTriggerId = triggerId;
5105 mCurrentAfTriggerId = triggerId;
5106 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005107 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005108 }
5109
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005110 camera_metadata_entry entry = metadata.find(tag);
5111
5112 if (entry.count > 0) {
5113 /**
5114 * Already has an entry for this trigger in the request.
5115 * Rewrite it with our requested trigger value.
5116 */
5117 RequestTrigger oldTrigger = trigger;
5118
5119 oldTrigger.entryValue = entry.data.u8[0];
5120
5121 mTriggerReplacedMap.add(tag, oldTrigger);
5122 } else {
5123 /**
5124 * More typical, no trigger entry, so we just add it
5125 */
5126 mTriggerRemovedMap.add(tag, trigger);
5127 }
5128
5129 status_t res;
5130
5131 switch (trigger.getTagType()) {
5132 case TYPE_BYTE: {
5133 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5134 res = metadata.update(tag,
5135 &entryValue,
5136 /*count*/1);
5137 break;
5138 }
5139 case TYPE_INT32:
5140 res = metadata.update(tag,
5141 &trigger.entryValue,
5142 /*count*/1);
5143 break;
5144 default:
5145 ALOGE("%s: Type not supported: 0x%x",
5146 __FUNCTION__,
5147 trigger.getTagType());
5148 return INVALID_OPERATION;
5149 }
5150
5151 if (res != OK) {
5152 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5153 ", value %d", __FUNCTION__, trigger.getTagName(),
5154 trigger.entryValue);
5155 return res;
5156 }
5157
5158 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5159 trigger.getTagName(),
5160 trigger.entryValue);
5161 }
5162
5163 mTriggerMap.clear();
5164
5165 return count;
5166}
5167
5168status_t Camera3Device::RequestThread::removeTriggers(
5169 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005170 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005171 Mutex::Autolock al(mTriggerMutex);
5172
Emilian Peevaebbe412018-01-15 13:53:24 +00005173 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005174
5175 /**
5176 * Replace all old entries with their old values.
5177 */
5178 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5179 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5180
5181 status_t res;
5182
5183 uint32_t tag = trigger.metadataTag;
5184 switch (trigger.getTagType()) {
5185 case TYPE_BYTE: {
5186 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5187 res = metadata.update(tag,
5188 &entryValue,
5189 /*count*/1);
5190 break;
5191 }
5192 case TYPE_INT32:
5193 res = metadata.update(tag,
5194 &trigger.entryValue,
5195 /*count*/1);
5196 break;
5197 default:
5198 ALOGE("%s: Type not supported: 0x%x",
5199 __FUNCTION__,
5200 trigger.getTagType());
5201 return INVALID_OPERATION;
5202 }
5203
5204 if (res != OK) {
5205 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5206 ", trigger value %d", __FUNCTION__,
5207 trigger.getTagName(), trigger.entryValue);
5208 return res;
5209 }
5210 }
5211 mTriggerReplacedMap.clear();
5212
5213 /**
5214 * Remove all new entries.
5215 */
5216 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5217 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5218 status_t res = metadata.erase(trigger.metadataTag);
5219
5220 if (res != OK) {
5221 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5222 ", trigger value %d", __FUNCTION__,
5223 trigger.getTagName(), trigger.entryValue);
5224 return res;
5225 }
5226 }
5227 mTriggerRemovedMap.clear();
5228
5229 return OK;
5230}
5231
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005232status_t Camera3Device::RequestThread::addDummyTriggerIds(
5233 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005234 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005235 static const int32_t dummyTriggerId = 1;
5236 status_t res;
5237
Emilian Peevaebbe412018-01-15 13:53:24 +00005238 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005239
5240 // If AF trigger is active, insert a dummy AF trigger ID if none already
5241 // exists
5242 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5243 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5244 if (afTrigger.count > 0 &&
5245 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5246 afId.count == 0) {
5247 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5248 if (res != OK) return res;
5249 }
5250
5251 // If AE precapture trigger is active, insert a dummy precapture trigger ID
5252 // if none already exists
5253 camera_metadata_entry pcTrigger =
5254 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5255 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5256 if (pcTrigger.count > 0 &&
5257 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5258 pcId.count == 0) {
5259 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5260 &dummyTriggerId, 1);
5261 if (res != OK) return res;
5262 }
5263
5264 return OK;
5265}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005266
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005267/**
5268 * PreparerThread inner class methods
5269 */
5270
5271Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005272 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005273 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005274}
5275
5276Camera3Device::PreparerThread::~PreparerThread() {
5277 Thread::requestExitAndWait();
5278 if (mCurrentStream != nullptr) {
5279 mCurrentStream->cancelPrepare();
5280 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5281 mCurrentStream.clear();
5282 }
5283 clear();
5284}
5285
Ruben Brunkc78ac262015-08-13 17:58:46 -07005286status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005287 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005288 status_t res;
5289
5290 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005291 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005292
Ruben Brunkc78ac262015-08-13 17:58:46 -07005293 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005294 if (res == OK) {
5295 // No preparation needed, fire listener right off
5296 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005297 if (listener != NULL) {
5298 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005299 }
5300 return OK;
5301 } else if (res != NOT_ENOUGH_DATA) {
5302 return res;
5303 }
5304
5305 // Need to prepare, start up thread if necessary
5306 if (!mActive) {
5307 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5308 // isn't running
5309 Thread::requestExitAndWait();
5310 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5311 if (res != OK) {
5312 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005313 if (listener != NULL) {
5314 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005315 }
5316 return res;
5317 }
5318 mCancelNow = false;
5319 mActive = true;
5320 ALOGV("%s: Preparer stream started", __FUNCTION__);
5321 }
5322
5323 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005324 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005325 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5326
5327 return OK;
5328}
5329
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005330void Camera3Device::PreparerThread::pause() {
5331 ATRACE_CALL();
5332
5333 Mutex::Autolock l(mLock);
5334
5335 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5336 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5337 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5338 int currentMaxCount = mCurrentMaxCount;
5339 mPendingStreams.clear();
5340 mCancelNow = true;
5341 while (mActive) {
5342 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5343 if (res == TIMED_OUT) {
5344 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5345 return;
5346 } else if (res != OK) {
5347 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5348 return;
5349 }
5350 }
5351
5352 //Check whether the prepare thread was able to complete the current
5353 //stream. In case work is still pending emplace it along with the rest
5354 //of the streams in the pending list.
5355 if (currentStream != nullptr) {
5356 if (!mCurrentPrepareComplete) {
5357 pendingStreams.emplace(currentMaxCount, currentStream);
5358 }
5359 }
5360
5361 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5362 for (const auto& it : mPendingStreams) {
5363 it.second->cancelPrepare();
5364 }
5365}
5366
5367status_t Camera3Device::PreparerThread::resume() {
5368 ATRACE_CALL();
5369 status_t res;
5370
5371 Mutex::Autolock l(mLock);
5372 sp<NotificationListener> listener = mListener.promote();
5373
5374 if (mActive) {
5375 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5376 return NO_INIT;
5377 }
5378
5379 auto it = mPendingStreams.begin();
5380 for (; it != mPendingStreams.end();) {
5381 res = it->second->startPrepare(it->first);
5382 if (res == OK) {
5383 if (listener != NULL) {
5384 listener->notifyPrepared(it->second->getId());
5385 }
5386 it = mPendingStreams.erase(it);
5387 } else if (res != NOT_ENOUGH_DATA) {
5388 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5389 res, strerror(-res));
5390 it = mPendingStreams.erase(it);
5391 } else {
5392 it++;
5393 }
5394 }
5395
5396 if (mPendingStreams.empty()) {
5397 return OK;
5398 }
5399
5400 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5401 if (res != OK) {
5402 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5403 __FUNCTION__, res, strerror(-res));
5404 return res;
5405 }
5406 mCancelNow = false;
5407 mActive = true;
5408 ALOGV("%s: Preparer stream started", __FUNCTION__);
5409
5410 return OK;
5411}
5412
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005413status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005414 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005415 Mutex::Autolock l(mLock);
5416
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005417 for (const auto& it : mPendingStreams) {
5418 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005419 }
5420 mPendingStreams.clear();
5421 mCancelNow = true;
5422
5423 return OK;
5424}
5425
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005426void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005427 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005428 Mutex::Autolock l(mLock);
5429 mListener = listener;
5430}
5431
5432bool Camera3Device::PreparerThread::threadLoop() {
5433 status_t res;
5434 {
5435 Mutex::Autolock l(mLock);
5436 if (mCurrentStream == nullptr) {
5437 // End thread if done with work
5438 if (mPendingStreams.empty()) {
5439 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5440 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5441 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5442 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005443 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005444 return false;
5445 }
5446
5447 // Get next stream to prepare
5448 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005449 mCurrentStream = it->second;
5450 mCurrentMaxCount = it->first;
5451 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005452 mPendingStreams.erase(it);
5453 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5454 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5455 } else if (mCancelNow) {
5456 mCurrentStream->cancelPrepare();
5457 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5458 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5459 mCurrentStream.clear();
5460 mCancelNow = false;
5461 return true;
5462 }
5463 }
5464
5465 res = mCurrentStream->prepareNextBuffer();
5466 if (res == NOT_ENOUGH_DATA) return true;
5467 if (res != OK) {
5468 // Something bad happened; try to recover by cancelling prepare and
5469 // signalling listener anyway
5470 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5471 mCurrentStream->getId(), res, strerror(-res));
5472 mCurrentStream->cancelPrepare();
5473 }
5474
5475 // This stream has finished, notify listener
5476 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005477 sp<NotificationListener> listener = mListener.promote();
5478 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005479 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5480 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005481 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005482 }
5483
5484 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5485 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005486 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005487
5488 return true;
5489}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005490
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005491/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005492 * Static callback forwarding methods from HAL to instance
5493 */
5494
5495void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5496 const camera3_capture_result *result) {
5497 Camera3Device *d =
5498 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07005499
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005500 d->processCaptureResult(result);
5501}
5502
5503void Camera3Device::sNotify(const camera3_callback_ops *cb,
5504 const camera3_notify_msg *msg) {
5505 Camera3Device *d =
5506 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5507 d->notify(msg);
5508}
5509
5510}; // namespace android