blob: e8b9b205b4a330038b83c8ee588c2b82795571c3 [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 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700141 auto resultQueueRet = session->getCaptureResultMetadataQueue(
142 [&queue = mResultMetadataQueue](const auto& descriptor) {
143 queue = std::make_unique<ResultMetadataQueue>(descriptor);
144 if (!queue->isValid() || queue->availableToWrite() <= 0) {
145 ALOGE("HAL returns empty result metadata fmq, not use it");
146 queue = nullptr;
147 // Don't use the queue onwards.
148 }
149 });
150 if (!resultQueueRet.isOk()) {
151 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
152 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700153 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700154 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700155
Yifan Hongf79b5542017-04-11 14:44:25 -0700156 mInterface = std::make_unique<HalInterface>(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000157 std::string providerType;
158 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800159
160 return initializeCommonLocked();
161}
162
163status_t Camera3Device::initializeCommonLocked() {
164
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700165 /** Start up status tracker thread */
166 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800167 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700168 if (res != OK) {
169 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
170 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800171 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700172 mStatusTracker.clear();
173 return res;
174 }
175
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700176 /** Register in-flight map to the status tracker */
177 mInFlightStatusId = mStatusTracker->addComponent();
178
Zhijun He125684a2015-12-26 15:07:30 -0800179 /** Create buffer manager */
180 mBufferManager = new Camera3BufferManager();
181
Emilian Peev71c73a22017-03-21 16:35:51 +0000182 mTagMonitor.initialize(mVendorTagId);
183
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700184 /** Start up request queue thread */
Emilian Peev7e25e5e2017-04-07 15:48:49 +0100185 mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800186 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700188 SET_ERR_L("Unable to start request queue thread: %s (%d)",
189 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800190 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return res;
193 }
194
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700195 mPreparerThread = new PreparerThread();
196
Ruben Brunk183f0562015-08-12 12:55:02 -0700197 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800198 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700199 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700200 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800202
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800203 // Measure the clock domain offset between camera and video/hw_composer
204 camera_metadata_entry timestampSource =
205 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
206 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
207 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
208 mTimestampOffset = getMonoToBoottimeOffset();
209 }
210
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700211 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100212 camera_metadata_entry partialResultsCount =
213 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
214 if (partialResultsCount.count > 0) {
215 mNumPartialResults = partialResultsCount.data.i32[0];
216 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700217 }
218
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700219 camera_metadata_entry configs =
220 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
221 for (uint32_t i = 0; i < configs.count; i += 4) {
222 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
223 configs.data.i32[i + 3] ==
224 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
225 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
226 configs.data.i32[i + 2]));
227 }
228 }
229
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800230 return OK;
231}
232
233status_t Camera3Device::disconnect() {
234 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700235 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800236
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700237 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800238
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700241 {
242 Mutex::Autolock l(mLock);
243 if (mStatus == STATUS_UNINITIALIZED) return res;
244
245 if (mStatus == STATUS_ACTIVE ||
246 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
247 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700248 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700250 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700251 } else {
252 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
253 if (res != OK) {
254 SET_ERR_L("Timeout waiting for HAL to drain");
255 // Continue to close device even in case of error
256 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700257 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 if (mStatus == STATUS_ERROR) {
261 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700262 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263
264 if (mStatusTracker != NULL) {
265 mStatusTracker->requestExit();
266 }
267
268 if (mRequestThread != NULL) {
269 mRequestThread->requestExit();
270 }
271
272 mOutputStreams.clear();
273 mInputStream.clear();
274 }
275
276 // Joining done without holding mLock, otherwise deadlocks may ensue
277 // as the threads try to access parent state
278 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
279 // HAL may be in a bad state, so waiting for request thread
280 // (which may be stuck in the HAL processCaptureRequest call)
281 // could be dangerous.
282 mRequestThread->join();
283 }
284
285 if (mStatusTracker != NULL) {
286 mStatusTracker->join();
287 }
288
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800289 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290 {
291 Mutex::Autolock l(mLock);
292
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700294 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800295 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800296
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800297 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700298 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800299
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700300 // Call close without internal mutex held, as the HAL close may need to
301 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800302 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700303
304 {
305 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800306 mInterface->clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700307 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800309
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700310 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700311 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800312}
313
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314// For dumping/debugging only -
315// try to acquire a lock a few times, eventually give up to proceed with
316// debug/dump operations
317bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
318 bool gotLock = false;
319 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
320 if (lock.tryLock() == NO_ERROR) {
321 gotLock = true;
322 break;
323 } else {
324 usleep(kDumpSleepDuration);
325 }
326 }
327 return gotLock;
328}
329
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700330Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
331 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100332 const int STREAM_CONFIGURATION_SIZE = 4;
333 const int STREAM_FORMAT_OFFSET = 0;
334 const int STREAM_WIDTH_OFFSET = 1;
335 const int STREAM_HEIGHT_OFFSET = 2;
336 const int STREAM_IS_INPUT_OFFSET = 3;
337 camera_metadata_ro_entry_t availableStreamConfigs =
338 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
339 if (availableStreamConfigs.count == 0 ||
340 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
341 return Size(0, 0);
342 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700343
Emilian Peev08dd2452017-04-06 16:55:14 +0100344 // Get max jpeg size (area-wise).
345 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
346 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
347 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
348 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
349 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
350 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
351 && format == HAL_PIXEL_FORMAT_BLOB &&
352 (width * height > maxJpegWidth * maxJpegHeight)) {
353 maxJpegWidth = width;
354 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700355 }
356 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100357
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700358 return Size(maxJpegWidth, maxJpegHeight);
359}
360
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800361nsecs_t Camera3Device::getMonoToBoottimeOffset() {
362 // try three times to get the clock offset, choose the one
363 // with the minimum gap in measurements.
364 const int tries = 3;
365 nsecs_t bestGap, measured;
366 for (int i = 0; i < tries; ++i) {
367 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
368 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
369 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
370 const nsecs_t gap = tmono2 - tmono;
371 if (i == 0 || gap < bestGap) {
372 bestGap = gap;
373 measured = tbase - ((tmono + tmono2) >> 1);
374 }
375 }
376 return measured;
377}
378
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800379hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
380 int frameworkFormat) {
381 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
382}
383
384DataspaceFlags Camera3Device::mapToHidlDataspace(
385 android_dataspace dataSpace) {
386 return dataSpace;
387}
388
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700389BufferUsageFlags Camera3Device::mapToConsumerUsage(
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700390 uint32_t usage) {
391 return usage;
392}
393
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800394StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
395 switch (rotation) {
396 case CAMERA3_STREAM_ROTATION_0:
397 return StreamRotation::ROTATION_0;
398 case CAMERA3_STREAM_ROTATION_90:
399 return StreamRotation::ROTATION_90;
400 case CAMERA3_STREAM_ROTATION_180:
401 return StreamRotation::ROTATION_180;
402 case CAMERA3_STREAM_ROTATION_270:
403 return StreamRotation::ROTATION_270;
404 }
405 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
406 return StreamRotation::ROTATION_0;
407}
408
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800409status_t Camera3Device::mapToStreamConfigurationMode(
410 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
411 if (mode == nullptr) return BAD_VALUE;
412 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
413 switch(operationMode) {
414 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
415 *mode = StreamConfigurationMode::NORMAL_MODE;
416 break;
417 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
418 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
419 break;
420 default:
421 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
422 return BAD_VALUE;
423 }
424 } else {
425 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800426 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800427 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800428}
429
430camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
431 switch (status) {
432 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
433 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
434 }
435 return CAMERA3_BUFFER_STATUS_ERROR;
436}
437
438int Camera3Device::mapToFrameworkFormat(
439 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
440 return static_cast<uint32_t>(pixelFormat);
441}
442
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700443uint32_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700444 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700445 return usage;
446}
447
448uint32_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700449 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700450 return usage;
451}
452
Zhijun Hef7da0962014-04-24 13:27:56 -0700453ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700454 // Get max jpeg size (area-wise).
455 Size maxJpegResolution = getMaxJpegResolution();
456 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800457 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
458 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700459 return BAD_VALUE;
460 }
461
Zhijun Hef7da0962014-04-24 13:27:56 -0700462 // Get max jpeg buffer size
463 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700464 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
465 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800466 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
467 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700468 return BAD_VALUE;
469 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700470 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800471 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700472
473 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700474 float scaleFactor = ((float) (width * height)) /
475 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800476 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
477 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700478 if (jpegBufferSize > maxJpegBufferSize) {
479 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700480 }
481
482 return jpegBufferSize;
483}
484
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700485ssize_t Camera3Device::getPointCloudBufferSize() const {
486 const int FLOATS_PER_POINT=4;
487 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
488 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800489 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
490 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700491 return BAD_VALUE;
492 }
493 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
494 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
495 return maxBytesForPointCloud;
496}
497
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800498ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800499 const int PER_CONFIGURATION_SIZE = 3;
500 const int WIDTH_OFFSET = 0;
501 const int HEIGHT_OFFSET = 1;
502 const int SIZE_OFFSET = 2;
503 camera_metadata_ro_entry rawOpaqueSizes =
504 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800505 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800506 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800507 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
508 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800509 return BAD_VALUE;
510 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700511
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800512 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
513 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
514 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
515 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
516 }
517 }
518
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800519 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
520 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800521 return BAD_VALUE;
522}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700523
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
525 ATRACE_CALL();
526 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700527
528 // Try to lock, but continue in case of failure (to avoid blocking in
529 // deadlocks)
530 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
531 bool gotLock = tryLockSpinRightRound(mLock);
532
533 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800534 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
535 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700536 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800537 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
538 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700539
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800540 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700541
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800542 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700543 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800544 int n = args.size();
545 for (int i = 0; i < n; i++) {
546 if (args[i] == templatesOption) {
547 dumpTemplates = true;
548 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700549 if (args[i] == monitorOption) {
550 if (i + 1 < n) {
551 String8 monitorTags = String8(args[i + 1]);
552 if (monitorTags == "off") {
553 mTagMonitor.disableMonitoring();
554 } else {
555 mTagMonitor.parseTagsToMonitor(monitorTags);
556 }
557 } else {
558 mTagMonitor.disableMonitoring();
559 }
560 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800561 }
562
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800563 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800564
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 const char *status =
566 mStatus == STATUS_ERROR ? "ERROR" :
567 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700568 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
569 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800570 mStatus == STATUS_ACTIVE ? "ACTIVE" :
571 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700572
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700574 if (mStatus == STATUS_ERROR) {
575 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
576 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800577 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800578 const char *mode =
579 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
580 mOperatingMode == static_cast<int>(
581 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
582 "CUSTOM";
583 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800584
585 if (mInputStream != NULL) {
586 write(fd, lines.string(), lines.size());
587 mInputStream->dump(fd, args);
588 } else {
589 lines.appendFormat(" No input stream.\n");
590 write(fd, lines.string(), lines.size());
591 }
592 for (size_t i = 0; i < mOutputStreams.size(); i++) {
593 mOutputStreams[i]->dump(fd,args);
594 }
595
Zhijun He431503c2016-03-07 17:30:16 -0800596 if (mBufferManager != NULL) {
597 lines = String8(" Camera3 Buffer Manager:\n");
598 write(fd, lines.string(), lines.size());
599 mBufferManager->dump(fd, args);
600 }
Zhijun He125684a2015-12-26 15:07:30 -0800601
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700602 lines = String8(" In-flight requests:\n");
603 if (mInFlightMap.size() == 0) {
604 lines.append(" None\n");
605 } else {
606 for (size_t i = 0; i < mInFlightMap.size(); i++) {
607 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700608 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700609 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800610 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700611 r.numBuffersLeft);
612 }
613 }
614 write(fd, lines.string(), lines.size());
615
Shuzhen Wang686f6442017-06-20 16:16:04 -0700616 if (mRequestThread != NULL) {
617 mRequestThread->dumpCaptureRequestLatency(fd,
618 " ProcessCaptureRequest latency histogram:");
619 }
620
Igor Murashkin1e479c02013-09-06 16:55:14 -0700621 {
622 lines = String8(" Last request sent:\n");
623 write(fd, lines.string(), lines.size());
624
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700625 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700626 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
627 }
628
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800629 if (dumpTemplates) {
630 const char *templateNames[] = {
631 "TEMPLATE_PREVIEW",
632 "TEMPLATE_STILL_CAPTURE",
633 "TEMPLATE_VIDEO_RECORD",
634 "TEMPLATE_VIDEO_SNAPSHOT",
635 "TEMPLATE_ZERO_SHUTTER_LAG",
636 "TEMPLATE_MANUAL"
637 };
638
639 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800640 camera_metadata_t *templateRequest = nullptr;
641 mInterface->constructDefaultRequestSettings(
642 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800643 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800644 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800645 lines.append(" Not supported\n");
646 write(fd, lines.string(), lines.size());
647 } else {
648 write(fd, lines.string(), lines.size());
649 dump_indented_camera_metadata(templateRequest,
650 fd, /*verbosity*/2, /*indentation*/8);
651 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800652 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800653 }
654 }
655
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700656 mTagMonitor.dumpMonitoredMetadata(fd);
657
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800658 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800659 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800661 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800663
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 if (gotLock) mLock.unlock();
665 if (gotInterfaceLock) mInterfaceLock.unlock();
666
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667 return OK;
668}
669
670const CameraMetadata& Camera3Device::info() const {
671 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
673 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700674 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800675 mStatus == STATUS_ERROR ?
676 "when in error state" : "before init");
677 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800678 return mDeviceInfo;
679}
680
Jianing Wei90e59c92014-03-12 18:29:36 -0700681status_t Camera3Device::checkStatusOkToCaptureLocked() {
682 switch (mStatus) {
683 case STATUS_ERROR:
684 CLOGE("Device has encountered a serious error");
685 return INVALID_OPERATION;
686 case STATUS_UNINITIALIZED:
687 CLOGE("Device not initialized");
688 return INVALID_OPERATION;
689 case STATUS_UNCONFIGURED:
690 case STATUS_CONFIGURED:
691 case STATUS_ACTIVE:
692 // OK
693 break;
694 default:
695 SET_ERR_L("Unexpected status: %d", mStatus);
696 return INVALID_OPERATION;
697 }
698 return OK;
699}
700
701status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700702 const List<const CameraMetadata> &metadataList,
703 const std::list<const SurfaceMap> &surfaceMaps,
704 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700705 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700706 if (requestList == NULL) {
707 CLOGE("requestList cannot be NULL.");
708 return BAD_VALUE;
709 }
710
Jianing Weicb0652e2014-03-12 18:29:36 -0700711 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700712 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
713 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
714 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
715 ++metadataIt, ++surfaceMapIt) {
716 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700717 if (newRequest == 0) {
718 CLOGE("Can't create capture request");
719 return BAD_VALUE;
720 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700721
Shuzhen Wang9d066012016-09-30 11:30:20 -0700722 newRequest->mRepeating = repeating;
723
Jianing Weicb0652e2014-03-12 18:29:36 -0700724 // Setup burst Id and request Id
725 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700726 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
727 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700728 CLOGE("RequestID entry exists; but must not be empty in metadata");
729 return BAD_VALUE;
730 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700731 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700732 } else {
733 CLOGE("RequestID does not exist in metadata");
734 return BAD_VALUE;
735 }
736
Jianing Wei90e59c92014-03-12 18:29:36 -0700737 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700738
739 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700740 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700741 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
742 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
743 return BAD_VALUE;
744 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700745
746 // Setup batch size if this is a high speed video recording request.
747 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
748 auto firstRequest = requestList->begin();
749 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
750 if (outputStream->isVideoStream()) {
751 (*firstRequest)->mBatchSize = requestList->size();
752 break;
753 }
754 }
755 }
756
Jianing Wei90e59c92014-03-12 18:29:36 -0700757 return OK;
758}
759
Jianing Weicb0652e2014-03-12 18:29:36 -0700760status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800761 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800762
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700763 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700764 std::list<const SurfaceMap> surfaceMaps;
765 convertToRequestList(requests, surfaceMaps, request);
766
767 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
768}
769
770void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
771 std::list<const SurfaceMap>& surfaceMaps,
772 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700773 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700774
775 SurfaceMap surfaceMap;
776 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
777 // With no surface list passed in, stream and surface will have 1-to-1
778 // mapping. So the surface index is 0 for each stream in the surfaceMap.
779 for (size_t i = 0; i < streams.count; i++) {
780 surfaceMap[streams.data.i32[i]].push_back(0);
781 }
782 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783}
784
Jianing Wei90e59c92014-03-12 18:29:36 -0700785status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700786 const List<const CameraMetadata> &requests,
787 const std::list<const SurfaceMap> &surfaceMaps,
788 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700789 /*out*/
790 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700791 ATRACE_CALL();
792 Mutex::Autolock il(mInterfaceLock);
793 Mutex::Autolock l(mLock);
794
795 status_t res = checkStatusOkToCaptureLocked();
796 if (res != OK) {
797 // error logged by previous call
798 return res;
799 }
800
801 RequestList requestList;
802
Shuzhen Wang0129d522016-10-30 22:43:41 -0700803 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
804 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700805 if (res != OK) {
806 // error logged by previous call
807 return res;
808 }
809
810 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700811 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700812 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700813 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700814 }
815
816 if (res == OK) {
817 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
818 if (res != OK) {
819 SET_ERR_L("Can't transition to active in %f seconds!",
820 kActiveTimeout/1e9);
821 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800822 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700823 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700824 } else {
825 CLOGE("Cannot queue request. Impossible.");
826 return BAD_VALUE;
827 }
828
829 return res;
830}
831
Yifan Honga640c5a2017-04-12 16:30:31 -0700832// Only one processCaptureResult should be called at a time, so
833// the locks won't block. The locks are present here simply to enforce this.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800834hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800835 const hardware::hidl_vec<
836 hardware::camera::device::V3_2::CaptureResult>& results) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700837
838 if (mProcessCaptureResultLock.tryLock() != OK) {
839 // This should never happen; it indicates a wrong client implementation
840 // that doesn't follow the contract. But, we can be tolerant here.
841 ALOGE("%s: callback overlapped! waiting 1s...",
842 __FUNCTION__);
843 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
844 ALOGE("%s: cannot acquire lock in 1s, dropping results",
845 __FUNCTION__);
846 // really don't know what to do, so bail out.
847 return hardware::Void();
848 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800849 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700850 for (const auto& result : results) {
851 processOneCaptureResultLocked(result);
852 }
853 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800854 return hardware::Void();
855}
856
Yifan Honga640c5a2017-04-12 16:30:31 -0700857void Camera3Device::processOneCaptureResultLocked(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800858 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800859 camera3_capture_result r;
860 status_t res;
861 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700862
863 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
864 if (result.fmqResultSize > 0) {
865 resultMetadata.resize(result.fmqResultSize);
866 if (mResultMetadataQueue == nullptr) {
867 return; // logged in initialize()
868 }
869 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
870 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
871 __FUNCTION__, result.frameNumber, result.fmqResultSize);
872 return;
873 }
874 } else {
875 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
876 result.result.size());
877 }
878
879 if (resultMetadata.size() != 0) {
880 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
881 size_t expected_metadata_size = resultMetadata.size();
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800882 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
883 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
884 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800885 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800886 }
887 } else {
888 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800889 }
890
891 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
892 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
893 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
894 auto& bDst = outputBuffers[i];
895 const StreamBuffer &bSrc = result.outputBuffers[i];
896
897 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100898 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800899 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
900 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800901 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800902 }
903 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
904
905 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800906 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800907 if (res != OK) {
908 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
909 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800910 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800911 }
912 bDst.buffer = buffer;
913 bDst.status = mapHidlBufferStatus(bSrc.status);
914 bDst.acquire_fence = -1;
915 if (bSrc.releaseFence == nullptr) {
916 bDst.release_fence = -1;
917 } else if (bSrc.releaseFence->numFds == 1) {
918 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
919 } else {
920 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
921 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800922 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800923 }
924 }
925 r.num_output_buffers = outputBuffers.size();
926 r.output_buffers = outputBuffers.data();
927
928 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800929 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800930 r.input_buffer = nullptr;
931 } else {
932 if (mInputStream->getId() != result.inputBuffer.streamId) {
933 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
934 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800935 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800936 }
937 inputBuffer.stream = mInputStream->asHalStream();
938 buffer_handle_t *buffer;
939 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
940 &buffer);
941 if (res != OK) {
942 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
943 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800944 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800945 }
946 inputBuffer.buffer = buffer;
947 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
948 inputBuffer.acquire_fence = -1;
949 if (result.inputBuffer.releaseFence == nullptr) {
950 inputBuffer.release_fence = -1;
951 } else if (result.inputBuffer.releaseFence->numFds == 1) {
952 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
953 } else {
954 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
955 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800956 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800957 }
958 r.input_buffer = &inputBuffer;
959 }
960
961 r.partial_result = result.partialResult;
962
963 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800964}
965
966hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800967 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
968 for (const auto& msg : msgs) {
969 notify(msg);
970 }
971 return hardware::Void();
972}
973
974void Camera3Device::notify(
975 const hardware::camera::device::V3_2::NotifyMsg& msg) {
976
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800977 camera3_notify_msg m;
978 switch (msg.type) {
979 case MsgType::ERROR:
980 m.type = CAMERA3_MSG_ERROR;
981 m.message.error.frame_number = msg.msg.error.frameNumber;
982 if (msg.msg.error.errorStreamId >= 0) {
983 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100984 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800985 ALOGE("%s: Frame %d: Invalid error stream id %d",
986 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800987 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800988 }
989 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
990 } else {
991 m.message.error.error_stream = nullptr;
992 }
993 switch (msg.msg.error.errorCode) {
994 case ErrorCode::ERROR_DEVICE:
995 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
996 break;
997 case ErrorCode::ERROR_REQUEST:
998 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
999 break;
1000 case ErrorCode::ERROR_RESULT:
1001 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1002 break;
1003 case ErrorCode::ERROR_BUFFER:
1004 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1005 break;
1006 }
1007 break;
1008 case MsgType::SHUTTER:
1009 m.type = CAMERA3_MSG_SHUTTER;
1010 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1011 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1012 break;
1013 }
1014 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001015}
1016
Jianing Weicb0652e2014-03-12 18:29:36 -07001017status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001018 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001019 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001020 ATRACE_CALL();
1021
Shuzhen Wang0129d522016-10-30 22:43:41 -07001022 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001023}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001024
Jianing Weicb0652e2014-03-12 18:29:36 -07001025status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1026 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001027 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001028
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001029 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001030 std::list<const SurfaceMap> surfaceMaps;
1031 convertToRequestList(requests, surfaceMaps, request);
1032
1033 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1034 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035}
1036
Jianing Weicb0652e2014-03-12 18:29:36 -07001037status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001038 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001039 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001040 ATRACE_CALL();
1041
Shuzhen Wang0129d522016-10-30 22:43:41 -07001042 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001043}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044
1045sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001046 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 status_t res;
1048
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001049 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001050 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1051 // so unilaterally select normal operating mode.
1052 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001053 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001055 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001057 } else if (mStatus == STATUS_UNCONFIGURED) {
1058 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001059 CLOGE("No streams configured");
1060 return NULL;
1061 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 }
1063
Shuzhen Wang0129d522016-10-30 22:43:41 -07001064 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001065 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001066}
1067
Jianing Weicb0652e2014-03-12 18:29:36 -07001068status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001069 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001072
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 switch (mStatus) {
1074 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080 case STATUS_UNCONFIGURED:
1081 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082 case STATUS_ACTIVE:
1083 // OK
1084 break;
1085 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001086 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 return INVALID_OPERATION;
1088 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001089 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001090
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001091 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092}
1093
1094status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1095 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001096 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001097
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001098 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001099}
1100
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001101status_t Camera3Device::createInputStream(
1102 uint32_t width, uint32_t height, int format, int *id) {
1103 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001104 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001105 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001106 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1107 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001108
1109 status_t res;
1110 bool wasActive = false;
1111
1112 switch (mStatus) {
1113 case STATUS_ERROR:
1114 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1115 return INVALID_OPERATION;
1116 case STATUS_UNINITIALIZED:
1117 ALOGE("%s: Device not initialized", __FUNCTION__);
1118 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001119 case STATUS_UNCONFIGURED:
1120 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001121 // OK
1122 break;
1123 case STATUS_ACTIVE:
1124 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001125 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001126 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001127 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001128 return res;
1129 }
1130 wasActive = true;
1131 break;
1132 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001133 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001134 return INVALID_OPERATION;
1135 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001136 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001137
1138 if (mInputStream != 0) {
1139 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1140 return INVALID_OPERATION;
1141 }
1142
1143 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1144 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001146
1147 mInputStream = newStream;
1148
1149 *id = mNextStreamId++;
1150
1151 // Continue captures if active at start
1152 if (wasActive) {
1153 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001154 // Reuse current operating mode for new stream config
1155 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001156 if (res != OK) {
1157 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1158 __FUNCTION__, mNextStreamId, strerror(-res), res);
1159 return res;
1160 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001161 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001162 }
1163
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001164 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001165 return OK;
1166}
1167
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001168status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001169 uint32_t width, uint32_t height, int format,
1170 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001171 int streamSetId, bool isShared, uint32_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001172 ATRACE_CALL();
1173
1174 if (consumer == nullptr) {
1175 ALOGE("%s: consumer must not be null", __FUNCTION__);
1176 return BAD_VALUE;
1177 }
1178
1179 std::vector<sp<Surface>> consumers;
1180 consumers.push_back(consumer);
1181
1182 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001183 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001184}
1185
1186status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1187 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1188 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001189 int streamSetId, bool isShared, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001191 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001193 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang758c2152017-01-10 18:26:18 -08001194 " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format,
1195 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 status_t res;
1198 bool wasActive = false;
1199
1200 switch (mStatus) {
1201 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 return INVALID_OPERATION;
1204 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001205 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001206 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001207 case STATUS_UNCONFIGURED:
1208 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001209 // OK
1210 break;
1211 case STATUS_ACTIVE:
1212 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001214 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001215 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 return res;
1217 }
1218 wasActive = true;
1219 break;
1220 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001221 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 return INVALID_OPERATION;
1223 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001224 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225
1226 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001227
Shuzhen Wang0129d522016-10-30 22:43:41 -07001228 if (consumers.size() == 0 && !hasDeferredConsumer) {
1229 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1230 return BAD_VALUE;
1231 }
Zhijun He5d677d12016-05-29 16:52:39 -07001232
Shuzhen Wang0129d522016-10-30 22:43:41 -07001233 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001234 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1235 return BAD_VALUE;
1236 }
1237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001239 ssize_t blobBufferSize;
1240 if (dataSpace != HAL_DATASPACE_DEPTH) {
1241 blobBufferSize = getJpegBufferSize(width, height);
1242 if (blobBufferSize <= 0) {
1243 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1244 return BAD_VALUE;
1245 }
1246 } else {
1247 blobBufferSize = getPointCloudBufferSize();
1248 if (blobBufferSize <= 0) {
1249 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1250 return BAD_VALUE;
1251 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001252 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001253 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001254 width, height, blobBufferSize, format, dataSpace, rotation,
1255 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001256 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1257 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1258 if (rawOpaqueBufferSize <= 0) {
1259 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1260 return BAD_VALUE;
1261 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001262 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001263 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1264 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001265 } else if (isShared) {
1266 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1267 width, height, format, consumerUsage, dataSpace, rotation,
1268 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001269 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001270 newStream = new Camera3OutputStream(mNextStreamId,
1271 width, height, format, consumerUsage, dataSpace, rotation,
1272 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001273 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001274 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001275 width, height, format, dataSpace, rotation,
1276 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001277 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001279
Emilian Peev08dd2452017-04-06 16:55:14 +01001280 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001281
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001282 res = mOutputStreams.add(mNextStreamId, newStream);
1283 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001284 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001285 return res;
1286 }
1287
1288 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001289 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290
1291 // Continue captures if active at start
1292 if (wasActive) {
1293 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001294 // Reuse current operating mode for new stream config
1295 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001296 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001297 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1298 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 return res;
1300 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001301 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001303 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001305}
1306
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001307status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001308 uint32_t *width, uint32_t *height,
1309 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001311 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001313
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314 switch (mStatus) {
1315 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317 return INVALID_OPERATION;
1318 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001319 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 case STATUS_UNCONFIGURED:
1322 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 case STATUS_ACTIVE:
1324 // OK
1325 break;
1326 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001327 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 return INVALID_OPERATION;
1329 }
1330
1331 ssize_t idx = mOutputStreams.indexOfKey(id);
1332 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001333 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 return idx;
1335 }
1336
1337 if (width) *width = mOutputStreams[idx]->getWidth();
1338 if (height) *height = mOutputStreams[idx]->getHeight();
1339 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001340 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001342}
1343
1344status_t Camera3Device::setStreamTransform(int id,
1345 int transform) {
1346 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001347 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001348 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001350 switch (mStatus) {
1351 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001352 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001353 return INVALID_OPERATION;
1354 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001355 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001357 case STATUS_UNCONFIGURED:
1358 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001359 case STATUS_ACTIVE:
1360 // OK
1361 break;
1362 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001363 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 return INVALID_OPERATION;
1365 }
1366
1367 ssize_t idx = mOutputStreams.indexOfKey(id);
1368 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 CLOGE("Stream %d does not exist",
1370 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371 return BAD_VALUE;
1372 }
1373
1374 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001375}
1376
1377status_t Camera3Device::deleteStream(int id) {
1378 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380 Mutex::Autolock l(mLock);
1381 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001383 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385 // CameraDevice semantics require device to already be idle before
1386 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001387 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001388 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001389 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 }
1391
Igor Murashkin2fba5842013-04-22 14:03:54 -07001392 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001393 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 if (mInputStream != NULL && id == mInputStream->getId()) {
1395 deletedStream = mInputStream;
1396 mInputStream.clear();
1397 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001398 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001399 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 return BAD_VALUE;
1401 }
Zhijun He5f446352014-01-22 09:49:33 -08001402 }
1403
1404 // Delete output stream or the output part of a bi-directional stream.
1405 if (outputStreamIdx != NAME_NOT_FOUND) {
1406 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001407 mOutputStreams.removeItem(id);
1408 }
1409
1410 // Free up the stream endpoint so that it can be used by some other stream
1411 res = deletedStream->disconnect();
1412 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001413 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001414 // fall through since we want to still list the stream as deleted.
1415 }
1416 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001417 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001418
1419 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001420}
1421
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001422status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001423 ATRACE_CALL();
1424 ALOGV("%s: E", __FUNCTION__);
1425
1426 Mutex::Autolock il(mInterfaceLock);
1427 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001428
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001429 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001430}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001431
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001432status_t Camera3Device::getInputBufferProducer(
1433 sp<IGraphicBufferProducer> *producer) {
1434 Mutex::Autolock il(mInterfaceLock);
1435 Mutex::Autolock l(mLock);
1436
1437 if (producer == NULL) {
1438 return BAD_VALUE;
1439 } else if (mInputStream == NULL) {
1440 return INVALID_OPERATION;
1441 }
1442
1443 return mInputStream->getInputBufferProducer(producer);
1444}
1445
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001446status_t Camera3Device::createDefaultRequest(int templateId,
1447 CameraMetadata *request) {
1448 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001449 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001450
1451 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1452 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1453 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1454 return BAD_VALUE;
1455 }
1456
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001457 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001458 Mutex::Autolock l(mLock);
1459
1460 switch (mStatus) {
1461 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001462 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001463 return INVALID_OPERATION;
1464 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001467 case STATUS_UNCONFIGURED:
1468 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001469 case STATUS_ACTIVE:
1470 // OK
1471 break;
1472 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001473 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474 return INVALID_OPERATION;
1475 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001476
Zhijun Hea1530f12014-09-14 12:44:20 -07001477 if (!mRequestTemplateCache[templateId].isEmpty()) {
1478 *request = mRequestTemplateCache[templateId];
1479 return OK;
1480 }
1481
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001482 camera_metadata_t *rawRequest;
1483 status_t res = mInterface->constructDefaultRequestSettings(
1484 (camera3_request_template_t) templateId, &rawRequest);
1485 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001486 ALOGI("%s: template %d is not supported on this camera device",
1487 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001488 return res;
1489 } else if (res != OK) {
1490 CLOGE("Unable to construct request template %d: %s (%d)",
1491 templateId, strerror(-res), res);
1492 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001493 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001494
Emilian Peev71c73a22017-03-21 16:35:51 +00001495 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001496 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001498 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001499 return OK;
1500}
1501
1502status_t Camera3Device::waitUntilDrained() {
1503 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001504 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001506
Zhijun He69a37482014-03-23 18:44:49 -07001507 return waitUntilDrainedLocked();
1508}
1509
1510status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001511 switch (mStatus) {
1512 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001513 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001514 ALOGV("%s: Already idle", __FUNCTION__);
1515 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 case STATUS_CONFIGURED:
1517 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001518 case STATUS_ERROR:
1519 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001520 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001521 break;
1522 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001523 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001524 return INVALID_OPERATION;
1525 }
1526
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001527 ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001528 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001529 if (res != OK) {
1530 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1531 res);
1532 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001533 return res;
1534}
1535
Ruben Brunk183f0562015-08-12 12:55:02 -07001536
1537void Camera3Device::internalUpdateStatusLocked(Status status) {
1538 mStatus = status;
1539 mRecentStatusUpdates.add(mStatus);
1540 mStatusChanged.broadcast();
1541}
1542
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001543// Pause to reconfigure
1544status_t Camera3Device::internalPauseAndWaitLocked() {
1545 mRequestThread->setPaused(true);
1546 mPauseStateNotify = true;
1547
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001548 ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001549 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1550 if (res != OK) {
1551 SET_ERR_L("Can't idle device in %f seconds!",
1552 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 }
1554
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001555 return res;
1556}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001558// Resume after internalPauseAndWaitLocked
1559status_t Camera3Device::internalResumeLocked() {
1560 status_t res;
1561
1562 mRequestThread->setPaused(false);
1563
1564 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1565 if (res != OK) {
1566 SET_ERR_L("Can't transition to active in %f seconds!",
1567 kActiveTimeout/1e9);
1568 }
1569 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001571}
1572
Ruben Brunk183f0562015-08-12 12:55:02 -07001573status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001574 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001575
1576 size_t startIndex = 0;
1577 if (mStatusWaiters == 0) {
1578 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1579 // this status list
1580 mRecentStatusUpdates.clear();
1581 } else {
1582 // If other threads are waiting on updates to this status list, set the position of the
1583 // first element that this list will check rather than clearing the list.
1584 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585 }
1586
Ruben Brunk183f0562015-08-12 12:55:02 -07001587 mStatusWaiters++;
1588
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001589 bool stateSeen = false;
1590 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001591 if (active == (mStatus == STATUS_ACTIVE)) {
1592 // Desired state is current
1593 break;
1594 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001595
1596 res = mStatusChanged.waitRelative(mLock, timeout);
1597 if (res != OK) break;
1598
Ruben Brunk183f0562015-08-12 12:55:02 -07001599 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1600 // transitions.
1601 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1602 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1603 __FUNCTION__);
1604
1605 // Encountered desired state since we began waiting
1606 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001607 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1608 stateSeen = true;
1609 break;
1610 }
1611 }
1612 } while (!stateSeen);
1613
Ruben Brunk183f0562015-08-12 12:55:02 -07001614 mStatusWaiters--;
1615
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001616 return res;
1617}
1618
1619
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001620status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001621 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001622 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001623
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001624 if (listener != NULL && mListener != NULL) {
1625 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1626 }
1627 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001628 mRequestThread->setNotificationListener(listener);
1629 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001630
1631 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001632}
1633
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001634bool Camera3Device::willNotify3A() {
1635 return false;
1636}
1637
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001638status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001639 status_t res;
1640 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001641
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001642 while (mResultQueue.empty()) {
1643 res = mResultSignal.waitRelative(mOutputLock, timeout);
1644 if (res == TIMED_OUT) {
1645 return res;
1646 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001647 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1648 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001649 return res;
1650 }
1651 }
1652 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001653}
1654
Jianing Weicb0652e2014-03-12 18:29:36 -07001655status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001656 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001657 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001658
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001659 if (mResultQueue.empty()) {
1660 return NOT_ENOUGH_DATA;
1661 }
1662
Jianing Weicb0652e2014-03-12 18:29:36 -07001663 if (frame == NULL) {
1664 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1665 return BAD_VALUE;
1666 }
1667
1668 CaptureResult &result = *(mResultQueue.begin());
1669 frame->mResultExtras = result.mResultExtras;
1670 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001671 mResultQueue.erase(mResultQueue.begin());
1672
1673 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001674}
1675
1676status_t Camera3Device::triggerAutofocus(uint32_t id) {
1677 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001678 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001679
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001680 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1681 // Mix-in this trigger into the next request and only the next request.
1682 RequestTrigger trigger[] = {
1683 {
1684 ANDROID_CONTROL_AF_TRIGGER,
1685 ANDROID_CONTROL_AF_TRIGGER_START
1686 },
1687 {
1688 ANDROID_CONTROL_AF_TRIGGER_ID,
1689 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001690 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001691 };
1692
1693 return mRequestThread->queueTrigger(trigger,
1694 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001695}
1696
1697status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1698 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001699 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001701 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1702 // Mix-in this trigger into the next request and only the next request.
1703 RequestTrigger trigger[] = {
1704 {
1705 ANDROID_CONTROL_AF_TRIGGER,
1706 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1707 },
1708 {
1709 ANDROID_CONTROL_AF_TRIGGER_ID,
1710 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001711 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001712 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001713
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001714 return mRequestThread->queueTrigger(trigger,
1715 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001716}
1717
1718status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1719 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001720 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001722 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1723 // Mix-in this trigger into the next request and only the next request.
1724 RequestTrigger trigger[] = {
1725 {
1726 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1727 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1728 },
1729 {
1730 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1731 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001732 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001733 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001734
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001735 return mRequestThread->queueTrigger(trigger,
1736 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001737}
1738
Jianing Weicb0652e2014-03-12 18:29:36 -07001739status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001740 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001741 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001742 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001743
Zhijun He7ef20392014-04-21 16:04:17 -07001744 {
1745 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001746 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001747 }
1748
Emilian Peev08dd2452017-04-06 16:55:14 +01001749 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001750}
1751
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001752status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001753 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1754}
1755
1756status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001757 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001758 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001759 Mutex::Autolock il(mInterfaceLock);
1760 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001761
1762 sp<Camera3StreamInterface> stream;
1763 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1764 if (outputStreamIdx == NAME_NOT_FOUND) {
1765 CLOGE("Stream %d does not exist", streamId);
1766 return BAD_VALUE;
1767 }
1768
1769 stream = mOutputStreams.editValueAt(outputStreamIdx);
1770
1771 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001772 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001773 return BAD_VALUE;
1774 }
1775
1776 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001777 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001778 return BAD_VALUE;
1779 }
1780
Ruben Brunkc78ac262015-08-13 17:58:46 -07001781 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001782}
1783
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001784status_t Camera3Device::tearDown(int streamId) {
1785 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001786 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001787 Mutex::Autolock il(mInterfaceLock);
1788 Mutex::Autolock l(mLock);
1789
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001790 sp<Camera3StreamInterface> stream;
1791 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1792 if (outputStreamIdx == NAME_NOT_FOUND) {
1793 CLOGE("Stream %d does not exist", streamId);
1794 return BAD_VALUE;
1795 }
1796
1797 stream = mOutputStreams.editValueAt(outputStreamIdx);
1798
1799 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1800 CLOGE("Stream %d is a target of a in-progress request", streamId);
1801 return BAD_VALUE;
1802 }
1803
1804 return stream->tearDown();
1805}
1806
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001807status_t Camera3Device::addBufferListenerForStream(int streamId,
1808 wp<Camera3StreamBufferListener> listener) {
1809 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001810 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001811 Mutex::Autolock il(mInterfaceLock);
1812 Mutex::Autolock l(mLock);
1813
1814 sp<Camera3StreamInterface> stream;
1815 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1816 if (outputStreamIdx == NAME_NOT_FOUND) {
1817 CLOGE("Stream %d does not exist", streamId);
1818 return BAD_VALUE;
1819 }
1820
1821 stream = mOutputStreams.editValueAt(outputStreamIdx);
1822 stream->addBufferListener(listener);
1823
1824 return OK;
1825}
1826
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001827/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001828 * Methods called by subclasses
1829 */
1830
1831void Camera3Device::notifyStatus(bool idle) {
1832 {
1833 // Need mLock to safely update state and synchronize to current
1834 // state of methods in flight.
1835 Mutex::Autolock l(mLock);
1836 // We can get various system-idle notices from the status tracker
1837 // while starting up. Only care about them if we've actually sent
1838 // in some requests recently.
1839 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1840 return;
1841 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001842 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001843 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001844 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001845
1846 // Skip notifying listener if we're doing some user-transparent
1847 // state changes
1848 if (mPauseStateNotify) return;
1849 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001850
1851 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001852 {
1853 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001854 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001855 }
1856 if (idle && listener != NULL) {
1857 listener->notifyIdle();
1858 }
1859}
1860
Shuzhen Wang758c2152017-01-10 18:26:18 -08001861status_t Camera3Device::setConsumerSurfaces(int streamId,
1862 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07001863 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001864 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1865 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001866 Mutex::Autolock il(mInterfaceLock);
1867 Mutex::Autolock l(mLock);
1868
Shuzhen Wang758c2152017-01-10 18:26:18 -08001869 if (consumers.size() == 0) {
1870 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001871 return BAD_VALUE;
1872 }
1873
1874 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1875 if (idx == NAME_NOT_FOUND) {
1876 CLOGE("Stream %d is unknown", streamId);
1877 return idx;
1878 }
1879 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08001880 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001881 if (res != OK) {
1882 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1883 return res;
1884 }
1885
Shuzhen Wang0129d522016-10-30 22:43:41 -07001886 if (stream->isConsumerConfigurationDeferred()) {
1887 if (!stream->isConfiguring()) {
1888 CLOGE("Stream %d was already fully configured.", streamId);
1889 return INVALID_OPERATION;
1890 }
Zhijun He5d677d12016-05-29 16:52:39 -07001891
Shuzhen Wang0129d522016-10-30 22:43:41 -07001892 res = stream->finishConfiguration();
1893 if (res != OK) {
1894 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1895 stream->getId(), strerror(-res), res);
1896 return res;
1897 }
Zhijun He5d677d12016-05-29 16:52:39 -07001898 }
1899
1900 return OK;
1901}
1902
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001903/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001904 * Camera3Device private methods
1905 */
1906
1907sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001908 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001909 ATRACE_CALL();
1910 status_t res;
1911
1912 sp<CaptureRequest> newRequest = new CaptureRequest;
1913 newRequest->mSettings = request;
1914
1915 camera_metadata_entry_t inputStreams =
1916 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1917 if (inputStreams.count > 0) {
1918 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001919 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001920 CLOGE("Request references unknown input stream %d",
1921 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001922 return NULL;
1923 }
1924 // Lazy completion of stream configuration (allocation/registration)
1925 // on first use
1926 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001927 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001929 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001930 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001931 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001932 return NULL;
1933 }
1934 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001935 // Check if stream is being prepared
1936 if (mInputStream->isPreparing()) {
1937 CLOGE("Request references an input stream that's being prepared!");
1938 return NULL;
1939 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001940
1941 newRequest->mInputStream = mInputStream;
1942 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1943 }
1944
1945 camera_metadata_entry_t streams =
1946 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1947 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001948 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001949 return NULL;
1950 }
1951
1952 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001953 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001955 CLOGE("Request references unknown stream %d",
1956 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957 return NULL;
1958 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001959 sp<Camera3OutputStreamInterface> stream =
1960 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001961
Zhijun He5d677d12016-05-29 16:52:39 -07001962 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07001963 auto iter = surfaceMap.find(streams.data.i32[i]);
1964 if (iter != surfaceMap.end()) {
1965 const std::vector<size_t>& surfaces = iter->second;
1966 for (const auto& surface : surfaces) {
1967 if (stream->isConsumerConfigurationDeferred(surface)) {
1968 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
1969 "due to deferred consumer", stream->getId(), surface);
1970 return NULL;
1971 }
1972 }
1973 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07001974 }
1975
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001976 // Lazy completion of stream configuration (allocation/registration)
1977 // on first use
1978 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001979 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001981 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1982 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001983 return NULL;
1984 }
1985 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001986 // Check if stream is being prepared
1987 if (stream->isPreparing()) {
1988 CLOGE("Request references an output stream that's being prepared!");
1989 return NULL;
1990 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001991
1992 newRequest->mOutputStreams.push(stream);
1993 }
1994 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001995 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001996
1997 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001998}
1999
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002000bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2001 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2002 Size size = mSupportedOpaqueInputSizes[i];
2003 if (size.width == width && size.height == height) {
2004 return true;
2005 }
2006 }
2007
2008 return false;
2009}
2010
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002011void Camera3Device::cancelStreamsConfigurationLocked() {
2012 int res = OK;
2013 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2014 res = mInputStream->cancelConfiguration();
2015 if (res != OK) {
2016 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2017 mInputStream->getId(), strerror(-res), res);
2018 }
2019 }
2020
2021 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2022 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2023 if (outputStream->isConfiguring()) {
2024 res = outputStream->cancelConfiguration();
2025 if (res != OK) {
2026 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2027 outputStream->getId(), strerror(-res), res);
2028 }
2029 }
2030 }
2031
2032 // Return state to that at start of call, so that future configures
2033 // properly clean things up
2034 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2035 mNeedConfig = true;
2036}
2037
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002038status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002039 ATRACE_CALL();
2040 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002041
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002042 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002043 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 return INVALID_OPERATION;
2045 }
2046
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002047 if (operatingMode < 0) {
2048 CLOGE("Invalid operating mode: %d", operatingMode);
2049 return BAD_VALUE;
2050 }
2051
2052 bool isConstrainedHighSpeed =
2053 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2054 operatingMode;
2055
2056 if (mOperatingMode != operatingMode) {
2057 mNeedConfig = true;
2058 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2059 mOperatingMode = operatingMode;
2060 }
2061
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002062 if (!mNeedConfig) {
2063 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2064 return OK;
2065 }
2066
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002067 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2068 // adding a dummy stream instead.
2069 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2070 if (mOutputStreams.size() == 0) {
2071 addDummyStreamLocked();
2072 } else {
2073 tryRemoveDummyStreamLocked();
2074 }
2075
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002077 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002078
2079 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002080 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002081 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2082
2083 Vector<camera3_stream_t*> streams;
2084 streams.setCapacity(config.num_streams);
2085
2086 if (mInputStream != NULL) {
2087 camera3_stream_t *inputStream;
2088 inputStream = mInputStream->startConfiguration();
2089 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002090 CLOGE("Can't start input stream configuration");
2091 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 return INVALID_OPERATION;
2093 }
2094 streams.add(inputStream);
2095 }
2096
2097 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002098
2099 // Don't configure bidi streams twice, nor add them twice to the list
2100 if (mOutputStreams[i].get() ==
2101 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2102
2103 config.num_streams--;
2104 continue;
2105 }
2106
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002107 camera3_stream_t *outputStream;
2108 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2109 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002110 CLOGE("Can't start output stream configuration");
2111 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 return INVALID_OPERATION;
2113 }
2114 streams.add(outputStream);
2115 }
2116
2117 config.streams = streams.editArray();
2118
2119 // Do the HAL configuration; will potentially touch stream
2120 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002121
2122 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002124 if (res == BAD_VALUE) {
2125 // HAL rejected this set of streams as unsupported, clean up config
2126 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002127 CLOGE("Set of requested inputs/outputs not supported by HAL");
2128 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002129 return BAD_VALUE;
2130 } else if (res != OK) {
2131 // Some other kind of error from configure_streams - this is not
2132 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002133 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2134 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135 return res;
2136 }
2137
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002138 // Finish all stream configuration immediately.
2139 // TODO: Try to relax this later back to lazy completion, which should be
2140 // faster
2141
Igor Murashkin073f8572013-05-02 14:59:28 -07002142 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002143 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002144 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002145 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002146 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002147 cancelStreamsConfigurationLocked();
2148 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002149 }
2150 }
2151
2152 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002153 sp<Camera3OutputStreamInterface> outputStream =
2154 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002155 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002156 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002157 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002158 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002159 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002160 cancelStreamsConfigurationLocked();
2161 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002162 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002163 }
2164 }
2165
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002166 // Request thread needs to know to avoid using repeat-last-settings protocol
2167 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002168 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002169
Zhijun He90f7c372016-08-16 16:19:43 -07002170 char value[PROPERTY_VALUE_MAX];
2171 property_get("camera.fifo.disable", value, "0");
2172 int32_t disableFifo = atoi(value);
2173 if (disableFifo != 1) {
2174 // Boost priority of request thread to SCHED_FIFO.
2175 pid_t requestThreadTid = mRequestThread->getTid();
2176 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002177 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002178 if (res != OK) {
2179 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2180 strerror(-res), res);
2181 } else {
2182 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2183 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002184 }
2185
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002186 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002187
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002188 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002189
Ruben Brunk183f0562015-08-12 12:55:02 -07002190 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2191 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002192
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002193 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002194
Zhijun He0a210512014-07-24 13:45:15 -07002195 // tear down the deleted streams after configure streams.
2196 mDeletedStreams.clear();
2197
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002198 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002199}
2200
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002201status_t Camera3Device::addDummyStreamLocked() {
2202 ATRACE_CALL();
2203 status_t res;
2204
2205 if (mDummyStreamId != NO_STREAM) {
2206 // Should never be adding a second dummy stream when one is already
2207 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002208 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2209 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002210 return INVALID_OPERATION;
2211 }
2212
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002213 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002214
2215 sp<Camera3OutputStreamInterface> dummyStream =
2216 new Camera3DummyStream(mNextStreamId);
2217
2218 res = mOutputStreams.add(mNextStreamId, dummyStream);
2219 if (res < 0) {
2220 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2221 return res;
2222 }
2223
2224 mDummyStreamId = mNextStreamId;
2225 mNextStreamId++;
2226
2227 return OK;
2228}
2229
2230status_t Camera3Device::tryRemoveDummyStreamLocked() {
2231 ATRACE_CALL();
2232 status_t res;
2233
2234 if (mDummyStreamId == NO_STREAM) return OK;
2235 if (mOutputStreams.size() == 1) return OK;
2236
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002237 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002238
2239 // Ok, have a dummy stream and there's at least one other output stream,
2240 // so remove the dummy
2241
2242 sp<Camera3StreamInterface> deletedStream;
2243 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2244 if (outputStreamIdx == NAME_NOT_FOUND) {
2245 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2246 return INVALID_OPERATION;
2247 }
2248
2249 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2250 mOutputStreams.removeItemsAt(outputStreamIdx);
2251
2252 // Free up the stream endpoint so that it can be used by some other stream
2253 res = deletedStream->disconnect();
2254 if (res != OK) {
2255 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2256 // fall through since we want to still list the stream as deleted.
2257 }
2258 mDeletedStreams.add(deletedStream);
2259 mDummyStreamId = NO_STREAM;
2260
2261 return res;
2262}
2263
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002264void Camera3Device::setErrorState(const char *fmt, ...) {
2265 Mutex::Autolock l(mLock);
2266 va_list args;
2267 va_start(args, fmt);
2268
2269 setErrorStateLockedV(fmt, args);
2270
2271 va_end(args);
2272}
2273
2274void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2275 Mutex::Autolock l(mLock);
2276 setErrorStateLockedV(fmt, args);
2277}
2278
2279void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2280 va_list args;
2281 va_start(args, fmt);
2282
2283 setErrorStateLockedV(fmt, args);
2284
2285 va_end(args);
2286}
2287
2288void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002289 // Print out all error messages to log
2290 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002291 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002292
2293 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002294 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002295
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002296 mErrorCause = errorCause;
2297
2298 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002299 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002300
2301 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002302 sp<NotificationListener> listener = mListener.promote();
2303 if (listener != NULL) {
2304 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002305 CaptureResultExtras());
2306 }
2307
2308 // Save stack trace. View by dumping it later.
2309 CameraTraces::saveTrace();
2310 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002311}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002312
2313/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002314 * In-flight request management
2315 */
2316
Jianing Weicb0652e2014-03-12 18:29:36 -07002317status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002318 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002319 bool hasAppCallback) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002320 ATRACE_CALL();
2321 Mutex::Autolock l(mInFlightLock);
2322
2323 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002324 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002325 hasAppCallback));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002326 if (res < 0) return res;
2327
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002328 if (mInFlightMap.size() == 1) {
2329 mStatusTracker->markComponentActive(mInFlightStatusId);
2330 }
2331
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002332 return OK;
2333}
2334
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002335void Camera3Device::returnOutputBuffers(
2336 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2337 nsecs_t timestamp) {
2338 for (size_t i = 0; i < numBuffers; i++)
2339 {
2340 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2341 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2342 // Note: stream may be deallocated at this point, if this buffer was
2343 // the last reference to it.
2344 if (res != OK) {
2345 ALOGE("Can't return buffer to its stream: %s (%d)",
2346 strerror(-res), res);
2347 }
2348 }
2349}
2350
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002351void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2352 mInFlightMap.removeItemsAt(idx, 1);
2353
2354 // Indicate idle inFlightMap to the status tracker
2355 if (mInFlightMap.size() == 0) {
2356 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2357 }
2358}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002359
2360void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2361
2362 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2363 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2364
2365 nsecs_t sensorTimestamp = request.sensorTimestamp;
2366 nsecs_t shutterTimestamp = request.shutterTimestamp;
2367
2368 // Check if it's okay to remove the request from InFlightMap:
2369 // In the case of a successful request:
2370 // all input and output buffers, all result metadata, shutter callback
2371 // arrived.
2372 // In the case of a unsuccessful request:
2373 // all input and output buffers arrived.
2374 if (request.numBuffersLeft == 0 &&
2375 (request.requestStatus != OK ||
2376 (request.haveResultMetadata && shutterTimestamp != 0))) {
2377 ATRACE_ASYNC_END("frame capture", frameNumber);
2378
Shuzhen Wang403044a2017-02-26 23:29:04 -08002379 // Sanity check - if sensor timestamp matches shutter timestamp in the
2380 // case of request having callback.
2381 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002382 sensorTimestamp != shutterTimestamp) {
2383 SET_ERR("sensor timestamp (%" PRId64
2384 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2385 sensorTimestamp, frameNumber, shutterTimestamp);
2386 }
2387
2388 // for an unsuccessful request, it may have pending output buffers to
2389 // return.
2390 assert(request.requestStatus != OK ||
2391 request.pendingOutputBuffers.size() == 0);
2392 returnOutputBuffers(request.pendingOutputBuffers.array(),
2393 request.pendingOutputBuffers.size(), 0);
2394
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002395 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002396 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2397 }
2398
2399 // Sanity check - if we have too many in-flight frames, something has
2400 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002401 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002402 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002403 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2404 kInFlightWarnLimitHighSpeed) {
2405 CLOGE("In-flight list too large for high speed configuration: %zu",
2406 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002407 }
2408}
2409
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002410void Camera3Device::insertResultLocked(CaptureResult *result,
2411 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002412 if (result == nullptr) return;
2413
Emilian Peev71c73a22017-03-21 16:35:51 +00002414 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2415 result->mMetadata.getAndLock());
2416 set_camera_metadata_vendor_id(meta, mVendorTagId);
2417 result->mMetadata.unlock(meta);
2418
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002419 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2420 (int32_t*)&frameNumber, 1) != OK) {
2421 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2422 return;
2423 }
2424
2425 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2426 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2427 return;
2428 }
2429
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002430 // Valid result, insert into queue
2431 List<CaptureResult>::iterator queuedResult =
2432 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2433 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2434 ", burstId = %" PRId32, __FUNCTION__,
2435 queuedResult->mResultExtras.requestId,
2436 queuedResult->mResultExtras.frameNumber,
2437 queuedResult->mResultExtras.burstId);
2438
2439 mResultSignal.signal();
2440}
2441
2442
2443void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002444 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002445 Mutex::Autolock l(mOutputLock);
2446
2447 CaptureResult captureResult;
2448 captureResult.mResultExtras = resultExtras;
2449 captureResult.mMetadata = partialResult;
2450
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002451 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002452}
2453
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002454
2455void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2456 CaptureResultExtras &resultExtras,
2457 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002458 uint32_t frameNumber,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002459 bool reprocess) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002460 if (pendingMetadata.isEmpty())
2461 return;
2462
2463 Mutex::Autolock l(mOutputLock);
2464
2465 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002466 if (reprocess) {
2467 if (frameNumber < mNextReprocessResultFrameNumber) {
2468 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002469 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002470 frameNumber, mNextReprocessResultFrameNumber);
2471 return;
2472 }
2473 mNextReprocessResultFrameNumber = frameNumber + 1;
2474 } else {
2475 if (frameNumber < mNextResultFrameNumber) {
2476 SET_ERR("Out-of-order capture result metadata submitted! "
2477 "(got frame number %d, expecting %d)",
2478 frameNumber, mNextResultFrameNumber);
2479 return;
2480 }
2481 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002482 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002483
2484 CaptureResult captureResult;
2485 captureResult.mResultExtras = resultExtras;
2486 captureResult.mMetadata = pendingMetadata;
2487
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002488 // Append any previous partials to form a complete result
2489 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2490 captureResult.mMetadata.append(collectedPartialResult);
2491 }
2492
2493 captureResult.mMetadata.sort();
2494
2495 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002496 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2497 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002498 SET_ERR("No timestamp provided by HAL for frame %d!",
2499 frameNumber);
2500 return;
2501 }
2502
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002503 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2504 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2505
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002506 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002507}
2508
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002509/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002510 * Camera HAL device callback methods
2511 */
2512
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002513void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002514 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002515
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002516 status_t res;
2517
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002518 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002519 if (result->result == NULL && result->num_output_buffers == 0 &&
2520 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002521 SET_ERR("No result data provided by HAL for frame %d",
2522 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002523 return;
2524 }
Zhijun He204e3292014-07-14 17:09:23 -07002525
Zhijun He204e3292014-07-14 17:09:23 -07002526 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002527 result->result != NULL &&
2528 result->partial_result != 1) {
2529 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2530 " if partial result is not supported",
2531 frameNumber, result->partial_result);
2532 return;
2533 }
2534
2535 bool isPartialResult = false;
2536 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002537 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002538 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002539
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002540 // Get shutter timestamp and resultExtras from list of in-flight requests,
2541 // where it was added by the shutter notification for this frame. If the
2542 // shutter timestamp isn't received yet, append the output buffers to the
2543 // in-flight request and they will be returned when the shutter timestamp
2544 // arrives. Update the in-flight status and remove the in-flight entry if
2545 // all result data and shutter timestamp have been received.
2546 nsecs_t shutterTimestamp = 0;
2547
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002548 {
2549 Mutex::Autolock l(mInFlightLock);
2550 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2551 if (idx == NAME_NOT_FOUND) {
2552 SET_ERR("Unknown frame number for capture result: %d",
2553 frameNumber);
2554 return;
2555 }
2556 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002557 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2558 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002559 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002560 __FUNCTION__, request.resultExtras.requestId,
2561 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002562 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002563 // Always update the partial count to the latest one if it's not 0
2564 // (buffers only). When framework aggregates adjacent partial results
2565 // into one, the latest partial count will be used.
2566 if (result->partial_result != 0)
2567 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002568
2569 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002570 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002571 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2572 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2573 " the range of [1, %d] when metadata is included in the result",
2574 frameNumber, result->partial_result, mNumPartialResults);
2575 return;
2576 }
2577 isPartialResult = (result->partial_result < mNumPartialResults);
2578 if (isPartialResult) {
2579 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002580 }
2581
Shuzhen Wang4a472662017-02-26 23:29:04 -08002582 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002583 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002584 sendPartialCaptureResult(result->result, request.resultExtras,
2585 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002586 }
2587 }
2588
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002589 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002590 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002591
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002592 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002593 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002594 if (request.haveResultMetadata) {
2595 SET_ERR("Called multiple times with metadata for frame %d",
2596 frameNumber);
2597 return;
2598 }
Zhijun He204e3292014-07-14 17:09:23 -07002599 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002600 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002601 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002602 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002603 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002604 request.haveResultMetadata = true;
2605 }
2606
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002607 uint32_t numBuffersReturned = result->num_output_buffers;
2608 if (result->input_buffer != NULL) {
2609 if (hasInputBufferInRequest) {
2610 numBuffersReturned += 1;
2611 } else {
2612 ALOGW("%s: Input buffer should be NULL if there is no input"
2613 " buffer sent in the request",
2614 __FUNCTION__);
2615 }
2616 }
2617 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002618 if (request.numBuffersLeft < 0) {
2619 SET_ERR("Too many buffers returned for frame %d",
2620 frameNumber);
2621 return;
2622 }
2623
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002624 camera_metadata_ro_entry_t entry;
2625 res = find_camera_metadata_ro_entry(result->result,
2626 ANDROID_SENSOR_TIMESTAMP, &entry);
2627 if (res == OK && entry.count == 1) {
2628 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002629 }
2630
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002631 // If shutter event isn't received yet, append the output buffers to
2632 // the in-flight request. Otherwise, return the output buffers to
2633 // streams.
2634 if (shutterTimestamp == 0) {
2635 request.pendingOutputBuffers.appendArray(result->output_buffers,
2636 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002637 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002638 returnOutputBuffers(result->output_buffers,
2639 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002640 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002641
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002642 if (result->result != NULL && !isPartialResult) {
2643 if (shutterTimestamp == 0) {
2644 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002645 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002646 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002647 CameraMetadata metadata;
2648 metadata = result->result;
2649 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002650 collectedPartialResult, frameNumber,
2651 hasInputBufferInRequest);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002652 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002653 }
2654
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002655 removeInFlightRequestIfReadyLocked(idx);
2656 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002657
Zhijun Hef0d962a2014-06-30 10:24:11 -07002658 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002659 if (hasInputBufferInRequest) {
2660 Camera3Stream *stream =
2661 Camera3Stream::cast(result->input_buffer->stream);
2662 res = stream->returnInputBuffer(*(result->input_buffer));
2663 // Note: stream may be deallocated at this point, if this buffer was the
2664 // last reference to it.
2665 if (res != OK) {
2666 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2667 " its stream:%s (%d)", __FUNCTION__,
2668 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002669 }
2670 } else {
2671 ALOGW("%s: Input buffer should be NULL if there is no input"
2672 " buffer sent in the request, skipping input buffer return.",
2673 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002674 }
2675 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002676}
2677
2678void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002679 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002680 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002681 {
2682 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002683 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002684 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002685
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002686 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002687 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002688 return;
2689 }
2690
2691 switch (msg->type) {
2692 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002693 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002694 break;
2695 }
2696 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002697 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002698 break;
2699 }
2700 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002701 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002702 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002703 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002704}
2705
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002706void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002707 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002708
2709 // Map camera HAL error codes to ICameraDeviceCallback error codes
2710 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002711 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002712 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002713 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002714 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002715 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002716 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002717 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002718 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002719 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002720 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002721 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002722 };
2723
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002724 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002725 ((msg.error_code >= 0) &&
2726 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2727 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002728 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002729
2730 int streamId = 0;
2731 if (msg.error_stream != NULL) {
2732 Camera3Stream *stream =
2733 Camera3Stream::cast(msg.error_stream);
2734 streamId = stream->getId();
2735 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002736 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2737 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002738 streamId, msg.error_code);
2739
2740 CaptureResultExtras resultExtras;
2741 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002742 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002743 // SET_ERR calls notifyError
2744 SET_ERR("Camera HAL reported serious device error");
2745 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002746 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2747 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2748 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002749 {
2750 Mutex::Autolock l(mInFlightLock);
2751 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2752 if (idx >= 0) {
2753 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2754 r.requestStatus = msg.error_code;
2755 resultExtras = r.resultExtras;
Emilian Peevba0fac32017-03-30 09:05:34 +01002756 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
2757 errorCode) {
2758 // In case of missing result check whether the buffers
2759 // returned. If they returned, then remove inflight
2760 // request.
2761 removeInFlightRequestIfReadyLocked(idx);
2762 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002763 } else {
2764 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002765 ALOGE("Camera %s: %s: cannot find in-flight request on "
2766 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002767 resultExtras.frameNumber);
2768 }
2769 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002770 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002771 if (listener != NULL) {
2772 listener->notifyError(errorCode, resultExtras);
2773 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002774 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002775 }
2776 break;
2777 default:
2778 // SET_ERR calls notifyError
2779 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2780 break;
2781 }
2782}
2783
2784void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002785 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002786 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002787
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002788 // Set timestamp for the request in the in-flight tracking
2789 // and get the request ID to send upstream
2790 {
2791 Mutex::Autolock l(mInFlightLock);
2792 idx = mInFlightMap.indexOfKey(msg.frame_number);
2793 if (idx >= 0) {
2794 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002795
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002796 // Verify ordering of shutter notifications
2797 {
2798 Mutex::Autolock l(mOutputLock);
2799 // TODO: need to track errors for tighter bounds on expected frame number.
2800 if (r.hasInputBuffer) {
2801 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2802 SET_ERR("Shutter notification out-of-order. Expected "
2803 "notification for frame %d, got frame %d",
2804 mNextReprocessShutterFrameNumber, msg.frame_number);
2805 return;
2806 }
2807 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2808 } else {
2809 if (msg.frame_number < mNextShutterFrameNumber) {
2810 SET_ERR("Shutter notification out-of-order. Expected "
2811 "notification for frame %d, got frame %d",
2812 mNextShutterFrameNumber, msg.frame_number);
2813 return;
2814 }
2815 mNextShutterFrameNumber = msg.frame_number + 1;
2816 }
2817 }
2818
Shuzhen Wang4a472662017-02-26 23:29:04 -08002819 r.shutterTimestamp = msg.timestamp;
2820 if (r.hasCallback) {
2821 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002822 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002823 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08002824 // Call listener, if any
2825 if (listener != NULL) {
2826 listener->notifyShutter(r.resultExtras, msg.timestamp);
2827 }
2828 // send pending result and buffers
2829 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2830 r.collectedPartialResult, msg.frame_number,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002831 r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002832 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002833 returnOutputBuffers(r.pendingOutputBuffers.array(),
2834 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2835 r.pendingOutputBuffers.clear();
2836
2837 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002838 }
2839 }
2840 if (idx < 0) {
2841 SET_ERR("Shutter notification for non-existent frame number %d",
2842 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002843 }
2844}
2845
2846
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002847CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002848 ALOGV("%s", __FUNCTION__);
2849
Igor Murashkin1e479c02013-09-06 16:55:14 -07002850 CameraMetadata retVal;
2851
2852 if (mRequestThread != NULL) {
2853 retVal = mRequestThread->getLatestRequest();
2854 }
2855
Igor Murashkin1e479c02013-09-06 16:55:14 -07002856 return retVal;
2857}
2858
Jianing Weicb0652e2014-03-12 18:29:36 -07002859
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002860void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2861 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2862 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2863}
2864
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002865/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002866 * HalInterface inner class methods
2867 */
2868
Yifan Hongf79b5542017-04-11 14:44:25 -07002869Camera3Device::HalInterface::HalInterface(
2870 sp<ICameraDeviceSession> &session,
2871 std::shared_ptr<RequestMetadataQueue> queue) :
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002872 mHal3Device(nullptr),
Yifan Hongf79b5542017-04-11 14:44:25 -07002873 mHidlSession(session),
2874 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002875
2876Camera3Device::HalInterface::HalInterface() :
2877 mHal3Device(nullptr) {}
2878
2879Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08002880 mHal3Device(other.mHal3Device),
Yifan Hongf79b5542017-04-11 14:44:25 -07002881 mHidlSession(other.mHidlSession),
2882 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002883
2884bool Camera3Device::HalInterface::valid() {
2885 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
2886}
2887
2888void Camera3Device::HalInterface::clear() {
2889 mHal3Device = nullptr;
2890 mHidlSession.clear();
2891}
2892
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08002893bool Camera3Device::HalInterface::supportBatchRequest() {
2894 return mHidlSession != nullptr;
2895}
2896
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002897status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
2898 camera3_request_template_t templateId,
2899 /*out*/ camera_metadata_t **requestTemplate) {
2900 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
2901 if (!valid()) return INVALID_OPERATION;
2902 status_t res = OK;
2903
2904 if (mHal3Device != nullptr) {
2905 const camera_metadata *r;
2906 r = mHal3Device->ops->construct_default_request_settings(
2907 mHal3Device, templateId);
2908 if (r == nullptr) return BAD_VALUE;
2909 *requestTemplate = clone_camera_metadata(r);
2910 if (requestTemplate == nullptr) {
2911 ALOGE("%s: Unable to clone camera metadata received from HAL",
2912 __FUNCTION__);
2913 return INVALID_OPERATION;
2914 }
2915 } else {
2916 common::V1_0::Status status;
2917 RequestTemplate id;
2918 switch (templateId) {
2919 case CAMERA3_TEMPLATE_PREVIEW:
2920 id = RequestTemplate::PREVIEW;
2921 break;
2922 case CAMERA3_TEMPLATE_STILL_CAPTURE:
2923 id = RequestTemplate::STILL_CAPTURE;
2924 break;
2925 case CAMERA3_TEMPLATE_VIDEO_RECORD:
2926 id = RequestTemplate::VIDEO_RECORD;
2927 break;
2928 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
2929 id = RequestTemplate::VIDEO_SNAPSHOT;
2930 break;
2931 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
2932 id = RequestTemplate::ZERO_SHUTTER_LAG;
2933 break;
2934 case CAMERA3_TEMPLATE_MANUAL:
2935 id = RequestTemplate::MANUAL;
2936 break;
2937 default:
2938 // Unknown template ID
2939 return BAD_VALUE;
2940 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07002941 auto err = mHidlSession->constructDefaultRequestSettings(id,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002942 [&status, &requestTemplate]
2943 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
2944 status = s;
2945 if (status == common::V1_0::Status::OK) {
2946 const camera_metadata *r =
2947 reinterpret_cast<const camera_metadata_t*>(request.data());
2948 size_t expectedSize = request.size();
2949 int ret = validate_camera_metadata_structure(r, &expectedSize);
Yin-Chia Yeh238ef5f2017-04-18 15:01:15 -07002950 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002951 *requestTemplate = clone_camera_metadata(r);
2952 if (*requestTemplate == nullptr) {
2953 ALOGE("%s: Unable to clone camera metadata received from HAL",
2954 __FUNCTION__);
2955 status = common::V1_0::Status::INTERNAL_ERROR;
2956 }
2957 } else {
2958 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
2959 status = common::V1_0::Status::INTERNAL_ERROR;
2960 }
2961 }
2962 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07002963 if (!err.isOk()) {
2964 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
2965 res = DEAD_OBJECT;
2966 } else {
2967 res = CameraProviderManager::mapToStatusT(status);
2968 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002969 }
2970 return res;
2971}
2972
2973status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
2974 ATRACE_NAME("CameraHal::configureStreams");
2975 if (!valid()) return INVALID_OPERATION;
2976 status_t res = OK;
2977
2978 if (mHal3Device != nullptr) {
2979 res = mHal3Device->ops->configure_streams(mHal3Device, config);
2980 } else {
2981 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002982 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002983 StreamConfiguration requestedConfiguration;
2984 requestedConfiguration.streams.resize(config->num_streams);
2985 for (size_t i = 0; i < config->num_streams; i++) {
2986 Stream &dst = requestedConfiguration.streams[i];
2987 camera3_stream_t *src = config->streams[i];
2988
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002989 Camera3Stream* cam3stream = Camera3Stream::cast(src);
2990 cam3stream->setBufferFreedListener(this);
2991 int streamId = cam3stream->getId();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002992 StreamType streamType;
2993 switch (src->stream_type) {
2994 case CAMERA3_STREAM_OUTPUT:
2995 streamType = StreamType::OUTPUT;
2996 break;
2997 case CAMERA3_STREAM_INPUT:
2998 streamType = StreamType::INPUT;
2999 break;
3000 default:
3001 ALOGE("%s: Stream %d: Unsupported stream type %d",
3002 __FUNCTION__, streamId, config->streams[i]->stream_type);
3003 return BAD_VALUE;
3004 }
3005 dst.id = streamId;
3006 dst.streamType = streamType;
3007 dst.width = src->width;
3008 dst.height = src->height;
3009 dst.format = mapToPixelFormat(src->format);
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003010 dst.usage = mapToConsumerUsage(src->usage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003011 dst.dataSpace = mapToHidlDataspace(src->data_space);
3012 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003013
3014 activeStreams.insert(streamId);
3015 // Create Buffer ID map if necessary
3016 if (mBufferIdMaps.count(streamId) == 0) {
3017 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3018 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003019 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003020 // remove BufferIdMap for deleted streams
3021 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3022 int streamId = it->first;
3023 bool active = activeStreams.count(streamId) > 0;
3024 if (!active) {
3025 it = mBufferIdMaps.erase(it);
3026 } else {
3027 ++it;
3028 }
3029 }
3030
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08003031 res = mapToStreamConfigurationMode(
3032 (camera3_stream_configuration_mode_t) config->operation_mode,
3033 /*out*/ &requestedConfiguration.operationMode);
3034 if (res != OK) {
3035 return res;
3036 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003037
3038 // Invoke configureStreams
3039
3040 HalStreamConfiguration finalConfiguration;
3041 common::V1_0::Status status;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003042 auto err = mHidlSession->configureStreams(requestedConfiguration,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003043 [&status, &finalConfiguration]
3044 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3045 finalConfiguration = halConfiguration;
3046 status = s;
3047 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003048 if (!err.isOk()) {
3049 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3050 return DEAD_OBJECT;
3051 }
3052
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003053 if (status != common::V1_0::Status::OK ) {
3054 return CameraProviderManager::mapToStatusT(status);
3055 }
3056
3057 // And convert output stream configuration from HIDL
3058
3059 for (size_t i = 0; i < config->num_streams; i++) {
3060 camera3_stream_t *dst = config->streams[i];
3061 int streamId = Camera3Stream::cast(dst)->getId();
3062
3063 // Start scan at i, with the assumption that the stream order matches
3064 size_t realIdx = i;
3065 bool found = false;
3066 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3067 if (finalConfiguration.streams[realIdx].id == streamId) {
3068 found = true;
3069 break;
3070 }
3071 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3072 }
3073 if (!found) {
3074 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3075 __FUNCTION__, streamId);
3076 return INVALID_OPERATION;
3077 }
3078 HalStream &src = finalConfiguration.streams[realIdx];
3079
3080 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3081 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3082 if (dst->format != overrideFormat) {
3083 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3084 streamId, dst->format);
3085 }
3086 } else {
3087 // Override allowed with IMPLEMENTATION_DEFINED
3088 dst->format = overrideFormat;
3089 }
3090
3091 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3092 if (src.producerUsage != 0) {
3093 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3094 __FUNCTION__, streamId);
3095 return INVALID_OPERATION;
3096 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003097 dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003098 } else {
3099 // OUTPUT
3100 if (src.consumerUsage != 0) {
3101 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3102 __FUNCTION__, streamId);
3103 return INVALID_OPERATION;
3104 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003105 dst->usage = mapProducerToFrameworkUsage(src.producerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003106 }
3107 dst->max_buffers = src.maxBuffers;
3108 }
3109 }
3110 return res;
3111}
3112
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003113void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3114 /*out*/device::V3_2::CaptureRequest* captureRequest,
3115 /*out*/std::vector<native_handle_t*>* handlesCreated) {
3116
3117 if (captureRequest == nullptr || handlesCreated == nullptr) {
3118 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3119 __FUNCTION__, captureRequest, handlesCreated);
3120 return;
3121 }
3122
3123 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003124
3125 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003126
3127 {
3128 std::lock_guard<std::mutex> lock(mInflightLock);
3129 if (request->input_buffer != nullptr) {
3130 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3131 buffer_handle_t buf = *(request->input_buffer->buffer);
3132 auto pair = getBufferId(buf, streamId);
3133 bool isNewBuffer = pair.first;
3134 uint64_t bufferId = pair.second;
3135 captureRequest->inputBuffer.streamId = streamId;
3136 captureRequest->inputBuffer.bufferId = bufferId;
3137 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3138 captureRequest->inputBuffer.status = BufferStatus::OK;
3139 native_handle_t *acquireFence = nullptr;
3140 if (request->input_buffer->acquire_fence != -1) {
3141 acquireFence = native_handle_create(1,0);
3142 acquireFence->data[0] = request->input_buffer->acquire_fence;
3143 handlesCreated->push_back(acquireFence);
3144 }
3145 captureRequest->inputBuffer.acquireFence = acquireFence;
3146 captureRequest->inputBuffer.releaseFence = nullptr;
3147
3148 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3149 request->input_buffer->buffer,
3150 request->input_buffer->acquire_fence);
3151 } else {
3152 captureRequest->inputBuffer.streamId = -1;
3153 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3154 }
3155
3156 captureRequest->outputBuffers.resize(request->num_output_buffers);
3157 for (size_t i = 0; i < request->num_output_buffers; i++) {
3158 const camera3_stream_buffer_t *src = request->output_buffers + i;
3159 StreamBuffer &dst = captureRequest->outputBuffers[i];
3160 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3161 buffer_handle_t buf = *(src->buffer);
3162 auto pair = getBufferId(buf, streamId);
3163 bool isNewBuffer = pair.first;
3164 dst.streamId = streamId;
3165 dst.bufferId = pair.second;
3166 dst.buffer = isNewBuffer ? buf : nullptr;
3167 dst.status = BufferStatus::OK;
3168 native_handle_t *acquireFence = nullptr;
3169 if (src->acquire_fence != -1) {
3170 acquireFence = native_handle_create(1,0);
3171 acquireFence->data[0] = src->acquire_fence;
3172 handlesCreated->push_back(acquireFence);
3173 }
3174 dst.acquireFence = acquireFence;
3175 dst.releaseFence = nullptr;
3176
3177 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3178 src->buffer, src->acquire_fence);
3179 }
3180 }
3181}
3182
3183status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3184 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3185 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3186 if (!valid()) return INVALID_OPERATION;
3187
3188 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3189 size_t batchSize = requests.size();
3190 captureRequests.resize(batchSize);
3191 std::vector<native_handle_t*> handlesCreated;
3192
3193 for (size_t i = 0; i < batchSize; i++) {
3194 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3195 }
3196
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003197 std::vector<device::V3_2::BufferCache> cachesToRemove;
3198 {
3199 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3200 for (auto& pair : mFreedBuffers) {
3201 // The stream might have been removed since onBufferFreed
3202 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3203 cachesToRemove.push_back({pair.first, pair.second});
3204 }
3205 }
3206 mFreedBuffers.clear();
3207 }
3208
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003209 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3210 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003211
3212 // Write metadata to FMQ.
3213 for (size_t i = 0; i < batchSize; i++) {
3214 camera3_capture_request_t* request = requests[i];
3215 device::V3_2::CaptureRequest* captureRequest = &captureRequests[i];
3216
3217 if (request->settings != nullptr) {
3218 size_t settingsSize = get_camera_metadata_size(request->settings);
3219 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3220 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3221 captureRequest->settings.resize(0);
3222 captureRequest->fmqSettingsSize = settingsSize;
3223 } else {
3224 if (mRequestMetadataQueue != nullptr) {
3225 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3226 }
3227 captureRequest->settings.setToExternal(
3228 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3229 get_camera_metadata_size(request->settings));
3230 captureRequest->fmqSettingsSize = 0u;
3231 }
3232 } else {
3233 // A null request settings maps to a size-0 CameraMetadata
3234 captureRequest->settings.resize(0);
3235 captureRequest->fmqSettingsSize = 0u;
3236 }
3237 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003238 auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003239 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3240 status = s;
3241 *numRequestProcessed = n;
3242 });
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003243 if (!err.isOk()) {
3244 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3245 return DEAD_OBJECT;
3246 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003247 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3248 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3249 __FUNCTION__, *numRequestProcessed, batchSize);
3250 status = common::V1_0::Status::INTERNAL_ERROR;
3251 }
3252
3253 for (auto& handle : handlesCreated) {
3254 native_handle_delete(handle);
3255 }
3256 return CameraProviderManager::mapToStatusT(status);
3257}
3258
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003259status_t Camera3Device::HalInterface::processCaptureRequest(
3260 camera3_capture_request_t *request) {
3261 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003262 if (!valid()) return INVALID_OPERATION;
3263 status_t res = OK;
3264
3265 if (mHal3Device != nullptr) {
3266 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3267 } else {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003268 uint32_t numRequestProcessed = 0;
3269 std::vector<camera3_capture_request_t*> requests(1);
3270 requests[0] = request;
3271 res = processBatchCaptureRequests(requests, &numRequestProcessed);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003272 }
3273 return res;
3274}
3275
3276status_t Camera3Device::HalInterface::flush() {
3277 ATRACE_NAME("CameraHal::flush");
3278 if (!valid()) return INVALID_OPERATION;
3279 status_t res = OK;
3280
3281 if (mHal3Device != nullptr) {
3282 res = mHal3Device->ops->flush(mHal3Device);
3283 } else {
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003284 auto err = mHidlSession->flush();
3285 if (!err.isOk()) {
3286 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3287 res = DEAD_OBJECT;
3288 } else {
3289 res = CameraProviderManager::mapToStatusT(err);
3290 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003291 }
3292 return res;
3293}
3294
3295status_t Camera3Device::HalInterface::dump(int fd) {
3296 ATRACE_NAME("CameraHal::dump");
3297 if (!valid()) return INVALID_OPERATION;
3298 status_t res = OK;
3299
3300 if (mHal3Device != nullptr) {
3301 mHal3Device->ops->dump(mHal3Device, fd);
3302 } else {
3303 // Handled by CameraProviderManager::dump
3304 }
3305 return res;
3306}
3307
3308status_t Camera3Device::HalInterface::close() {
3309 ATRACE_NAME("CameraHal::close()");
3310 if (!valid()) return INVALID_OPERATION;
3311 status_t res = OK;
3312
3313 if (mHal3Device != nullptr) {
3314 mHal3Device->common.close(&mHal3Device->common);
3315 } else {
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003316 auto err = mHidlSession->close();
3317 // Interface will be dead shortly anyway, so don't log errors
3318 if (!err.isOk()) {
3319 res = DEAD_OBJECT;
3320 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003321 }
3322 return res;
3323}
3324
3325status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003326 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003327 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003328 auto pair = std::make_pair(buffer, acquireFence);
3329 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003330 return OK;
3331}
3332
3333status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003334 int32_t frameNumber, int32_t streamId,
3335 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003336 std::lock_guard<std::mutex> lock(mInflightLock);
3337
3338 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3339 auto it = mInflightBufferMap.find(key);
3340 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003341 auto pair = it->second;
3342 *buffer = pair.first;
3343 int acquireFence = pair.second;
3344 if (acquireFence > 0) {
3345 ::close(acquireFence);
3346 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003347 mInflightBufferMap.erase(it);
3348 return OK;
3349}
3350
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003351std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3352 const buffer_handle_t& buf, int streamId) {
3353 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3354
3355 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3356 auto it = bIdMap.find(buf);
3357 if (it == bIdMap.end()) {
3358 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003359 ALOGV("stream %d now have %zu buffer caches, buf %p",
3360 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003361 return std::make_pair(true, mNextBufferId - 1);
3362 } else {
3363 return std::make_pair(false, it->second);
3364 }
3365}
3366
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003367void Camera3Device::HalInterface::onBufferFreed(
3368 int streamId, const native_handle_t* handle) {
3369 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3370 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3371 auto mapIt = mBufferIdMaps.find(streamId);
3372 if (mapIt == mBufferIdMaps.end()) {
3373 // streamId might be from a deleted stream here
3374 ALOGI("%s: stream %d has been removed",
3375 __FUNCTION__, streamId);
3376 return;
3377 }
3378 BufferIdMap& bIdMap = mapIt->second;
3379 auto it = bIdMap.find(handle);
3380 if (it == bIdMap.end()) {
3381 ALOGW("%s: cannot find buffer %p in stream %d",
3382 __FUNCTION__, handle, streamId);
3383 return;
3384 } else {
3385 bufferId = it->second;
3386 bIdMap.erase(it);
3387 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3388 __FUNCTION__, streamId, bIdMap.size(), handle);
3389 }
3390 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3391}
3392
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003393/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003394 * RequestThread inner class methods
3395 */
3396
3397Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003398 sp<StatusTracker> statusTracker,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003399 HalInterface* interface) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003400 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003401 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003402 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003403 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003404 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003405 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003406 mReconfigured(false),
3407 mDoPause(false),
3408 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003409 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003410 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003411 mCurrentAfTriggerId(0),
3412 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003413 mRepeatingLastFrameNumber(
3414 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003415 mPrepareVideoStream(false),
3416 mRequestLatency(kRequestLatencyBinSize) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003417 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003418}
3419
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003420Camera3Device::RequestThread::~RequestThread() {}
3421
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003422void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003423 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003424 Mutex::Autolock l(mRequestLock);
3425 mListener = listener;
3426}
3427
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003428void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003429 Mutex::Autolock l(mRequestLock);
3430 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003431 // Prepare video stream for high speed recording.
3432 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003433}
3434
Jianing Wei90e59c92014-03-12 18:29:36 -07003435status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003436 List<sp<CaptureRequest> > &requests,
3437 /*out*/
3438 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003439 Mutex::Autolock l(mRequestLock);
3440 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3441 ++it) {
3442 mRequestQueue.push_back(*it);
3443 }
3444
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003445 if (lastFrameNumber != NULL) {
3446 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3447 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3448 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3449 *lastFrameNumber);
3450 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003451
Jianing Wei90e59c92014-03-12 18:29:36 -07003452 unpauseForNewRequests();
3453
3454 return OK;
3455}
3456
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003457
3458status_t Camera3Device::RequestThread::queueTrigger(
3459 RequestTrigger trigger[],
3460 size_t count) {
3461
3462 Mutex::Autolock l(mTriggerMutex);
3463 status_t ret;
3464
3465 for (size_t i = 0; i < count; ++i) {
3466 ret = queueTriggerLocked(trigger[i]);
3467
3468 if (ret != OK) {
3469 return ret;
3470 }
3471 }
3472
3473 return OK;
3474}
3475
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003476const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3477 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003478 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003479 if (d != nullptr) return d->mId;
3480 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003481}
3482
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003483status_t Camera3Device::RequestThread::queueTriggerLocked(
3484 RequestTrigger trigger) {
3485
3486 uint32_t tag = trigger.metadataTag;
3487 ssize_t index = mTriggerMap.indexOfKey(tag);
3488
3489 switch (trigger.getTagType()) {
3490 case TYPE_BYTE:
3491 // fall-through
3492 case TYPE_INT32:
3493 break;
3494 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003495 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3496 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003497 return INVALID_OPERATION;
3498 }
3499
3500 /**
3501 * Collect only the latest trigger, since we only have 1 field
3502 * in the request settings per trigger tag, and can't send more than 1
3503 * trigger per request.
3504 */
3505 if (index != NAME_NOT_FOUND) {
3506 mTriggerMap.editValueAt(index) = trigger;
3507 } else {
3508 mTriggerMap.add(tag, trigger);
3509 }
3510
3511 return OK;
3512}
3513
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003514status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003515 const RequestList &requests,
3516 /*out*/
3517 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003518 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003519 if (lastFrameNumber != NULL) {
3520 *lastFrameNumber = mRepeatingLastFrameNumber;
3521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003522 mRepeatingRequests.clear();
3523 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3524 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003525
3526 unpauseForNewRequests();
3527
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003528 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003529 return OK;
3530}
3531
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003532bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003533 if (mRepeatingRequests.empty()) {
3534 return false;
3535 }
3536 int32_t requestId = requestIn->mResultExtras.requestId;
3537 const RequestList &repeatRequests = mRepeatingRequests;
3538 // All repeating requests are guaranteed to have same id so only check first quest
3539 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3540 return (firstRequest->mResultExtras.requestId == requestId);
3541}
3542
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003543status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003544 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003545 return clearRepeatingRequestsLocked(lastFrameNumber);
3546
3547}
3548
3549status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003550 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003551 if (lastFrameNumber != NULL) {
3552 *lastFrameNumber = mRepeatingLastFrameNumber;
3553 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003554 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003555 return OK;
3556}
3557
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003558status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003559 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003560 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003561 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003562
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003563 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003564
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003565 // Send errors for all requests pending in the request queue, including
3566 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003567 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003568 if (listener != NULL) {
3569 for (RequestList::iterator it = mRequestQueue.begin();
3570 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003571 // Abort the input buffers for reprocess requests.
3572 if ((*it)->mInputStream != NULL) {
3573 camera3_stream_buffer_t inputBuffer;
3574 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
3575 if (res != OK) {
3576 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3577 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3578 } else {
3579 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3580 if (res != OK) {
3581 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3582 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3583 }
3584 }
3585 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003586 // Set the frame number this request would have had, if it
3587 // had been submitted; this frame number will not be reused.
3588 // The requestId and burstId fields were set when the request was
3589 // submitted originally (in convertMetadataListToRequestListLocked)
3590 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003591 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003592 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003593 }
3594 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003595 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003596
3597 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003598 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003599 if (lastFrameNumber != NULL) {
3600 *lastFrameNumber = mRepeatingLastFrameNumber;
3601 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003602 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003603 return OK;
3604}
3605
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003606status_t Camera3Device::RequestThread::flush() {
3607 ATRACE_CALL();
3608 Mutex::Autolock l(mFlushLock);
3609
Emilian Peev08dd2452017-04-06 16:55:14 +01003610 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003611}
3612
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003613void Camera3Device::RequestThread::setPaused(bool paused) {
3614 Mutex::Autolock l(mPauseLock);
3615 mDoPause = paused;
3616 mDoPauseSignal.signal();
3617}
3618
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003619status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3620 int32_t requestId, nsecs_t timeout) {
3621 Mutex::Autolock l(mLatestRequestMutex);
3622 status_t res;
3623 while (mLatestRequestId != requestId) {
3624 nsecs_t startTime = systemTime();
3625
3626 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3627 if (res != OK) return res;
3628
3629 timeout -= (systemTime() - startTime);
3630 }
3631
3632 return OK;
3633}
3634
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003635void Camera3Device::RequestThread::requestExit() {
3636 // Call parent to set up shutdown
3637 Thread::requestExit();
3638 // The exit from any possible waits
3639 mDoPauseSignal.signal();
3640 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003641
3642 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3643 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003644}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003645
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003646void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003647 bool surfaceAbandoned = false;
3648 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003649 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003650 {
3651 Mutex::Autolock l(mRequestLock);
3652 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3653 // repeating requests.
3654 for (const auto& request : mRepeatingRequests) {
3655 for (const auto& s : request->mOutputStreams) {
3656 if (s->isAbandoned()) {
3657 surfaceAbandoned = true;
3658 clearRepeatingRequestsLocked(&lastFrameNumber);
3659 break;
3660 }
3661 }
3662 if (surfaceAbandoned) {
3663 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003664 }
3665 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003666 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003667 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003668
3669 if (listener != NULL && surfaceAbandoned) {
3670 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003671 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003672}
3673
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003674bool Camera3Device::RequestThread::sendRequestsBatch() {
3675 status_t res;
3676 size_t batchSize = mNextRequests.size();
3677 std::vector<camera3_capture_request_t*> requests(batchSize);
3678 uint32_t numRequestProcessed = 0;
3679 for (size_t i = 0; i < batchSize; i++) {
3680 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3681 }
3682
3683 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
3684 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3685
3686 bool triggerRemoveFailed = false;
3687 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3688 for (size_t i = 0; i < numRequestProcessed; i++) {
3689 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3690 nextRequest.submitted = true;
3691
3692
3693 // Update the latest request sent to HAL
3694 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3695 Mutex::Autolock al(mLatestRequestMutex);
3696
3697 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3698 mLatestRequest.acquire(cloned);
3699
3700 sp<Camera3Device> parent = mParent.promote();
3701 if (parent != NULL) {
3702 parent->monitorMetadata(TagMonitor::REQUEST,
3703 nextRequest.halRequest.frame_number,
3704 0, mLatestRequest);
3705 }
3706 }
3707
3708 if (nextRequest.halRequest.settings != NULL) {
3709 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3710 }
3711
3712 if (!triggerRemoveFailed) {
3713 // Remove any previously queued triggers (after unlock)
3714 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3715 if (removeTriggerRes != OK) {
3716 triggerRemoveFailed = true;
3717 triggerFailedRequest = nextRequest;
3718 }
3719 }
3720 }
3721
3722 if (triggerRemoveFailed) {
3723 SET_ERR("RequestThread: Unable to remove triggers "
3724 "(capture request %d, HAL device: %s (%d)",
3725 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3726 cleanUpFailedRequests(/*sendRequestError*/ false);
3727 return false;
3728 }
3729
3730 if (res != OK) {
3731 // Should only get a failure here for malformed requests or device-level
3732 // errors, so consider all errors fatal. Bad metadata failures should
3733 // come through notify.
3734 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3735 mNextRequests[numRequestProcessed].halRequest.frame_number,
3736 strerror(-res), res);
3737 cleanUpFailedRequests(/*sendRequestError*/ false);
3738 return false;
3739 }
3740 return true;
3741}
3742
3743bool Camera3Device::RequestThread::sendRequestsOneByOne() {
3744 status_t res;
3745
3746 for (auto& nextRequest : mNextRequests) {
3747 // Submit request and block until ready for next one
3748 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3749 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
3750
3751 if (res != OK) {
3752 // Should only get a failure here for malformed requests or device-level
3753 // errors, so consider all errors fatal. Bad metadata failures should
3754 // come through notify.
3755 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3756 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3757 res);
3758 cleanUpFailedRequests(/*sendRequestError*/ false);
3759 return false;
3760 }
3761
3762 // Mark that the request has be submitted successfully.
3763 nextRequest.submitted = true;
3764
3765 // Update the latest request sent to HAL
3766 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3767 Mutex::Autolock al(mLatestRequestMutex);
3768
3769 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3770 mLatestRequest.acquire(cloned);
3771
3772 sp<Camera3Device> parent = mParent.promote();
3773 if (parent != NULL) {
3774 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3775 0, mLatestRequest);
3776 }
3777 }
3778
3779 if (nextRequest.halRequest.settings != NULL) {
3780 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3781 }
3782
3783 // Remove any previously queued triggers (after unlock)
3784 res = removeTriggers(mPrevRequest);
3785 if (res != OK) {
3786 SET_ERR("RequestThread: Unable to remove triggers "
3787 "(capture request %d, HAL device: %s (%d)",
3788 nextRequest.halRequest.frame_number, strerror(-res), res);
3789 cleanUpFailedRequests(/*sendRequestError*/ false);
3790 return false;
3791 }
3792 }
3793 return true;
3794}
3795
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003796bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003797 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003798 status_t res;
3799
3800 // Handle paused state.
3801 if (waitIfPaused()) {
3802 return true;
3803 }
3804
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003805 // Wait for the next batch of requests.
3806 waitForNextRequestBatch();
3807 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003808 return true;
3809 }
3810
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003811 // Get the latest request ID, if any
3812 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003813 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003814 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003815 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003816 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003817 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003818 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3819 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003820 }
3821
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003822 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003823 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003824 if (res == TIMED_OUT) {
3825 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003826 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003827 // Check if any stream is abandoned.
3828 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003829 return true;
3830 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003831 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003832 return false;
3833 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003834
Zhijun Hecc27e112013-10-03 16:12:43 -07003835 // Inform waitUntilRequestProcessed thread of a new request ID
3836 {
3837 Mutex::Autolock al(mLatestRequestMutex);
3838
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003839 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003840 mLatestRequestSignal.signal();
3841 }
3842
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003843 // Submit a batch of requests to HAL.
3844 // Use flush lock only when submitting multilple requests in a batch.
3845 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3846 // which may take a long time to finish so synchronizing flush() and
3847 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3848 // For now, only synchronize for high speed recording and we should figure something out for
3849 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003850 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003851
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003852 if (useFlushLock) {
3853 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003854 }
3855
Zhijun Hef0645c12016-08-02 00:58:11 -07003856 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003857 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003858
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003859 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003860 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003861 if (mInterface->supportBatchRequest()) {
3862 submitRequestSuccess = sendRequestsBatch();
3863 } else {
3864 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07003865 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07003866 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3867 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003868
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003869 if (useFlushLock) {
3870 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003871 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003872
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003873 // Unset as current request
3874 {
3875 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003876 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003877 }
3878
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003879 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003880}
3881
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003882status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003883 ATRACE_CALL();
3884
Shuzhen Wang4a472662017-02-26 23:29:04 -08003885 for (size_t i = 0; i < mNextRequests.size(); i++) {
3886 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003887 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3888 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3889 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3890
3891 // Prepare a request to HAL
3892 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3893
3894 // Insert any queued triggers (before metadata is locked)
3895 status_t res = insertTriggers(captureRequest);
3896
3897 if (res < 0) {
3898 SET_ERR("RequestThread: Unable to insert triggers "
3899 "(capture request %d, HAL device: %s (%d)",
3900 halRequest->frame_number, strerror(-res), res);
3901 return INVALID_OPERATION;
3902 }
3903 int triggerCount = res;
3904 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3905 mPrevTriggers = triggerCount;
3906
3907 // If the request is the same as last, or we had triggers last time
3908 if (mPrevRequest != captureRequest || triggersMixedIn) {
3909 /**
3910 * HAL workaround:
3911 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3912 */
3913 res = addDummyTriggerIds(captureRequest);
3914 if (res != OK) {
3915 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3916 "(capture request %d, HAL device: %s (%d)",
3917 halRequest->frame_number, strerror(-res), res);
3918 return INVALID_OPERATION;
3919 }
3920
3921 /**
3922 * The request should be presorted so accesses in HAL
3923 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3924 */
3925 captureRequest->mSettings.sort();
3926 halRequest->settings = captureRequest->mSettings.getAndLock();
3927 mPrevRequest = captureRequest;
3928 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3929
3930 IF_ALOGV() {
3931 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3932 find_camera_metadata_ro_entry(
3933 halRequest->settings,
3934 ANDROID_CONTROL_AF_TRIGGER,
3935 &e
3936 );
3937 if (e.count > 0) {
3938 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3939 __FUNCTION__,
3940 halRequest->frame_number,
3941 e.data.u8[0]);
3942 }
3943 }
3944 } else {
3945 // leave request.settings NULL to indicate 'reuse latest given'
3946 ALOGVV("%s: Request settings are REUSED",
3947 __FUNCTION__);
3948 }
3949
3950 uint32_t totalNumBuffers = 0;
3951
3952 // Fill in buffers
3953 if (captureRequest->mInputStream != NULL) {
3954 halRequest->input_buffer = &captureRequest->mInputBuffer;
3955 totalNumBuffers += 1;
3956 } else {
3957 halRequest->input_buffer = NULL;
3958 }
3959
3960 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3961 captureRequest->mOutputStreams.size());
3962 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08003963 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
3964 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003965
3966 // Prepare video buffers for high speed recording on the first video request.
3967 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3968 // Only try to prepare video stream on the first video request.
3969 mPrepareVideoStream = false;
3970
3971 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3972 while (res == NOT_ENOUGH_DATA) {
3973 res = outputStream->prepareNextBuffer();
3974 }
3975 if (res != OK) {
3976 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3977 __FUNCTION__, strerror(-res), res);
3978 outputStream->cancelPrepare();
3979 }
3980 }
3981
Shuzhen Wang4a472662017-02-26 23:29:04 -08003982 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3983 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003984 if (res != OK) {
3985 // Can't get output buffer from gralloc queue - this could be due to
3986 // abandoned queue or other consumer misbehavior, so not a fatal
3987 // error
3988 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3989 " %s (%d)", strerror(-res), res);
3990
3991 return TIMED_OUT;
3992 }
3993 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07003994
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003995 }
3996 totalNumBuffers += halRequest->num_output_buffers;
3997
3998 // Log request in the in-flight queue
3999 sp<Camera3Device> parent = mParent.promote();
4000 if (parent == NULL) {
4001 // Should not happen, and nowhere to send errors to, so just log it
4002 CLOGE("RequestThread: Parent is gone");
4003 return INVALID_OPERATION;
4004 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004005
4006 // If this request list is for constrained high speed recording (not
4007 // preview), and the current request is not the last one in the batch,
4008 // do not send callback to the app.
4009 bool hasCallback = true;
4010 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4011 hasCallback = false;
4012 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004013 res = parent->registerInFlight(halRequest->frame_number,
4014 totalNumBuffers, captureRequest->mResultExtras,
4015 /*hasInput*/halRequest->input_buffer != NULL,
Shuzhen Wang4a472662017-02-26 23:29:04 -08004016 hasCallback);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004017 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4018 ", burstId = %" PRId32 ".",
4019 __FUNCTION__,
4020 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4021 captureRequest->mResultExtras.burstId);
4022 if (res != OK) {
4023 SET_ERR("RequestThread: Unable to register new in-flight request:"
4024 " %s (%d)", strerror(-res), res);
4025 return INVALID_OPERATION;
4026 }
4027 }
4028
4029 return OK;
4030}
4031
Igor Murashkin1e479c02013-09-06 16:55:14 -07004032CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4033 Mutex::Autolock al(mLatestRequestMutex);
4034
4035 ALOGV("RequestThread::%s", __FUNCTION__);
4036
4037 return mLatestRequest;
4038}
4039
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004040bool Camera3Device::RequestThread::isStreamPending(
4041 sp<Camera3StreamInterface>& stream) {
4042 Mutex::Autolock l(mRequestLock);
4043
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004044 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004045 if (!nextRequest.submitted) {
4046 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4047 if (stream == s) return true;
4048 }
4049 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004050 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004051 }
4052
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004053 for (const auto& request : mRequestQueue) {
4054 for (const auto& s : request->mOutputStreams) {
4055 if (stream == s) return true;
4056 }
4057 if (stream == request->mInputStream) return true;
4058 }
4059
4060 for (const auto& request : mRepeatingRequests) {
4061 for (const auto& s : request->mOutputStreams) {
4062 if (stream == s) return true;
4063 }
4064 if (stream == request->mInputStream) return true;
4065 }
4066
4067 return false;
4068}
Jianing Weicb0652e2014-03-12 18:29:36 -07004069
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004070void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4071 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004072 return;
4073 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004074
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004075 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004076 // Skip the ones that have been submitted successfully.
4077 if (nextRequest.submitted) {
4078 continue;
4079 }
4080
4081 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4082 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4083 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4084
4085 if (halRequest->settings != NULL) {
4086 captureRequest->mSettings.unlock(halRequest->settings);
4087 }
4088
4089 if (captureRequest->mInputStream != NULL) {
4090 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4091 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4092 }
4093
4094 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004095 //Buffers that failed processing could still have
4096 //valid acquire fence.
4097 int acquireFence = (*outputBuffers)[i].acquire_fence;
4098 if (0 <= acquireFence) {
4099 close(acquireFence);
4100 outputBuffers->editItemAt(i).acquire_fence = -1;
4101 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004102 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4103 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4104 }
4105
4106 if (sendRequestError) {
4107 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004108 sp<NotificationListener> listener = mListener.promote();
4109 if (listener != NULL) {
4110 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004111 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004112 captureRequest->mResultExtras);
4113 }
4114 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004115
4116 // Remove yet-to-be submitted inflight request from inflightMap
4117 {
4118 sp<Camera3Device> parent = mParent.promote();
4119 if (parent != NULL) {
4120 Mutex::Autolock l(parent->mInFlightLock);
4121 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4122 if (idx >= 0) {
4123 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4124 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4125 parent->removeInFlightMapEntryLocked(idx);
4126 }
4127 }
4128 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004129 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004130
4131 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004132 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004133}
4134
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004135void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004136 // Optimized a bit for the simple steady-state case (single repeating
4137 // request), to avoid putting that request in the queue temporarily.
4138 Mutex::Autolock l(mRequestLock);
4139
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004140 assert(mNextRequests.empty());
4141
4142 NextRequest nextRequest;
4143 nextRequest.captureRequest = waitForNextRequestLocked();
4144 if (nextRequest.captureRequest == nullptr) {
4145 return;
4146 }
4147
4148 nextRequest.halRequest = camera3_capture_request_t();
4149 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004150 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004151
4152 // Wait for additional requests
4153 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4154
4155 for (size_t i = 1; i < batchSize; i++) {
4156 NextRequest additionalRequest;
4157 additionalRequest.captureRequest = waitForNextRequestLocked();
4158 if (additionalRequest.captureRequest == nullptr) {
4159 break;
4160 }
4161
4162 additionalRequest.halRequest = camera3_capture_request_t();
4163 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004164 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004165 }
4166
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004167 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004168 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004169 mNextRequests.size(), batchSize);
4170 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004171 }
4172
4173 return;
4174}
4175
4176sp<Camera3Device::CaptureRequest>
4177 Camera3Device::RequestThread::waitForNextRequestLocked() {
4178 status_t res;
4179 sp<CaptureRequest> nextRequest;
4180
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004181 while (mRequestQueue.empty()) {
4182 if (!mRepeatingRequests.empty()) {
4183 // Always atomically enqueue all requests in a repeating request
4184 // list. Guarantees a complete in-sequence set of captures to
4185 // application.
4186 const RequestList &requests = mRepeatingRequests;
4187 RequestList::const_iterator firstRequest =
4188 requests.begin();
4189 nextRequest = *firstRequest;
4190 mRequestQueue.insert(mRequestQueue.end(),
4191 ++firstRequest,
4192 requests.end());
4193 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004194
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004195 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004197 break;
4198 }
4199
4200 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4201
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004202 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4203 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004204 Mutex::Autolock pl(mPauseLock);
4205 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004206 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004207 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004208 // Let the tracker know
4209 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4210 if (statusTracker != 0) {
4211 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4212 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004213 }
4214 // Stop waiting for now and let thread management happen
4215 return NULL;
4216 }
4217 }
4218
4219 if (nextRequest == NULL) {
4220 // Don't have a repeating request already in hand, so queue
4221 // must have an entry now.
4222 RequestList::iterator firstRequest =
4223 mRequestQueue.begin();
4224 nextRequest = *firstRequest;
4225 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004226 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4227 sp<NotificationListener> listener = mListener.promote();
4228 if (listener != NULL) {
4229 listener->notifyRequestQueueEmpty();
4230 }
4231 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004232 }
4233
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004234 // In case we've been unpaused by setPaused clearing mDoPause, need to
4235 // update internal pause state (capture/setRepeatingRequest unpause
4236 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004237 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004238 if (mPaused) {
4239 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4240 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4241 if (statusTracker != 0) {
4242 statusTracker->markComponentActive(mStatusId);
4243 }
4244 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004245 mPaused = false;
4246
4247 // Check if we've reconfigured since last time, and reset the preview
4248 // request if so. Can't use 'NULL request == repeat' across configure calls.
4249 if (mReconfigured) {
4250 mPrevRequest.clear();
4251 mReconfigured = false;
4252 }
4253
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004254 if (nextRequest != NULL) {
4255 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004256 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4257 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004258
4259 // Since RequestThread::clear() removes buffers from the input stream,
4260 // get the right buffer here before unlocking mRequestLock
4261 if (nextRequest->mInputStream != NULL) {
4262 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4263 if (res != OK) {
4264 // Can't get input buffer from gralloc queue - this could be due to
4265 // disconnected queue or other producer misbehavior, so not a fatal
4266 // error
4267 ALOGE("%s: Can't get input buffer, skipping request:"
4268 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004269
4270 sp<NotificationListener> listener = mListener.promote();
4271 if (listener != NULL) {
4272 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004273 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004274 nextRequest->mResultExtras);
4275 }
4276 return NULL;
4277 }
4278 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004279 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004280
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004281 return nextRequest;
4282}
4283
4284bool Camera3Device::RequestThread::waitIfPaused() {
4285 status_t res;
4286 Mutex::Autolock l(mPauseLock);
4287 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004288 if (mPaused == false) {
4289 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004290 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4291 // Let the tracker know
4292 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4293 if (statusTracker != 0) {
4294 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4295 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004296 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004297
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004298 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004299 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004300 return true;
4301 }
4302 }
4303 // We don't set mPaused to false here, because waitForNextRequest needs
4304 // to further manage the paused state in case of starvation.
4305 return false;
4306}
4307
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004308void Camera3Device::RequestThread::unpauseForNewRequests() {
4309 // With work to do, mark thread as unpaused.
4310 // If paused by request (setPaused), don't resume, to avoid
4311 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004312 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004313 Mutex::Autolock p(mPauseLock);
4314 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004315 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4316 if (mPaused) {
4317 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4318 if (statusTracker != 0) {
4319 statusTracker->markComponentActive(mStatusId);
4320 }
4321 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004322 mPaused = false;
4323 }
4324}
4325
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004326void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4327 sp<Camera3Device> parent = mParent.promote();
4328 if (parent != NULL) {
4329 va_list args;
4330 va_start(args, fmt);
4331
4332 parent->setErrorStateV(fmt, args);
4333
4334 va_end(args);
4335 }
4336}
4337
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004338status_t Camera3Device::RequestThread::insertTriggers(
4339 const sp<CaptureRequest> &request) {
4340
4341 Mutex::Autolock al(mTriggerMutex);
4342
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004343 sp<Camera3Device> parent = mParent.promote();
4344 if (parent == NULL) {
4345 CLOGE("RequestThread: Parent is gone");
4346 return DEAD_OBJECT;
4347 }
4348
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004349 CameraMetadata &metadata = request->mSettings;
4350 size_t count = mTriggerMap.size();
4351
4352 for (size_t i = 0; i < count; ++i) {
4353 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004354 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004355
4356 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4357 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4358 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004359 if (isAeTrigger) {
4360 request->mResultExtras.precaptureTriggerId = triggerId;
4361 mCurrentPreCaptureTriggerId = triggerId;
4362 } else {
4363 request->mResultExtras.afTriggerId = triggerId;
4364 mCurrentAfTriggerId = triggerId;
4365 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004366 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004367 }
4368
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004369 camera_metadata_entry entry = metadata.find(tag);
4370
4371 if (entry.count > 0) {
4372 /**
4373 * Already has an entry for this trigger in the request.
4374 * Rewrite it with our requested trigger value.
4375 */
4376 RequestTrigger oldTrigger = trigger;
4377
4378 oldTrigger.entryValue = entry.data.u8[0];
4379
4380 mTriggerReplacedMap.add(tag, oldTrigger);
4381 } else {
4382 /**
4383 * More typical, no trigger entry, so we just add it
4384 */
4385 mTriggerRemovedMap.add(tag, trigger);
4386 }
4387
4388 status_t res;
4389
4390 switch (trigger.getTagType()) {
4391 case TYPE_BYTE: {
4392 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4393 res = metadata.update(tag,
4394 &entryValue,
4395 /*count*/1);
4396 break;
4397 }
4398 case TYPE_INT32:
4399 res = metadata.update(tag,
4400 &trigger.entryValue,
4401 /*count*/1);
4402 break;
4403 default:
4404 ALOGE("%s: Type not supported: 0x%x",
4405 __FUNCTION__,
4406 trigger.getTagType());
4407 return INVALID_OPERATION;
4408 }
4409
4410 if (res != OK) {
4411 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4412 ", value %d", __FUNCTION__, trigger.getTagName(),
4413 trigger.entryValue);
4414 return res;
4415 }
4416
4417 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4418 trigger.getTagName(),
4419 trigger.entryValue);
4420 }
4421
4422 mTriggerMap.clear();
4423
4424 return count;
4425}
4426
4427status_t Camera3Device::RequestThread::removeTriggers(
4428 const sp<CaptureRequest> &request) {
4429 Mutex::Autolock al(mTriggerMutex);
4430
4431 CameraMetadata &metadata = request->mSettings;
4432
4433 /**
4434 * Replace all old entries with their old values.
4435 */
4436 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4437 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4438
4439 status_t res;
4440
4441 uint32_t tag = trigger.metadataTag;
4442 switch (trigger.getTagType()) {
4443 case TYPE_BYTE: {
4444 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4445 res = metadata.update(tag,
4446 &entryValue,
4447 /*count*/1);
4448 break;
4449 }
4450 case TYPE_INT32:
4451 res = metadata.update(tag,
4452 &trigger.entryValue,
4453 /*count*/1);
4454 break;
4455 default:
4456 ALOGE("%s: Type not supported: 0x%x",
4457 __FUNCTION__,
4458 trigger.getTagType());
4459 return INVALID_OPERATION;
4460 }
4461
4462 if (res != OK) {
4463 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4464 ", trigger value %d", __FUNCTION__,
4465 trigger.getTagName(), trigger.entryValue);
4466 return res;
4467 }
4468 }
4469 mTriggerReplacedMap.clear();
4470
4471 /**
4472 * Remove all new entries.
4473 */
4474 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4475 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4476 status_t res = metadata.erase(trigger.metadataTag);
4477
4478 if (res != OK) {
4479 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4480 ", trigger value %d", __FUNCTION__,
4481 trigger.getTagName(), trigger.entryValue);
4482 return res;
4483 }
4484 }
4485 mTriggerRemovedMap.clear();
4486
4487 return OK;
4488}
4489
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004490status_t Camera3Device::RequestThread::addDummyTriggerIds(
4491 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004492 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004493 static const int32_t dummyTriggerId = 1;
4494 status_t res;
4495
4496 CameraMetadata &metadata = request->mSettings;
4497
4498 // If AF trigger is active, insert a dummy AF trigger ID if none already
4499 // exists
4500 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4501 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4502 if (afTrigger.count > 0 &&
4503 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4504 afId.count == 0) {
4505 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4506 if (res != OK) return res;
4507 }
4508
4509 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4510 // if none already exists
4511 camera_metadata_entry pcTrigger =
4512 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4513 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4514 if (pcTrigger.count > 0 &&
4515 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4516 pcId.count == 0) {
4517 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4518 &dummyTriggerId, 1);
4519 if (res != OK) return res;
4520 }
4521
4522 return OK;
4523}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004524
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004525/**
4526 * PreparerThread inner class methods
4527 */
4528
4529Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004530 Thread(/*canCallJava*/false), mListener(nullptr),
4531 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004532}
4533
4534Camera3Device::PreparerThread::~PreparerThread() {
4535 Thread::requestExitAndWait();
4536 if (mCurrentStream != nullptr) {
4537 mCurrentStream->cancelPrepare();
4538 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4539 mCurrentStream.clear();
4540 }
4541 clear();
4542}
4543
Ruben Brunkc78ac262015-08-13 17:58:46 -07004544status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004545 status_t res;
4546
4547 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004548 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004549
Ruben Brunkc78ac262015-08-13 17:58:46 -07004550 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004551 if (res == OK) {
4552 // No preparation needed, fire listener right off
4553 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004554 if (listener != NULL) {
4555 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004556 }
4557 return OK;
4558 } else if (res != NOT_ENOUGH_DATA) {
4559 return res;
4560 }
4561
4562 // Need to prepare, start up thread if necessary
4563 if (!mActive) {
4564 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4565 // isn't running
4566 Thread::requestExitAndWait();
4567 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4568 if (res != OK) {
4569 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004570 if (listener != NULL) {
4571 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004572 }
4573 return res;
4574 }
4575 mCancelNow = false;
4576 mActive = true;
4577 ALOGV("%s: Preparer stream started", __FUNCTION__);
4578 }
4579
4580 // queue up the work
4581 mPendingStreams.push_back(stream);
4582 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4583
4584 return OK;
4585}
4586
4587status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004588 Mutex::Autolock l(mLock);
4589
4590 for (const auto& stream : mPendingStreams) {
4591 stream->cancelPrepare();
4592 }
4593 mPendingStreams.clear();
4594 mCancelNow = true;
4595
4596 return OK;
4597}
4598
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004599void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004600 Mutex::Autolock l(mLock);
4601 mListener = listener;
4602}
4603
4604bool Camera3Device::PreparerThread::threadLoop() {
4605 status_t res;
4606 {
4607 Mutex::Autolock l(mLock);
4608 if (mCurrentStream == nullptr) {
4609 // End thread if done with work
4610 if (mPendingStreams.empty()) {
4611 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4612 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4613 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4614 mActive = false;
4615 return false;
4616 }
4617
4618 // Get next stream to prepare
4619 auto it = mPendingStreams.begin();
4620 mCurrentStream = *it;
4621 mPendingStreams.erase(it);
4622 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4623 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4624 } else if (mCancelNow) {
4625 mCurrentStream->cancelPrepare();
4626 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4627 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4628 mCurrentStream.clear();
4629 mCancelNow = false;
4630 return true;
4631 }
4632 }
4633
4634 res = mCurrentStream->prepareNextBuffer();
4635 if (res == NOT_ENOUGH_DATA) return true;
4636 if (res != OK) {
4637 // Something bad happened; try to recover by cancelling prepare and
4638 // signalling listener anyway
4639 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4640 mCurrentStream->getId(), res, strerror(-res));
4641 mCurrentStream->cancelPrepare();
4642 }
4643
4644 // This stream has finished, notify listener
4645 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004646 sp<NotificationListener> listener = mListener.promote();
4647 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004648 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4649 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004650 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004651 }
4652
4653 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4654 mCurrentStream.clear();
4655
4656 return true;
4657}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004658
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004659/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004660 * Static callback forwarding methods from HAL to instance
4661 */
4662
4663void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4664 const camera3_capture_result *result) {
4665 Camera3Device *d =
4666 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004667
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004668 d->processCaptureResult(result);
4669}
4670
4671void Camera3Device::sNotify(const camera3_callback_ops *cb,
4672 const camera3_notify_msg *msg) {
4673 Camera3Device *d =
4674 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4675 d->notify(msg);
4676}
4677
4678}; // namespace android