blob: 6e61be25cd01051bc342b6b4086368e5bcd06775 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Shuzhen Wang5c22c152017-12-31 17:12:25 -080042#include <utility>
43
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080044#include <utils/Log.h>
45#include <utils/Trace.h>
46#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070047#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070048
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049#include <android/hardware/camera2/ICameraDeviceUser.h>
50
Igor Murashkinff3e31d2013-10-23 16:40:06 -070051#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070052#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070053#include "device3/Camera3Device.h"
54#include "device3/Camera3OutputStream.h"
55#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070056#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070057#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070058#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070059#include "utils/CameraThreadState.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060
61using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080062using namespace android::hardware::camera;
63using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064
65namespace android {
66
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080067Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080068 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080069 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070070 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070071 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070072 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070073 mUsePartialResult(false),
74 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080075 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070076 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070077 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070078 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070079 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000080 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010081 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
82 mLastTemplateId(-1)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083{
84 ATRACE_CALL();
85 camera3_callback_ops::notify = &sNotify;
86 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080087 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088}
89
90Camera3Device::~Camera3Device()
91{
92 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -070094 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095}
96
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080097const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080098 return mId;
99}
100
Emilian Peevbd8c5032018-02-14 23:05:40 +0000101status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800102 ATRACE_CALL();
103 Mutex::Autolock il(mInterfaceLock);
104 Mutex::Autolock l(mLock);
105
106 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
107 if (mStatus != STATUS_UNINITIALIZED) {
108 CLOGE("Already initialized!");
109 return INVALID_OPERATION;
110 }
111 if (manager == nullptr) return INVALID_OPERATION;
112
113 sp<ICameraDeviceSession> session;
114 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800115 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800116 /*out*/ &session);
117 ATRACE_END();
118 if (res != OK) {
119 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
120 return res;
121 }
122
Steven Moreland5ff9c912017-03-09 23:13:00 -0800123 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800124 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700125 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800126 session->close();
127 return res;
128 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800129
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700130 std::vector<std::string> physicalCameraIds;
131 bool isLogical = CameraProviderManager::isLogicalCamera(mDeviceInfo, &physicalCameraIds);
132 if (isLogical) {
133 for (auto& physicalId : physicalCameraIds) {
134 res = manager->getCameraCharacteristics(physicalId, &mPhysicalDeviceInfoMap[physicalId]);
135 if (res != OK) {
136 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
137 physicalId.c_str(), strerror(-res), res);
138 session->close();
139 return res;
140 }
141 }
142 }
143
Yifan Hongf79b5542017-04-11 14:44:25 -0700144 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700145 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
146 [&queue](const auto& descriptor) {
147 queue = std::make_shared<RequestMetadataQueue>(descriptor);
148 if (!queue->isValid() || queue->availableToWrite() <= 0) {
149 ALOGE("HAL returns empty request metadata fmq, not use it");
150 queue = nullptr;
151 // don't use the queue onwards.
152 }
153 });
154 if (!requestQueueRet.isOk()) {
155 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
156 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700157 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700158 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700159
160 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700161 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700162 [&resQueue](const auto& descriptor) {
163 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
164 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700165 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700166 resQueue = nullptr;
167 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700168 }
169 });
170 if (!resultQueueRet.isOk()) {
171 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
172 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700173 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700174 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700175 IF_ALOGV() {
176 session->interfaceChain([](
177 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
178 ALOGV("Session interface chain:");
179 for (auto iface : interfaceChain) {
180 ALOGV(" %s", iface.c_str());
181 }
182 });
183 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700184
Yin-Chia Yehdb1e8642017-07-14 15:19:30 -0700185 mInterface = new HalInterface(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000186 std::string providerType;
187 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000188 mTagMonitor.initialize(mVendorTagId);
189 if (!monitorTags.isEmpty()) {
190 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
191 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800192
193 return initializeCommonLocked();
194}
195
196status_t Camera3Device::initializeCommonLocked() {
197
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700198 /** Start up status tracker thread */
199 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800200 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 if (res != OK) {
202 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
203 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800204 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700205 mStatusTracker.clear();
206 return res;
207 }
208
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700209 /** Register in-flight map to the status tracker */
210 mInFlightStatusId = mStatusTracker->addComponent();
211
Zhijun He125684a2015-12-26 15:07:30 -0800212 /** Create buffer manager */
213 mBufferManager = new Camera3BufferManager();
214
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000215 Vector<int32_t> sessionParamKeys;
216 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
217 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
218 if (sessionKeysEntry.count > 0) {
219 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
220 }
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700221 /** Start up request queue thread */
Emilian Peevac3ce6c2017-12-12 15:27:02 +0000222 mRequestThread = new RequestThread(this, mStatusTracker, mInterface, sessionParamKeys);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800223 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800224 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700225 SET_ERR_L("Unable to start request queue thread: %s (%d)",
226 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800227 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800228 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800229 return res;
230 }
231
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700232 mPreparerThread = new PreparerThread();
233
Ruben Brunk183f0562015-08-12 12:55:02 -0700234 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800235 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700236 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700237 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800240 // Measure the clock domain offset between camera and video/hw_composer
241 camera_metadata_entry timestampSource =
242 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
243 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
244 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
245 mTimestampOffset = getMonoToBoottimeOffset();
246 }
247
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700248 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100249 camera_metadata_entry partialResultsCount =
250 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
251 if (partialResultsCount.count > 0) {
252 mNumPartialResults = partialResultsCount.data.i32[0];
253 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700254 }
255
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700256 camera_metadata_entry configs =
257 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
258 for (uint32_t i = 0; i < configs.count; i += 4) {
259 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
260 configs.data.i32[i + 3] ==
261 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
262 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
263 configs.data.i32[i + 2]));
264 }
265 }
266
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700267 if (DistortionMapper::isDistortionSupported(mDeviceInfo)) {
268 res = mDistortionMapper.setupStaticInfo(mDeviceInfo);
269 if (res != OK) {
270 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
271 return res;
272 }
273 }
274
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800275 return OK;
276}
277
278status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700279 return disconnectImpl();
280}
281
282status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800285
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700286 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800287
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700288 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700289 std::vector<wp<Camera3StreamInterface>> streams;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700290 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291 {
292 Mutex::Autolock l(mLock);
293 if (mStatus == STATUS_UNINITIALIZED) return res;
294
295 if (mStatus == STATUS_ACTIVE ||
296 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
297 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700298 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700299 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700300 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700301 } else {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700302 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303 if (res != OK) {
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -0700304 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
305 maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 // Continue to close device even in case of error
307 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700308 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800309 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311 if (mStatus == STATUS_ERROR) {
312 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700313 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314
315 if (mStatusTracker != NULL) {
316 mStatusTracker->requestExit();
317 }
318
319 if (mRequestThread != NULL) {
320 mRequestThread->requestExit();
321 }
322
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700323 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
324 for (size_t i = 0; i < mOutputStreams.size(); i++) {
325 streams.push_back(mOutputStreams[i]);
326 }
327 if (mInputStream != nullptr) {
328 streams.push_back(mInputStream);
329 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700330 }
331
332 // Joining done without holding mLock, otherwise deadlocks may ensue
333 // as the threads try to access parent state
334 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
335 // HAL may be in a bad state, so waiting for request thread
336 // (which may be stuck in the HAL processCaptureRequest call)
337 // could be dangerous.
338 mRequestThread->join();
339 }
340
341 if (mStatusTracker != NULL) {
342 mStatusTracker->join();
343 }
344
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800345 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700346 {
347 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800348 mRequestThread.clear();
Emilian Peev2843c362018-09-26 08:49:40 +0100349 Mutex::Autolock stLock(mTrackerLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700350 mStatusTracker.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800351 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700352 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800353
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700354 // Call close without internal mutex held, as the HAL close may need to
355 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800356 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700357
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700358 flushInflightRequests();
359
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700360 {
361 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800362 mInterface->clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700363 mOutputStreams.clear();
364 mInputStream.clear();
Yin-Chia Yeh5090c732017-07-20 16:05:29 -0700365 mDeletedStreams.clear();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700366 mBufferManager.clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700367 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700368 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800369
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700370 for (auto& weakStream : streams) {
371 sp<Camera3StreamInterface> stream = weakStream.promote();
372 if (stream != nullptr) {
373 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
374 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
375 }
376 }
377
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700378 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700379 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800380}
381
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382// For dumping/debugging only -
383// try to acquire a lock a few times, eventually give up to proceed with
384// debug/dump operations
385bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
386 bool gotLock = false;
387 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
388 if (lock.tryLock() == NO_ERROR) {
389 gotLock = true;
390 break;
391 } else {
392 usleep(kDumpSleepDuration);
393 }
394 }
395 return gotLock;
396}
397
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700398Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
399 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100400 const int STREAM_CONFIGURATION_SIZE = 4;
401 const int STREAM_FORMAT_OFFSET = 0;
402 const int STREAM_WIDTH_OFFSET = 1;
403 const int STREAM_HEIGHT_OFFSET = 2;
404 const int STREAM_IS_INPUT_OFFSET = 3;
405 camera_metadata_ro_entry_t availableStreamConfigs =
406 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
407 if (availableStreamConfigs.count == 0 ||
408 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
409 return Size(0, 0);
410 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700411
Emilian Peev08dd2452017-04-06 16:55:14 +0100412 // Get max jpeg size (area-wise).
413 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
414 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
415 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
416 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
417 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
418 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
419 && format == HAL_PIXEL_FORMAT_BLOB &&
420 (width * height > maxJpegWidth * maxJpegHeight)) {
421 maxJpegWidth = width;
422 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700423 }
424 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100425
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700426 return Size(maxJpegWidth, maxJpegHeight);
427}
428
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800429nsecs_t Camera3Device::getMonoToBoottimeOffset() {
430 // try three times to get the clock offset, choose the one
431 // with the minimum gap in measurements.
432 const int tries = 3;
433 nsecs_t bestGap, measured;
434 for (int i = 0; i < tries; ++i) {
435 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
436 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
437 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
438 const nsecs_t gap = tmono2 - tmono;
439 if (i == 0 || gap < bestGap) {
440 bestGap = gap;
441 measured = tbase - ((tmono + tmono2) >> 1);
442 }
443 }
444 return measured;
445}
446
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800447hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
448 int frameworkFormat) {
449 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
450}
451
452DataspaceFlags Camera3Device::mapToHidlDataspace(
453 android_dataspace dataSpace) {
454 return dataSpace;
455}
456
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700457BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100458 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700459 return usage;
460}
461
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800462StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
463 switch (rotation) {
464 case CAMERA3_STREAM_ROTATION_0:
465 return StreamRotation::ROTATION_0;
466 case CAMERA3_STREAM_ROTATION_90:
467 return StreamRotation::ROTATION_90;
468 case CAMERA3_STREAM_ROTATION_180:
469 return StreamRotation::ROTATION_180;
470 case CAMERA3_STREAM_ROTATION_270:
471 return StreamRotation::ROTATION_270;
472 }
473 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
474 return StreamRotation::ROTATION_0;
475}
476
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800477status_t Camera3Device::mapToStreamConfigurationMode(
478 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
479 if (mode == nullptr) return BAD_VALUE;
480 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
481 switch(operationMode) {
482 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
483 *mode = StreamConfigurationMode::NORMAL_MODE;
484 break;
485 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
486 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
487 break;
488 default:
489 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
490 return BAD_VALUE;
491 }
492 } else {
493 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800494 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800495 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800496}
497
498camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
499 switch (status) {
500 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
501 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
502 }
503 return CAMERA3_BUFFER_STATUS_ERROR;
504}
505
506int Camera3Device::mapToFrameworkFormat(
507 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
508 return static_cast<uint32_t>(pixelFormat);
509}
510
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700511android_dataspace Camera3Device::mapToFrameworkDataspace(
512 DataspaceFlags dataSpace) {
513 return static_cast<android_dataspace>(dataSpace);
514}
515
Emilian Peev050f5dc2017-05-18 14:43:56 +0100516uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700517 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700518 return usage;
519}
520
Emilian Peev050f5dc2017-05-18 14:43:56 +0100521uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700522 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700523 return usage;
524}
525
Zhijun Hef7da0962014-04-24 13:27:56 -0700526ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700527 // Get max jpeg size (area-wise).
528 Size maxJpegResolution = getMaxJpegResolution();
529 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800530 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
531 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700532 return BAD_VALUE;
533 }
534
Zhijun Hef7da0962014-04-24 13:27:56 -0700535 // Get max jpeg buffer size
536 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700537 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
538 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800539 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
540 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700541 return BAD_VALUE;
542 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700543 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800544 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700545
546 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700547 float scaleFactor = ((float) (width * height)) /
548 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800549 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
550 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700551 if (jpegBufferSize > maxJpegBufferSize) {
552 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700553 }
554
555 return jpegBufferSize;
556}
557
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700558ssize_t Camera3Device::getPointCloudBufferSize() const {
559 const int FLOATS_PER_POINT=4;
560 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
561 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800562 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
563 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700564 return BAD_VALUE;
565 }
566 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
567 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
568 return maxBytesForPointCloud;
569}
570
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800571ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800572 const int PER_CONFIGURATION_SIZE = 3;
573 const int WIDTH_OFFSET = 0;
574 const int HEIGHT_OFFSET = 1;
575 const int SIZE_OFFSET = 2;
576 camera_metadata_ro_entry rawOpaqueSizes =
577 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800578 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800579 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800580 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
581 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800582 return BAD_VALUE;
583 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700584
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800585 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
586 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
587 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
588 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
589 }
590 }
591
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800592 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
593 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800594 return BAD_VALUE;
595}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700596
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800597status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
598 ATRACE_CALL();
599 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700600
601 // Try to lock, but continue in case of failure (to avoid blocking in
602 // deadlocks)
603 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
604 bool gotLock = tryLockSpinRightRound(mLock);
605
606 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800607 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
608 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700609 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800610 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
611 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800613 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700614
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800615 String16 templatesOption("-t");
616 int n = args.size();
617 for (int i = 0; i < n; i++) {
618 if (args[i] == templatesOption) {
619 dumpTemplates = true;
620 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000621 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700622 if (i + 1 < n) {
623 String8 monitorTags = String8(args[i + 1]);
624 if (monitorTags == "off") {
625 mTagMonitor.disableMonitoring();
626 } else {
627 mTagMonitor.parseTagsToMonitor(monitorTags);
628 }
629 } else {
630 mTagMonitor.disableMonitoring();
631 }
632 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800633 }
634
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800636
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 const char *status =
638 mStatus == STATUS_ERROR ? "ERROR" :
639 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
641 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800642 mStatus == STATUS_ACTIVE ? "ACTIVE" :
643 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700644
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700646 if (mStatus == STATUS_ERROR) {
647 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
648 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800650 const char *mode =
651 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
652 mOperatingMode == static_cast<int>(
653 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
654 "CUSTOM";
655 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800656
657 if (mInputStream != NULL) {
658 write(fd, lines.string(), lines.size());
659 mInputStream->dump(fd, args);
660 } else {
661 lines.appendFormat(" No input stream.\n");
662 write(fd, lines.string(), lines.size());
663 }
664 for (size_t i = 0; i < mOutputStreams.size(); i++) {
665 mOutputStreams[i]->dump(fd,args);
666 }
667
Zhijun He431503c2016-03-07 17:30:16 -0800668 if (mBufferManager != NULL) {
669 lines = String8(" Camera3 Buffer Manager:\n");
670 write(fd, lines.string(), lines.size());
671 mBufferManager->dump(fd, args);
672 }
Zhijun He125684a2015-12-26 15:07:30 -0800673
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700674 lines = String8(" In-flight requests:\n");
675 if (mInFlightMap.size() == 0) {
676 lines.append(" None\n");
677 } else {
678 for (size_t i = 0; i < mInFlightMap.size(); i++) {
679 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700680 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700681 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800682 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700683 r.numBuffersLeft);
684 }
685 }
686 write(fd, lines.string(), lines.size());
687
Shuzhen Wang686f6442017-06-20 16:16:04 -0700688 if (mRequestThread != NULL) {
689 mRequestThread->dumpCaptureRequestLatency(fd,
690 " ProcessCaptureRequest latency histogram:");
691 }
692
Igor Murashkin1e479c02013-09-06 16:55:14 -0700693 {
694 lines = String8(" Last request sent:\n");
695 write(fd, lines.string(), lines.size());
696
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700697 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700698 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
699 }
700
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800701 if (dumpTemplates) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800702 const char *templateNames[CAMERA3_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800703 "TEMPLATE_PREVIEW",
704 "TEMPLATE_STILL_CAPTURE",
705 "TEMPLATE_VIDEO_RECORD",
706 "TEMPLATE_VIDEO_SNAPSHOT",
707 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800708 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800709 };
710
711 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800712 camera_metadata_t *templateRequest = nullptr;
713 mInterface->constructDefaultRequestSettings(
714 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800715 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800716 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800717 lines.append(" Not supported\n");
718 write(fd, lines.string(), lines.size());
719 } else {
720 write(fd, lines.string(), lines.size());
721 dump_indented_camera_metadata(templateRequest,
722 fd, /*verbosity*/2, /*indentation*/8);
723 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800724 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800725 }
726 }
727
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700728 mTagMonitor.dumpMonitoredMetadata(fd);
729
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800730 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800731 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800732 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800733 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800734 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800735
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736 if (gotLock) mLock.unlock();
737 if (gotInterfaceLock) mInterfaceLock.unlock();
738
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739 return OK;
740}
741
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700742const CameraMetadata& Camera3Device::info(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800743 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800744 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
745 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700746 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747 mStatus == STATUS_ERROR ?
748 "when in error state" : "before init");
749 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700750 if (physicalId.isEmpty()) {
751 return mDeviceInfo;
752 } else {
753 std::string id(physicalId.c_str());
754 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
755 return mPhysicalDeviceInfoMap.at(id);
756 } else {
757 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
758 return mDeviceInfo;
759 }
760 }
761}
762
763const CameraMetadata& Camera3Device::info() const {
764 String8 emptyId;
765 return info(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800766}
767
Jianing Wei90e59c92014-03-12 18:29:36 -0700768status_t Camera3Device::checkStatusOkToCaptureLocked() {
769 switch (mStatus) {
770 case STATUS_ERROR:
771 CLOGE("Device has encountered a serious error");
772 return INVALID_OPERATION;
773 case STATUS_UNINITIALIZED:
774 CLOGE("Device not initialized");
775 return INVALID_OPERATION;
776 case STATUS_UNCONFIGURED:
777 case STATUS_CONFIGURED:
778 case STATUS_ACTIVE:
779 // OK
780 break;
781 default:
782 SET_ERR_L("Unexpected status: %d", mStatus);
783 return INVALID_OPERATION;
784 }
785 return OK;
786}
787
788status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000789 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790 const std::list<const SurfaceMap> &surfaceMaps,
791 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700792 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700793 if (requestList == NULL) {
794 CLOGE("requestList cannot be NULL.");
795 return BAD_VALUE;
796 }
797
Jianing Weicb0652e2014-03-12 18:29:36 -0700798 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000799 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700800 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
801 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
802 ++metadataIt, ++surfaceMapIt) {
803 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700804 if (newRequest == 0) {
805 CLOGE("Can't create capture request");
806 return BAD_VALUE;
807 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700808
Shuzhen Wang9d066012016-09-30 11:30:20 -0700809 newRequest->mRepeating = repeating;
810
Jianing Weicb0652e2014-03-12 18:29:36 -0700811 // Setup burst Id and request Id
812 newRequest->mResultExtras.burstId = burstId++;
Emilian Peevaebbe412018-01-15 13:53:24 +0000813 if (metadataIt->begin()->metadata.exists(ANDROID_REQUEST_ID)) {
814 if (metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700815 CLOGE("RequestID entry exists; but must not be empty in metadata");
816 return BAD_VALUE;
817 }
Emilian Peevaebbe412018-01-15 13:53:24 +0000818 newRequest->mResultExtras.requestId = metadataIt->begin()->metadata.find(
819 ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700820 } else {
821 CLOGE("RequestID does not exist in metadata");
822 return BAD_VALUE;
823 }
824
Jianing Wei90e59c92014-03-12 18:29:36 -0700825 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700826
827 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700828 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700829 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
830 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
831 return BAD_VALUE;
832 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700833
834 // Setup batch size if this is a high speed video recording request.
835 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
836 auto firstRequest = requestList->begin();
837 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
838 if (outputStream->isVideoStream()) {
839 (*firstRequest)->mBatchSize = requestList->size();
840 break;
841 }
842 }
843 }
844
Jianing Wei90e59c92014-03-12 18:29:36 -0700845 return OK;
846}
847
Jianing Weicb0652e2014-03-12 18:29:36 -0700848status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800849 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800850
Emilian Peevaebbe412018-01-15 13:53:24 +0000851 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700852 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000853 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700854
Emilian Peevaebbe412018-01-15 13:53:24 +0000855 return captureList(requestsList, surfaceMaps, /*lastFrameNumber*/NULL);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700856}
857
Emilian Peevaebbe412018-01-15 13:53:24 +0000858void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700859 std::list<const SurfaceMap>& surfaceMaps,
860 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000861 PhysicalCameraSettingsList requestList;
862 requestList.push_back({std::string(getId().string()), request});
863 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700864
865 SurfaceMap surfaceMap;
866 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
867 // With no surface list passed in, stream and surface will have 1-to-1
868 // mapping. So the surface index is 0 for each stream in the surfaceMap.
869 for (size_t i = 0; i < streams.count; i++) {
870 surfaceMap[streams.data.i32[i]].push_back(0);
871 }
872 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800873}
874
Jianing Wei90e59c92014-03-12 18:29:36 -0700875status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000876 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700877 const std::list<const SurfaceMap> &surfaceMaps,
878 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700879 /*out*/
880 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700881 ATRACE_CALL();
882 Mutex::Autolock il(mInterfaceLock);
883 Mutex::Autolock l(mLock);
884
885 status_t res = checkStatusOkToCaptureLocked();
886 if (res != OK) {
887 // error logged by previous call
888 return res;
889 }
890
891 RequestList requestList;
892
Shuzhen Wang0129d522016-10-30 22:43:41 -0700893 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
894 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700895 if (res != OK) {
896 // error logged by previous call
897 return res;
898 }
899
900 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700901 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700902 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700903 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700904 }
905
906 if (res == OK) {
907 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
908 if (res != OK) {
909 SET_ERR_L("Can't transition to active in %f seconds!",
910 kActiveTimeout/1e9);
911 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800912 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700913 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700914 } else {
915 CLOGE("Cannot queue request. Impossible.");
916 return BAD_VALUE;
917 }
918
919 return res;
920}
921
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800922hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800923 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800924 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -0700925 // Ideally we should grab mLock, but that can lead to deadlock, and
926 // it's not super important to get up to date value of mStatus for this
927 // warning print, hence skipping the lock here
928 if (mStatus == STATUS_ERROR) {
929 // Per API contract, HAL should act as closed after device error
930 // But mStatus can be set to error by framework as well, so just log
931 // a warning here.
932 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700933 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700934
935 if (mProcessCaptureResultLock.tryLock() != OK) {
936 // This should never happen; it indicates a wrong client implementation
937 // that doesn't follow the contract. But, we can be tolerant here.
938 ALOGE("%s: callback overlapped! waiting 1s...",
939 __FUNCTION__);
940 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
941 ALOGE("%s: cannot acquire lock in 1s, dropping results",
942 __FUNCTION__);
943 // really don't know what to do, so bail out.
944 return hardware::Void();
945 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800946 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700947 for (const auto& result : results) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800948 processOneCaptureResultLocked(result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -0700949 }
950 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800951 return hardware::Void();
952}
953
Shuzhen Wang5c22c152017-12-31 17:12:25 -0800954// Only one processCaptureResult should be called at a time, so
955// the locks won't block. The locks are present here simply to enforce this.
956hardware::Return<void> Camera3Device::processCaptureResult(
957 const hardware::hidl_vec<
958 hardware::camera::device::V3_2::CaptureResult>& results) {
959 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
960
961 // Ideally we should grab mLock, but that can lead to deadlock, and
962 // it's not super important to get up to date value of mStatus for this
963 // warning print, hence skipping the lock here
964 if (mStatus == STATUS_ERROR) {
965 // Per API contract, HAL should act as closed after device error
966 // But mStatus can be set to error by framework as well, so just log
967 // a warning here.
968 ALOGW("%s: received capture result in error state.", __FUNCTION__);
969 }
970
971 if (mProcessCaptureResultLock.tryLock() != OK) {
972 // This should never happen; it indicates a wrong client implementation
973 // that doesn't follow the contract. But, we can be tolerant here.
974 ALOGE("%s: callback overlapped! waiting 1s...",
975 __FUNCTION__);
976 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
977 ALOGE("%s: cannot acquire lock in 1s, dropping results",
978 __FUNCTION__);
979 // really don't know what to do, so bail out.
980 return hardware::Void();
981 }
982 }
983 for (const auto& result : results) {
984 processOneCaptureResultLocked(result, noPhysMetadata);
985 }
986 mProcessCaptureResultLock.unlock();
987 return hardware::Void();
988}
989
990status_t Camera3Device::readOneCameraMetadataLocked(
991 uint64_t fmqResultSize, hardware::camera::device::V3_2::CameraMetadata& resultMetadata,
992 const hardware::camera::device::V3_2::CameraMetadata& result) {
993 if (fmqResultSize > 0) {
994 resultMetadata.resize(fmqResultSize);
995 if (mResultMetadataQueue == nullptr) {
996 return NO_MEMORY; // logged in initialize()
997 }
998 if (!mResultMetadataQueue->read(resultMetadata.data(), fmqResultSize)) {
999 ALOGE("%s: Cannot read camera metadata from fmq, size = %" PRIu64,
1000 __FUNCTION__, fmqResultSize);
1001 return INVALID_OPERATION;
1002 }
1003 } else {
1004 resultMetadata.setToExternal(const_cast<uint8_t *>(result.data()),
1005 result.size());
1006 }
1007
1008 if (resultMetadata.size() != 0) {
1009 status_t res;
1010 const camera_metadata_t* metadata =
1011 reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
1012 size_t expected_metadata_size = resultMetadata.size();
1013 if ((res = validate_camera_metadata_structure(metadata, &expected_metadata_size)) != OK) {
1014 ALOGE("%s: Invalid camera metadata received by camera service from HAL: %s (%d)",
1015 __FUNCTION__, strerror(-res), res);
1016 return INVALID_OPERATION;
1017 }
1018 }
1019
1020 return OK;
1021}
1022
Yifan Honga640c5a2017-04-12 16:30:31 -07001023void Camera3Device::processOneCaptureResultLocked(
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001024 const hardware::camera::device::V3_2::CaptureResult& result,
1025 const hardware::hidl_vec<
1026 hardware::camera::device::V3_4::PhysicalCameraMetadata> physicalCameraMetadatas) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001027 camera3_capture_result r;
1028 status_t res;
1029 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -07001030
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001031 // Read and validate the result metadata.
Yifan Honga640c5a2017-04-12 16:30:31 -07001032 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001033 res = readOneCameraMetadataLocked(result.fmqResultSize, resultMetadata, result.result);
1034 if (res != OK) {
1035 ALOGE("%s: Frame %d: Failed to read capture result metadata",
1036 __FUNCTION__, result.frameNumber);
1037 return;
Yifan Honga640c5a2017-04-12 16:30:31 -07001038 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001039 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
Yifan Honga640c5a2017-04-12 16:30:31 -07001040
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001041 // Read and validate physical camera metadata
1042 size_t physResultCount = physicalCameraMetadatas.size();
1043 std::vector<const char*> physCamIds(physResultCount);
1044 std::vector<const camera_metadata_t *> phyCamMetadatas(physResultCount);
1045 std::vector<hardware::camera::device::V3_2::CameraMetadata> physResultMetadata;
1046 physResultMetadata.resize(physResultCount);
1047 for (size_t i = 0; i < physicalCameraMetadatas.size(); i++) {
1048 res = readOneCameraMetadataLocked(physicalCameraMetadatas[i].fmqMetadataSize,
1049 physResultMetadata[i], physicalCameraMetadatas[i].metadata);
1050 if (res != OK) {
1051 ALOGE("%s: Frame %d: Failed to read capture result metadata for camera %s",
1052 __FUNCTION__, result.frameNumber,
1053 physicalCameraMetadatas[i].physicalCameraId.c_str());
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001054 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001055 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001056 physCamIds[i] = physicalCameraMetadatas[i].physicalCameraId.c_str();
1057 phyCamMetadatas[i] = reinterpret_cast<const camera_metadata_t*>(
1058 physResultMetadata[i].data());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001059 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001060 r.num_physcam_metadata = physResultCount;
1061 r.physcam_ids = physCamIds.data();
1062 r.physcam_metadata = phyCamMetadatas.data();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001063
1064 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
1065 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
1066 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
1067 auto& bDst = outputBuffers[i];
1068 const StreamBuffer &bSrc = result.outputBuffers[i];
1069
1070 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001071 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001072 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
1073 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001074 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001075 }
1076 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
1077
1078 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08001079 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001080 if (res != OK) {
1081 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1082 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001083 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001084 }
1085 bDst.buffer = buffer;
1086 bDst.status = mapHidlBufferStatus(bSrc.status);
1087 bDst.acquire_fence = -1;
1088 if (bSrc.releaseFence == nullptr) {
1089 bDst.release_fence = -1;
1090 } else if (bSrc.releaseFence->numFds == 1) {
1091 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1092 } else {
1093 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1094 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001095 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001096 }
1097 }
1098 r.num_output_buffers = outputBuffers.size();
1099 r.output_buffers = outputBuffers.data();
1100
1101 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001102 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001103 r.input_buffer = nullptr;
1104 } else {
1105 if (mInputStream->getId() != result.inputBuffer.streamId) {
1106 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1107 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001108 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001109 }
1110 inputBuffer.stream = mInputStream->asHalStream();
1111 buffer_handle_t *buffer;
1112 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1113 &buffer);
1114 if (res != OK) {
1115 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1116 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001117 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001118 }
1119 inputBuffer.buffer = buffer;
1120 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1121 inputBuffer.acquire_fence = -1;
1122 if (result.inputBuffer.releaseFence == nullptr) {
1123 inputBuffer.release_fence = -1;
1124 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1125 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1126 } else {
1127 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1128 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001129 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001130 }
1131 r.input_buffer = &inputBuffer;
1132 }
1133
1134 r.partial_result = result.partialResult;
1135
1136 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001137}
1138
1139hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001140 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001141 // Ideally we should grab mLock, but that can lead to deadlock, and
1142 // it's not super important to get up to date value of mStatus for this
1143 // warning print, hence skipping the lock here
1144 if (mStatus == STATUS_ERROR) {
1145 // Per API contract, HAL should act as closed after device error
1146 // But mStatus can be set to error by framework as well, so just log
1147 // a warning here.
1148 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001149 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001150
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001151 for (const auto& msg : msgs) {
1152 notify(msg);
1153 }
1154 return hardware::Void();
1155}
1156
1157void Camera3Device::notify(
1158 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1159
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001160 camera3_notify_msg m;
1161 switch (msg.type) {
1162 case MsgType::ERROR:
1163 m.type = CAMERA3_MSG_ERROR;
1164 m.message.error.frame_number = msg.msg.error.frameNumber;
1165 if (msg.msg.error.errorStreamId >= 0) {
1166 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001167 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001168 ALOGE("%s: Frame %d: Invalid error stream id %d",
1169 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001170 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001171 }
1172 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1173 } else {
1174 m.message.error.error_stream = nullptr;
1175 }
1176 switch (msg.msg.error.errorCode) {
1177 case ErrorCode::ERROR_DEVICE:
1178 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1179 break;
1180 case ErrorCode::ERROR_REQUEST:
1181 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1182 break;
1183 case ErrorCode::ERROR_RESULT:
1184 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1185 break;
1186 case ErrorCode::ERROR_BUFFER:
1187 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1188 break;
1189 }
1190 break;
1191 case MsgType::SHUTTER:
1192 m.type = CAMERA3_MSG_SHUTTER;
1193 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1194 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1195 break;
1196 }
1197 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001198}
1199
Emilian Peevaebbe412018-01-15 13:53:24 +00001200status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001201 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001202 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001203 ATRACE_CALL();
1204
Emilian Peevaebbe412018-01-15 13:53:24 +00001205 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001206}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001207
Jianing Weicb0652e2014-03-12 18:29:36 -07001208status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1209 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001210 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001211
Emilian Peevaebbe412018-01-15 13:53:24 +00001212 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001213 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001214 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001215
Emilian Peevaebbe412018-01-15 13:53:24 +00001216 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001217 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218}
1219
Emilian Peevaebbe412018-01-15 13:53:24 +00001220status_t Camera3Device::setStreamingRequestList(
1221 const List<const PhysicalCameraSettingsList> &requestsList,
1222 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001223 ATRACE_CALL();
1224
Emilian Peevaebbe412018-01-15 13:53:24 +00001225 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001226}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227
1228sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001229 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 status_t res;
1231
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001232 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001233 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1234 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001235 res = filterParamsAndConfigureLocked(request.begin()->metadata,
1236 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001237 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001239 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001241 } else if (mStatus == STATUS_UNCONFIGURED) {
1242 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001243 CLOGE("No streams configured");
1244 return NULL;
1245 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 }
1247
Shuzhen Wang0129d522016-10-30 22:43:41 -07001248 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001250}
1251
Jianing Weicb0652e2014-03-12 18:29:36 -07001252status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001253 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001254 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001255 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001256
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 switch (mStatus) {
1258 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001259 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001260 return INVALID_OPERATION;
1261 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001262 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001263 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001264 case STATUS_UNCONFIGURED:
1265 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 case STATUS_ACTIVE:
1267 // OK
1268 break;
1269 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 return INVALID_OPERATION;
1272 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001273 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001274
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001275 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276}
1277
1278status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1279 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001280 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001281
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001282 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001283}
1284
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001285status_t Camera3Device::createInputStream(
1286 uint32_t width, uint32_t height, int format, int *id) {
1287 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001289 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001290 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001291 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1292 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001293
1294 status_t res;
1295 bool wasActive = false;
1296
1297 switch (mStatus) {
1298 case STATUS_ERROR:
1299 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1300 return INVALID_OPERATION;
1301 case STATUS_UNINITIALIZED:
1302 ALOGE("%s: Device not initialized", __FUNCTION__);
1303 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 case STATUS_UNCONFIGURED:
1305 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001306 // OK
1307 break;
1308 case STATUS_ACTIVE:
1309 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001310 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001311 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001312 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001313 return res;
1314 }
1315 wasActive = true;
1316 break;
1317 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001318 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001319 return INVALID_OPERATION;
1320 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001322
1323 if (mInputStream != 0) {
1324 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1325 return INVALID_OPERATION;
1326 }
1327
1328 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1329 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001330 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001331
1332 mInputStream = newStream;
1333
1334 *id = mNextStreamId++;
1335
1336 // Continue captures if active at start
1337 if (wasActive) {
1338 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001339 // Reuse current operating mode and session parameters for new stream config
1340 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001341 if (res != OK) {
1342 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1343 __FUNCTION__, mNextStreamId, strerror(-res), res);
1344 return res;
1345 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001346 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001347 }
1348
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001349 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001350 return OK;
1351}
1352
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001353status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001354 uint32_t width, uint32_t height, int format,
1355 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001356 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001357 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001358 ATRACE_CALL();
1359
1360 if (consumer == nullptr) {
1361 ALOGE("%s: consumer must not be null", __FUNCTION__);
1362 return BAD_VALUE;
1363 }
1364
1365 std::vector<sp<Surface>> consumers;
1366 consumers.push_back(consumer);
1367
1368 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001369 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
1370 isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001371}
1372
1373status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1374 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1375 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001376 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +01001377 std::vector<int> *surfaceIds, int streamSetId, bool isShared, uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001378 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001379
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001380 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001381 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001382 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001383 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001384 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s", mId.string(),
1385 mNextStreamId, width, height, format, dataSpace, rotation, consumerUsage, isShared,
1386 physicalCameraId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001387
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 status_t res;
1389 bool wasActive = false;
1390
1391 switch (mStatus) {
1392 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001393 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 return INVALID_OPERATION;
1395 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001396 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001397 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001398 case STATUS_UNCONFIGURED:
1399 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 // OK
1401 break;
1402 case STATUS_ACTIVE:
1403 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001404 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001405 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001407 return res;
1408 }
1409 wasActive = true;
1410 break;
1411 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001412 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001413 return INVALID_OPERATION;
1414 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001415 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416
1417 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001418
Shuzhen Wang0129d522016-10-30 22:43:41 -07001419 if (consumers.size() == 0 && !hasDeferredConsumer) {
1420 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1421 return BAD_VALUE;
1422 }
Zhijun He5d677d12016-05-29 16:52:39 -07001423
Shuzhen Wang0129d522016-10-30 22:43:41 -07001424 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001425 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1426 return BAD_VALUE;
1427 }
1428
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001430 ssize_t blobBufferSize;
1431 if (dataSpace != HAL_DATASPACE_DEPTH) {
1432 blobBufferSize = getJpegBufferSize(width, height);
1433 if (blobBufferSize <= 0) {
1434 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1435 return BAD_VALUE;
1436 }
1437 } else {
1438 blobBufferSize = getPointCloudBufferSize();
1439 if (blobBufferSize <= 0) {
1440 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1441 return BAD_VALUE;
1442 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001443 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001444 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001445 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001446 mTimestampOffset, physicalCameraId, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001447 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1448 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1449 if (rawOpaqueBufferSize <= 0) {
1450 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1451 return BAD_VALUE;
1452 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001453 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001454 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001455 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001456 } else if (isShared) {
1457 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1458 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001459 mTimestampOffset, physicalCameraId, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001460 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001461 newStream = new Camera3OutputStream(mNextStreamId,
1462 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001463 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001464 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001465 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001466 width, height, format, dataSpace, rotation,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001467 mTimestampOffset, physicalCameraId, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001468 }
Emilian Peev40ead602017-09-26 15:46:36 +01001469
1470 size_t consumerCount = consumers.size();
1471 for (size_t i = 0; i < consumerCount; i++) {
1472 int id = newStream->getSurfaceId(consumers[i]);
1473 if (id < 0) {
1474 SET_ERR_L("Invalid surface id");
1475 return BAD_VALUE;
1476 }
1477 if (surfaceIds != nullptr) {
1478 surfaceIds->push_back(id);
1479 }
1480 }
1481
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001482 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483
Emilian Peev08dd2452017-04-06 16:55:14 +01001484 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001485
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 res = mOutputStreams.add(mNextStreamId, newStream);
1487 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001488 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001489 return res;
1490 }
1491
1492 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001493 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494
1495 // Continue captures if active at start
1496 if (wasActive) {
1497 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001498 // Reuse current operating mode and session parameters for new stream config
1499 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001500 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001501 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1502 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001503 return res;
1504 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001505 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001506 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001507 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001508 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001509}
1510
Emilian Peev710c1422017-08-30 11:19:38 +01001511status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001513 if (nullptr == streamInfo) {
1514 return BAD_VALUE;
1515 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001516 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 switch (mStatus) {
1520 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001521 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522 return INVALID_OPERATION;
1523 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001524 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001526 case STATUS_UNCONFIGURED:
1527 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001528 case STATUS_ACTIVE:
1529 // OK
1530 break;
1531 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001532 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001533 return INVALID_OPERATION;
1534 }
1535
1536 ssize_t idx = mOutputStreams.indexOfKey(id);
1537 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001538 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001539 return idx;
1540 }
1541
Emilian Peev710c1422017-08-30 11:19:38 +01001542 streamInfo->width = mOutputStreams[idx]->getWidth();
1543 streamInfo->height = mOutputStreams[idx]->getHeight();
1544 streamInfo->format = mOutputStreams[idx]->getFormat();
1545 streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace();
1546 streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden();
1547 streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat();
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07001548 streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden();
1549 streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001550 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001551}
1552
1553status_t Camera3Device::setStreamTransform(int id,
1554 int transform) {
1555 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 switch (mStatus) {
1560 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001561 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 return INVALID_OPERATION;
1563 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001564 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001565 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 case STATUS_UNCONFIGURED:
1567 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 case STATUS_ACTIVE:
1569 // OK
1570 break;
1571 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001572 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 return INVALID_OPERATION;
1574 }
1575
1576 ssize_t idx = mOutputStreams.indexOfKey(id);
1577 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001578 CLOGE("Stream %d does not exist",
1579 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001580 return BAD_VALUE;
1581 }
1582
1583 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001584}
1585
1586status_t Camera3Device::deleteStream(int id) {
1587 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001588 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001589 Mutex::Autolock l(mLock);
1590 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001592 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001593
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001594 // CameraDevice semantics require device to already be idle before
1595 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001596 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001597 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001598 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001599 }
1600
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001601 if (mStatus == STATUS_ERROR) {
1602 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1603 __FUNCTION__, mId.string());
1604 return -EBUSY;
1605 }
1606
Igor Murashkin2fba5842013-04-22 14:03:54 -07001607 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001608 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001609 if (mInputStream != NULL && id == mInputStream->getId()) {
1610 deletedStream = mInputStream;
1611 mInputStream.clear();
1612 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001613 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001614 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615 return BAD_VALUE;
1616 }
Zhijun He5f446352014-01-22 09:49:33 -08001617 }
1618
1619 // Delete output stream or the output part of a bi-directional stream.
1620 if (outputStreamIdx != NAME_NOT_FOUND) {
1621 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001622 mOutputStreams.removeItem(id);
1623 }
1624
1625 // Free up the stream endpoint so that it can be used by some other stream
1626 res = deletedStream->disconnect();
1627 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001628 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001629 // fall through since we want to still list the stream as deleted.
1630 }
1631 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001632 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001633
1634 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001635}
1636
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001637status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001638 ATRACE_CALL();
1639 ALOGV("%s: E", __FUNCTION__);
1640
1641 Mutex::Autolock il(mInterfaceLock);
1642 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001643
Emilian Peev811d2952018-05-25 11:08:40 +01001644 // In case the client doesn't include any session parameter, try a
1645 // speculative configuration using the values from the last cached
1646 // default request.
1647 if (sessionParams.isEmpty() &&
1648 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA3_TEMPLATE_COUNT)) &&
1649 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1650 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1651 mLastTemplateId);
1652 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1653 operatingMode);
1654 }
1655
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001656 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1657}
1658
1659status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1660 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001661 //Filter out any incoming session parameters
1662 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001663 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1664 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001665 CameraMetadata filteredParams(availableSessionKeys.count);
1666 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1667 filteredParams.getAndLock());
1668 set_camera_metadata_vendor_id(meta, mVendorTagId);
1669 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001670 if (availableSessionKeys.count > 0) {
1671 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1672 camera_metadata_ro_entry entry = params.find(
1673 availableSessionKeys.data.i32[i]);
1674 if (entry.count > 0) {
1675 filteredParams.update(entry);
1676 }
1677 }
1678 }
1679
1680 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001681}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001682
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001683status_t Camera3Device::getInputBufferProducer(
1684 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001685 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001686 Mutex::Autolock il(mInterfaceLock);
1687 Mutex::Autolock l(mLock);
1688
1689 if (producer == NULL) {
1690 return BAD_VALUE;
1691 } else if (mInputStream == NULL) {
1692 return INVALID_OPERATION;
1693 }
1694
1695 return mInputStream->getInputBufferProducer(producer);
1696}
1697
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001698status_t Camera3Device::createDefaultRequest(int templateId,
1699 CameraMetadata *request) {
1700 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001701 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001702
1703 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1704 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001705 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001706 return BAD_VALUE;
1707 }
1708
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001709 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001710
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001711 {
1712 Mutex::Autolock l(mLock);
1713 switch (mStatus) {
1714 case STATUS_ERROR:
1715 CLOGE("Device has encountered a serious error");
1716 return INVALID_OPERATION;
1717 case STATUS_UNINITIALIZED:
1718 CLOGE("Device is not initialized!");
1719 return INVALID_OPERATION;
1720 case STATUS_UNCONFIGURED:
1721 case STATUS_CONFIGURED:
1722 case STATUS_ACTIVE:
1723 // OK
1724 break;
1725 default:
1726 SET_ERR_L("Unexpected status: %d", mStatus);
1727 return INVALID_OPERATION;
1728 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001729
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001730 if (!mRequestTemplateCache[templateId].isEmpty()) {
1731 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001732 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001733 return OK;
1734 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001735 }
1736
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001737 camera_metadata_t *rawRequest;
1738 status_t res = mInterface->constructDefaultRequestSettings(
1739 (camera3_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001740
1741 {
1742 Mutex::Autolock l(mLock);
1743 if (res == BAD_VALUE) {
1744 ALOGI("%s: template %d is not supported on this camera device",
1745 __FUNCTION__, templateId);
1746 return res;
1747 } else if (res != OK) {
1748 CLOGE("Unable to construct request template %d: %s (%d)",
1749 templateId, strerror(-res), res);
1750 return res;
1751 }
1752
1753 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1754 mRequestTemplateCache[templateId].acquire(rawRequest);
1755
1756 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001757 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001758 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001759 return OK;
1760}
1761
1762status_t Camera3Device::waitUntilDrained() {
1763 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001764 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001765 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001766 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001767
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001768 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001769}
1770
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001771status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001772 switch (mStatus) {
1773 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001774 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001775 ALOGV("%s: Already idle", __FUNCTION__);
1776 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001777 case STATUS_CONFIGURED:
1778 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001779 case STATUS_ERROR:
1780 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001781 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001782 break;
1783 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001784 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001785 return INVALID_OPERATION;
1786 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001787 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1788 maxExpectedDuration);
1789 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001790 if (res != OK) {
1791 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1792 res);
1793 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001794 return res;
1795}
1796
Ruben Brunk183f0562015-08-12 12:55:02 -07001797
1798void Camera3Device::internalUpdateStatusLocked(Status status) {
1799 mStatus = status;
1800 mRecentStatusUpdates.add(mStatus);
1801 mStatusChanged.broadcast();
1802}
1803
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001804void Camera3Device::pauseStateNotify(bool enable) {
1805 Mutex::Autolock il(mInterfaceLock);
1806 Mutex::Autolock l(mLock);
1807
1808 mPauseStateNotify = enable;
1809}
1810
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001811// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001812status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001813 mRequestThread->setPaused(true);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001814
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001815 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1816 maxExpectedDuration);
1817 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001818 if (res != OK) {
1819 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001820 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001821 }
1822
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001823 return res;
1824}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001825
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001826// Resume after internalPauseAndWaitLocked
1827status_t Camera3Device::internalResumeLocked() {
1828 status_t res;
1829
1830 mRequestThread->setPaused(false);
1831
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001832 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1833 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001834 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1835 if (res != OK) {
1836 SET_ERR_L("Can't transition to active in %f seconds!",
1837 kActiveTimeout/1e9);
1838 }
1839 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001840 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001841}
1842
Ruben Brunk183f0562015-08-12 12:55:02 -07001843status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001844 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001845
1846 size_t startIndex = 0;
1847 if (mStatusWaiters == 0) {
1848 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1849 // this status list
1850 mRecentStatusUpdates.clear();
1851 } else {
1852 // If other threads are waiting on updates to this status list, set the position of the
1853 // first element that this list will check rather than clearing the list.
1854 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001855 }
1856
Ruben Brunk183f0562015-08-12 12:55:02 -07001857 mStatusWaiters++;
1858
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001859 bool stateSeen = false;
1860 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001861 if (active == (mStatus == STATUS_ACTIVE)) {
1862 // Desired state is current
1863 break;
1864 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001865
1866 res = mStatusChanged.waitRelative(mLock, timeout);
1867 if (res != OK) break;
1868
Ruben Brunk183f0562015-08-12 12:55:02 -07001869 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1870 // transitions.
1871 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1872 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1873 __FUNCTION__);
1874
1875 // Encountered desired state since we began waiting
1876 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001877 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1878 stateSeen = true;
1879 break;
1880 }
1881 }
1882 } while (!stateSeen);
1883
Ruben Brunk183f0562015-08-12 12:55:02 -07001884 mStatusWaiters--;
1885
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 return res;
1887}
1888
1889
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001890status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001891 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001892 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001893
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001894 if (listener != NULL && mListener != NULL) {
1895 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1896 }
1897 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001898 mRequestThread->setNotificationListener(listener);
1899 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001900
1901 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001902}
1903
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001904bool Camera3Device::willNotify3A() {
1905 return false;
1906}
1907
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001908status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001909 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001910 status_t res;
1911 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001912
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001913 while (mResultQueue.empty()) {
1914 res = mResultSignal.waitRelative(mOutputLock, timeout);
1915 if (res == TIMED_OUT) {
1916 return res;
1917 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001918 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1919 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001920 return res;
1921 }
1922 }
1923 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001924}
1925
Jianing Weicb0652e2014-03-12 18:29:36 -07001926status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001927 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001928 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001929
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001930 if (mResultQueue.empty()) {
1931 return NOT_ENOUGH_DATA;
1932 }
1933
Jianing Weicb0652e2014-03-12 18:29:36 -07001934 if (frame == NULL) {
1935 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1936 return BAD_VALUE;
1937 }
1938
1939 CaptureResult &result = *(mResultQueue.begin());
1940 frame->mResultExtras = result.mResultExtras;
1941 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001942 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001943 mResultQueue.erase(mResultQueue.begin());
1944
1945 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001946}
1947
1948status_t Camera3Device::triggerAutofocus(uint32_t id) {
1949 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001950 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001951
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001952 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1953 // Mix-in this trigger into the next request and only the next request.
1954 RequestTrigger trigger[] = {
1955 {
1956 ANDROID_CONTROL_AF_TRIGGER,
1957 ANDROID_CONTROL_AF_TRIGGER_START
1958 },
1959 {
1960 ANDROID_CONTROL_AF_TRIGGER_ID,
1961 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001962 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001963 };
1964
1965 return mRequestThread->queueTrigger(trigger,
1966 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001967}
1968
1969status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1970 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001971 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001972
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001973 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1974 // Mix-in this trigger into the next request and only the next request.
1975 RequestTrigger trigger[] = {
1976 {
1977 ANDROID_CONTROL_AF_TRIGGER,
1978 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1979 },
1980 {
1981 ANDROID_CONTROL_AF_TRIGGER_ID,
1982 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001983 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001984 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001986 return mRequestThread->queueTrigger(trigger,
1987 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001988}
1989
1990status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1991 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001992 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001993
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001994 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1995 // Mix-in this trigger into the next request and only the next request.
1996 RequestTrigger trigger[] = {
1997 {
1998 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1999 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2000 },
2001 {
2002 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2003 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002004 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002005 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002006
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002007 return mRequestThread->queueTrigger(trigger,
2008 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002009}
2010
Jianing Weicb0652e2014-03-12 18:29:36 -07002011status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002012 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002013 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002014 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002015
Zhijun He7ef20392014-04-21 16:04:17 -07002016 {
2017 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002018
2019 // b/116514106 "disconnect()" can get called twice for the same device. The
2020 // camera device will not be initialized during the second run.
2021 if (mStatus == STATUS_UNINITIALIZED) {
2022 return OK;
2023 }
2024
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002025 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07002026 }
2027
Emilian Peev08dd2452017-04-06 16:55:14 +01002028 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002029}
2030
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002031status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002032 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2033}
2034
2035status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002036 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002037 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002038 Mutex::Autolock il(mInterfaceLock);
2039 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002040
2041 sp<Camera3StreamInterface> stream;
2042 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2043 if (outputStreamIdx == NAME_NOT_FOUND) {
2044 CLOGE("Stream %d does not exist", streamId);
2045 return BAD_VALUE;
2046 }
2047
2048 stream = mOutputStreams.editValueAt(outputStreamIdx);
2049
2050 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002051 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002052 return BAD_VALUE;
2053 }
2054
2055 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002056 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002057 return BAD_VALUE;
2058 }
2059
Ruben Brunkc78ac262015-08-13 17:58:46 -07002060 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002061}
2062
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002063status_t Camera3Device::tearDown(int streamId) {
2064 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002065 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002066 Mutex::Autolock il(mInterfaceLock);
2067 Mutex::Autolock l(mLock);
2068
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002069 sp<Camera3StreamInterface> stream;
2070 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2071 if (outputStreamIdx == NAME_NOT_FOUND) {
2072 CLOGE("Stream %d does not exist", streamId);
2073 return BAD_VALUE;
2074 }
2075
2076 stream = mOutputStreams.editValueAt(outputStreamIdx);
2077
2078 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2079 CLOGE("Stream %d is a target of a in-progress request", streamId);
2080 return BAD_VALUE;
2081 }
2082
2083 return stream->tearDown();
2084}
2085
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002086status_t Camera3Device::addBufferListenerForStream(int streamId,
2087 wp<Camera3StreamBufferListener> listener) {
2088 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002089 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002090 Mutex::Autolock il(mInterfaceLock);
2091 Mutex::Autolock l(mLock);
2092
2093 sp<Camera3StreamInterface> stream;
2094 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
2095 if (outputStreamIdx == NAME_NOT_FOUND) {
2096 CLOGE("Stream %d does not exist", streamId);
2097 return BAD_VALUE;
2098 }
2099
2100 stream = mOutputStreams.editValueAt(outputStreamIdx);
2101 stream->addBufferListener(listener);
2102
2103 return OK;
2104}
2105
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002106/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002107 * Methods called by subclasses
2108 */
2109
2110void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002111 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002112 {
2113 // Need mLock to safely update state and synchronize to current
2114 // state of methods in flight.
2115 Mutex::Autolock l(mLock);
2116 // We can get various system-idle notices from the status tracker
2117 // while starting up. Only care about them if we've actually sent
2118 // in some requests recently.
2119 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2120 return;
2121 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002122 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2123 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002124 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002125
2126 // Skip notifying listener if we're doing some user-transparent
2127 // state changes
2128 if (mPauseStateNotify) return;
2129 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002130
2131 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002132 {
2133 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002134 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002135 }
2136 if (idle && listener != NULL) {
2137 listener->notifyIdle();
2138 }
2139}
2140
Shuzhen Wang758c2152017-01-10 18:26:18 -08002141status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002142 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002143 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002144 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2145 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002146
2147 if (surfaceIds == nullptr) {
2148 return BAD_VALUE;
2149 }
2150
Zhijun He5d677d12016-05-29 16:52:39 -07002151 Mutex::Autolock il(mInterfaceLock);
2152 Mutex::Autolock l(mLock);
2153
Shuzhen Wang758c2152017-01-10 18:26:18 -08002154 if (consumers.size() == 0) {
2155 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002156 return BAD_VALUE;
2157 }
2158
2159 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2160 if (idx == NAME_NOT_FOUND) {
2161 CLOGE("Stream %d is unknown", streamId);
2162 return idx;
2163 }
2164 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002165 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002166 if (res != OK) {
2167 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2168 return res;
2169 }
2170
Emilian Peev40ead602017-09-26 15:46:36 +01002171 for (auto &consumer : consumers) {
2172 int id = stream->getSurfaceId(consumer);
2173 if (id < 0) {
2174 CLOGE("Invalid surface id!");
2175 return BAD_VALUE;
2176 }
2177 surfaceIds->push_back(id);
2178 }
2179
Shuzhen Wang0129d522016-10-30 22:43:41 -07002180 if (stream->isConsumerConfigurationDeferred()) {
2181 if (!stream->isConfiguring()) {
2182 CLOGE("Stream %d was already fully configured.", streamId);
2183 return INVALID_OPERATION;
2184 }
Zhijun He5d677d12016-05-29 16:52:39 -07002185
Shuzhen Wang0129d522016-10-30 22:43:41 -07002186 res = stream->finishConfiguration();
2187 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002188 // If finishConfiguration fails due to abandoned surface, do not set
2189 // device to error state.
2190 bool isSurfaceAbandoned =
2191 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2192 if (!isSurfaceAbandoned) {
2193 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2194 stream->getId(), strerror(-res), res);
2195 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002196 return res;
2197 }
Zhijun He5d677d12016-05-29 16:52:39 -07002198 }
2199
2200 return OK;
2201}
2202
Emilian Peev40ead602017-09-26 15:46:36 +01002203status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2204 const std::vector<OutputStreamInfo> &outputInfo,
2205 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2206 Mutex::Autolock il(mInterfaceLock);
2207 Mutex::Autolock l(mLock);
2208
2209 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2210 if (idx == NAME_NOT_FOUND) {
2211 CLOGE("Stream %d is unknown", streamId);
2212 return idx;
2213 }
2214
2215 for (const auto &it : removedSurfaceIds) {
2216 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2217 CLOGE("Shared surface still part of a pending request!");
2218 return -EBUSY;
2219 }
2220 }
2221
2222 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
2223 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2224 if (res != OK) {
2225 CLOGE("Stream %d failed to update stream (error %d %s) ",
2226 streamId, res, strerror(-res));
2227 if (res == UNKNOWN_ERROR) {
2228 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2229 __FUNCTION__);
2230 }
2231 return res;
2232 }
2233
2234 return res;
2235}
2236
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002237status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2238 Mutex::Autolock il(mInterfaceLock);
2239 Mutex::Autolock l(mLock);
2240
2241 int idx = mOutputStreams.indexOfKey(streamId);
2242 if (idx == NAME_NOT_FOUND) {
2243 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2244 return BAD_VALUE;
2245 }
2246
2247 sp<Camera3OutputStreamInterface> stream = mOutputStreams.editValueAt(idx);
2248 return stream->dropBuffers(dropping);
2249}
2250
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002251/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002252 * Camera3Device private methods
2253 */
2254
2255sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002256 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002257 ATRACE_CALL();
2258 status_t res;
2259
2260 sp<CaptureRequest> newRequest = new CaptureRequest;
Emilian Peevaebbe412018-01-15 13:53:24 +00002261 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002262
2263 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002264 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002265 if (inputStreams.count > 0) {
2266 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002267 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002268 CLOGE("Request references unknown input stream %d",
2269 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002270 return NULL;
2271 }
2272 // Lazy completion of stream configuration (allocation/registration)
2273 // on first use
2274 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002275 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002276 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002277 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002279 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002280 return NULL;
2281 }
2282 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002283 // Check if stream prepare is blocking requests.
2284 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002285 CLOGE("Request references an input stream that's being prepared!");
2286 return NULL;
2287 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002288
2289 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002290 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002291 }
2292
2293 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002294 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002296 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002297 return NULL;
2298 }
2299
2300 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002301 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002302 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002303 CLOGE("Request references unknown stream %d",
2304 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002305 return NULL;
2306 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002307 sp<Camera3OutputStreamInterface> stream =
2308 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002309
Zhijun He5d677d12016-05-29 16:52:39 -07002310 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002311 auto iter = surfaceMap.find(streams.data.i32[i]);
2312 if (iter != surfaceMap.end()) {
2313 const std::vector<size_t>& surfaces = iter->second;
2314 for (const auto& surface : surfaces) {
2315 if (stream->isConsumerConfigurationDeferred(surface)) {
2316 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2317 "due to deferred consumer", stream->getId(), surface);
2318 return NULL;
2319 }
2320 }
2321 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002322 }
2323
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002324 // Lazy completion of stream configuration (allocation/registration)
2325 // on first use
2326 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002327 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002328 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002329 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2330 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002331 return NULL;
2332 }
2333 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002334 // Check if stream prepare is blocking requests.
2335 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002336 CLOGE("Request references an output stream that's being prepared!");
2337 return NULL;
2338 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002339
2340 newRequest->mOutputStreams.push(stream);
2341 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002342 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002343 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002344
2345 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002346}
2347
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002348bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2349 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2350 Size size = mSupportedOpaqueInputSizes[i];
2351 if (size.width == width && size.height == height) {
2352 return true;
2353 }
2354 }
2355
2356 return false;
2357}
2358
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002359void Camera3Device::cancelStreamsConfigurationLocked() {
2360 int res = OK;
2361 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2362 res = mInputStream->cancelConfiguration();
2363 if (res != OK) {
2364 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2365 mInputStream->getId(), strerror(-res), res);
2366 }
2367 }
2368
2369 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2370 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2371 if (outputStream->isConfiguring()) {
2372 res = outputStream->cancelConfiguration();
2373 if (res != OK) {
2374 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2375 outputStream->getId(), strerror(-res), res);
2376 }
2377 }
2378 }
2379
2380 // Return state to that at start of call, so that future configures
2381 // properly clean things up
2382 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2383 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002384
2385 res = mPreparerThread->resume();
2386 if (res != OK) {
2387 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2388 }
2389}
2390
2391bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams) {
2392 ATRACE_CALL();
2393 bool ret = false;
2394
2395 Mutex::Autolock il(mInterfaceLock);
2396 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2397
2398 Mutex::Autolock l(mLock);
2399 auto rc = internalPauseAndWaitLocked(maxExpectedDuration);
2400 if (rc == NO_ERROR) {
2401 mNeedConfig = true;
2402 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2403 if (rc == NO_ERROR) {
2404 ret = true;
2405 mPauseStateNotify = false;
2406 //Moving to active state while holding 'mLock' is important.
2407 //There could be pending calls to 'create-/deleteStream' which
2408 //will trigger another stream configuration while the already
2409 //present streams end up with outstanding buffers that will
2410 //not get drained.
2411 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002412 } else if (rc == DEAD_OBJECT) {
2413 // DEAD_OBJECT can be returned if either the consumer surface is
2414 // abandoned, or the HAL has died.
2415 // - If the HAL has died, configureStreamsLocked call will set
2416 // device to error state,
2417 // - If surface is abandoned, we should not set device to error
2418 // state.
2419 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002420 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002421 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002422 }
2423 } else {
2424 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2425 }
2426
2427 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002428}
2429
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002430status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002431 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002432 ATRACE_CALL();
2433 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002434
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002435 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002436 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002437 return INVALID_OPERATION;
2438 }
2439
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002440 if (operatingMode < 0) {
2441 CLOGE("Invalid operating mode: %d", operatingMode);
2442 return BAD_VALUE;
2443 }
2444
2445 bool isConstrainedHighSpeed =
2446 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2447 operatingMode;
2448
2449 if (mOperatingMode != operatingMode) {
2450 mNeedConfig = true;
2451 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2452 mOperatingMode = operatingMode;
2453 }
2454
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002455 if (!mNeedConfig) {
2456 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2457 return OK;
2458 }
2459
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002460 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2461 // adding a dummy stream instead.
2462 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2463 if (mOutputStreams.size() == 0) {
2464 addDummyStreamLocked();
2465 } else {
2466 tryRemoveDummyStreamLocked();
2467 }
2468
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002469 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002470 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002471
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002472 mPreparerThread->pause();
2473
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002474 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002475 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002476 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2477
2478 Vector<camera3_stream_t*> streams;
2479 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002480 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482
2483 if (mInputStream != NULL) {
2484 camera3_stream_t *inputStream;
2485 inputStream = mInputStream->startConfiguration();
2486 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002487 CLOGE("Can't start input stream configuration");
2488 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002489 return INVALID_OPERATION;
2490 }
2491 streams.add(inputStream);
2492 }
2493
2494 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002495
2496 // Don't configure bidi streams twice, nor add them twice to the list
2497 if (mOutputStreams[i].get() ==
2498 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2499
2500 config.num_streams--;
2501 continue;
2502 }
2503
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002504 camera3_stream_t *outputStream;
2505 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2506 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002507 CLOGE("Can't start output stream configuration");
2508 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002509 return INVALID_OPERATION;
2510 }
2511 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002512
2513 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB &&
2514 outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
Emilian Peev192ee832018-01-31 14:46:47 +00002515 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2516 // always occupy the initial entry.
2517 bufferSizes[k] = static_cast<uint32_t>(
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002518 getJpegBufferSize(outputStream->width, outputStream->height));
2519 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002520 }
2521
2522 config.streams = streams.editArray();
2523
2524 // Do the HAL configuration; will potentially touch stream
2525 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002526
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002527 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002528 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002529 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002530
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002531 if (res == BAD_VALUE) {
2532 // HAL rejected this set of streams as unsupported, clean up config
2533 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002534 CLOGE("Set of requested inputs/outputs not supported by HAL");
2535 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002536 return BAD_VALUE;
2537 } else if (res != OK) {
2538 // Some other kind of error from configure_streams - this is not
2539 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002540 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2541 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002542 return res;
2543 }
2544
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002545 // Finish all stream configuration immediately.
2546 // TODO: Try to relax this later back to lazy completion, which should be
2547 // faster
2548
Igor Murashkin073f8572013-05-02 14:59:28 -07002549 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002550 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002551 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002552 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002553 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002554 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002555 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2556 return DEAD_OBJECT;
2557 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002558 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002559 }
2560 }
2561
2562 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002563 sp<Camera3OutputStreamInterface> outputStream =
2564 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002565 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002566 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002567 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002568 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002569 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002570 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002571 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2572 return DEAD_OBJECT;
2573 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002574 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002575 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002576 }
2577 }
2578
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002579 // Request thread needs to know to avoid using repeat-last-settings protocol
2580 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002581 if (notifyRequestThread) {
2582 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2583 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002584
Zhijun He90f7c372016-08-16 16:19:43 -07002585 char value[PROPERTY_VALUE_MAX];
2586 property_get("camera.fifo.disable", value, "0");
2587 int32_t disableFifo = atoi(value);
2588 if (disableFifo != 1) {
2589 // Boost priority of request thread to SCHED_FIFO.
2590 pid_t requestThreadTid = mRequestThread->getTid();
2591 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002592 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002593 if (res != OK) {
2594 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2595 strerror(-res), res);
2596 } else {
2597 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2598 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002599 }
2600
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002601 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002602 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2603 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2604 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2605 sessionParams.unlock(newSessionParams);
2606 mSessionParams.unlock(currentSessionParams);
2607 if (updateSessionParams) {
2608 mSessionParams = sessionParams;
2609 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002610
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002611 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612
Ruben Brunk183f0562015-08-12 12:55:02 -07002613 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2614 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002615
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002616 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002617
Zhijun He0a210512014-07-24 13:45:15 -07002618 // tear down the deleted streams after configure streams.
2619 mDeletedStreams.clear();
2620
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002621 auto rc = mPreparerThread->resume();
2622 if (rc != OK) {
2623 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2624 return rc;
2625 }
2626
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002627 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002628}
2629
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002630status_t Camera3Device::addDummyStreamLocked() {
2631 ATRACE_CALL();
2632 status_t res;
2633
2634 if (mDummyStreamId != NO_STREAM) {
2635 // Should never be adding a second dummy stream when one is already
2636 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002637 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2638 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002639 return INVALID_OPERATION;
2640 }
2641
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002642 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002643
2644 sp<Camera3OutputStreamInterface> dummyStream =
2645 new Camera3DummyStream(mNextStreamId);
2646
2647 res = mOutputStreams.add(mNextStreamId, dummyStream);
2648 if (res < 0) {
2649 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2650 return res;
2651 }
2652
2653 mDummyStreamId = mNextStreamId;
2654 mNextStreamId++;
2655
2656 return OK;
2657}
2658
2659status_t Camera3Device::tryRemoveDummyStreamLocked() {
2660 ATRACE_CALL();
2661 status_t res;
2662
2663 if (mDummyStreamId == NO_STREAM) return OK;
2664 if (mOutputStreams.size() == 1) return OK;
2665
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002666 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002667
2668 // Ok, have a dummy stream and there's at least one other output stream,
2669 // so remove the dummy
2670
2671 sp<Camera3StreamInterface> deletedStream;
2672 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2673 if (outputStreamIdx == NAME_NOT_FOUND) {
2674 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2675 return INVALID_OPERATION;
2676 }
2677
2678 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2679 mOutputStreams.removeItemsAt(outputStreamIdx);
2680
2681 // Free up the stream endpoint so that it can be used by some other stream
2682 res = deletedStream->disconnect();
2683 if (res != OK) {
2684 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2685 // fall through since we want to still list the stream as deleted.
2686 }
2687 mDeletedStreams.add(deletedStream);
2688 mDummyStreamId = NO_STREAM;
2689
2690 return res;
2691}
2692
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002693void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002694 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002695 Mutex::Autolock l(mLock);
2696 va_list args;
2697 va_start(args, fmt);
2698
2699 setErrorStateLockedV(fmt, args);
2700
2701 va_end(args);
2702}
2703
2704void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002705 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002706 Mutex::Autolock l(mLock);
2707 setErrorStateLockedV(fmt, args);
2708}
2709
2710void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2711 va_list args;
2712 va_start(args, fmt);
2713
2714 setErrorStateLockedV(fmt, args);
2715
2716 va_end(args);
2717}
2718
2719void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002720 // Print out all error messages to log
2721 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002722 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002723
2724 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002725 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002726
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002727 mErrorCause = errorCause;
2728
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002729 if (mRequestThread != nullptr) {
2730 mRequestThread->setPaused(true);
2731 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002732 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002733
2734 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002735 sp<NotificationListener> listener = mListener.promote();
2736 if (listener != NULL) {
2737 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002738 CaptureResultExtras());
2739 }
2740
2741 // Save stack trace. View by dumping it later.
2742 CameraTraces::saveTrace();
2743 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002744}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002745
2746/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002747 * In-flight request management
2748 */
2749
Jianing Weicb0652e2014-03-12 18:29:36 -07002750status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002751 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002752 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07002753 std::set<String8>& physicalCameraIds, bool isStillCapture,
2754 bool isZslCapture) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002755 ATRACE_CALL();
2756 Mutex::Autolock l(mInFlightLock);
2757
2758 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002759 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07002760 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002761 if (res < 0) return res;
2762
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002763 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002764 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2765 // avoid a deadlock during reprocess requests.
2766 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002767 if (mStatusTracker != nullptr) {
2768 mStatusTracker->markComponentActive(mInFlightStatusId);
2769 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002770 }
2771
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002772 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002773 return OK;
2774}
2775
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002776void Camera3Device::returnOutputBuffers(
2777 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07002778 nsecs_t timestamp, bool timestampIncreasing) {
2779
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002780 for (size_t i = 0; i < numBuffers; i++)
2781 {
2782 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07002783 status_t res = stream->returnBuffer(outputBuffers[i], timestamp, timestampIncreasing);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002784 // Note: stream may be deallocated at this point, if this buffer was
2785 // the last reference to it.
2786 if (res != OK) {
2787 ALOGE("Can't return buffer to its stream: %s (%d)",
2788 strerror(-res), res);
2789 }
2790 }
2791}
2792
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002793void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002794 ATRACE_CALL();
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002795 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002796 mInFlightMap.removeItemsAt(idx, 1);
2797
2798 // Indicate idle inFlightMap to the status tracker
2799 if (mInFlightMap.size() == 0) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002800 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2801 // avoid a deadlock during reprocess requests.
2802 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002803 if (mStatusTracker != nullptr) {
2804 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2805 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002806 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002807 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002808}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002809
2810void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2811
2812 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2813 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2814
2815 nsecs_t sensorTimestamp = request.sensorTimestamp;
2816 nsecs_t shutterTimestamp = request.shutterTimestamp;
2817
2818 // Check if it's okay to remove the request from InFlightMap:
2819 // In the case of a successful request:
2820 // all input and output buffers, all result metadata, shutter callback
2821 // arrived.
2822 // In the case of a unsuccessful request:
2823 // all input and output buffers arrived.
2824 if (request.numBuffersLeft == 0 &&
Shuzhen Wang20f57342017-08-24 15:39:05 -07002825 (request.skipResultMetadata ||
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002826 (request.haveResultMetadata && shutterTimestamp != 0))) {
Emilian Peev9dd21f42018-08-03 13:39:29 +01002827 if (request.stillCapture) {
2828 ATRACE_ASYNC_END("still capture", frameNumber);
2829 }
2830
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002831 ATRACE_ASYNC_END("frame capture", frameNumber);
2832
Shuzhen Wang403044a2017-02-26 23:29:04 -08002833 // Sanity check - if sensor timestamp matches shutter timestamp in the
2834 // case of request having callback.
2835 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002836 sensorTimestamp != shutterTimestamp) {
2837 SET_ERR("sensor timestamp (%" PRId64
2838 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2839 sensorTimestamp, frameNumber, shutterTimestamp);
2840 }
2841
2842 // for an unsuccessful request, it may have pending output buffers to
2843 // return.
2844 assert(request.requestStatus != OK ||
2845 request.pendingOutputBuffers.size() == 0);
2846 returnOutputBuffers(request.pendingOutputBuffers.array(),
2847 request.pendingOutputBuffers.size(), 0);
2848
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002849 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002850 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2851 }
2852
2853 // Sanity check - if we have too many in-flight frames, something has
2854 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002855 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002856 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002857 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2858 kInFlightWarnLimitHighSpeed) {
2859 CLOGE("In-flight list too large for high speed configuration: %zu",
2860 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002861 }
2862}
2863
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002864void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002865 ATRACE_CALL();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002866 { // First return buffers cached in mInFlightMap
2867 Mutex::Autolock l(mInFlightLock);
2868 for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
2869 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2870 returnOutputBuffers(request.pendingOutputBuffers.array(),
2871 request.pendingOutputBuffers.size(), 0);
2872 }
2873 mInFlightMap.clear();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07002874 mExpectedInflightDuration = 0;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002875 }
2876
2877 // Then return all inflight buffers not returned by HAL
2878 std::vector<std::pair<int32_t, int32_t>> inflightKeys;
2879 mInterface->getInflightBufferKeys(&inflightKeys);
2880
2881 int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
2882 for (auto& pair : inflightKeys) {
2883 int32_t frameNumber = pair.first;
2884 int32_t streamId = pair.second;
2885 buffer_handle_t* buffer;
2886 status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
2887 if (res != OK) {
2888 ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
2889 __FUNCTION__, frameNumber, streamId);
2890 continue;
2891 }
2892
2893 camera3_stream_buffer_t streamBuffer;
2894 streamBuffer.buffer = buffer;
2895 streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
2896 streamBuffer.acquire_fence = -1;
2897 streamBuffer.release_fence = -1;
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002898
2899 // First check if the buffer belongs to deleted stream
2900 bool streamDeleted = false;
2901 for (auto& stream : mDeletedStreams) {
2902 if (streamId == stream->getId()) {
2903 streamDeleted = true;
2904 // Return buffer to deleted stream
2905 camera3_stream* halStream = stream->asHalStream();
2906 streamBuffer.stream = halStream;
2907 switch (halStream->stream_type) {
2908 case CAMERA3_STREAM_OUTPUT:
2909 res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
2910 if (res != OK) {
2911 ALOGE("%s: Can't return output buffer for frame %d to"
2912 " stream %d: %s (%d)", __FUNCTION__,
2913 frameNumber, streamId, strerror(-res), res);
2914 }
2915 break;
2916 case CAMERA3_STREAM_INPUT:
2917 res = stream->returnInputBuffer(streamBuffer);
2918 if (res != OK) {
2919 ALOGE("%s: Can't return input buffer for frame %d to"
2920 " stream %d: %s (%d)", __FUNCTION__,
2921 frameNumber, streamId, strerror(-res), res);
2922 }
2923 break;
2924 default: // Bi-direcitonal stream is deprecated
2925 ALOGE("%s: stream %d has unknown stream type %d",
2926 __FUNCTION__, streamId, halStream->stream_type);
2927 break;
2928 }
2929 break;
2930 }
2931 }
2932 if (streamDeleted) {
2933 continue;
2934 }
2935
2936 // Then check against configured streams
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002937 if (streamId == inputStreamId) {
2938 streamBuffer.stream = mInputStream->asHalStream();
2939 res = mInputStream->returnInputBuffer(streamBuffer);
2940 if (res != OK) {
2941 ALOGE("%s: Can't return input buffer for frame %d to"
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002942 " stream %d: %s (%d)", __FUNCTION__,
2943 frameNumber, streamId, strerror(-res), res);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002944 }
2945 } else {
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07002946 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2947 if (idx == NAME_NOT_FOUND) {
2948 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
2949 continue;
2950 }
2951 streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002952 returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
2953 }
2954 }
2955}
2956
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002957void Camera3Device::insertResultLocked(CaptureResult *result,
2958 uint32_t frameNumber) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002959 if (result == nullptr) return;
2960
Emilian Peev71c73a22017-03-21 16:35:51 +00002961 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2962 result->mMetadata.getAndLock());
2963 set_camera_metadata_vendor_id(meta, mVendorTagId);
2964 result->mMetadata.unlock(meta);
2965
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002966 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2967 (int32_t*)&frameNumber, 1) != OK) {
2968 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2969 return;
2970 }
2971
2972 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2973 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2974 return;
2975 }
2976
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002977 // Valid result, insert into queue
2978 List<CaptureResult>::iterator queuedResult =
2979 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2980 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2981 ", burstId = %" PRId32, __FUNCTION__,
2982 queuedResult->mResultExtras.requestId,
2983 queuedResult->mResultExtras.frameNumber,
2984 queuedResult->mResultExtras.burstId);
2985
2986 mResultSignal.signal();
2987}
2988
2989
2990void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002991 const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002992 ATRACE_CALL();
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002993 Mutex::Autolock l(mOutputLock);
2994
2995 CaptureResult captureResult;
2996 captureResult.mResultExtras = resultExtras;
2997 captureResult.mMetadata = partialResult;
2998
Emilian Peev7e25e5e2017-04-07 15:48:49 +01002999 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003000}
3001
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003002
3003void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
3004 CaptureResultExtras &resultExtras,
3005 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003006 uint32_t frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003007 bool reprocess,
3008 const std::vector<PhysicalCaptureResultInfo>& physicalMetadatas) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003009 ATRACE_CALL();
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003010 if (pendingMetadata.isEmpty())
3011 return;
3012
3013 Mutex::Autolock l(mOutputLock);
3014
3015 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003016 if (reprocess) {
3017 if (frameNumber < mNextReprocessResultFrameNumber) {
3018 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003019 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07003020 frameNumber, mNextReprocessResultFrameNumber);
3021 return;
3022 }
3023 mNextReprocessResultFrameNumber = frameNumber + 1;
3024 } else {
3025 if (frameNumber < mNextResultFrameNumber) {
3026 SET_ERR("Out-of-order capture result metadata submitted! "
3027 "(got frame number %d, expecting %d)",
3028 frameNumber, mNextResultFrameNumber);
3029 return;
3030 }
3031 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003032 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003033
3034 CaptureResult captureResult;
3035 captureResult.mResultExtras = resultExtras;
3036 captureResult.mMetadata = pendingMetadata;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003037 captureResult.mPhysicalMetadatas = physicalMetadatas;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003038
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003039 // Append any previous partials to form a complete result
3040 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
3041 captureResult.mMetadata.append(collectedPartialResult);
3042 }
3043
3044 captureResult.mMetadata.sort();
3045
3046 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003047 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3048 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003049 SET_ERR("No timestamp provided by HAL for frame %d!",
3050 frameNumber);
3051 return;
3052 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003053 for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
3054 camera_metadata_entry timestamp =
3055 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
3056 if (timestamp.count == 0) {
3057 SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
3058 String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
3059 return;
3060 }
3061 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003062
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003063 // Fix up some result metadata to account for HAL-level distortion correction
3064 status_t res = mDistortionMapper.correctCaptureResult(&captureResult.mMetadata);
3065 if (res != OK) {
3066 SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
3067 frameNumber, strerror(res), res);
3068 return;
3069 }
3070
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003071 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
3072 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
3073
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003074 insertResultLocked(&captureResult, frameNumber);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003075}
3076
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003077/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003078 * Camera HAL device callback methods
3079 */
3080
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003081void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003082 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003083
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003084 status_t res;
3085
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003086 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07003087 if (result->result == NULL && result->num_output_buffers == 0 &&
3088 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003089 SET_ERR("No result data provided by HAL for frame %d",
3090 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003091 return;
3092 }
Zhijun He204e3292014-07-14 17:09:23 -07003093
Zhijun He204e3292014-07-14 17:09:23 -07003094 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07003095 result->result != NULL &&
3096 result->partial_result != 1) {
3097 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3098 " if partial result is not supported",
3099 frameNumber, result->partial_result);
3100 return;
3101 }
3102
3103 bool isPartialResult = false;
3104 CameraMetadata collectedPartialResult;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003105 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003106
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003107 // Get shutter timestamp and resultExtras from list of in-flight requests,
3108 // where it was added by the shutter notification for this frame. If the
3109 // shutter timestamp isn't received yet, append the output buffers to the
3110 // in-flight request and they will be returned when the shutter timestamp
3111 // arrives. Update the in-flight status and remove the in-flight entry if
3112 // all result data and shutter timestamp have been received.
3113 nsecs_t shutterTimestamp = 0;
3114
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003115 {
3116 Mutex::Autolock l(mInFlightLock);
3117 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3118 if (idx == NAME_NOT_FOUND) {
3119 SET_ERR("Unknown frame number for capture result: %d",
3120 frameNumber);
3121 return;
3122 }
3123 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003124 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3125 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08003126 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003127 __FUNCTION__, request.resultExtras.requestId,
3128 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08003129 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003130 // Always update the partial count to the latest one if it's not 0
3131 // (buffers only). When framework aggregates adjacent partial results
3132 // into one, the latest partial count will be used.
3133 if (result->partial_result != 0)
3134 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003135
3136 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07003137 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01003138 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3139 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
3140 " the range of [1, %d] when metadata is included in the result",
3141 frameNumber, result->partial_result, mNumPartialResults);
3142 return;
3143 }
3144 isPartialResult = (result->partial_result < mNumPartialResults);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003145 if (isPartialResult && result->num_physcam_metadata) {
3146 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3147 " physical camera result", frameNumber);
3148 return;
3149 }
Emilian Peev08dd2452017-04-06 16:55:14 +01003150 if (isPartialResult) {
3151 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07003152 }
3153
Shuzhen Wang4a472662017-02-26 23:29:04 -08003154 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003155 // Send partial capture result
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003156 sendPartialCaptureResult(result->result, request.resultExtras,
3157 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003158 }
3159 }
3160
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003161 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003162 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07003163
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003164 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07003165 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003166 if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
3167 SET_ERR("Requested physical Camera Ids %d not equal to number of metadata %d",
3168 request.physicalCameraIds.size(), result->num_physcam_metadata);
3169 return;
3170 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003171 if (request.haveResultMetadata) {
3172 SET_ERR("Called multiple times with metadata for frame %d",
3173 frameNumber);
3174 return;
3175 }
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003176 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3177 String8 physicalId(result->physcam_ids[i]);
3178 std::set<String8>::iterator cameraIdIter =
3179 request.physicalCameraIds.find(physicalId);
3180 if (cameraIdIter != request.physicalCameraIds.end()) {
3181 request.physicalCameraIds.erase(cameraIdIter);
3182 } else {
3183 SET_ERR("Total result for frame %d has already returned for camera %s",
3184 frameNumber, physicalId.c_str());
3185 return;
3186 }
3187 }
Zhijun He204e3292014-07-14 17:09:23 -07003188 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003189 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07003190 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003191 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003192 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003193 request.haveResultMetadata = true;
3194 }
3195
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003196 uint32_t numBuffersReturned = result->num_output_buffers;
3197 if (result->input_buffer != NULL) {
3198 if (hasInputBufferInRequest) {
3199 numBuffersReturned += 1;
3200 } else {
3201 ALOGW("%s: Input buffer should be NULL if there is no input"
3202 " buffer sent in the request",
3203 __FUNCTION__);
3204 }
3205 }
3206 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003207 if (request.numBuffersLeft < 0) {
3208 SET_ERR("Too many buffers returned for frame %d",
3209 frameNumber);
3210 return;
3211 }
3212
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003213 camera_metadata_ro_entry_t entry;
3214 res = find_camera_metadata_ro_entry(result->result,
3215 ANDROID_SENSOR_TIMESTAMP, &entry);
3216 if (res == OK && entry.count == 1) {
3217 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003218 }
3219
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003220 // If shutter event isn't received yet, append the output buffers to
3221 // the in-flight request. Otherwise, return the output buffers to
3222 // streams.
3223 if (shutterTimestamp == 0) {
3224 request.pendingOutputBuffers.appendArray(result->output_buffers,
3225 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07003226 } else {
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003227 bool timestampIncreasing = !(request.zslCapture || request.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003228 returnOutputBuffers(result->output_buffers,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003229 result->num_output_buffers, shutterTimestamp, timestampIncreasing);
Igor Murashkind2c90692013-04-02 12:32:32 -07003230 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003231
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003232 if (result->result != NULL && !isPartialResult) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003233 for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3234 CameraMetadata physicalMetadata;
3235 physicalMetadata.append(result->physcam_metadata[i]);
3236 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3237 physicalMetadata});
3238 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003239 if (shutterTimestamp == 0) {
3240 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08003241 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003242 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003243 CameraMetadata metadata;
3244 metadata = result->result;
3245 sendCaptureResult(metadata, request.resultExtras,
Emilian Peev7e25e5e2017-04-07 15:48:49 +01003246 collectedPartialResult, frameNumber,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003247 hasInputBufferInRequest, request.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003248 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07003249 }
3250
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003251 removeInFlightRequestIfReadyLocked(idx);
3252 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003253
Zhijun Hef0d962a2014-06-30 10:24:11 -07003254 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07003255 if (hasInputBufferInRequest) {
3256 Camera3Stream *stream =
3257 Camera3Stream::cast(result->input_buffer->stream);
3258 res = stream->returnInputBuffer(*(result->input_buffer));
3259 // Note: stream may be deallocated at this point, if this buffer was the
3260 // last reference to it.
3261 if (res != OK) {
3262 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3263 " its stream:%s (%d)", __FUNCTION__,
3264 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07003265 }
3266 } else {
3267 ALOGW("%s: Input buffer should be NULL if there is no input"
3268 " buffer sent in the request, skipping input buffer return.",
3269 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07003270 }
3271 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003272}
3273
3274void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003275 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003276 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003277 {
3278 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003279 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003280 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003281
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003282 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003283 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003284 return;
3285 }
3286
3287 switch (msg->type) {
3288 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003289 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003290 break;
3291 }
3292 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003293 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003294 break;
3295 }
3296 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003297 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003298 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003299 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003300}
3301
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003302void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003303 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003304 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003305 // Map camera HAL error codes to ICameraDeviceCallback error codes
3306 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003307 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003308 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003309 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003310 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003311 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003312 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003313 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003314 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003315 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003316 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003317 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003318 };
3319
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003320 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003321 ((msg.error_code >= 0) &&
3322 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3323 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003324 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003325
3326 int streamId = 0;
3327 if (msg.error_stream != NULL) {
3328 Camera3Stream *stream =
3329 Camera3Stream::cast(msg.error_stream);
3330 streamId = stream->getId();
3331 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003332 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3333 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003334 streamId, msg.error_code);
3335
3336 CaptureResultExtras resultExtras;
3337 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003338 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003339 // SET_ERR calls notifyError
3340 SET_ERR("Camera HAL reported serious device error");
3341 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003342 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3343 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3344 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003345 {
3346 Mutex::Autolock l(mInFlightLock);
3347 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3348 if (idx >= 0) {
3349 InFlightRequest &r = mInFlightMap.editValueAt(idx);
3350 r.requestStatus = msg.error_code;
3351 resultExtras = r.resultExtras;
Shuzhen Wang20f57342017-08-24 15:39:05 -07003352 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3353 || hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3354 errorCode) {
3355 r.skipResultMetadata = true;
3356 }
Emilian Peevba0fac32017-03-30 09:05:34 +01003357 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3358 errorCode) {
3359 // In case of missing result check whether the buffers
3360 // returned. If they returned, then remove inflight
3361 // request.
3362 removeInFlightRequestIfReadyLocked(idx);
3363 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003364 } else {
3365 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003366 ALOGE("Camera %s: %s: cannot find in-flight request on "
3367 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003368 resultExtras.frameNumber);
3369 }
3370 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08003371 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003372 if (listener != NULL) {
3373 listener->notifyError(errorCode, resultExtras);
3374 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003375 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003376 }
3377 break;
3378 default:
3379 // SET_ERR calls notifyError
3380 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3381 break;
3382 }
3383}
3384
3385void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003386 sp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003387 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003388 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003389
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003390 // Set timestamp for the request in the in-flight tracking
3391 // and get the request ID to send upstream
3392 {
3393 Mutex::Autolock l(mInFlightLock);
3394 idx = mInFlightMap.indexOfKey(msg.frame_number);
3395 if (idx >= 0) {
3396 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003397
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07003398 // Verify ordering of shutter notifications
3399 {
3400 Mutex::Autolock l(mOutputLock);
3401 // TODO: need to track errors for tighter bounds on expected frame number.
3402 if (r.hasInputBuffer) {
3403 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3404 SET_ERR("Shutter notification out-of-order. Expected "
3405 "notification for frame %d, got frame %d",
3406 mNextReprocessShutterFrameNumber, msg.frame_number);
3407 return;
3408 }
3409 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3410 } else {
3411 if (msg.frame_number < mNextShutterFrameNumber) {
3412 SET_ERR("Shutter notification out-of-order. Expected "
3413 "notification for frame %d, got frame %d",
3414 mNextShutterFrameNumber, msg.frame_number);
3415 return;
3416 }
3417 mNextShutterFrameNumber = msg.frame_number + 1;
3418 }
3419 }
3420
Shuzhen Wang4a472662017-02-26 23:29:04 -08003421 r.shutterTimestamp = msg.timestamp;
3422 if (r.hasCallback) {
3423 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003424 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003425 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08003426 // Call listener, if any
3427 if (listener != NULL) {
3428 listener->notifyShutter(r.resultExtras, msg.timestamp);
3429 }
3430 // send pending result and buffers
3431 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3432 r.collectedPartialResult, msg.frame_number,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003433 r.hasInputBuffer, r.physicalMetadatas);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003434 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003435 bool timestampIncreasing = !(r.zslCapture || r.hasInputBuffer);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003436 returnOutputBuffers(r.pendingOutputBuffers.array(),
Shuzhen Wang26abaf42018-08-28 15:41:20 -07003437 r.pendingOutputBuffers.size(), r.shutterTimestamp, timestampIncreasing);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003438 r.pendingOutputBuffers.clear();
3439
3440 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003441 }
3442 }
3443 if (idx < 0) {
3444 SET_ERR("Shutter notification for non-existent frame number %d",
3445 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003446 }
3447}
3448
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003449CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003450 ALOGV("%s", __FUNCTION__);
3451
Igor Murashkin1e479c02013-09-06 16:55:14 -07003452 CameraMetadata retVal;
3453
3454 if (mRequestThread != NULL) {
3455 retVal = mRequestThread->getLatestRequest();
3456 }
3457
Igor Murashkin1e479c02013-09-06 16:55:14 -07003458 return retVal;
3459}
3460
Jianing Weicb0652e2014-03-12 18:29:36 -07003461
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003462void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3463 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3464 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3465}
3466
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003467/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003468 * HalInterface inner class methods
3469 */
3470
Yifan Hongf79b5542017-04-11 14:44:25 -07003471Camera3Device::HalInterface::HalInterface(
3472 sp<ICameraDeviceSession> &session,
3473 std::shared_ptr<RequestMetadataQueue> queue) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003474 mHidlSession(session),
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003475 mRequestMetadataQueue(queue) {
3476 // Check with hardware service manager if we can downcast these interfaces
3477 // Somewhat expensive, so cache the results at startup
3478 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3479 if (castResult_3_4.isOk()) {
3480 mHidlSession_3_4 = castResult_3_4;
3481 }
3482 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3483 if (castResult_3_3.isOk()) {
3484 mHidlSession_3_3 = castResult_3_3;
3485 }
3486}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003487
Emilian Peev31abd0a2017-05-11 18:37:46 +01003488Camera3Device::HalInterface::HalInterface() {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003489
3490Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003491 mHidlSession(other.mHidlSession),
3492 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003493
3494bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003495 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003496}
3497
3498void Camera3Device::HalInterface::clear() {
Emilian Peev9e740b02018-01-30 18:28:03 +00003499 mHidlSession_3_4.clear();
3500 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003501 mHidlSession.clear();
3502}
3503
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003504bool Camera3Device::HalInterface::supportBatchRequest() {
3505 return mHidlSession != nullptr;
3506}
3507
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003508status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3509 camera3_request_template_t templateId,
3510 /*out*/ camera_metadata_t **requestTemplate) {
3511 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3512 if (!valid()) return INVALID_OPERATION;
3513 status_t res = OK;
3514
Emilian Peev31abd0a2017-05-11 18:37:46 +01003515 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003516
3517 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003518 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003519 status = s;
3520 if (status == common::V1_0::Status::OK) {
3521 const camera_metadata *r =
3522 reinterpret_cast<const camera_metadata_t*>(request.data());
3523 size_t expectedSize = request.size();
3524 int ret = validate_camera_metadata_structure(r, &expectedSize);
3525 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3526 *requestTemplate = clone_camera_metadata(r);
3527 if (*requestTemplate == nullptr) {
3528 ALOGE("%s: Unable to clone camera metadata received from HAL",
3529 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003530 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003531 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003532 } else {
3533 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3534 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003535 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003536 }
3537 };
3538 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003539 RequestTemplate id;
3540 switch (templateId) {
3541 case CAMERA3_TEMPLATE_PREVIEW:
3542 id = RequestTemplate::PREVIEW;
3543 break;
3544 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3545 id = RequestTemplate::STILL_CAPTURE;
3546 break;
3547 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3548 id = RequestTemplate::VIDEO_RECORD;
3549 break;
3550 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3551 id = RequestTemplate::VIDEO_SNAPSHOT;
3552 break;
3553 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3554 id = RequestTemplate::ZERO_SHUTTER_LAG;
3555 break;
3556 case CAMERA3_TEMPLATE_MANUAL:
3557 id = RequestTemplate::MANUAL;
3558 break;
3559 default:
3560 // Unknown template ID, or this HAL is too old to support it
3561 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003562 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003563 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003564
Emilian Peev31abd0a2017-05-11 18:37:46 +01003565 if (!err.isOk()) {
3566 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3567 res = DEAD_OBJECT;
3568 } else {
3569 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003570 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003571
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003572 return res;
3573}
3574
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003575status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peev192ee832018-01-31 14:46:47 +00003576 camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003577 ATRACE_NAME("CameraHal::configureStreams");
3578 if (!valid()) return INVALID_OPERATION;
3579 status_t res = OK;
3580
Emilian Peev31abd0a2017-05-11 18:37:46 +01003581 // Convert stream config to HIDL
3582 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003583 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3584 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3585 requestedConfiguration3_2.streams.resize(config->num_streams);
3586 requestedConfiguration3_4.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003587 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003588 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3589 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003590 camera3_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003591
Emilian Peev31abd0a2017-05-11 18:37:46 +01003592 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3593 cam3stream->setBufferFreedListener(this);
3594 int streamId = cam3stream->getId();
3595 StreamType streamType;
3596 switch (src->stream_type) {
3597 case CAMERA3_STREAM_OUTPUT:
3598 streamType = StreamType::OUTPUT;
3599 break;
3600 case CAMERA3_STREAM_INPUT:
3601 streamType = StreamType::INPUT;
3602 break;
3603 default:
3604 ALOGE("%s: Stream %d: Unsupported stream type %d",
3605 __FUNCTION__, streamId, config->streams[i]->stream_type);
3606 return BAD_VALUE;
3607 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003608 dst3_2.id = streamId;
3609 dst3_2.streamType = streamType;
3610 dst3_2.width = src->width;
3611 dst3_2.height = src->height;
3612 dst3_2.format = mapToPixelFormat(src->format);
3613 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3614 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3615 dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3616 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003617 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003618 if (src->physical_camera_id != nullptr) {
3619 dst3_4.physicalCameraId = src->physical_camera_id;
3620 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003621
3622 activeStreams.insert(streamId);
3623 // Create Buffer ID map if necessary
3624 if (mBufferIdMaps.count(streamId) == 0) {
3625 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3626 }
3627 }
3628 // remove BufferIdMap for deleted streams
3629 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3630 int streamId = it->first;
3631 bool active = activeStreams.count(streamId) > 0;
3632 if (!active) {
3633 it = mBufferIdMaps.erase(it);
3634 } else {
3635 ++it;
3636 }
3637 }
3638
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003639 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003640 res = mapToStreamConfigurationMode(
3641 (camera3_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003642 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003643 if (res != OK) {
3644 return res;
3645 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003646 requestedConfiguration3_2.operationMode = operationMode;
3647 requestedConfiguration3_4.operationMode = operationMode;
3648 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003649 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3650 get_camera_metadata_size(sessionParams));
3651
Emilian Peev31abd0a2017-05-11 18:37:46 +01003652 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003653 device::V3_3::HalStreamConfiguration finalConfiguration;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003654 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003655
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003656 // See if we have v3.4 or v3.3 HAL
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003657 if (mHidlSession_3_4 != nullptr) {
3658 // We do; use v3.4 for the call
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003659 ALOGV("%s: v3.4 device found", __FUNCTION__);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003660 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003661 auto err = mHidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003662 [&status, &finalConfiguration3_4]
3663 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3664 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003665 status = s;
3666 });
3667 if (!err.isOk()) {
3668 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3669 return DEAD_OBJECT;
3670 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003671 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3672 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3673 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3674 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003675 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003676 // We do; use v3.3 for the call
3677 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003678 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003679 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003680 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003681 finalConfiguration = halConfiguration;
3682 status = s;
3683 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003684 if (!err.isOk()) {
3685 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3686 return DEAD_OBJECT;
3687 }
3688 } else {
3689 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3690 ALOGV("%s: v3.2 device found", __FUNCTION__);
3691 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003692 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003693 [&status, &finalConfiguration_3_2]
3694 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3695 finalConfiguration_3_2 = halConfiguration;
3696 status = s;
3697 });
3698 if (!err.isOk()) {
3699 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3700 return DEAD_OBJECT;
3701 }
3702 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3703 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3704 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3705 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003706 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003707 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003708 }
3709
3710 if (status != common::V1_0::Status::OK ) {
3711 return CameraProviderManager::mapToStatusT(status);
3712 }
3713
3714 // And convert output stream configuration from HIDL
3715
3716 for (size_t i = 0; i < config->num_streams; i++) {
3717 camera3_stream_t *dst = config->streams[i];
3718 int streamId = Camera3Stream::cast(dst)->getId();
3719
3720 // Start scan at i, with the assumption that the stream order matches
3721 size_t realIdx = i;
3722 bool found = false;
3723 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003724 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003725 found = true;
3726 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003727 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003728 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3729 }
3730 if (!found) {
3731 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3732 __FUNCTION__, streamId);
3733 return INVALID_OPERATION;
3734 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003735 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003736
Emilian Peev710c1422017-08-30 11:19:38 +01003737 Camera3Stream* dstStream = Camera3Stream::cast(dst);
3738 dstStream->setFormatOverride(false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003739 dstStream->setDataSpaceOverride(false);
3740 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3741 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3742
Emilian Peev31abd0a2017-05-11 18:37:46 +01003743 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3744 if (dst->format != overrideFormat) {
3745 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3746 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003747 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003748 if (dst->data_space != overrideDataSpace) {
3749 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3750 streamId, dst->format);
3751 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003752 } else {
Emilian Peev710c1422017-08-30 11:19:38 +01003753 dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003754 dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3755
Emilian Peev31abd0a2017-05-11 18:37:46 +01003756 // Override allowed with IMPLEMENTATION_DEFINED
3757 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003758 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003759 }
3760
Emilian Peev31abd0a2017-05-11 18:37:46 +01003761 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003762 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003763 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003764 __FUNCTION__, streamId);
3765 return INVALID_OPERATION;
3766 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003767 dstStream->setUsage(
3768 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003769 } else {
3770 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003771 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003772 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3773 __FUNCTION__, streamId);
3774 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003775 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003776 dstStream->setUsage(
3777 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003778 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003779 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003780 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003781
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003782 return res;
3783}
3784
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003785void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3786 /*out*/device::V3_2::CaptureRequest* captureRequest,
3787 /*out*/std::vector<native_handle_t*>* handlesCreated) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003788 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003789 if (captureRequest == nullptr || handlesCreated == nullptr) {
3790 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3791 __FUNCTION__, captureRequest, handlesCreated);
3792 return;
3793 }
3794
3795 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003796
3797 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003798
3799 {
3800 std::lock_guard<std::mutex> lock(mInflightLock);
3801 if (request->input_buffer != nullptr) {
3802 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3803 buffer_handle_t buf = *(request->input_buffer->buffer);
3804 auto pair = getBufferId(buf, streamId);
3805 bool isNewBuffer = pair.first;
3806 uint64_t bufferId = pair.second;
3807 captureRequest->inputBuffer.streamId = streamId;
3808 captureRequest->inputBuffer.bufferId = bufferId;
3809 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3810 captureRequest->inputBuffer.status = BufferStatus::OK;
3811 native_handle_t *acquireFence = nullptr;
3812 if (request->input_buffer->acquire_fence != -1) {
3813 acquireFence = native_handle_create(1,0);
3814 acquireFence->data[0] = request->input_buffer->acquire_fence;
3815 handlesCreated->push_back(acquireFence);
3816 }
3817 captureRequest->inputBuffer.acquireFence = acquireFence;
3818 captureRequest->inputBuffer.releaseFence = nullptr;
3819
3820 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3821 request->input_buffer->buffer,
3822 request->input_buffer->acquire_fence);
3823 } else {
3824 captureRequest->inputBuffer.streamId = -1;
3825 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3826 }
3827
3828 captureRequest->outputBuffers.resize(request->num_output_buffers);
3829 for (size_t i = 0; i < request->num_output_buffers; i++) {
3830 const camera3_stream_buffer_t *src = request->output_buffers + i;
3831 StreamBuffer &dst = captureRequest->outputBuffers[i];
3832 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3833 buffer_handle_t buf = *(src->buffer);
3834 auto pair = getBufferId(buf, streamId);
3835 bool isNewBuffer = pair.first;
3836 dst.streamId = streamId;
3837 dst.bufferId = pair.second;
3838 dst.buffer = isNewBuffer ? buf : nullptr;
3839 dst.status = BufferStatus::OK;
3840 native_handle_t *acquireFence = nullptr;
3841 if (src->acquire_fence != -1) {
3842 acquireFence = native_handle_create(1,0);
3843 acquireFence->data[0] = src->acquire_fence;
3844 handlesCreated->push_back(acquireFence);
3845 }
3846 dst.acquireFence = acquireFence;
3847 dst.releaseFence = nullptr;
3848
3849 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3850 src->buffer, src->acquire_fence);
3851 }
3852 }
3853}
3854
3855status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3856 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3857 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3858 if (!valid()) return INVALID_OPERATION;
3859
Emilian Peevaebbe412018-01-15 13:53:24 +00003860 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3861 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3862 if (castResult_3_4.isOk()) {
3863 hidlSession_3_4 = castResult_3_4;
3864 }
3865
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003866 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003867 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003868 size_t batchSize = requests.size();
Emilian Peevaebbe412018-01-15 13:53:24 +00003869 if (hidlSession_3_4 != nullptr) {
3870 captureRequests_3_4.resize(batchSize);
3871 } else {
3872 captureRequests.resize(batchSize);
3873 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003874 std::vector<native_handle_t*> handlesCreated;
3875
3876 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003877 if (hidlSession_3_4 != nullptr) {
3878 wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3879 /*out*/&handlesCreated);
3880 } else {
3881 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3882 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003883 }
3884
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003885 std::vector<device::V3_2::BufferCache> cachesToRemove;
3886 {
3887 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3888 for (auto& pair : mFreedBuffers) {
3889 // The stream might have been removed since onBufferFreed
3890 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3891 cachesToRemove.push_back({pair.first, pair.second});
3892 }
3893 }
3894 mFreedBuffers.clear();
3895 }
3896
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003897 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3898 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003899
3900 // Write metadata to FMQ.
3901 for (size_t i = 0; i < batchSize; i++) {
3902 camera3_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003903 device::V3_2::CaptureRequest* captureRequest;
3904 if (hidlSession_3_4 != nullptr) {
3905 captureRequest = &captureRequests_3_4[i].v3_2;
3906 } else {
3907 captureRequest = &captureRequests[i];
3908 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003909
3910 if (request->settings != nullptr) {
3911 size_t settingsSize = get_camera_metadata_size(request->settings);
3912 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3913 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3914 captureRequest->settings.resize(0);
3915 captureRequest->fmqSettingsSize = settingsSize;
3916 } else {
3917 if (mRequestMetadataQueue != nullptr) {
3918 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3919 }
3920 captureRequest->settings.setToExternal(
3921 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3922 get_camera_metadata_size(request->settings));
3923 captureRequest->fmqSettingsSize = 0u;
3924 }
3925 } else {
3926 // A null request settings maps to a size-0 CameraMetadata
3927 captureRequest->settings.resize(0);
3928 captureRequest->fmqSettingsSize = 0u;
3929 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003930
3931 if (hidlSession_3_4 != nullptr) {
3932 captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3933 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00003934 if (request->physcam_settings != nullptr) {
3935 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3936 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3937 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3938 settingsSize)) {
3939 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3940 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
3941 settingsSize;
3942 } else {
3943 if (mRequestMetadataQueue != nullptr) {
3944 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3945 }
3946 captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3947 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3948 request->physcam_settings[j])),
3949 get_camera_metadata_size(request->physcam_settings[j]));
3950 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00003951 }
Emilian Peev00420d22018-02-05 21:33:13 +00003952 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00003953 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00003954 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00003955 }
3956 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3957 request->physcam_id[j];
3958 }
3959 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003960 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003961
3962 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003963 auto resultCallback =
3964 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3965 status = s;
3966 *numRequestProcessed = n;
3967 };
Emilian Peevaebbe412018-01-15 13:53:24 +00003968 if (hidlSession_3_4 != nullptr) {
3969 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003970 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003971 } else {
3972 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003973 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003974 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003975 if (!err.isOk()) {
3976 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3977 return DEAD_OBJECT;
3978 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003979 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3980 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3981 __FUNCTION__, *numRequestProcessed, batchSize);
3982 status = common::V1_0::Status::INTERNAL_ERROR;
3983 }
3984
3985 for (auto& handle : handlesCreated) {
3986 native_handle_delete(handle);
3987 }
3988 return CameraProviderManager::mapToStatusT(status);
3989}
3990
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003991status_t Camera3Device::HalInterface::processCaptureRequest(
3992 camera3_capture_request_t *request) {
3993 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003994 if (!valid()) return INVALID_OPERATION;
3995 status_t res = OK;
3996
Emilian Peev31abd0a2017-05-11 18:37:46 +01003997 uint32_t numRequestProcessed = 0;
3998 std::vector<camera3_capture_request_t*> requests(1);
3999 requests[0] = request;
4000 res = processBatchCaptureRequests(requests, &numRequestProcessed);
4001
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004002 return res;
4003}
4004
4005status_t Camera3Device::HalInterface::flush() {
4006 ATRACE_NAME("CameraHal::flush");
4007 if (!valid()) return INVALID_OPERATION;
4008 status_t res = OK;
4009
Emilian Peev31abd0a2017-05-11 18:37:46 +01004010 auto err = mHidlSession->flush();
4011 if (!err.isOk()) {
4012 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4013 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004014 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004015 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004016 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004017
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004018 return res;
4019}
4020
Emilian Peev31abd0a2017-05-11 18:37:46 +01004021status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004022 ATRACE_NAME("CameraHal::dump");
4023 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004024
Emilian Peev31abd0a2017-05-11 18:37:46 +01004025 // Handled by CameraProviderManager::dump
4026
4027 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004028}
4029
4030status_t Camera3Device::HalInterface::close() {
4031 ATRACE_NAME("CameraHal::close()");
4032 if (!valid()) return INVALID_OPERATION;
4033 status_t res = OK;
4034
Emilian Peev31abd0a2017-05-11 18:37:46 +01004035 auto err = mHidlSession->close();
4036 // Interface will be dead shortly anyway, so don't log errors
4037 if (!err.isOk()) {
4038 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004039 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004040
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004041 return res;
4042}
4043
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004044void Camera3Device::HalInterface::getInflightBufferKeys(
4045 std::vector<std::pair<int32_t, int32_t>>* out) {
4046 std::lock_guard<std::mutex> lock(mInflightLock);
4047 out->clear();
4048 out->reserve(mInflightBufferMap.size());
4049 for (auto& pair : mInflightBufferMap) {
4050 uint64_t key = pair.first;
4051 int32_t streamId = key & 0xFFFFFFFF;
4052 int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
4053 out->push_back(std::make_pair(frameNumber, streamId));
4054 }
4055 return;
4056}
4057
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004058status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004059 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004060 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004061 auto pair = std::make_pair(buffer, acquireFence);
4062 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004063 return OK;
4064}
4065
4066status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004067 int32_t frameNumber, int32_t streamId,
4068 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004069 std::lock_guard<std::mutex> lock(mInflightLock);
4070
4071 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4072 auto it = mInflightBufferMap.find(key);
4073 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004074 auto pair = it->second;
4075 *buffer = pair.first;
4076 int acquireFence = pair.second;
4077 if (acquireFence > 0) {
4078 ::close(acquireFence);
4079 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004080 mInflightBufferMap.erase(it);
4081 return OK;
4082}
4083
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004084std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4085 const buffer_handle_t& buf, int streamId) {
4086 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4087
4088 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4089 auto it = bIdMap.find(buf);
4090 if (it == bIdMap.end()) {
4091 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004092 ALOGV("stream %d now have %zu buffer caches, buf %p",
4093 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004094 return std::make_pair(true, mNextBufferId - 1);
4095 } else {
4096 return std::make_pair(false, it->second);
4097 }
4098}
4099
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004100void Camera3Device::HalInterface::onBufferFreed(
4101 int streamId, const native_handle_t* handle) {
4102 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4103 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4104 auto mapIt = mBufferIdMaps.find(streamId);
4105 if (mapIt == mBufferIdMaps.end()) {
4106 // streamId might be from a deleted stream here
4107 ALOGI("%s: stream %d has been removed",
4108 __FUNCTION__, streamId);
4109 return;
4110 }
4111 BufferIdMap& bIdMap = mapIt->second;
4112 auto it = bIdMap.find(handle);
4113 if (it == bIdMap.end()) {
4114 ALOGW("%s: cannot find buffer %p in stream %d",
4115 __FUNCTION__, handle, streamId);
4116 return;
4117 } else {
4118 bufferId = it->second;
4119 bIdMap.erase(it);
4120 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4121 __FUNCTION__, streamId, bIdMap.size(), handle);
4122 }
4123 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4124}
4125
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004126/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004127 * RequestThread inner class methods
4128 */
4129
4130Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004131 sp<StatusTracker> statusTracker,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004132 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004133 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004134 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004135 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004136 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004137 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004138 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004139 mReconfigured(false),
4140 mDoPause(false),
4141 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004142 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004143 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004144 mCurrentAfTriggerId(0),
4145 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004146 mRepeatingLastFrameNumber(
4147 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004148 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004149 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004150 mRequestLatency(kRequestLatencyBinSize),
4151 mSessionParamKeys(sessionParamKeys),
4152 mLatestSessionParams(sessionParamKeys.size()) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004153 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004154}
4155
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004156Camera3Device::RequestThread::~RequestThread() {}
4157
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004158void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004159 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004160 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004161 Mutex::Autolock l(mRequestLock);
4162 mListener = listener;
4163}
4164
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004165void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4166 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004167 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004168 Mutex::Autolock l(mRequestLock);
4169 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004170 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004171 // Prepare video stream for high speed recording.
4172 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004173 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004174}
4175
Jianing Wei90e59c92014-03-12 18:29:36 -07004176status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004177 List<sp<CaptureRequest> > &requests,
4178 /*out*/
4179 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004180 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004181 Mutex::Autolock l(mRequestLock);
4182 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4183 ++it) {
4184 mRequestQueue.push_back(*it);
4185 }
4186
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004187 if (lastFrameNumber != NULL) {
4188 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4189 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4190 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4191 *lastFrameNumber);
4192 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004193
Jianing Wei90e59c92014-03-12 18:29:36 -07004194 unpauseForNewRequests();
4195
4196 return OK;
4197}
4198
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004199
4200status_t Camera3Device::RequestThread::queueTrigger(
4201 RequestTrigger trigger[],
4202 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004203 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004204 Mutex::Autolock l(mTriggerMutex);
4205 status_t ret;
4206
4207 for (size_t i = 0; i < count; ++i) {
4208 ret = queueTriggerLocked(trigger[i]);
4209
4210 if (ret != OK) {
4211 return ret;
4212 }
4213 }
4214
4215 return OK;
4216}
4217
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004218const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4219 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004220 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004221 if (d != nullptr) return d->mId;
4222 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004223}
4224
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004225status_t Camera3Device::RequestThread::queueTriggerLocked(
4226 RequestTrigger trigger) {
4227
4228 uint32_t tag = trigger.metadataTag;
4229 ssize_t index = mTriggerMap.indexOfKey(tag);
4230
4231 switch (trigger.getTagType()) {
4232 case TYPE_BYTE:
4233 // fall-through
4234 case TYPE_INT32:
4235 break;
4236 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004237 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4238 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004239 return INVALID_OPERATION;
4240 }
4241
4242 /**
4243 * Collect only the latest trigger, since we only have 1 field
4244 * in the request settings per trigger tag, and can't send more than 1
4245 * trigger per request.
4246 */
4247 if (index != NAME_NOT_FOUND) {
4248 mTriggerMap.editValueAt(index) = trigger;
4249 } else {
4250 mTriggerMap.add(tag, trigger);
4251 }
4252
4253 return OK;
4254}
4255
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004256status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004257 const RequestList &requests,
4258 /*out*/
4259 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004260 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004261 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004262 if (lastFrameNumber != NULL) {
4263 *lastFrameNumber = mRepeatingLastFrameNumber;
4264 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004265 mRepeatingRequests.clear();
4266 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4267 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004268
4269 unpauseForNewRequests();
4270
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004271 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004272 return OK;
4273}
4274
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004275bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004276 if (mRepeatingRequests.empty()) {
4277 return false;
4278 }
4279 int32_t requestId = requestIn->mResultExtras.requestId;
4280 const RequestList &repeatRequests = mRepeatingRequests;
4281 // All repeating requests are guaranteed to have same id so only check first quest
4282 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4283 return (firstRequest->mResultExtras.requestId == requestId);
4284}
4285
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004286status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004287 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004288 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004289 return clearRepeatingRequestsLocked(lastFrameNumber);
4290
4291}
4292
4293status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004294 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004295 if (lastFrameNumber != NULL) {
4296 *lastFrameNumber = mRepeatingLastFrameNumber;
4297 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004298 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004299 return OK;
4300}
4301
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004302status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004303 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004304 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004305 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004306 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004307
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004308 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004309
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004310 // Send errors for all requests pending in the request queue, including
4311 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004312 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004313 if (listener != NULL) {
4314 for (RequestList::iterator it = mRequestQueue.begin();
4315 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004316 // Abort the input buffers for reprocess requests.
4317 if ((*it)->mInputStream != NULL) {
4318 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004319 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4320 /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004321 if (res != OK) {
4322 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4323 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4324 } else {
4325 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4326 if (res != OK) {
4327 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4328 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4329 }
4330 }
4331 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004332 // Set the frame number this request would have had, if it
4333 // had been submitted; this frame number will not be reused.
4334 // The requestId and burstId fields were set when the request was
4335 // submitted originally (in convertMetadataListToRequestListLocked)
4336 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004337 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004338 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004339 }
4340 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004341 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004342
4343 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004344 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004345 if (lastFrameNumber != NULL) {
4346 *lastFrameNumber = mRepeatingLastFrameNumber;
4347 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004348 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004349 return OK;
4350}
4351
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004352status_t Camera3Device::RequestThread::flush() {
4353 ATRACE_CALL();
4354 Mutex::Autolock l(mFlushLock);
4355
Emilian Peev08dd2452017-04-06 16:55:14 +01004356 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004357}
4358
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004359void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004360 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004361 Mutex::Autolock l(mPauseLock);
4362 mDoPause = paused;
4363 mDoPauseSignal.signal();
4364}
4365
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004366status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4367 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004368 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004369 Mutex::Autolock l(mLatestRequestMutex);
4370 status_t res;
4371 while (mLatestRequestId != requestId) {
4372 nsecs_t startTime = systemTime();
4373
4374 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4375 if (res != OK) return res;
4376
4377 timeout -= (systemTime() - startTime);
4378 }
4379
4380 return OK;
4381}
4382
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004383void Camera3Device::RequestThread::requestExit() {
4384 // Call parent to set up shutdown
4385 Thread::requestExit();
4386 // The exit from any possible waits
4387 mDoPauseSignal.signal();
4388 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004389
4390 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4391 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004392}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004393
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004394void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004395 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004396 bool surfaceAbandoned = false;
4397 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004398 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004399 {
4400 Mutex::Autolock l(mRequestLock);
4401 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4402 // repeating requests.
4403 for (const auto& request : mRepeatingRequests) {
4404 for (const auto& s : request->mOutputStreams) {
4405 if (s->isAbandoned()) {
4406 surfaceAbandoned = true;
4407 clearRepeatingRequestsLocked(&lastFrameNumber);
4408 break;
4409 }
4410 }
4411 if (surfaceAbandoned) {
4412 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004413 }
4414 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004415 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004416 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004417
4418 if (listener != NULL && surfaceAbandoned) {
4419 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004420 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004421}
4422
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004423bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004424 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004425 status_t res;
4426 size_t batchSize = mNextRequests.size();
4427 std::vector<camera3_capture_request_t*> requests(batchSize);
4428 uint32_t numRequestProcessed = 0;
4429 for (size_t i = 0; i < batchSize; i++) {
4430 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004431 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004432 }
4433
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004434 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4435
4436 bool triggerRemoveFailed = false;
4437 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4438 for (size_t i = 0; i < numRequestProcessed; i++) {
4439 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4440 nextRequest.submitted = true;
4441
4442
4443 // Update the latest request sent to HAL
4444 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4445 Mutex::Autolock al(mLatestRequestMutex);
4446
4447 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4448 mLatestRequest.acquire(cloned);
4449
4450 sp<Camera3Device> parent = mParent.promote();
4451 if (parent != NULL) {
4452 parent->monitorMetadata(TagMonitor::REQUEST,
4453 nextRequest.halRequest.frame_number,
4454 0, mLatestRequest);
4455 }
4456 }
4457
4458 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004459 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4460 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004461 }
4462
Emilian Peevaebbe412018-01-15 13:53:24 +00004463 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4464
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004465 if (!triggerRemoveFailed) {
4466 // Remove any previously queued triggers (after unlock)
4467 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4468 if (removeTriggerRes != OK) {
4469 triggerRemoveFailed = true;
4470 triggerFailedRequest = nextRequest;
4471 }
4472 }
4473 }
4474
4475 if (triggerRemoveFailed) {
4476 SET_ERR("RequestThread: Unable to remove triggers "
4477 "(capture request %d, HAL device: %s (%d)",
4478 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4479 cleanUpFailedRequests(/*sendRequestError*/ false);
4480 return false;
4481 }
4482
4483 if (res != OK) {
4484 // Should only get a failure here for malformed requests or device-level
4485 // errors, so consider all errors fatal. Bad metadata failures should
4486 // come through notify.
4487 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4488 mNextRequests[numRequestProcessed].halRequest.frame_number,
4489 strerror(-res), res);
4490 cleanUpFailedRequests(/*sendRequestError*/ false);
4491 return false;
4492 }
4493 return true;
4494}
4495
4496bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4497 status_t res;
4498
4499 for (auto& nextRequest : mNextRequests) {
4500 // Submit request and block until ready for next one
4501 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4502 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4503
4504 if (res != OK) {
4505 // Should only get a failure here for malformed requests or device-level
4506 // errors, so consider all errors fatal. Bad metadata failures should
4507 // come through notify.
4508 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4509 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4510 res);
4511 cleanUpFailedRequests(/*sendRequestError*/ false);
4512 return false;
4513 }
4514
4515 // Mark that the request has be submitted successfully.
4516 nextRequest.submitted = true;
4517
4518 // Update the latest request sent to HAL
4519 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4520 Mutex::Autolock al(mLatestRequestMutex);
4521
4522 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4523 mLatestRequest.acquire(cloned);
4524
4525 sp<Camera3Device> parent = mParent.promote();
4526 if (parent != NULL) {
4527 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4528 0, mLatestRequest);
4529 }
4530 }
4531
4532 if (nextRequest.halRequest.settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004533 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4534 nextRequest.halRequest.settings);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004535 }
4536
Emilian Peevaebbe412018-01-15 13:53:24 +00004537 cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4538
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004539 // Remove any previously queued triggers (after unlock)
4540 res = removeTriggers(mPrevRequest);
4541 if (res != OK) {
4542 SET_ERR("RequestThread: Unable to remove triggers "
4543 "(capture request %d, HAL device: %s (%d)",
4544 nextRequest.halRequest.frame_number, strerror(-res), res);
4545 cleanUpFailedRequests(/*sendRequestError*/ false);
4546 return false;
4547 }
4548 }
4549 return true;
4550}
4551
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004552nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4553 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4554 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4555 find_camera_metadata_ro_entry(request,
4556 ANDROID_CONTROL_AE_MODE,
4557 &e);
4558 if (e.count == 0) return maxExpectedDuration;
4559
4560 switch (e.data.u8[0]) {
4561 case ANDROID_CONTROL_AE_MODE_OFF:
4562 find_camera_metadata_ro_entry(request,
4563 ANDROID_SENSOR_EXPOSURE_TIME,
4564 &e);
4565 if (e.count > 0) {
4566 maxExpectedDuration = e.data.i64[0];
4567 }
4568 find_camera_metadata_ro_entry(request,
4569 ANDROID_SENSOR_FRAME_DURATION,
4570 &e);
4571 if (e.count > 0) {
4572 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4573 }
4574 break;
4575 default:
4576 find_camera_metadata_ro_entry(request,
4577 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4578 &e);
4579 if (e.count > 1) {
4580 maxExpectedDuration = 1e9 / e.data.u8[0];
4581 }
4582 break;
4583 }
4584
4585 return maxExpectedDuration;
4586}
4587
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004588bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4589 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4590 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4591 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4592 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4593 return true;
4594 }
4595
4596 return false;
4597}
4598
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004599bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4600 ATRACE_CALL();
4601 bool updatesDetected = false;
4602
4603 for (auto tag : mSessionParamKeys) {
4604 camera_metadata_ro_entry entry = settings.find(tag);
4605 camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4606
4607 if (entry.count > 0) {
4608 bool isDifferent = false;
4609 if (lastEntry.count > 0) {
4610 // Have a last value, compare to see if changed
4611 if (lastEntry.type == entry.type &&
4612 lastEntry.count == entry.count) {
4613 // Same type and count, compare values
4614 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4615 size_t entryBytes = bytesPerValue * lastEntry.count;
4616 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4617 if (cmp != 0) {
4618 isDifferent = true;
4619 }
4620 } else {
4621 // Count or type has changed
4622 isDifferent = true;
4623 }
4624 } else {
4625 // No last entry, so always consider to be different
4626 isDifferent = true;
4627 }
4628
4629 if (isDifferent) {
4630 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004631 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4632 updatesDetected = true;
4633 }
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004634 mLatestSessionParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004635 }
4636 } else if (lastEntry.count > 0) {
4637 // Value has been removed
4638 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4639 mLatestSessionParams.erase(tag);
4640 updatesDetected = true;
4641 }
4642 }
4643
4644 return updatesDetected;
4645}
4646
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004647bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004648 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004649 status_t res;
4650
4651 // Handle paused state.
4652 if (waitIfPaused()) {
4653 return true;
4654 }
4655
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004656 // Wait for the next batch of requests.
4657 waitForNextRequestBatch();
4658 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004659 return true;
4660 }
4661
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004662 // Get the latest request ID, if any
4663 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004664 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004665 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004666 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004667 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004668 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004669 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4670 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004671 }
4672
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004673 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4674 // or a single request from streaming or burst. In either case the first element
4675 // should contain the latest camera settings that we need to check for any session
4676 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004677 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004678 res = OK;
4679
4680 //Input stream buffers are already acquired at this point so an input stream
4681 //will not be able to move to idle state unless we force it.
4682 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4683 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4684 if (res != OK) {
4685 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4686 cleanUpFailedRequests(/*sendRequestError*/ false);
4687 return false;
4688 }
4689 }
4690
4691 if (res == OK) {
4692 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4693 if (statusTracker != 0) {
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08004694 sp<Camera3Device> parent = mParent.promote();
4695 if (parent != nullptr) {
4696 parent->pauseStateNotify(true);
4697 }
4698
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004699 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4700
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004701 if (parent != nullptr) {
4702 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4703 }
4704
4705 statusTracker->markComponentActive(mStatusId);
4706 setPaused(false);
4707 }
4708
4709 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4710 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4711 if (res != OK) {
4712 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4713 cleanUpFailedRequests(/*sendRequestError*/ false);
4714 return false;
4715 }
4716 }
4717 }
4718 }
4719
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004720 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004721 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004722 if (res == TIMED_OUT) {
4723 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004724 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004725 // Check if any stream is abandoned.
4726 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004727 return true;
4728 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004729 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004730 return false;
4731 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004732
Zhijun Hecc27e112013-10-03 16:12:43 -07004733 // Inform waitUntilRequestProcessed thread of a new request ID
4734 {
4735 Mutex::Autolock al(mLatestRequestMutex);
4736
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004737 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004738 mLatestRequestSignal.signal();
4739 }
4740
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004741 // Submit a batch of requests to HAL.
4742 // Use flush lock only when submitting multilple requests in a batch.
4743 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4744 // which may take a long time to finish so synchronizing flush() and
4745 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4746 // For now, only synchronize for high speed recording and we should figure something out for
4747 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004748 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004749
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004750 if (useFlushLock) {
4751 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004752 }
4753
Zhijun Hef0645c12016-08-02 00:58:11 -07004754 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004755 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004756
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004757 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004758 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004759 if (mInterface->supportBatchRequest()) {
4760 submitRequestSuccess = sendRequestsBatch();
4761 } else {
4762 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004763 }
Shuzhen Wang686f6442017-06-20 16:16:04 -07004764 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4765 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004766
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004767 if (useFlushLock) {
4768 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004769 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004770
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004771 // Unset as current request
4772 {
4773 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004774 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004775 }
4776
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004777 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004778}
4779
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004780status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004781 ATRACE_CALL();
4782
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004783 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004784 for (size_t i = 0; i < mNextRequests.size(); i++) {
4785 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004786 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4787 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4788 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4789
4790 // Prepare a request to HAL
4791 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4792
4793 // Insert any queued triggers (before metadata is locked)
4794 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004795 if (res < 0) {
4796 SET_ERR("RequestThread: Unable to insert triggers "
4797 "(capture request %d, HAL device: %s (%d)",
4798 halRequest->frame_number, strerror(-res), res);
4799 return INVALID_OPERATION;
4800 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004801
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004802 int triggerCount = res;
4803 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4804 mPrevTriggers = triggerCount;
4805
4806 // If the request is the same as last, or we had triggers last time
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004807 bool newRequest = (mPrevRequest != captureRequest || triggersMixedIn) &&
4808 // Request settings are all the same within one batch, so only treat the first
4809 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07004810 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00004811 if (newRequest) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004812 /**
4813 * HAL workaround:
4814 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4815 */
4816 res = addDummyTriggerIds(captureRequest);
4817 if (res != OK) {
4818 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4819 "(capture request %d, HAL device: %s (%d)",
4820 halRequest->frame_number, strerror(-res), res);
4821 return INVALID_OPERATION;
4822 }
4823
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004824 {
4825 // Correct metadata regions for distortion correction if enabled
4826 sp<Camera3Device> parent = mParent.promote();
4827 if (parent != nullptr) {
4828 res = parent->mDistortionMapper.correctCaptureRequest(
4829 &(captureRequest->mSettingsList.begin()->metadata));
4830 if (res != OK) {
4831 SET_ERR("RequestThread: Unable to correct capture requests "
4832 "for lens distortion for request %d: %s (%d)",
4833 halRequest->frame_number, strerror(-res), res);
4834 return INVALID_OPERATION;
4835 }
4836 }
4837 }
4838
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004839 /**
4840 * The request should be presorted so accesses in HAL
4841 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4842 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004843 captureRequest->mSettingsList.begin()->metadata.sort();
4844 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004845 mPrevRequest = captureRequest;
4846 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4847
4848 IF_ALOGV() {
4849 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4850 find_camera_metadata_ro_entry(
4851 halRequest->settings,
4852 ANDROID_CONTROL_AF_TRIGGER,
4853 &e
4854 );
4855 if (e.count > 0) {
4856 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4857 __FUNCTION__,
4858 halRequest->frame_number,
4859 e.data.u8[0]);
4860 }
4861 }
4862 } else {
4863 // leave request.settings NULL to indicate 'reuse latest given'
4864 ALOGVV("%s: Request settings are REUSED",
4865 __FUNCTION__);
4866 }
4867
Emilian Peevaebbe412018-01-15 13:53:24 +00004868 if (captureRequest->mSettingsList.size() > 1) {
4869 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4870 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00004871 if (newRequest) {
4872 halRequest->physcam_settings =
4873 new const camera_metadata* [halRequest->num_physcam_settings];
4874 } else {
4875 halRequest->physcam_settings = nullptr;
4876 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004877 auto it = ++captureRequest->mSettingsList.begin();
4878 size_t i = 0;
4879 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4880 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00004881 if (newRequest) {
4882 it->metadata.sort();
4883 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4884 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004885 }
4886 }
4887
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004888 uint32_t totalNumBuffers = 0;
4889
4890 // Fill in buffers
4891 if (captureRequest->mInputStream != NULL) {
4892 halRequest->input_buffer = &captureRequest->mInputBuffer;
4893 totalNumBuffers += 1;
4894 } else {
4895 halRequest->input_buffer = NULL;
4896 }
4897
4898 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4899 captureRequest->mOutputStreams.size());
4900 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004901 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07004902
4903 sp<Camera3Device> parent = mParent.promote();
4904 if (parent == NULL) {
4905 // Should not happen, and nowhere to send errors to, so just log it
4906 CLOGE("RequestThread: Parent is gone");
4907 return INVALID_OPERATION;
4908 }
4909 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
4910
Shuzhen Wang4a472662017-02-26 23:29:04 -08004911 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4912 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004913
4914 // Prepare video buffers for high speed recording on the first video request.
4915 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4916 // Only try to prepare video stream on the first video request.
4917 mPrepareVideoStream = false;
4918
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004919 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
4920 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004921 while (res == NOT_ENOUGH_DATA) {
4922 res = outputStream->prepareNextBuffer();
4923 }
4924 if (res != OK) {
4925 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4926 __FUNCTION__, strerror(-res), res);
4927 outputStream->cancelPrepare();
4928 }
4929 }
4930
Shuzhen Wang4a472662017-02-26 23:29:04 -08004931 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07004932 waitDuration,
Shuzhen Wang4a472662017-02-26 23:29:04 -08004933 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004934 if (res != OK) {
4935 // Can't get output buffer from gralloc queue - this could be due to
4936 // abandoned queue or other consumer misbehavior, so not a fatal
4937 // error
4938 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4939 " %s (%d)", strerror(-res), res);
4940
4941 return TIMED_OUT;
4942 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004943
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004944 String8 physicalCameraId = outputStream->getPhysicalCameraId();
4945
4946 if (!physicalCameraId.isEmpty()) {
4947 // Physical stream isn't supported for input request.
4948 if (halRequest->input_buffer) {
4949 CLOGE("Physical stream is not supported for input request");
4950 return INVALID_OPERATION;
4951 }
4952 requestedPhysicalCameras.insert(physicalCameraId);
4953 }
4954 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004955 }
4956 totalNumBuffers += halRequest->num_output_buffers;
4957
4958 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004959 // If this request list is for constrained high speed recording (not
4960 // preview), and the current request is not the last one in the batch,
4961 // do not send callback to the app.
4962 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004963 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004964 hasCallback = false;
4965 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004966 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004967 bool isZslCapture = false;
Emilian Peev9dd21f42018-08-03 13:39:29 +01004968 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4969 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4970 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
4971 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4972 isStillCapture = true;
4973 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4974 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004975
4976 find_camera_metadata_ro_entry(halRequest->settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
4977 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4978 isZslCapture = true;
4979 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004980 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004981 res = parent->registerInFlight(halRequest->frame_number,
4982 totalNumBuffers, captureRequest->mResultExtras,
4983 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004984 hasCallback,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004985 calculateMaxExpectedDuration(halRequest->settings),
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004986 requestedPhysicalCameras, isStillCapture, isZslCapture);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004987 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4988 ", burstId = %" PRId32 ".",
4989 __FUNCTION__,
4990 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4991 captureRequest->mResultExtras.burstId);
4992 if (res != OK) {
4993 SET_ERR("RequestThread: Unable to register new in-flight request:"
4994 " %s (%d)", strerror(-res), res);
4995 return INVALID_OPERATION;
4996 }
4997 }
4998
4999 return OK;
5000}
5001
Igor Murashkin1e479c02013-09-06 16:55:14 -07005002CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005003 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005004 Mutex::Autolock al(mLatestRequestMutex);
5005
5006 ALOGV("RequestThread::%s", __FUNCTION__);
5007
5008 return mLatestRequest;
5009}
5010
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005011bool Camera3Device::RequestThread::isStreamPending(
5012 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005013 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005014 Mutex::Autolock l(mRequestLock);
5015
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005016 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005017 if (!nextRequest.submitted) {
5018 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5019 if (stream == s) return true;
5020 }
5021 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005022 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005023 }
5024
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005025 for (const auto& request : mRequestQueue) {
5026 for (const auto& s : request->mOutputStreams) {
5027 if (stream == s) return true;
5028 }
5029 if (stream == request->mInputStream) return true;
5030 }
5031
5032 for (const auto& request : mRepeatingRequests) {
5033 for (const auto& s : request->mOutputStreams) {
5034 if (stream == s) return true;
5035 }
5036 if (stream == request->mInputStream) return true;
5037 }
5038
5039 return false;
5040}
Jianing Weicb0652e2014-03-12 18:29:36 -07005041
Emilian Peev40ead602017-09-26 15:46:36 +01005042bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5043 ATRACE_CALL();
5044 Mutex::Autolock l(mRequestLock);
5045
5046 for (const auto& nextRequest : mNextRequests) {
5047 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5048 if (s.first == streamId) {
5049 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5050 if (it != s.second.end()) {
5051 return true;
5052 }
5053 }
5054 }
5055 }
5056
5057 for (const auto& request : mRequestQueue) {
5058 for (const auto& s : request->mOutputSurfaces) {
5059 if (s.first == streamId) {
5060 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5061 if (it != s.second.end()) {
5062 return true;
5063 }
5064 }
5065 }
5066 }
5067
5068 for (const auto& request : mRepeatingRequests) {
5069 for (const auto& s : request->mOutputSurfaces) {
5070 if (s.first == streamId) {
5071 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5072 if (it != s.second.end()) {
5073 return true;
5074 }
5075 }
5076 }
5077 }
5078
5079 return false;
5080}
5081
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005082nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005083 ATRACE_CALL();
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005084 Mutex::Autolock al(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005085 return mExpectedInflightDuration > kMinInflightDuration ?
5086 mExpectedInflightDuration : kMinInflightDuration;
5087}
5088
Emilian Peevaebbe412018-01-15 13:53:24 +00005089void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
5090 camera3_capture_request_t *halRequest) {
5091 if ((request == nullptr) || (halRequest == nullptr)) {
5092 ALOGE("%s: Invalid request!", __FUNCTION__);
5093 return;
5094 }
5095
5096 if (halRequest->num_physcam_settings > 0) {
5097 if (halRequest->physcam_id != nullptr) {
5098 delete [] halRequest->physcam_id;
5099 halRequest->physcam_id = nullptr;
5100 }
5101 if (halRequest->physcam_settings != nullptr) {
5102 auto it = ++(request->mSettingsList.begin());
5103 size_t i = 0;
5104 for (; it != request->mSettingsList.end(); it++, i++) {
5105 it->metadata.unlock(halRequest->physcam_settings[i]);
5106 }
5107 delete [] halRequest->physcam_settings;
5108 halRequest->physcam_settings = nullptr;
5109 }
5110 }
5111}
5112
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005113void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5114 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005115 return;
5116 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005117
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005118 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005119 // Skip the ones that have been submitted successfully.
5120 if (nextRequest.submitted) {
5121 continue;
5122 }
5123
5124 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5125 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5126 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5127
5128 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005129 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005130 }
5131
Emilian Peevaebbe412018-01-15 13:53:24 +00005132 cleanupPhysicalSettings(captureRequest, halRequest);
5133
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005134 if (captureRequest->mInputStream != NULL) {
5135 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5136 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5137 }
5138
5139 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005140 //Buffers that failed processing could still have
5141 //valid acquire fence.
5142 int acquireFence = (*outputBuffers)[i].acquire_fence;
5143 if (0 <= acquireFence) {
5144 close(acquireFence);
5145 outputBuffers->editItemAt(i).acquire_fence = -1;
5146 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005147 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5148 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
5149 }
5150
5151 if (sendRequestError) {
5152 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005153 sp<NotificationListener> listener = mListener.promote();
5154 if (listener != NULL) {
5155 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005156 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005157 captureRequest->mResultExtras);
5158 }
5159 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005160
5161 // Remove yet-to-be submitted inflight request from inflightMap
5162 {
5163 sp<Camera3Device> parent = mParent.promote();
5164 if (parent != NULL) {
5165 Mutex::Autolock l(parent->mInFlightLock);
5166 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5167 if (idx >= 0) {
5168 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5169 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5170 parent->removeInFlightMapEntryLocked(idx);
5171 }
5172 }
5173 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005174 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005175
5176 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005177 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005178}
5179
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005180void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005181 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005182 // Optimized a bit for the simple steady-state case (single repeating
5183 // request), to avoid putting that request in the queue temporarily.
5184 Mutex::Autolock l(mRequestLock);
5185
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005186 assert(mNextRequests.empty());
5187
5188 NextRequest nextRequest;
5189 nextRequest.captureRequest = waitForNextRequestLocked();
5190 if (nextRequest.captureRequest == nullptr) {
5191 return;
5192 }
5193
5194 nextRequest.halRequest = camera3_capture_request_t();
5195 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005196 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005197
5198 // Wait for additional requests
5199 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5200
5201 for (size_t i = 1; i < batchSize; i++) {
5202 NextRequest additionalRequest;
5203 additionalRequest.captureRequest = waitForNextRequestLocked();
5204 if (additionalRequest.captureRequest == nullptr) {
5205 break;
5206 }
5207
5208 additionalRequest.halRequest = camera3_capture_request_t();
5209 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005210 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005211 }
5212
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005213 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005214 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005215 mNextRequests.size(), batchSize);
5216 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005217 }
5218
5219 return;
5220}
5221
5222sp<Camera3Device::CaptureRequest>
5223 Camera3Device::RequestThread::waitForNextRequestLocked() {
5224 status_t res;
5225 sp<CaptureRequest> nextRequest;
5226
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005227 while (mRequestQueue.empty()) {
5228 if (!mRepeatingRequests.empty()) {
5229 // Always atomically enqueue all requests in a repeating request
5230 // list. Guarantees a complete in-sequence set of captures to
5231 // application.
5232 const RequestList &requests = mRepeatingRequests;
5233 RequestList::const_iterator firstRequest =
5234 requests.begin();
5235 nextRequest = *firstRequest;
5236 mRequestQueue.insert(mRequestQueue.end(),
5237 ++firstRequest,
5238 requests.end());
5239 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005240
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005241 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005243 break;
5244 }
5245
5246 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5247
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005248 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5249 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005250 Mutex::Autolock pl(mPauseLock);
5251 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005252 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005253 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005254 // Let the tracker know
5255 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5256 if (statusTracker != 0) {
5257 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5258 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005259 }
5260 // Stop waiting for now and let thread management happen
5261 return NULL;
5262 }
5263 }
5264
5265 if (nextRequest == NULL) {
5266 // Don't have a repeating request already in hand, so queue
5267 // must have an entry now.
5268 RequestList::iterator firstRequest =
5269 mRequestQueue.begin();
5270 nextRequest = *firstRequest;
5271 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005272 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5273 sp<NotificationListener> listener = mListener.promote();
5274 if (listener != NULL) {
5275 listener->notifyRequestQueueEmpty();
5276 }
5277 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005278 }
5279
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005280 // In case we've been unpaused by setPaused clearing mDoPause, need to
5281 // update internal pause state (capture/setRepeatingRequest unpause
5282 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005283 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005284 if (mPaused) {
5285 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5286 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5287 if (statusTracker != 0) {
5288 statusTracker->markComponentActive(mStatusId);
5289 }
5290 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005291 mPaused = false;
5292
5293 // Check if we've reconfigured since last time, and reset the preview
5294 // request if so. Can't use 'NULL request == repeat' across configure calls.
5295 if (mReconfigured) {
5296 mPrevRequest.clear();
5297 mReconfigured = false;
5298 }
5299
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005300 if (nextRequest != NULL) {
5301 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005302 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5303 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005304
5305 // Since RequestThread::clear() removes buffers from the input stream,
5306 // get the right buffer here before unlocking mRequestLock
5307 if (nextRequest->mInputStream != NULL) {
5308 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5309 if (res != OK) {
5310 // Can't get input buffer from gralloc queue - this could be due to
5311 // disconnected queue or other producer misbehavior, so not a fatal
5312 // error
5313 ALOGE("%s: Can't get input buffer, skipping request:"
5314 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005315
5316 sp<NotificationListener> listener = mListener.promote();
5317 if (listener != NULL) {
5318 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005319 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005320 nextRequest->mResultExtras);
5321 }
5322 return NULL;
5323 }
5324 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005325 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005326
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005327 return nextRequest;
5328}
5329
5330bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005331 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005332 status_t res;
5333 Mutex::Autolock l(mPauseLock);
5334 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005335 if (mPaused == false) {
5336 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005337 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5338 // Let the tracker know
5339 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5340 if (statusTracker != 0) {
5341 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5342 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005343 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005344
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005345 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005346 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005347 return true;
5348 }
5349 }
5350 // We don't set mPaused to false here, because waitForNextRequest needs
5351 // to further manage the paused state in case of starvation.
5352 return false;
5353}
5354
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005355void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005356 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005357 // With work to do, mark thread as unpaused.
5358 // If paused by request (setPaused), don't resume, to avoid
5359 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005360 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005361 Mutex::Autolock p(mPauseLock);
5362 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005363 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5364 if (mPaused) {
5365 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5366 if (statusTracker != 0) {
5367 statusTracker->markComponentActive(mStatusId);
5368 }
5369 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005370 mPaused = false;
5371 }
5372}
5373
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005374void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5375 sp<Camera3Device> parent = mParent.promote();
5376 if (parent != NULL) {
5377 va_list args;
5378 va_start(args, fmt);
5379
5380 parent->setErrorStateV(fmt, args);
5381
5382 va_end(args);
5383 }
5384}
5385
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005386status_t Camera3Device::RequestThread::insertTriggers(
5387 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005388 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005389 Mutex::Autolock al(mTriggerMutex);
5390
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005391 sp<Camera3Device> parent = mParent.promote();
5392 if (parent == NULL) {
5393 CLOGE("RequestThread: Parent is gone");
5394 return DEAD_OBJECT;
5395 }
5396
Emilian Peevaebbe412018-01-15 13:53:24 +00005397 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005398 size_t count = mTriggerMap.size();
5399
5400 for (size_t i = 0; i < count; ++i) {
5401 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005402 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005403
5404 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5405 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5406 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005407 if (isAeTrigger) {
5408 request->mResultExtras.precaptureTriggerId = triggerId;
5409 mCurrentPreCaptureTriggerId = triggerId;
5410 } else {
5411 request->mResultExtras.afTriggerId = triggerId;
5412 mCurrentAfTriggerId = triggerId;
5413 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005414 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005415 }
5416
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005417 camera_metadata_entry entry = metadata.find(tag);
5418
5419 if (entry.count > 0) {
5420 /**
5421 * Already has an entry for this trigger in the request.
5422 * Rewrite it with our requested trigger value.
5423 */
5424 RequestTrigger oldTrigger = trigger;
5425
5426 oldTrigger.entryValue = entry.data.u8[0];
5427
5428 mTriggerReplacedMap.add(tag, oldTrigger);
5429 } else {
5430 /**
5431 * More typical, no trigger entry, so we just add it
5432 */
5433 mTriggerRemovedMap.add(tag, trigger);
5434 }
5435
5436 status_t res;
5437
5438 switch (trigger.getTagType()) {
5439 case TYPE_BYTE: {
5440 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5441 res = metadata.update(tag,
5442 &entryValue,
5443 /*count*/1);
5444 break;
5445 }
5446 case TYPE_INT32:
5447 res = metadata.update(tag,
5448 &trigger.entryValue,
5449 /*count*/1);
5450 break;
5451 default:
5452 ALOGE("%s: Type not supported: 0x%x",
5453 __FUNCTION__,
5454 trigger.getTagType());
5455 return INVALID_OPERATION;
5456 }
5457
5458 if (res != OK) {
5459 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5460 ", value %d", __FUNCTION__, trigger.getTagName(),
5461 trigger.entryValue);
5462 return res;
5463 }
5464
5465 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5466 trigger.getTagName(),
5467 trigger.entryValue);
5468 }
5469
5470 mTriggerMap.clear();
5471
5472 return count;
5473}
5474
5475status_t Camera3Device::RequestThread::removeTriggers(
5476 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005477 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005478 Mutex::Autolock al(mTriggerMutex);
5479
Emilian Peevaebbe412018-01-15 13:53:24 +00005480 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005481
5482 /**
5483 * Replace all old entries with their old values.
5484 */
5485 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5486 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5487
5488 status_t res;
5489
5490 uint32_t tag = trigger.metadataTag;
5491 switch (trigger.getTagType()) {
5492 case TYPE_BYTE: {
5493 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5494 res = metadata.update(tag,
5495 &entryValue,
5496 /*count*/1);
5497 break;
5498 }
5499 case TYPE_INT32:
5500 res = metadata.update(tag,
5501 &trigger.entryValue,
5502 /*count*/1);
5503 break;
5504 default:
5505 ALOGE("%s: Type not supported: 0x%x",
5506 __FUNCTION__,
5507 trigger.getTagType());
5508 return INVALID_OPERATION;
5509 }
5510
5511 if (res != OK) {
5512 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5513 ", trigger value %d", __FUNCTION__,
5514 trigger.getTagName(), trigger.entryValue);
5515 return res;
5516 }
5517 }
5518 mTriggerReplacedMap.clear();
5519
5520 /**
5521 * Remove all new entries.
5522 */
5523 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5524 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5525 status_t res = metadata.erase(trigger.metadataTag);
5526
5527 if (res != OK) {
5528 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5529 ", trigger value %d", __FUNCTION__,
5530 trigger.getTagName(), trigger.entryValue);
5531 return res;
5532 }
5533 }
5534 mTriggerRemovedMap.clear();
5535
5536 return OK;
5537}
5538
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005539status_t Camera3Device::RequestThread::addDummyTriggerIds(
5540 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005541 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005542 static const int32_t dummyTriggerId = 1;
5543 status_t res;
5544
Emilian Peevaebbe412018-01-15 13:53:24 +00005545 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005546
5547 // If AF trigger is active, insert a dummy AF trigger ID if none already
5548 // exists
5549 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5550 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5551 if (afTrigger.count > 0 &&
5552 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5553 afId.count == 0) {
5554 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5555 if (res != OK) return res;
5556 }
5557
5558 // If AE precapture trigger is active, insert a dummy precapture trigger ID
5559 // if none already exists
5560 camera_metadata_entry pcTrigger =
5561 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5562 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5563 if (pcTrigger.count > 0 &&
5564 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5565 pcId.count == 0) {
5566 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5567 &dummyTriggerId, 1);
5568 if (res != OK) return res;
5569 }
5570
5571 return OK;
5572}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005573
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005574/**
5575 * PreparerThread inner class methods
5576 */
5577
5578Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005579 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005580 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005581}
5582
5583Camera3Device::PreparerThread::~PreparerThread() {
5584 Thread::requestExitAndWait();
5585 if (mCurrentStream != nullptr) {
5586 mCurrentStream->cancelPrepare();
5587 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5588 mCurrentStream.clear();
5589 }
5590 clear();
5591}
5592
Ruben Brunkc78ac262015-08-13 17:58:46 -07005593status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005594 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005595 status_t res;
5596
5597 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005598 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005599
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005600 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005601 if (res == OK) {
5602 // No preparation needed, fire listener right off
5603 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005604 if (listener != NULL) {
5605 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005606 }
5607 return OK;
5608 } else if (res != NOT_ENOUGH_DATA) {
5609 return res;
5610 }
5611
5612 // Need to prepare, start up thread if necessary
5613 if (!mActive) {
5614 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5615 // isn't running
5616 Thread::requestExitAndWait();
5617 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5618 if (res != OK) {
5619 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005620 if (listener != NULL) {
5621 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005622 }
5623 return res;
5624 }
5625 mCancelNow = false;
5626 mActive = true;
5627 ALOGV("%s: Preparer stream started", __FUNCTION__);
5628 }
5629
5630 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005631 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005632 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5633
5634 return OK;
5635}
5636
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005637void Camera3Device::PreparerThread::pause() {
5638 ATRACE_CALL();
5639
5640 Mutex::Autolock l(mLock);
5641
5642 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5643 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5644 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5645 int currentMaxCount = mCurrentMaxCount;
5646 mPendingStreams.clear();
5647 mCancelNow = true;
5648 while (mActive) {
5649 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5650 if (res == TIMED_OUT) {
5651 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5652 return;
5653 } else if (res != OK) {
5654 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5655 return;
5656 }
5657 }
5658
5659 //Check whether the prepare thread was able to complete the current
5660 //stream. In case work is still pending emplace it along with the rest
5661 //of the streams in the pending list.
5662 if (currentStream != nullptr) {
5663 if (!mCurrentPrepareComplete) {
5664 pendingStreams.emplace(currentMaxCount, currentStream);
5665 }
5666 }
5667
5668 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5669 for (const auto& it : mPendingStreams) {
5670 it.second->cancelPrepare();
5671 }
5672}
5673
5674status_t Camera3Device::PreparerThread::resume() {
5675 ATRACE_CALL();
5676 status_t res;
5677
5678 Mutex::Autolock l(mLock);
5679 sp<NotificationListener> listener = mListener.promote();
5680
5681 if (mActive) {
5682 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5683 return NO_INIT;
5684 }
5685
5686 auto it = mPendingStreams.begin();
5687 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005688 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005689 if (res == OK) {
5690 if (listener != NULL) {
5691 listener->notifyPrepared(it->second->getId());
5692 }
5693 it = mPendingStreams.erase(it);
5694 } else if (res != NOT_ENOUGH_DATA) {
5695 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5696 res, strerror(-res));
5697 it = mPendingStreams.erase(it);
5698 } else {
5699 it++;
5700 }
5701 }
5702
5703 if (mPendingStreams.empty()) {
5704 return OK;
5705 }
5706
5707 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5708 if (res != OK) {
5709 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5710 __FUNCTION__, res, strerror(-res));
5711 return res;
5712 }
5713 mCancelNow = false;
5714 mActive = true;
5715 ALOGV("%s: Preparer stream started", __FUNCTION__);
5716
5717 return OK;
5718}
5719
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005720status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005721 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005722 Mutex::Autolock l(mLock);
5723
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005724 for (const auto& it : mPendingStreams) {
5725 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005726 }
5727 mPendingStreams.clear();
5728 mCancelNow = true;
5729
5730 return OK;
5731}
5732
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005733void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005734 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005735 Mutex::Autolock l(mLock);
5736 mListener = listener;
5737}
5738
5739bool Camera3Device::PreparerThread::threadLoop() {
5740 status_t res;
5741 {
5742 Mutex::Autolock l(mLock);
5743 if (mCurrentStream == nullptr) {
5744 // End thread if done with work
5745 if (mPendingStreams.empty()) {
5746 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5747 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5748 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5749 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005750 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005751 return false;
5752 }
5753
5754 // Get next stream to prepare
5755 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005756 mCurrentStream = it->second;
5757 mCurrentMaxCount = it->first;
5758 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005759 mPendingStreams.erase(it);
5760 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5761 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5762 } else if (mCancelNow) {
5763 mCurrentStream->cancelPrepare();
5764 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5765 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5766 mCurrentStream.clear();
5767 mCancelNow = false;
5768 return true;
5769 }
5770 }
5771
5772 res = mCurrentStream->prepareNextBuffer();
5773 if (res == NOT_ENOUGH_DATA) return true;
5774 if (res != OK) {
5775 // Something bad happened; try to recover by cancelling prepare and
5776 // signalling listener anyway
5777 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5778 mCurrentStream->getId(), res, strerror(-res));
5779 mCurrentStream->cancelPrepare();
5780 }
5781
5782 // This stream has finished, notify listener
5783 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005784 sp<NotificationListener> listener = mListener.promote();
5785 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005786 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5787 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005788 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005789 }
5790
5791 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5792 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005793 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005794
5795 return true;
5796}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005797
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005798/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005799 * Static callback forwarding methods from HAL to instance
5800 */
5801
5802void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5803 const camera3_capture_result *result) {
5804 Camera3Device *d =
5805 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07005806
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005807 d->processCaptureResult(result);
5808}
5809
5810void Camera3Device::sNotify(const camera3_callback_ops *cb,
5811 const camera3_notify_msg *msg) {
5812 Camera3Device *d =
5813 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5814 d->notify(msg);
5815}
5816
5817}; // namespace android