blob: 7e71c3bcbaa91f0b03e06e951a2f2a19399b06f4 [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 Talvalaf1e98d82013-09-06 09:32:43 -07001671// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001672status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001673 mRequestThread->setPaused(true);
1674 mPauseStateNotify = true;
1675
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001676 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1677 maxExpectedDuration);
1678 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001679 if (res != OK) {
1680 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001681 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001682 }
1683
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001684 return res;
1685}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001686
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001687// Resume after internalPauseAndWaitLocked
1688status_t Camera3Device::internalResumeLocked() {
1689 status_t res;
1690
1691 mRequestThread->setPaused(false);
1692
1693 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1694 if (res != OK) {
1695 SET_ERR_L("Can't transition to active in %f seconds!",
1696 kActiveTimeout/1e9);
1697 }
1698 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001699 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700}
1701
Ruben Brunk183f0562015-08-12 12:55:02 -07001702status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001703 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001704
1705 size_t startIndex = 0;
1706 if (mStatusWaiters == 0) {
1707 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1708 // this status list
1709 mRecentStatusUpdates.clear();
1710 } else {
1711 // If other threads are waiting on updates to this status list, set the position of the
1712 // first element that this list will check rather than clearing the list.
1713 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001714 }
1715
Ruben Brunk183f0562015-08-12 12:55:02 -07001716 mStatusWaiters++;
1717
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001718 bool stateSeen = false;
1719 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001720 if (active == (mStatus == STATUS_ACTIVE)) {
1721 // Desired state is current
1722 break;
1723 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001724
1725 res = mStatusChanged.waitRelative(mLock, timeout);
1726 if (res != OK) break;
1727
Ruben Brunk183f0562015-08-12 12:55:02 -07001728 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1729 // transitions.
1730 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1731 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1732 __FUNCTION__);
1733
1734 // Encountered desired state since we began waiting
1735 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001736 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1737 stateSeen = true;
1738 break;
1739 }
1740 }
1741 } while (!stateSeen);
1742
Ruben Brunk183f0562015-08-12 12:55:02 -07001743 mStatusWaiters--;
1744
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001745 return res;
1746}
1747
1748
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001749status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001750 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001751 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001752
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001753 if (listener != NULL && mListener != NULL) {
1754 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1755 }
1756 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001757 mRequestThread->setNotificationListener(listener);
1758 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001759
1760 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001761}
1762
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001763bool Camera3Device::willNotify3A() {
1764 return false;
1765}
1766
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001767status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001768 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001769 status_t res;
1770 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001771
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001772 while (mResultQueue.empty()) {
1773 res = mResultSignal.waitRelative(mOutputLock, timeout);
1774 if (res == TIMED_OUT) {
1775 return res;
1776 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001777 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1778 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001779 return res;
1780 }
1781 }
1782 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001783}
1784
Jianing Weicb0652e2014-03-12 18:29:36 -07001785status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001786 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001787 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001788
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001789 if (mResultQueue.empty()) {
1790 return NOT_ENOUGH_DATA;
1791 }
1792
Jianing Weicb0652e2014-03-12 18:29:36 -07001793 if (frame == NULL) {
1794 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1795 return BAD_VALUE;
1796 }
1797
1798 CaptureResult &result = *(mResultQueue.begin());
1799 frame->mResultExtras = result.mResultExtras;
1800 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001801 mResultQueue.erase(mResultQueue.begin());
1802
1803 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001804}
1805
1806status_t Camera3Device::triggerAutofocus(uint32_t id) {
1807 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001808 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001809
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001810 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1811 // Mix-in this trigger into the next request and only the next request.
1812 RequestTrigger trigger[] = {
1813 {
1814 ANDROID_CONTROL_AF_TRIGGER,
1815 ANDROID_CONTROL_AF_TRIGGER_START
1816 },
1817 {
1818 ANDROID_CONTROL_AF_TRIGGER_ID,
1819 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001820 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001821 };
1822
1823 return mRequestThread->queueTrigger(trigger,
1824 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001825}
1826
1827status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1828 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001829 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001830
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001831 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1832 // Mix-in this trigger into the next request and only the next request.
1833 RequestTrigger trigger[] = {
1834 {
1835 ANDROID_CONTROL_AF_TRIGGER,
1836 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1837 },
1838 {
1839 ANDROID_CONTROL_AF_TRIGGER_ID,
1840 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001841 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001842 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001843
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001844 return mRequestThread->queueTrigger(trigger,
1845 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001846}
1847
1848status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1849 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001850 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001851
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001852 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1853 // Mix-in this trigger into the next request and only the next request.
1854 RequestTrigger trigger[] = {
1855 {
1856 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1857 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1858 },
1859 {
1860 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1861 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001862 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001863 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001864
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001865 return mRequestThread->queueTrigger(trigger,
1866 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001867}
1868
Jianing Weicb0652e2014-03-12 18:29:36 -07001869status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001870 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001871 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001872 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001873
Zhijun He7ef20392014-04-21 16:04:17 -07001874 {
1875 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001876 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001877 }
1878
Emilian Peev08dd2452017-04-06 16:55:14 +01001879 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001880}
1881
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001882status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001883 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1884}
1885
1886status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001887 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001888 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001889 Mutex::Autolock il(mInterfaceLock);
1890 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001891
1892 sp<Camera3StreamInterface> stream;
1893 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1894 if (outputStreamIdx == NAME_NOT_FOUND) {
1895 CLOGE("Stream %d does not exist", streamId);
1896 return BAD_VALUE;
1897 }
1898
1899 stream = mOutputStreams.editValueAt(outputStreamIdx);
1900
1901 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001902 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001903 return BAD_VALUE;
1904 }
1905
1906 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001907 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001908 return BAD_VALUE;
1909 }
1910
Ruben Brunkc78ac262015-08-13 17:58:46 -07001911 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001912}
1913
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001914status_t Camera3Device::tearDown(int streamId) {
1915 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001916 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001917 Mutex::Autolock il(mInterfaceLock);
1918 Mutex::Autolock l(mLock);
1919
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001920 sp<Camera3StreamInterface> stream;
1921 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1922 if (outputStreamIdx == NAME_NOT_FOUND) {
1923 CLOGE("Stream %d does not exist", streamId);
1924 return BAD_VALUE;
1925 }
1926
1927 stream = mOutputStreams.editValueAt(outputStreamIdx);
1928
1929 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1930 CLOGE("Stream %d is a target of a in-progress request", streamId);
1931 return BAD_VALUE;
1932 }
1933
1934 return stream->tearDown();
1935}
1936
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001937status_t Camera3Device::addBufferListenerForStream(int streamId,
1938 wp<Camera3StreamBufferListener> listener) {
1939 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001940 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001941 Mutex::Autolock il(mInterfaceLock);
1942 Mutex::Autolock l(mLock);
1943
1944 sp<Camera3StreamInterface> stream;
1945 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1946 if (outputStreamIdx == NAME_NOT_FOUND) {
1947 CLOGE("Stream %d does not exist", streamId);
1948 return BAD_VALUE;
1949 }
1950
1951 stream = mOutputStreams.editValueAt(outputStreamIdx);
1952 stream->addBufferListener(listener);
1953
1954 return OK;
1955}
1956
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001958 * Methods called by subclasses
1959 */
1960
1961void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001962 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963 {
1964 // Need mLock to safely update state and synchronize to current
1965 // state of methods in flight.
1966 Mutex::Autolock l(mLock);
1967 // We can get various system-idle notices from the status tracker
1968 // while starting up. Only care about them if we've actually sent
1969 // in some requests recently.
1970 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1971 return;
1972 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001973 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001974 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001975 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001976
1977 // Skip notifying listener if we're doing some user-transparent
1978 // state changes
1979 if (mPauseStateNotify) return;
1980 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001981
1982 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983 {
1984 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001985 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001986 }
1987 if (idle && listener != NULL) {
1988 listener->notifyIdle();
1989 }
1990}
1991
Shuzhen Wang758c2152017-01-10 18:26:18 -08001992status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01001993 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07001994 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001995 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1996 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01001997
1998 if (surfaceIds == nullptr) {
1999 return BAD_VALUE;
2000 }
2001
Zhijun He5d677d12016-05-29 16:52:39 -07002002 Mutex::Autolock il(mInterfaceLock);
2003 Mutex::Autolock l(mLock);
2004
Shuzhen Wang758c2152017-01-10 18:26:18 -08002005 if (consumers.size() == 0) {
2006 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002007 return BAD_VALUE;
2008 }
2009
2010 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2011 if (idx == NAME_NOT_FOUND) {
2012 CLOGE("Stream %d is unknown", streamId);
2013 return idx;
2014 }
2015 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002016 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002017 if (res != OK) {
2018 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2019 return res;
2020 }
2021
Emilian Peev40ead602017-09-26 15:46:36 +01002022 for (auto &consumer : consumers) {
2023 int id = stream->getSurfaceId(consumer);
2024 if (id < 0) {
2025 CLOGE("Invalid surface id!");
2026 return BAD_VALUE;
2027 }
2028 surfaceIds->push_back(id);
2029 }
2030
Shuzhen Wang0129d522016-10-30 22:43:41 -07002031 if (stream->isConsumerConfigurationDeferred()) {
2032 if (!stream->isConfiguring()) {
2033 CLOGE("Stream %d was already fully configured.", streamId);
2034 return INVALID_OPERATION;
2035 }
Zhijun He5d677d12016-05-29 16:52:39 -07002036
Shuzhen Wang0129d522016-10-30 22:43:41 -07002037 res = stream->finishConfiguration();
2038 if (res != OK) {
2039 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2040 stream->getId(), strerror(-res), res);
2041 return res;
2042 }
Zhijun He5d677d12016-05-29 16:52:39 -07002043 }
2044
2045 return OK;
2046}
2047
Emilian Peev40ead602017-09-26 15:46:36 +01002048status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2049 const std::vector<OutputStreamInfo> &outputInfo,
2050 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2051 Mutex::Autolock il(mInterfaceLock);
2052 Mutex::Autolock l(mLock);
2053
2054 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2055 if (idx == NAME_NOT_FOUND) {
2056 CLOGE("Stream %d is unknown", streamId);
2057 return idx;
2058 }
2059
2060 for (const auto &it : removedSurfaceIds) {
2061 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2062 CLOGE("Shared surface still part of a pending request!");
2063 return -EBUSY;
2064 }
2065 }
2066
2067 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
2068 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2069 if (res != OK) {
2070 CLOGE("Stream %d failed to update stream (error %d %s) ",
2071 streamId, res, strerror(-res));
2072 if (res == UNKNOWN_ERROR) {
2073 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2074 __FUNCTION__);
2075 }
2076 return res;
2077 }
2078
2079 return res;
2080}
2081
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002082status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2083 Mutex::Autolock il(mInterfaceLock);
2084 Mutex::Autolock l(mLock);
2085
2086 int idx = mOutputStreams.indexOfKey(streamId);
2087 if (idx == NAME_NOT_FOUND) {
2088 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2089 return BAD_VALUE;
2090 }
2091
2092 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx);
2093 return stream->dropBuffers(dropping);
2094}
2095
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002096/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097 * Camera3Device private methods
2098 */
2099
2100sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002101 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002102 ATRACE_CALL();
2103 status_t res;
2104
2105 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002106 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002107
2108 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002109 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110 if (inputStreams.count > 0) {
2111 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002112 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002113 CLOGE("Request references unknown input stream %d",
2114 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002115 return NULL;
2116 }
2117 // Lazy completion of stream configuration (allocation/registration)
2118 // on first use
2119 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002120 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002121 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002122 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002124 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002125 return NULL;
2126 }
2127 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002128 // Check if stream is being prepared
2129 if (mInputStream->isPreparing()) {
2130 CLOGE("Request references an input stream that's being prepared!");
2131 return NULL;
2132 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002133
2134 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002135 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002136 }
2137
2138 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002139 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002141 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002142 return NULL;
2143 }
2144
2145 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002146 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002147 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002148 CLOGE("Request references unknown stream %d",
2149 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002150 return NULL;
2151 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002152 sp<Camera3OutputStreamInterface> stream =
2153 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002154
Zhijun He5d677d12016-05-29 16:52:39 -07002155 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002156 auto iter = surfaceMap.find(streams.data.i32[i]);
2157 if (iter != surfaceMap.end()) {
2158 const std::vector<size_t>& surfaces = iter->second;
2159 for (const auto& surface : surfaces) {
2160 if (stream->isConsumerConfigurationDeferred(surface)) {
2161 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2162 "due to deferred consumer", stream->getId(), surface);
2163 return NULL;
2164 }
2165 }
2166 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002167 }
2168
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002169 // Lazy completion of stream configuration (allocation/registration)
2170 // on first use
2171 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002172 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002173 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002174 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2175 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002176 return NULL;
2177 }
2178 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002179 // Check if stream is being prepared
2180 if (stream->isPreparing()) {
2181 CLOGE("Request references an output stream that's being prepared!");
2182 return NULL;
2183 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002184
2185 newRequest->mOutputStreams.push(stream);
2186 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002187 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002188 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002189
2190 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002191}
2192
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002193bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2194 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2195 Size size = mSupportedOpaqueInputSizes[i];
2196 if (size.width == width && size.height == height) {
2197 return true;
2198 }
2199 }
2200
2201 return false;
2202}
2203
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002204void Camera3Device::cancelStreamsConfigurationLocked() {
2205 int res = OK;
2206 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2207 res = mInputStream->cancelConfiguration();
2208 if (res != OK) {
2209 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2210 mInputStream->getId(), strerror(-res), res);
2211 }
2212 }
2213
2214 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2215 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2216 if (outputStream->isConfiguring()) {
2217 res = outputStream->cancelConfiguration();
2218 if (res != OK) {
2219 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2220 outputStream->getId(), strerror(-res), res);
2221 }
2222 }
2223 }
2224
2225 // Return state to that at start of call, so that future configures
2226 // properly clean things up
2227 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2228 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002229
2230 res = mPreparerThread->resume();
2231 if (res != OK) {
2232 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2233 }
2234}
2235
2236bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2237 ATRACE_CALL();
2238 bool ret = false;
2239
2240 Mutex::Autolock il(mInterfaceLock);
2241 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2242
2243 Mutex::Autolock l(mLock);
2244 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2245 if (rc == NO_ERROR) {
2246 mNeedConfig = true;
2247 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2248 if (rc == NO_ERROR) {
2249 ret = true;
2250 mPauseStateNotify = false;
2251 //Moving to active state while holding 'mLock' is important.
2252 //There could be pending calls to 'create-/deleteStream' which
2253 //will trigger another stream configuration while the already
2254 //present streams end up with outstanding buffers that will
2255 //not get drained.
2256 internalUpdateStatusLocked(STATUS_ACTIVE);
2257 } else {
2258 setErrorStateLocked("%s: Failed to re-configure camera: %d",
2259 __FUNCTION__, rc);
2260 }
2261 } else {
2262 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2263 }
2264
2265 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002266}
2267
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002268status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002269 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002270 ATRACE_CALL();
2271 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002273 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002274 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002275 return INVALID_OPERATION;
2276 }
2277
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002278 if (operatingMode < 0) {
2279 CLOGE("Invalid operating mode: %d", operatingMode);
2280 return BAD_VALUE;
2281 }
2282
2283 bool isConstrainedHighSpeed =
2284 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2285 operatingMode;
2286
2287 if (mOperatingMode != operatingMode) {
2288 mNeedConfig = true;
2289 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2290 mOperatingMode = operatingMode;
2291 }
2292
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002293 if (!mNeedConfig) {
2294 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2295 return OK;
2296 }
2297
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002298 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2299 // adding a dummy stream instead.
2300 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2301 if (mOutputStreams.size() == 0) {
2302 addDummyStreamLocked();
2303 } else {
2304 tryRemoveDummyStreamLocked();
2305 }
2306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002307 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002308 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002309
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002310 mPreparerThread->pause();
2311
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002312 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002313 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002314 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2315
2316 Vector<camera3_stream_t*> streams;
2317 streams.setCapacity(config.num_streams);
2318
2319 if (mInputStream != NULL) {
2320 camera3_stream_t *inputStream;
2321 inputStream = mInputStream->startConfiguration();
2322 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002323 CLOGE("Can't start input stream configuration");
2324 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002325 return INVALID_OPERATION;
2326 }
2327 streams.add(inputStream);
2328 }
2329
2330 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002331
2332 // Don't configure bidi streams twice, nor add them twice to the list
2333 if (mOutputStreams[i].get() ==
2334 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2335
2336 config.num_streams--;
2337 continue;
2338 }
2339
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340 camera3_stream_t *outputStream;
2341 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2342 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002343 CLOGE("Can't start output stream configuration");
2344 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345 return INVALID_OPERATION;
2346 }
2347 streams.add(outputStream);
2348 }
2349
2350 config.streams = streams.editArray();
2351
2352 // Do the HAL configuration; will potentially touch stream
2353 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002354
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002355 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
2356 res = mInterface->configureStreams(sessionBuffer, &config);
2357 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002358
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002359 if (res == BAD_VALUE) {
2360 // HAL rejected this set of streams as unsupported, clean up config
2361 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002362 CLOGE("Set of requested inputs/outputs not supported by HAL");
2363 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002364 return BAD_VALUE;
2365 } else if (res != OK) {
2366 // Some other kind of error from configure_streams - this is not
2367 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002368 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2369 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002370 return res;
2371 }
2372
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002373 // Finish all stream configuration immediately.
2374 // TODO: Try to relax this later back to lazy completion, which should be
2375 // faster
2376
Igor Murashkin073f8572013-05-02 14:59:28 -07002377 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002378 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002379 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002380 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002381 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002382 cancelStreamsConfigurationLocked();
2383 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002384 }
2385 }
2386
2387 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002388 sp<Camera3OutputStreamInterface> outputStream =
2389 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002390 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002391 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002392 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002393 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002394 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002395 cancelStreamsConfigurationLocked();
2396 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002397 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002398 }
2399 }
2400
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002401 // Request thread needs to know to avoid using repeat-last-settings protocol
2402 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002403 if (notifyRequestThread) {
2404 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2405 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002406
Zhijun He90f7c372016-08-16 16:19:43 -07002407 char value[PROPERTY_VALUE_MAX];
2408 property_get("camera.fifo.disable", value, "0");
2409 int32_t disableFifo = atoi(value);
2410 if (disableFifo != 1) {
2411 // Boost priority of request thread to SCHED_FIFO.
2412 pid_t requestThreadTid = mRequestThread->getTid();
2413 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002414 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002415 if (res != OK) {
2416 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2417 strerror(-res), res);
2418 } else {
2419 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2420 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002421 }
2422
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002423 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002424 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2425 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2426 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2427 sessionParams.unlock(newSessionParams);
2428 mSessionParams.unlock(currentSessionParams);
2429 if (updateSessionParams) {
2430 mSessionParams = sessionParams;
2431 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002432
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002433 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002434
Ruben Brunk183f0562015-08-12 12:55:02 -07002435 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2436 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002437
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002438 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002439
Zhijun He0a210512014-07-24 13:45:15 -07002440 // tear down the deleted streams after configure streams.
2441 mDeletedStreams.clear();
2442
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002443 auto rc = mPreparerThread->resume();
2444 if (rc != OK) {
2445 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2446 return rc;
2447 }
2448
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002449 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002450}
2451
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002452status_t Camera3Device::addDummyStreamLocked() {
2453 ATRACE_CALL();
2454 status_t res;
2455
2456 if (mDummyStreamId != NO_STREAM) {
2457 // Should never be adding a second dummy stream when one is already
2458 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002459 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2460 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002461 return INVALID_OPERATION;
2462 }
2463
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002464 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002465
2466 sp<Camera3OutputStreamInterface> dummyStream =
2467 new Camera3DummyStream(mNextStreamId);
2468
2469 res = mOutputStreams.add(mNextStreamId, dummyStream);
2470 if (res < 0) {
2471 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2472 return res;
2473 }
2474
2475 mDummyStreamId = mNextStreamId;
2476 mNextStreamId++;
2477
2478 return OK;
2479}
2480
2481status_t Camera3Device::tryRemoveDummyStreamLocked() {
2482 ATRACE_CALL();
2483 status_t res;
2484
2485 if (mDummyStreamId == NO_STREAM) return OK;
2486 if (mOutputStreams.size() == 1) return OK;
2487
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002488 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002489
2490 // Ok, have a dummy stream and there's at least one other output stream,
2491 // so remove the dummy
2492
2493 sp<Camera3StreamInterface> deletedStream;
2494 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2495 if (outputStreamIdx == NAME_NOT_FOUND) {
2496 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2497 return INVALID_OPERATION;
2498 }
2499
2500 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2501 mOutputStreams.removeItemsAt(outputStreamIdx);
2502
2503 // Free up the stream endpoint so that it can be used by some other stream
2504 res = deletedStream->disconnect();
2505 if (res != OK) {
2506 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2507 // fall through since we want to still list the stream as deleted.
2508 }
2509 mDeletedStreams.add(deletedStream);
2510 mDummyStreamId = NO_STREAM;
2511
2512 return res;
2513}
2514
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002515void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002516 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002517 Mutex::Autolock l(mLock);
2518 va_list args;
2519 va_start(args, fmt);
2520
2521 setErrorStateLockedV(fmt, args);
2522
2523 va_end(args);
2524}
2525
2526void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002527 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002528 Mutex::Autolock l(mLock);
2529 setErrorStateLockedV(fmt, args);
2530}
2531
2532void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2533 va_list args;
2534 va_start(args, fmt);
2535
2536 setErrorStateLockedV(fmt, args);
2537
2538 va_end(args);
2539}
2540
2541void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002542 // Print out all error messages to log
2543 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002544 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002545
2546 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002547 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002548
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002549 mErrorCause = errorCause;
2550
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002551 if (mRequestThread != nullptr) {
2552 mRequestThread->setPaused(true);
2553 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002554 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002555
2556 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002557 sp<NotificationListener> listener = mListener.promote();
2558 if (listener != NULL) {
2559 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002560 CaptureResultExtras());
2561 }
2562
2563 // Save stack trace. View by dumping it later.
2564 CameraTraces::saveTrace();
2565 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002566}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002567
2568/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002569 * In-flight request management
2570 */
2571
Jianing Weicb0652e2014-03-12 18:29:36 -07002572status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002573 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002574 bool hasAppCallback, nsecs_t maxExpectedDuration) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002575 ATRACE_CALL();
2576 Mutex::Autolock l(mInFlightLock);
2577
2578 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002579 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002580 hasAppCallback, maxExpectedDuration));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002581 if (res < 0) return res;
2582
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002583 if (mInFlightMap.size() == 1) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002584 // hold mLock to prevent race with disconnect
2585 Mutex::Autolock l(mLock);
2586 if (mStatusTracker != nullptr) {
2587 mStatusTracker->markComponentActive(mInFlightStatusId);
2588 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002589 }
2590
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002591 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002592 return OK;
2593}
2594
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002595void Camera3Device::returnOutputBuffers(
2596 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2597 nsecs_t timestamp) {
2598 for (size_t i = 0; i < numBuffers; i++)
2599 {
2600 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2601 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2602 // Note: stream may be deallocated at this point, if this buffer was
2603 // the last reference to it.
2604 if (res != OK) {
2605 ALOGE("Can't return buffer to its stream: %s (%d)",
2606 strerror(-res), res);
2607 }
2608 }
2609}
2610
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002611void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002612 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002613 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002614 mInFlightMap.removeItemsAt(idx, 1);
2615
2616 // Indicate idle inFlightMap to the status tracker
2617 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002618 // hold mLock to prevent race with disconnect
2619 Mutex::Autolock l(mLock);
2620 if (mStatusTracker != nullptr) {
2621 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2622 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002623 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002624 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002625}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002626
2627void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2628
2629 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2630 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2631
2632 nsecs_t sensorTimestamp = request.sensorTimestamp;
2633 nsecs_t shutterTimestamp = request.shutterTimestamp;
2634
2635 // Check if it's okay to remove the request from InFlightMap:
2636 // In the case of a successful request:
2637 // all input and output buffers, all result metadata, shutter callback
2638 // arrived.
2639 // In the case of a unsuccessful request:
2640 // all input and output buffers arrived.
2641 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07002642 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002643 (request.haveResultMetadata && shutterTimestamp != 0))) {
2644 ATRACE_ASYNC_END("frame capture", frameNumber);
2645
Shuzhen Wang403044a2017-02-26 23:29:04 -08002646 // Sanity check - if sensor timestamp matches shutter timestamp in the
2647 // case of request having callback.
2648 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002649 sensorTimestamp != shutterTimestamp) {
2650 SET_ERR("sensor timestamp (%" PRId64
2651 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2652 sensorTimestamp, frameNumber, shutterTimestamp);
2653 }
2654
2655 // for an unsuccessful request, it may have pending output buffers to
2656 // return.
2657 assert(request.requestStatus != OK ||
2658 request.pendingOutputBuffers.size() == 0);
2659 returnOutputBuffers(request.pendingOutputBuffers.array(),
2660 request.pendingOutputBuffers.size(), 0);
2661
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002662 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002663 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2664 }
2665
2666 // Sanity check - if we have too many in-flight frames, something has
2667 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002668 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002669 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002670 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2671 kInFlightWarnLimitHighSpeed) {
2672 CLOGE("In-flight list too large for high speed configuration: %zu",
2673 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002674 }
2675}
2676
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002677void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002678 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002679 { // First return buffers cached in mInFlightMap
2680 Mutex::Autolock l(mInFlightLock);
2681 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2682 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2683 returnOutputBuffers(request.pendingOutputBuffers.array(),
2684 request.pendingOutputBuffers.size(), 0);
2685 }
2686 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002687 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002688 }
2689
2690 // Then return all inflight buffers not returned by HAL
2691 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2692 mInterface->getInflightBufferKeys(&inflightKeys);
2693
2694 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2695 for (auto& pair : inflightKeys) {
2696 int32_t frameNumber = pair.first;
2697 int32_t streamId = pair.second;
2698 buffer_handle_t* buffer;
2699 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2700 if (res != OK) {
2701 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2702 __FUNCTION__, frameNumber, streamId);
2703 continue;
2704 }
2705
2706 camera3_stream_buffer_t streamBuffer;
2707 streamBuffer.buffer = buffer;
2708 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2709 streamBuffer.acquire_fence = -1;
2710 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002711
2712 // First check if the buffer belongs to deleted stream
2713 bool streamDeleted = false;
2714 for (auto& stream : mDeletedStreams) {
2715 if (streamId == stream->getId()) {
2716 streamDeleted = true;
2717 // Return buffer to deleted stream
2718 camera3_stream* halStream = stream->asHalStream();
2719 streamBuffer.stream = halStream;
2720 switch (halStream->stream_type) {
2721 case CAMERA3_STREAM_OUTPUT:
2722 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2723 if (res != OK) {
2724 ALOGE("%s: Can't return output buffer for frame %d to"
2725 " stream %d: %s (%d)", __FUNCTION__,
2726 frameNumber, streamId, strerror(-res), res);
2727 }
2728 break;
2729 case CAMERA3_STREAM_INPUT:
2730 res = stream->returnInputBuffer(streamBuffer);
2731 if (res != OK) {
2732 ALOGE("%s: Can't return input buffer for frame %d to"
2733 " stream %d: %s (%d)", __FUNCTION__,
2734 frameNumber, streamId, strerror(-res), res);
2735 }
2736 break;
2737 default: // Bi-direcitonal stream is deprecated
2738 ALOGE("%s: stream %d has unknown stream type %d",
2739 __FUNCTION__, streamId, halStream->stream_type);
2740 break;
2741 }
2742 break;
2743 }
2744 }
2745 if (streamDeleted) {
2746 continue;
2747 }
2748
2749 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002750 if (streamId == inputStreamId) {
2751 streamBuffer.stream = mInputStream->asHalStream();
2752 res = mInputStream->returnInputBuffer(streamBuffer);
2753 if (res != OK) {
2754 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002755 " stream %d: %s (%d)", __FUNCTION__,
2756 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002757 }
2758 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002759 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2760 if (idx == NAME_NOT_FOUND) {
2761 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2762 continue;
2763 }
2764 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002765 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2766 }
2767 }
2768}
2769
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002770void Camera3Device::insertResultLocked(CaptureResult *result,
2771 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002772 if (result == nullptr) return;
2773
Emilian Peev71c73a22017-03-21 16:35:51 +00002774 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2775 result->mMetadata.getAndLock());
2776 set_camera_metadata_vendor_id(meta, mVendorTagId);
2777 result->mMetadata.unlock(meta);
2778
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002779 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2780 (int32_t*)&frameNumber, 1) != OK) {
2781 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2782 return;
2783 }
2784
2785 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2786 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2787 return;
2788 }
2789
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002790 // Valid result, insert into queue
2791 List<CaptureResult>::iterator queuedResult =
2792 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2793 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2794 ", burstId = %" PRId32, __FUNCTION__,
2795 queuedResult->mResultExtras.requestId,
2796 queuedResult->mResultExtras.frameNumber,
2797 queuedResult->mResultExtras.burstId);
2798
2799 mResultSignal.signal();
2800}
2801
2802
2803void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002804 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002805 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002806 Mutex::Autolock l(mOutputLock);
2807
2808 CaptureResult captureResult;
2809 captureResult.mResultExtras = resultExtras;
2810 captureResult.mMetadata = partialResult;
2811
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002812 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002813}
2814
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002815
2816void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2817 CaptureResultExtras &resultExtras,
2818 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002819 uint32_t frameNumber,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002820 bool reprocess) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002821 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002822 if (pendingMetadata.isEmpty())
2823 return;
2824
2825 Mutex::Autolock l(mOutputLock);
2826
2827 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002828 if (reprocess) {
2829 if (frameNumber < mNextReprocessResultFrameNumber) {
2830 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002831 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002832 frameNumber, mNextReprocessResultFrameNumber);
2833 return;
2834 }
2835 mNextReprocessResultFrameNumber = frameNumber + 1;
2836 } else {
2837 if (frameNumber < mNextResultFrameNumber) {
2838 SET_ERR("Out-of-order capture result metadata submitted! "
2839 "(got frame number %d, expecting %d)",
2840 frameNumber, mNextResultFrameNumber);
2841 return;
2842 }
2843 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002844 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002845
2846 CaptureResult captureResult;
2847 captureResult.mResultExtras = resultExtras;
2848 captureResult.mMetadata = pendingMetadata;
2849
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002850 // Append any previous partials to form a complete result
2851 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2852 captureResult.mMetadata.append(collectedPartialResult);
2853 }
2854
2855 captureResult.mMetadata.sort();
2856
2857 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002858 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2859 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002860 SET_ERR("No timestamp provided by HAL for frame %d!",
2861 frameNumber);
2862 return;
2863 }
2864
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002865 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2866 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2867
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002868 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002869}
2870
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002871/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002872 * Camera HAL device callback methods
2873 */
2874
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002875void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002876 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002877
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002878 status_t res;
2879
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002880 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002881 if (result->result == NULL && result->num_output_buffers == 0 &&
2882 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002883 SET_ERR("No result data provided by HAL for frame %d",
2884 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002885 return;
2886 }
Zhijun He204e3292014-07-14 17:09:23 -07002887
Zhijun He204e3292014-07-14 17:09:23 -07002888 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002889 result->result != NULL &&
2890 result->partial_result != 1) {
2891 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2892 " if partial result is not supported",
2893 frameNumber, result->partial_result);
2894 return;
2895 }
2896
2897 bool isPartialResult = false;
2898 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002899 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002900 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002901
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002902 // Get shutter timestamp and resultExtras from list of in-flight requests,
2903 // where it was added by the shutter notification for this frame. If the
2904 // shutter timestamp isn't received yet, append the output buffers to the
2905 // in-flight request and they will be returned when the shutter timestamp
2906 // arrives. Update the in-flight status and remove the in-flight entry if
2907 // all result data and shutter timestamp have been received.
2908 nsecs_t shutterTimestamp = 0;
2909
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002910 {
2911 Mutex::Autolock l(mInFlightLock);
2912 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2913 if (idx == NAME_NOT_FOUND) {
2914 SET_ERR("Unknown frame number for capture result: %d",
2915 frameNumber);
2916 return;
2917 }
2918 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002919 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2920 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002921 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002922 __FUNCTION__, request.resultExtras.requestId,
2923 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002924 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002925 // Always update the partial count to the latest one if it's not 0
2926 // (buffers only). When framework aggregates adjacent partial results
2927 // into one, the latest partial count will be used.
2928 if (result->partial_result != 0)
2929 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002930
2931 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002932 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002933 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2934 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2935 " the range of [1, %d] when metadata is included in the result",
2936 frameNumber, result->partial_result, mNumPartialResults);
2937 return;
2938 }
2939 isPartialResult = (result->partial_result < mNumPartialResults);
2940 if (isPartialResult) {
2941 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002942 }
2943
Shuzhen Wang4a472662017-02-26 23:29:04 -08002944 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002945 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002946 sendPartialCaptureResult(result->result, request.resultExtras,
2947 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002948 }
2949 }
2950
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002951 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002952 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002953
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002954 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002955 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002956 if (request.haveResultMetadata) {
2957 SET_ERR("Called multiple times with metadata for frame %d",
2958 frameNumber);
2959 return;
2960 }
Zhijun He204e3292014-07-14 17:09:23 -07002961 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002962 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002963 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002964 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002965 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002966 request.haveResultMetadata = true;
2967 }
2968
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002969 uint32_t numBuffersReturned = result->num_output_buffers;
2970 if (result->input_buffer != NULL) {
2971 if (hasInputBufferInRequest) {
2972 numBuffersReturned += 1;
2973 } else {
2974 ALOGW("%s: Input buffer should be NULL if there is no input"
2975 " buffer sent in the request",
2976 __FUNCTION__);
2977 }
2978 }
2979 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002980 if (request.numBuffersLeft < 0) {
2981 SET_ERR("Too many buffers returned for frame %d",
2982 frameNumber);
2983 return;
2984 }
2985
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002986 camera_metadata_ro_entry_t entry;
2987 res = find_camera_metadata_ro_entry(result->result,
2988 ANDROID_SENSOR_TIMESTAMP, &entry);
2989 if (res == OK && entry.count == 1) {
2990 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002991 }
2992
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002993 // If shutter event isn't received yet, append the output buffers to
2994 // the in-flight request. Otherwise, return the output buffers to
2995 // streams.
2996 if (shutterTimestamp == 0) {
2997 request.pendingOutputBuffers.appendArray(result->output_buffers,
2998 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002999 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003000 returnOutputBuffers(result->output_buffers,
3001 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07003002 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003003
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003004 if (result->result != NULL && !isPartialResult) {
3005 if (shutterTimestamp == 0) {
3006 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003007 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003008 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003009 CameraMetadata metadata;
3010 metadata = result->result;
3011 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003012 collectedPartialResult, frameNumber,
3013 hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003014 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003015 }
3016
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003017 removeInFlightRequestIfReadyLocked(idx);
3018 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003019
Zhijun Hef0d962a2014-06-30 10:24:11 -07003020 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003021 if (hasInputBufferInRequest) {
3022 Camera3Stream *stream =
3023 Camera3Stream::cast(result->input_buffer->stream);
3024 res = stream->returnInputBuffer(*(result->input_buffer));
3025 // Note: stream may be deallocated at this point, if this buffer was the
3026 // last reference to it.
3027 if (res != OK) {
3028 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3029 " its stream:%s (%d)", __FUNCTION__,
3030 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003031 }
3032 } else {
3033 ALOGW("%s: Input buffer should be NULL if there is no input"
3034 " buffer sent in the request, skipping input buffer return.",
3035 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003036 }
3037 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003038}
3039
3040void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003041 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003042 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003043 {
3044 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003045 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003046 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003047
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003048 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003049 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003050 return;
3051 }
3052
3053 switch (msg->type) {
3054 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003055 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003056 break;
3057 }
3058 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003059 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003060 break;
3061 }
3062 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003063 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003064 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003065 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003066}
3067
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003068void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003069 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003070 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003071 // Map camera HAL error codes to ICameraDeviceCallback error codes
3072 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003073 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003074 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003075 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003076 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003077 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003078 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003079 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003080 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003081 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003082 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003083 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003084 };
3085
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003086 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003087 ((msg.error_code >= 0) &&
3088 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3089 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003090 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003091
3092 int streamId = 0;
3093 if (msg.error_stream != NULL) {
3094 Camera3Stream *stream =
3095 Camera3Stream::cast(msg.error_stream);
3096 streamId = stream->getId();
3097 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003098 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3099 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003100 streamId, msg.error_code);
3101
3102 CaptureResultExtras resultExtras;
3103 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003104 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003105 // SET_ERR calls notifyError
3106 SET_ERR("Camera HAL reported serious device error");
3107 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003108 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3109 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3110 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003111 {
3112 Mutex::Autolock l(mInFlightLock);
3113 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3114 if (idx >= 0) {
3115 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3116 r.requestStatus = msg.error_code;
3117 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07003118 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3119 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3120 errorCode) {
3121 r.skipResultMetadata = true;
3122 }
Emilian Peevba0fac32017-03-30 09:05:34 +01003123 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3124 errorCode) {
3125 // In case of missing result check whether the buffers
3126 // returned. If they returned, then remove inflight
3127 // request.
3128 removeInFlightRequestIfReadyLocked(idx);
3129 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003130 } else {
3131 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003132 ALOGE("Camera %s: %s: cannot find in-flight request on "
3133 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003134 resultExtras.frameNumber);
3135 }
3136 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003137 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003138 if (listener != NULL) {
3139 listener->notifyError(errorCode, resultExtras);
3140 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003141 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003142 }
3143 break;
3144 default:
3145 // SET_ERR calls notifyError
3146 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3147 break;
3148 }
3149}
3150
3151void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003152 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003153 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003154 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003155
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003156 // Set timestamp for the request in the in-flight tracking
3157 // and get the request ID to send upstream
3158 {
3159 Mutex::Autolock l(mInFlightLock);
3160 idx = mInFlightMap.indexOfKey(msg.frame_number);
3161 if (idx >= 0) {
3162 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003163
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003164 // Verify ordering of shutter notifications
3165 {
3166 Mutex::Autolock l(mOutputLock);
3167 // TODO: need to track errors for tighter bounds on expected frame number.
3168 if (r.hasInputBuffer) {
3169 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3170 SET_ERR("Shutter notification out-of-order. Expected "
3171 "notification for frame %d, got frame %d",
3172 mNextReprocessShutterFrameNumber, msg.frame_number);
3173 return;
3174 }
3175 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3176 } else {
3177 if (msg.frame_number < mNextShutterFrameNumber) {
3178 SET_ERR("Shutter notification out-of-order. Expected "
3179 "notification for frame %d, got frame %d",
3180 mNextShutterFrameNumber, msg.frame_number);
3181 return;
3182 }
3183 mNextShutterFrameNumber = msg.frame_number + 1;
3184 }
3185 }
3186
Shuzhen Wang4a472662017-02-26 23:29:04 -08003187 r.shutterTimestamp = msg.timestamp;
3188 if (r.hasCallback) {
3189 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003190 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003191 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003192 // Call listener, if any
3193 if (listener != NULL) {
3194 listener->notifyShutter(r.resultExtras, msg.timestamp);
3195 }
3196 // send pending result and buffers
3197 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3198 r.collectedPartialResult, msg.frame_number,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003199 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003200 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003201 returnOutputBuffers(r.pendingOutputBuffers.array(),
3202 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3203 r.pendingOutputBuffers.clear();
3204
3205 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003206 }
3207 }
3208 if (idx < 0) {
3209 SET_ERR("Shutter notification for non-existent frame number %d",
3210 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003211 }
3212}
3213
3214
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003215CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003216 ALOGV("%s", __FUNCTION__);
3217
Igor Murashkin1e479c02013-09-06 16:55:14 -07003218 CameraMetadata retVal;
3219
3220 if (mRequestThread != NULL) {
3221 retVal = mRequestThread->getLatestRequest();
3222 }
3223
Igor Murashkin1e479c02013-09-06 16:55:14 -07003224 return retVal;
3225}
3226
Jianing Weicb0652e2014-03-12 18:29:36 -07003227
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003228void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3229 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3230 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3231}
3232
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003233/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003234 * HalInterface inner class methods
3235 */
3236
Yifan Hongf79b5542017-04-11 14:44:25 -07003237Camera3Device::HalInterface::HalInterface(
3238 sp<ICameraDeviceSession> &session,
3239 std::shared_ptr<RequestMetadataQueue> queue) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003240 mHidlSession(session),
3241 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003242
Emilian Peev31abd0a2017-05-11 18:37:46 +01003243Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003244
3245Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003246 mHidlSession(other.mHidlSession),
3247 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003248
3249bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003250 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003251}
3252
3253void Camera3Device::HalInterface::clear() {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003254 mHidlSession.clear();
3255}
3256
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003257bool Camera3Device::HalInterface::supportBatchRequest() {
3258 return mHidlSession != nullptr;
3259}
3260
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003261status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3262 camera3_request_template_t templateId,
3263 /*out*/ camera_metadata_t **requestTemplate) {
3264 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3265 if (!valid()) return INVALID_OPERATION;
3266 status_t res = OK;
3267
Emilian Peev31abd0a2017-05-11 18:37:46 +01003268 common::V1_0::Status status;
3269 RequestTemplate id;
3270 switch (templateId) {
3271 case CAMERA3_TEMPLATE_PREVIEW:
3272 id = RequestTemplate::PREVIEW;
3273 break;
3274 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3275 id = RequestTemplate::STILL_CAPTURE;
3276 break;
3277 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3278 id = RequestTemplate::VIDEO_RECORD;
3279 break;
3280 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3281 id = RequestTemplate::VIDEO_SNAPSHOT;
3282 break;
3283 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3284 id = RequestTemplate::ZERO_SHUTTER_LAG;
3285 break;
3286 case CAMERA3_TEMPLATE_MANUAL:
3287 id = RequestTemplate::MANUAL;
3288 break;
3289 default:
3290 // Unknown template ID
3291 return BAD_VALUE;
3292 }
3293 auto err = mHidlSession->constructDefaultRequestSettings(id,
3294 [&status, &requestTemplate]
3295 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3296 status = s;
3297 if (status == common::V1_0::Status::OK) {
3298 const camera_metadata *r =
3299 reinterpret_cast<const camera_metadata_t*>(request.data());
3300 size_t expectedSize = request.size();
3301 int ret = validate_camera_metadata_structure(r, &expectedSize);
3302 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3303 *requestTemplate = clone_camera_metadata(r);
3304 if (*requestTemplate == nullptr) {
3305 ALOGE("%s: Unable to clone camera metadata received from HAL",
3306 __FUNCTION__);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003307 status = common::V1_0::Status::INTERNAL_ERROR;
3308 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003309 } else {
3310 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3311 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003312 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003313 }
3314 });
3315 if (!err.isOk()) {
3316 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3317 res = DEAD_OBJECT;
3318 } else {
3319 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003320 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003321
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003322 return res;
3323}
3324
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003325status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
3326 camera3_stream_configuration *config) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003327 ATRACE_NAME("CameraHal::configureStreams");
3328 if (!valid()) return INVALID_OPERATION;
3329 status_t res = OK;
3330
Emilian Peev31abd0a2017-05-11 18:37:46 +01003331 // Convert stream config to HIDL
3332 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003333 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3334 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3335 requestedConfiguration3_2.streams.resize(config->num_streams);
3336 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003337 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003338 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3339 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003340 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003341
Emilian Peev31abd0a2017-05-11 18:37:46 +01003342 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3343 cam3stream->setBufferFreedListener(this);
3344 int streamId = cam3stream->getId();
3345 StreamType streamType;
3346 switch (src->stream_type) {
3347 case CAMERA3_STREAM_OUTPUT:
3348 streamType = StreamType::OUTPUT;
3349 break;
3350 case CAMERA3_STREAM_INPUT:
3351 streamType = StreamType::INPUT;
3352 break;
3353 default:
3354 ALOGE("%s: Stream %d: Unsupported stream type %d",
3355 __FUNCTION__, streamId, config->streams[i]->stream_type);
3356 return BAD_VALUE;
3357 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003358 dst3_2.id = streamId;
3359 dst3_2.streamType = streamType;
3360 dst3_2.width = src->width;
3361 dst3_2.height = src->height;
3362 dst3_2.format = mapToPixelFormat(src->format);
3363 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3364 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3365 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3366 dst3_4.v3_2 = dst3_2;
3367 if (src->physical_camera_id != nullptr) {
3368 dst3_4.physicalCameraId = src->physical_camera_id;
3369 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003370
3371 activeStreams.insert(streamId);
3372 // Create Buffer ID map if necessary
3373 if (mBufferIdMaps.count(streamId) == 0) {
3374 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3375 }
3376 }
3377 // remove BufferIdMap for deleted streams
3378 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3379 int streamId = it->first;
3380 bool active = activeStreams.count(streamId) > 0;
3381 if (!active) {
3382 it = mBufferIdMaps.erase(it);
3383 } else {
3384 ++it;
3385 }
3386 }
3387
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003388 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003389 res = mapToStreamConfigurationMode(
3390 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003391 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003392 if (res != OK) {
3393 return res;
3394 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003395 requestedConfiguration3_2.operationMode = operationMode;
3396 requestedConfiguration3_4.operationMode = operationMode;
3397 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003398 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3399 get_camera_metadata_size(sessionParams));
3400
Emilian Peev31abd0a2017-05-11 18:37:46 +01003401 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003402 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003403 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003404
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003405 // See if we have v3.4 or v3.3 HAL
3406 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003407 sp<device::V3_3::ICameraDeviceSession> hidlSession_3_3;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003408 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3409 if (castResult_3_4.isOk()) {
3410 hidlSession_3_4 = castResult_3_4;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003411 } else {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003412 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3413 if (castResult_3_3.isOk()) {
3414 hidlSession_3_3 = castResult_3_3;
3415 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003416 }
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003417
3418 if (hidlSession_3_4 != nullptr) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003419 // We do; use v3.4 for the call, and construct a v3.4
3420 // HalStreamConfiguration
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003421 ALOGV("%s: v3.4 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003422 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
3423 auto err = hidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
3424 [&status, &finalConfiguration3_4]
3425 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3426 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003427 status = s;
3428 });
3429 if (!err.isOk()) {
3430 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3431 return DEAD_OBJECT;
3432 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003433 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3434 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3435 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3436 }
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003437 } else if (hidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003438 // We do; use v3.3 for the call
3439 ALOGV("%s: v3.3 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003440 auto err = hidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003441 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003442 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003443 finalConfiguration = halConfiguration;
3444 status = s;
3445 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003446 if (!err.isOk()) {
3447 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3448 return DEAD_OBJECT;
3449 }
3450 } else {
3451 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3452 ALOGV("%s: v3.2 device found", __FUNCTION__);
3453 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003454 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003455 [&status, &finalConfiguration_3_2]
3456 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3457 finalConfiguration_3_2 = halConfiguration;
3458 status = s;
3459 });
3460 if (!err.isOk()) {
3461 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3462 return DEAD_OBJECT;
3463 }
3464 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3465 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3466 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3467 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003468 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003469 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003470 }
3471
3472 if (status != common::V1_0::Status::OK ) {
3473 return CameraProviderManager::mapToStatusT(status);
3474 }
3475
3476 // And convert output stream configuration from HIDL
3477
3478 for (size_t i = 0; i < config->num_streams; i++) {
3479 camera3_stream_t *dst = config->streams[i];
3480 int streamId = Camera3Stream::cast(dst)->getId();
3481
3482 // Start scan at i, with the assumption that the stream order matches
3483 size_t realIdx = i;
3484 bool found = false;
3485 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003486 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003487 found = true;
3488 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003489 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003490 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3491 }
3492 if (!found) {
3493 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3494 __FUNCTION__, streamId);
3495 return INVALID_OPERATION;
3496 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003497 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003498
Emilian Peev710c1422017-08-30 11:19:38 +01003499 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3500 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003501 dstStream->setDataSpaceOverride(false);
3502 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3503 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3504
Emilian Peev31abd0a2017-05-11 18:37:46 +01003505 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3506 if (dst->format != overrideFormat) {
3507 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3508 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003509 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003510 if (dst->data_space != overrideDataSpace) {
3511 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3512 streamId, dst->format);
3513 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003514 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003515 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003516 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3517
Emilian Peev31abd0a2017-05-11 18:37:46 +01003518 // Override allowed with IMPLEMENTATION_DEFINED
3519 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003520 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003521 }
3522
Emilian Peev31abd0a2017-05-11 18:37:46 +01003523 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003524 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003525 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003526 __FUNCTION__, streamId);
3527 return INVALID_OPERATION;
3528 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003529 dstStream->setUsage(
3530 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003531 } else {
3532 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003533 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003534 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3535 __FUNCTION__, streamId);
3536 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003537 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003538 dstStream->setUsage(
3539 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003540 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003541 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003542 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003543
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003544 return res;
3545}
3546
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003547void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3548 /*out*/device::V3_2::CaptureRequest* captureRequest,
3549 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003550 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003551 if (captureRequest == nullptr || handlesCreated == nullptr) {
3552 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3553 __FUNCTION__, captureRequest, handlesCreated);
3554 return;
3555 }
3556
3557 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003558
3559 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003560
3561 {
3562 std::lock_guard<std::mutex> lock(mInflightLock);
3563 if (request->input_buffer != nullptr) {
3564 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3565 buffer_handle_t buf = *(request->input_buffer->buffer);
3566 auto pair = getBufferId(buf, streamId);
3567 bool isNewBuffer = pair.first;
3568 uint64_t bufferId = pair.second;
3569 captureRequest->inputBuffer.streamId = streamId;
3570 captureRequest->inputBuffer.bufferId = bufferId;
3571 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3572 captureRequest->inputBuffer.status = BufferStatus::OK;
3573 native_handle_t *acquireFence = nullptr;
3574 if (request->input_buffer->acquire_fence != -1) {
3575 acquireFence = native_handle_create(1,0);
3576 acquireFence->data[0] = request->input_buffer->acquire_fence;
3577 handlesCreated->push_back(acquireFence);
3578 }
3579 captureRequest->inputBuffer.acquireFence = acquireFence;
3580 captureRequest->inputBuffer.releaseFence = nullptr;
3581
3582 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3583 request->input_buffer->buffer,
3584 request->input_buffer->acquire_fence);
3585 } else {
3586 captureRequest->inputBuffer.streamId = -1;
3587 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3588 }
3589
3590 captureRequest->outputBuffers.resize(request->num_output_buffers);
3591 for (size_t i = 0; i < request->num_output_buffers; i++) {
3592 const camera3_stream_buffer_t *src = request->output_buffers + i;
3593 StreamBuffer &dst = captureRequest->outputBuffers[i];
3594 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3595 buffer_handle_t buf = *(src->buffer);
3596 auto pair = getBufferId(buf, streamId);
3597 bool isNewBuffer = pair.first;
3598 dst.streamId = streamId;
3599 dst.bufferId = pair.second;
3600 dst.buffer = isNewBuffer ? buf : nullptr;
3601 dst.status = BufferStatus::OK;
3602 native_handle_t *acquireFence = nullptr;
3603 if (src->acquire_fence != -1) {
3604 acquireFence = native_handle_create(1,0);
3605 acquireFence->data[0] = src->acquire_fence;
3606 handlesCreated->push_back(acquireFence);
3607 }
3608 dst.acquireFence = acquireFence;
3609 dst.releaseFence = nullptr;
3610
3611 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3612 src->buffer, src->acquire_fence);
3613 }
3614 }
3615}
3616
3617status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3618 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3619 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3620 if (!valid()) return INVALID_OPERATION;
3621
Emilian Peevaebbe412018-01-15 13:53:24 +00003622 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3623 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3624 if (castResult_3_4.isOk()) {
3625 hidlSession_3_4 = castResult_3_4;
3626 }
3627
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003628 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003629 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003630 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00003631 if (hidlSession_3_4 != nullptr) {
3632 captureRequests_3_4.resize(batchSize);
3633 } else {
3634 captureRequests.resize(batchSize);
3635 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003636 std::vector<native_handle_t*> handlesCreated;
3637
3638 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003639 if (hidlSession_3_4 != nullptr) {
3640 wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3641 /*out*/&handlesCreated);
3642 } else {
3643 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3644 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003645 }
3646
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003647 std::vector<device::V3_2::BufferCache> cachesToRemove;
3648 {
3649 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3650 for (auto& pair : mFreedBuffers) {
3651 // The stream might have been removed since onBufferFreed
3652 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3653 cachesToRemove.push_back({pair.first, pair.second});
3654 }
3655 }
3656 mFreedBuffers.clear();
3657 }
3658
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003659 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3660 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003661
3662 // Write metadata to FMQ.
3663 for (size_t i = 0; i < batchSize; i++) {
3664 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003665 device::V3_2::CaptureRequest* captureRequest;
3666 if (hidlSession_3_4 != nullptr) {
3667 captureRequest = &captureRequests_3_4[i].v3_2;
3668 } else {
3669 captureRequest = &captureRequests[i];
3670 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003671
3672 if (request->settings != nullptr) {
3673 size_t settingsSize = get_camera_metadata_size(request->settings);
3674 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3675 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3676 captureRequest->settings.resize(0);
3677 captureRequest->fmqSettingsSize = settingsSize;
3678 } else {
3679 if (mRequestMetadataQueue != nullptr) {
3680 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3681 }
3682 captureRequest->settings.setToExternal(
3683 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3684 get_camera_metadata_size(request->settings));
3685 captureRequest->fmqSettingsSize = 0u;
3686 }
3687 } else {
3688 // A null request settings maps to a size-0 CameraMetadata
3689 captureRequest->settings.resize(0);
3690 captureRequest->fmqSettingsSize = 0u;
3691 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003692
3693 if (hidlSession_3_4 != nullptr) {
3694 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3695 for (size_t j = 0; j < request->num_physcam_settings; j++) {
3696 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3697 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3698 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3699 settingsSize)) {
3700 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3701 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = settingsSize;
3702 } else {
3703 if (mRequestMetadataQueue != nullptr) {
3704 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3705 }
3706 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3707 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3708 request->physcam_settings[j])),
3709 get_camera_metadata_size(request->physcam_settings[j]));
3710 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
3711 }
3712 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3713 request->physcam_id[j];
3714 }
3715 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003716 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003717
3718 hardware::details::return_status err;
3719 if (hidlSession_3_4 != nullptr) {
3720 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003721 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3722 status = s;
3723 *numRequestProcessed = n;
3724 });
Emilian Peevaebbe412018-01-15 13:53:24 +00003725 } else {
3726 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
3727 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3728 status = s;
3729 *numRequestProcessed = n;
3730 });
3731 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003732 if (!err.isOk()) {
3733 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3734 return DEAD_OBJECT;
3735 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003736 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3737 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3738 __FUNCTION__, *numRequestProcessed, batchSize);
3739 status = common::V1_0::Status::INTERNAL_ERROR;
3740 }
3741
3742 for (auto& handle : handlesCreated) {
3743 native_handle_delete(handle);
3744 }
3745 return CameraProviderManager::mapToStatusT(status);
3746}
3747
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003748status_t Camera3Device::HalInterface::processCaptureRequest(
3749 camera3_capture_request_t *request) {
3750 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003751 if (!valid()) return INVALID_OPERATION;
3752 status_t res = OK;
3753
Emilian Peev31abd0a2017-05-11 18:37:46 +01003754 uint32_t numRequestProcessed = 0;
3755 std::vector<camera3_capture_request_t*> requests(1);
3756 requests[0] = request;
3757 res = processBatchCaptureRequests(requests, &numRequestProcessed);
3758
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003759 return res;
3760}
3761
3762status_t Camera3Device::HalInterface::flush() {
3763 ATRACE_NAME("CameraHal::flush");
3764 if (!valid()) return INVALID_OPERATION;
3765 status_t res = OK;
3766
Emilian Peev31abd0a2017-05-11 18:37:46 +01003767 auto err = mHidlSession->flush();
3768 if (!err.isOk()) {
3769 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3770 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003771 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003772 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003773 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003774
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003775 return res;
3776}
3777
Emilian Peev31abd0a2017-05-11 18:37:46 +01003778status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003779 ATRACE_NAME("CameraHal::dump");
3780 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003781
Emilian Peev31abd0a2017-05-11 18:37:46 +01003782 // Handled by CameraProviderManager::dump
3783
3784 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003785}
3786
3787status_t Camera3Device::HalInterface::close() {
3788 ATRACE_NAME("CameraHal::close()");
3789 if (!valid()) return INVALID_OPERATION;
3790 status_t res = OK;
3791
Emilian Peev31abd0a2017-05-11 18:37:46 +01003792 auto err = mHidlSession->close();
3793 // Interface will be dead shortly anyway, so don't log errors
3794 if (!err.isOk()) {
3795 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003796 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003797
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003798 return res;
3799}
3800
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003801void Camera3Device::HalInterface::getInflightBufferKeys(
3802 std::vector<std::pair<int32_t, int32_t>>* out) {
3803 std::lock_guard<std::mutex> lock(mInflightLock);
3804 out->clear();
3805 out->reserve(mInflightBufferMap.size());
3806 for (auto& pair : mInflightBufferMap) {
3807 uint64_t key = pair.first;
3808 int32_t streamId = key & 0xFFFFFFFF;
3809 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3810 out->push_back(std::make_pair(frameNumber, streamId));
3811 }
3812 return;
3813}
3814
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003815status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003816 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003817 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003818 auto pair = std::make_pair(buffer, acquireFence);
3819 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003820 return OK;
3821}
3822
3823status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003824 int32_t frameNumber, int32_t streamId,
3825 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003826 std::lock_guard<std::mutex> lock(mInflightLock);
3827
3828 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3829 auto it = mInflightBufferMap.find(key);
3830 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003831 auto pair = it->second;
3832 *buffer = pair.first;
3833 int acquireFence = pair.second;
3834 if (acquireFence > 0) {
3835 ::close(acquireFence);
3836 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003837 mInflightBufferMap.erase(it);
3838 return OK;
3839}
3840
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003841std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3842 const buffer_handle_t& buf, int streamId) {
3843 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3844
3845 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3846 auto it = bIdMap.find(buf);
3847 if (it == bIdMap.end()) {
3848 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003849 ALOGV("stream %d now have %zu buffer caches, buf %p",
3850 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003851 return std::make_pair(true, mNextBufferId - 1);
3852 } else {
3853 return std::make_pair(false, it->second);
3854 }
3855}
3856
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003857void Camera3Device::HalInterface::onBufferFreed(
3858 int streamId, const native_handle_t* handle) {
3859 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3860 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3861 auto mapIt = mBufferIdMaps.find(streamId);
3862 if (mapIt == mBufferIdMaps.end()) {
3863 // streamId might be from a deleted stream here
3864 ALOGI("%s: stream %d has been removed",
3865 __FUNCTION__, streamId);
3866 return;
3867 }
3868 BufferIdMap& bIdMap = mapIt->second;
3869 auto it = bIdMap.find(handle);
3870 if (it == bIdMap.end()) {
3871 ALOGW("%s: cannot find buffer %p in stream %d",
3872 __FUNCTION__, handle, streamId);
3873 return;
3874 } else {
3875 bufferId = it->second;
3876 bIdMap.erase(it);
3877 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3878 __FUNCTION__, streamId, bIdMap.size(), handle);
3879 }
3880 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3881}
3882
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003883/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003884 * RequestThread inner class methods
3885 */
3886
3887Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003888 sp<StatusTracker> statusTracker,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003889 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003890 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003891 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003892 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003893 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003894 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003895 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003896 mReconfigured(false),
3897 mDoPause(false),
3898 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003899 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003900 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003901 mCurrentAfTriggerId(0),
3902 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003903 mRepeatingLastFrameNumber(
3904 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003905 mPrepareVideoStream(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003906 mRequestLatency(kRequestLatencyBinSize),
3907 mSessionParamKeys(sessionParamKeys),
3908 mLatestSessionParams(sessionParamKeys.size()) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003909 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003910}
3911
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003912Camera3Device::RequestThread::~RequestThread() {}
3913
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003914void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003915 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003916 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003917 Mutex::Autolock l(mRequestLock);
3918 mListener = listener;
3919}
3920
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003921void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
3922 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003923 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003924 Mutex::Autolock l(mRequestLock);
3925 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003926 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003927 // Prepare video stream for high speed recording.
3928 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003929}
3930
Jianing Wei90e59c92014-03-12 18:29:36 -07003931status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003932 List<sp<CaptureRequest> > &requests,
3933 /*out*/
3934 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003935 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003936 Mutex::Autolock l(mRequestLock);
3937 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3938 ++it) {
3939 mRequestQueue.push_back(*it);
3940 }
3941
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003942 if (lastFrameNumber != NULL) {
3943 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3944 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3945 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3946 *lastFrameNumber);
3947 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003948
Jianing Wei90e59c92014-03-12 18:29:36 -07003949 unpauseForNewRequests();
3950
3951 return OK;
3952}
3953
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003954
3955status_t Camera3Device::RequestThread::queueTrigger(
3956 RequestTrigger trigger[],
3957 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003958 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003959 Mutex::Autolock l(mTriggerMutex);
3960 status_t ret;
3961
3962 for (size_t i = 0; i < count; ++i) {
3963 ret = queueTriggerLocked(trigger[i]);
3964
3965 if (ret != OK) {
3966 return ret;
3967 }
3968 }
3969
3970 return OK;
3971}
3972
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003973const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3974 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003975 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003976 if (d != nullptr) return d->mId;
3977 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003978}
3979
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003980status_t Camera3Device::RequestThread::queueTriggerLocked(
3981 RequestTrigger trigger) {
3982
3983 uint32_t tag = trigger.metadataTag;
3984 ssize_t index = mTriggerMap.indexOfKey(tag);
3985
3986 switch (trigger.getTagType()) {
3987 case TYPE_BYTE:
3988 // fall-through
3989 case TYPE_INT32:
3990 break;
3991 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003992 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3993 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003994 return INVALID_OPERATION;
3995 }
3996
3997 /**
3998 * Collect only the latest trigger, since we only have 1 field
3999 * in the request settings per trigger tag, and can't send more than 1
4000 * trigger per request.
4001 */
4002 if (index != NAME_NOT_FOUND) {
4003 mTriggerMap.editValueAt(index) = trigger;
4004 } else {
4005 mTriggerMap.add(tag, trigger);
4006 }
4007
4008 return OK;
4009}
4010
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004011status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004012 const RequestList &requests,
4013 /*out*/
4014 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004015 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004016 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004017 if (lastFrameNumber != NULL) {
4018 *lastFrameNumber = mRepeatingLastFrameNumber;
4019 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004020 mRepeatingRequests.clear();
4021 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4022 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004023
4024 unpauseForNewRequests();
4025
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004026 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004027 return OK;
4028}
4029
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004030bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004031 if (mRepeatingRequests.empty()) {
4032 return false;
4033 }
4034 int32_t requestId = requestIn->mResultExtras.requestId;
4035 const RequestList &repeatRequests = mRepeatingRequests;
4036 // All repeating requests are guaranteed to have same id so only check first quest
4037 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4038 return (firstRequest->mResultExtras.requestId == requestId);
4039}
4040
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004041status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004042 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004043 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004044 return clearRepeatingRequestsLocked(lastFrameNumber);
4045
4046}
4047
4048status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004049 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004050 if (lastFrameNumber != NULL) {
4051 *lastFrameNumber = mRepeatingLastFrameNumber;
4052 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004053 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004054 return OK;
4055}
4056
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004057status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004058 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004059 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004060 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004061 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004062
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004063 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004064
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004065 // Send errors for all requests pending in the request queue, including
4066 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004067 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004068 if (listener != NULL) {
4069 for (RequestList::iterator it = mRequestQueue.begin();
4070 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004071 // Abort the input buffers for reprocess requests.
4072 if ((*it)->mInputStream != NULL) {
4073 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004074 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4075 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004076 if (res != OK) {
4077 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4078 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4079 } else {
4080 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4081 if (res != OK) {
4082 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4083 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4084 }
4085 }
4086 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004087 // Set the frame number this request would have had, if it
4088 // had been submitted; this frame number will not be reused.
4089 // The requestId and burstId fields were set when the request was
4090 // submitted originally (in convertMetadataListToRequestListLocked)
4091 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004092 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004093 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004094 }
4095 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004096 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004097
4098 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004099 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004100 if (lastFrameNumber != NULL) {
4101 *lastFrameNumber = mRepeatingLastFrameNumber;
4102 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004103 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004104 return OK;
4105}
4106
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004107status_t Camera3Device::RequestThread::flush() {
4108 ATRACE_CALL();
4109 Mutex::Autolock l(mFlushLock);
4110
Emilian Peev08dd2452017-04-06 16:55:14 +01004111 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004112}
4113
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004114void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004115 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004116 Mutex::Autolock l(mPauseLock);
4117 mDoPause = paused;
4118 mDoPauseSignal.signal();
4119}
4120
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004121status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4122 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004123 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004124 Mutex::Autolock l(mLatestRequestMutex);
4125 status_t res;
4126 while (mLatestRequestId != requestId) {
4127 nsecs_t startTime = systemTime();
4128
4129 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4130 if (res != OK) return res;
4131
4132 timeout -= (systemTime() - startTime);
4133 }
4134
4135 return OK;
4136}
4137
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004138void Camera3Device::RequestThread::requestExit() {
4139 // Call parent to set up shutdown
4140 Thread::requestExit();
4141 // The exit from any possible waits
4142 mDoPauseSignal.signal();
4143 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004144
4145 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4146 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004147}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004148
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004149void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004150 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004151 bool surfaceAbandoned = false;
4152 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004153 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004154 {
4155 Mutex::Autolock l(mRequestLock);
4156 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4157 // repeating requests.
4158 for (const auto& request : mRepeatingRequests) {
4159 for (const auto& s : request->mOutputStreams) {
4160 if (s->isAbandoned()) {
4161 surfaceAbandoned = true;
4162 clearRepeatingRequestsLocked(&lastFrameNumber);
4163 break;
4164 }
4165 }
4166 if (surfaceAbandoned) {
4167 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004168 }
4169 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004170 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004171 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004172
4173 if (listener != NULL && surfaceAbandoned) {
4174 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004175 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004176}
4177
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004178bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004179 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004180 status_t res;
4181 size_t batchSize = mNextRequests.size();
4182 std::vector<camera3_capture_request_t*> requests(batchSize);
4183 uint32_t numRequestProcessed = 0;
4184 for (size_t i = 0; i < batchSize; i++) {
4185 requests[i] = &mNextRequests.editItemAt(i).halRequest;
4186 }
4187
4188 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
4189 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4190
4191 bool triggerRemoveFailed = false;
4192 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4193 for (size_t i = 0; i < numRequestProcessed; i++) {
4194 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4195 nextRequest.submitted = true;
4196
4197
4198 // Update the latest request sent to HAL
4199 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4200 Mutex::Autolock al(mLatestRequestMutex);
4201
4202 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4203 mLatestRequest.acquire(cloned);
4204
4205 sp<Camera3Device> parent = mParent.promote();
4206 if (parent != NULL) {
4207 parent->monitorMetadata(TagMonitor::REQUEST,
4208 nextRequest.halRequest.frame_number,
4209 0, mLatestRequest);
4210 }
4211 }
4212
4213 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004214 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4215 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004216 }
4217
Emilian Peevaebbe412018-01-15 13:53:24 +00004218 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4219
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004220 if (!triggerRemoveFailed) {
4221 // Remove any previously queued triggers (after unlock)
4222 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4223 if (removeTriggerRes != OK) {
4224 triggerRemoveFailed = true;
4225 triggerFailedRequest = nextRequest;
4226 }
4227 }
4228 }
4229
4230 if (triggerRemoveFailed) {
4231 SET_ERR("RequestThread: Unable to remove triggers "
4232 "(capture request %d, HAL device: %s (%d)",
4233 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4234 cleanUpFailedRequests(/*sendRequestError*/ false);
4235 return false;
4236 }
4237
4238 if (res != OK) {
4239 // Should only get a failure here for malformed requests or device-level
4240 // errors, so consider all errors fatal. Bad metadata failures should
4241 // come through notify.
4242 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4243 mNextRequests[numRequestProcessed].halRequest.frame_number,
4244 strerror(-res), res);
4245 cleanUpFailedRequests(/*sendRequestError*/ false);
4246 return false;
4247 }
4248 return true;
4249}
4250
4251bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4252 status_t res;
4253
4254 for (auto& nextRequest : mNextRequests) {
4255 // Submit request and block until ready for next one
4256 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4257 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4258
4259 if (res != OK) {
4260 // Should only get a failure here for malformed requests or device-level
4261 // errors, so consider all errors fatal. Bad metadata failures should
4262 // come through notify.
4263 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4264 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4265 res);
4266 cleanUpFailedRequests(/*sendRequestError*/ false);
4267 return false;
4268 }
4269
4270 // Mark that the request has be submitted successfully.
4271 nextRequest.submitted = true;
4272
4273 // Update the latest request sent to HAL
4274 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4275 Mutex::Autolock al(mLatestRequestMutex);
4276
4277 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4278 mLatestRequest.acquire(cloned);
4279
4280 sp<Camera3Device> parent = mParent.promote();
4281 if (parent != NULL) {
4282 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4283 0, mLatestRequest);
4284 }
4285 }
4286
4287 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004288 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4289 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004290 }
4291
Emilian Peevaebbe412018-01-15 13:53:24 +00004292 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4293
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004294 // Remove any previously queued triggers (after unlock)
4295 res = removeTriggers(mPrevRequest);
4296 if (res != OK) {
4297 SET_ERR("RequestThread: Unable to remove triggers "
4298 "(capture request %d, HAL device: %s (%d)",
4299 nextRequest.halRequest.frame_number, strerror(-res), res);
4300 cleanUpFailedRequests(/*sendRequestError*/ false);
4301 return false;
4302 }
4303 }
4304 return true;
4305}
4306
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004307nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4308 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4309 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4310 find_camera_metadata_ro_entry(request,
4311 ANDROID_CONTROL_AE_MODE,
4312 &e);
4313 if (e.count == 0) return maxExpectedDuration;
4314
4315 switch (e.data.u8[0]) {
4316 case ANDROID_CONTROL_AE_MODE_OFF:
4317 find_camera_metadata_ro_entry(request,
4318 ANDROID_SENSOR_EXPOSURE_TIME,
4319 &e);
4320 if (e.count > 0) {
4321 maxExpectedDuration = e.data.i64[0];
4322 }
4323 find_camera_metadata_ro_entry(request,
4324 ANDROID_SENSOR_FRAME_DURATION,
4325 &e);
4326 if (e.count > 0) {
4327 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4328 }
4329 break;
4330 default:
4331 find_camera_metadata_ro_entry(request,
4332 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4333 &e);
4334 if (e.count > 1) {
4335 maxExpectedDuration = 1e9 / e.data.u8[0];
4336 }
4337 break;
4338 }
4339
4340 return maxExpectedDuration;
4341}
4342
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004343bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4344 ATRACE_CALL();
4345 bool updatesDetected = false;
4346
4347 for (auto tag : mSessionParamKeys) {
4348 camera_metadata_ro_entry entry = settings.find(tag);
4349 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4350
4351 if (entry.count > 0) {
4352 bool isDifferent = false;
4353 if (lastEntry.count > 0) {
4354 // Have a last value, compare to see if changed
4355 if (lastEntry.type == entry.type &&
4356 lastEntry.count == entry.count) {
4357 // Same type and count, compare values
4358 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4359 size_t entryBytes = bytesPerValue * lastEntry.count;
4360 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4361 if (cmp != 0) {
4362 isDifferent = true;
4363 }
4364 } else {
4365 // Count or type has changed
4366 isDifferent = true;
4367 }
4368 } else {
4369 // No last entry, so always consider to be different
4370 isDifferent = true;
4371 }
4372
4373 if (isDifferent) {
4374 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
4375 mLatestSessionParams.update(entry);
4376 updatesDetected = true;
4377 }
4378 } else if (lastEntry.count > 0) {
4379 // Value has been removed
4380 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4381 mLatestSessionParams.erase(tag);
4382 updatesDetected = true;
4383 }
4384 }
4385
4386 return updatesDetected;
4387}
4388
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004389bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004390 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004391 status_t res;
4392
4393 // Handle paused state.
4394 if (waitIfPaused()) {
4395 return true;
4396 }
4397
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004398 // Wait for the next batch of requests.
4399 waitForNextRequestBatch();
4400 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004401 return true;
4402 }
4403
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004404 // Get the latest request ID, if any
4405 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004406 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004407 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004408 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004409 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004410 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004411 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4412 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004413 }
4414
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004415 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4416 // or a single request from streaming or burst. In either case the first element
4417 // should contain the latest camera settings that we need to check for any session
4418 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004419 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004420 res = OK;
4421
4422 //Input stream buffers are already acquired at this point so an input stream
4423 //will not be able to move to idle state unless we force it.
4424 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4425 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4426 if (res != OK) {
4427 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4428 cleanUpFailedRequests(/*sendRequestError*/ false);
4429 return false;
4430 }
4431 }
4432
4433 if (res == OK) {
4434 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4435 if (statusTracker != 0) {
4436 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4437
4438 sp<Camera3Device> parent = mParent.promote();
4439 if (parent != nullptr) {
4440 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4441 }
4442
4443 statusTracker->markComponentActive(mStatusId);
4444 setPaused(false);
4445 }
4446
4447 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4448 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4449 if (res != OK) {
4450 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4451 cleanUpFailedRequests(/*sendRequestError*/ false);
4452 return false;
4453 }
4454 }
4455 }
4456 }
4457
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004458 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004459 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004460 if (res == TIMED_OUT) {
4461 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004462 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004463 // Check if any stream is abandoned.
4464 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004465 return true;
4466 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004467 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004468 return false;
4469 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004470
Zhijun Hecc27e112013-10-03 16:12:43 -07004471 // Inform waitUntilRequestProcessed thread of a new request ID
4472 {
4473 Mutex::Autolock al(mLatestRequestMutex);
4474
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004475 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004476 mLatestRequestSignal.signal();
4477 }
4478
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004479 // Submit a batch of requests to HAL.
4480 // Use flush lock only when submitting multilple requests in a batch.
4481 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4482 // which may take a long time to finish so synchronizing flush() and
4483 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4484 // For now, only synchronize for high speed recording and we should figure something out for
4485 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004486 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004487
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004488 if (useFlushLock) {
4489 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004490 }
4491
Zhijun Hef0645c12016-08-02 00:58:11 -07004492 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004493 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004494
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004495 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004496 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004497 if (mInterface->supportBatchRequest()) {
4498 submitRequestSuccess = sendRequestsBatch();
4499 } else {
4500 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004501 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004502 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4503 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004504
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004505 if (useFlushLock) {
4506 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004507 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004508
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004509 // Unset as current request
4510 {
4511 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004512 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004513 }
4514
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004515 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004516}
4517
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004518status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004519 ATRACE_CALL();
4520
Shuzhen Wang4a472662017-02-26 23:29:04 -08004521 for (size_t i = 0; i < mNextRequests.size(); i++) {
4522 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004523 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4524 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4525 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4526
4527 // Prepare a request to HAL
4528 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4529
4530 // Insert any queued triggers (before metadata is locked)
4531 status_t res = insertTriggers(captureRequest);
4532
4533 if (res < 0) {
4534 SET_ERR("RequestThread: Unable to insert triggers "
4535 "(capture request %d, HAL device: %s (%d)",
4536 halRequest->frame_number, strerror(-res), res);
4537 return INVALID_OPERATION;
4538 }
4539 int triggerCount = res;
4540 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4541 mPrevTriggers = triggerCount;
4542
4543 // If the request is the same as last, or we had triggers last time
4544 if (mPrevRequest != captureRequest || triggersMixedIn) {
4545 /**
4546 * HAL workaround:
4547 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4548 */
4549 res = addDummyTriggerIds(captureRequest);
4550 if (res != OK) {
4551 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4552 "(capture request %d, HAL device: %s (%d)",
4553 halRequest->frame_number, strerror(-res), res);
4554 return INVALID_OPERATION;
4555 }
4556
4557 /**
4558 * The request should be presorted so accesses in HAL
4559 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4560 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004561 captureRequest->mSettingsList.begin()->metadata.sort();
4562 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004563 mPrevRequest = captureRequest;
4564 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4565
4566 IF_ALOGV() {
4567 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4568 find_camera_metadata_ro_entry(
4569 halRequest->settings,
4570 ANDROID_CONTROL_AF_TRIGGER,
4571 &e
4572 );
4573 if (e.count > 0) {
4574 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4575 __FUNCTION__,
4576 halRequest->frame_number,
4577 e.data.u8[0]);
4578 }
4579 }
4580 } else {
4581 // leave request.settings NULL to indicate 'reuse latest given'
4582 ALOGVV("%s: Request settings are REUSED",
4583 __FUNCTION__);
4584 }
4585
Emilian Peevaebbe412018-01-15 13:53:24 +00004586 if (captureRequest->mSettingsList.size() > 1) {
4587 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4588 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
4589 halRequest->physcam_settings =
4590 new const camera_metadata* [halRequest->num_physcam_settings];
4591 auto it = ++captureRequest->mSettingsList.begin();
4592 size_t i = 0;
4593 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4594 halRequest->physcam_id[i] = it->cameraId.c_str();
4595 it->metadata.sort();
4596 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4597 }
4598 }
4599
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004600 uint32_t totalNumBuffers = 0;
4601
4602 // Fill in buffers
4603 if (captureRequest->mInputStream != NULL) {
4604 halRequest->input_buffer = &captureRequest->mInputBuffer;
4605 totalNumBuffers += 1;
4606 } else {
4607 halRequest->input_buffer = NULL;
4608 }
4609
4610 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4611 captureRequest->mOutputStreams.size());
4612 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004613 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4614 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004615
4616 // Prepare video buffers for high speed recording on the first video request.
4617 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4618 // Only try to prepare video stream on the first video request.
4619 mPrepareVideoStream = false;
4620
4621 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4622 while (res == NOT_ENOUGH_DATA) {
4623 res = outputStream->prepareNextBuffer();
4624 }
4625 if (res != OK) {
4626 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4627 __FUNCTION__, strerror(-res), res);
4628 outputStream->cancelPrepare();
4629 }
4630 }
4631
Shuzhen Wang4a472662017-02-26 23:29:04 -08004632 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4633 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004634 if (res != OK) {
4635 // Can't get output buffer from gralloc queue - this could be due to
4636 // abandoned queue or other consumer misbehavior, so not a fatal
4637 // error
4638 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4639 " %s (%d)", strerror(-res), res);
4640
4641 return TIMED_OUT;
4642 }
4643 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004644
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004645 }
4646 totalNumBuffers += halRequest->num_output_buffers;
4647
4648 // Log request in the in-flight queue
4649 sp<Camera3Device> parent = mParent.promote();
4650 if (parent == NULL) {
4651 // Should not happen, and nowhere to send errors to, so just log it
4652 CLOGE("RequestThread: Parent is gone");
4653 return INVALID_OPERATION;
4654 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004655
4656 // If this request list is for constrained high speed recording (not
4657 // preview), and the current request is not the last one in the batch,
4658 // do not send callback to the app.
4659 bool hasCallback = true;
4660 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4661 hasCallback = false;
4662 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004663 res = parent->registerInFlight(halRequest->frame_number,
4664 totalNumBuffers, captureRequest->mResultExtras,
4665 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004666 hasCallback,
4667 calculateMaxExpectedDuration(halRequest->settings));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004668 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4669 ", burstId = %" PRId32 ".",
4670 __FUNCTION__,
4671 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4672 captureRequest->mResultExtras.burstId);
4673 if (res != OK) {
4674 SET_ERR("RequestThread: Unable to register new in-flight request:"
4675 " %s (%d)", strerror(-res), res);
4676 return INVALID_OPERATION;
4677 }
4678 }
4679
4680 return OK;
4681}
4682
Igor Murashkin1e479c02013-09-06 16:55:14 -07004683CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004684 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004685 Mutex::Autolock al(mLatestRequestMutex);
4686
4687 ALOGV("RequestThread::%s", __FUNCTION__);
4688
4689 return mLatestRequest;
4690}
4691
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004692bool Camera3Device::RequestThread::isStreamPending(
4693 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004694 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004695 Mutex::Autolock l(mRequestLock);
4696
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004697 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004698 if (!nextRequest.submitted) {
4699 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4700 if (stream == s) return true;
4701 }
4702 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004703 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004704 }
4705
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004706 for (const auto& request : mRequestQueue) {
4707 for (const auto& s : request->mOutputStreams) {
4708 if (stream == s) return true;
4709 }
4710 if (stream == request->mInputStream) return true;
4711 }
4712
4713 for (const auto& request : mRepeatingRequests) {
4714 for (const auto& s : request->mOutputStreams) {
4715 if (stream == s) return true;
4716 }
4717 if (stream == request->mInputStream) return true;
4718 }
4719
4720 return false;
4721}
Jianing Weicb0652e2014-03-12 18:29:36 -07004722
Emilian Peev40ead602017-09-26 15:46:36 +01004723bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4724 ATRACE_CALL();
4725 Mutex::Autolock l(mRequestLock);
4726
4727 for (const auto& nextRequest : mNextRequests) {
4728 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4729 if (s.first == streamId) {
4730 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4731 if (it != s.second.end()) {
4732 return true;
4733 }
4734 }
4735 }
4736 }
4737
4738 for (const auto& request : mRequestQueue) {
4739 for (const auto& s : request->mOutputSurfaces) {
4740 if (s.first == streamId) {
4741 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4742 if (it != s.second.end()) {
4743 return true;
4744 }
4745 }
4746 }
4747 }
4748
4749 for (const auto& request : mRepeatingRequests) {
4750 for (const auto& s : request->mOutputSurfaces) {
4751 if (s.first == streamId) {
4752 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4753 if (it != s.second.end()) {
4754 return true;
4755 }
4756 }
4757 }
4758 }
4759
4760 return false;
4761}
4762
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004763nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004764 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004765 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004766 return mExpectedInflightDuration > kMinInflightDuration ?
4767 mExpectedInflightDuration : kMinInflightDuration;
4768}
4769
Emilian Peevaebbe412018-01-15 13:53:24 +00004770void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
4771 camera3_capture_request_t *halRequest) {
4772 if ((request == nullptr) || (halRequest == nullptr)) {
4773 ALOGE("%s: Invalid request!", __FUNCTION__);
4774 return;
4775 }
4776
4777 if (halRequest->num_physcam_settings > 0) {
4778 if (halRequest->physcam_id != nullptr) {
4779 delete [] halRequest->physcam_id;
4780 halRequest->physcam_id = nullptr;
4781 }
4782 if (halRequest->physcam_settings != nullptr) {
4783 auto it = ++(request->mSettingsList.begin());
4784 size_t i = 0;
4785 for (; it != request->mSettingsList.end(); it++, i++) {
4786 it->metadata.unlock(halRequest->physcam_settings[i]);
4787 }
4788 delete [] halRequest->physcam_settings;
4789 halRequest->physcam_settings = nullptr;
4790 }
4791 }
4792}
4793
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004794void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4795 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004796 return;
4797 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004798
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004799 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004800 // Skip the ones that have been submitted successfully.
4801 if (nextRequest.submitted) {
4802 continue;
4803 }
4804
4805 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4806 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4807 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4808
4809 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004810 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004811 }
4812
Emilian Peevaebbe412018-01-15 13:53:24 +00004813 cleanupPhysicalSettings(captureRequest, halRequest);
4814
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004815 if (captureRequest->mInputStream != NULL) {
4816 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4817 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4818 }
4819
4820 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004821 //Buffers that failed processing could still have
4822 //valid acquire fence.
4823 int acquireFence = (*outputBuffers)[i].acquire_fence;
4824 if (0 <= acquireFence) {
4825 close(acquireFence);
4826 outputBuffers->editItemAt(i).acquire_fence = -1;
4827 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004828 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4829 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4830 }
4831
4832 if (sendRequestError) {
4833 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004834 sp<NotificationListener> listener = mListener.promote();
4835 if (listener != NULL) {
4836 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004837 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004838 captureRequest->mResultExtras);
4839 }
4840 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004841
4842 // Remove yet-to-be submitted inflight request from inflightMap
4843 {
4844 sp<Camera3Device> parent = mParent.promote();
4845 if (parent != NULL) {
4846 Mutex::Autolock l(parent->mInFlightLock);
4847 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4848 if (idx >= 0) {
4849 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4850 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4851 parent->removeInFlightMapEntryLocked(idx);
4852 }
4853 }
4854 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004855 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004856
4857 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004858 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004859}
4860
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004861void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004862 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004863 // Optimized a bit for the simple steady-state case (single repeating
4864 // request), to avoid putting that request in the queue temporarily.
4865 Mutex::Autolock l(mRequestLock);
4866
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004867 assert(mNextRequests.empty());
4868
4869 NextRequest nextRequest;
4870 nextRequest.captureRequest = waitForNextRequestLocked();
4871 if (nextRequest.captureRequest == nullptr) {
4872 return;
4873 }
4874
4875 nextRequest.halRequest = camera3_capture_request_t();
4876 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004877 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004878
4879 // Wait for additional requests
4880 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4881
4882 for (size_t i = 1; i < batchSize; i++) {
4883 NextRequest additionalRequest;
4884 additionalRequest.captureRequest = waitForNextRequestLocked();
4885 if (additionalRequest.captureRequest == nullptr) {
4886 break;
4887 }
4888
4889 additionalRequest.halRequest = camera3_capture_request_t();
4890 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004891 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004892 }
4893
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004894 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004895 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004896 mNextRequests.size(), batchSize);
4897 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004898 }
4899
4900 return;
4901}
4902
4903sp<Camera3Device::CaptureRequest>
4904 Camera3Device::RequestThread::waitForNextRequestLocked() {
4905 status_t res;
4906 sp<CaptureRequest> nextRequest;
4907
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004908 while (mRequestQueue.empty()) {
4909 if (!mRepeatingRequests.empty()) {
4910 // Always atomically enqueue all requests in a repeating request
4911 // list. Guarantees a complete in-sequence set of captures to
4912 // application.
4913 const RequestList &requests = mRepeatingRequests;
4914 RequestList::const_iterator firstRequest =
4915 requests.begin();
4916 nextRequest = *firstRequest;
4917 mRequestQueue.insert(mRequestQueue.end(),
4918 ++firstRequest,
4919 requests.end());
4920 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004921
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004922 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004923
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004924 break;
4925 }
4926
4927 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4928
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004929 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4930 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004931 Mutex::Autolock pl(mPauseLock);
4932 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004933 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004934 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004935 // Let the tracker know
4936 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4937 if (statusTracker != 0) {
4938 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4939 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004940 }
4941 // Stop waiting for now and let thread management happen
4942 return NULL;
4943 }
4944 }
4945
4946 if (nextRequest == NULL) {
4947 // Don't have a repeating request already in hand, so queue
4948 // must have an entry now.
4949 RequestList::iterator firstRequest =
4950 mRequestQueue.begin();
4951 nextRequest = *firstRequest;
4952 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004953 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4954 sp<NotificationListener> listener = mListener.promote();
4955 if (listener != NULL) {
4956 listener->notifyRequestQueueEmpty();
4957 }
4958 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004959 }
4960
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004961 // In case we've been unpaused by setPaused clearing mDoPause, need to
4962 // update internal pause state (capture/setRepeatingRequest unpause
4963 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004964 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004965 if (mPaused) {
4966 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4967 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4968 if (statusTracker != 0) {
4969 statusTracker->markComponentActive(mStatusId);
4970 }
4971 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004972 mPaused = false;
4973
4974 // Check if we've reconfigured since last time, and reset the preview
4975 // request if so. Can't use 'NULL request == repeat' across configure calls.
4976 if (mReconfigured) {
4977 mPrevRequest.clear();
4978 mReconfigured = false;
4979 }
4980
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004981 if (nextRequest != NULL) {
4982 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004983 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4984 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004985
4986 // Since RequestThread::clear() removes buffers from the input stream,
4987 // get the right buffer here before unlocking mRequestLock
4988 if (nextRequest->mInputStream != NULL) {
4989 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4990 if (res != OK) {
4991 // Can't get input buffer from gralloc queue - this could be due to
4992 // disconnected queue or other producer misbehavior, so not a fatal
4993 // error
4994 ALOGE("%s: Can't get input buffer, skipping request:"
4995 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004996
4997 sp<NotificationListener> listener = mListener.promote();
4998 if (listener != NULL) {
4999 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005000 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005001 nextRequest->mResultExtras);
5002 }
5003 return NULL;
5004 }
5005 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005006 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005007
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005008 return nextRequest;
5009}
5010
5011bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005012 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005013 status_t res;
5014 Mutex::Autolock l(mPauseLock);
5015 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005016 if (mPaused == false) {
5017 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005018 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5019 // Let the tracker know
5020 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5021 if (statusTracker != 0) {
5022 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5023 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005024 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005025
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005026 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005027 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005028 return true;
5029 }
5030 }
5031 // We don't set mPaused to false here, because waitForNextRequest needs
5032 // to further manage the paused state in case of starvation.
5033 return false;
5034}
5035
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005036void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005037 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005038 // With work to do, mark thread as unpaused.
5039 // If paused by request (setPaused), don't resume, to avoid
5040 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005041 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005042 Mutex::Autolock p(mPauseLock);
5043 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005044 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5045 if (mPaused) {
5046 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5047 if (statusTracker != 0) {
5048 statusTracker->markComponentActive(mStatusId);
5049 }
5050 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005051 mPaused = false;
5052 }
5053}
5054
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005055void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5056 sp<Camera3Device> parent = mParent.promote();
5057 if (parent != NULL) {
5058 va_list args;
5059 va_start(args, fmt);
5060
5061 parent->setErrorStateV(fmt, args);
5062
5063 va_end(args);
5064 }
5065}
5066
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005067status_t Camera3Device::RequestThread::insertTriggers(
5068 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005069 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005070 Mutex::Autolock al(mTriggerMutex);
5071
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005072 sp<Camera3Device> parent = mParent.promote();
5073 if (parent == NULL) {
5074 CLOGE("RequestThread: Parent is gone");
5075 return DEAD_OBJECT;
5076 }
5077
Emilian Peevaebbe412018-01-15 13:53:24 +00005078 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005079 size_t count = mTriggerMap.size();
5080
5081 for (size_t i = 0; i < count; ++i) {
5082 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005083 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005084
5085 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5086 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5087 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005088 if (isAeTrigger) {
5089 request->mResultExtras.precaptureTriggerId = triggerId;
5090 mCurrentPreCaptureTriggerId = triggerId;
5091 } else {
5092 request->mResultExtras.afTriggerId = triggerId;
5093 mCurrentAfTriggerId = triggerId;
5094 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005095 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005096 }
5097
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005098 camera_metadata_entry entry = metadata.find(tag);
5099
5100 if (entry.count > 0) {
5101 /**
5102 * Already has an entry for this trigger in the request.
5103 * Rewrite it with our requested trigger value.
5104 */
5105 RequestTrigger oldTrigger = trigger;
5106
5107 oldTrigger.entryValue = entry.data.u8[0];
5108
5109 mTriggerReplacedMap.add(tag, oldTrigger);
5110 } else {
5111 /**
5112 * More typical, no trigger entry, so we just add it
5113 */
5114 mTriggerRemovedMap.add(tag, trigger);
5115 }
5116
5117 status_t res;
5118
5119 switch (trigger.getTagType()) {
5120 case TYPE_BYTE: {
5121 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5122 res = metadata.update(tag,
5123 &entryValue,
5124 /*count*/1);
5125 break;
5126 }
5127 case TYPE_INT32:
5128 res = metadata.update(tag,
5129 &trigger.entryValue,
5130 /*count*/1);
5131 break;
5132 default:
5133 ALOGE("%s: Type not supported: 0x%x",
5134 __FUNCTION__,
5135 trigger.getTagType());
5136 return INVALID_OPERATION;
5137 }
5138
5139 if (res != OK) {
5140 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5141 ", value %d", __FUNCTION__, trigger.getTagName(),
5142 trigger.entryValue);
5143 return res;
5144 }
5145
5146 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5147 trigger.getTagName(),
5148 trigger.entryValue);
5149 }
5150
5151 mTriggerMap.clear();
5152
5153 return count;
5154}
5155
5156status_t Camera3Device::RequestThread::removeTriggers(
5157 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005158 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005159 Mutex::Autolock al(mTriggerMutex);
5160
Emilian Peevaebbe412018-01-15 13:53:24 +00005161 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005162
5163 /**
5164 * Replace all old entries with their old values.
5165 */
5166 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5167 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5168
5169 status_t res;
5170
5171 uint32_t tag = trigger.metadataTag;
5172 switch (trigger.getTagType()) {
5173 case TYPE_BYTE: {
5174 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5175 res = metadata.update(tag,
5176 &entryValue,
5177 /*count*/1);
5178 break;
5179 }
5180 case TYPE_INT32:
5181 res = metadata.update(tag,
5182 &trigger.entryValue,
5183 /*count*/1);
5184 break;
5185 default:
5186 ALOGE("%s: Type not supported: 0x%x",
5187 __FUNCTION__,
5188 trigger.getTagType());
5189 return INVALID_OPERATION;
5190 }
5191
5192 if (res != OK) {
5193 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5194 ", trigger value %d", __FUNCTION__,
5195 trigger.getTagName(), trigger.entryValue);
5196 return res;
5197 }
5198 }
5199 mTriggerReplacedMap.clear();
5200
5201 /**
5202 * Remove all new entries.
5203 */
5204 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5205 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5206 status_t res = metadata.erase(trigger.metadataTag);
5207
5208 if (res != OK) {
5209 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5210 ", trigger value %d", __FUNCTION__,
5211 trigger.getTagName(), trigger.entryValue);
5212 return res;
5213 }
5214 }
5215 mTriggerRemovedMap.clear();
5216
5217 return OK;
5218}
5219
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005220status_t Camera3Device::RequestThread::addDummyTriggerIds(
5221 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005222 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005223 static const int32_t dummyTriggerId = 1;
5224 status_t res;
5225
Emilian Peevaebbe412018-01-15 13:53:24 +00005226 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005227
5228 // If AF trigger is active, insert a dummy AF trigger ID if none already
5229 // exists
5230 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5231 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5232 if (afTrigger.count > 0 &&
5233 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5234 afId.count == 0) {
5235 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5236 if (res != OK) return res;
5237 }
5238
5239 // If AE precapture trigger is active, insert a dummy precapture trigger ID
5240 // if none already exists
5241 camera_metadata_entry pcTrigger =
5242 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5243 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5244 if (pcTrigger.count > 0 &&
5245 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5246 pcId.count == 0) {
5247 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5248 &dummyTriggerId, 1);
5249 if (res != OK) return res;
5250 }
5251
5252 return OK;
5253}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005254
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005255/**
5256 * PreparerThread inner class methods
5257 */
5258
5259Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005260 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005261 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005262}
5263
5264Camera3Device::PreparerThread::~PreparerThread() {
5265 Thread::requestExitAndWait();
5266 if (mCurrentStream != nullptr) {
5267 mCurrentStream->cancelPrepare();
5268 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5269 mCurrentStream.clear();
5270 }
5271 clear();
5272}
5273
Ruben Brunkc78ac262015-08-13 17:58:46 -07005274status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005275 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005276 status_t res;
5277
5278 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005279 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005280
Ruben Brunkc78ac262015-08-13 17:58:46 -07005281 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005282 if (res == OK) {
5283 // No preparation needed, fire listener right off
5284 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005285 if (listener != NULL) {
5286 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005287 }
5288 return OK;
5289 } else if (res != NOT_ENOUGH_DATA) {
5290 return res;
5291 }
5292
5293 // Need to prepare, start up thread if necessary
5294 if (!mActive) {
5295 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5296 // isn't running
5297 Thread::requestExitAndWait();
5298 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5299 if (res != OK) {
5300 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005301 if (listener != NULL) {
5302 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005303 }
5304 return res;
5305 }
5306 mCancelNow = false;
5307 mActive = true;
5308 ALOGV("%s: Preparer stream started", __FUNCTION__);
5309 }
5310
5311 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005312 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005313 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5314
5315 return OK;
5316}
5317
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005318void Camera3Device::PreparerThread::pause() {
5319 ATRACE_CALL();
5320
5321 Mutex::Autolock l(mLock);
5322
5323 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5324 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5325 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5326 int currentMaxCount = mCurrentMaxCount;
5327 mPendingStreams.clear();
5328 mCancelNow = true;
5329 while (mActive) {
5330 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5331 if (res == TIMED_OUT) {
5332 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5333 return;
5334 } else if (res != OK) {
5335 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5336 return;
5337 }
5338 }
5339
5340 //Check whether the prepare thread was able to complete the current
5341 //stream. In case work is still pending emplace it along with the rest
5342 //of the streams in the pending list.
5343 if (currentStream != nullptr) {
5344 if (!mCurrentPrepareComplete) {
5345 pendingStreams.emplace(currentMaxCount, currentStream);
5346 }
5347 }
5348
5349 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5350 for (const auto& it : mPendingStreams) {
5351 it.second->cancelPrepare();
5352 }
5353}
5354
5355status_t Camera3Device::PreparerThread::resume() {
5356 ATRACE_CALL();
5357 status_t res;
5358
5359 Mutex::Autolock l(mLock);
5360 sp<NotificationListener> listener = mListener.promote();
5361
5362 if (mActive) {
5363 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5364 return NO_INIT;
5365 }
5366
5367 auto it = mPendingStreams.begin();
5368 for (; it != mPendingStreams.end();) {
5369 res = it->second->startPrepare(it->first);
5370 if (res == OK) {
5371 if (listener != NULL) {
5372 listener->notifyPrepared(it->second->getId());
5373 }
5374 it = mPendingStreams.erase(it);
5375 } else if (res != NOT_ENOUGH_DATA) {
5376 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5377 res, strerror(-res));
5378 it = mPendingStreams.erase(it);
5379 } else {
5380 it++;
5381 }
5382 }
5383
5384 if (mPendingStreams.empty()) {
5385 return OK;
5386 }
5387
5388 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5389 if (res != OK) {
5390 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5391 __FUNCTION__, res, strerror(-res));
5392 return res;
5393 }
5394 mCancelNow = false;
5395 mActive = true;
5396 ALOGV("%s: Preparer stream started", __FUNCTION__);
5397
5398 return OK;
5399}
5400
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005401status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005402 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005403 Mutex::Autolock l(mLock);
5404
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005405 for (const auto& it : mPendingStreams) {
5406 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005407 }
5408 mPendingStreams.clear();
5409 mCancelNow = true;
5410
5411 return OK;
5412}
5413
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005414void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005415 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005416 Mutex::Autolock l(mLock);
5417 mListener = listener;
5418}
5419
5420bool Camera3Device::PreparerThread::threadLoop() {
5421 status_t res;
5422 {
5423 Mutex::Autolock l(mLock);
5424 if (mCurrentStream == nullptr) {
5425 // End thread if done with work
5426 if (mPendingStreams.empty()) {
5427 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5428 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5429 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5430 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005431 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005432 return false;
5433 }
5434
5435 // Get next stream to prepare
5436 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005437 mCurrentStream = it->second;
5438 mCurrentMaxCount = it->first;
5439 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005440 mPendingStreams.erase(it);
5441 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5442 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5443 } else if (mCancelNow) {
5444 mCurrentStream->cancelPrepare();
5445 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5446 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5447 mCurrentStream.clear();
5448 mCancelNow = false;
5449 return true;
5450 }
5451 }
5452
5453 res = mCurrentStream->prepareNextBuffer();
5454 if (res == NOT_ENOUGH_DATA) return true;
5455 if (res != OK) {
5456 // Something bad happened; try to recover by cancelling prepare and
5457 // signalling listener anyway
5458 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5459 mCurrentStream->getId(), res, strerror(-res));
5460 mCurrentStream->cancelPrepare();
5461 }
5462
5463 // This stream has finished, notify listener
5464 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005465 sp<NotificationListener> listener = mListener.promote();
5466 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005467 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5468 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005469 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005470 }
5471
5472 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5473 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005474 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005475
5476 return true;
5477}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005478
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005479/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005480 * Static callback forwarding methods from HAL to instance
5481 */
5482
5483void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5484 const camera3_capture_result *result) {
5485 Camera3Device *d =
5486 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07005487
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005488 d->processCaptureResult(result);
5489}
5490
5491void Camera3Device::sNotify(const camera3_callback_ops *cb,
5492 const camera3_notify_msg *msg) {
5493 Camera3Device *d =
5494 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5495 d->notify(msg);
5496}
5497
5498}; // namespace android