blob: a980cde235be360866b4d0396dc7ad0ce0a1c688 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070055#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080059using namespace android::hardware::camera;
60using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62namespace android {
63
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080065 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080066 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070067 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070069 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070070 mUsePartialResult(false),
71 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070074 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070076 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000077 mListener(NULL),
78 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080079{
80 ATRACE_CALL();
81 camera3_callback_ops::notify = &sNotify;
82 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080083 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084}
85
86Camera3Device::~Camera3Device()
87{
88 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080089 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 disconnect();
91}
92
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080094 return mId;
95}
96
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080097status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
98 ATRACE_CALL();
99 Mutex::Autolock il(mInterfaceLock);
100 Mutex::Autolock l(mLock);
101
102 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
103 if (mStatus != STATUS_UNINITIALIZED) {
104 CLOGE("Already initialized!");
105 return INVALID_OPERATION;
106 }
107 if (manager == nullptr) return INVALID_OPERATION;
108
109 sp<ICameraDeviceSession> session;
110 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800111 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800112 /*out*/ &session);
113 ATRACE_END();
114 if (res != OK) {
115 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
116 return res;
117 }
118
Steven Moreland5ff9c912017-03-09 23:13:00 -0800119 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120 if (res != OK) {
121 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
122 session->close();
123 return res;
124 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800125
Yifan Hongf79b5542017-04-11 14:44:25 -0700126 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700127 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
128 [&queue](const auto& descriptor) {
129 queue = std::make_shared<RequestMetadataQueue>(descriptor);
130 if (!queue->isValid() || queue->availableToWrite() <= 0) {
131 ALOGE("HAL returns empty request metadata fmq, not use it");
132 queue = nullptr;
133 // don't use the queue onwards.
134 }
135 });
136 if (!requestQueueRet.isOk()) {
137 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
138 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700139 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700140 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700141
142 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700143 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700144 [&resQueue](const auto& descriptor) {
145 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
146 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700147 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700148 resQueue = nullptr;
149 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700150 }
151 });
152 if (!resultQueueRet.isOk()) {
153 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
154 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700155 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700156 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700157 IF_ALOGV() {
158 session->interfaceChain([](
159 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
160 ALOGV("Session interface chain:");
161 for (auto iface : interfaceChain) {
162 ALOGV(" %s", iface.c_str());
163 }
164 });
165 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700166
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700167 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000168 std::string providerType;
169 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800170
171 return initializeCommonLocked();
172}
173
174status_t Camera3Device::initializeCommonLocked() {
175
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700176 /** Start up status tracker thread */
177 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800178 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 if (res != OK) {
180 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
181 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800182 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 mStatusTracker.clear();
184 return res;
185 }
186
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700187 /** Register in-flight map to the status tracker */
188 mInFlightStatusId = mStatusTracker->addComponent();
189
Zhijun He125684a2015-12-26 15:07:30 -0800190 /** Create buffer manager */
191 mBufferManager = new Camera3BufferManager();
192
Emilian Peev71c73a22017-03-21 16:35:51 +0000193 mTagMonitor.initialize(mVendorTagId);
194
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700195 /** Start up request queue thread */
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700196 mRequestThread = new RequestThread(this, mStatusTracker, mInterface);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800197 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700199 SET_ERR_L("Unable to start request queue thread: %s (%d)",
200 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800201 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800202 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800203 return res;
204 }
205
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700206 mPreparerThread = new PreparerThread();
207
Ruben Brunk183f0562015-08-12 12:55:02 -0700208 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800209 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700210 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700211 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700212 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800213
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800214 // Measure the clock domain offset between camera and video/hw_composer
215 camera_metadata_entry timestampSource =
216 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
217 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
218 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
219 mTimestampOffset = getMonoToBoottimeOffset();
220 }
221
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700222 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100223 camera_metadata_entry partialResultsCount =
224 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
225 if (partialResultsCount.count > 0) {
226 mNumPartialResults = partialResultsCount.data.i32[0];
227 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700228 }
229
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700230 camera_metadata_entry configs =
231 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
232 for (uint32_t i = 0; i < configs.count; i += 4) {
233 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
234 configs.data.i32[i + 3] ==
235 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
236 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
237 configs.data.i32[i + 2]));
238 }
239 }
240
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800241 return OK;
242}
243
244status_t Camera3Device::disconnect() {
245 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700248 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800249
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700251 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700252 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 {
254 Mutex::Autolock l(mLock);
255 if (mStatus == STATUS_UNINITIALIZED) return res;
256
257 if (mStatus == STATUS_ACTIVE ||
258 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
259 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700260 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700264 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700266 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
267 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 // Continue to close device even in case of error
269 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800271 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 if (mStatus == STATUS_ERROR) {
274 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700275 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700276
277 if (mStatusTracker != NULL) {
278 mStatusTracker->requestExit();
279 }
280
281 if (mRequestThread != NULL) {
282 mRequestThread->requestExit();
283 }
284
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700285 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
286 for (size_t i = 0; i < mOutputStreams.size(); i++) {
287 streams.push_back(mOutputStreams[i]);
288 }
289 if (mInputStream != nullptr) {
290 streams.push_back(mInputStream);
291 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292 }
293
294 // Joining done without holding mLock, otherwise deadlocks may ensue
295 // as the threads try to access parent state
296 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
297 // HAL may be in a bad state, so waiting for request thread
298 // (which may be stuck in the HAL processCaptureRequest call)
299 // could be dangerous.
300 mRequestThread->join();
301 }
302
303 if (mStatusTracker != NULL) {
304 mStatusTracker->join();
305 }
306
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800307 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 {
309 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800312 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700313 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700315 // Call close without internal mutex held, as the HAL close may need to
316 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800317 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700318
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700319 flushInflightRequests();
320
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700321 {
322 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800323 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700324 mOutputStreams.clear();
325 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700326 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700327 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700328 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700329 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800330
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700331 for (auto& weakStream : streams) {
332 sp<Camera3StreamInterface> stream = weakStream.promote();
333 if (stream != nullptr) {
334 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
335 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
336 }
337 }
338
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700339 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700340 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800341}
342
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700343// For dumping/debugging only -
344// try to acquire a lock a few times, eventually give up to proceed with
345// debug/dump operations
346bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
347 bool gotLock = false;
348 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
349 if (lock.tryLock() == NO_ERROR) {
350 gotLock = true;
351 break;
352 } else {
353 usleep(kDumpSleepDuration);
354 }
355 }
356 return gotLock;
357}
358
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700359Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
360 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100361 const int STREAM_CONFIGURATION_SIZE = 4;
362 const int STREAM_FORMAT_OFFSET = 0;
363 const int STREAM_WIDTH_OFFSET = 1;
364 const int STREAM_HEIGHT_OFFSET = 2;
365 const int STREAM_IS_INPUT_OFFSET = 3;
366 camera_metadata_ro_entry_t availableStreamConfigs =
367 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
368 if (availableStreamConfigs.count == 0 ||
369 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
370 return Size(0, 0);
371 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700372
Emilian Peev08dd2452017-04-06 16:55:14 +0100373 // Get max jpeg size (area-wise).
374 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
375 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
376 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
377 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
378 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
379 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
380 && format == HAL_PIXEL_FORMAT_BLOB &&
381 (width * height > maxJpegWidth * maxJpegHeight)) {
382 maxJpegWidth = width;
383 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700384 }
385 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100386
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700387 return Size(maxJpegWidth, maxJpegHeight);
388}
389
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800390nsecs_t Camera3Device::getMonoToBoottimeOffset() {
391 // try three times to get the clock offset, choose the one
392 // with the minimum gap in measurements.
393 const int tries = 3;
394 nsecs_t bestGap, measured;
395 for (int i = 0; i < tries; ++i) {
396 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
397 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
398 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
399 const nsecs_t gap = tmono2 - tmono;
400 if (i == 0 || gap < bestGap) {
401 bestGap = gap;
402 measured = tbase - ((tmono + tmono2) >> 1);
403 }
404 }
405 return measured;
406}
407
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800408hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
409 int frameworkFormat) {
410 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
411}
412
413DataspaceFlags Camera3Device::mapToHidlDataspace(
414 android_dataspace dataSpace) {
415 return dataSpace;
416}
417
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700418BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100419 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700420 return usage;
421}
422
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800423StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
424 switch (rotation) {
425 case CAMERA3_STREAM_ROTATION_0:
426 return StreamRotation::ROTATION_0;
427 case CAMERA3_STREAM_ROTATION_90:
428 return StreamRotation::ROTATION_90;
429 case CAMERA3_STREAM_ROTATION_180:
430 return StreamRotation::ROTATION_180;
431 case CAMERA3_STREAM_ROTATION_270:
432 return StreamRotation::ROTATION_270;
433 }
434 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
435 return StreamRotation::ROTATION_0;
436}
437
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800438status_t Camera3Device::mapToStreamConfigurationMode(
439 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
440 if (mode == nullptr) return BAD_VALUE;
441 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
442 switch(operationMode) {
443 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
444 *mode = StreamConfigurationMode::NORMAL_MODE;
445 break;
446 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
447 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
448 break;
449 default:
450 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
451 return BAD_VALUE;
452 }
453 } else {
454 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800455 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800456 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800457}
458
459camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
460 switch (status) {
461 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
462 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
463 }
464 return CAMERA3_BUFFER_STATUS_ERROR;
465}
466
467int Camera3Device::mapToFrameworkFormat(
468 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
469 return static_cast<uint32_t>(pixelFormat);
470}
471
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700472android_dataspace Camera3Device::mapToFrameworkDataspace(
473 DataspaceFlags dataSpace) {
474 return static_cast<android_dataspace>(dataSpace);
475}
476
Emilian Peev050f5dc2017-05-18 14:43:56 +0100477uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700478 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700479 return usage;
480}
481
Emilian Peev050f5dc2017-05-18 14:43:56 +0100482uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700483 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700484 return usage;
485}
486
Zhijun Hef7da0962014-04-24 13:27:56 -0700487ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700488 // Get max jpeg size (area-wise).
489 Size maxJpegResolution = getMaxJpegResolution();
490 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800491 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
492 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700493 return BAD_VALUE;
494 }
495
Zhijun Hef7da0962014-04-24 13:27:56 -0700496 // Get max jpeg buffer size
497 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700498 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
499 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800500 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
501 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700502 return BAD_VALUE;
503 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700504 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800505 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700506
507 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700508 float scaleFactor = ((float) (width * height)) /
509 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800510 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
511 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700512 if (jpegBufferSize > maxJpegBufferSize) {
513 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700514 }
515
516 return jpegBufferSize;
517}
518
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700519ssize_t Camera3Device::getPointCloudBufferSize() const {
520 const int FLOATS_PER_POINT=4;
521 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
522 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800523 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
524 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700525 return BAD_VALUE;
526 }
527 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
528 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
529 return maxBytesForPointCloud;
530}
531
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800532ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800533 const int PER_CONFIGURATION_SIZE = 3;
534 const int WIDTH_OFFSET = 0;
535 const int HEIGHT_OFFSET = 1;
536 const int SIZE_OFFSET = 2;
537 camera_metadata_ro_entry rawOpaqueSizes =
538 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800539 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800540 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800541 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
542 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800543 return BAD_VALUE;
544 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700545
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800546 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
547 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
548 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
549 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
550 }
551 }
552
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800553 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
554 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800555 return BAD_VALUE;
556}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700557
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800558status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
559 ATRACE_CALL();
560 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700561
562 // Try to lock, but continue in case of failure (to avoid blocking in
563 // deadlocks)
564 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
565 bool gotLock = tryLockSpinRightRound(mLock);
566
567 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800568 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
569 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700570 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800571 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
572 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700573
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800574 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700575
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800576 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700577 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800578 int n = args.size();
579 for (int i = 0; i < n; i++) {
580 if (args[i] == templatesOption) {
581 dumpTemplates = true;
582 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700583 if (args[i] == monitorOption) {
584 if (i + 1 < n) {
585 String8 monitorTags = String8(args[i + 1]);
586 if (monitorTags == "off") {
587 mTagMonitor.disableMonitoring();
588 } else {
589 mTagMonitor.parseTagsToMonitor(monitorTags);
590 }
591 } else {
592 mTagMonitor.disableMonitoring();
593 }
594 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800595 }
596
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800598
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800599 const char *status =
600 mStatus == STATUS_ERROR ? "ERROR" :
601 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
603 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 mStatus == STATUS_ACTIVE ? "ACTIVE" :
605 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700606
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800607 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700608 if (mStatus == STATUS_ERROR) {
609 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
610 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800612 const char *mode =
613 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
614 mOperatingMode == static_cast<int>(
615 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
616 "CUSTOM";
617 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800618
619 if (mInputStream != NULL) {
620 write(fd, lines.string(), lines.size());
621 mInputStream->dump(fd, args);
622 } else {
623 lines.appendFormat(" No input stream.\n");
624 write(fd, lines.string(), lines.size());
625 }
626 for (size_t i = 0; i < mOutputStreams.size(); i++) {
627 mOutputStreams[i]->dump(fd,args);
628 }
629
Zhijun He431503c2016-03-07 17:30:16 -0800630 if (mBufferManager != NULL) {
631 lines = String8(" Camera3 Buffer Manager:\n");
632 write(fd, lines.string(), lines.size());
633 mBufferManager->dump(fd, args);
634 }
Zhijun He125684a2015-12-26 15:07:30 -0800635
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700636 lines = String8(" In-flight requests:\n");
637 if (mInFlightMap.size() == 0) {
638 lines.append(" None\n");
639 } else {
640 for (size_t i = 0; i < mInFlightMap.size(); i++) {
641 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700642 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700643 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800644 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700645 r.numBuffersLeft);
646 }
647 }
648 write(fd, lines.string(), lines.size());
649
Shuzhen Wang686f6442017-06-20 16:16:04 -0700650 if (mRequestThread != NULL) {
651 mRequestThread->dumpCaptureRequestLatency(fd,
652 " ProcessCaptureRequest latency histogram:");
653 }
654
Igor Murashkin1e479c02013-09-06 16:55:14 -0700655 {
656 lines = String8(" Last request sent:\n");
657 write(fd, lines.string(), lines.size());
658
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700659 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700660 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
661 }
662
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800663 if (dumpTemplates) {
664 const char *templateNames[] = {
665 "TEMPLATE_PREVIEW",
666 "TEMPLATE_STILL_CAPTURE",
667 "TEMPLATE_VIDEO_RECORD",
668 "TEMPLATE_VIDEO_SNAPSHOT",
669 "TEMPLATE_ZERO_SHUTTER_LAG",
670 "TEMPLATE_MANUAL"
671 };
672
673 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800674 camera_metadata_t *templateRequest = nullptr;
675 mInterface->constructDefaultRequestSettings(
676 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800677 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800678 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800679 lines.append(" Not supported\n");
680 write(fd, lines.string(), lines.size());
681 } else {
682 write(fd, lines.string(), lines.size());
683 dump_indented_camera_metadata(templateRequest,
684 fd, /*verbosity*/2, /*indentation*/8);
685 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800686 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800687 }
688 }
689
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700690 mTagMonitor.dumpMonitoredMetadata(fd);
691
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800692 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800693 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800695 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800697
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 if (gotLock) mLock.unlock();
699 if (gotInterfaceLock) mInterfaceLock.unlock();
700
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800701 return OK;
702}
703
704const CameraMetadata& Camera3Device::info() const {
705 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800706 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
707 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700708 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709 mStatus == STATUS_ERROR ?
710 "when in error state" : "before init");
711 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800712 return mDeviceInfo;
713}
714
Jianing Wei90e59c92014-03-12 18:29:36 -0700715status_t Camera3Device::checkStatusOkToCaptureLocked() {
716 switch (mStatus) {
717 case STATUS_ERROR:
718 CLOGE("Device has encountered a serious error");
719 return INVALID_OPERATION;
720 case STATUS_UNINITIALIZED:
721 CLOGE("Device not initialized");
722 return INVALID_OPERATION;
723 case STATUS_UNCONFIGURED:
724 case STATUS_CONFIGURED:
725 case STATUS_ACTIVE:
726 // OK
727 break;
728 default:
729 SET_ERR_L("Unexpected status: %d", mStatus);
730 return INVALID_OPERATION;
731 }
732 return OK;
733}
734
735status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700736 const List<const CameraMetadata> &metadataList,
737 const std::list<const SurfaceMap> &surfaceMaps,
738 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700739 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700740 if (requestList == NULL) {
741 CLOGE("requestList cannot be NULL.");
742 return BAD_VALUE;
743 }
744
Jianing Weicb0652e2014-03-12 18:29:36 -0700745 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700746 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
747 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
748 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
749 ++metadataIt, ++surfaceMapIt) {
750 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700751 if (newRequest == 0) {
752 CLOGE("Can't create capture request");
753 return BAD_VALUE;
754 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700755
Shuzhen Wang9d066012016-09-30 11:30:20 -0700756 newRequest->mRepeating = repeating;
757
Jianing Weicb0652e2014-03-12 18:29:36 -0700758 // Setup burst Id and request Id
759 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700760 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
761 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700762 CLOGE("RequestID entry exists; but must not be empty in metadata");
763 return BAD_VALUE;
764 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700765 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700766 } else {
767 CLOGE("RequestID does not exist in metadata");
768 return BAD_VALUE;
769 }
770
Jianing Wei90e59c92014-03-12 18:29:36 -0700771 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700772
773 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700774 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700775 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
776 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
777 return BAD_VALUE;
778 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700779
780 // Setup batch size if this is a high speed video recording request.
781 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
782 auto firstRequest = requestList->begin();
783 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
784 if (outputStream->isVideoStream()) {
785 (*firstRequest)->mBatchSize = requestList->size();
786 break;
787 }
788 }
789 }
790
Jianing Wei90e59c92014-03-12 18:29:36 -0700791 return OK;
792}
793
Jianing Weicb0652e2014-03-12 18:29:36 -0700794status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800795 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800796
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700797 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700798 std::list<const SurfaceMap> surfaceMaps;
799 convertToRequestList(requests, surfaceMaps, request);
800
801 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
802}
803
804void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
805 std::list<const SurfaceMap>& surfaceMaps,
806 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700807 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700808
809 SurfaceMap surfaceMap;
810 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
811 // With no surface list passed in, stream and surface will have 1-to-1
812 // mapping. So the surface index is 0 for each stream in the surfaceMap.
813 for (size_t i = 0; i < streams.count; i++) {
814 surfaceMap[streams.data.i32[i]].push_back(0);
815 }
816 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817}
818
Jianing Wei90e59c92014-03-12 18:29:36 -0700819status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700820 const List<const CameraMetadata> &requests,
821 const std::list<const SurfaceMap> &surfaceMaps,
822 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700823 /*out*/
824 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700825 ATRACE_CALL();
826 Mutex::Autolock il(mInterfaceLock);
827 Mutex::Autolock l(mLock);
828
829 status_t res = checkStatusOkToCaptureLocked();
830 if (res != OK) {
831 // error logged by previous call
832 return res;
833 }
834
835 RequestList requestList;
836
Shuzhen Wang0129d522016-10-30 22:43:41 -0700837 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
838 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700839 if (res != OK) {
840 // error logged by previous call
841 return res;
842 }
843
844 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700845 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700846 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700847 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700848 }
849
850 if (res == OK) {
851 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
852 if (res != OK) {
853 SET_ERR_L("Can't transition to active in %f seconds!",
854 kActiveTimeout/1e9);
855 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800856 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700857 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700858 } else {
859 CLOGE("Cannot queue request. Impossible.");
860 return BAD_VALUE;
861 }
862
863 return res;
864}
865
Yifan Honga640c5a2017-04-12 16:30:31 -0700866// Only one processCaptureResult should be called at a time, so
867// the locks won't block. The locks are present here simply to enforce this.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800868hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800869 const hardware::hidl_vec<
870 hardware::camera::device::V3_2::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700871 // Ideally we should grab mLock, but that can lead to deadlock, and
872 // it's not super important to get up to date value of mStatus for this
873 // warning print, hence skipping the lock here
874 if (mStatus == STATUS_ERROR) {
875 // Per API contract, HAL should act as closed after device error
876 // But mStatus can be set to error by framework as well, so just log
877 // a warning here.
878 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700879 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700880
881 if (mProcessCaptureResultLock.tryLock() != OK) {
882 // This should never happen; it indicates a wrong client implementation
883 // that doesn't follow the contract. But, we can be tolerant here.
884 ALOGE("%s: callback overlapped! waiting 1s...",
885 __FUNCTION__);
886 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
887 ALOGE("%s: cannot acquire lock in 1s, dropping results",
888 __FUNCTION__);
889 // really don't know what to do, so bail out.
890 return hardware::Void();
891 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800892 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700893 for (const auto& result : results) {
894 processOneCaptureResultLocked(result);
895 }
896 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800897 return hardware::Void();
898}
899
Yifan Honga640c5a2017-04-12 16:30:31 -0700900void Camera3Device::processOneCaptureResultLocked(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800901 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800902 camera3_capture_result r;
903 status_t res;
904 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700905
906 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
907 if (result.fmqResultSize > 0) {
908 resultMetadata.resize(result.fmqResultSize);
909 if (mResultMetadataQueue == nullptr) {
910 return; // logged in initialize()
911 }
912 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
913 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
914 __FUNCTION__, result.frameNumber, result.fmqResultSize);
915 return;
916 }
917 } else {
918 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
919 result.result.size());
920 }
921
922 if (resultMetadata.size() != 0) {
923 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
924 size_t expected_metadata_size = resultMetadata.size();
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800925 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
926 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
927 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800928 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800929 }
930 } else {
931 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800932 }
933
934 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
935 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
936 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
937 auto& bDst = outputBuffers[i];
938 const StreamBuffer &bSrc = result.outputBuffers[i];
939
940 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100941 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800942 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
943 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800944 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800945 }
946 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
947
948 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800949 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800950 if (res != OK) {
951 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
952 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800953 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800954 }
955 bDst.buffer = buffer;
956 bDst.status = mapHidlBufferStatus(bSrc.status);
957 bDst.acquire_fence = -1;
958 if (bSrc.releaseFence == nullptr) {
959 bDst.release_fence = -1;
960 } else if (bSrc.releaseFence->numFds == 1) {
961 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
962 } else {
963 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
964 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800965 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800966 }
967 }
968 r.num_output_buffers = outputBuffers.size();
969 r.output_buffers = outputBuffers.data();
970
971 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800972 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800973 r.input_buffer = nullptr;
974 } else {
975 if (mInputStream->getId() != result.inputBuffer.streamId) {
976 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
977 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800978 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800979 }
980 inputBuffer.stream = mInputStream->asHalStream();
981 buffer_handle_t *buffer;
982 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
983 &buffer);
984 if (res != OK) {
985 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
986 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800987 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800988 }
989 inputBuffer.buffer = buffer;
990 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
991 inputBuffer.acquire_fence = -1;
992 if (result.inputBuffer.releaseFence == nullptr) {
993 inputBuffer.release_fence = -1;
994 } else if (result.inputBuffer.releaseFence->numFds == 1) {
995 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
996 } else {
997 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
998 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800999 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001000 }
1001 r.input_buffer = &inputBuffer;
1002 }
1003
1004 r.partial_result = result.partialResult;
1005
1006 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001007}
1008
1009hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001010 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001011 // Ideally we should grab mLock, but that can lead to deadlock, and
1012 // it's not super important to get up to date value of mStatus for this
1013 // warning print, hence skipping the lock here
1014 if (mStatus == STATUS_ERROR) {
1015 // Per API contract, HAL should act as closed after device error
1016 // But mStatus can be set to error by framework as well, so just log
1017 // a warning here.
1018 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001019 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001020
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001021 for (const auto& msg : msgs) {
1022 notify(msg);
1023 }
1024 return hardware::Void();
1025}
1026
1027void Camera3Device::notify(
1028 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1029
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001030 camera3_notify_msg m;
1031 switch (msg.type) {
1032 case MsgType::ERROR:
1033 m.type = CAMERA3_MSG_ERROR;
1034 m.message.error.frame_number = msg.msg.error.frameNumber;
1035 if (msg.msg.error.errorStreamId >= 0) {
1036 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001037 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001038 ALOGE("%s: Frame %d: Invalid error stream id %d",
1039 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001040 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001041 }
1042 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1043 } else {
1044 m.message.error.error_stream = nullptr;
1045 }
1046 switch (msg.msg.error.errorCode) {
1047 case ErrorCode::ERROR_DEVICE:
1048 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1049 break;
1050 case ErrorCode::ERROR_REQUEST:
1051 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1052 break;
1053 case ErrorCode::ERROR_RESULT:
1054 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1055 break;
1056 case ErrorCode::ERROR_BUFFER:
1057 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1058 break;
1059 }
1060 break;
1061 case MsgType::SHUTTER:
1062 m.type = CAMERA3_MSG_SHUTTER;
1063 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1064 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1065 break;
1066 }
1067 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001068}
1069
Jianing Weicb0652e2014-03-12 18:29:36 -07001070status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001071 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001072 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001073 ATRACE_CALL();
1074
Shuzhen Wang0129d522016-10-30 22:43:41 -07001075 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001076}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001077
Jianing Weicb0652e2014-03-12 18:29:36 -07001078status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1079 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001081
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001082 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001083 std::list<const SurfaceMap> surfaceMaps;
1084 convertToRequestList(requests, surfaceMaps, request);
1085
1086 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1087 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088}
1089
Jianing Weicb0652e2014-03-12 18:29:36 -07001090status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001091 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001092 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001093 ATRACE_CALL();
1094
Shuzhen Wang0129d522016-10-30 22:43:41 -07001095 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001096}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097
1098sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001099 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001100 status_t res;
1101
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001102 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001103 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1104 // so unilaterally select normal operating mode.
1105 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001106 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001107 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001108 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001109 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001110 } else if (mStatus == STATUS_UNCONFIGURED) {
1111 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001112 CLOGE("No streams configured");
1113 return NULL;
1114 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 }
1116
Shuzhen Wang0129d522016-10-30 22:43:41 -07001117 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119}
1120
Jianing Weicb0652e2014-03-12 18:29:36 -07001121status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001122 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001123 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001125
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126 switch (mStatus) {
1127 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001128 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129 return INVALID_OPERATION;
1130 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001132 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001133 case STATUS_UNCONFIGURED:
1134 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 case STATUS_ACTIVE:
1136 // OK
1137 break;
1138 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001139 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001140 return INVALID_OPERATION;
1141 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001142 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001143
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001144 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001145}
1146
1147status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1148 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001149 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001151 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001152}
1153
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001154status_t Camera3Device::createInputStream(
1155 uint32_t width, uint32_t height, int format, int *id) {
1156 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001157 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001158 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001159 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001160 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1161 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001162
1163 status_t res;
1164 bool wasActive = false;
1165
1166 switch (mStatus) {
1167 case STATUS_ERROR:
1168 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1169 return INVALID_OPERATION;
1170 case STATUS_UNINITIALIZED:
1171 ALOGE("%s: Device not initialized", __FUNCTION__);
1172 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001173 case STATUS_UNCONFIGURED:
1174 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001175 // OK
1176 break;
1177 case STATUS_ACTIVE:
1178 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001179 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001180 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001181 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001182 return res;
1183 }
1184 wasActive = true;
1185 break;
1186 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001187 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001188 return INVALID_OPERATION;
1189 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001190 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001191
1192 if (mInputStream != 0) {
1193 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1194 return INVALID_OPERATION;
1195 }
1196
1197 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1198 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001200
1201 mInputStream = newStream;
1202
1203 *id = mNextStreamId++;
1204
1205 // Continue captures if active at start
1206 if (wasActive) {
1207 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001208 // Reuse current operating mode for new stream config
1209 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001210 if (res != OK) {
1211 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1212 __FUNCTION__, mNextStreamId, strerror(-res), res);
1213 return res;
1214 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001215 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001216 }
1217
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001218 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001219 return OK;
1220}
1221
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001222status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001223 uint32_t width, uint32_t height, int format,
1224 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Emilian Peev050f5dc2017-05-18 14:43:56 +01001225 int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001226 ATRACE_CALL();
1227
1228 if (consumer == nullptr) {
1229 ALOGE("%s: consumer must not be null", __FUNCTION__);
1230 return BAD_VALUE;
1231 }
1232
1233 std::vector<sp<Surface>> consumers;
1234 consumers.push_back(consumer);
1235
1236 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001237 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001238}
1239
1240status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1241 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1242 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Emilian Peev050f5dc2017-05-18 14:43:56 +01001243 int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001244 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001246 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001248 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Emilian Peev050f5dc2017-05-18 14:43:56 +01001249 " consumer usage %" PRIu64 ", isShared %d", mId.string(), mNextStreamId, width, height, format,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001250 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 status_t res;
1253 bool wasActive = false;
1254
1255 switch (mStatus) {
1256 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001257 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 return INVALID_OPERATION;
1259 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001262 case STATUS_UNCONFIGURED:
1263 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 // OK
1265 break;
1266 case STATUS_ACTIVE:
1267 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001268 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001270 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 return res;
1272 }
1273 wasActive = true;
1274 break;
1275 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001276 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001277 return INVALID_OPERATION;
1278 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280
1281 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001282
Shuzhen Wang0129d522016-10-30 22:43:41 -07001283 if (consumers.size() == 0 && !hasDeferredConsumer) {
1284 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1285 return BAD_VALUE;
1286 }
Zhijun He5d677d12016-05-29 16:52:39 -07001287
Shuzhen Wang0129d522016-10-30 22:43:41 -07001288 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001289 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1290 return BAD_VALUE;
1291 }
1292
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001293 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001294 ssize_t blobBufferSize;
1295 if (dataSpace != HAL_DATASPACE_DEPTH) {
1296 blobBufferSize = getJpegBufferSize(width, height);
1297 if (blobBufferSize <= 0) {
1298 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1299 return BAD_VALUE;
1300 }
1301 } else {
1302 blobBufferSize = getPointCloudBufferSize();
1303 if (blobBufferSize <= 0) {
1304 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1305 return BAD_VALUE;
1306 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001307 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001308 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001309 width, height, blobBufferSize, format, dataSpace, rotation,
1310 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001311 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1312 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1313 if (rawOpaqueBufferSize <= 0) {
1314 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1315 return BAD_VALUE;
1316 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001317 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001318 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1319 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001320 } else if (isShared) {
1321 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1322 width, height, format, consumerUsage, dataSpace, rotation,
1323 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001324 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001325 newStream = new Camera3OutputStream(mNextStreamId,
1326 width, height, format, consumerUsage, dataSpace, rotation,
1327 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001329 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001330 width, height, format, dataSpace, rotation,
1331 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001332 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334
Emilian Peev08dd2452017-04-06 16:55:14 +01001335 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001336
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001337 res = mOutputStreams.add(mNextStreamId, newStream);
1338 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001339 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001340 return res;
1341 }
1342
1343 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001344 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345
1346 // Continue captures if active at start
1347 if (wasActive) {
1348 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001349 // Reuse current operating mode for new stream config
1350 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001352 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1353 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001354 return res;
1355 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001356 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001358 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001359 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001360}
1361
Emilian Peev710c1422017-08-30 11:19:38 +01001362status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001363 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001364 if (nullptr == streamInfo) {
1365 return BAD_VALUE;
1366 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001367 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001368 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001369
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 switch (mStatus) {
1371 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001372 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373 return INVALID_OPERATION;
1374 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001375 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 case STATUS_UNCONFIGURED:
1378 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001379 case STATUS_ACTIVE:
1380 // OK
1381 break;
1382 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001383 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 return INVALID_OPERATION;
1385 }
1386
1387 ssize_t idx = mOutputStreams.indexOfKey(id);
1388 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 return idx;
1391 }
1392
Emilian Peev710c1422017-08-30 11:19:38 +01001393 streamInfo->width = mOutputStreams[idx]->getWidth();
1394 streamInfo->height = mOutputStreams[idx]->getHeight();
1395 streamInfo->format = mOutputStreams[idx]->getFormat();
1396 streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace();
1397 streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden();
1398 streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat();
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07001399 streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden();
1400 streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001401 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402}
1403
1404status_t Camera3Device::setStreamTransform(int id,
1405 int transform) {
1406 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001407 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001408 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001409
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001410 switch (mStatus) {
1411 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001412 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001413 return INVALID_OPERATION;
1414 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001415 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001417 case STATUS_UNCONFIGURED:
1418 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419 case STATUS_ACTIVE:
1420 // OK
1421 break;
1422 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001423 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001424 return INVALID_OPERATION;
1425 }
1426
1427 ssize_t idx = mOutputStreams.indexOfKey(id);
1428 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001429 CLOGE("Stream %d does not exist",
1430 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001431 return BAD_VALUE;
1432 }
1433
1434 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001435}
1436
1437status_t Camera3Device::deleteStream(int id) {
1438 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001439 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 Mutex::Autolock l(mLock);
1441 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001442
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001443 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 // CameraDevice semantics require device to already be idle before
1446 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001447 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001448 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001449 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001450 }
1451
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001452 if (mStatus == STATUS_ERROR) {
1453 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1454 __FUNCTION__, mId.string());
1455 return -EBUSY;
1456 }
1457
Igor Murashkin2fba5842013-04-22 14:03:54 -07001458 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001459 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460 if (mInputStream != NULL && id == mInputStream->getId()) {
1461 deletedStream = mInputStream;
1462 mInputStream.clear();
1463 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001464 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return BAD_VALUE;
1467 }
Zhijun He5f446352014-01-22 09:49:33 -08001468 }
1469
1470 // Delete output stream or the output part of a bi-directional stream.
1471 if (outputStreamIdx != NAME_NOT_FOUND) {
1472 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473 mOutputStreams.removeItem(id);
1474 }
1475
1476 // Free up the stream endpoint so that it can be used by some other stream
1477 res = deletedStream->disconnect();
1478 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001479 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001480 // fall through since we want to still list the stream as deleted.
1481 }
1482 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001483 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484
1485 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001486}
1487
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001488status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001489 ATRACE_CALL();
1490 ALOGV("%s: E", __FUNCTION__);
1491
1492 Mutex::Autolock il(mInterfaceLock);
1493 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001494
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001495 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001496}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001498status_t Camera3Device::getInputBufferProducer(
1499 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001500 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001501 Mutex::Autolock il(mInterfaceLock);
1502 Mutex::Autolock l(mLock);
1503
1504 if (producer == NULL) {
1505 return BAD_VALUE;
1506 } else if (mInputStream == NULL) {
1507 return INVALID_OPERATION;
1508 }
1509
1510 return mInputStream->getInputBufferProducer(producer);
1511}
1512
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001513status_t Camera3Device::createDefaultRequest(int templateId,
1514 CameraMetadata *request) {
1515 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001516 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001517
1518 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1519 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1520 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1521 return BAD_VALUE;
1522 }
1523
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001524 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001526 {
1527 Mutex::Autolock l(mLock);
1528 switch (mStatus) {
1529 case STATUS_ERROR:
1530 CLOGE("Device has encountered a serious error");
1531 return INVALID_OPERATION;
1532 case STATUS_UNINITIALIZED:
1533 CLOGE("Device is not initialized!");
1534 return INVALID_OPERATION;
1535 case STATUS_UNCONFIGURED:
1536 case STATUS_CONFIGURED:
1537 case STATUS_ACTIVE:
1538 // OK
1539 break;
1540 default:
1541 SET_ERR_L("Unexpected status: %d", mStatus);
1542 return INVALID_OPERATION;
1543 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001544
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001545 if (!mRequestTemplateCache[templateId].isEmpty()) {
1546 *request = mRequestTemplateCache[templateId];
1547 return OK;
1548 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001549 }
1550
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001551 camera_metadata_t *rawRequest;
1552 status_t res = mInterface->constructDefaultRequestSettings(
1553 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001554
1555 {
1556 Mutex::Autolock l(mLock);
1557 if (res == BAD_VALUE) {
1558 ALOGI("%s: template %d is not supported on this camera device",
1559 __FUNCTION__, templateId);
1560 return res;
1561 } else if (res != OK) {
1562 CLOGE("Unable to construct request template %d: %s (%d)",
1563 templateId, strerror(-res), res);
1564 return res;
1565 }
1566
1567 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1568 mRequestTemplateCache[templateId].acquire(rawRequest);
1569
1570 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001571 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001572 return OK;
1573}
1574
1575status_t Camera3Device::waitUntilDrained() {
1576 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001578 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001580
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001581 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001582}
1583
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001584status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001585 switch (mStatus) {
1586 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001587 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 ALOGV("%s: Already idle", __FUNCTION__);
1589 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001590 case STATUS_CONFIGURED:
1591 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592 case STATUS_ERROR:
1593 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001594 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001595 break;
1596 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001597 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598 return INVALID_OPERATION;
1599 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001600 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1601 maxExpectedDuration);
1602 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001603 if (res != OK) {
1604 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1605 res);
1606 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001607 return res;
1608}
1609
Ruben Brunk183f0562015-08-12 12:55:02 -07001610
1611void Camera3Device::internalUpdateStatusLocked(Status status) {
1612 mStatus = status;
1613 mRecentStatusUpdates.add(mStatus);
1614 mStatusChanged.broadcast();
1615}
1616
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001618status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001619 mRequestThread->setPaused(true);
1620 mPauseStateNotify = true;
1621
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001622 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1623 maxExpectedDuration);
1624 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001625 if (res != OK) {
1626 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001627 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 }
1629
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001630 return res;
1631}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633// Resume after internalPauseAndWaitLocked
1634status_t Camera3Device::internalResumeLocked() {
1635 status_t res;
1636
1637 mRequestThread->setPaused(false);
1638
1639 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1640 if (res != OK) {
1641 SET_ERR_L("Can't transition to active in %f seconds!",
1642 kActiveTimeout/1e9);
1643 }
1644 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001645 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001646}
1647
Ruben Brunk183f0562015-08-12 12:55:02 -07001648status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001649 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001650
1651 size_t startIndex = 0;
1652 if (mStatusWaiters == 0) {
1653 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1654 // this status list
1655 mRecentStatusUpdates.clear();
1656 } else {
1657 // If other threads are waiting on updates to this status list, set the position of the
1658 // first element that this list will check rather than clearing the list.
1659 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001660 }
1661
Ruben Brunk183f0562015-08-12 12:55:02 -07001662 mStatusWaiters++;
1663
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001664 bool stateSeen = false;
1665 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001666 if (active == (mStatus == STATUS_ACTIVE)) {
1667 // Desired state is current
1668 break;
1669 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001670
1671 res = mStatusChanged.waitRelative(mLock, timeout);
1672 if (res != OK) break;
1673
Ruben Brunk183f0562015-08-12 12:55:02 -07001674 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1675 // transitions.
1676 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1677 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1678 __FUNCTION__);
1679
1680 // Encountered desired state since we began waiting
1681 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001682 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1683 stateSeen = true;
1684 break;
1685 }
1686 }
1687 } while (!stateSeen);
1688
Ruben Brunk183f0562015-08-12 12:55:02 -07001689 mStatusWaiters--;
1690
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001691 return res;
1692}
1693
1694
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001695status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001696 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001697 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001698
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001699 if (listener != NULL && mListener != NULL) {
1700 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1701 }
1702 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001703 mRequestThread->setNotificationListener(listener);
1704 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001705
1706 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707}
1708
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001709bool Camera3Device::willNotify3A() {
1710 return false;
1711}
1712
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001713status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001714 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001715 status_t res;
1716 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001717
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001718 while (mResultQueue.empty()) {
1719 res = mResultSignal.waitRelative(mOutputLock, timeout);
1720 if (res == TIMED_OUT) {
1721 return res;
1722 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001723 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1724 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001725 return res;
1726 }
1727 }
1728 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001729}
1730
Jianing Weicb0652e2014-03-12 18:29:36 -07001731status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001732 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001733 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001734
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001735 if (mResultQueue.empty()) {
1736 return NOT_ENOUGH_DATA;
1737 }
1738
Jianing Weicb0652e2014-03-12 18:29:36 -07001739 if (frame == NULL) {
1740 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1741 return BAD_VALUE;
1742 }
1743
1744 CaptureResult &result = *(mResultQueue.begin());
1745 frame->mResultExtras = result.mResultExtras;
1746 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001747 mResultQueue.erase(mResultQueue.begin());
1748
1749 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001750}
1751
1752status_t Camera3Device::triggerAutofocus(uint32_t id) {
1753 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001754 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001755
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001756 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1757 // Mix-in this trigger into the next request and only the next request.
1758 RequestTrigger trigger[] = {
1759 {
1760 ANDROID_CONTROL_AF_TRIGGER,
1761 ANDROID_CONTROL_AF_TRIGGER_START
1762 },
1763 {
1764 ANDROID_CONTROL_AF_TRIGGER_ID,
1765 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001766 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001767 };
1768
1769 return mRequestThread->queueTrigger(trigger,
1770 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001771}
1772
1773status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1774 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001775 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001776
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001777 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1778 // Mix-in this trigger into the next request and only the next request.
1779 RequestTrigger trigger[] = {
1780 {
1781 ANDROID_CONTROL_AF_TRIGGER,
1782 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1783 },
1784 {
1785 ANDROID_CONTROL_AF_TRIGGER_ID,
1786 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001787 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001788 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001789
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001790 return mRequestThread->queueTrigger(trigger,
1791 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001792}
1793
1794status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1795 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001796 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001797
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001798 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1799 // Mix-in this trigger into the next request and only the next request.
1800 RequestTrigger trigger[] = {
1801 {
1802 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1803 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1804 },
1805 {
1806 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1807 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001808 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001809 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001810
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001811 return mRequestThread->queueTrigger(trigger,
1812 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001813}
1814
Jianing Weicb0652e2014-03-12 18:29:36 -07001815status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001816 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001817 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001818 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001819
Zhijun He7ef20392014-04-21 16:04:17 -07001820 {
1821 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001822 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001823 }
1824
Emilian Peev08dd2452017-04-06 16:55:14 +01001825 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001826}
1827
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001828status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001829 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1830}
1831
1832status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001833 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001834 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001835 Mutex::Autolock il(mInterfaceLock);
1836 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001837
1838 sp<Camera3StreamInterface> stream;
1839 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1840 if (outputStreamIdx == NAME_NOT_FOUND) {
1841 CLOGE("Stream %d does not exist", streamId);
1842 return BAD_VALUE;
1843 }
1844
1845 stream = mOutputStreams.editValueAt(outputStreamIdx);
1846
1847 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001848 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001849 return BAD_VALUE;
1850 }
1851
1852 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001853 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001854 return BAD_VALUE;
1855 }
1856
Ruben Brunkc78ac262015-08-13 17:58:46 -07001857 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001858}
1859
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001860status_t Camera3Device::tearDown(int streamId) {
1861 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001862 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001863 Mutex::Autolock il(mInterfaceLock);
1864 Mutex::Autolock l(mLock);
1865
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001866 sp<Camera3StreamInterface> stream;
1867 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1868 if (outputStreamIdx == NAME_NOT_FOUND) {
1869 CLOGE("Stream %d does not exist", streamId);
1870 return BAD_VALUE;
1871 }
1872
1873 stream = mOutputStreams.editValueAt(outputStreamIdx);
1874
1875 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1876 CLOGE("Stream %d is a target of a in-progress request", streamId);
1877 return BAD_VALUE;
1878 }
1879
1880 return stream->tearDown();
1881}
1882
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001883status_t Camera3Device::addBufferListenerForStream(int streamId,
1884 wp<Camera3StreamBufferListener> listener) {
1885 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001886 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001887 Mutex::Autolock il(mInterfaceLock);
1888 Mutex::Autolock l(mLock);
1889
1890 sp<Camera3StreamInterface> stream;
1891 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1892 if (outputStreamIdx == NAME_NOT_FOUND) {
1893 CLOGE("Stream %d does not exist", streamId);
1894 return BAD_VALUE;
1895 }
1896
1897 stream = mOutputStreams.editValueAt(outputStreamIdx);
1898 stream->addBufferListener(listener);
1899
1900 return OK;
1901}
1902
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001903/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001904 * Methods called by subclasses
1905 */
1906
1907void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001908 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001909 {
1910 // Need mLock to safely update state and synchronize to current
1911 // state of methods in flight.
1912 Mutex::Autolock l(mLock);
1913 // We can get various system-idle notices from the status tracker
1914 // while starting up. Only care about them if we've actually sent
1915 // in some requests recently.
1916 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1917 return;
1918 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001919 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001920 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001921 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001922
1923 // Skip notifying listener if we're doing some user-transparent
1924 // state changes
1925 if (mPauseStateNotify) return;
1926 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001927
1928 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001929 {
1930 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001931 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 }
1933 if (idle && listener != NULL) {
1934 listener->notifyIdle();
1935 }
1936}
1937
Shuzhen Wang758c2152017-01-10 18:26:18 -08001938status_t Camera3Device::setConsumerSurfaces(int streamId,
1939 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07001940 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001941 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1942 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001943 Mutex::Autolock il(mInterfaceLock);
1944 Mutex::Autolock l(mLock);
1945
Shuzhen Wang758c2152017-01-10 18:26:18 -08001946 if (consumers.size() == 0) {
1947 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001948 return BAD_VALUE;
1949 }
1950
1951 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1952 if (idx == NAME_NOT_FOUND) {
1953 CLOGE("Stream %d is unknown", streamId);
1954 return idx;
1955 }
1956 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08001957 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001958 if (res != OK) {
1959 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1960 return res;
1961 }
1962
Shuzhen Wang0129d522016-10-30 22:43:41 -07001963 if (stream->isConsumerConfigurationDeferred()) {
1964 if (!stream->isConfiguring()) {
1965 CLOGE("Stream %d was already fully configured.", streamId);
1966 return INVALID_OPERATION;
1967 }
Zhijun He5d677d12016-05-29 16:52:39 -07001968
Shuzhen Wang0129d522016-10-30 22:43:41 -07001969 res = stream->finishConfiguration();
1970 if (res != OK) {
1971 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1972 stream->getId(), strerror(-res), res);
1973 return res;
1974 }
Zhijun He5d677d12016-05-29 16:52:39 -07001975 }
1976
1977 return OK;
1978}
1979
Chien-Yu Chen63068522017-10-23 15:59:49 -07001980status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
1981 Mutex::Autolock il(mInterfaceLock);
1982 Mutex::Autolock l(mLock);
1983
1984 int idx = mOutputStreams.indexOfKey(streamId);
1985 if (idx == NAME_NOT_FOUND) {
1986 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
1987 return BAD_VALUE;
1988 }
1989
1990 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx);
1991 return stream->dropBuffers(dropping);
1992}
1993
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001994/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001995 * Camera3Device private methods
1996 */
1997
1998sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001999 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002000 ATRACE_CALL();
2001 status_t res;
2002
2003 sp<CaptureRequest> newRequest = new CaptureRequest;
2004 newRequest->mSettings = request;
2005
2006 camera_metadata_entry_t inputStreams =
2007 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
2008 if (inputStreams.count > 0) {
2009 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002010 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002011 CLOGE("Request references unknown input stream %d",
2012 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 return NULL;
2014 }
2015 // Lazy completion of stream configuration (allocation/registration)
2016 // on first use
2017 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002018 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002019 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002020 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002021 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002022 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002023 return NULL;
2024 }
2025 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002026 // Check if stream is being prepared
2027 if (mInputStream->isPreparing()) {
2028 CLOGE("Request references an input stream that's being prepared!");
2029 return NULL;
2030 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002031
2032 newRequest->mInputStream = mInputStream;
2033 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
2034 }
2035
2036 camera_metadata_entry_t streams =
2037 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2038 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002039 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 return NULL;
2041 }
2042
2043 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002044 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002046 CLOGE("Request references unknown stream %d",
2047 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002048 return NULL;
2049 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002050 sp<Camera3OutputStreamInterface> stream =
2051 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052
Zhijun He5d677d12016-05-29 16:52:39 -07002053 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002054 auto iter = surfaceMap.find(streams.data.i32[i]);
2055 if (iter != surfaceMap.end()) {
2056 const std::vector<size_t>& surfaces = iter->second;
2057 for (const auto& surface : surfaces) {
2058 if (stream->isConsumerConfigurationDeferred(surface)) {
2059 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2060 "due to deferred consumer", stream->getId(), surface);
2061 return NULL;
2062 }
2063 }
2064 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002065 }
2066
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002067 // Lazy completion of stream configuration (allocation/registration)
2068 // on first use
2069 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002070 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002072 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2073 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002074 return NULL;
2075 }
2076 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002077 // Check if stream is being prepared
2078 if (stream->isPreparing()) {
2079 CLOGE("Request references an output stream that's being prepared!");
2080 return NULL;
2081 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082
2083 newRequest->mOutputStreams.push(stream);
2084 }
2085 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002086 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087
2088 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002089}
2090
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002091bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2092 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2093 Size size = mSupportedOpaqueInputSizes[i];
2094 if (size.width == width && size.height == height) {
2095 return true;
2096 }
2097 }
2098
2099 return false;
2100}
2101
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002102void Camera3Device::cancelStreamsConfigurationLocked() {
2103 int res = OK;
2104 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2105 res = mInputStream->cancelConfiguration();
2106 if (res != OK) {
2107 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2108 mInputStream->getId(), strerror(-res), res);
2109 }
2110 }
2111
2112 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2113 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2114 if (outputStream->isConfiguring()) {
2115 res = outputStream->cancelConfiguration();
2116 if (res != OK) {
2117 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2118 outputStream->getId(), strerror(-res), res);
2119 }
2120 }
2121 }
2122
2123 // Return state to that at start of call, so that future configures
2124 // properly clean things up
2125 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2126 mNeedConfig = true;
2127}
2128
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002129status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002130 ATRACE_CALL();
2131 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002132
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002133 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002134 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135 return INVALID_OPERATION;
2136 }
2137
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002138 if (operatingMode < 0) {
2139 CLOGE("Invalid operating mode: %d", operatingMode);
2140 return BAD_VALUE;
2141 }
2142
2143 bool isConstrainedHighSpeed =
2144 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2145 operatingMode;
2146
2147 if (mOperatingMode != operatingMode) {
2148 mNeedConfig = true;
2149 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2150 mOperatingMode = operatingMode;
2151 }
2152
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002153 if (!mNeedConfig) {
2154 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2155 return OK;
2156 }
2157
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002158 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2159 // adding a dummy stream instead.
2160 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2161 if (mOutputStreams.size() == 0) {
2162 addDummyStreamLocked();
2163 } else {
2164 tryRemoveDummyStreamLocked();
2165 }
2166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002167 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002168 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002169
2170 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002171 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002172 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2173
2174 Vector<camera3_stream_t*> streams;
2175 streams.setCapacity(config.num_streams);
2176
2177 if (mInputStream != NULL) {
2178 camera3_stream_t *inputStream;
2179 inputStream = mInputStream->startConfiguration();
2180 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002181 CLOGE("Can't start input stream configuration");
2182 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002183 return INVALID_OPERATION;
2184 }
2185 streams.add(inputStream);
2186 }
2187
2188 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002189
2190 // Don't configure bidi streams twice, nor add them twice to the list
2191 if (mOutputStreams[i].get() ==
2192 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2193
2194 config.num_streams--;
2195 continue;
2196 }
2197
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002198 camera3_stream_t *outputStream;
2199 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2200 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002201 CLOGE("Can't start output stream configuration");
2202 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002203 return INVALID_OPERATION;
2204 }
2205 streams.add(outputStream);
2206 }
2207
2208 config.streams = streams.editArray();
2209
2210 // Do the HAL configuration; will potentially touch stream
2211 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002212
2213 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002214
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002215 if (res == BAD_VALUE) {
2216 // HAL rejected this set of streams as unsupported, clean up config
2217 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002218 CLOGE("Set of requested inputs/outputs not supported by HAL");
2219 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002220 return BAD_VALUE;
2221 } else if (res != OK) {
2222 // Some other kind of error from configure_streams - this is not
2223 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002224 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2225 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002226 return res;
2227 }
2228
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002229 // Finish all stream configuration immediately.
2230 // TODO: Try to relax this later back to lazy completion, which should be
2231 // faster
2232
Igor Murashkin073f8572013-05-02 14:59:28 -07002233 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002234 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002235 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002236 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002237 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002238 cancelStreamsConfigurationLocked();
2239 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002240 }
2241 }
2242
2243 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002244 sp<Camera3OutputStreamInterface> outputStream =
2245 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002246 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002247 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002248 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002249 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002250 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002251 cancelStreamsConfigurationLocked();
2252 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002253 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002254 }
2255 }
2256
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002257 // Request thread needs to know to avoid using repeat-last-settings protocol
2258 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002259 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002260
Zhijun He90f7c372016-08-16 16:19:43 -07002261 char value[PROPERTY_VALUE_MAX];
2262 property_get("camera.fifo.disable", value, "0");
2263 int32_t disableFifo = atoi(value);
2264 if (disableFifo != 1) {
2265 // Boost priority of request thread to SCHED_FIFO.
2266 pid_t requestThreadTid = mRequestThread->getTid();
2267 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002268 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002269 if (res != OK) {
2270 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2271 strerror(-res), res);
2272 } else {
2273 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2274 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002275 }
2276
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002277 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002279 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002280
Ruben Brunk183f0562015-08-12 12:55:02 -07002281 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2282 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002283
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002284 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002285
Zhijun He0a210512014-07-24 13:45:15 -07002286 // tear down the deleted streams after configure streams.
2287 mDeletedStreams.clear();
2288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002289 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002290}
2291
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002292status_t Camera3Device::addDummyStreamLocked() {
2293 ATRACE_CALL();
2294 status_t res;
2295
2296 if (mDummyStreamId != NO_STREAM) {
2297 // Should never be adding a second dummy stream when one is already
2298 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002299 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2300 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002301 return INVALID_OPERATION;
2302 }
2303
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002304 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002305
2306 sp<Camera3OutputStreamInterface> dummyStream =
2307 new Camera3DummyStream(mNextStreamId);
2308
2309 res = mOutputStreams.add(mNextStreamId, dummyStream);
2310 if (res < 0) {
2311 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2312 return res;
2313 }
2314
2315 mDummyStreamId = mNextStreamId;
2316 mNextStreamId++;
2317
2318 return OK;
2319}
2320
2321status_t Camera3Device::tryRemoveDummyStreamLocked() {
2322 ATRACE_CALL();
2323 status_t res;
2324
2325 if (mDummyStreamId == NO_STREAM) return OK;
2326 if (mOutputStreams.size() == 1) return OK;
2327
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002328 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002329
2330 // Ok, have a dummy stream and there's at least one other output stream,
2331 // so remove the dummy
2332
2333 sp<Camera3StreamInterface> deletedStream;
2334 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2335 if (outputStreamIdx == NAME_NOT_FOUND) {
2336 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2337 return INVALID_OPERATION;
2338 }
2339
2340 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2341 mOutputStreams.removeItemsAt(outputStreamIdx);
2342
2343 // Free up the stream endpoint so that it can be used by some other stream
2344 res = deletedStream->disconnect();
2345 if (res != OK) {
2346 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2347 // fall through since we want to still list the stream as deleted.
2348 }
2349 mDeletedStreams.add(deletedStream);
2350 mDummyStreamId = NO_STREAM;
2351
2352 return res;
2353}
2354
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002355void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002356 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002357 Mutex::Autolock l(mLock);
2358 va_list args;
2359 va_start(args, fmt);
2360
2361 setErrorStateLockedV(fmt, args);
2362
2363 va_end(args);
2364}
2365
2366void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002367 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002368 Mutex::Autolock l(mLock);
2369 setErrorStateLockedV(fmt, args);
2370}
2371
2372void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2373 va_list args;
2374 va_start(args, fmt);
2375
2376 setErrorStateLockedV(fmt, args);
2377
2378 va_end(args);
2379}
2380
2381void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002382 // Print out all error messages to log
2383 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002384 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002385
2386 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002387 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002388
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002389 mErrorCause = errorCause;
2390
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002391 if (mRequestThread != nullptr) {
2392 mRequestThread->setPaused(true);
2393 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002394 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002395
2396 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002397 sp<NotificationListener> listener = mListener.promote();
2398 if (listener != NULL) {
2399 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002400 CaptureResultExtras());
2401 }
2402
2403 // Save stack trace. View by dumping it later.
2404 CameraTraces::saveTrace();
2405 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002406}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002407
2408/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002409 * In-flight request management
2410 */
2411
Jianing Weicb0652e2014-03-12 18:29:36 -07002412status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002413 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002414 bool hasAppCallback, nsecs_t maxExpectedDuration) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002415 ATRACE_CALL();
2416 Mutex::Autolock l(mInFlightLock);
2417
2418 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002419 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002420 hasAppCallback, maxExpectedDuration));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002421 if (res < 0) return res;
2422
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002423 if (mInFlightMap.size() == 1) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002424 // hold mLock to prevent race with disconnect
2425 Mutex::Autolock l(mLock);
2426 if (mStatusTracker != nullptr) {
2427 mStatusTracker->markComponentActive(mInFlightStatusId);
2428 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002429 }
2430
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002431 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002432 return OK;
2433}
2434
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002435void Camera3Device::returnOutputBuffers(
2436 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2437 nsecs_t timestamp) {
2438 for (size_t i = 0; i < numBuffers; i++)
2439 {
2440 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2441 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2442 // Note: stream may be deallocated at this point, if this buffer was
2443 // the last reference to it.
2444 if (res != OK) {
2445 ALOGE("Can't return buffer to its stream: %s (%d)",
2446 strerror(-res), res);
2447 }
2448 }
2449}
2450
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002451void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002452 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002453 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002454 mInFlightMap.removeItemsAt(idx, 1);
2455
2456 // Indicate idle inFlightMap to the status tracker
2457 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002458 // hold mLock to prevent race with disconnect
2459 Mutex::Autolock l(mLock);
2460 if (mStatusTracker != nullptr) {
2461 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2462 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002463 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002464 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002465}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002466
2467void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2468
2469 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2470 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2471
2472 nsecs_t sensorTimestamp = request.sensorTimestamp;
2473 nsecs_t shutterTimestamp = request.shutterTimestamp;
2474
2475 // Check if it's okay to remove the request from InFlightMap:
2476 // In the case of a successful request:
2477 // all input and output buffers, all result metadata, shutter callback
2478 // arrived.
2479 // In the case of a unsuccessful request:
2480 // all input and output buffers arrived.
2481 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07002482 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002483 (request.haveResultMetadata && shutterTimestamp != 0))) {
2484 ATRACE_ASYNC_END("frame capture", frameNumber);
2485
Shuzhen Wang403044a2017-02-26 23:29:04 -08002486 // Sanity check - if sensor timestamp matches shutter timestamp in the
2487 // case of request having callback.
2488 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002489 sensorTimestamp != shutterTimestamp) {
2490 SET_ERR("sensor timestamp (%" PRId64
2491 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2492 sensorTimestamp, frameNumber, shutterTimestamp);
2493 }
2494
2495 // for an unsuccessful request, it may have pending output buffers to
2496 // return.
2497 assert(request.requestStatus != OK ||
2498 request.pendingOutputBuffers.size() == 0);
2499 returnOutputBuffers(request.pendingOutputBuffers.array(),
2500 request.pendingOutputBuffers.size(), 0);
2501
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002502 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002503 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2504 }
2505
2506 // Sanity check - if we have too many in-flight frames, something has
2507 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002508 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002509 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002510 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2511 kInFlightWarnLimitHighSpeed) {
2512 CLOGE("In-flight list too large for high speed configuration: %zu",
2513 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002514 }
2515}
2516
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002517void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002518 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002519 { // First return buffers cached in mInFlightMap
2520 Mutex::Autolock l(mInFlightLock);
2521 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2522 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2523 returnOutputBuffers(request.pendingOutputBuffers.array(),
2524 request.pendingOutputBuffers.size(), 0);
2525 }
2526 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002527 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002528 }
2529
2530 // Then return all inflight buffers not returned by HAL
2531 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2532 mInterface->getInflightBufferKeys(&inflightKeys);
2533
2534 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2535 for (auto& pair : inflightKeys) {
2536 int32_t frameNumber = pair.first;
2537 int32_t streamId = pair.second;
2538 buffer_handle_t* buffer;
2539 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2540 if (res != OK) {
2541 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2542 __FUNCTION__, frameNumber, streamId);
2543 continue;
2544 }
2545
2546 camera3_stream_buffer_t streamBuffer;
2547 streamBuffer.buffer = buffer;
2548 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2549 streamBuffer.acquire_fence = -1;
2550 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002551
2552 // First check if the buffer belongs to deleted stream
2553 bool streamDeleted = false;
2554 for (auto& stream : mDeletedStreams) {
2555 if (streamId == stream->getId()) {
2556 streamDeleted = true;
2557 // Return buffer to deleted stream
2558 camera3_stream* halStream = stream->asHalStream();
2559 streamBuffer.stream = halStream;
2560 switch (halStream->stream_type) {
2561 case CAMERA3_STREAM_OUTPUT:
2562 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2563 if (res != OK) {
2564 ALOGE("%s: Can't return output buffer for frame %d to"
2565 " stream %d: %s (%d)", __FUNCTION__,
2566 frameNumber, streamId, strerror(-res), res);
2567 }
2568 break;
2569 case CAMERA3_STREAM_INPUT:
2570 res = stream->returnInputBuffer(streamBuffer);
2571 if (res != OK) {
2572 ALOGE("%s: Can't return input buffer for frame %d to"
2573 " stream %d: %s (%d)", __FUNCTION__,
2574 frameNumber, streamId, strerror(-res), res);
2575 }
2576 break;
2577 default: // Bi-direcitonal stream is deprecated
2578 ALOGE("%s: stream %d has unknown stream type %d",
2579 __FUNCTION__, streamId, halStream->stream_type);
2580 break;
2581 }
2582 break;
2583 }
2584 }
2585 if (streamDeleted) {
2586 continue;
2587 }
2588
2589 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002590 if (streamId == inputStreamId) {
2591 streamBuffer.stream = mInputStream->asHalStream();
2592 res = mInputStream->returnInputBuffer(streamBuffer);
2593 if (res != OK) {
2594 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002595 " stream %d: %s (%d)", __FUNCTION__,
2596 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002597 }
2598 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002599 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2600 if (idx == NAME_NOT_FOUND) {
2601 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2602 continue;
2603 }
2604 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002605 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2606 }
2607 }
2608}
2609
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002610void Camera3Device::insertResultLocked(CaptureResult *result,
2611 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002612 if (result == nullptr) return;
2613
Emilian Peev71c73a22017-03-21 16:35:51 +00002614 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2615 result->mMetadata.getAndLock());
2616 set_camera_metadata_vendor_id(meta, mVendorTagId);
2617 result->mMetadata.unlock(meta);
2618
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002619 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2620 (int32_t*)&frameNumber, 1) != OK) {
2621 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2622 return;
2623 }
2624
2625 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2626 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2627 return;
2628 }
2629
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002630 // Valid result, insert into queue
2631 List<CaptureResult>::iterator queuedResult =
2632 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2633 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2634 ", burstId = %" PRId32, __FUNCTION__,
2635 queuedResult->mResultExtras.requestId,
2636 queuedResult->mResultExtras.frameNumber,
2637 queuedResult->mResultExtras.burstId);
2638
2639 mResultSignal.signal();
2640}
2641
2642
2643void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002644 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002645 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002646 Mutex::Autolock l(mOutputLock);
2647
2648 CaptureResult captureResult;
2649 captureResult.mResultExtras = resultExtras;
2650 captureResult.mMetadata = partialResult;
2651
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002652 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002653}
2654
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002655
2656void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2657 CaptureResultExtras &resultExtras,
2658 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002659 uint32_t frameNumber,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002660 bool reprocess) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002661 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002662 if (pendingMetadata.isEmpty())
2663 return;
2664
2665 Mutex::Autolock l(mOutputLock);
2666
2667 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002668 if (reprocess) {
2669 if (frameNumber < mNextReprocessResultFrameNumber) {
2670 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002671 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002672 frameNumber, mNextReprocessResultFrameNumber);
2673 return;
2674 }
2675 mNextReprocessResultFrameNumber = frameNumber + 1;
2676 } else {
2677 if (frameNumber < mNextResultFrameNumber) {
2678 SET_ERR("Out-of-order capture result metadata submitted! "
2679 "(got frame number %d, expecting %d)",
2680 frameNumber, mNextResultFrameNumber);
2681 return;
2682 }
2683 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002684 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002685
2686 CaptureResult captureResult;
2687 captureResult.mResultExtras = resultExtras;
2688 captureResult.mMetadata = pendingMetadata;
2689
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002690 // Append any previous partials to form a complete result
2691 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2692 captureResult.mMetadata.append(collectedPartialResult);
2693 }
2694
2695 captureResult.mMetadata.sort();
2696
2697 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002698 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2699 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002700 SET_ERR("No timestamp provided by HAL for frame %d!",
2701 frameNumber);
2702 return;
2703 }
2704
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002705 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2706 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2707
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002708 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002709}
2710
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002711/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002712 * Camera HAL device callback methods
2713 */
2714
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002715void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002716 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002717
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002718 status_t res;
2719
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002720 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002721 if (result->result == NULL && result->num_output_buffers == 0 &&
2722 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002723 SET_ERR("No result data provided by HAL for frame %d",
2724 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002725 return;
2726 }
Zhijun He204e3292014-07-14 17:09:23 -07002727
Zhijun He204e3292014-07-14 17:09:23 -07002728 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002729 result->result != NULL &&
2730 result->partial_result != 1) {
2731 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2732 " if partial result is not supported",
2733 frameNumber, result->partial_result);
2734 return;
2735 }
2736
2737 bool isPartialResult = false;
2738 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002739 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002740 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002741
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002742 // Get shutter timestamp and resultExtras from list of in-flight requests,
2743 // where it was added by the shutter notification for this frame. If the
2744 // shutter timestamp isn't received yet, append the output buffers to the
2745 // in-flight request and they will be returned when the shutter timestamp
2746 // arrives. Update the in-flight status and remove the in-flight entry if
2747 // all result data and shutter timestamp have been received.
2748 nsecs_t shutterTimestamp = 0;
2749
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002750 {
2751 Mutex::Autolock l(mInFlightLock);
2752 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2753 if (idx == NAME_NOT_FOUND) {
2754 SET_ERR("Unknown frame number for capture result: %d",
2755 frameNumber);
2756 return;
2757 }
2758 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002759 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2760 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002761 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002762 __FUNCTION__, request.resultExtras.requestId,
2763 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002764 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002765 // Always update the partial count to the latest one if it's not 0
2766 // (buffers only). When framework aggregates adjacent partial results
2767 // into one, the latest partial count will be used.
2768 if (result->partial_result != 0)
2769 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002770
2771 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002772 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002773 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2774 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2775 " the range of [1, %d] when metadata is included in the result",
2776 frameNumber, result->partial_result, mNumPartialResults);
2777 return;
2778 }
2779 isPartialResult = (result->partial_result < mNumPartialResults);
2780 if (isPartialResult) {
2781 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002782 }
2783
Shuzhen Wang4a472662017-02-26 23:29:04 -08002784 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002785 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002786 sendPartialCaptureResult(result->result, request.resultExtras,
2787 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002788 }
2789 }
2790
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002791 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002792 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002793
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002794 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002795 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002796 if (request.haveResultMetadata) {
2797 SET_ERR("Called multiple times with metadata for frame %d",
2798 frameNumber);
2799 return;
2800 }
Zhijun He204e3292014-07-14 17:09:23 -07002801 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002802 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002803 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002804 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002805 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002806 request.haveResultMetadata = true;
2807 }
2808
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002809 uint32_t numBuffersReturned = result->num_output_buffers;
2810 if (result->input_buffer != NULL) {
2811 if (hasInputBufferInRequest) {
2812 numBuffersReturned += 1;
2813 } else {
2814 ALOGW("%s: Input buffer should be NULL if there is no input"
2815 " buffer sent in the request",
2816 __FUNCTION__);
2817 }
2818 }
2819 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002820 if (request.numBuffersLeft < 0) {
2821 SET_ERR("Too many buffers returned for frame %d",
2822 frameNumber);
2823 return;
2824 }
2825
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002826 camera_metadata_ro_entry_t entry;
2827 res = find_camera_metadata_ro_entry(result->result,
2828 ANDROID_SENSOR_TIMESTAMP, &entry);
2829 if (res == OK && entry.count == 1) {
2830 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002831 }
2832
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002833 // If shutter event isn't received yet, append the output buffers to
2834 // the in-flight request. Otherwise, return the output buffers to
2835 // streams.
2836 if (shutterTimestamp == 0) {
2837 request.pendingOutputBuffers.appendArray(result->output_buffers,
2838 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002839 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002840 returnOutputBuffers(result->output_buffers,
2841 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002842 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002843
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002844 if (result->result != NULL && !isPartialResult) {
2845 if (shutterTimestamp == 0) {
2846 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002847 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002848 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002849 CameraMetadata metadata;
2850 metadata = result->result;
2851 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002852 collectedPartialResult, frameNumber,
2853 hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002854 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002855 }
2856
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002857 removeInFlightRequestIfReadyLocked(idx);
2858 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002859
Zhijun Hef0d962a2014-06-30 10:24:11 -07002860 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002861 if (hasInputBufferInRequest) {
2862 Camera3Stream *stream =
2863 Camera3Stream::cast(result->input_buffer->stream);
2864 res = stream->returnInputBuffer(*(result->input_buffer));
2865 // Note: stream may be deallocated at this point, if this buffer was the
2866 // last reference to it.
2867 if (res != OK) {
2868 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2869 " its stream:%s (%d)", __FUNCTION__,
2870 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002871 }
2872 } else {
2873 ALOGW("%s: Input buffer should be NULL if there is no input"
2874 " buffer sent in the request, skipping input buffer return.",
2875 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002876 }
2877 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002878}
2879
2880void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002881 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002882 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002883 {
2884 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002885 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002886 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002887
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002888 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002889 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002890 return;
2891 }
2892
2893 switch (msg->type) {
2894 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002895 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002896 break;
2897 }
2898 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002899 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002900 break;
2901 }
2902 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002903 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002904 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002905 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002906}
2907
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002908void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002909 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002910 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002911 // Map camera HAL error codes to ICameraDeviceCallback error codes
2912 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002913 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002914 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002915 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002916 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002917 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002918 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002919 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002920 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002921 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002922 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002923 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002924 };
2925
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002926 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002927 ((msg.error_code >= 0) &&
2928 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2929 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002930 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931
2932 int streamId = 0;
2933 if (msg.error_stream != NULL) {
2934 Camera3Stream *stream =
2935 Camera3Stream::cast(msg.error_stream);
2936 streamId = stream->getId();
2937 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002938 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2939 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002940 streamId, msg.error_code);
2941
2942 CaptureResultExtras resultExtras;
2943 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002944 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002945 // SET_ERR calls notifyError
2946 SET_ERR("Camera HAL reported serious device error");
2947 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002948 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2949 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2950 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002951 {
2952 Mutex::Autolock l(mInFlightLock);
2953 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2954 if (idx >= 0) {
2955 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2956 r.requestStatus = msg.error_code;
2957 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07002958 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
2959 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
2960 errorCode) {
2961 r.skipResultMetadata = true;
2962 }
Emilian Peevba0fac32017-03-30 09:05:34 +01002963 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
2964 errorCode) {
2965 // In case of missing result check whether the buffers
2966 // returned. If they returned, then remove inflight
2967 // request.
2968 removeInFlightRequestIfReadyLocked(idx);
2969 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002970 } else {
2971 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002972 ALOGE("Camera %s: %s: cannot find in-flight request on "
2973 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002974 resultExtras.frameNumber);
2975 }
2976 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002977 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002978 if (listener != NULL) {
2979 listener->notifyError(errorCode, resultExtras);
2980 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002981 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002982 }
2983 break;
2984 default:
2985 // SET_ERR calls notifyError
2986 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2987 break;
2988 }
2989}
2990
2991void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002992 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002993 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002994 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002995
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002996 // Set timestamp for the request in the in-flight tracking
2997 // and get the request ID to send upstream
2998 {
2999 Mutex::Autolock l(mInFlightLock);
3000 idx = mInFlightMap.indexOfKey(msg.frame_number);
3001 if (idx >= 0) {
3002 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003003
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003004 // Verify ordering of shutter notifications
3005 {
3006 Mutex::Autolock l(mOutputLock);
3007 // TODO: need to track errors for tighter bounds on expected frame number.
3008 if (r.hasInputBuffer) {
3009 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3010 SET_ERR("Shutter notification out-of-order. Expected "
3011 "notification for frame %d, got frame %d",
3012 mNextReprocessShutterFrameNumber, msg.frame_number);
3013 return;
3014 }
3015 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3016 } else {
3017 if (msg.frame_number < mNextShutterFrameNumber) {
3018 SET_ERR("Shutter notification out-of-order. Expected "
3019 "notification for frame %d, got frame %d",
3020 mNextShutterFrameNumber, msg.frame_number);
3021 return;
3022 }
3023 mNextShutterFrameNumber = msg.frame_number + 1;
3024 }
3025 }
3026
Shuzhen Wang4a472662017-02-26 23:29:04 -08003027 r.shutterTimestamp = msg.timestamp;
3028 if (r.hasCallback) {
3029 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003030 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003031 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003032 // Call listener, if any
3033 if (listener != NULL) {
3034 listener->notifyShutter(r.resultExtras, msg.timestamp);
3035 }
3036 // send pending result and buffers
3037 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3038 r.collectedPartialResult, msg.frame_number,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003039 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003040 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003041 returnOutputBuffers(r.pendingOutputBuffers.array(),
3042 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3043 r.pendingOutputBuffers.clear();
3044
3045 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003046 }
3047 }
3048 if (idx < 0) {
3049 SET_ERR("Shutter notification for non-existent frame number %d",
3050 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003051 }
3052}
3053
3054
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003055CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003056 ALOGV("%s", __FUNCTION__);
3057
Igor Murashkin1e479c02013-09-06 16:55:14 -07003058 CameraMetadata retVal;
3059
3060 if (mRequestThread != NULL) {
3061 retVal = mRequestThread->getLatestRequest();
3062 }
3063
Igor Murashkin1e479c02013-09-06 16:55:14 -07003064 return retVal;
3065}
3066
Jianing Weicb0652e2014-03-12 18:29:36 -07003067
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003068void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3069 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3070 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3071}
3072
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003073/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003074 * HalInterface inner class methods
3075 */
3076
Yifan Hongf79b5542017-04-11 14:44:25 -07003077Camera3Device::HalInterface::HalInterface(
3078 sp<ICameraDeviceSession> &session,
3079 std::shared_ptr<RequestMetadataQueue> queue) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003080 mHidlSession(session),
3081 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003082
Emilian Peev31abd0a2017-05-11 18:37:46 +01003083Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003084
3085Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003086 mHidlSession(other.mHidlSession),
3087 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003088
3089bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003090 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003091}
3092
3093void Camera3Device::HalInterface::clear() {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003094 mHidlSession.clear();
3095}
3096
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003097bool Camera3Device::HalInterface::supportBatchRequest() {
3098 return mHidlSession != nullptr;
3099}
3100
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003101status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3102 camera3_request_template_t templateId,
3103 /*out*/ camera_metadata_t **requestTemplate) {
3104 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3105 if (!valid()) return INVALID_OPERATION;
3106 status_t res = OK;
3107
Emilian Peev31abd0a2017-05-11 18:37:46 +01003108 common::V1_0::Status status;
3109 RequestTemplate id;
3110 switch (templateId) {
3111 case CAMERA3_TEMPLATE_PREVIEW:
3112 id = RequestTemplate::PREVIEW;
3113 break;
3114 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3115 id = RequestTemplate::STILL_CAPTURE;
3116 break;
3117 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3118 id = RequestTemplate::VIDEO_RECORD;
3119 break;
3120 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3121 id = RequestTemplate::VIDEO_SNAPSHOT;
3122 break;
3123 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3124 id = RequestTemplate::ZERO_SHUTTER_LAG;
3125 break;
3126 case CAMERA3_TEMPLATE_MANUAL:
3127 id = RequestTemplate::MANUAL;
3128 break;
3129 default:
3130 // Unknown template ID
3131 return BAD_VALUE;
3132 }
3133 auto err = mHidlSession->constructDefaultRequestSettings(id,
3134 [&status, &requestTemplate]
3135 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3136 status = s;
3137 if (status == common::V1_0::Status::OK) {
3138 const camera_metadata *r =
3139 reinterpret_cast<const camera_metadata_t*>(request.data());
3140 size_t expectedSize = request.size();
3141 int ret = validate_camera_metadata_structure(r, &expectedSize);
3142 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3143 *requestTemplate = clone_camera_metadata(r);
3144 if (*requestTemplate == nullptr) {
3145 ALOGE("%s: Unable to clone camera metadata received from HAL",
3146 __FUNCTION__);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003147 status = common::V1_0::Status::INTERNAL_ERROR;
3148 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003149 } else {
3150 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3151 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003152 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003153 }
3154 });
3155 if (!err.isOk()) {
3156 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3157 res = DEAD_OBJECT;
3158 } else {
3159 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003160 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003161
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003162 return res;
3163}
3164
3165status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3166 ATRACE_NAME("CameraHal::configureStreams");
3167 if (!valid()) return INVALID_OPERATION;
3168 status_t res = OK;
3169
Emilian Peev31abd0a2017-05-11 18:37:46 +01003170 // Convert stream config to HIDL
3171 std::set<int> activeStreams;
3172 StreamConfiguration requestedConfiguration;
3173 requestedConfiguration.streams.resize(config->num_streams);
3174 for (size_t i = 0; i < config->num_streams; i++) {
3175 Stream &dst = requestedConfiguration.streams[i];
3176 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003177
Emilian Peev31abd0a2017-05-11 18:37:46 +01003178 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3179 cam3stream->setBufferFreedListener(this);
3180 int streamId = cam3stream->getId();
3181 StreamType streamType;
3182 switch (src->stream_type) {
3183 case CAMERA3_STREAM_OUTPUT:
3184 streamType = StreamType::OUTPUT;
3185 break;
3186 case CAMERA3_STREAM_INPUT:
3187 streamType = StreamType::INPUT;
3188 break;
3189 default:
3190 ALOGE("%s: Stream %d: Unsupported stream type %d",
3191 __FUNCTION__, streamId, config->streams[i]->stream_type);
3192 return BAD_VALUE;
3193 }
3194 dst.id = streamId;
3195 dst.streamType = streamType;
3196 dst.width = src->width;
3197 dst.height = src->height;
3198 dst.format = mapToPixelFormat(src->format);
Emilian Peev050f5dc2017-05-18 14:43:56 +01003199 dst.usage = mapToConsumerUsage(cam3stream->getUsage());
Emilian Peev31abd0a2017-05-11 18:37:46 +01003200 dst.dataSpace = mapToHidlDataspace(src->data_space);
3201 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3202
3203 activeStreams.insert(streamId);
3204 // Create Buffer ID map if necessary
3205 if (mBufferIdMaps.count(streamId) == 0) {
3206 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3207 }
3208 }
3209 // remove BufferIdMap for deleted streams
3210 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3211 int streamId = it->first;
3212 bool active = activeStreams.count(streamId) > 0;
3213 if (!active) {
3214 it = mBufferIdMaps.erase(it);
3215 } else {
3216 ++it;
3217 }
3218 }
3219
3220 res = mapToStreamConfigurationMode(
3221 (camera3_stream_configuration_mode_t) config->operation_mode,
3222 /*out*/ &requestedConfiguration.operationMode);
3223 if (res != OK) {
3224 return res;
3225 }
3226
3227 // Invoke configureStreams
3228
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003229 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003230 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003231
3232 // See if we have v3.3 HAL
3233 sp<device::V3_3::ICameraDeviceSession> hidlSession_3_3;
3234 auto castResult = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3235 if (castResult.isOk()) {
3236 hidlSession_3_3 = castResult;
3237 } else {
3238 ALOGE("%s: Transaction error when casting ICameraDeviceSession: %s", __FUNCTION__,
3239 castResult.description().c_str());
3240 }
3241 if (hidlSession_3_3 != nullptr) {
3242 // We do; use v3.3 for the call
3243 ALOGV("%s: v3.3 device found", __FUNCTION__);
3244 auto err = hidlSession_3_3->configureStreams_3_3(requestedConfiguration,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003245 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003246 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003247 finalConfiguration = halConfiguration;
3248 status = s;
3249 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003250 if (!err.isOk()) {
3251 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3252 return DEAD_OBJECT;
3253 }
3254 } else {
3255 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3256 ALOGV("%s: v3.2 device found", __FUNCTION__);
3257 HalStreamConfiguration finalConfiguration_3_2;
3258 auto err = mHidlSession->configureStreams(requestedConfiguration,
3259 [&status, &finalConfiguration_3_2]
3260 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3261 finalConfiguration_3_2 = halConfiguration;
3262 status = s;
3263 });
3264 if (!err.isOk()) {
3265 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3266 return DEAD_OBJECT;
3267 }
3268 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3269 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3270 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3271 finalConfiguration.streams[i].overrideDataSpace =
3272 requestedConfiguration.streams[i].dataSpace;
3273 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003274 }
3275
3276 if (status != common::V1_0::Status::OK ) {
3277 return CameraProviderManager::mapToStatusT(status);
3278 }
3279
3280 // And convert output stream configuration from HIDL
3281
3282 for (size_t i = 0; i < config->num_streams; i++) {
3283 camera3_stream_t *dst = config->streams[i];
3284 int streamId = Camera3Stream::cast(dst)->getId();
3285
3286 // Start scan at i, with the assumption that the stream order matches
3287 size_t realIdx = i;
3288 bool found = false;
3289 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003290 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003291 found = true;
3292 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003293 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003294 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3295 }
3296 if (!found) {
3297 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3298 __FUNCTION__, streamId);
3299 return INVALID_OPERATION;
3300 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003301 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003302
Emilian Peev710c1422017-08-30 11:19:38 +01003303 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3304 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003305 dstStream->setDataSpaceOverride(false);
3306 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3307 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3308
Emilian Peev31abd0a2017-05-11 18:37:46 +01003309 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3310 if (dst->format != overrideFormat) {
3311 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3312 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003313 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003314 if (dst->data_space != overrideDataSpace) {
3315 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3316 streamId, dst->format);
3317 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003318 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003319 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003320 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3321
Emilian Peev31abd0a2017-05-11 18:37:46 +01003322 // Override allowed with IMPLEMENTATION_DEFINED
3323 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003324 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003325 }
3326
Emilian Peev31abd0a2017-05-11 18:37:46 +01003327 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003328 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003329 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003330 __FUNCTION__, streamId);
3331 return INVALID_OPERATION;
3332 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003333 dstStream->setUsage(
3334 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003335 } else {
3336 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003337 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003338 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3339 __FUNCTION__, streamId);
3340 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003341 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003342 dstStream->setUsage(
3343 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003344 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003345 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003346 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003347
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003348 return res;
3349}
3350
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003351void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3352 /*out*/device::V3_2::CaptureRequest* captureRequest,
3353 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003354 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003355 if (captureRequest == nullptr || handlesCreated == nullptr) {
3356 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3357 __FUNCTION__, captureRequest, handlesCreated);
3358 return;
3359 }
3360
3361 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003362
3363 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003364
3365 {
3366 std::lock_guard<std::mutex> lock(mInflightLock);
3367 if (request->input_buffer != nullptr) {
3368 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3369 buffer_handle_t buf = *(request->input_buffer->buffer);
3370 auto pair = getBufferId(buf, streamId);
3371 bool isNewBuffer = pair.first;
3372 uint64_t bufferId = pair.second;
3373 captureRequest->inputBuffer.streamId = streamId;
3374 captureRequest->inputBuffer.bufferId = bufferId;
3375 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3376 captureRequest->inputBuffer.status = BufferStatus::OK;
3377 native_handle_t *acquireFence = nullptr;
3378 if (request->input_buffer->acquire_fence != -1) {
3379 acquireFence = native_handle_create(1,0);
3380 acquireFence->data[0] = request->input_buffer->acquire_fence;
3381 handlesCreated->push_back(acquireFence);
3382 }
3383 captureRequest->inputBuffer.acquireFence = acquireFence;
3384 captureRequest->inputBuffer.releaseFence = nullptr;
3385
3386 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3387 request->input_buffer->buffer,
3388 request->input_buffer->acquire_fence);
3389 } else {
3390 captureRequest->inputBuffer.streamId = -1;
3391 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3392 }
3393
3394 captureRequest->outputBuffers.resize(request->num_output_buffers);
3395 for (size_t i = 0; i < request->num_output_buffers; i++) {
3396 const camera3_stream_buffer_t *src = request->output_buffers + i;
3397 StreamBuffer &dst = captureRequest->outputBuffers[i];
3398 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3399 buffer_handle_t buf = *(src->buffer);
3400 auto pair = getBufferId(buf, streamId);
3401 bool isNewBuffer = pair.first;
3402 dst.streamId = streamId;
3403 dst.bufferId = pair.second;
3404 dst.buffer = isNewBuffer ? buf : nullptr;
3405 dst.status = BufferStatus::OK;
3406 native_handle_t *acquireFence = nullptr;
3407 if (src->acquire_fence != -1) {
3408 acquireFence = native_handle_create(1,0);
3409 acquireFence->data[0] = src->acquire_fence;
3410 handlesCreated->push_back(acquireFence);
3411 }
3412 dst.acquireFence = acquireFence;
3413 dst.releaseFence = nullptr;
3414
3415 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3416 src->buffer, src->acquire_fence);
3417 }
3418 }
3419}
3420
3421status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3422 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3423 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3424 if (!valid()) return INVALID_OPERATION;
3425
3426 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3427 size_t batchSize = requests.size();
3428 captureRequests.resize(batchSize);
3429 std::vector<native_handle_t*> handlesCreated;
3430
3431 for (size_t i = 0; i < batchSize; i++) {
3432 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3433 }
3434
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003435 std::vector<device::V3_2::BufferCache> cachesToRemove;
3436 {
3437 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3438 for (auto& pair : mFreedBuffers) {
3439 // The stream might have been removed since onBufferFreed
3440 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3441 cachesToRemove.push_back({pair.first, pair.second});
3442 }
3443 }
3444 mFreedBuffers.clear();
3445 }
3446
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003447 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3448 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003449
3450 // Write metadata to FMQ.
3451 for (size_t i = 0; i < batchSize; i++) {
3452 camera3_capture_request_t* request = requests[i];
3453 device::V3_2::CaptureRequest* captureRequest = &captureRequests[i];
3454
3455 if (request->settings != nullptr) {
3456 size_t settingsSize = get_camera_metadata_size(request->settings);
3457 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3458 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3459 captureRequest->settings.resize(0);
3460 captureRequest->fmqSettingsSize = settingsSize;
3461 } else {
3462 if (mRequestMetadataQueue != nullptr) {
3463 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3464 }
3465 captureRequest->settings.setToExternal(
3466 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3467 get_camera_metadata_size(request->settings));
3468 captureRequest->fmqSettingsSize = 0u;
3469 }
3470 } else {
3471 // A null request settings maps to a size-0 CameraMetadata
3472 captureRequest->settings.resize(0);
3473 captureRequest->fmqSettingsSize = 0u;
3474 }
3475 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003476 auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003477 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3478 status = s;
3479 *numRequestProcessed = n;
3480 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003481 if (!err.isOk()) {
3482 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3483 return DEAD_OBJECT;
3484 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003485 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3486 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3487 __FUNCTION__, *numRequestProcessed, batchSize);
3488 status = common::V1_0::Status::INTERNAL_ERROR;
3489 }
3490
3491 for (auto& handle : handlesCreated) {
3492 native_handle_delete(handle);
3493 }
3494 return CameraProviderManager::mapToStatusT(status);
3495}
3496
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003497status_t Camera3Device::HalInterface::processCaptureRequest(
3498 camera3_capture_request_t *request) {
3499 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003500 if (!valid()) return INVALID_OPERATION;
3501 status_t res = OK;
3502
Emilian Peev31abd0a2017-05-11 18:37:46 +01003503 uint32_t numRequestProcessed = 0;
3504 std::vector<camera3_capture_request_t*> requests(1);
3505 requests[0] = request;
3506 res = processBatchCaptureRequests(requests, &numRequestProcessed);
3507
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003508 return res;
3509}
3510
3511status_t Camera3Device::HalInterface::flush() {
3512 ATRACE_NAME("CameraHal::flush");
3513 if (!valid()) return INVALID_OPERATION;
3514 status_t res = OK;
3515
Emilian Peev31abd0a2017-05-11 18:37:46 +01003516 auto err = mHidlSession->flush();
3517 if (!err.isOk()) {
3518 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3519 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003520 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003521 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003522 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003523
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003524 return res;
3525}
3526
Emilian Peev31abd0a2017-05-11 18:37:46 +01003527status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003528 ATRACE_NAME("CameraHal::dump");
3529 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003530
Emilian Peev31abd0a2017-05-11 18:37:46 +01003531 // Handled by CameraProviderManager::dump
3532
3533 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003534}
3535
3536status_t Camera3Device::HalInterface::close() {
3537 ATRACE_NAME("CameraHal::close()");
3538 if (!valid()) return INVALID_OPERATION;
3539 status_t res = OK;
3540
Emilian Peev31abd0a2017-05-11 18:37:46 +01003541 auto err = mHidlSession->close();
3542 // Interface will be dead shortly anyway, so don't log errors
3543 if (!err.isOk()) {
3544 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003545 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003546
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003547 return res;
3548}
3549
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003550void Camera3Device::HalInterface::getInflightBufferKeys(
3551 std::vector<std::pair<int32_t, int32_t>>* out) {
3552 std::lock_guard<std::mutex> lock(mInflightLock);
3553 out->clear();
3554 out->reserve(mInflightBufferMap.size());
3555 for (auto& pair : mInflightBufferMap) {
3556 uint64_t key = pair.first;
3557 int32_t streamId = key & 0xFFFFFFFF;
3558 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3559 out->push_back(std::make_pair(frameNumber, streamId));
3560 }
3561 return;
3562}
3563
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003564status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003565 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003566 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003567 auto pair = std::make_pair(buffer, acquireFence);
3568 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003569 return OK;
3570}
3571
3572status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003573 int32_t frameNumber, int32_t streamId,
3574 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003575 std::lock_guard<std::mutex> lock(mInflightLock);
3576
3577 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3578 auto it = mInflightBufferMap.find(key);
3579 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003580 auto pair = it->second;
3581 *buffer = pair.first;
3582 int acquireFence = pair.second;
3583 if (acquireFence > 0) {
3584 ::close(acquireFence);
3585 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003586 mInflightBufferMap.erase(it);
3587 return OK;
3588}
3589
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003590std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3591 const buffer_handle_t& buf, int streamId) {
3592 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3593
3594 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3595 auto it = bIdMap.find(buf);
3596 if (it == bIdMap.end()) {
3597 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003598 ALOGV("stream %d now have %zu buffer caches, buf %p",
3599 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003600 return std::make_pair(true, mNextBufferId - 1);
3601 } else {
3602 return std::make_pair(false, it->second);
3603 }
3604}
3605
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003606void Camera3Device::HalInterface::onBufferFreed(
3607 int streamId, const native_handle_t* handle) {
3608 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3609 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3610 auto mapIt = mBufferIdMaps.find(streamId);
3611 if (mapIt == mBufferIdMaps.end()) {
3612 // streamId might be from a deleted stream here
3613 ALOGI("%s: stream %d has been removed",
3614 __FUNCTION__, streamId);
3615 return;
3616 }
3617 BufferIdMap& bIdMap = mapIt->second;
3618 auto it = bIdMap.find(handle);
3619 if (it == bIdMap.end()) {
3620 ALOGW("%s: cannot find buffer %p in stream %d",
3621 __FUNCTION__, handle, streamId);
3622 return;
3623 } else {
3624 bufferId = it->second;
3625 bIdMap.erase(it);
3626 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3627 __FUNCTION__, streamId, bIdMap.size(), handle);
3628 }
3629 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3630}
3631
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003632/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003633 * RequestThread inner class methods
3634 */
3635
3636Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003637 sp<StatusTracker> statusTracker,
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -07003638 sp<HalInterface> interface) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003639 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003640 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003641 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003642 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003643 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003644 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003645 mReconfigured(false),
3646 mDoPause(false),
3647 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003648 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003649 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003650 mCurrentAfTriggerId(0),
3651 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003652 mRepeatingLastFrameNumber(
3653 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003654 mPrepareVideoStream(false),
3655 mRequestLatency(kRequestLatencyBinSize) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003656 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003657}
3658
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003659Camera3Device::RequestThread::~RequestThread() {}
3660
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003661void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003662 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003663 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003664 Mutex::Autolock l(mRequestLock);
3665 mListener = listener;
3666}
3667
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003668void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003669 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003670 Mutex::Autolock l(mRequestLock);
3671 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003672 // Prepare video stream for high speed recording.
3673 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003674}
3675
Jianing Wei90e59c92014-03-12 18:29:36 -07003676status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003677 List<sp<CaptureRequest> > &requests,
3678 /*out*/
3679 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003680 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003681 Mutex::Autolock l(mRequestLock);
3682 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3683 ++it) {
3684 mRequestQueue.push_back(*it);
3685 }
3686
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003687 if (lastFrameNumber != NULL) {
3688 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3689 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3690 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3691 *lastFrameNumber);
3692 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003693
Jianing Wei90e59c92014-03-12 18:29:36 -07003694 unpauseForNewRequests();
3695
3696 return OK;
3697}
3698
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003699
3700status_t Camera3Device::RequestThread::queueTrigger(
3701 RequestTrigger trigger[],
3702 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003703 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003704 Mutex::Autolock l(mTriggerMutex);
3705 status_t ret;
3706
3707 for (size_t i = 0; i < count; ++i) {
3708 ret = queueTriggerLocked(trigger[i]);
3709
3710 if (ret != OK) {
3711 return ret;
3712 }
3713 }
3714
3715 return OK;
3716}
3717
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003718const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3719 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003720 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003721 if (d != nullptr) return d->mId;
3722 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003723}
3724
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003725status_t Camera3Device::RequestThread::queueTriggerLocked(
3726 RequestTrigger trigger) {
3727
3728 uint32_t tag = trigger.metadataTag;
3729 ssize_t index = mTriggerMap.indexOfKey(tag);
3730
3731 switch (trigger.getTagType()) {
3732 case TYPE_BYTE:
3733 // fall-through
3734 case TYPE_INT32:
3735 break;
3736 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003737 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3738 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003739 return INVALID_OPERATION;
3740 }
3741
3742 /**
3743 * Collect only the latest trigger, since we only have 1 field
3744 * in the request settings per trigger tag, and can't send more than 1
3745 * trigger per request.
3746 */
3747 if (index != NAME_NOT_FOUND) {
3748 mTriggerMap.editValueAt(index) = trigger;
3749 } else {
3750 mTriggerMap.add(tag, trigger);
3751 }
3752
3753 return OK;
3754}
3755
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003756status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003757 const RequestList &requests,
3758 /*out*/
3759 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003760 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003761 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003762 if (lastFrameNumber != NULL) {
3763 *lastFrameNumber = mRepeatingLastFrameNumber;
3764 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003765 mRepeatingRequests.clear();
3766 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3767 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003768
3769 unpauseForNewRequests();
3770
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003771 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003772 return OK;
3773}
3774
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003775bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003776 if (mRepeatingRequests.empty()) {
3777 return false;
3778 }
3779 int32_t requestId = requestIn->mResultExtras.requestId;
3780 const RequestList &repeatRequests = mRepeatingRequests;
3781 // All repeating requests are guaranteed to have same id so only check first quest
3782 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3783 return (firstRequest->mResultExtras.requestId == requestId);
3784}
3785
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003786status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003787 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003788 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003789 return clearRepeatingRequestsLocked(lastFrameNumber);
3790
3791}
3792
3793status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003794 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003795 if (lastFrameNumber != NULL) {
3796 *lastFrameNumber = mRepeatingLastFrameNumber;
3797 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003798 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003799 return OK;
3800}
3801
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003802status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003803 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003804 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003805 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003806 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003807
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003808 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003809
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003810 // Send errors for all requests pending in the request queue, including
3811 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003812 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003813 if (listener != NULL) {
3814 for (RequestList::iterator it = mRequestQueue.begin();
3815 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003816 // Abort the input buffers for reprocess requests.
3817 if ((*it)->mInputStream != NULL) {
3818 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003819 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
3820 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003821 if (res != OK) {
3822 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3823 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3824 } else {
3825 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3826 if (res != OK) {
3827 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3828 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3829 }
3830 }
3831 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003832 // Set the frame number this request would have had, if it
3833 // had been submitted; this frame number will not be reused.
3834 // The requestId and burstId fields were set when the request was
3835 // submitted originally (in convertMetadataListToRequestListLocked)
3836 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003837 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003838 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003839 }
3840 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003841 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003842
3843 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003844 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003845 if (lastFrameNumber != NULL) {
3846 *lastFrameNumber = mRepeatingLastFrameNumber;
3847 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003848 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003849 return OK;
3850}
3851
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003852status_t Camera3Device::RequestThread::flush() {
3853 ATRACE_CALL();
3854 Mutex::Autolock l(mFlushLock);
3855
Emilian Peev08dd2452017-04-06 16:55:14 +01003856 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003857}
3858
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003859void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003860 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003861 Mutex::Autolock l(mPauseLock);
3862 mDoPause = paused;
3863 mDoPauseSignal.signal();
3864}
3865
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003866status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3867 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003868 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003869 Mutex::Autolock l(mLatestRequestMutex);
3870 status_t res;
3871 while (mLatestRequestId != requestId) {
3872 nsecs_t startTime = systemTime();
3873
3874 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3875 if (res != OK) return res;
3876
3877 timeout -= (systemTime() - startTime);
3878 }
3879
3880 return OK;
3881}
3882
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003883void Camera3Device::RequestThread::requestExit() {
3884 // Call parent to set up shutdown
3885 Thread::requestExit();
3886 // The exit from any possible waits
3887 mDoPauseSignal.signal();
3888 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003889
3890 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3891 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003892}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003893
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003894void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003895 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003896 bool surfaceAbandoned = false;
3897 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003898 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003899 {
3900 Mutex::Autolock l(mRequestLock);
3901 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3902 // repeating requests.
3903 for (const auto& request : mRepeatingRequests) {
3904 for (const auto& s : request->mOutputStreams) {
3905 if (s->isAbandoned()) {
3906 surfaceAbandoned = true;
3907 clearRepeatingRequestsLocked(&lastFrameNumber);
3908 break;
3909 }
3910 }
3911 if (surfaceAbandoned) {
3912 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003913 }
3914 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003915 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003916 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003917
3918 if (listener != NULL && surfaceAbandoned) {
3919 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003920 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003921}
3922
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003923bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003924 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003925 status_t res;
3926 size_t batchSize = mNextRequests.size();
3927 std::vector<camera3_capture_request_t*> requests(batchSize);
3928 uint32_t numRequestProcessed = 0;
3929 for (size_t i = 0; i < batchSize; i++) {
3930 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3931 }
3932
3933 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
3934 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3935
3936 bool triggerRemoveFailed = false;
3937 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3938 for (size_t i = 0; i < numRequestProcessed; i++) {
3939 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3940 nextRequest.submitted = true;
3941
3942
3943 // Update the latest request sent to HAL
3944 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3945 Mutex::Autolock al(mLatestRequestMutex);
3946
3947 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3948 mLatestRequest.acquire(cloned);
3949
3950 sp<Camera3Device> parent = mParent.promote();
3951 if (parent != NULL) {
3952 parent->monitorMetadata(TagMonitor::REQUEST,
3953 nextRequest.halRequest.frame_number,
3954 0, mLatestRequest);
3955 }
3956 }
3957
3958 if (nextRequest.halRequest.settings != NULL) {
3959 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3960 }
3961
3962 if (!triggerRemoveFailed) {
3963 // Remove any previously queued triggers (after unlock)
3964 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3965 if (removeTriggerRes != OK) {
3966 triggerRemoveFailed = true;
3967 triggerFailedRequest = nextRequest;
3968 }
3969 }
3970 }
3971
3972 if (triggerRemoveFailed) {
3973 SET_ERR("RequestThread: Unable to remove triggers "
3974 "(capture request %d, HAL device: %s (%d)",
3975 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3976 cleanUpFailedRequests(/*sendRequestError*/ false);
3977 return false;
3978 }
3979
3980 if (res != OK) {
3981 // Should only get a failure here for malformed requests or device-level
3982 // errors, so consider all errors fatal. Bad metadata failures should
3983 // come through notify.
3984 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3985 mNextRequests[numRequestProcessed].halRequest.frame_number,
3986 strerror(-res), res);
3987 cleanUpFailedRequests(/*sendRequestError*/ false);
3988 return false;
3989 }
3990 return true;
3991}
3992
3993bool Camera3Device::RequestThread::sendRequestsOneByOne() {
3994 status_t res;
3995
3996 for (auto& nextRequest : mNextRequests) {
3997 // Submit request and block until ready for next one
3998 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3999 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4000
4001 if (res != OK) {
4002 // Should only get a failure here for malformed requests or device-level
4003 // errors, so consider all errors fatal. Bad metadata failures should
4004 // come through notify.
4005 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4006 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4007 res);
4008 cleanUpFailedRequests(/*sendRequestError*/ false);
4009 return false;
4010 }
4011
4012 // Mark that the request has be submitted successfully.
4013 nextRequest.submitted = true;
4014
4015 // Update the latest request sent to HAL
4016 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4017 Mutex::Autolock al(mLatestRequestMutex);
4018
4019 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4020 mLatestRequest.acquire(cloned);
4021
4022 sp<Camera3Device> parent = mParent.promote();
4023 if (parent != NULL) {
4024 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4025 0, mLatestRequest);
4026 }
4027 }
4028
4029 if (nextRequest.halRequest.settings != NULL) {
4030 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
4031 }
4032
4033 // Remove any previously queued triggers (after unlock)
4034 res = removeTriggers(mPrevRequest);
4035 if (res != OK) {
4036 SET_ERR("RequestThread: Unable to remove triggers "
4037 "(capture request %d, HAL device: %s (%d)",
4038 nextRequest.halRequest.frame_number, strerror(-res), res);
4039 cleanUpFailedRequests(/*sendRequestError*/ false);
4040 return false;
4041 }
4042 }
4043 return true;
4044}
4045
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004046nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4047 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4048 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4049 find_camera_metadata_ro_entry(request,
4050 ANDROID_CONTROL_AE_MODE,
4051 &e);
4052 if (e.count == 0) return maxExpectedDuration;
4053
4054 switch (e.data.u8[0]) {
4055 case ANDROID_CONTROL_AE_MODE_OFF:
4056 find_camera_metadata_ro_entry(request,
4057 ANDROID_SENSOR_EXPOSURE_TIME,
4058 &e);
4059 if (e.count > 0) {
4060 maxExpectedDuration = e.data.i64[0];
4061 }
4062 find_camera_metadata_ro_entry(request,
4063 ANDROID_SENSOR_FRAME_DURATION,
4064 &e);
4065 if (e.count > 0) {
4066 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4067 }
4068 break;
4069 default:
4070 find_camera_metadata_ro_entry(request,
4071 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4072 &e);
4073 if (e.count > 1) {
4074 maxExpectedDuration = 1e9 / e.data.u8[0];
4075 }
4076 break;
4077 }
4078
4079 return maxExpectedDuration;
4080}
4081
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004082bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004083 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004084 status_t res;
4085
4086 // Handle paused state.
4087 if (waitIfPaused()) {
4088 return true;
4089 }
4090
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004091 // Wait for the next batch of requests.
4092 waitForNextRequestBatch();
4093 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004094 return true;
4095 }
4096
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004097 // Get the latest request ID, if any
4098 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004099 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004100 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004101 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004102 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004103 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004104 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4105 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004106 }
4107
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004108 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004109 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004110 if (res == TIMED_OUT) {
4111 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004112 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004113 // Check if any stream is abandoned.
4114 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004115 return true;
4116 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004117 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004118 return false;
4119 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004120
Zhijun Hecc27e112013-10-03 16:12:43 -07004121 // Inform waitUntilRequestProcessed thread of a new request ID
4122 {
4123 Mutex::Autolock al(mLatestRequestMutex);
4124
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004125 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004126 mLatestRequestSignal.signal();
4127 }
4128
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004129 // Submit a batch of requests to HAL.
4130 // Use flush lock only when submitting multilple requests in a batch.
4131 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4132 // which may take a long time to finish so synchronizing flush() and
4133 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4134 // For now, only synchronize for high speed recording and we should figure something out for
4135 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004136 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004137
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004138 if (useFlushLock) {
4139 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004140 }
4141
Zhijun Hef0645c12016-08-02 00:58:11 -07004142 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004143 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004144
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004145 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004146 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004147 if (mInterface->supportBatchRequest()) {
4148 submitRequestSuccess = sendRequestsBatch();
4149 } else {
4150 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004151 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004152 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4153 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004154
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004155 if (useFlushLock) {
4156 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004157 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004158
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004159 // Unset as current request
4160 {
4161 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004162 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004163 }
4164
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004165 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004166}
4167
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004168status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004169 ATRACE_CALL();
4170
Shuzhen Wang4a472662017-02-26 23:29:04 -08004171 for (size_t i = 0; i < mNextRequests.size(); i++) {
4172 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004173 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4174 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4175 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4176
4177 // Prepare a request to HAL
4178 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4179
4180 // Insert any queued triggers (before metadata is locked)
4181 status_t res = insertTriggers(captureRequest);
4182
4183 if (res < 0) {
4184 SET_ERR("RequestThread: Unable to insert triggers "
4185 "(capture request %d, HAL device: %s (%d)",
4186 halRequest->frame_number, strerror(-res), res);
4187 return INVALID_OPERATION;
4188 }
4189 int triggerCount = res;
4190 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4191 mPrevTriggers = triggerCount;
4192
4193 // If the request is the same as last, or we had triggers last time
4194 if (mPrevRequest != captureRequest || triggersMixedIn) {
4195 /**
4196 * HAL workaround:
4197 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4198 */
4199 res = addDummyTriggerIds(captureRequest);
4200 if (res != OK) {
4201 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4202 "(capture request %d, HAL device: %s (%d)",
4203 halRequest->frame_number, strerror(-res), res);
4204 return INVALID_OPERATION;
4205 }
4206
4207 /**
4208 * The request should be presorted so accesses in HAL
4209 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4210 */
4211 captureRequest->mSettings.sort();
4212 halRequest->settings = captureRequest->mSettings.getAndLock();
4213 mPrevRequest = captureRequest;
4214 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4215
4216 IF_ALOGV() {
4217 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4218 find_camera_metadata_ro_entry(
4219 halRequest->settings,
4220 ANDROID_CONTROL_AF_TRIGGER,
4221 &e
4222 );
4223 if (e.count > 0) {
4224 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4225 __FUNCTION__,
4226 halRequest->frame_number,
4227 e.data.u8[0]);
4228 }
4229 }
4230 } else {
4231 // leave request.settings NULL to indicate 'reuse latest given'
4232 ALOGVV("%s: Request settings are REUSED",
4233 __FUNCTION__);
4234 }
4235
4236 uint32_t totalNumBuffers = 0;
4237
4238 // Fill in buffers
4239 if (captureRequest->mInputStream != NULL) {
4240 halRequest->input_buffer = &captureRequest->mInputBuffer;
4241 totalNumBuffers += 1;
4242 } else {
4243 halRequest->input_buffer = NULL;
4244 }
4245
4246 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4247 captureRequest->mOutputStreams.size());
4248 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004249 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4250 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004251
4252 // Prepare video buffers for high speed recording on the first video request.
4253 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4254 // Only try to prepare video stream on the first video request.
4255 mPrepareVideoStream = false;
4256
4257 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4258 while (res == NOT_ENOUGH_DATA) {
4259 res = outputStream->prepareNextBuffer();
4260 }
4261 if (res != OK) {
4262 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4263 __FUNCTION__, strerror(-res), res);
4264 outputStream->cancelPrepare();
4265 }
4266 }
4267
Shuzhen Wang4a472662017-02-26 23:29:04 -08004268 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4269 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004270 if (res != OK) {
4271 // Can't get output buffer from gralloc queue - this could be due to
4272 // abandoned queue or other consumer misbehavior, so not a fatal
4273 // error
4274 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4275 " %s (%d)", strerror(-res), res);
4276
4277 return TIMED_OUT;
4278 }
4279 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004280
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004281 }
4282 totalNumBuffers += halRequest->num_output_buffers;
4283
4284 // Log request in the in-flight queue
4285 sp<Camera3Device> parent = mParent.promote();
4286 if (parent == NULL) {
4287 // Should not happen, and nowhere to send errors to, so just log it
4288 CLOGE("RequestThread: Parent is gone");
4289 return INVALID_OPERATION;
4290 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004291
4292 // If this request list is for constrained high speed recording (not
4293 // preview), and the current request is not the last one in the batch,
4294 // do not send callback to the app.
4295 bool hasCallback = true;
4296 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4297 hasCallback = false;
4298 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004299 res = parent->registerInFlight(halRequest->frame_number,
4300 totalNumBuffers, captureRequest->mResultExtras,
4301 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004302 hasCallback,
4303 calculateMaxExpectedDuration(halRequest->settings));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004304 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4305 ", burstId = %" PRId32 ".",
4306 __FUNCTION__,
4307 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4308 captureRequest->mResultExtras.burstId);
4309 if (res != OK) {
4310 SET_ERR("RequestThread: Unable to register new in-flight request:"
4311 " %s (%d)", strerror(-res), res);
4312 return INVALID_OPERATION;
4313 }
4314 }
4315
4316 return OK;
4317}
4318
Igor Murashkin1e479c02013-09-06 16:55:14 -07004319CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004320 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004321 Mutex::Autolock al(mLatestRequestMutex);
4322
4323 ALOGV("RequestThread::%s", __FUNCTION__);
4324
4325 return mLatestRequest;
4326}
4327
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004328bool Camera3Device::RequestThread::isStreamPending(
4329 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004330 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004331 Mutex::Autolock l(mRequestLock);
4332
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004333 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004334 if (!nextRequest.submitted) {
4335 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4336 if (stream == s) return true;
4337 }
4338 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004339 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004340 }
4341
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004342 for (const auto& request : mRequestQueue) {
4343 for (const auto& s : request->mOutputStreams) {
4344 if (stream == s) return true;
4345 }
4346 if (stream == request->mInputStream) return true;
4347 }
4348
4349 for (const auto& request : mRepeatingRequests) {
4350 for (const auto& s : request->mOutputStreams) {
4351 if (stream == s) return true;
4352 }
4353 if (stream == request->mInputStream) return true;
4354 }
4355
4356 return false;
4357}
Jianing Weicb0652e2014-03-12 18:29:36 -07004358
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004359nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004360 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004361 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004362 return mExpectedInflightDuration > kMinInflightDuration ?
4363 mExpectedInflightDuration : kMinInflightDuration;
4364}
4365
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004366void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4367 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004368 return;
4369 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004370
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004371 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004372 // Skip the ones that have been submitted successfully.
4373 if (nextRequest.submitted) {
4374 continue;
4375 }
4376
4377 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4378 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4379 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4380
4381 if (halRequest->settings != NULL) {
4382 captureRequest->mSettings.unlock(halRequest->settings);
4383 }
4384
4385 if (captureRequest->mInputStream != NULL) {
4386 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4387 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4388 }
4389
4390 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004391 //Buffers that failed processing could still have
4392 //valid acquire fence.
4393 int acquireFence = (*outputBuffers)[i].acquire_fence;
4394 if (0 <= acquireFence) {
4395 close(acquireFence);
4396 outputBuffers->editItemAt(i).acquire_fence = -1;
4397 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004398 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4399 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4400 }
4401
4402 if (sendRequestError) {
4403 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004404 sp<NotificationListener> listener = mListener.promote();
4405 if (listener != NULL) {
4406 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004407 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004408 captureRequest->mResultExtras);
4409 }
4410 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004411
4412 // Remove yet-to-be submitted inflight request from inflightMap
4413 {
4414 sp<Camera3Device> parent = mParent.promote();
4415 if (parent != NULL) {
4416 Mutex::Autolock l(parent->mInFlightLock);
4417 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4418 if (idx >= 0) {
4419 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4420 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4421 parent->removeInFlightMapEntryLocked(idx);
4422 }
4423 }
4424 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004425 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004426
4427 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004428 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004429}
4430
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004431void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004432 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004433 // Optimized a bit for the simple steady-state case (single repeating
4434 // request), to avoid putting that request in the queue temporarily.
4435 Mutex::Autolock l(mRequestLock);
4436
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004437 assert(mNextRequests.empty());
4438
4439 NextRequest nextRequest;
4440 nextRequest.captureRequest = waitForNextRequestLocked();
4441 if (nextRequest.captureRequest == nullptr) {
4442 return;
4443 }
4444
4445 nextRequest.halRequest = camera3_capture_request_t();
4446 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004447 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004448
4449 // Wait for additional requests
4450 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4451
4452 for (size_t i = 1; i < batchSize; i++) {
4453 NextRequest additionalRequest;
4454 additionalRequest.captureRequest = waitForNextRequestLocked();
4455 if (additionalRequest.captureRequest == nullptr) {
4456 break;
4457 }
4458
4459 additionalRequest.halRequest = camera3_capture_request_t();
4460 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004461 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004462 }
4463
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004464 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004465 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004466 mNextRequests.size(), batchSize);
4467 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004468 }
4469
4470 return;
4471}
4472
4473sp<Camera3Device::CaptureRequest>
4474 Camera3Device::RequestThread::waitForNextRequestLocked() {
4475 status_t res;
4476 sp<CaptureRequest> nextRequest;
4477
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004478 while (mRequestQueue.empty()) {
4479 if (!mRepeatingRequests.empty()) {
4480 // Always atomically enqueue all requests in a repeating request
4481 // list. Guarantees a complete in-sequence set of captures to
4482 // application.
4483 const RequestList &requests = mRepeatingRequests;
4484 RequestList::const_iterator firstRequest =
4485 requests.begin();
4486 nextRequest = *firstRequest;
4487 mRequestQueue.insert(mRequestQueue.end(),
4488 ++firstRequest,
4489 requests.end());
4490 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004491
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004492 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004493
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004494 break;
4495 }
4496
4497 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4498
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004499 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4500 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004501 Mutex::Autolock pl(mPauseLock);
4502 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004503 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004504 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004505 // Let the tracker know
4506 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4507 if (statusTracker != 0) {
4508 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4509 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004510 }
4511 // Stop waiting for now and let thread management happen
4512 return NULL;
4513 }
4514 }
4515
4516 if (nextRequest == NULL) {
4517 // Don't have a repeating request already in hand, so queue
4518 // must have an entry now.
4519 RequestList::iterator firstRequest =
4520 mRequestQueue.begin();
4521 nextRequest = *firstRequest;
4522 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004523 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4524 sp<NotificationListener> listener = mListener.promote();
4525 if (listener != NULL) {
4526 listener->notifyRequestQueueEmpty();
4527 }
4528 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004529 }
4530
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004531 // In case we've been unpaused by setPaused clearing mDoPause, need to
4532 // update internal pause state (capture/setRepeatingRequest unpause
4533 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004534 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004535 if (mPaused) {
4536 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4537 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4538 if (statusTracker != 0) {
4539 statusTracker->markComponentActive(mStatusId);
4540 }
4541 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004542 mPaused = false;
4543
4544 // Check if we've reconfigured since last time, and reset the preview
4545 // request if so. Can't use 'NULL request == repeat' across configure calls.
4546 if (mReconfigured) {
4547 mPrevRequest.clear();
4548 mReconfigured = false;
4549 }
4550
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004551 if (nextRequest != NULL) {
4552 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004553 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4554 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004555
4556 // Since RequestThread::clear() removes buffers from the input stream,
4557 // get the right buffer here before unlocking mRequestLock
4558 if (nextRequest->mInputStream != NULL) {
4559 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4560 if (res != OK) {
4561 // Can't get input buffer from gralloc queue - this could be due to
4562 // disconnected queue or other producer misbehavior, so not a fatal
4563 // error
4564 ALOGE("%s: Can't get input buffer, skipping request:"
4565 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004566
4567 sp<NotificationListener> listener = mListener.promote();
4568 if (listener != NULL) {
4569 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004570 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004571 nextRequest->mResultExtras);
4572 }
4573 return NULL;
4574 }
4575 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004576 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004577
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004578 return nextRequest;
4579}
4580
4581bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004582 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004583 status_t res;
4584 Mutex::Autolock l(mPauseLock);
4585 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004586 if (mPaused == false) {
4587 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004588 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4589 // Let the tracker know
4590 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4591 if (statusTracker != 0) {
4592 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4593 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004594 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004595
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004596 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004597 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004598 return true;
4599 }
4600 }
4601 // We don't set mPaused to false here, because waitForNextRequest needs
4602 // to further manage the paused state in case of starvation.
4603 return false;
4604}
4605
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004606void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004607 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004608 // With work to do, mark thread as unpaused.
4609 // If paused by request (setPaused), don't resume, to avoid
4610 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004611 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004612 Mutex::Autolock p(mPauseLock);
4613 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004614 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4615 if (mPaused) {
4616 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4617 if (statusTracker != 0) {
4618 statusTracker->markComponentActive(mStatusId);
4619 }
4620 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004621 mPaused = false;
4622 }
4623}
4624
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004625void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4626 sp<Camera3Device> parent = mParent.promote();
4627 if (parent != NULL) {
4628 va_list args;
4629 va_start(args, fmt);
4630
4631 parent->setErrorStateV(fmt, args);
4632
4633 va_end(args);
4634 }
4635}
4636
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004637status_t Camera3Device::RequestThread::insertTriggers(
4638 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004639 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004640 Mutex::Autolock al(mTriggerMutex);
4641
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004642 sp<Camera3Device> parent = mParent.promote();
4643 if (parent == NULL) {
4644 CLOGE("RequestThread: Parent is gone");
4645 return DEAD_OBJECT;
4646 }
4647
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004648 CameraMetadata &metadata = request->mSettings;
4649 size_t count = mTriggerMap.size();
4650
4651 for (size_t i = 0; i < count; ++i) {
4652 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004653 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004654
4655 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4656 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4657 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004658 if (isAeTrigger) {
4659 request->mResultExtras.precaptureTriggerId = triggerId;
4660 mCurrentPreCaptureTriggerId = triggerId;
4661 } else {
4662 request->mResultExtras.afTriggerId = triggerId;
4663 mCurrentAfTriggerId = triggerId;
4664 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004665 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004666 }
4667
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004668 camera_metadata_entry entry = metadata.find(tag);
4669
4670 if (entry.count > 0) {
4671 /**
4672 * Already has an entry for this trigger in the request.
4673 * Rewrite it with our requested trigger value.
4674 */
4675 RequestTrigger oldTrigger = trigger;
4676
4677 oldTrigger.entryValue = entry.data.u8[0];
4678
4679 mTriggerReplacedMap.add(tag, oldTrigger);
4680 } else {
4681 /**
4682 * More typical, no trigger entry, so we just add it
4683 */
4684 mTriggerRemovedMap.add(tag, trigger);
4685 }
4686
4687 status_t res;
4688
4689 switch (trigger.getTagType()) {
4690 case TYPE_BYTE: {
4691 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4692 res = metadata.update(tag,
4693 &entryValue,
4694 /*count*/1);
4695 break;
4696 }
4697 case TYPE_INT32:
4698 res = metadata.update(tag,
4699 &trigger.entryValue,
4700 /*count*/1);
4701 break;
4702 default:
4703 ALOGE("%s: Type not supported: 0x%x",
4704 __FUNCTION__,
4705 trigger.getTagType());
4706 return INVALID_OPERATION;
4707 }
4708
4709 if (res != OK) {
4710 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4711 ", value %d", __FUNCTION__, trigger.getTagName(),
4712 trigger.entryValue);
4713 return res;
4714 }
4715
4716 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4717 trigger.getTagName(),
4718 trigger.entryValue);
4719 }
4720
4721 mTriggerMap.clear();
4722
4723 return count;
4724}
4725
4726status_t Camera3Device::RequestThread::removeTriggers(
4727 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004728 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004729 Mutex::Autolock al(mTriggerMutex);
4730
4731 CameraMetadata &metadata = request->mSettings;
4732
4733 /**
4734 * Replace all old entries with their old values.
4735 */
4736 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4737 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4738
4739 status_t res;
4740
4741 uint32_t tag = trigger.metadataTag;
4742 switch (trigger.getTagType()) {
4743 case TYPE_BYTE: {
4744 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4745 res = metadata.update(tag,
4746 &entryValue,
4747 /*count*/1);
4748 break;
4749 }
4750 case TYPE_INT32:
4751 res = metadata.update(tag,
4752 &trigger.entryValue,
4753 /*count*/1);
4754 break;
4755 default:
4756 ALOGE("%s: Type not supported: 0x%x",
4757 __FUNCTION__,
4758 trigger.getTagType());
4759 return INVALID_OPERATION;
4760 }
4761
4762 if (res != OK) {
4763 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4764 ", trigger value %d", __FUNCTION__,
4765 trigger.getTagName(), trigger.entryValue);
4766 return res;
4767 }
4768 }
4769 mTriggerReplacedMap.clear();
4770
4771 /**
4772 * Remove all new entries.
4773 */
4774 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4775 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4776 status_t res = metadata.erase(trigger.metadataTag);
4777
4778 if (res != OK) {
4779 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4780 ", trigger value %d", __FUNCTION__,
4781 trigger.getTagName(), trigger.entryValue);
4782 return res;
4783 }
4784 }
4785 mTriggerRemovedMap.clear();
4786
4787 return OK;
4788}
4789
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004790status_t Camera3Device::RequestThread::addDummyTriggerIds(
4791 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004792 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004793 static const int32_t dummyTriggerId = 1;
4794 status_t res;
4795
4796 CameraMetadata &metadata = request->mSettings;
4797
4798 // If AF trigger is active, insert a dummy AF trigger ID if none already
4799 // exists
4800 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4801 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4802 if (afTrigger.count > 0 &&
4803 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4804 afId.count == 0) {
4805 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4806 if (res != OK) return res;
4807 }
4808
4809 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4810 // if none already exists
4811 camera_metadata_entry pcTrigger =
4812 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4813 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4814 if (pcTrigger.count > 0 &&
4815 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4816 pcId.count == 0) {
4817 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4818 &dummyTriggerId, 1);
4819 if (res != OK) return res;
4820 }
4821
4822 return OK;
4823}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004824
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004825/**
4826 * PreparerThread inner class methods
4827 */
4828
4829Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004830 Thread(/*canCallJava*/false), mListener(nullptr),
4831 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004832}
4833
4834Camera3Device::PreparerThread::~PreparerThread() {
4835 Thread::requestExitAndWait();
4836 if (mCurrentStream != nullptr) {
4837 mCurrentStream->cancelPrepare();
4838 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4839 mCurrentStream.clear();
4840 }
4841 clear();
4842}
4843
Ruben Brunkc78ac262015-08-13 17:58:46 -07004844status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004845 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004846 status_t res;
4847
4848 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004849 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004850
Ruben Brunkc78ac262015-08-13 17:58:46 -07004851 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004852 if (res == OK) {
4853 // No preparation needed, fire listener right off
4854 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004855 if (listener != NULL) {
4856 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004857 }
4858 return OK;
4859 } else if (res != NOT_ENOUGH_DATA) {
4860 return res;
4861 }
4862
4863 // Need to prepare, start up thread if necessary
4864 if (!mActive) {
4865 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4866 // isn't running
4867 Thread::requestExitAndWait();
4868 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4869 if (res != OK) {
4870 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004871 if (listener != NULL) {
4872 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004873 }
4874 return res;
4875 }
4876 mCancelNow = false;
4877 mActive = true;
4878 ALOGV("%s: Preparer stream started", __FUNCTION__);
4879 }
4880
4881 // queue up the work
4882 mPendingStreams.push_back(stream);
4883 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4884
4885 return OK;
4886}
4887
4888status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004889 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004890 Mutex::Autolock l(mLock);
4891
4892 for (const auto& stream : mPendingStreams) {
4893 stream->cancelPrepare();
4894 }
4895 mPendingStreams.clear();
4896 mCancelNow = true;
4897
4898 return OK;
4899}
4900
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004901void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004902 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004903 Mutex::Autolock l(mLock);
4904 mListener = listener;
4905}
4906
4907bool Camera3Device::PreparerThread::threadLoop() {
4908 status_t res;
4909 {
4910 Mutex::Autolock l(mLock);
4911 if (mCurrentStream == nullptr) {
4912 // End thread if done with work
4913 if (mPendingStreams.empty()) {
4914 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4915 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4916 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4917 mActive = false;
4918 return false;
4919 }
4920
4921 // Get next stream to prepare
4922 auto it = mPendingStreams.begin();
4923 mCurrentStream = *it;
4924 mPendingStreams.erase(it);
4925 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4926 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4927 } else if (mCancelNow) {
4928 mCurrentStream->cancelPrepare();
4929 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4930 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4931 mCurrentStream.clear();
4932 mCancelNow = false;
4933 return true;
4934 }
4935 }
4936
4937 res = mCurrentStream->prepareNextBuffer();
4938 if (res == NOT_ENOUGH_DATA) return true;
4939 if (res != OK) {
4940 // Something bad happened; try to recover by cancelling prepare and
4941 // signalling listener anyway
4942 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4943 mCurrentStream->getId(), res, strerror(-res));
4944 mCurrentStream->cancelPrepare();
4945 }
4946
4947 // This stream has finished, notify listener
4948 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004949 sp<NotificationListener> listener = mListener.promote();
4950 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004951 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4952 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004953 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004954 }
4955
4956 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4957 mCurrentStream.clear();
4958
4959 return true;
4960}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004961
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004962/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004963 * Static callback forwarding methods from HAL to instance
4964 */
4965
4966void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4967 const camera3_capture_result *result) {
4968 Camera3Device *d =
4969 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004970
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004971 d->processCaptureResult(result);
4972}
4973
4974void Camera3Device::sNotify(const camera3_callback_ops *cb,
4975 const camera3_notify_msg *msg) {
4976 Camera3Device *d =
4977 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4978 d->notify(msg);
4979}
4980
4981}; // namespace android