blob: 79685ce8482719b117603758b039a55299ce3014 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070054#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070055#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080059using namespace android::hardware::camera;
60using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62namespace android {
63
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080065 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080066 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070067 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070068 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070069 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070070 mUsePartialResult(false),
71 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080072 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070073 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070074 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070076 mNextReprocessShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000077 mListener(NULL),
78 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080079{
80 ATRACE_CALL();
81 camera3_callback_ops::notify = &sNotify;
82 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080083 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084}
85
86Camera3Device::~Camera3Device()
87{
88 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080089 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 disconnect();
91}
92
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080093const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080094 return mId;
95}
96
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080097/**
98 * CameraDeviceBase interface
99 */
100
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800101status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102{
103 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700104 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800105 Mutex::Autolock l(mLock);
106
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800107 ALOGV("%s: Initializing device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800108 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 return INVALID_OPERATION;
111 }
112
113 /** Open HAL device */
114
115 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116
117 camera3_device_t *device;
118
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800119 ATRACE_BEGIN("CameraHal::open");
120 res = module->open(mId.string(),
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800121 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800122 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123
124 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126 return res;
127 }
128
129 /** Cross-check device version */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800130 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700132 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800133 CAMERA_DEVICE_API_VERSION_3_2,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 device->common.version);
135 device->common.close(&device->common);
136 return BAD_VALUE;
137 }
138
139 camera_info info;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800140 res = module->getCameraInfo(atoi(mId), &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 if (res != OK) return res;
142
143 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700144 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
145 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700146 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 device->common.close(&device->common);
148 return BAD_VALUE;
149 }
150
151 /** Initialize device with callback functions */
152
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800153 ATRACE_BEGIN("CameraHal::initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700155 ATRACE_END();
156
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700158 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
159 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800160 device->common.close(&device->common);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800161 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 }
163
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800164 /** Everything is good to go */
165
166 mDeviceVersion = device->common.version;
167 mDeviceInfo = info.static_camera_characteristics;
168 mInterface = std::make_unique<HalInterface>(device);
169
170 return initializeCommonLocked();
171}
172
173status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
174 ATRACE_CALL();
175 Mutex::Autolock il(mInterfaceLock);
176 Mutex::Autolock l(mLock);
177
178 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
179 if (mStatus != STATUS_UNINITIALIZED) {
180 CLOGE("Already initialized!");
181 return INVALID_OPERATION;
182 }
183 if (manager == nullptr) return INVALID_OPERATION;
184
185 sp<ICameraDeviceSession> session;
186 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800187 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800188 /*out*/ &session);
189 ATRACE_END();
190 if (res != OK) {
191 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
192 return res;
193 }
194
Steven Moreland5ff9c912017-03-09 23:13:00 -0800195 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800196 if (res != OK) {
197 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
198 session->close();
199 return res;
200 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800201
Yifan Hongf79b5542017-04-11 14:44:25 -0700202 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700203 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
204 [&queue](const auto& descriptor) {
205 queue = std::make_shared<RequestMetadataQueue>(descriptor);
206 if (!queue->isValid() || queue->availableToWrite() <= 0) {
207 ALOGE("HAL returns empty request metadata fmq, not use it");
208 queue = nullptr;
209 // don't use the queue onwards.
210 }
211 });
212 if (!requestQueueRet.isOk()) {
213 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
214 requestQueueRet.description().c_str());
Yifan Hongf79b5542017-04-11 14:44:25 -0700215 queue = nullptr;
216 // Don't use the queue onwards.
217 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700218 auto resultQueueRet = session->getCaptureResultMetadataQueue(
219 [&queue = mResultMetadataQueue](const auto& descriptor) {
220 queue = std::make_unique<ResultMetadataQueue>(descriptor);
221 if (!queue->isValid() || queue->availableToWrite() <= 0) {
222 ALOGE("HAL returns empty result metadata fmq, not use it");
223 queue = nullptr;
224 // Don't use the queue onwards.
225 }
226 });
227 if (!resultQueueRet.isOk()) {
228 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
229 resultQueueRet.description().c_str());
230 mResultMetadataQueue = nullptr;
231 // Don't use the queue onwards.
232 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700233
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800234 // TODO: camera service will absorb 3_2/3_3/3_4 differences in the future
235 // for now use 3_4 to keep legacy devices working
236 mDeviceVersion = CAMERA_DEVICE_API_VERSION_3_4;
Yifan Hongf79b5542017-04-11 14:44:25 -0700237 mInterface = std::make_unique<HalInterface>(session, queue);
Emilian Peev71c73a22017-03-21 16:35:51 +0000238 std::string providerType;
239 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800240
241 return initializeCommonLocked();
242}
243
244status_t Camera3Device::initializeCommonLocked() {
245
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700246 /** Start up status tracker thread */
247 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800248 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 if (res != OK) {
250 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
251 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800252 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 mStatusTracker.clear();
254 return res;
255 }
256
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700257 /** Register in-flight map to the status tracker */
258 mInFlightStatusId = mStatusTracker->addComponent();
259
Zhijun He125684a2015-12-26 15:07:30 -0800260 /** Create buffer manager */
261 mBufferManager = new Camera3BufferManager();
262
Emilian Peev71c73a22017-03-21 16:35:51 +0000263 mTagMonitor.initialize(mVendorTagId);
264
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700265 bool aeLockAvailable = false;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800266 camera_metadata_entry aeLockAvailableEntry = mDeviceInfo.find(
267 ANDROID_CONTROL_AE_LOCK_AVAILABLE);
268 if (aeLockAvailableEntry.count > 0) {
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700269 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
270 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
271 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800272
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700273 /** Start up request queue thread */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800274 mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get(), mDeviceVersion,
275 aeLockAvailable);
276 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800277 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700278 SET_ERR_L("Unable to start request queue thread: %s (%d)",
279 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800280 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800282 return res;
283 }
284
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700285 mPreparerThread = new PreparerThread();
286
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700287 // Determine whether we need to derive sensitivity boost values for older devices.
288 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
289 // be listed (as the default value 100)
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800290 if (mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700291 mDerivePostRawSensKey = true;
292 }
293
Ruben Brunk183f0562015-08-12 12:55:02 -0700294 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800295 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700296 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700297 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700298 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800299
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800300 // Measure the clock domain offset between camera and video/hw_composer
301 camera_metadata_entry timestampSource =
302 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
303 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
304 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
305 mTimestampOffset = getMonoToBoottimeOffset();
306 }
307
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700308 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100309 camera_metadata_entry partialResultsCount =
310 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
311 if (partialResultsCount.count > 0) {
312 mNumPartialResults = partialResultsCount.data.i32[0];
313 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700314 }
315
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700316 camera_metadata_entry configs =
317 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
318 for (uint32_t i = 0; i < configs.count; i += 4) {
319 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
320 configs.data.i32[i + 3] ==
321 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
322 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
323 configs.data.i32[i + 2]));
324 }
325 }
326
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800327 return OK;
328}
329
330status_t Camera3Device::disconnect() {
331 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700332 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800333
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700334 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800335
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700336 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800337
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700338 {
339 Mutex::Autolock l(mLock);
340 if (mStatus == STATUS_UNINITIALIZED) return res;
341
342 if (mStatus == STATUS_ACTIVE ||
343 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
344 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700345 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700346 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700347 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700348 } else {
349 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
350 if (res != OK) {
351 SET_ERR_L("Timeout waiting for HAL to drain");
352 // Continue to close device even in case of error
353 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700354 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800355 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800356
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700357 if (mStatus == STATUS_ERROR) {
358 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700359 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700360
361 if (mStatusTracker != NULL) {
362 mStatusTracker->requestExit();
363 }
364
365 if (mRequestThread != NULL) {
366 mRequestThread->requestExit();
367 }
368
369 mOutputStreams.clear();
370 mInputStream.clear();
371 }
372
373 // Joining done without holding mLock, otherwise deadlocks may ensue
374 // as the threads try to access parent state
375 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
376 // HAL may be in a bad state, so waiting for request thread
377 // (which may be stuck in the HAL processCaptureRequest call)
378 // could be dangerous.
379 mRequestThread->join();
380 }
381
382 if (mStatusTracker != NULL) {
383 mStatusTracker->join();
384 }
385
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800386 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700387 {
388 Mutex::Autolock l(mLock);
389
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800390 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700391 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800392 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800393
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800394 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700395 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700397 // Call close without internal mutex held, as the HAL close may need to
398 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800399 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700400
401 {
402 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800403 mInterface->clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700404 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700405 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700407 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700408 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800409}
410
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700411// For dumping/debugging only -
412// try to acquire a lock a few times, eventually give up to proceed with
413// debug/dump operations
414bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
415 bool gotLock = false;
416 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
417 if (lock.tryLock() == NO_ERROR) {
418 gotLock = true;
419 break;
420 } else {
421 usleep(kDumpSleepDuration);
422 }
423 }
424 return gotLock;
425}
426
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700427Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
428 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100429 const int STREAM_CONFIGURATION_SIZE = 4;
430 const int STREAM_FORMAT_OFFSET = 0;
431 const int STREAM_WIDTH_OFFSET = 1;
432 const int STREAM_HEIGHT_OFFSET = 2;
433 const int STREAM_IS_INPUT_OFFSET = 3;
434 camera_metadata_ro_entry_t availableStreamConfigs =
435 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
436 if (availableStreamConfigs.count == 0 ||
437 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
438 return Size(0, 0);
439 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700440
Emilian Peev08dd2452017-04-06 16:55:14 +0100441 // Get max jpeg size (area-wise).
442 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
443 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
444 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
445 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
446 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
447 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
448 && format == HAL_PIXEL_FORMAT_BLOB &&
449 (width * height > maxJpegWidth * maxJpegHeight)) {
450 maxJpegWidth = width;
451 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700452 }
453 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100454
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700455 return Size(maxJpegWidth, maxJpegHeight);
456}
457
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800458nsecs_t Camera3Device::getMonoToBoottimeOffset() {
459 // try three times to get the clock offset, choose the one
460 // with the minimum gap in measurements.
461 const int tries = 3;
462 nsecs_t bestGap, measured;
463 for (int i = 0; i < tries; ++i) {
464 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
465 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
466 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
467 const nsecs_t gap = tmono2 - tmono;
468 if (i == 0 || gap < bestGap) {
469 bestGap = gap;
470 measured = tbase - ((tmono + tmono2) >> 1);
471 }
472 }
473 return measured;
474}
475
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800476hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
477 int frameworkFormat) {
478 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
479}
480
481DataspaceFlags Camera3Device::mapToHidlDataspace(
482 android_dataspace dataSpace) {
483 return dataSpace;
484}
485
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700486BufferUsageFlags Camera3Device::mapToConsumerUsage(
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700487 uint32_t usage) {
488 return usage;
489}
490
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800491StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
492 switch (rotation) {
493 case CAMERA3_STREAM_ROTATION_0:
494 return StreamRotation::ROTATION_0;
495 case CAMERA3_STREAM_ROTATION_90:
496 return StreamRotation::ROTATION_90;
497 case CAMERA3_STREAM_ROTATION_180:
498 return StreamRotation::ROTATION_180;
499 case CAMERA3_STREAM_ROTATION_270:
500 return StreamRotation::ROTATION_270;
501 }
502 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
503 return StreamRotation::ROTATION_0;
504}
505
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800506status_t Camera3Device::mapToStreamConfigurationMode(
507 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
508 if (mode == nullptr) return BAD_VALUE;
509 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
510 switch(operationMode) {
511 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
512 *mode = StreamConfigurationMode::NORMAL_MODE;
513 break;
514 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
515 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
516 break;
517 default:
518 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
519 return BAD_VALUE;
520 }
521 } else {
522 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800523 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800524 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800525}
526
527camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
528 switch (status) {
529 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
530 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
531 }
532 return CAMERA3_BUFFER_STATUS_ERROR;
533}
534
535int Camera3Device::mapToFrameworkFormat(
536 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
537 return static_cast<uint32_t>(pixelFormat);
538}
539
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700540uint32_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700541 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700542 return usage;
543}
544
545uint32_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700546 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700547 return usage;
548}
549
Zhijun Hef7da0962014-04-24 13:27:56 -0700550ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700551 // Get max jpeg size (area-wise).
552 Size maxJpegResolution = getMaxJpegResolution();
553 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800554 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
555 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700556 return BAD_VALUE;
557 }
558
Zhijun Hef7da0962014-04-24 13:27:56 -0700559 // Get max jpeg buffer size
560 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700561 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
562 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800563 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
564 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700565 return BAD_VALUE;
566 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700567 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800568 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700569
570 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700571 float scaleFactor = ((float) (width * height)) /
572 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800573 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
574 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700575 if (jpegBufferSize > maxJpegBufferSize) {
576 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700577 }
578
579 return jpegBufferSize;
580}
581
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700582ssize_t Camera3Device::getPointCloudBufferSize() const {
583 const int FLOATS_PER_POINT=4;
584 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
585 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800586 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
587 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700588 return BAD_VALUE;
589 }
590 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
591 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
592 return maxBytesForPointCloud;
593}
594
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800595ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800596 const int PER_CONFIGURATION_SIZE = 3;
597 const int WIDTH_OFFSET = 0;
598 const int HEIGHT_OFFSET = 1;
599 const int SIZE_OFFSET = 2;
600 camera_metadata_ro_entry rawOpaqueSizes =
601 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800602 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800603 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800604 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
605 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800606 return BAD_VALUE;
607 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700608
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800609 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
610 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
611 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
612 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
613 }
614 }
615
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800616 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
617 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800618 return BAD_VALUE;
619}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700620
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800621status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
622 ATRACE_CALL();
623 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700624
625 // Try to lock, but continue in case of failure (to avoid blocking in
626 // deadlocks)
627 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
628 bool gotLock = tryLockSpinRightRound(mLock);
629
630 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800631 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
632 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700633 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800634 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
635 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700636
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800637 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700638
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800639 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700640 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800641 int n = args.size();
642 for (int i = 0; i < n; i++) {
643 if (args[i] == templatesOption) {
644 dumpTemplates = true;
645 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700646 if (args[i] == monitorOption) {
647 if (i + 1 < n) {
648 String8 monitorTags = String8(args[i + 1]);
649 if (monitorTags == "off") {
650 mTagMonitor.disableMonitoring();
651 } else {
652 mTagMonitor.parseTagsToMonitor(monitorTags);
653 }
654 } else {
655 mTagMonitor.disableMonitoring();
656 }
657 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800658 }
659
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800660 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800661
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 const char *status =
663 mStatus == STATUS_ERROR ? "ERROR" :
664 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
666 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 mStatus == STATUS_ACTIVE ? "ACTIVE" :
668 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800670 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700671 if (mStatus == STATUS_ERROR) {
672 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
673 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800675 const char *mode =
676 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
677 mOperatingMode == static_cast<int>(
678 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
679 "CUSTOM";
680 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681
682 if (mInputStream != NULL) {
683 write(fd, lines.string(), lines.size());
684 mInputStream->dump(fd, args);
685 } else {
686 lines.appendFormat(" No input stream.\n");
687 write(fd, lines.string(), lines.size());
688 }
689 for (size_t i = 0; i < mOutputStreams.size(); i++) {
690 mOutputStreams[i]->dump(fd,args);
691 }
692
Zhijun He431503c2016-03-07 17:30:16 -0800693 if (mBufferManager != NULL) {
694 lines = String8(" Camera3 Buffer Manager:\n");
695 write(fd, lines.string(), lines.size());
696 mBufferManager->dump(fd, args);
697 }
Zhijun He125684a2015-12-26 15:07:30 -0800698
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700699 lines = String8(" In-flight requests:\n");
700 if (mInFlightMap.size() == 0) {
701 lines.append(" None\n");
702 } else {
703 for (size_t i = 0; i < mInFlightMap.size(); i++) {
704 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700705 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700706 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800707 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700708 r.numBuffersLeft);
709 }
710 }
711 write(fd, lines.string(), lines.size());
712
Igor Murashkin1e479c02013-09-06 16:55:14 -0700713 {
714 lines = String8(" Last request sent:\n");
715 write(fd, lines.string(), lines.size());
716
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700718 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
719 }
720
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800721 if (dumpTemplates) {
722 const char *templateNames[] = {
723 "TEMPLATE_PREVIEW",
724 "TEMPLATE_STILL_CAPTURE",
725 "TEMPLATE_VIDEO_RECORD",
726 "TEMPLATE_VIDEO_SNAPSHOT",
727 "TEMPLATE_ZERO_SHUTTER_LAG",
728 "TEMPLATE_MANUAL"
729 };
730
731 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800732 camera_metadata_t *templateRequest = nullptr;
733 mInterface->constructDefaultRequestSettings(
734 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800735 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800736 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800737 lines.append(" Not supported\n");
738 write(fd, lines.string(), lines.size());
739 } else {
740 write(fd, lines.string(), lines.size());
741 dump_indented_camera_metadata(templateRequest,
742 fd, /*verbosity*/2, /*indentation*/8);
743 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800744 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800745 }
746 }
747
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700748 mTagMonitor.dumpMonitoredMetadata(fd);
749
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800750 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800751 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800752 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800753 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800754 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800755
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 if (gotLock) mLock.unlock();
757 if (gotInterfaceLock) mInterfaceLock.unlock();
758
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759 return OK;
760}
761
762const CameraMetadata& Camera3Device::info() const {
763 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
765 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 mStatus == STATUS_ERROR ?
768 "when in error state" : "before init");
769 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800770 return mDeviceInfo;
771}
772
Jianing Wei90e59c92014-03-12 18:29:36 -0700773status_t Camera3Device::checkStatusOkToCaptureLocked() {
774 switch (mStatus) {
775 case STATUS_ERROR:
776 CLOGE("Device has encountered a serious error");
777 return INVALID_OPERATION;
778 case STATUS_UNINITIALIZED:
779 CLOGE("Device not initialized");
780 return INVALID_OPERATION;
781 case STATUS_UNCONFIGURED:
782 case STATUS_CONFIGURED:
783 case STATUS_ACTIVE:
784 // OK
785 break;
786 default:
787 SET_ERR_L("Unexpected status: %d", mStatus);
788 return INVALID_OPERATION;
789 }
790 return OK;
791}
792
793status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700794 const List<const CameraMetadata> &metadataList,
795 const std::list<const SurfaceMap> &surfaceMaps,
796 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700797 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700798 if (requestList == NULL) {
799 CLOGE("requestList cannot be NULL.");
800 return BAD_VALUE;
801 }
802
Jianing Weicb0652e2014-03-12 18:29:36 -0700803 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700804 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
805 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
806 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
807 ++metadataIt, ++surfaceMapIt) {
808 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700809 if (newRequest == 0) {
810 CLOGE("Can't create capture request");
811 return BAD_VALUE;
812 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700813
Shuzhen Wang9d066012016-09-30 11:30:20 -0700814 newRequest->mRepeating = repeating;
815
Jianing Weicb0652e2014-03-12 18:29:36 -0700816 // Setup burst Id and request Id
817 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700818 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
819 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700820 CLOGE("RequestID entry exists; but must not be empty in metadata");
821 return BAD_VALUE;
822 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700823 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700824 } else {
825 CLOGE("RequestID does not exist in metadata");
826 return BAD_VALUE;
827 }
828
Jianing Wei90e59c92014-03-12 18:29:36 -0700829 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700830
831 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700832 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700833 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
834 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
835 return BAD_VALUE;
836 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700837
838 // Setup batch size if this is a high speed video recording request.
839 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
840 auto firstRequest = requestList->begin();
841 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
842 if (outputStream->isVideoStream()) {
843 (*firstRequest)->mBatchSize = requestList->size();
844 break;
845 }
846 }
847 }
848
Jianing Wei90e59c92014-03-12 18:29:36 -0700849 return OK;
850}
851
Jianing Weicb0652e2014-03-12 18:29:36 -0700852status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800854
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700855 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700856 std::list<const SurfaceMap> surfaceMaps;
857 convertToRequestList(requests, surfaceMaps, request);
858
859 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
860}
861
862void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
863 std::list<const SurfaceMap>& surfaceMaps,
864 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700865 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700866
867 SurfaceMap surfaceMap;
868 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
869 // With no surface list passed in, stream and surface will have 1-to-1
870 // mapping. So the surface index is 0 for each stream in the surfaceMap.
871 for (size_t i = 0; i < streams.count; i++) {
872 surfaceMap[streams.data.i32[i]].push_back(0);
873 }
874 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800875}
876
Jianing Wei90e59c92014-03-12 18:29:36 -0700877status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700878 const List<const CameraMetadata> &requests,
879 const std::list<const SurfaceMap> &surfaceMaps,
880 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700881 /*out*/
882 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700883 ATRACE_CALL();
884 Mutex::Autolock il(mInterfaceLock);
885 Mutex::Autolock l(mLock);
886
887 status_t res = checkStatusOkToCaptureLocked();
888 if (res != OK) {
889 // error logged by previous call
890 return res;
891 }
892
893 RequestList requestList;
894
Shuzhen Wang0129d522016-10-30 22:43:41 -0700895 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
896 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700897 if (res != OK) {
898 // error logged by previous call
899 return res;
900 }
901
902 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700903 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700904 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700905 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700906 }
907
908 if (res == OK) {
909 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
910 if (res != OK) {
911 SET_ERR_L("Can't transition to active in %f seconds!",
912 kActiveTimeout/1e9);
913 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800914 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700915 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700916 } else {
917 CLOGE("Cannot queue request. Impossible.");
918 return BAD_VALUE;
919 }
920
921 return res;
922}
923
Yifan Honga640c5a2017-04-12 16:30:31 -0700924// Only one processCaptureResult should be called at a time, so
925// the locks won't block. The locks are present here simply to enforce this.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800926hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800927 const hardware::hidl_vec<
928 hardware::camera::device::V3_2::CaptureResult>& results) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700929
930 if (mProcessCaptureResultLock.tryLock() != OK) {
931 // This should never happen; it indicates a wrong client implementation
932 // that doesn't follow the contract. But, we can be tolerant here.
933 ALOGE("%s: callback overlapped! waiting 1s...",
934 __FUNCTION__);
935 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
936 ALOGE("%s: cannot acquire lock in 1s, dropping results",
937 __FUNCTION__);
938 // really don't know what to do, so bail out.
939 return hardware::Void();
940 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800941 }
Yifan Honga640c5a2017-04-12 16:30:31 -0700942 for (const auto& result : results) {
943 processOneCaptureResultLocked(result);
944 }
945 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800946 return hardware::Void();
947}
948
Yifan Honga640c5a2017-04-12 16:30:31 -0700949void Camera3Device::processOneCaptureResultLocked(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800950 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800951 camera3_capture_result r;
952 status_t res;
953 r.frame_number = result.frameNumber;
Yifan Honga640c5a2017-04-12 16:30:31 -0700954
955 hardware::camera::device::V3_2::CameraMetadata resultMetadata;
956 if (result.fmqResultSize > 0) {
957 resultMetadata.resize(result.fmqResultSize);
958 if (mResultMetadataQueue == nullptr) {
959 return; // logged in initialize()
960 }
961 if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
962 ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
963 __FUNCTION__, result.frameNumber, result.fmqResultSize);
964 return;
965 }
966 } else {
967 resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
968 result.result.size());
969 }
970
971 if (resultMetadata.size() != 0) {
972 r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
973 size_t expected_metadata_size = resultMetadata.size();
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800974 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
975 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
976 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800977 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800978 }
979 } else {
980 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800981 }
982
983 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
984 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
985 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
986 auto& bDst = outputBuffers[i];
987 const StreamBuffer &bSrc = result.outputBuffers[i];
988
989 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100990 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800991 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
992 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800993 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800994 }
995 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
996
997 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800998 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800999 if (res != OK) {
1000 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
1001 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001002 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001003 }
1004 bDst.buffer = buffer;
1005 bDst.status = mapHidlBufferStatus(bSrc.status);
1006 bDst.acquire_fence = -1;
1007 if (bSrc.releaseFence == nullptr) {
1008 bDst.release_fence = -1;
1009 } else if (bSrc.releaseFence->numFds == 1) {
1010 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
1011 } else {
1012 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
1013 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001014 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001015 }
1016 }
1017 r.num_output_buffers = outputBuffers.size();
1018 r.output_buffers = outputBuffers.data();
1019
1020 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08001021 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001022 r.input_buffer = nullptr;
1023 } else {
1024 if (mInputStream->getId() != result.inputBuffer.streamId) {
1025 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
1026 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001027 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001028 }
1029 inputBuffer.stream = mInputStream->asHalStream();
1030 buffer_handle_t *buffer;
1031 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
1032 &buffer);
1033 if (res != OK) {
1034 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1035 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001036 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001037 }
1038 inputBuffer.buffer = buffer;
1039 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1040 inputBuffer.acquire_fence = -1;
1041 if (result.inputBuffer.releaseFence == nullptr) {
1042 inputBuffer.release_fence = -1;
1043 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1044 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1045 } else {
1046 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1047 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001048 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001049 }
1050 r.input_buffer = &inputBuffer;
1051 }
1052
1053 r.partial_result = result.partialResult;
1054
1055 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001056}
1057
1058hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001059 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
1060 for (const auto& msg : msgs) {
1061 notify(msg);
1062 }
1063 return hardware::Void();
1064}
1065
1066void Camera3Device::notify(
1067 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1068
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001069 camera3_notify_msg m;
1070 switch (msg.type) {
1071 case MsgType::ERROR:
1072 m.type = CAMERA3_MSG_ERROR;
1073 m.message.error.frame_number = msg.msg.error.frameNumber;
1074 if (msg.msg.error.errorStreamId >= 0) {
1075 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001076 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001077 ALOGE("%s: Frame %d: Invalid error stream id %d",
1078 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001079 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001080 }
1081 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1082 } else {
1083 m.message.error.error_stream = nullptr;
1084 }
1085 switch (msg.msg.error.errorCode) {
1086 case ErrorCode::ERROR_DEVICE:
1087 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1088 break;
1089 case ErrorCode::ERROR_REQUEST:
1090 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1091 break;
1092 case ErrorCode::ERROR_RESULT:
1093 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1094 break;
1095 case ErrorCode::ERROR_BUFFER:
1096 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1097 break;
1098 }
1099 break;
1100 case MsgType::SHUTTER:
1101 m.type = CAMERA3_MSG_SHUTTER;
1102 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1103 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1104 break;
1105 }
1106 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001107}
1108
Jianing Weicb0652e2014-03-12 18:29:36 -07001109status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001110 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001111 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001112 ATRACE_CALL();
1113
Shuzhen Wang0129d522016-10-30 22:43:41 -07001114 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001115}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116
Jianing Weicb0652e2014-03-12 18:29:36 -07001117status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1118 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001120
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001121 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001122 std::list<const SurfaceMap> surfaceMaps;
1123 convertToRequestList(requests, surfaceMaps, request);
1124
1125 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1126 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001127}
1128
Jianing Weicb0652e2014-03-12 18:29:36 -07001129status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001130 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001131 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001132 ATRACE_CALL();
1133
Shuzhen Wang0129d522016-10-30 22:43:41 -07001134 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001135}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136
1137sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001138 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001139 status_t res;
1140
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001141 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001142 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1143 // so unilaterally select normal operating mode.
1144 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001145 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001147 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001148 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001149 } else if (mStatus == STATUS_UNCONFIGURED) {
1150 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001151 CLOGE("No streams configured");
1152 return NULL;
1153 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001154 }
1155
Shuzhen Wang0129d522016-10-30 22:43:41 -07001156 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001157 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001158}
1159
Jianing Weicb0652e2014-03-12 18:29:36 -07001160status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001161 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001162 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001165 switch (mStatus) {
1166 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 return INVALID_OPERATION;
1169 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001170 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001171 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001172 case STATUS_UNCONFIGURED:
1173 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 case STATUS_ACTIVE:
1175 // OK
1176 break;
1177 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return INVALID_OPERATION;
1180 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001181 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001182
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001183 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001184}
1185
1186status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1187 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001189
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001190 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191}
1192
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001193status_t Camera3Device::createInputStream(
1194 uint32_t width, uint32_t height, int format, int *id) {
1195 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001197 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001198 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1199 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001200
1201 status_t res;
1202 bool wasActive = false;
1203
1204 switch (mStatus) {
1205 case STATUS_ERROR:
1206 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1207 return INVALID_OPERATION;
1208 case STATUS_UNINITIALIZED:
1209 ALOGE("%s: Device not initialized", __FUNCTION__);
1210 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001211 case STATUS_UNCONFIGURED:
1212 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001213 // OK
1214 break;
1215 case STATUS_ACTIVE:
1216 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001218 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001219 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001220 return res;
1221 }
1222 wasActive = true;
1223 break;
1224 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001225 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001226 return INVALID_OPERATION;
1227 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001228 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001229
1230 if (mInputStream != 0) {
1231 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1232 return INVALID_OPERATION;
1233 }
1234
1235 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1236 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001237 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001238
1239 mInputStream = newStream;
1240
1241 *id = mNextStreamId++;
1242
1243 // Continue captures if active at start
1244 if (wasActive) {
1245 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001246 // Reuse current operating mode for new stream config
1247 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001248 if (res != OK) {
1249 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1250 __FUNCTION__, mNextStreamId, strerror(-res), res);
1251 return res;
1252 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001253 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001254 }
1255
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001256 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001257 return OK;
1258}
1259
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001260status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001261 uint32_t width, uint32_t height, int format,
1262 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001263 int streamSetId, bool isShared, uint32_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001264 ATRACE_CALL();
1265
1266 if (consumer == nullptr) {
1267 ALOGE("%s: consumer must not be null", __FUNCTION__);
1268 return BAD_VALUE;
1269 }
1270
1271 std::vector<sp<Surface>> consumers;
1272 consumers.push_back(consumer);
1273
1274 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001275 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001276}
1277
1278status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1279 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1280 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001281 int streamSetId, bool isShared, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001282 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001285 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang758c2152017-01-10 18:26:18 -08001286 " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format,
1287 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001289 status_t res;
1290 bool wasActive = false;
1291
1292 switch (mStatus) {
1293 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001294 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001295 return INVALID_OPERATION;
1296 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001297 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001298 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001299 case STATUS_UNCONFIGURED:
1300 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301 // OK
1302 break;
1303 case STATUS_ACTIVE:
1304 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001305 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001307 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001308 return res;
1309 }
1310 wasActive = true;
1311 break;
1312 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001313 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314 return INVALID_OPERATION;
1315 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001316 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001317
1318 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001319
Shuzhen Wang0129d522016-10-30 22:43:41 -07001320 if (consumers.size() == 0 && !hasDeferredConsumer) {
1321 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1322 return BAD_VALUE;
1323 }
Zhijun He5d677d12016-05-29 16:52:39 -07001324
Shuzhen Wang0129d522016-10-30 22:43:41 -07001325 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001326 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1327 return BAD_VALUE;
1328 }
1329
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001331 ssize_t blobBufferSize;
1332 if (dataSpace != HAL_DATASPACE_DEPTH) {
1333 blobBufferSize = getJpegBufferSize(width, height);
1334 if (blobBufferSize <= 0) {
1335 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1336 return BAD_VALUE;
1337 }
1338 } else {
1339 blobBufferSize = getPointCloudBufferSize();
1340 if (blobBufferSize <= 0) {
1341 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1342 return BAD_VALUE;
1343 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001344 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001345 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001346 width, height, blobBufferSize, format, dataSpace, rotation,
1347 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001348 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1349 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1350 if (rawOpaqueBufferSize <= 0) {
1351 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1352 return BAD_VALUE;
1353 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001354 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001355 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1356 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001357 } else if (isShared) {
1358 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1359 width, height, format, consumerUsage, dataSpace, rotation,
1360 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001361 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001362 newStream = new Camera3OutputStream(mNextStreamId,
1363 width, height, format, consumerUsage, dataSpace, rotation,
1364 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001365 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001366 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001367 width, height, format, dataSpace, rotation,
1368 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001369 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001370 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371
Emilian Peev08dd2452017-04-06 16:55:14 +01001372 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001373
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 res = mOutputStreams.add(mNextStreamId, newStream);
1375 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001376 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 return res;
1378 }
1379
1380 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001381 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001382
1383 // Continue captures if active at start
1384 if (wasActive) {
1385 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001386 // Reuse current operating mode for new stream config
1387 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1390 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 return res;
1392 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001393 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001395 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001396 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001397}
1398
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001400 uint32_t *width, uint32_t *height,
1401 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001402 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001403 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001405
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 switch (mStatus) {
1407 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001408 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 return INVALID_OPERATION;
1410 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001411 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001413 case STATUS_UNCONFIGURED:
1414 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415 case STATUS_ACTIVE:
1416 // OK
1417 break;
1418 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001419 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001420 return INVALID_OPERATION;
1421 }
1422
1423 ssize_t idx = mOutputStreams.indexOfKey(id);
1424 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001425 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001426 return idx;
1427 }
1428
1429 if (width) *width = mOutputStreams[idx]->getWidth();
1430 if (height) *height = mOutputStreams[idx]->getHeight();
1431 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001432 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001434}
1435
1436status_t Camera3Device::setStreamTransform(int id,
1437 int transform) {
1438 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001439 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001441
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001442 switch (mStatus) {
1443 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001444 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 return INVALID_OPERATION;
1446 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001447 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001449 case STATUS_UNCONFIGURED:
1450 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001451 case STATUS_ACTIVE:
1452 // OK
1453 break;
1454 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001455 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001456 return INVALID_OPERATION;
1457 }
1458
1459 ssize_t idx = mOutputStreams.indexOfKey(id);
1460 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001461 CLOGE("Stream %d does not exist",
1462 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001463 return BAD_VALUE;
1464 }
1465
1466 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001467}
1468
1469status_t Camera3Device::deleteStream(int id) {
1470 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001471 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001472 Mutex::Autolock l(mLock);
1473 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001474
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001475 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001476
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001477 // CameraDevice semantics require device to already be idle before
1478 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001479 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001480 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001481 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 }
1483
Igor Murashkin2fba5842013-04-22 14:03:54 -07001484 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001485 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 if (mInputStream != NULL && id == mInputStream->getId()) {
1487 deletedStream = mInputStream;
1488 mInputStream.clear();
1489 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001490 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001491 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001492 return BAD_VALUE;
1493 }
Zhijun He5f446352014-01-22 09:49:33 -08001494 }
1495
1496 // Delete output stream or the output part of a bi-directional stream.
1497 if (outputStreamIdx != NAME_NOT_FOUND) {
1498 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001499 mOutputStreams.removeItem(id);
1500 }
1501
1502 // Free up the stream endpoint so that it can be used by some other stream
1503 res = deletedStream->disconnect();
1504 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001505 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001506 // fall through since we want to still list the stream as deleted.
1507 }
1508 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001509 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510
1511 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001512}
1513
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001514status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001515 ATRACE_CALL();
1516 ALOGV("%s: E", __FUNCTION__);
1517
1518 Mutex::Autolock il(mInterfaceLock);
1519 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001520
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001521 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001522}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001524status_t Camera3Device::getInputBufferProducer(
1525 sp<IGraphicBufferProducer> *producer) {
1526 Mutex::Autolock il(mInterfaceLock);
1527 Mutex::Autolock l(mLock);
1528
1529 if (producer == NULL) {
1530 return BAD_VALUE;
1531 } else if (mInputStream == NULL) {
1532 return INVALID_OPERATION;
1533 }
1534
1535 return mInputStream->getInputBufferProducer(producer);
1536}
1537
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538status_t Camera3Device::createDefaultRequest(int templateId,
1539 CameraMetadata *request) {
1540 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001541 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001542
1543 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1544 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1545 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1546 return BAD_VALUE;
1547 }
1548
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001549 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001550 Mutex::Autolock l(mLock);
1551
1552 switch (mStatus) {
1553 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001554 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001555 return INVALID_OPERATION;
1556 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001557 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001559 case STATUS_UNCONFIGURED:
1560 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001561 case STATUS_ACTIVE:
1562 // OK
1563 break;
1564 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001565 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566 return INVALID_OPERATION;
1567 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001568
Zhijun Hea1530f12014-09-14 12:44:20 -07001569 if (!mRequestTemplateCache[templateId].isEmpty()) {
1570 *request = mRequestTemplateCache[templateId];
1571 return OK;
1572 }
1573
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001574 camera_metadata_t *rawRequest;
1575 status_t res = mInterface->constructDefaultRequestSettings(
1576 (camera3_request_template_t) templateId, &rawRequest);
1577 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001578 ALOGI("%s: template %d is not supported on this camera device",
1579 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001580 return res;
1581 } else if (res != OK) {
1582 CLOGE("Unable to construct request template %d: %s (%d)",
1583 templateId, strerror(-res), res);
1584 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001585 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001586
Emilian Peev71c73a22017-03-21 16:35:51 +00001587 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001588 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001589
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001590 // Derive some new keys for backward compatibility
1591 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1592 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1593 int32_t defaultBoost[1] = {100};
1594 mRequestTemplateCache[templateId].update(
1595 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1596 defaultBoost, 1);
1597 }
1598
1599 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001600 return OK;
1601}
1602
1603status_t Camera3Device::waitUntilDrained() {
1604 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001605 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001606 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001607
Zhijun He69a37482014-03-23 18:44:49 -07001608 return waitUntilDrainedLocked();
1609}
1610
1611status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001612 switch (mStatus) {
1613 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001614 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615 ALOGV("%s: Already idle", __FUNCTION__);
1616 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617 case STATUS_CONFIGURED:
1618 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 case STATUS_ERROR:
1620 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001621 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001622 break;
1623 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001624 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001625 return INVALID_OPERATION;
1626 }
1627
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001628 ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001629 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001630 if (res != OK) {
1631 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1632 res);
1633 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001634 return res;
1635}
1636
Ruben Brunk183f0562015-08-12 12:55:02 -07001637
1638void Camera3Device::internalUpdateStatusLocked(Status status) {
1639 mStatus = status;
1640 mRecentStatusUpdates.add(mStatus);
1641 mStatusChanged.broadcast();
1642}
1643
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001644// Pause to reconfigure
1645status_t Camera3Device::internalPauseAndWaitLocked() {
1646 mRequestThread->setPaused(true);
1647 mPauseStateNotify = true;
1648
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001649 ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001650 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1651 if (res != OK) {
1652 SET_ERR_L("Can't idle device in %f seconds!",
1653 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001654 }
1655
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 return res;
1657}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001658
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001659// Resume after internalPauseAndWaitLocked
1660status_t Camera3Device::internalResumeLocked() {
1661 status_t res;
1662
1663 mRequestThread->setPaused(false);
1664
1665 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1666 if (res != OK) {
1667 SET_ERR_L("Can't transition to active in %f seconds!",
1668 kActiveTimeout/1e9);
1669 }
1670 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001671 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672}
1673
Ruben Brunk183f0562015-08-12 12:55:02 -07001674status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001675 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001676
1677 size_t startIndex = 0;
1678 if (mStatusWaiters == 0) {
1679 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1680 // this status list
1681 mRecentStatusUpdates.clear();
1682 } else {
1683 // If other threads are waiting on updates to this status list, set the position of the
1684 // first element that this list will check rather than clearing the list.
1685 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001686 }
1687
Ruben Brunk183f0562015-08-12 12:55:02 -07001688 mStatusWaiters++;
1689
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 bool stateSeen = false;
1691 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001692 if (active == (mStatus == STATUS_ACTIVE)) {
1693 // Desired state is current
1694 break;
1695 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001696
1697 res = mStatusChanged.waitRelative(mLock, timeout);
1698 if (res != OK) break;
1699
Ruben Brunk183f0562015-08-12 12:55:02 -07001700 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1701 // transitions.
1702 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1703 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1704 __FUNCTION__);
1705
1706 // Encountered desired state since we began waiting
1707 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001708 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1709 stateSeen = true;
1710 break;
1711 }
1712 }
1713 } while (!stateSeen);
1714
Ruben Brunk183f0562015-08-12 12:55:02 -07001715 mStatusWaiters--;
1716
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001717 return res;
1718}
1719
1720
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001721status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001722 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001723 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001724
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001725 if (listener != NULL && mListener != NULL) {
1726 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1727 }
1728 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001729 mRequestThread->setNotificationListener(listener);
1730 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001731
1732 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001733}
1734
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001735bool Camera3Device::willNotify3A() {
1736 return false;
1737}
1738
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001740 status_t res;
1741 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001742
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001743 while (mResultQueue.empty()) {
1744 res = mResultSignal.waitRelative(mOutputLock, timeout);
1745 if (res == TIMED_OUT) {
1746 return res;
1747 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001748 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1749 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001750 return res;
1751 }
1752 }
1753 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001754}
1755
Jianing Weicb0652e2014-03-12 18:29:36 -07001756status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001757 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001758 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001759
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001760 if (mResultQueue.empty()) {
1761 return NOT_ENOUGH_DATA;
1762 }
1763
Jianing Weicb0652e2014-03-12 18:29:36 -07001764 if (frame == NULL) {
1765 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1766 return BAD_VALUE;
1767 }
1768
1769 CaptureResult &result = *(mResultQueue.begin());
1770 frame->mResultExtras = result.mResultExtras;
1771 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001772 mResultQueue.erase(mResultQueue.begin());
1773
1774 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001775}
1776
1777status_t Camera3Device::triggerAutofocus(uint32_t id) {
1778 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001779 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001780
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001781 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1782 // Mix-in this trigger into the next request and only the next request.
1783 RequestTrigger trigger[] = {
1784 {
1785 ANDROID_CONTROL_AF_TRIGGER,
1786 ANDROID_CONTROL_AF_TRIGGER_START
1787 },
1788 {
1789 ANDROID_CONTROL_AF_TRIGGER_ID,
1790 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001791 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001792 };
1793
1794 return mRequestThread->queueTrigger(trigger,
1795 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001796}
1797
1798status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1799 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001800 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001801
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001802 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1803 // Mix-in this trigger into the next request and only the next request.
1804 RequestTrigger trigger[] = {
1805 {
1806 ANDROID_CONTROL_AF_TRIGGER,
1807 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1808 },
1809 {
1810 ANDROID_CONTROL_AF_TRIGGER_ID,
1811 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001812 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001813 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001814
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001815 return mRequestThread->queueTrigger(trigger,
1816 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001817}
1818
1819status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1820 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001821 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001822
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001823 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1824 // Mix-in this trigger into the next request and only the next request.
1825 RequestTrigger trigger[] = {
1826 {
1827 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1828 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1829 },
1830 {
1831 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1832 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001833 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001834 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001835
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001836 return mRequestThread->queueTrigger(trigger,
1837 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001838}
1839
Jianing Weicb0652e2014-03-12 18:29:36 -07001840status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001841 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001842 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001843 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001844
Zhijun He7ef20392014-04-21 16:04:17 -07001845 {
1846 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001847 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001848 }
1849
Emilian Peev08dd2452017-04-06 16:55:14 +01001850 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001851}
1852
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001853status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001854 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1855}
1856
1857status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001858 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001859 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001860 Mutex::Autolock il(mInterfaceLock);
1861 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001862
1863 sp<Camera3StreamInterface> stream;
1864 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1865 if (outputStreamIdx == NAME_NOT_FOUND) {
1866 CLOGE("Stream %d does not exist", streamId);
1867 return BAD_VALUE;
1868 }
1869
1870 stream = mOutputStreams.editValueAt(outputStreamIdx);
1871
1872 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001873 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001874 return BAD_VALUE;
1875 }
1876
1877 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001878 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001879 return BAD_VALUE;
1880 }
1881
Ruben Brunkc78ac262015-08-13 17:58:46 -07001882 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001883}
1884
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001885status_t Camera3Device::tearDown(int streamId) {
1886 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001887 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001888 Mutex::Autolock il(mInterfaceLock);
1889 Mutex::Autolock l(mLock);
1890
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001891 sp<Camera3StreamInterface> stream;
1892 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1893 if (outputStreamIdx == NAME_NOT_FOUND) {
1894 CLOGE("Stream %d does not exist", streamId);
1895 return BAD_VALUE;
1896 }
1897
1898 stream = mOutputStreams.editValueAt(outputStreamIdx);
1899
1900 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1901 CLOGE("Stream %d is a target of a in-progress request", streamId);
1902 return BAD_VALUE;
1903 }
1904
1905 return stream->tearDown();
1906}
1907
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001908status_t Camera3Device::addBufferListenerForStream(int streamId,
1909 wp<Camera3StreamBufferListener> listener) {
1910 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001911 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001912 Mutex::Autolock il(mInterfaceLock);
1913 Mutex::Autolock l(mLock);
1914
1915 sp<Camera3StreamInterface> stream;
1916 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1917 if (outputStreamIdx == NAME_NOT_FOUND) {
1918 CLOGE("Stream %d does not exist", streamId);
1919 return BAD_VALUE;
1920 }
1921
1922 stream = mOutputStreams.editValueAt(outputStreamIdx);
1923 stream->addBufferListener(listener);
1924
1925 return OK;
1926}
1927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001929 * Methods called by subclasses
1930 */
1931
1932void Camera3Device::notifyStatus(bool idle) {
1933 {
1934 // Need mLock to safely update state and synchronize to current
1935 // state of methods in flight.
1936 Mutex::Autolock l(mLock);
1937 // We can get various system-idle notices from the status tracker
1938 // while starting up. Only care about them if we've actually sent
1939 // in some requests recently.
1940 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1941 return;
1942 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001943 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001944 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001945 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001946
1947 // Skip notifying listener if we're doing some user-transparent
1948 // state changes
1949 if (mPauseStateNotify) return;
1950 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001951
1952 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001953 {
1954 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001955 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001956 }
1957 if (idle && listener != NULL) {
1958 listener->notifyIdle();
1959 }
1960}
1961
Shuzhen Wang758c2152017-01-10 18:26:18 -08001962status_t Camera3Device::setConsumerSurfaces(int streamId,
1963 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07001964 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08001965 ALOGV("%s: Camera %s: set consumer surface for stream %d",
1966 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07001967 Mutex::Autolock il(mInterfaceLock);
1968 Mutex::Autolock l(mLock);
1969
Shuzhen Wang758c2152017-01-10 18:26:18 -08001970 if (consumers.size() == 0) {
1971 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07001972 return BAD_VALUE;
1973 }
1974
1975 ssize_t idx = mOutputStreams.indexOfKey(streamId);
1976 if (idx == NAME_NOT_FOUND) {
1977 CLOGE("Stream %d is unknown", streamId);
1978 return idx;
1979 }
1980 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08001981 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07001982 if (res != OK) {
1983 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
1984 return res;
1985 }
1986
Shuzhen Wang0129d522016-10-30 22:43:41 -07001987 if (stream->isConsumerConfigurationDeferred()) {
1988 if (!stream->isConfiguring()) {
1989 CLOGE("Stream %d was already fully configured.", streamId);
1990 return INVALID_OPERATION;
1991 }
Zhijun He5d677d12016-05-29 16:52:39 -07001992
Shuzhen Wang0129d522016-10-30 22:43:41 -07001993 res = stream->finishConfiguration();
1994 if (res != OK) {
1995 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1996 stream->getId(), strerror(-res), res);
1997 return res;
1998 }
Zhijun He5d677d12016-05-29 16:52:39 -07001999 }
2000
2001 return OK;
2002}
2003
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002004/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002005 * Camera3Device private methods
2006 */
2007
2008sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07002009 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002010 ATRACE_CALL();
2011 status_t res;
2012
2013 sp<CaptureRequest> newRequest = new CaptureRequest;
2014 newRequest->mSettings = request;
2015
2016 camera_metadata_entry_t inputStreams =
2017 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
2018 if (inputStreams.count > 0) {
2019 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002020 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002021 CLOGE("Request references unknown input stream %d",
2022 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002023 return NULL;
2024 }
2025 // Lazy completion of stream configuration (allocation/registration)
2026 // on first use
2027 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002028 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002029 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002030 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002031 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002032 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002033 return NULL;
2034 }
2035 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002036 // Check if stream is being prepared
2037 if (mInputStream->isPreparing()) {
2038 CLOGE("Request references an input stream that's being prepared!");
2039 return NULL;
2040 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002041
2042 newRequest->mInputStream = mInputStream;
2043 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
2044 }
2045
2046 camera_metadata_entry_t streams =
2047 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2048 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002049 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050 return NULL;
2051 }
2052
2053 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002054 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002055 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002056 CLOGE("Request references unknown stream %d",
2057 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058 return NULL;
2059 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002060 sp<Camera3OutputStreamInterface> stream =
2061 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002062
Zhijun He5d677d12016-05-29 16:52:39 -07002063 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002064 auto iter = surfaceMap.find(streams.data.i32[i]);
2065 if (iter != surfaceMap.end()) {
2066 const std::vector<size_t>& surfaces = iter->second;
2067 for (const auto& surface : surfaces) {
2068 if (stream->isConsumerConfigurationDeferred(surface)) {
2069 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2070 "due to deferred consumer", stream->getId(), surface);
2071 return NULL;
2072 }
2073 }
2074 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002075 }
2076
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 // Lazy completion of stream configuration (allocation/registration)
2078 // on first use
2079 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002080 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002081 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002082 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2083 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002084 return NULL;
2085 }
2086 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002087 // Check if stream is being prepared
2088 if (stream->isPreparing()) {
2089 CLOGE("Request references an output stream that's being prepared!");
2090 return NULL;
2091 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092
2093 newRequest->mOutputStreams.push(stream);
2094 }
2095 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002096 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097
2098 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002099}
2100
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002101bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2102 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2103 Size size = mSupportedOpaqueInputSizes[i];
2104 if (size.width == width && size.height == height) {
2105 return true;
2106 }
2107 }
2108
2109 return false;
2110}
2111
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002112void Camera3Device::cancelStreamsConfigurationLocked() {
2113 int res = OK;
2114 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2115 res = mInputStream->cancelConfiguration();
2116 if (res != OK) {
2117 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2118 mInputStream->getId(), strerror(-res), res);
2119 }
2120 }
2121
2122 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2123 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2124 if (outputStream->isConfiguring()) {
2125 res = outputStream->cancelConfiguration();
2126 if (res != OK) {
2127 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2128 outputStream->getId(), strerror(-res), res);
2129 }
2130 }
2131 }
2132
2133 // Return state to that at start of call, so that future configures
2134 // properly clean things up
2135 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2136 mNeedConfig = true;
2137}
2138
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002139status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140 ATRACE_CALL();
2141 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002142
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002143 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002144 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002145 return INVALID_OPERATION;
2146 }
2147
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002148 if (operatingMode < 0) {
2149 CLOGE("Invalid operating mode: %d", operatingMode);
2150 return BAD_VALUE;
2151 }
2152
2153 bool isConstrainedHighSpeed =
2154 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2155 operatingMode;
2156
2157 if (mOperatingMode != operatingMode) {
2158 mNeedConfig = true;
2159 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2160 mOperatingMode = operatingMode;
2161 }
2162
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002163 if (!mNeedConfig) {
2164 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2165 return OK;
2166 }
2167
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002168 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2169 // adding a dummy stream instead.
2170 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2171 if (mOutputStreams.size() == 0) {
2172 addDummyStreamLocked();
2173 } else {
2174 tryRemoveDummyStreamLocked();
2175 }
2176
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002177 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002178 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002179
2180 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002181 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002182 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2183
2184 Vector<camera3_stream_t*> streams;
2185 streams.setCapacity(config.num_streams);
2186
2187 if (mInputStream != NULL) {
2188 camera3_stream_t *inputStream;
2189 inputStream = mInputStream->startConfiguration();
2190 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002191 CLOGE("Can't start input stream configuration");
2192 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002193 return INVALID_OPERATION;
2194 }
2195 streams.add(inputStream);
2196 }
2197
2198 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002199
2200 // Don't configure bidi streams twice, nor add them twice to the list
2201 if (mOutputStreams[i].get() ==
2202 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2203
2204 config.num_streams--;
2205 continue;
2206 }
2207
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002208 camera3_stream_t *outputStream;
2209 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2210 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002211 CLOGE("Can't start output stream configuration");
2212 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213 return INVALID_OPERATION;
2214 }
2215 streams.add(outputStream);
2216 }
2217
2218 config.streams = streams.editArray();
2219
2220 // Do the HAL configuration; will potentially touch stream
2221 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002222
2223 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002224
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002225 if (res == BAD_VALUE) {
2226 // HAL rejected this set of streams as unsupported, clean up config
2227 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002228 CLOGE("Set of requested inputs/outputs not supported by HAL");
2229 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002230 return BAD_VALUE;
2231 } else if (res != OK) {
2232 // Some other kind of error from configure_streams - this is not
2233 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002234 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2235 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002236 return res;
2237 }
2238
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002239 // Finish all stream configuration immediately.
2240 // TODO: Try to relax this later back to lazy completion, which should be
2241 // faster
2242
Igor Murashkin073f8572013-05-02 14:59:28 -07002243 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002244 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002245 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002246 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002247 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002248 cancelStreamsConfigurationLocked();
2249 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002250 }
2251 }
2252
2253 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002254 sp<Camera3OutputStreamInterface> outputStream =
2255 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002256 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002257 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002258 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002259 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002260 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002261 cancelStreamsConfigurationLocked();
2262 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002263 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002264 }
2265 }
2266
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002267 // Request thread needs to know to avoid using repeat-last-settings protocol
2268 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002269 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002270
Zhijun He90f7c372016-08-16 16:19:43 -07002271 char value[PROPERTY_VALUE_MAX];
2272 property_get("camera.fifo.disable", value, "0");
2273 int32_t disableFifo = atoi(value);
2274 if (disableFifo != 1) {
2275 // Boost priority of request thread to SCHED_FIFO.
2276 pid_t requestThreadTid = mRequestThread->getTid();
2277 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002278 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002279 if (res != OK) {
2280 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2281 strerror(-res), res);
2282 } else {
2283 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2284 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002285 }
2286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002287 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002288
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002289 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002290
Ruben Brunk183f0562015-08-12 12:55:02 -07002291 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2292 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002293
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002294 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002295
Zhijun He0a210512014-07-24 13:45:15 -07002296 // tear down the deleted streams after configure streams.
2297 mDeletedStreams.clear();
2298
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002299 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002300}
2301
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002302status_t Camera3Device::addDummyStreamLocked() {
2303 ATRACE_CALL();
2304 status_t res;
2305
2306 if (mDummyStreamId != NO_STREAM) {
2307 // Should never be adding a second dummy stream when one is already
2308 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002309 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2310 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002311 return INVALID_OPERATION;
2312 }
2313
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002314 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002315
2316 sp<Camera3OutputStreamInterface> dummyStream =
2317 new Camera3DummyStream(mNextStreamId);
2318
2319 res = mOutputStreams.add(mNextStreamId, dummyStream);
2320 if (res < 0) {
2321 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2322 return res;
2323 }
2324
2325 mDummyStreamId = mNextStreamId;
2326 mNextStreamId++;
2327
2328 return OK;
2329}
2330
2331status_t Camera3Device::tryRemoveDummyStreamLocked() {
2332 ATRACE_CALL();
2333 status_t res;
2334
2335 if (mDummyStreamId == NO_STREAM) return OK;
2336 if (mOutputStreams.size() == 1) return OK;
2337
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002338 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002339
2340 // Ok, have a dummy stream and there's at least one other output stream,
2341 // so remove the dummy
2342
2343 sp<Camera3StreamInterface> deletedStream;
2344 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2345 if (outputStreamIdx == NAME_NOT_FOUND) {
2346 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2347 return INVALID_OPERATION;
2348 }
2349
2350 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2351 mOutputStreams.removeItemsAt(outputStreamIdx);
2352
2353 // Free up the stream endpoint so that it can be used by some other stream
2354 res = deletedStream->disconnect();
2355 if (res != OK) {
2356 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2357 // fall through since we want to still list the stream as deleted.
2358 }
2359 mDeletedStreams.add(deletedStream);
2360 mDummyStreamId = NO_STREAM;
2361
2362 return res;
2363}
2364
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002365void Camera3Device::setErrorState(const char *fmt, ...) {
2366 Mutex::Autolock l(mLock);
2367 va_list args;
2368 va_start(args, fmt);
2369
2370 setErrorStateLockedV(fmt, args);
2371
2372 va_end(args);
2373}
2374
2375void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2376 Mutex::Autolock l(mLock);
2377 setErrorStateLockedV(fmt, args);
2378}
2379
2380void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2381 va_list args;
2382 va_start(args, fmt);
2383
2384 setErrorStateLockedV(fmt, args);
2385
2386 va_end(args);
2387}
2388
2389void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002390 // Print out all error messages to log
2391 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002392 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002393
2394 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002395 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002396
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002397 mErrorCause = errorCause;
2398
2399 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002400 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002401
2402 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002403 sp<NotificationListener> listener = mListener.promote();
2404 if (listener != NULL) {
2405 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002406 CaptureResultExtras());
2407 }
2408
2409 // Save stack trace. View by dumping it later.
2410 CameraTraces::saveTrace();
2411 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002412}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413
2414/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002415 * In-flight request management
2416 */
2417
Jianing Weicb0652e2014-03-12 18:29:36 -07002418status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002419 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002420 const AeTriggerCancelOverride_t &aeTriggerCancelOverride,
2421 bool hasAppCallback) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002422 ATRACE_CALL();
2423 Mutex::Autolock l(mInFlightLock);
2424
2425 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002426 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002427 aeTriggerCancelOverride, hasAppCallback));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002428 if (res < 0) return res;
2429
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002430 if (mInFlightMap.size() == 1) {
2431 mStatusTracker->markComponentActive(mInFlightStatusId);
2432 }
2433
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002434 return OK;
2435}
2436
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002437void Camera3Device::returnOutputBuffers(
2438 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2439 nsecs_t timestamp) {
2440 for (size_t i = 0; i < numBuffers; i++)
2441 {
2442 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2443 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2444 // Note: stream may be deallocated at this point, if this buffer was
2445 // the last reference to it.
2446 if (res != OK) {
2447 ALOGE("Can't return buffer to its stream: %s (%d)",
2448 strerror(-res), res);
2449 }
2450 }
2451}
2452
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002453void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2454 mInFlightMap.removeItemsAt(idx, 1);
2455
2456 // Indicate idle inFlightMap to the status tracker
2457 if (mInFlightMap.size() == 0) {
2458 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2459 }
2460}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002461
2462void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2463
2464 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2465 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2466
2467 nsecs_t sensorTimestamp = request.sensorTimestamp;
2468 nsecs_t shutterTimestamp = request.shutterTimestamp;
2469
2470 // Check if it's okay to remove the request from InFlightMap:
2471 // In the case of a successful request:
2472 // all input and output buffers, all result metadata, shutter callback
2473 // arrived.
2474 // In the case of a unsuccessful request:
2475 // all input and output buffers arrived.
2476 if (request.numBuffersLeft == 0 &&
2477 (request.requestStatus != OK ||
2478 (request.haveResultMetadata && shutterTimestamp != 0))) {
2479 ATRACE_ASYNC_END("frame capture", frameNumber);
2480
Shuzhen Wang403044a2017-02-26 23:29:04 -08002481 // Sanity check - if sensor timestamp matches shutter timestamp in the
2482 // case of request having callback.
2483 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002484 sensorTimestamp != shutterTimestamp) {
2485 SET_ERR("sensor timestamp (%" PRId64
2486 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2487 sensorTimestamp, frameNumber, shutterTimestamp);
2488 }
2489
2490 // for an unsuccessful request, it may have pending output buffers to
2491 // return.
2492 assert(request.requestStatus != OK ||
2493 request.pendingOutputBuffers.size() == 0);
2494 returnOutputBuffers(request.pendingOutputBuffers.array(),
2495 request.pendingOutputBuffers.size(), 0);
2496
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002497 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002498 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2499 }
2500
2501 // Sanity check - if we have too many in-flight frames, something has
2502 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002503 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002504 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002505 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2506 kInFlightWarnLimitHighSpeed) {
2507 CLOGE("In-flight list too large for high speed configuration: %zu",
2508 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002509 }
2510}
2511
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002512void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2513 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2514 if (result == nullptr) return;
2515
Emilian Peev71c73a22017-03-21 16:35:51 +00002516 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
2517 result->mMetadata.getAndLock());
2518 set_camera_metadata_vendor_id(meta, mVendorTagId);
2519 result->mMetadata.unlock(meta);
2520
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002521 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2522 (int32_t*)&frameNumber, 1) != OK) {
2523 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2524 return;
2525 }
2526
2527 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2528 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2529 return;
2530 }
2531
2532 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2533
2534 // Valid result, insert into queue
2535 List<CaptureResult>::iterator queuedResult =
2536 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2537 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2538 ", burstId = %" PRId32, __FUNCTION__,
2539 queuedResult->mResultExtras.requestId,
2540 queuedResult->mResultExtras.frameNumber,
2541 queuedResult->mResultExtras.burstId);
2542
2543 mResultSignal.signal();
2544}
2545
2546
2547void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2548 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2549 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2550 Mutex::Autolock l(mOutputLock);
2551
2552 CaptureResult captureResult;
2553 captureResult.mResultExtras = resultExtras;
2554 captureResult.mMetadata = partialResult;
2555
2556 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2557}
2558
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002559
2560void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2561 CaptureResultExtras &resultExtras,
2562 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002563 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002564 bool reprocess,
2565 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002566 if (pendingMetadata.isEmpty())
2567 return;
2568
2569 Mutex::Autolock l(mOutputLock);
2570
2571 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002572 if (reprocess) {
2573 if (frameNumber < mNextReprocessResultFrameNumber) {
2574 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002575 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002576 frameNumber, mNextReprocessResultFrameNumber);
2577 return;
2578 }
2579 mNextReprocessResultFrameNumber = frameNumber + 1;
2580 } else {
2581 if (frameNumber < mNextResultFrameNumber) {
2582 SET_ERR("Out-of-order capture result metadata submitted! "
2583 "(got frame number %d, expecting %d)",
2584 frameNumber, mNextResultFrameNumber);
2585 return;
2586 }
2587 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002588 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002589
2590 CaptureResult captureResult;
2591 captureResult.mResultExtras = resultExtras;
2592 captureResult.mMetadata = pendingMetadata;
2593
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002594 // Append any previous partials to form a complete result
2595 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2596 captureResult.mMetadata.append(collectedPartialResult);
2597 }
2598
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002599 // Derive some new keys for backward compaibility
2600 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2601 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2602 int32_t defaultBoost[1] = {100};
2603 captureResult.mMetadata.update(
2604 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2605 defaultBoost, 1);
2606 }
2607
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002608 captureResult.mMetadata.sort();
2609
2610 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002611 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2612 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002613 SET_ERR("No timestamp provided by HAL for frame %d!",
2614 frameNumber);
2615 return;
2616 }
2617
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002618 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2619 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2620
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002621 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002622}
2623
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002624/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002625 * Camera HAL device callback methods
2626 */
2627
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002628void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002629 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002630
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002631 status_t res;
2632
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002633 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002634 if (result->result == NULL && result->num_output_buffers == 0 &&
2635 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002636 SET_ERR("No result data provided by HAL for frame %d",
2637 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002638 return;
2639 }
Zhijun He204e3292014-07-14 17:09:23 -07002640
Zhijun He204e3292014-07-14 17:09:23 -07002641 if (!mUsePartialResult &&
Zhijun He204e3292014-07-14 17:09:23 -07002642 result->result != NULL &&
2643 result->partial_result != 1) {
2644 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2645 " if partial result is not supported",
2646 frameNumber, result->partial_result);
2647 return;
2648 }
2649
2650 bool isPartialResult = false;
2651 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002652 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002653 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002654
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002655 // Get shutter timestamp and resultExtras from list of in-flight requests,
2656 // where it was added by the shutter notification for this frame. If the
2657 // shutter timestamp isn't received yet, append the output buffers to the
2658 // in-flight request and they will be returned when the shutter timestamp
2659 // arrives. Update the in-flight status and remove the in-flight entry if
2660 // all result data and shutter timestamp have been received.
2661 nsecs_t shutterTimestamp = 0;
2662
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002663 {
2664 Mutex::Autolock l(mInFlightLock);
2665 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2666 if (idx == NAME_NOT_FOUND) {
2667 SET_ERR("Unknown frame number for capture result: %d",
2668 frameNumber);
2669 return;
2670 }
2671 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002672 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2673 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002674 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002675 __FUNCTION__, request.resultExtras.requestId,
2676 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002677 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002678 // Always update the partial count to the latest one if it's not 0
2679 // (buffers only). When framework aggregates adjacent partial results
2680 // into one, the latest partial count will be used.
2681 if (result->partial_result != 0)
2682 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002683
2684 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002685 if (mUsePartialResult && result->result != NULL) {
Emilian Peev08dd2452017-04-06 16:55:14 +01002686 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2687 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2688 " the range of [1, %d] when metadata is included in the result",
2689 frameNumber, result->partial_result, mNumPartialResults);
2690 return;
2691 }
2692 isPartialResult = (result->partial_result < mNumPartialResults);
2693 if (isPartialResult) {
2694 request.collectedPartialResult.append(result->result);
Zhijun He204e3292014-07-14 17:09:23 -07002695 }
2696
Shuzhen Wang4a472662017-02-26 23:29:04 -08002697 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002698 // Send partial capture result
2699 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2700 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002701 }
2702 }
2703
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002704 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002705 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002706
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002707 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002708 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002709 if (request.haveResultMetadata) {
2710 SET_ERR("Called multiple times with metadata for frame %d",
2711 frameNumber);
2712 return;
2713 }
Zhijun He204e3292014-07-14 17:09:23 -07002714 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002715 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002716 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002717 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002718 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002719 request.haveResultMetadata = true;
2720 }
2721
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002722 uint32_t numBuffersReturned = result->num_output_buffers;
2723 if (result->input_buffer != NULL) {
2724 if (hasInputBufferInRequest) {
2725 numBuffersReturned += 1;
2726 } else {
2727 ALOGW("%s: Input buffer should be NULL if there is no input"
2728 " buffer sent in the request",
2729 __FUNCTION__);
2730 }
2731 }
2732 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002733 if (request.numBuffersLeft < 0) {
2734 SET_ERR("Too many buffers returned for frame %d",
2735 frameNumber);
2736 return;
2737 }
2738
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002739 camera_metadata_ro_entry_t entry;
2740 res = find_camera_metadata_ro_entry(result->result,
2741 ANDROID_SENSOR_TIMESTAMP, &entry);
2742 if (res == OK && entry.count == 1) {
2743 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002744 }
2745
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002746 // If shutter event isn't received yet, append the output buffers to
2747 // the in-flight request. Otherwise, return the output buffers to
2748 // streams.
2749 if (shutterTimestamp == 0) {
2750 request.pendingOutputBuffers.appendArray(result->output_buffers,
2751 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002752 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002753 returnOutputBuffers(result->output_buffers,
2754 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002755 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002756
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002757 if (result->result != NULL && !isPartialResult) {
2758 if (shutterTimestamp == 0) {
2759 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002760 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002761 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002762 CameraMetadata metadata;
2763 metadata = result->result;
2764 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002765 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2766 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002767 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002768 }
2769
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002770 removeInFlightRequestIfReadyLocked(idx);
2771 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002772
Zhijun Hef0d962a2014-06-30 10:24:11 -07002773 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002774 if (hasInputBufferInRequest) {
2775 Camera3Stream *stream =
2776 Camera3Stream::cast(result->input_buffer->stream);
2777 res = stream->returnInputBuffer(*(result->input_buffer));
2778 // Note: stream may be deallocated at this point, if this buffer was the
2779 // last reference to it.
2780 if (res != OK) {
2781 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2782 " its stream:%s (%d)", __FUNCTION__,
2783 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002784 }
2785 } else {
2786 ALOGW("%s: Input buffer should be NULL if there is no input"
2787 " buffer sent in the request, skipping input buffer return.",
2788 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002789 }
2790 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002791}
2792
2793void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002794 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002795 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002796 {
2797 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002798 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002799 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002800
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002801 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002802 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002803 return;
2804 }
2805
2806 switch (msg->type) {
2807 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002808 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002809 break;
2810 }
2811 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002812 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002813 break;
2814 }
2815 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002816 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002817 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002818 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002819}
2820
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002821void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002822 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002823
2824 // Map camera HAL error codes to ICameraDeviceCallback error codes
2825 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002826 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002827 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002828 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002829 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002830 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002831 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002832 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002833 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002834 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002835 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002836 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002837 };
2838
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002839 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002840 ((msg.error_code >= 0) &&
2841 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2842 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002843 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002844
2845 int streamId = 0;
2846 if (msg.error_stream != NULL) {
2847 Camera3Stream *stream =
2848 Camera3Stream::cast(msg.error_stream);
2849 streamId = stream->getId();
2850 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002851 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2852 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002853 streamId, msg.error_code);
2854
2855 CaptureResultExtras resultExtras;
2856 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002857 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002858 // SET_ERR calls notifyError
2859 SET_ERR("Camera HAL reported serious device error");
2860 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002861 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2862 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2863 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002864 {
2865 Mutex::Autolock l(mInFlightLock);
2866 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2867 if (idx >= 0) {
2868 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2869 r.requestStatus = msg.error_code;
2870 resultExtras = r.resultExtras;
Emilian Peevba0fac32017-03-30 09:05:34 +01002871 if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
2872 errorCode) {
2873 // In case of missing result check whether the buffers
2874 // returned. If they returned, then remove inflight
2875 // request.
2876 removeInFlightRequestIfReadyLocked(idx);
2877 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002878 } else {
2879 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002880 ALOGE("Camera %s: %s: cannot find in-flight request on "
2881 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002882 resultExtras.frameNumber);
2883 }
2884 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002885 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002886 if (listener != NULL) {
2887 listener->notifyError(errorCode, resultExtras);
2888 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002889 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002890 }
2891 break;
2892 default:
2893 // SET_ERR calls notifyError
2894 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2895 break;
2896 }
2897}
2898
2899void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002900 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002901 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002902
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002903 // Set timestamp for the request in the in-flight tracking
2904 // and get the request ID to send upstream
2905 {
2906 Mutex::Autolock l(mInFlightLock);
2907 idx = mInFlightMap.indexOfKey(msg.frame_number);
2908 if (idx >= 0) {
2909 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002910
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002911 // Verify ordering of shutter notifications
2912 {
2913 Mutex::Autolock l(mOutputLock);
2914 // TODO: need to track errors for tighter bounds on expected frame number.
2915 if (r.hasInputBuffer) {
2916 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2917 SET_ERR("Shutter notification out-of-order. Expected "
2918 "notification for frame %d, got frame %d",
2919 mNextReprocessShutterFrameNumber, msg.frame_number);
2920 return;
2921 }
2922 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2923 } else {
2924 if (msg.frame_number < mNextShutterFrameNumber) {
2925 SET_ERR("Shutter notification out-of-order. Expected "
2926 "notification for frame %d, got frame %d",
2927 mNextShutterFrameNumber, msg.frame_number);
2928 return;
2929 }
2930 mNextShutterFrameNumber = msg.frame_number + 1;
2931 }
2932 }
2933
Shuzhen Wang4a472662017-02-26 23:29:04 -08002934 r.shutterTimestamp = msg.timestamp;
2935 if (r.hasCallback) {
2936 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002937 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002938 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08002939 // Call listener, if any
2940 if (listener != NULL) {
2941 listener->notifyShutter(r.resultExtras, msg.timestamp);
2942 }
2943 // send pending result and buffers
2944 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2945 r.collectedPartialResult, msg.frame_number,
2946 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002947 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002948 returnOutputBuffers(r.pendingOutputBuffers.array(),
2949 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2950 r.pendingOutputBuffers.clear();
2951
2952 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002953 }
2954 }
2955 if (idx < 0) {
2956 SET_ERR("Shutter notification for non-existent frame number %d",
2957 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002958 }
2959}
2960
2961
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002962CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002963 ALOGV("%s", __FUNCTION__);
2964
Igor Murashkin1e479c02013-09-06 16:55:14 -07002965 CameraMetadata retVal;
2966
2967 if (mRequestThread != NULL) {
2968 retVal = mRequestThread->getLatestRequest();
2969 }
2970
Igor Murashkin1e479c02013-09-06 16:55:14 -07002971 return retVal;
2972}
2973
Jianing Weicb0652e2014-03-12 18:29:36 -07002974
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002975void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2976 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
2977 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
2978}
2979
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002980/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002981 * HalInterface inner class methods
2982 */
2983
2984Camera3Device::HalInterface::HalInterface(camera3_device_t *device) :
2985 mHal3Device(device) {}
2986
Yifan Hongf79b5542017-04-11 14:44:25 -07002987Camera3Device::HalInterface::HalInterface(
2988 sp<ICameraDeviceSession> &session,
2989 std::shared_ptr<RequestMetadataQueue> queue) :
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002990 mHal3Device(nullptr),
Yifan Hongf79b5542017-04-11 14:44:25 -07002991 mHidlSession(session),
2992 mRequestMetadataQueue(queue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002993
2994Camera3Device::HalInterface::HalInterface() :
2995 mHal3Device(nullptr) {}
2996
2997Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08002998 mHal3Device(other.mHal3Device),
Yifan Hongf79b5542017-04-11 14:44:25 -07002999 mHidlSession(other.mHidlSession),
3000 mRequestMetadataQueue(other.mRequestMetadataQueue) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003001
3002bool Camera3Device::HalInterface::valid() {
3003 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
3004}
3005
3006void Camera3Device::HalInterface::clear() {
3007 mHal3Device = nullptr;
3008 mHidlSession.clear();
3009}
3010
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003011bool Camera3Device::HalInterface::supportBatchRequest() {
3012 return mHidlSession != nullptr;
3013}
3014
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003015status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3016 camera3_request_template_t templateId,
3017 /*out*/ camera_metadata_t **requestTemplate) {
3018 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3019 if (!valid()) return INVALID_OPERATION;
3020 status_t res = OK;
3021
3022 if (mHal3Device != nullptr) {
3023 const camera_metadata *r;
3024 r = mHal3Device->ops->construct_default_request_settings(
3025 mHal3Device, templateId);
3026 if (r == nullptr) return BAD_VALUE;
3027 *requestTemplate = clone_camera_metadata(r);
3028 if (requestTemplate == nullptr) {
3029 ALOGE("%s: Unable to clone camera metadata received from HAL",
3030 __FUNCTION__);
3031 return INVALID_OPERATION;
3032 }
3033 } else {
3034 common::V1_0::Status status;
3035 RequestTemplate id;
3036 switch (templateId) {
3037 case CAMERA3_TEMPLATE_PREVIEW:
3038 id = RequestTemplate::PREVIEW;
3039 break;
3040 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3041 id = RequestTemplate::STILL_CAPTURE;
3042 break;
3043 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3044 id = RequestTemplate::VIDEO_RECORD;
3045 break;
3046 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3047 id = RequestTemplate::VIDEO_SNAPSHOT;
3048 break;
3049 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3050 id = RequestTemplate::ZERO_SHUTTER_LAG;
3051 break;
3052 case CAMERA3_TEMPLATE_MANUAL:
3053 id = RequestTemplate::MANUAL;
3054 break;
3055 default:
3056 // Unknown template ID
3057 return BAD_VALUE;
3058 }
3059 mHidlSession->constructDefaultRequestSettings(id,
3060 [&status, &requestTemplate]
3061 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3062 status = s;
3063 if (status == common::V1_0::Status::OK) {
3064 const camera_metadata *r =
3065 reinterpret_cast<const camera_metadata_t*>(request.data());
3066 size_t expectedSize = request.size();
3067 int ret = validate_camera_metadata_structure(r, &expectedSize);
Yin-Chia Yeh238ef5f2017-04-18 15:01:15 -07003068 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003069 *requestTemplate = clone_camera_metadata(r);
3070 if (*requestTemplate == nullptr) {
3071 ALOGE("%s: Unable to clone camera metadata received from HAL",
3072 __FUNCTION__);
3073 status = common::V1_0::Status::INTERNAL_ERROR;
3074 }
3075 } else {
3076 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3077 status = common::V1_0::Status::INTERNAL_ERROR;
3078 }
3079 }
3080 });
3081 res = CameraProviderManager::mapToStatusT(status);
3082 }
3083 return res;
3084}
3085
3086status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3087 ATRACE_NAME("CameraHal::configureStreams");
3088 if (!valid()) return INVALID_OPERATION;
3089 status_t res = OK;
3090
3091 if (mHal3Device != nullptr) {
3092 res = mHal3Device->ops->configure_streams(mHal3Device, config);
3093 } else {
3094 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003095 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003096 StreamConfiguration requestedConfiguration;
3097 requestedConfiguration.streams.resize(config->num_streams);
3098 for (size_t i = 0; i < config->num_streams; i++) {
3099 Stream &dst = requestedConfiguration.streams[i];
3100 camera3_stream_t *src = config->streams[i];
3101
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003102 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3103 cam3stream->setBufferFreedListener(this);
3104 int streamId = cam3stream->getId();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003105 StreamType streamType;
3106 switch (src->stream_type) {
3107 case CAMERA3_STREAM_OUTPUT:
3108 streamType = StreamType::OUTPUT;
3109 break;
3110 case CAMERA3_STREAM_INPUT:
3111 streamType = StreamType::INPUT;
3112 break;
3113 default:
3114 ALOGE("%s: Stream %d: Unsupported stream type %d",
3115 __FUNCTION__, streamId, config->streams[i]->stream_type);
3116 return BAD_VALUE;
3117 }
3118 dst.id = streamId;
3119 dst.streamType = streamType;
3120 dst.width = src->width;
3121 dst.height = src->height;
3122 dst.format = mapToPixelFormat(src->format);
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003123 dst.usage = mapToConsumerUsage(src->usage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003124 dst.dataSpace = mapToHidlDataspace(src->data_space);
3125 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003126
3127 activeStreams.insert(streamId);
3128 // Create Buffer ID map if necessary
3129 if (mBufferIdMaps.count(streamId) == 0) {
3130 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3131 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003132 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003133 // remove BufferIdMap for deleted streams
3134 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3135 int streamId = it->first;
3136 bool active = activeStreams.count(streamId) > 0;
3137 if (!active) {
3138 it = mBufferIdMaps.erase(it);
3139 } else {
3140 ++it;
3141 }
3142 }
3143
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08003144 res = mapToStreamConfigurationMode(
3145 (camera3_stream_configuration_mode_t) config->operation_mode,
3146 /*out*/ &requestedConfiguration.operationMode);
3147 if (res != OK) {
3148 return res;
3149 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003150
3151 // Invoke configureStreams
3152
3153 HalStreamConfiguration finalConfiguration;
3154 common::V1_0::Status status;
3155 mHidlSession->configureStreams(requestedConfiguration,
3156 [&status, &finalConfiguration]
3157 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3158 finalConfiguration = halConfiguration;
3159 status = s;
3160 });
3161 if (status != common::V1_0::Status::OK ) {
3162 return CameraProviderManager::mapToStatusT(status);
3163 }
3164
3165 // And convert output stream configuration from HIDL
3166
3167 for (size_t i = 0; i < config->num_streams; i++) {
3168 camera3_stream_t *dst = config->streams[i];
3169 int streamId = Camera3Stream::cast(dst)->getId();
3170
3171 // Start scan at i, with the assumption that the stream order matches
3172 size_t realIdx = i;
3173 bool found = false;
3174 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3175 if (finalConfiguration.streams[realIdx].id == streamId) {
3176 found = true;
3177 break;
3178 }
3179 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3180 }
3181 if (!found) {
3182 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3183 __FUNCTION__, streamId);
3184 return INVALID_OPERATION;
3185 }
3186 HalStream &src = finalConfiguration.streams[realIdx];
3187
3188 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3189 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3190 if (dst->format != overrideFormat) {
3191 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3192 streamId, dst->format);
3193 }
3194 } else {
3195 // Override allowed with IMPLEMENTATION_DEFINED
3196 dst->format = overrideFormat;
3197 }
3198
3199 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3200 if (src.producerUsage != 0) {
3201 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3202 __FUNCTION__, streamId);
3203 return INVALID_OPERATION;
3204 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003205 dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003206 } else {
3207 // OUTPUT
3208 if (src.consumerUsage != 0) {
3209 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3210 __FUNCTION__, streamId);
3211 return INVALID_OPERATION;
3212 }
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -07003213 dst->usage = mapProducerToFrameworkUsage(src.producerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003214 }
3215 dst->max_buffers = src.maxBuffers;
3216 }
3217 }
3218 return res;
3219}
3220
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003221void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3222 /*out*/device::V3_2::CaptureRequest* captureRequest,
3223 /*out*/std::vector<native_handle_t*>* handlesCreated) {
3224
3225 if (captureRequest == nullptr || handlesCreated == nullptr) {
3226 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3227 __FUNCTION__, captureRequest, handlesCreated);
3228 return;
3229 }
3230
3231 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003232
3233 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003234
3235 {
3236 std::lock_guard<std::mutex> lock(mInflightLock);
3237 if (request->input_buffer != nullptr) {
3238 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3239 buffer_handle_t buf = *(request->input_buffer->buffer);
3240 auto pair = getBufferId(buf, streamId);
3241 bool isNewBuffer = pair.first;
3242 uint64_t bufferId = pair.second;
3243 captureRequest->inputBuffer.streamId = streamId;
3244 captureRequest->inputBuffer.bufferId = bufferId;
3245 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3246 captureRequest->inputBuffer.status = BufferStatus::OK;
3247 native_handle_t *acquireFence = nullptr;
3248 if (request->input_buffer->acquire_fence != -1) {
3249 acquireFence = native_handle_create(1,0);
3250 acquireFence->data[0] = request->input_buffer->acquire_fence;
3251 handlesCreated->push_back(acquireFence);
3252 }
3253 captureRequest->inputBuffer.acquireFence = acquireFence;
3254 captureRequest->inputBuffer.releaseFence = nullptr;
3255
3256 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3257 request->input_buffer->buffer,
3258 request->input_buffer->acquire_fence);
3259 } else {
3260 captureRequest->inputBuffer.streamId = -1;
3261 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3262 }
3263
3264 captureRequest->outputBuffers.resize(request->num_output_buffers);
3265 for (size_t i = 0; i < request->num_output_buffers; i++) {
3266 const camera3_stream_buffer_t *src = request->output_buffers + i;
3267 StreamBuffer &dst = captureRequest->outputBuffers[i];
3268 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3269 buffer_handle_t buf = *(src->buffer);
3270 auto pair = getBufferId(buf, streamId);
3271 bool isNewBuffer = pair.first;
3272 dst.streamId = streamId;
3273 dst.bufferId = pair.second;
3274 dst.buffer = isNewBuffer ? buf : nullptr;
3275 dst.status = BufferStatus::OK;
3276 native_handle_t *acquireFence = nullptr;
3277 if (src->acquire_fence != -1) {
3278 acquireFence = native_handle_create(1,0);
3279 acquireFence->data[0] = src->acquire_fence;
3280 handlesCreated->push_back(acquireFence);
3281 }
3282 dst.acquireFence = acquireFence;
3283 dst.releaseFence = nullptr;
3284
3285 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3286 src->buffer, src->acquire_fence);
3287 }
3288 }
3289}
3290
3291status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3292 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3293 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3294 if (!valid()) return INVALID_OPERATION;
3295
3296 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3297 size_t batchSize = requests.size();
3298 captureRequests.resize(batchSize);
3299 std::vector<native_handle_t*> handlesCreated;
3300
3301 for (size_t i = 0; i < batchSize; i++) {
3302 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3303 }
3304
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003305 std::vector<device::V3_2::BufferCache> cachesToRemove;
3306 {
3307 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3308 for (auto& pair : mFreedBuffers) {
3309 // The stream might have been removed since onBufferFreed
3310 if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3311 cachesToRemove.push_back({pair.first, pair.second});
3312 }
3313 }
3314 mFreedBuffers.clear();
3315 }
3316
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003317 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3318 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003319
3320 // Write metadata to FMQ.
3321 for (size_t i = 0; i < batchSize; i++) {
3322 camera3_capture_request_t* request = requests[i];
3323 device::V3_2::CaptureRequest* captureRequest = &captureRequests[i];
3324
3325 if (request->settings != nullptr) {
3326 size_t settingsSize = get_camera_metadata_size(request->settings);
3327 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3328 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3329 captureRequest->settings.resize(0);
3330 captureRequest->fmqSettingsSize = settingsSize;
3331 } else {
3332 if (mRequestMetadataQueue != nullptr) {
3333 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3334 }
3335 captureRequest->settings.setToExternal(
3336 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3337 get_camera_metadata_size(request->settings));
3338 captureRequest->fmqSettingsSize = 0u;
3339 }
3340 } else {
3341 // A null request settings maps to a size-0 CameraMetadata
3342 captureRequest->settings.resize(0);
3343 captureRequest->fmqSettingsSize = 0u;
3344 }
3345 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003346 mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003347 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3348 status = s;
3349 *numRequestProcessed = n;
3350 });
3351
3352 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3353 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3354 __FUNCTION__, *numRequestProcessed, batchSize);
3355 status = common::V1_0::Status::INTERNAL_ERROR;
3356 }
3357
3358 for (auto& handle : handlesCreated) {
3359 native_handle_delete(handle);
3360 }
3361 return CameraProviderManager::mapToStatusT(status);
3362}
3363
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003364status_t Camera3Device::HalInterface::processCaptureRequest(
3365 camera3_capture_request_t *request) {
3366 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003367 if (!valid()) return INVALID_OPERATION;
3368 status_t res = OK;
3369
3370 if (mHal3Device != nullptr) {
3371 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3372 } else {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003373 uint32_t numRequestProcessed = 0;
3374 std::vector<camera3_capture_request_t*> requests(1);
3375 requests[0] = request;
3376 res = processBatchCaptureRequests(requests, &numRequestProcessed);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003377 }
3378 return res;
3379}
3380
3381status_t Camera3Device::HalInterface::flush() {
3382 ATRACE_NAME("CameraHal::flush");
3383 if (!valid()) return INVALID_OPERATION;
3384 status_t res = OK;
3385
3386 if (mHal3Device != nullptr) {
3387 res = mHal3Device->ops->flush(mHal3Device);
3388 } else {
3389 res = CameraProviderManager::mapToStatusT(mHidlSession->flush());
3390 }
3391 return res;
3392}
3393
3394status_t Camera3Device::HalInterface::dump(int fd) {
3395 ATRACE_NAME("CameraHal::dump");
3396 if (!valid()) return INVALID_OPERATION;
3397 status_t res = OK;
3398
3399 if (mHal3Device != nullptr) {
3400 mHal3Device->ops->dump(mHal3Device, fd);
3401 } else {
3402 // Handled by CameraProviderManager::dump
3403 }
3404 return res;
3405}
3406
3407status_t Camera3Device::HalInterface::close() {
3408 ATRACE_NAME("CameraHal::close()");
3409 if (!valid()) return INVALID_OPERATION;
3410 status_t res = OK;
3411
3412 if (mHal3Device != nullptr) {
3413 mHal3Device->common.close(&mHal3Device->common);
3414 } else {
3415 mHidlSession->close();
3416 }
3417 return res;
3418}
3419
3420status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003421 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003422 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003423 auto pair = std::make_pair(buffer, acquireFence);
3424 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003425 return OK;
3426}
3427
3428status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003429 int32_t frameNumber, int32_t streamId,
3430 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003431 std::lock_guard<std::mutex> lock(mInflightLock);
3432
3433 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3434 auto it = mInflightBufferMap.find(key);
3435 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003436 auto pair = it->second;
3437 *buffer = pair.first;
3438 int acquireFence = pair.second;
3439 if (acquireFence > 0) {
3440 ::close(acquireFence);
3441 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003442 mInflightBufferMap.erase(it);
3443 return OK;
3444}
3445
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003446std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3447 const buffer_handle_t& buf, int streamId) {
3448 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3449
3450 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3451 auto it = bIdMap.find(buf);
3452 if (it == bIdMap.end()) {
3453 bIdMap[buf] = mNextBufferId++;
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003454 ALOGV("stream %d now have %zu buffer caches, buf %p",
3455 streamId, bIdMap.size(), buf);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003456 return std::make_pair(true, mNextBufferId - 1);
3457 } else {
3458 return std::make_pair(false, it->second);
3459 }
3460}
3461
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003462void Camera3Device::HalInterface::onBufferFreed(
3463 int streamId, const native_handle_t* handle) {
3464 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3465 uint64_t bufferId = BUFFER_ID_NO_BUFFER;
3466 auto mapIt = mBufferIdMaps.find(streamId);
3467 if (mapIt == mBufferIdMaps.end()) {
3468 // streamId might be from a deleted stream here
3469 ALOGI("%s: stream %d has been removed",
3470 __FUNCTION__, streamId);
3471 return;
3472 }
3473 BufferIdMap& bIdMap = mapIt->second;
3474 auto it = bIdMap.find(handle);
3475 if (it == bIdMap.end()) {
3476 ALOGW("%s: cannot find buffer %p in stream %d",
3477 __FUNCTION__, handle, streamId);
3478 return;
3479 } else {
3480 bufferId = it->second;
3481 bIdMap.erase(it);
3482 ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
3483 __FUNCTION__, streamId, bIdMap.size(), handle);
3484 }
3485 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3486}
3487
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003488/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003489 * RequestThread inner class methods
3490 */
3491
3492Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003493 sp<StatusTracker> statusTracker,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003494 HalInterface* interface,
3495 uint32_t deviceVersion,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07003496 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003497 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003498 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003499 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003500 mInterface(interface),
3501 mDeviceVersion(deviceVersion),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003502 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003503 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003504 mReconfigured(false),
3505 mDoPause(false),
3506 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003507 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003508 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003509 mCurrentAfTriggerId(0),
3510 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003511 mRepeatingLastFrameNumber(
3512 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003513 mAeLockAvailable(aeLockAvailable),
3514 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003515 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003516}
3517
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003518Camera3Device::RequestThread::~RequestThread() {}
3519
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003520void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003521 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003522 Mutex::Autolock l(mRequestLock);
3523 mListener = listener;
3524}
3525
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003526void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003527 Mutex::Autolock l(mRequestLock);
3528 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003529 // Prepare video stream for high speed recording.
3530 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003531}
3532
Jianing Wei90e59c92014-03-12 18:29:36 -07003533status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003534 List<sp<CaptureRequest> > &requests,
3535 /*out*/
3536 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003537 Mutex::Autolock l(mRequestLock);
3538 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3539 ++it) {
3540 mRequestQueue.push_back(*it);
3541 }
3542
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003543 if (lastFrameNumber != NULL) {
3544 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3545 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3546 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3547 *lastFrameNumber);
3548 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003549
Jianing Wei90e59c92014-03-12 18:29:36 -07003550 unpauseForNewRequests();
3551
3552 return OK;
3553}
3554
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003555
3556status_t Camera3Device::RequestThread::queueTrigger(
3557 RequestTrigger trigger[],
3558 size_t count) {
3559
3560 Mutex::Autolock l(mTriggerMutex);
3561 status_t ret;
3562
3563 for (size_t i = 0; i < count; ++i) {
3564 ret = queueTriggerLocked(trigger[i]);
3565
3566 if (ret != OK) {
3567 return ret;
3568 }
3569 }
3570
3571 return OK;
3572}
3573
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003574const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3575 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003576 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003577 if (d != nullptr) return d->mId;
3578 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003579}
3580
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003581status_t Camera3Device::RequestThread::queueTriggerLocked(
3582 RequestTrigger trigger) {
3583
3584 uint32_t tag = trigger.metadataTag;
3585 ssize_t index = mTriggerMap.indexOfKey(tag);
3586
3587 switch (trigger.getTagType()) {
3588 case TYPE_BYTE:
3589 // fall-through
3590 case TYPE_INT32:
3591 break;
3592 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003593 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3594 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003595 return INVALID_OPERATION;
3596 }
3597
3598 /**
3599 * Collect only the latest trigger, since we only have 1 field
3600 * in the request settings per trigger tag, and can't send more than 1
3601 * trigger per request.
3602 */
3603 if (index != NAME_NOT_FOUND) {
3604 mTriggerMap.editValueAt(index) = trigger;
3605 } else {
3606 mTriggerMap.add(tag, trigger);
3607 }
3608
3609 return OK;
3610}
3611
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003612status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003613 const RequestList &requests,
3614 /*out*/
3615 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003616 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003617 if (lastFrameNumber != NULL) {
3618 *lastFrameNumber = mRepeatingLastFrameNumber;
3619 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003620 mRepeatingRequests.clear();
3621 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3622 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003623
3624 unpauseForNewRequests();
3625
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003626 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003627 return OK;
3628}
3629
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003630bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003631 if (mRepeatingRequests.empty()) {
3632 return false;
3633 }
3634 int32_t requestId = requestIn->mResultExtras.requestId;
3635 const RequestList &repeatRequests = mRepeatingRequests;
3636 // All repeating requests are guaranteed to have same id so only check first quest
3637 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3638 return (firstRequest->mResultExtras.requestId == requestId);
3639}
3640
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003641status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003642 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003643 return clearRepeatingRequestsLocked(lastFrameNumber);
3644
3645}
3646
3647status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003648 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003649 if (lastFrameNumber != NULL) {
3650 *lastFrameNumber = mRepeatingLastFrameNumber;
3651 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003652 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003653 return OK;
3654}
3655
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003656status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003657 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003658 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003659 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003660
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003661 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003662
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003663 // Send errors for all requests pending in the request queue, including
3664 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003665 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003666 if (listener != NULL) {
3667 for (RequestList::iterator it = mRequestQueue.begin();
3668 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003669 // Abort the input buffers for reprocess requests.
3670 if ((*it)->mInputStream != NULL) {
3671 camera3_stream_buffer_t inputBuffer;
3672 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
3673 if (res != OK) {
3674 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3675 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3676 } else {
3677 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3678 if (res != OK) {
3679 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3680 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3681 }
3682 }
3683 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003684 // Set the frame number this request would have had, if it
3685 // had been submitted; this frame number will not be reused.
3686 // The requestId and burstId fields were set when the request was
3687 // submitted originally (in convertMetadataListToRequestListLocked)
3688 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003689 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003690 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003691 }
3692 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003693 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003694
3695 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003696 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003697 if (lastFrameNumber != NULL) {
3698 *lastFrameNumber = mRepeatingLastFrameNumber;
3699 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003700 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003701 return OK;
3702}
3703
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003704status_t Camera3Device::RequestThread::flush() {
3705 ATRACE_CALL();
3706 Mutex::Autolock l(mFlushLock);
3707
Emilian Peev08dd2452017-04-06 16:55:14 +01003708 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003709}
3710
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003711void Camera3Device::RequestThread::setPaused(bool paused) {
3712 Mutex::Autolock l(mPauseLock);
3713 mDoPause = paused;
3714 mDoPauseSignal.signal();
3715}
3716
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003717status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3718 int32_t requestId, nsecs_t timeout) {
3719 Mutex::Autolock l(mLatestRequestMutex);
3720 status_t res;
3721 while (mLatestRequestId != requestId) {
3722 nsecs_t startTime = systemTime();
3723
3724 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3725 if (res != OK) return res;
3726
3727 timeout -= (systemTime() - startTime);
3728 }
3729
3730 return OK;
3731}
3732
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003733void Camera3Device::RequestThread::requestExit() {
3734 // Call parent to set up shutdown
3735 Thread::requestExit();
3736 // The exit from any possible waits
3737 mDoPauseSignal.signal();
3738 mRequestSignal.signal();
3739}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003740
Chien-Yu Chend196d612015-06-22 19:49:01 -07003741
3742/**
3743 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3744 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3745 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3746 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3747 * request.
3748 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003749void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003750 request->mAeTriggerCancelOverride.applyAeLock = false;
3751 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3752
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003753 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003754 return;
3755 }
3756
3757 camera_metadata_entry_t aePrecaptureTrigger =
3758 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3759 if (aePrecaptureTrigger.count > 0 &&
3760 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3761 // Always override CANCEL to IDLE
3762 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3763 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3764 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3765 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3766 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3767
3768 if (mAeLockAvailable == true) {
3769 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3770 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3771 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3772 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3773 request->mAeTriggerCancelOverride.applyAeLock = true;
3774 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3775 }
3776 }
3777 }
3778}
3779
3780/**
3781 * Override result metadata for cancelling AE precapture trigger applied in
3782 * handleAePrecaptureCancelRequest().
3783 */
3784void Camera3Device::overrideResultForPrecaptureCancel(
3785 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3786 if (aeTriggerCancelOverride.applyAeLock) {
3787 // Only devices <= v3.2 should have this override
3788 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3789 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3790 }
3791
3792 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3793 // Only devices <= v3.2 should have this override
3794 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3795 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3796 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3797 }
3798}
3799
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003800void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003801 bool surfaceAbandoned = false;
3802 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003803 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003804 {
3805 Mutex::Autolock l(mRequestLock);
3806 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3807 // repeating requests.
3808 for (const auto& request : mRepeatingRequests) {
3809 for (const auto& s : request->mOutputStreams) {
3810 if (s->isAbandoned()) {
3811 surfaceAbandoned = true;
3812 clearRepeatingRequestsLocked(&lastFrameNumber);
3813 break;
3814 }
3815 }
3816 if (surfaceAbandoned) {
3817 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003818 }
3819 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003820 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003821 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003822
3823 if (listener != NULL && surfaceAbandoned) {
3824 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003825 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003826}
3827
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003828bool Camera3Device::RequestThread::sendRequestsBatch() {
3829 status_t res;
3830 size_t batchSize = mNextRequests.size();
3831 std::vector<camera3_capture_request_t*> requests(batchSize);
3832 uint32_t numRequestProcessed = 0;
3833 for (size_t i = 0; i < batchSize; i++) {
3834 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3835 }
3836
3837 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
3838 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3839
3840 bool triggerRemoveFailed = false;
3841 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3842 for (size_t i = 0; i < numRequestProcessed; i++) {
3843 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3844 nextRequest.submitted = true;
3845
3846
3847 // Update the latest request sent to HAL
3848 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3849 Mutex::Autolock al(mLatestRequestMutex);
3850
3851 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3852 mLatestRequest.acquire(cloned);
3853
3854 sp<Camera3Device> parent = mParent.promote();
3855 if (parent != NULL) {
3856 parent->monitorMetadata(TagMonitor::REQUEST,
3857 nextRequest.halRequest.frame_number,
3858 0, mLatestRequest);
3859 }
3860 }
3861
3862 if (nextRequest.halRequest.settings != NULL) {
3863 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3864 }
3865
3866 if (!triggerRemoveFailed) {
3867 // Remove any previously queued triggers (after unlock)
3868 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3869 if (removeTriggerRes != OK) {
3870 triggerRemoveFailed = true;
3871 triggerFailedRequest = nextRequest;
3872 }
3873 }
3874 }
3875
3876 if (triggerRemoveFailed) {
3877 SET_ERR("RequestThread: Unable to remove triggers "
3878 "(capture request %d, HAL device: %s (%d)",
3879 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3880 cleanUpFailedRequests(/*sendRequestError*/ false);
3881 return false;
3882 }
3883
3884 if (res != OK) {
3885 // Should only get a failure here for malformed requests or device-level
3886 // errors, so consider all errors fatal. Bad metadata failures should
3887 // come through notify.
3888 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3889 mNextRequests[numRequestProcessed].halRequest.frame_number,
3890 strerror(-res), res);
3891 cleanUpFailedRequests(/*sendRequestError*/ false);
3892 return false;
3893 }
3894 return true;
3895}
3896
3897bool Camera3Device::RequestThread::sendRequestsOneByOne() {
3898 status_t res;
3899
3900 for (auto& nextRequest : mNextRequests) {
3901 // Submit request and block until ready for next one
3902 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3903 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
3904
3905 if (res != OK) {
3906 // Should only get a failure here for malformed requests or device-level
3907 // errors, so consider all errors fatal. Bad metadata failures should
3908 // come through notify.
3909 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3910 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3911 res);
3912 cleanUpFailedRequests(/*sendRequestError*/ false);
3913 return false;
3914 }
3915
3916 // Mark that the request has be submitted successfully.
3917 nextRequest.submitted = true;
3918
3919 // Update the latest request sent to HAL
3920 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3921 Mutex::Autolock al(mLatestRequestMutex);
3922
3923 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3924 mLatestRequest.acquire(cloned);
3925
3926 sp<Camera3Device> parent = mParent.promote();
3927 if (parent != NULL) {
3928 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3929 0, mLatestRequest);
3930 }
3931 }
3932
3933 if (nextRequest.halRequest.settings != NULL) {
3934 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3935 }
3936
3937 // Remove any previously queued triggers (after unlock)
3938 res = removeTriggers(mPrevRequest);
3939 if (res != OK) {
3940 SET_ERR("RequestThread: Unable to remove triggers "
3941 "(capture request %d, HAL device: %s (%d)",
3942 nextRequest.halRequest.frame_number, strerror(-res), res);
3943 cleanUpFailedRequests(/*sendRequestError*/ false);
3944 return false;
3945 }
3946 }
3947 return true;
3948}
3949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003950bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003951 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003952 status_t res;
3953
3954 // Handle paused state.
3955 if (waitIfPaused()) {
3956 return true;
3957 }
3958
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003959 // Wait for the next batch of requests.
3960 waitForNextRequestBatch();
3961 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003962 return true;
3963 }
3964
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003965 // Get the latest request ID, if any
3966 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003967 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003968 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003969 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003970 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003971 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003972 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3973 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003974 }
3975
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003976 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003977 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003978 if (res == TIMED_OUT) {
3979 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003980 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003981 // Check if any stream is abandoned.
3982 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003983 return true;
3984 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003985 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003986 return false;
3987 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003988
Zhijun Hecc27e112013-10-03 16:12:43 -07003989 // Inform waitUntilRequestProcessed thread of a new request ID
3990 {
3991 Mutex::Autolock al(mLatestRequestMutex);
3992
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003993 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003994 mLatestRequestSignal.signal();
3995 }
3996
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003997 // Submit a batch of requests to HAL.
3998 // Use flush lock only when submitting multilple requests in a batch.
3999 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4000 // which may take a long time to finish so synchronizing flush() and
4001 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4002 // For now, only synchronize for high speed recording and we should figure something out for
4003 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004004 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004005
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004006 if (useFlushLock) {
4007 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004008 }
4009
Zhijun Hef0645c12016-08-02 00:58:11 -07004010 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004011 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004012
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004013 bool submitRequestSuccess = false;
4014 if (mInterface->supportBatchRequest()) {
4015 submitRequestSuccess = sendRequestsBatch();
4016 } else {
4017 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004018 }
4019
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004020 if (useFlushLock) {
4021 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004022 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004023
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004024 // Unset as current request
4025 {
4026 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004027 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004028 }
4029
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004030 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004031}
4032
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004033status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004034 ATRACE_CALL();
4035
Shuzhen Wang4a472662017-02-26 23:29:04 -08004036 for (size_t i = 0; i < mNextRequests.size(); i++) {
4037 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004038 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4039 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4040 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4041
4042 // Prepare a request to HAL
4043 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4044
4045 // Insert any queued triggers (before metadata is locked)
4046 status_t res = insertTriggers(captureRequest);
4047
4048 if (res < 0) {
4049 SET_ERR("RequestThread: Unable to insert triggers "
4050 "(capture request %d, HAL device: %s (%d)",
4051 halRequest->frame_number, strerror(-res), res);
4052 return INVALID_OPERATION;
4053 }
4054 int triggerCount = res;
4055 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4056 mPrevTriggers = triggerCount;
4057
4058 // If the request is the same as last, or we had triggers last time
4059 if (mPrevRequest != captureRequest || triggersMixedIn) {
4060 /**
4061 * HAL workaround:
4062 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4063 */
4064 res = addDummyTriggerIds(captureRequest);
4065 if (res != OK) {
4066 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4067 "(capture request %d, HAL device: %s (%d)",
4068 halRequest->frame_number, strerror(-res), res);
4069 return INVALID_OPERATION;
4070 }
4071
4072 /**
4073 * The request should be presorted so accesses in HAL
4074 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4075 */
4076 captureRequest->mSettings.sort();
4077 halRequest->settings = captureRequest->mSettings.getAndLock();
4078 mPrevRequest = captureRequest;
4079 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4080
4081 IF_ALOGV() {
4082 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4083 find_camera_metadata_ro_entry(
4084 halRequest->settings,
4085 ANDROID_CONTROL_AF_TRIGGER,
4086 &e
4087 );
4088 if (e.count > 0) {
4089 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4090 __FUNCTION__,
4091 halRequest->frame_number,
4092 e.data.u8[0]);
4093 }
4094 }
4095 } else {
4096 // leave request.settings NULL to indicate 'reuse latest given'
4097 ALOGVV("%s: Request settings are REUSED",
4098 __FUNCTION__);
4099 }
4100
4101 uint32_t totalNumBuffers = 0;
4102
4103 // Fill in buffers
4104 if (captureRequest->mInputStream != NULL) {
4105 halRequest->input_buffer = &captureRequest->mInputBuffer;
4106 totalNumBuffers += 1;
4107 } else {
4108 halRequest->input_buffer = NULL;
4109 }
4110
4111 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4112 captureRequest->mOutputStreams.size());
4113 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004114 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4115 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004116
4117 // Prepare video buffers for high speed recording on the first video request.
4118 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4119 // Only try to prepare video stream on the first video request.
4120 mPrepareVideoStream = false;
4121
4122 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4123 while (res == NOT_ENOUGH_DATA) {
4124 res = outputStream->prepareNextBuffer();
4125 }
4126 if (res != OK) {
4127 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4128 __FUNCTION__, strerror(-res), res);
4129 outputStream->cancelPrepare();
4130 }
4131 }
4132
Shuzhen Wang4a472662017-02-26 23:29:04 -08004133 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4134 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004135 if (res != OK) {
4136 // Can't get output buffer from gralloc queue - this could be due to
4137 // abandoned queue or other consumer misbehavior, so not a fatal
4138 // error
4139 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4140 " %s (%d)", strerror(-res), res);
4141
4142 return TIMED_OUT;
4143 }
4144 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004145
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004146 }
4147 totalNumBuffers += halRequest->num_output_buffers;
4148
4149 // Log request in the in-flight queue
4150 sp<Camera3Device> parent = mParent.promote();
4151 if (parent == NULL) {
4152 // Should not happen, and nowhere to send errors to, so just log it
4153 CLOGE("RequestThread: Parent is gone");
4154 return INVALID_OPERATION;
4155 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004156
4157 // If this request list is for constrained high speed recording (not
4158 // preview), and the current request is not the last one in the batch,
4159 // do not send callback to the app.
4160 bool hasCallback = true;
4161 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4162 hasCallback = false;
4163 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004164 res = parent->registerInFlight(halRequest->frame_number,
4165 totalNumBuffers, captureRequest->mResultExtras,
4166 /*hasInput*/halRequest->input_buffer != NULL,
Shuzhen Wang4a472662017-02-26 23:29:04 -08004167 captureRequest->mAeTriggerCancelOverride,
4168 hasCallback);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004169 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4170 ", burstId = %" PRId32 ".",
4171 __FUNCTION__,
4172 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4173 captureRequest->mResultExtras.burstId);
4174 if (res != OK) {
4175 SET_ERR("RequestThread: Unable to register new in-flight request:"
4176 " %s (%d)", strerror(-res), res);
4177 return INVALID_OPERATION;
4178 }
4179 }
4180
4181 return OK;
4182}
4183
Igor Murashkin1e479c02013-09-06 16:55:14 -07004184CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4185 Mutex::Autolock al(mLatestRequestMutex);
4186
4187 ALOGV("RequestThread::%s", __FUNCTION__);
4188
4189 return mLatestRequest;
4190}
4191
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004192bool Camera3Device::RequestThread::isStreamPending(
4193 sp<Camera3StreamInterface>& stream) {
4194 Mutex::Autolock l(mRequestLock);
4195
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004196 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004197 if (!nextRequest.submitted) {
4198 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4199 if (stream == s) return true;
4200 }
4201 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004202 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004203 }
4204
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004205 for (const auto& request : mRequestQueue) {
4206 for (const auto& s : request->mOutputStreams) {
4207 if (stream == s) return true;
4208 }
4209 if (stream == request->mInputStream) return true;
4210 }
4211
4212 for (const auto& request : mRepeatingRequests) {
4213 for (const auto& s : request->mOutputStreams) {
4214 if (stream == s) return true;
4215 }
4216 if (stream == request->mInputStream) return true;
4217 }
4218
4219 return false;
4220}
Jianing Weicb0652e2014-03-12 18:29:36 -07004221
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004222void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4223 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004224 return;
4225 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004226
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004227 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004228 // Skip the ones that have been submitted successfully.
4229 if (nextRequest.submitted) {
4230 continue;
4231 }
4232
4233 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4234 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4235 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4236
4237 if (halRequest->settings != NULL) {
4238 captureRequest->mSettings.unlock(halRequest->settings);
4239 }
4240
4241 if (captureRequest->mInputStream != NULL) {
4242 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4243 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4244 }
4245
4246 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4247 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4248 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4249 }
4250
4251 if (sendRequestError) {
4252 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004253 sp<NotificationListener> listener = mListener.promote();
4254 if (listener != NULL) {
4255 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004256 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004257 captureRequest->mResultExtras);
4258 }
4259 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004260
4261 // Remove yet-to-be submitted inflight request from inflightMap
4262 {
4263 sp<Camera3Device> parent = mParent.promote();
4264 if (parent != NULL) {
4265 Mutex::Autolock l(parent->mInFlightLock);
4266 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4267 if (idx >= 0) {
4268 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4269 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4270 parent->removeInFlightMapEntryLocked(idx);
4271 }
4272 }
4273 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004274 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004275
4276 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004277 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004278}
4279
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004280void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004281 // Optimized a bit for the simple steady-state case (single repeating
4282 // request), to avoid putting that request in the queue temporarily.
4283 Mutex::Autolock l(mRequestLock);
4284
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004285 assert(mNextRequests.empty());
4286
4287 NextRequest nextRequest;
4288 nextRequest.captureRequest = waitForNextRequestLocked();
4289 if (nextRequest.captureRequest == nullptr) {
4290 return;
4291 }
4292
4293 nextRequest.halRequest = camera3_capture_request_t();
4294 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004295 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004296
4297 // Wait for additional requests
4298 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4299
4300 for (size_t i = 1; i < batchSize; i++) {
4301 NextRequest additionalRequest;
4302 additionalRequest.captureRequest = waitForNextRequestLocked();
4303 if (additionalRequest.captureRequest == nullptr) {
4304 break;
4305 }
4306
4307 additionalRequest.halRequest = camera3_capture_request_t();
4308 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004309 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004310 }
4311
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004312 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004313 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004314 mNextRequests.size(), batchSize);
4315 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004316 }
4317
4318 return;
4319}
4320
4321sp<Camera3Device::CaptureRequest>
4322 Camera3Device::RequestThread::waitForNextRequestLocked() {
4323 status_t res;
4324 sp<CaptureRequest> nextRequest;
4325
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004326 while (mRequestQueue.empty()) {
4327 if (!mRepeatingRequests.empty()) {
4328 // Always atomically enqueue all requests in a repeating request
4329 // list. Guarantees a complete in-sequence set of captures to
4330 // application.
4331 const RequestList &requests = mRepeatingRequests;
4332 RequestList::const_iterator firstRequest =
4333 requests.begin();
4334 nextRequest = *firstRequest;
4335 mRequestQueue.insert(mRequestQueue.end(),
4336 ++firstRequest,
4337 requests.end());
4338 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004339
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004340 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004341
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004342 break;
4343 }
4344
4345 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4346
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004347 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4348 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004349 Mutex::Autolock pl(mPauseLock);
4350 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004351 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004352 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004353 // Let the tracker know
4354 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4355 if (statusTracker != 0) {
4356 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4357 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004358 }
4359 // Stop waiting for now and let thread management happen
4360 return NULL;
4361 }
4362 }
4363
4364 if (nextRequest == NULL) {
4365 // Don't have a repeating request already in hand, so queue
4366 // must have an entry now.
4367 RequestList::iterator firstRequest =
4368 mRequestQueue.begin();
4369 nextRequest = *firstRequest;
4370 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004371 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4372 sp<NotificationListener> listener = mListener.promote();
4373 if (listener != NULL) {
4374 listener->notifyRequestQueueEmpty();
4375 }
4376 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004377 }
4378
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004379 // In case we've been unpaused by setPaused clearing mDoPause, need to
4380 // update internal pause state (capture/setRepeatingRequest unpause
4381 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004382 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004383 if (mPaused) {
4384 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4385 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4386 if (statusTracker != 0) {
4387 statusTracker->markComponentActive(mStatusId);
4388 }
4389 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004390 mPaused = false;
4391
4392 // Check if we've reconfigured since last time, and reset the preview
4393 // request if so. Can't use 'NULL request == repeat' across configure calls.
4394 if (mReconfigured) {
4395 mPrevRequest.clear();
4396 mReconfigured = false;
4397 }
4398
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004399 if (nextRequest != NULL) {
4400 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004401 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4402 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004403
4404 // Since RequestThread::clear() removes buffers from the input stream,
4405 // get the right buffer here before unlocking mRequestLock
4406 if (nextRequest->mInputStream != NULL) {
4407 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4408 if (res != OK) {
4409 // Can't get input buffer from gralloc queue - this could be due to
4410 // disconnected queue or other producer misbehavior, so not a fatal
4411 // error
4412 ALOGE("%s: Can't get input buffer, skipping request:"
4413 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004414
4415 sp<NotificationListener> listener = mListener.promote();
4416 if (listener != NULL) {
4417 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004418 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004419 nextRequest->mResultExtras);
4420 }
4421 return NULL;
4422 }
4423 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004424 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004425
4426 handleAePrecaptureCancelRequest(nextRequest);
4427
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004428 return nextRequest;
4429}
4430
4431bool Camera3Device::RequestThread::waitIfPaused() {
4432 status_t res;
4433 Mutex::Autolock l(mPauseLock);
4434 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004435 if (mPaused == false) {
4436 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004437 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4438 // Let the tracker know
4439 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4440 if (statusTracker != 0) {
4441 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4442 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004443 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004445 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004446 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004447 return true;
4448 }
4449 }
4450 // We don't set mPaused to false here, because waitForNextRequest needs
4451 // to further manage the paused state in case of starvation.
4452 return false;
4453}
4454
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004455void Camera3Device::RequestThread::unpauseForNewRequests() {
4456 // With work to do, mark thread as unpaused.
4457 // If paused by request (setPaused), don't resume, to avoid
4458 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004459 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004460 Mutex::Autolock p(mPauseLock);
4461 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004462 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4463 if (mPaused) {
4464 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4465 if (statusTracker != 0) {
4466 statusTracker->markComponentActive(mStatusId);
4467 }
4468 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004469 mPaused = false;
4470 }
4471}
4472
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004473void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4474 sp<Camera3Device> parent = mParent.promote();
4475 if (parent != NULL) {
4476 va_list args;
4477 va_start(args, fmt);
4478
4479 parent->setErrorStateV(fmt, args);
4480
4481 va_end(args);
4482 }
4483}
4484
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004485status_t Camera3Device::RequestThread::insertTriggers(
4486 const sp<CaptureRequest> &request) {
4487
4488 Mutex::Autolock al(mTriggerMutex);
4489
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004490 sp<Camera3Device> parent = mParent.promote();
4491 if (parent == NULL) {
4492 CLOGE("RequestThread: Parent is gone");
4493 return DEAD_OBJECT;
4494 }
4495
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004496 CameraMetadata &metadata = request->mSettings;
4497 size_t count = mTriggerMap.size();
4498
4499 for (size_t i = 0; i < count; ++i) {
4500 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004501 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004502
4503 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4504 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4505 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004506 if (isAeTrigger) {
4507 request->mResultExtras.precaptureTriggerId = triggerId;
4508 mCurrentPreCaptureTriggerId = triggerId;
4509 } else {
4510 request->mResultExtras.afTriggerId = triggerId;
4511 mCurrentAfTriggerId = triggerId;
4512 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004513 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
4514 continue; // Trigger ID tag is deprecated since device HAL 3.2
4515 }
4516 }
4517
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004518 camera_metadata_entry entry = metadata.find(tag);
4519
4520 if (entry.count > 0) {
4521 /**
4522 * Already has an entry for this trigger in the request.
4523 * Rewrite it with our requested trigger value.
4524 */
4525 RequestTrigger oldTrigger = trigger;
4526
4527 oldTrigger.entryValue = entry.data.u8[0];
4528
4529 mTriggerReplacedMap.add(tag, oldTrigger);
4530 } else {
4531 /**
4532 * More typical, no trigger entry, so we just add it
4533 */
4534 mTriggerRemovedMap.add(tag, trigger);
4535 }
4536
4537 status_t res;
4538
4539 switch (trigger.getTagType()) {
4540 case TYPE_BYTE: {
4541 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4542 res = metadata.update(tag,
4543 &entryValue,
4544 /*count*/1);
4545 break;
4546 }
4547 case TYPE_INT32:
4548 res = metadata.update(tag,
4549 &trigger.entryValue,
4550 /*count*/1);
4551 break;
4552 default:
4553 ALOGE("%s: Type not supported: 0x%x",
4554 __FUNCTION__,
4555 trigger.getTagType());
4556 return INVALID_OPERATION;
4557 }
4558
4559 if (res != OK) {
4560 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4561 ", value %d", __FUNCTION__, trigger.getTagName(),
4562 trigger.entryValue);
4563 return res;
4564 }
4565
4566 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4567 trigger.getTagName(),
4568 trigger.entryValue);
4569 }
4570
4571 mTriggerMap.clear();
4572
4573 return count;
4574}
4575
4576status_t Camera3Device::RequestThread::removeTriggers(
4577 const sp<CaptureRequest> &request) {
4578 Mutex::Autolock al(mTriggerMutex);
4579
4580 CameraMetadata &metadata = request->mSettings;
4581
4582 /**
4583 * Replace all old entries with their old values.
4584 */
4585 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4586 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4587
4588 status_t res;
4589
4590 uint32_t tag = trigger.metadataTag;
4591 switch (trigger.getTagType()) {
4592 case TYPE_BYTE: {
4593 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4594 res = metadata.update(tag,
4595 &entryValue,
4596 /*count*/1);
4597 break;
4598 }
4599 case TYPE_INT32:
4600 res = metadata.update(tag,
4601 &trigger.entryValue,
4602 /*count*/1);
4603 break;
4604 default:
4605 ALOGE("%s: Type not supported: 0x%x",
4606 __FUNCTION__,
4607 trigger.getTagType());
4608 return INVALID_OPERATION;
4609 }
4610
4611 if (res != OK) {
4612 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4613 ", trigger value %d", __FUNCTION__,
4614 trigger.getTagName(), trigger.entryValue);
4615 return res;
4616 }
4617 }
4618 mTriggerReplacedMap.clear();
4619
4620 /**
4621 * Remove all new entries.
4622 */
4623 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4624 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4625 status_t res = metadata.erase(trigger.metadataTag);
4626
4627 if (res != OK) {
4628 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4629 ", trigger value %d", __FUNCTION__,
4630 trigger.getTagName(), trigger.entryValue);
4631 return res;
4632 }
4633 }
4634 mTriggerRemovedMap.clear();
4635
4636 return OK;
4637}
4638
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004639status_t Camera3Device::RequestThread::addDummyTriggerIds(
4640 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004641 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004642 static const int32_t dummyTriggerId = 1;
4643 status_t res;
4644
4645 CameraMetadata &metadata = request->mSettings;
4646
4647 // If AF trigger is active, insert a dummy AF trigger ID if none already
4648 // exists
4649 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4650 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4651 if (afTrigger.count > 0 &&
4652 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4653 afId.count == 0) {
4654 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4655 if (res != OK) return res;
4656 }
4657
4658 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4659 // if none already exists
4660 camera_metadata_entry pcTrigger =
4661 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4662 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4663 if (pcTrigger.count > 0 &&
4664 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4665 pcId.count == 0) {
4666 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4667 &dummyTriggerId, 1);
4668 if (res != OK) return res;
4669 }
4670
4671 return OK;
4672}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004673
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004674/**
4675 * PreparerThread inner class methods
4676 */
4677
4678Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004679 Thread(/*canCallJava*/false), mListener(nullptr),
4680 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004681}
4682
4683Camera3Device::PreparerThread::~PreparerThread() {
4684 Thread::requestExitAndWait();
4685 if (mCurrentStream != nullptr) {
4686 mCurrentStream->cancelPrepare();
4687 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4688 mCurrentStream.clear();
4689 }
4690 clear();
4691}
4692
Ruben Brunkc78ac262015-08-13 17:58:46 -07004693status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004694 status_t res;
4695
4696 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004697 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004698
Ruben Brunkc78ac262015-08-13 17:58:46 -07004699 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004700 if (res == OK) {
4701 // No preparation needed, fire listener right off
4702 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004703 if (listener != NULL) {
4704 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004705 }
4706 return OK;
4707 } else if (res != NOT_ENOUGH_DATA) {
4708 return res;
4709 }
4710
4711 // Need to prepare, start up thread if necessary
4712 if (!mActive) {
4713 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4714 // isn't running
4715 Thread::requestExitAndWait();
4716 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4717 if (res != OK) {
4718 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004719 if (listener != NULL) {
4720 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004721 }
4722 return res;
4723 }
4724 mCancelNow = false;
4725 mActive = true;
4726 ALOGV("%s: Preparer stream started", __FUNCTION__);
4727 }
4728
4729 // queue up the work
4730 mPendingStreams.push_back(stream);
4731 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4732
4733 return OK;
4734}
4735
4736status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004737 Mutex::Autolock l(mLock);
4738
4739 for (const auto& stream : mPendingStreams) {
4740 stream->cancelPrepare();
4741 }
4742 mPendingStreams.clear();
4743 mCancelNow = true;
4744
4745 return OK;
4746}
4747
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004748void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004749 Mutex::Autolock l(mLock);
4750 mListener = listener;
4751}
4752
4753bool Camera3Device::PreparerThread::threadLoop() {
4754 status_t res;
4755 {
4756 Mutex::Autolock l(mLock);
4757 if (mCurrentStream == nullptr) {
4758 // End thread if done with work
4759 if (mPendingStreams.empty()) {
4760 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4761 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4762 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4763 mActive = false;
4764 return false;
4765 }
4766
4767 // Get next stream to prepare
4768 auto it = mPendingStreams.begin();
4769 mCurrentStream = *it;
4770 mPendingStreams.erase(it);
4771 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4772 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4773 } else if (mCancelNow) {
4774 mCurrentStream->cancelPrepare();
4775 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4776 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4777 mCurrentStream.clear();
4778 mCancelNow = false;
4779 return true;
4780 }
4781 }
4782
4783 res = mCurrentStream->prepareNextBuffer();
4784 if (res == NOT_ENOUGH_DATA) return true;
4785 if (res != OK) {
4786 // Something bad happened; try to recover by cancelling prepare and
4787 // signalling listener anyway
4788 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4789 mCurrentStream->getId(), res, strerror(-res));
4790 mCurrentStream->cancelPrepare();
4791 }
4792
4793 // This stream has finished, notify listener
4794 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004795 sp<NotificationListener> listener = mListener.promote();
4796 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004797 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4798 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004799 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004800 }
4801
4802 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4803 mCurrentStream.clear();
4804
4805 return true;
4806}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004807
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004808/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004809 * Static callback forwarding methods from HAL to instance
4810 */
4811
4812void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4813 const camera3_capture_result *result) {
4814 Camera3Device *d =
4815 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004816
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004817 d->processCaptureResult(result);
4818}
4819
4820void Camera3Device::sNotify(const camera3_callback_ops *cb,
4821 const camera3_notify_msg *msg) {
4822 Camera3Device *d =
4823 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4824 d->notify(msg);
4825}
4826
4827}; // namespace android