blob: e7b0c6b00274800c9f6a2c802fb991563fc571ea [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 Talvala1a2f15d2017-03-23 15:37:22 -070047#include <grallocusage/GrallocUsageConversion.h>
48
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080049#include <android/hardware/camera2/ICameraDeviceUser.h>
50
Igor Murashkinff3e31d2013-10-23 16:40:06 -070051#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070052#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070053#include "device3/Camera3Device.h"
54#include "device3/Camera3OutputStream.h"
55#include "device3/Camera3InputStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070056#include "device3/Camera3DummyStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070057#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070058#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080059
60using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080061using namespace android::hardware::camera;
62using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063
64namespace android {
65
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080066Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080067 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080068 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070069 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070070 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070071 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070072 mUsePartialResult(false),
73 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080074 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070075 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070076 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070077 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070078 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070079 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080{
81 ATRACE_CALL();
82 camera3_callback_ops::notify = &sNotify;
83 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080084 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085}
86
87Camera3Device::~Camera3Device()
88{
89 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080090 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091 disconnect();
92}
93
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080094const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080095 return mId;
96}
97
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080098/**
99 * CameraDeviceBase interface
100 */
101
Yin-Chia Yehe074a932015-01-30 10:29:02 -0800102status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103{
104 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700105 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800106 Mutex::Autolock l(mLock);
107
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800108 ALOGV("%s: Initializing device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800109 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700110 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800111 return INVALID_OPERATION;
112 }
113
114 /** Open HAL device */
115
116 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117
118 camera3_device_t *device;
119
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120 ATRACE_BEGIN("CameraHal::open");
121 res = module->open(mId.string(),
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800122 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800123 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124
125 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700126 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800127 return res;
128 }
129
130 /** Cross-check device version */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800131 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700132 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700133 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800134 CAMERA_DEVICE_API_VERSION_3_2,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 device->common.version);
136 device->common.close(&device->common);
137 return BAD_VALUE;
138 }
139
140 camera_info info;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800141 res = module->getCameraInfo(atoi(mId), &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 if (res != OK) return res;
143
144 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700145 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
146 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700147 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 device->common.close(&device->common);
149 return BAD_VALUE;
150 }
151
152 /** Initialize device with callback functions */
153
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800154 ATRACE_BEGIN("CameraHal::initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700156 ATRACE_END();
157
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700159 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
160 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800161 device->common.close(&device->common);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800162 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800163 }
164
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800165 /** Everything is good to go */
166
167 mDeviceVersion = device->common.version;
168 mDeviceInfo = info.static_camera_characteristics;
169 mInterface = std::make_unique<HalInterface>(device);
170
171 return initializeCommonLocked();
172}
173
174status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
175 ATRACE_CALL();
176 Mutex::Autolock il(mInterfaceLock);
177 Mutex::Autolock l(mLock);
178
179 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
180 if (mStatus != STATUS_UNINITIALIZED) {
181 CLOGE("Already initialized!");
182 return INVALID_OPERATION;
183 }
184 if (manager == nullptr) return INVALID_OPERATION;
185
186 sp<ICameraDeviceSession> session;
187 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800188 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800189 /*out*/ &session);
190 ATRACE_END();
191 if (res != OK) {
192 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
193 return res;
194 }
195
Steven Moreland5ff9c912017-03-09 23:13:00 -0800196 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800197 if (res != OK) {
198 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
199 session->close();
200 return res;
201 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800202
203 // TODO: camera service will absorb 3_2/3_3/3_4 differences in the future
204 // for now use 3_4 to keep legacy devices working
205 mDeviceVersion = CAMERA_DEVICE_API_VERSION_3_4;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800206 mInterface = std::make_unique<HalInterface>(session);
207
208 return initializeCommonLocked();
209}
210
211status_t Camera3Device::initializeCommonLocked() {
212
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 /** Start up status tracker thread */
214 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800215 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700216 if (res != OK) {
217 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
218 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800219 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700220 mStatusTracker.clear();
221 return res;
222 }
223
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700224 /** Register in-flight map to the status tracker */
225 mInFlightStatusId = mStatusTracker->addComponent();
226
Zhijun He125684a2015-12-26 15:07:30 -0800227 /** Create buffer manager */
228 mBufferManager = new Camera3BufferManager();
229
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700230 bool aeLockAvailable = false;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800231 camera_metadata_entry aeLockAvailableEntry = mDeviceInfo.find(
232 ANDROID_CONTROL_AE_LOCK_AVAILABLE);
233 if (aeLockAvailableEntry.count > 0) {
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700234 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
235 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
236 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800237
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700238 /** Start up request queue thread */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800239 mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get(), mDeviceVersion,
240 aeLockAvailable);
241 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800242 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700243 SET_ERR_L("Unable to start request queue thread: %s (%d)",
244 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800245 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800246 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247 return res;
248 }
249
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700250 mPreparerThread = new PreparerThread();
251
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700252 // Determine whether we need to derive sensitivity boost values for older devices.
253 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
254 // be listed (as the default value 100)
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800255 if (mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700256 mDerivePostRawSensKey = true;
257 }
258
Ruben Brunk183f0562015-08-12 12:55:02 -0700259 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700261 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700262 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700263 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800264
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800265 // Measure the clock domain offset between camera and video/hw_composer
266 camera_metadata_entry timestampSource =
267 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
268 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
269 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
270 mTimestampOffset = getMonoToBoottimeOffset();
271 }
272
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700273 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700274 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
275 camera_metadata_entry partialResultsCount =
276 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
277 if (partialResultsCount.count > 0) {
278 mNumPartialResults = partialResultsCount.data.i32[0];
279 mUsePartialResult = (mNumPartialResults > 1);
280 }
281 } else {
282 camera_metadata_entry partialResultsQuirk =
283 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
284 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
285 mUsePartialResult = true;
286 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700287 }
288
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700289 camera_metadata_entry configs =
290 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
291 for (uint32_t i = 0; i < configs.count; i += 4) {
292 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
293 configs.data.i32[i + 3] ==
294 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
295 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
296 configs.data.i32[i + 2]));
297 }
298 }
299
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300 return OK;
301}
302
303status_t Camera3Device::disconnect() {
304 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700305 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800306
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700307 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700309 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311 {
312 Mutex::Autolock l(mLock);
313 if (mStatus == STATUS_UNINITIALIZED) return res;
314
315 if (mStatus == STATUS_ACTIVE ||
316 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
317 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700318 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700319 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700320 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700321 } else {
322 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
323 if (res != OK) {
324 SET_ERR_L("Timeout waiting for HAL to drain");
325 // Continue to close device even in case of error
326 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700327 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800328 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700330 if (mStatus == STATUS_ERROR) {
331 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700332 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700333
334 if (mStatusTracker != NULL) {
335 mStatusTracker->requestExit();
336 }
337
338 if (mRequestThread != NULL) {
339 mRequestThread->requestExit();
340 }
341
342 mOutputStreams.clear();
343 mInputStream.clear();
344 }
345
346 // Joining done without holding mLock, otherwise deadlocks may ensue
347 // as the threads try to access parent state
348 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
349 // HAL may be in a bad state, so waiting for request thread
350 // (which may be stuck in the HAL processCaptureRequest call)
351 // could be dangerous.
352 mRequestThread->join();
353 }
354
355 if (mStatusTracker != NULL) {
356 mStatusTracker->join();
357 }
358
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800359 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700360 {
361 Mutex::Autolock l(mLock);
362
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800363 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700364 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800365 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800366
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800367 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700368 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800369
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700370 // Call close without internal mutex held, as the HAL close may need to
371 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800372 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700373
374 {
375 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800376 mInterface->clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700377 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700378 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800379
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700380 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700381 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800382}
383
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700384// For dumping/debugging only -
385// try to acquire a lock a few times, eventually give up to proceed with
386// debug/dump operations
387bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
388 bool gotLock = false;
389 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
390 if (lock.tryLock() == NO_ERROR) {
391 gotLock = true;
392 break;
393 } else {
394 usleep(kDumpSleepDuration);
395 }
396 }
397 return gotLock;
398}
399
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700400Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
401 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
402 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
403 const int STREAM_CONFIGURATION_SIZE = 4;
404 const int STREAM_FORMAT_OFFSET = 0;
405 const int STREAM_WIDTH_OFFSET = 1;
406 const int STREAM_HEIGHT_OFFSET = 2;
407 const int STREAM_IS_INPUT_OFFSET = 3;
408 camera_metadata_ro_entry_t availableStreamConfigs =
409 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
410 if (availableStreamConfigs.count == 0 ||
411 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
412 return Size(0, 0);
413 }
414
415 // Get max jpeg size (area-wise).
416 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
417 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
418 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
419 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
420 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
421 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
422 && format == HAL_PIXEL_FORMAT_BLOB &&
423 (width * height > maxJpegWidth * maxJpegHeight)) {
424 maxJpegWidth = width;
425 maxJpegHeight = height;
426 }
427 }
428 } else {
429 camera_metadata_ro_entry availableJpegSizes =
430 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
431 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
432 return Size(0, 0);
433 }
434
435 // Get max jpeg size (area-wise).
436 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
437 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
438 > (maxJpegWidth * maxJpegHeight)) {
439 maxJpegWidth = availableJpegSizes.data.i32[i];
440 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
441 }
442 }
443 }
444 return Size(maxJpegWidth, maxJpegHeight);
445}
446
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800447nsecs_t Camera3Device::getMonoToBoottimeOffset() {
448 // try three times to get the clock offset, choose the one
449 // with the minimum gap in measurements.
450 const int tries = 3;
451 nsecs_t bestGap, measured;
452 for (int i = 0; i < tries; ++i) {
453 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
454 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
455 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
456 const nsecs_t gap = tmono2 - tmono;
457 if (i == 0 || gap < bestGap) {
458 bestGap = gap;
459 measured = tbase - ((tmono + tmono2) >> 1);
460 }
461 }
462 return measured;
463}
464
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700465/**
466 * Map Android N dataspace definitions back to Android M definitions, for
467 * use with HALv3.3 or older.
468 *
469 * Only map where correspondences exist, and otherwise preserve the value.
470 */
471android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
472 switch (dataSpace) {
473 case HAL_DATASPACE_V0_SRGB_LINEAR:
474 return HAL_DATASPACE_SRGB_LINEAR;
475 case HAL_DATASPACE_V0_SRGB:
476 return HAL_DATASPACE_SRGB;
477 case HAL_DATASPACE_V0_JFIF:
478 return HAL_DATASPACE_JFIF;
479 case HAL_DATASPACE_V0_BT601_625:
480 return HAL_DATASPACE_BT601_625;
481 case HAL_DATASPACE_V0_BT601_525:
482 return HAL_DATASPACE_BT601_525;
483 case HAL_DATASPACE_V0_BT709:
484 return HAL_DATASPACE_BT709;
485 default:
486 return dataSpace;
487 }
488}
489
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800490hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
491 int frameworkFormat) {
492 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
493}
494
495DataspaceFlags Camera3Device::mapToHidlDataspace(
496 android_dataspace dataSpace) {
497 return dataSpace;
498}
499
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800500StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
501 switch (rotation) {
502 case CAMERA3_STREAM_ROTATION_0:
503 return StreamRotation::ROTATION_0;
504 case CAMERA3_STREAM_ROTATION_90:
505 return StreamRotation::ROTATION_90;
506 case CAMERA3_STREAM_ROTATION_180:
507 return StreamRotation::ROTATION_180;
508 case CAMERA3_STREAM_ROTATION_270:
509 return StreamRotation::ROTATION_270;
510 }
511 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
512 return StreamRotation::ROTATION_0;
513}
514
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800515status_t Camera3Device::mapToStreamConfigurationMode(
516 camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
517 if (mode == nullptr) return BAD_VALUE;
518 if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
519 switch(operationMode) {
520 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
521 *mode = StreamConfigurationMode::NORMAL_MODE;
522 break;
523 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
524 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
525 break;
526 default:
527 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
528 return BAD_VALUE;
529 }
530 } else {
531 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800532 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800533 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800534}
535
536camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
537 switch (status) {
538 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
539 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
540 }
541 return CAMERA3_BUFFER_STATUS_ERROR;
542}
543
544int Camera3Device::mapToFrameworkFormat(
545 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
546 return static_cast<uint32_t>(pixelFormat);
547}
548
Zhijun Hef7da0962014-04-24 13:27:56 -0700549ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700550 // Get max jpeg size (area-wise).
551 Size maxJpegResolution = getMaxJpegResolution();
552 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800553 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
554 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700555 return BAD_VALUE;
556 }
557
Zhijun Hef7da0962014-04-24 13:27:56 -0700558 // Get max jpeg buffer size
559 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700560 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
561 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800562 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
563 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700564 return BAD_VALUE;
565 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700566 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800567 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700568
569 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700570 float scaleFactor = ((float) (width * height)) /
571 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800572 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
573 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700574 if (jpegBufferSize > maxJpegBufferSize) {
575 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700576 }
577
578 return jpegBufferSize;
579}
580
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700581ssize_t Camera3Device::getPointCloudBufferSize() const {
582 const int FLOATS_PER_POINT=4;
583 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
584 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800585 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
586 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700587 return BAD_VALUE;
588 }
589 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
590 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
591 return maxBytesForPointCloud;
592}
593
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800594ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800595 const int PER_CONFIGURATION_SIZE = 3;
596 const int WIDTH_OFFSET = 0;
597 const int HEIGHT_OFFSET = 1;
598 const int SIZE_OFFSET = 2;
599 camera_metadata_ro_entry rawOpaqueSizes =
600 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800601 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800602 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800603 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
604 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800605 return BAD_VALUE;
606 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700607
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800608 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
609 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
610 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
611 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
612 }
613 }
614
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800615 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
616 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800617 return BAD_VALUE;
618}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700619
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800620status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
621 ATRACE_CALL();
622 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700623
624 // Try to lock, but continue in case of failure (to avoid blocking in
625 // deadlocks)
626 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
627 bool gotLock = tryLockSpinRightRound(mLock);
628
629 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800630 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
631 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700632 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800633 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
634 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700635
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800636 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700637
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800638 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700639 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800640 int n = args.size();
641 for (int i = 0; i < n; i++) {
642 if (args[i] == templatesOption) {
643 dumpTemplates = true;
644 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700645 if (args[i] == monitorOption) {
646 if (i + 1 < n) {
647 String8 monitorTags = String8(args[i + 1]);
648 if (monitorTags == "off") {
649 mTagMonitor.disableMonitoring();
650 } else {
651 mTagMonitor.parseTagsToMonitor(monitorTags);
652 }
653 } else {
654 mTagMonitor.disableMonitoring();
655 }
656 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800657 }
658
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800661 const char *status =
662 mStatus == STATUS_ERROR ? "ERROR" :
663 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
665 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 mStatus == STATUS_ACTIVE ? "ACTIVE" :
667 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700670 if (mStatus == STATUS_ERROR) {
671 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
672 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800673 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800674 const char *mode =
675 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
676 mOperatingMode == static_cast<int>(
677 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
678 "CUSTOM";
679 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680
681 if (mInputStream != NULL) {
682 write(fd, lines.string(), lines.size());
683 mInputStream->dump(fd, args);
684 } else {
685 lines.appendFormat(" No input stream.\n");
686 write(fd, lines.string(), lines.size());
687 }
688 for (size_t i = 0; i < mOutputStreams.size(); i++) {
689 mOutputStreams[i]->dump(fd,args);
690 }
691
Zhijun He431503c2016-03-07 17:30:16 -0800692 if (mBufferManager != NULL) {
693 lines = String8(" Camera3 Buffer Manager:\n");
694 write(fd, lines.string(), lines.size());
695 mBufferManager->dump(fd, args);
696 }
Zhijun He125684a2015-12-26 15:07:30 -0800697
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700698 lines = String8(" In-flight requests:\n");
699 if (mInFlightMap.size() == 0) {
700 lines.append(" None\n");
701 } else {
702 for (size_t i = 0; i < mInFlightMap.size(); i++) {
703 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700704 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700705 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800706 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700707 r.numBuffersLeft);
708 }
709 }
710 write(fd, lines.string(), lines.size());
711
Igor Murashkin1e479c02013-09-06 16:55:14 -0700712 {
713 lines = String8(" Last request sent:\n");
714 write(fd, lines.string(), lines.size());
715
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700717 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
718 }
719
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800720 if (dumpTemplates) {
721 const char *templateNames[] = {
722 "TEMPLATE_PREVIEW",
723 "TEMPLATE_STILL_CAPTURE",
724 "TEMPLATE_VIDEO_RECORD",
725 "TEMPLATE_VIDEO_SNAPSHOT",
726 "TEMPLATE_ZERO_SHUTTER_LAG",
727 "TEMPLATE_MANUAL"
728 };
729
730 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800731 camera_metadata_t *templateRequest = nullptr;
732 mInterface->constructDefaultRequestSettings(
733 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800734 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800735 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800736 lines.append(" Not supported\n");
737 write(fd, lines.string(), lines.size());
738 } else {
739 write(fd, lines.string(), lines.size());
740 dump_indented_camera_metadata(templateRequest,
741 fd, /*verbosity*/2, /*indentation*/8);
742 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800743 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800744 }
745 }
746
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700747 mTagMonitor.dumpMonitoredMetadata(fd);
748
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800749 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800750 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800751 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800752 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800754
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700755 if (gotLock) mLock.unlock();
756 if (gotInterfaceLock) mInterfaceLock.unlock();
757
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800758 return OK;
759}
760
761const CameraMetadata& Camera3Device::info() const {
762 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800763 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
764 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700765 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800766 mStatus == STATUS_ERROR ?
767 "when in error state" : "before init");
768 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800769 return mDeviceInfo;
770}
771
Jianing Wei90e59c92014-03-12 18:29:36 -0700772status_t Camera3Device::checkStatusOkToCaptureLocked() {
773 switch (mStatus) {
774 case STATUS_ERROR:
775 CLOGE("Device has encountered a serious error");
776 return INVALID_OPERATION;
777 case STATUS_UNINITIALIZED:
778 CLOGE("Device not initialized");
779 return INVALID_OPERATION;
780 case STATUS_UNCONFIGURED:
781 case STATUS_CONFIGURED:
782 case STATUS_ACTIVE:
783 // OK
784 break;
785 default:
786 SET_ERR_L("Unexpected status: %d", mStatus);
787 return INVALID_OPERATION;
788 }
789 return OK;
790}
791
792status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700793 const List<const CameraMetadata> &metadataList,
794 const std::list<const SurfaceMap> &surfaceMaps,
795 bool repeating,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700796 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700797 if (requestList == NULL) {
798 CLOGE("requestList cannot be NULL.");
799 return BAD_VALUE;
800 }
801
Jianing Weicb0652e2014-03-12 18:29:36 -0700802 int32_t burstId = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700803 List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
804 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
805 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
806 ++metadataIt, ++surfaceMapIt) {
807 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700808 if (newRequest == 0) {
809 CLOGE("Can't create capture request");
810 return BAD_VALUE;
811 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700812
Shuzhen Wang9d066012016-09-30 11:30:20 -0700813 newRequest->mRepeating = repeating;
814
Jianing Weicb0652e2014-03-12 18:29:36 -0700815 // Setup burst Id and request Id
816 newRequest->mResultExtras.burstId = burstId++;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700817 if (metadataIt->exists(ANDROID_REQUEST_ID)) {
818 if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700819 CLOGE("RequestID entry exists; but must not be empty in metadata");
820 return BAD_VALUE;
821 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700822 newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700823 } else {
824 CLOGE("RequestID does not exist in metadata");
825 return BAD_VALUE;
826 }
827
Jianing Wei90e59c92014-03-12 18:29:36 -0700828 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700829
830 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700831 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700832 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
833 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
834 return BAD_VALUE;
835 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700836
837 // Setup batch size if this is a high speed video recording request.
838 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
839 auto firstRequest = requestList->begin();
840 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
841 if (outputStream->isVideoStream()) {
842 (*firstRequest)->mBatchSize = requestList->size();
843 break;
844 }
845 }
846 }
847
Jianing Wei90e59c92014-03-12 18:29:36 -0700848 return OK;
849}
850
Jianing Weicb0652e2014-03-12 18:29:36 -0700851status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700854 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700855 std::list<const SurfaceMap> surfaceMaps;
856 convertToRequestList(requests, surfaceMaps, request);
857
858 return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
859}
860
861void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
862 std::list<const SurfaceMap>& surfaceMaps,
863 const CameraMetadata& request) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700864 requests.push_back(request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700865
866 SurfaceMap surfaceMap;
867 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
868 // With no surface list passed in, stream and surface will have 1-to-1
869 // mapping. So the surface index is 0 for each stream in the surfaceMap.
870 for (size_t i = 0; i < streams.count; i++) {
871 surfaceMap[streams.data.i32[i]].push_back(0);
872 }
873 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800874}
875
Jianing Wei90e59c92014-03-12 18:29:36 -0700876status_t Camera3Device::submitRequestsHelper(
Shuzhen Wang0129d522016-10-30 22:43:41 -0700877 const List<const CameraMetadata> &requests,
878 const std::list<const SurfaceMap> &surfaceMaps,
879 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700880 /*out*/
881 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700882 ATRACE_CALL();
883 Mutex::Autolock il(mInterfaceLock);
884 Mutex::Autolock l(mLock);
885
886 status_t res = checkStatusOkToCaptureLocked();
887 if (res != OK) {
888 // error logged by previous call
889 return res;
890 }
891
892 RequestList requestList;
893
Shuzhen Wang0129d522016-10-30 22:43:41 -0700894 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
895 repeating, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700896 if (res != OK) {
897 // error logged by previous call
898 return res;
899 }
900
901 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700902 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700903 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700904 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700905 }
906
907 if (res == OK) {
908 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
909 if (res != OK) {
910 SET_ERR_L("Can't transition to active in %f seconds!",
911 kActiveTimeout/1e9);
912 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800913 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700914 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700915 } else {
916 CLOGE("Cannot queue request. Impossible.");
917 return BAD_VALUE;
918 }
919
920 return res;
921}
922
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800923
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800924hardware::Return<void> Camera3Device::processCaptureResult(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800925 const hardware::hidl_vec<
926 hardware::camera::device::V3_2::CaptureResult>& results) {
927 for (const auto& result : results) {
928 processOneCaptureResult(result);
929 }
930 return hardware::Void();
931}
932
933void Camera3Device::processOneCaptureResult(
934 const hardware::camera::device::V3_2::CaptureResult& result) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800935 camera3_capture_result r;
936 status_t res;
937 r.frame_number = result.frameNumber;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800938 if (result.result.size() != 0) {
939 r.result = reinterpret_cast<const camera_metadata_t*>(result.result.data());
940 size_t expected_metadata_size = result.result.size();
941 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
942 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
943 __FUNCTION__, result.frameNumber, strerror(-res), res);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800944 return;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800945 }
946 } else {
947 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800948 }
949
950 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
951 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
952 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
953 auto& bDst = outputBuffers[i];
954 const StreamBuffer &bSrc = result.outputBuffers[i];
955
956 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +0100957 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800958 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
959 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800960 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800961 }
962 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
963
964 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800965 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800966 if (res != OK) {
967 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
968 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800969 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800970 }
971 bDst.buffer = buffer;
972 bDst.status = mapHidlBufferStatus(bSrc.status);
973 bDst.acquire_fence = -1;
974 if (bSrc.releaseFence == nullptr) {
975 bDst.release_fence = -1;
976 } else if (bSrc.releaseFence->numFds == 1) {
977 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
978 } else {
979 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
980 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800981 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800982 }
983 }
984 r.num_output_buffers = outputBuffers.size();
985 r.output_buffers = outputBuffers.data();
986
987 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800988 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800989 r.input_buffer = nullptr;
990 } else {
991 if (mInputStream->getId() != result.inputBuffer.streamId) {
992 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
993 result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -0800994 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800995 }
996 inputBuffer.stream = mInputStream->asHalStream();
997 buffer_handle_t *buffer;
998 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
999 &buffer);
1000 if (res != OK) {
1001 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
1002 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001003 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001004 }
1005 inputBuffer.buffer = buffer;
1006 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
1007 inputBuffer.acquire_fence = -1;
1008 if (result.inputBuffer.releaseFence == nullptr) {
1009 inputBuffer.release_fence = -1;
1010 } else if (result.inputBuffer.releaseFence->numFds == 1) {
1011 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
1012 } else {
1013 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
1014 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001015 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001016 }
1017 r.input_buffer = &inputBuffer;
1018 }
1019
1020 r.partial_result = result.partialResult;
1021
1022 processCaptureResult(&r);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001023}
1024
1025hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001026 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
1027 for (const auto& msg : msgs) {
1028 notify(msg);
1029 }
1030 return hardware::Void();
1031}
1032
1033void Camera3Device::notify(
1034 const hardware::camera::device::V3_2::NotifyMsg& msg) {
1035
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001036 camera3_notify_msg m;
1037 switch (msg.type) {
1038 case MsgType::ERROR:
1039 m.type = CAMERA3_MSG_ERROR;
1040 m.message.error.frame_number = msg.msg.error.frameNumber;
1041 if (msg.msg.error.errorStreamId >= 0) {
1042 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
Emilian Peevbe3d40c2017-03-27 13:03:10 +01001043 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001044 ALOGE("%s: Frame %d: Invalid error stream id %d",
1045 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001046 return;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001047 }
1048 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1049 } else {
1050 m.message.error.error_stream = nullptr;
1051 }
1052 switch (msg.msg.error.errorCode) {
1053 case ErrorCode::ERROR_DEVICE:
1054 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1055 break;
1056 case ErrorCode::ERROR_REQUEST:
1057 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1058 break;
1059 case ErrorCode::ERROR_RESULT:
1060 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1061 break;
1062 case ErrorCode::ERROR_BUFFER:
1063 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1064 break;
1065 }
1066 break;
1067 case MsgType::SHUTTER:
1068 m.type = CAMERA3_MSG_SHUTTER;
1069 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1070 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1071 break;
1072 }
1073 notify(&m);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001074}
1075
Jianing Weicb0652e2014-03-12 18:29:36 -07001076status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001077 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001078 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001079 ATRACE_CALL();
1080
Shuzhen Wang0129d522016-10-30 22:43:41 -07001081 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001082}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001083
Jianing Weicb0652e2014-03-12 18:29:36 -07001084status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1085 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001086 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001087
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001088 List<const CameraMetadata> requests;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001089 std::list<const SurfaceMap> surfaceMaps;
1090 convertToRequestList(requests, surfaceMaps, request);
1091
1092 return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
1093 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094}
1095
Jianing Weicb0652e2014-03-12 18:29:36 -07001096status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001097 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001098 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001099 ATRACE_CALL();
1100
Shuzhen Wang0129d522016-10-30 22:43:41 -07001101 return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001102}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103
1104sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Shuzhen Wang0129d522016-10-30 22:43:41 -07001105 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001106 status_t res;
1107
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001108 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001109 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1110 // so unilaterally select normal operating mode.
1111 res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001112 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001113 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001114 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001116 } else if (mStatus == STATUS_UNCONFIGURED) {
1117 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001118 CLOGE("No streams configured");
1119 return NULL;
1120 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001121 }
1122
Shuzhen Wang0129d522016-10-30 22:43:41 -07001123 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001125}
1126
Jianing Weicb0652e2014-03-12 18:29:36 -07001127status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001128 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001130 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001132 switch (mStatus) {
1133 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001134 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135 return INVALID_OPERATION;
1136 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001137 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001138 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 case STATUS_UNCONFIGURED:
1140 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141 case STATUS_ACTIVE:
1142 // OK
1143 break;
1144 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001146 return INVALID_OPERATION;
1147 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001148 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001149
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001150 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001151}
1152
1153status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1154 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001155 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001157 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001158}
1159
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001160status_t Camera3Device::createInputStream(
1161 uint32_t width, uint32_t height, int format, int *id) {
1162 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001163 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001164 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001165 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1166 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001167
1168 status_t res;
1169 bool wasActive = false;
1170
1171 switch (mStatus) {
1172 case STATUS_ERROR:
1173 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1174 return INVALID_OPERATION;
1175 case STATUS_UNINITIALIZED:
1176 ALOGE("%s: Device not initialized", __FUNCTION__);
1177 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001178 case STATUS_UNCONFIGURED:
1179 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001180 // OK
1181 break;
1182 case STATUS_ACTIVE:
1183 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001184 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001185 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001186 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001187 return res;
1188 }
1189 wasActive = true;
1190 break;
1191 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001192 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001193 return INVALID_OPERATION;
1194 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001195 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001196
1197 if (mInputStream != 0) {
1198 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1199 return INVALID_OPERATION;
1200 }
1201
1202 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1203 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001204 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001205
1206 mInputStream = newStream;
1207
1208 *id = mNextStreamId++;
1209
1210 // Continue captures if active at start
1211 if (wasActive) {
1212 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001213 // Reuse current operating mode for new stream config
1214 res = configureStreamsLocked(mOperatingMode);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001215 if (res != OK) {
1216 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1217 __FUNCTION__, mNextStreamId, strerror(-res), res);
1218 return res;
1219 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001221 }
1222
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001223 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001224 return OK;
1225}
1226
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001227status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001228 uint32_t width, uint32_t height, int format,
1229 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001230 int streamSetId, bool isShared, uint32_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001231 ATRACE_CALL();
1232
1233 if (consumer == nullptr) {
1234 ALOGE("%s: consumer must not be null", __FUNCTION__);
1235 return BAD_VALUE;
1236 }
1237
1238 std::vector<sp<Surface>> consumers;
1239 consumers.push_back(consumer);
1240
1241 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001242 format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001243}
1244
1245status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1246 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1247 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wang758c2152017-01-10 18:26:18 -08001248 int streamSetId, bool isShared, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001249 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001252 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang758c2152017-01-10 18:26:18 -08001253 " consumer usage 0x%x, isShared %d", mId.string(), mNextStreamId, width, height, format,
1254 dataSpace, rotation, consumerUsage, isShared);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001256 status_t res;
1257 bool wasActive = false;
1258
1259 switch (mStatus) {
1260 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001261 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 return INVALID_OPERATION;
1263 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001264 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001265 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001266 case STATUS_UNCONFIGURED:
1267 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001268 // OK
1269 break;
1270 case STATUS_ACTIVE:
1271 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001272 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001273 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001274 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 return res;
1276 }
1277 wasActive = true;
1278 break;
1279 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001280 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 return INVALID_OPERATION;
1282 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001283 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284
1285 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001286 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001287 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001288 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001289 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1290 }
Zhijun He5d677d12016-05-29 16:52:39 -07001291
Shuzhen Wang0129d522016-10-30 22:43:41 -07001292 if (consumers.size() == 0 && !hasDeferredConsumer) {
1293 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1294 return BAD_VALUE;
1295 }
Zhijun He5d677d12016-05-29 16:52:39 -07001296 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1297 // which requires a consumer surface to be available.
Shuzhen Wang0129d522016-10-30 22:43:41 -07001298 if (hasDeferredConsumer && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He5d677d12016-05-29 16:52:39 -07001299 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1300 return BAD_VALUE;
1301 }
1302
Shuzhen Wang0129d522016-10-30 22:43:41 -07001303 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001304 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1305 return BAD_VALUE;
1306 }
1307
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001308 // Use legacy dataspace values for older HALs
1309 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1310 dataSpace = mapToLegacyDataspace(dataSpace);
1311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001313 ssize_t blobBufferSize;
1314 if (dataSpace != HAL_DATASPACE_DEPTH) {
1315 blobBufferSize = getJpegBufferSize(width, height);
1316 if (blobBufferSize <= 0) {
1317 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1318 return BAD_VALUE;
1319 }
1320 } else {
1321 blobBufferSize = getPointCloudBufferSize();
1322 if (blobBufferSize <= 0) {
1323 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1324 return BAD_VALUE;
1325 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001326 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001327 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001328 width, height, blobBufferSize, format, dataSpace, rotation,
1329 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001330 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1331 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1332 if (rawOpaqueBufferSize <= 0) {
1333 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1334 return BAD_VALUE;
1335 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001336 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001337 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1338 mTimestampOffset, streamSetId);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001339 } else if (isShared) {
1340 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1341 width, height, format, consumerUsage, dataSpace, rotation,
1342 mTimestampOffset, streamSetId);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001343 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001344 newStream = new Camera3OutputStream(mNextStreamId,
1345 width, height, format, consumerUsage, dataSpace, rotation,
1346 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001348 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001349 width, height, format, dataSpace, rotation,
1350 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001352 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001353
Zhijun He125684a2015-12-26 15:07:30 -08001354 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001355 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1356 * requires buffers to be statically allocated for internal static buffer registration, while
1357 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1358 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001359 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001360 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001361 newStream->setBufferManager(mBufferManager);
1362 }
1363
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 res = mOutputStreams.add(mNextStreamId, newStream);
1365 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001366 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 return res;
1368 }
1369
1370 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001371 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001372
1373 // Continue captures if active at start
1374 if (wasActive) {
1375 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001376 // Reuse current operating mode for new stream config
1377 res = configureStreamsLocked(mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001378 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001379 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1380 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 return res;
1382 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001385 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001386 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001387}
1388
1389status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1390 ATRACE_CALL();
1391 (void)outputId; (void)id;
1392
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001393 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001394 return INVALID_OPERATION;
1395}
1396
1397
1398status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001399 uint32_t *width, uint32_t *height,
1400 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001401 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001402 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001405 switch (mStatus) {
1406 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001407 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001408 return INVALID_OPERATION;
1409 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001410 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001411 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001412 case STATUS_UNCONFIGURED:
1413 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001414 case STATUS_ACTIVE:
1415 // OK
1416 break;
1417 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001418 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419 return INVALID_OPERATION;
1420 }
1421
1422 ssize_t idx = mOutputStreams.indexOfKey(id);
1423 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001424 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001425 return idx;
1426 }
1427
1428 if (width) *width = mOutputStreams[idx]->getWidth();
1429 if (height) *height = mOutputStreams[idx]->getHeight();
1430 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001431 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001432 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001433}
1434
1435status_t Camera3Device::setStreamTransform(int id,
1436 int transform) {
1437 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001438 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001441 switch (mStatus) {
1442 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001443 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 return INVALID_OPERATION;
1445 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001446 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001448 case STATUS_UNCONFIGURED:
1449 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001450 case STATUS_ACTIVE:
1451 // OK
1452 break;
1453 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001454 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001455 return INVALID_OPERATION;
1456 }
1457
1458 ssize_t idx = mOutputStreams.indexOfKey(id);
1459 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001460 CLOGE("Stream %d does not exist",
1461 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001462 return BAD_VALUE;
1463 }
1464
1465 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001466}
1467
1468status_t Camera3Device::deleteStream(int id) {
1469 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 Mutex::Autolock l(mLock);
1472 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001473
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001474 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001475
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001476 // CameraDevice semantics require device to already be idle before
1477 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001478 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001479 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001480 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001481 }
1482
Igor Murashkin2fba5842013-04-22 14:03:54 -07001483 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001484 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001485 if (mInputStream != NULL && id == mInputStream->getId()) {
1486 deletedStream = mInputStream;
1487 mInputStream.clear();
1488 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001489 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001490 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001491 return BAD_VALUE;
1492 }
Zhijun He5f446352014-01-22 09:49:33 -08001493 }
1494
1495 // Delete output stream or the output part of a bi-directional stream.
1496 if (outputStreamIdx != NAME_NOT_FOUND) {
1497 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498 mOutputStreams.removeItem(id);
1499 }
1500
1501 // Free up the stream endpoint so that it can be used by some other stream
1502 res = deletedStream->disconnect();
1503 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001504 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505 // fall through since we want to still list the stream as deleted.
1506 }
1507 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001508 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509
1510 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001511}
1512
1513status_t Camera3Device::deleteReprocessStream(int id) {
1514 ATRACE_CALL();
1515 (void)id;
1516
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001517 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001518 return INVALID_OPERATION;
1519}
1520
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08001521status_t Camera3Device::configureStreams(int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001522 ATRACE_CALL();
1523 ALOGV("%s: E", __FUNCTION__);
1524
1525 Mutex::Autolock il(mInterfaceLock);
1526 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001527
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001528 return configureStreamsLocked(operatingMode);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001529}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001531status_t Camera3Device::getInputBufferProducer(
1532 sp<IGraphicBufferProducer> *producer) {
1533 Mutex::Autolock il(mInterfaceLock);
1534 Mutex::Autolock l(mLock);
1535
1536 if (producer == NULL) {
1537 return BAD_VALUE;
1538 } else if (mInputStream == NULL) {
1539 return INVALID_OPERATION;
1540 }
1541
1542 return mInputStream->getInputBufferProducer(producer);
1543}
1544
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001545status_t Camera3Device::createDefaultRequest(int templateId,
1546 CameraMetadata *request) {
1547 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001548 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001549
1550 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1551 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1552 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1553 return BAD_VALUE;
1554 }
1555
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 Mutex::Autolock l(mLock);
1558
1559 switch (mStatus) {
1560 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001561 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562 return INVALID_OPERATION;
1563 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001564 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001565 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001566 case STATUS_UNCONFIGURED:
1567 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 case STATUS_ACTIVE:
1569 // OK
1570 break;
1571 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001572 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 return INVALID_OPERATION;
1574 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001575
Zhijun Hea1530f12014-09-14 12:44:20 -07001576 if (!mRequestTemplateCache[templateId].isEmpty()) {
1577 *request = mRequestTemplateCache[templateId];
1578 return OK;
1579 }
1580
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001581 camera_metadata_t *rawRequest;
1582 status_t res = mInterface->constructDefaultRequestSettings(
1583 (camera3_request_template_t) templateId, &rawRequest);
1584 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001585 ALOGI("%s: template %d is not supported on this camera device",
1586 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001587 return res;
1588 } else if (res != OK) {
1589 CLOGE("Unable to construct request template %d: %s (%d)",
1590 templateId, strerror(-res), res);
1591 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001592 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001593
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001594 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001595
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001596 // Derive some new keys for backward compatibility
1597 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1598 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1599 int32_t defaultBoost[1] = {100};
1600 mRequestTemplateCache[templateId].update(
1601 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1602 defaultBoost, 1);
1603 }
1604
1605 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001606 return OK;
1607}
1608
1609status_t Camera3Device::waitUntilDrained() {
1610 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001611 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001612 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613
Zhijun He69a37482014-03-23 18:44:49 -07001614 return waitUntilDrainedLocked();
1615}
1616
1617status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001618 switch (mStatus) {
1619 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001620 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 ALOGV("%s: Already idle", __FUNCTION__);
1622 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001623 case STATUS_CONFIGURED:
1624 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001625 case STATUS_ERROR:
1626 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001627 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 break;
1629 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001630 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001631 return INVALID_OPERATION;
1632 }
1633
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001634 ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001635 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001636 if (res != OK) {
1637 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1638 res);
1639 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001640 return res;
1641}
1642
Ruben Brunk183f0562015-08-12 12:55:02 -07001643
1644void Camera3Device::internalUpdateStatusLocked(Status status) {
1645 mStatus = status;
1646 mRecentStatusUpdates.add(mStatus);
1647 mStatusChanged.broadcast();
1648}
1649
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001650// Pause to reconfigure
1651status_t Camera3Device::internalPauseAndWaitLocked() {
1652 mRequestThread->setPaused(true);
1653 mPauseStateNotify = true;
1654
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001655 ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001656 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1657 if (res != OK) {
1658 SET_ERR_L("Can't idle device in %f seconds!",
1659 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001660 }
1661
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001662 return res;
1663}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001665// Resume after internalPauseAndWaitLocked
1666status_t Camera3Device::internalResumeLocked() {
1667 status_t res;
1668
1669 mRequestThread->setPaused(false);
1670
1671 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1672 if (res != OK) {
1673 SET_ERR_L("Can't transition to active in %f seconds!",
1674 kActiveTimeout/1e9);
1675 }
1676 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001677 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001678}
1679
Ruben Brunk183f0562015-08-12 12:55:02 -07001680status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001681 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001682
1683 size_t startIndex = 0;
1684 if (mStatusWaiters == 0) {
1685 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1686 // this status list
1687 mRecentStatusUpdates.clear();
1688 } else {
1689 // If other threads are waiting on updates to this status list, set the position of the
1690 // first element that this list will check rather than clearing the list.
1691 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001692 }
1693
Ruben Brunk183f0562015-08-12 12:55:02 -07001694 mStatusWaiters++;
1695
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001696 bool stateSeen = false;
1697 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001698 if (active == (mStatus == STATUS_ACTIVE)) {
1699 // Desired state is current
1700 break;
1701 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001702
1703 res = mStatusChanged.waitRelative(mLock, timeout);
1704 if (res != OK) break;
1705
Ruben Brunk183f0562015-08-12 12:55:02 -07001706 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1707 // transitions.
1708 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1709 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1710 __FUNCTION__);
1711
1712 // Encountered desired state since we began waiting
1713 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001714 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1715 stateSeen = true;
1716 break;
1717 }
1718 }
1719 } while (!stateSeen);
1720
Ruben Brunk183f0562015-08-12 12:55:02 -07001721 mStatusWaiters--;
1722
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 return res;
1724}
1725
1726
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001727status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001728 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001729 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001730
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001731 if (listener != NULL && mListener != NULL) {
1732 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1733 }
1734 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001735 mRequestThread->setNotificationListener(listener);
1736 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001737
1738 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739}
1740
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001741bool Camera3Device::willNotify3A() {
1742 return false;
1743}
1744
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001745status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001746 status_t res;
1747 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001748
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001749 while (mResultQueue.empty()) {
1750 res = mResultSignal.waitRelative(mOutputLock, timeout);
1751 if (res == TIMED_OUT) {
1752 return res;
1753 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001754 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1755 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001756 return res;
1757 }
1758 }
1759 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760}
1761
Jianing Weicb0652e2014-03-12 18:29:36 -07001762status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001763 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001764 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001765
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001766 if (mResultQueue.empty()) {
1767 return NOT_ENOUGH_DATA;
1768 }
1769
Jianing Weicb0652e2014-03-12 18:29:36 -07001770 if (frame == NULL) {
1771 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1772 return BAD_VALUE;
1773 }
1774
1775 CaptureResult &result = *(mResultQueue.begin());
1776 frame->mResultExtras = result.mResultExtras;
1777 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001778 mResultQueue.erase(mResultQueue.begin());
1779
1780 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001781}
1782
1783status_t Camera3Device::triggerAutofocus(uint32_t id) {
1784 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001785 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001786
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001787 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1788 // Mix-in this trigger into the next request and only the next request.
1789 RequestTrigger trigger[] = {
1790 {
1791 ANDROID_CONTROL_AF_TRIGGER,
1792 ANDROID_CONTROL_AF_TRIGGER_START
1793 },
1794 {
1795 ANDROID_CONTROL_AF_TRIGGER_ID,
1796 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001797 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001798 };
1799
1800 return mRequestThread->queueTrigger(trigger,
1801 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001802}
1803
1804status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1805 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001806 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001807
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001808 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1809 // Mix-in this trigger into the next request and only the next request.
1810 RequestTrigger trigger[] = {
1811 {
1812 ANDROID_CONTROL_AF_TRIGGER,
1813 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1814 },
1815 {
1816 ANDROID_CONTROL_AF_TRIGGER_ID,
1817 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001818 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001819 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001820
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001821 return mRequestThread->queueTrigger(trigger,
1822 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001823}
1824
1825status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1826 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001827 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001828
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001829 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1830 // Mix-in this trigger into the next request and only the next request.
1831 RequestTrigger trigger[] = {
1832 {
1833 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1834 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1835 },
1836 {
1837 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1838 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001839 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001840 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001841
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001842 return mRequestThread->queueTrigger(trigger,
1843 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001844}
1845
1846status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1847 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1848 ATRACE_CALL();
1849 (void)reprocessStreamId; (void)buffer; (void)listener;
1850
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001851 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001852 return INVALID_OPERATION;
1853}
1854
Jianing Weicb0652e2014-03-12 18:29:36 -07001855status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001856 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001857 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001858 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001859
Zhijun He7ef20392014-04-21 16:04:17 -07001860 {
1861 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001862 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001863 }
1864
Zhijun He491e3412013-12-27 10:57:44 -08001865 status_t res;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001866 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001867 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001868 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001869 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001870 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001871 }
1872
1873 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001874}
1875
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001876status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001877 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1878}
1879
1880status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001881 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001882 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001883 Mutex::Autolock il(mInterfaceLock);
1884 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001885
1886 sp<Camera3StreamInterface> stream;
1887 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1888 if (outputStreamIdx == NAME_NOT_FOUND) {
1889 CLOGE("Stream %d does not exist", streamId);
1890 return BAD_VALUE;
1891 }
1892
1893 stream = mOutputStreams.editValueAt(outputStreamIdx);
1894
1895 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001896 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001897 return BAD_VALUE;
1898 }
1899
1900 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001901 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001902 return BAD_VALUE;
1903 }
1904
Ruben Brunkc78ac262015-08-13 17:58:46 -07001905 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001906}
1907
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001908status_t Camera3Device::tearDown(int streamId) {
1909 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001910 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001911 Mutex::Autolock il(mInterfaceLock);
1912 Mutex::Autolock l(mLock);
1913
1914 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1915 // since we cannot call register_stream_buffers except right after configure_streams.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001916 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001917 ALOGE("%s: Unable to tear down streams on device HAL v%x",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001918 __FUNCTION__, mDeviceVersion);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001919 return NO_INIT;
1920 }
1921
1922 sp<Camera3StreamInterface> stream;
1923 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1924 if (outputStreamIdx == NAME_NOT_FOUND) {
1925 CLOGE("Stream %d does not exist", streamId);
1926 return BAD_VALUE;
1927 }
1928
1929 stream = mOutputStreams.editValueAt(outputStreamIdx);
1930
1931 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1932 CLOGE("Stream %d is a target of a in-progress request", streamId);
1933 return BAD_VALUE;
1934 }
1935
1936 return stream->tearDown();
1937}
1938
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001939status_t Camera3Device::addBufferListenerForStream(int streamId,
1940 wp<Camera3StreamBufferListener> listener) {
1941 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001942 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001943 Mutex::Autolock il(mInterfaceLock);
1944 Mutex::Autolock l(mLock);
1945
1946 sp<Camera3StreamInterface> stream;
1947 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1948 if (outputStreamIdx == NAME_NOT_FOUND) {
1949 CLOGE("Stream %d does not exist", streamId);
1950 return BAD_VALUE;
1951 }
1952
1953 stream = mOutputStreams.editValueAt(outputStreamIdx);
1954 stream->addBufferListener(listener);
1955
1956 return OK;
1957}
1958
Zhijun He204e3292014-07-14 17:09:23 -07001959uint32_t Camera3Device::getDeviceVersion() {
1960 ATRACE_CALL();
1961 Mutex::Autolock il(mInterfaceLock);
1962 return mDeviceVersion;
1963}
1964
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001965/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 * Methods called by subclasses
1967 */
1968
1969void Camera3Device::notifyStatus(bool idle) {
1970 {
1971 // Need mLock to safely update state and synchronize to current
1972 // state of methods in flight.
1973 Mutex::Autolock l(mLock);
1974 // We can get various system-idle notices from the status tracker
1975 // while starting up. Only care about them if we've actually sent
1976 // in some requests recently.
1977 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1978 return;
1979 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001980 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001982 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983
1984 // Skip notifying listener if we're doing some user-transparent
1985 // state changes
1986 if (mPauseStateNotify) return;
1987 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001988
1989 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001990 {
1991 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001992 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001993 }
1994 if (idle && listener != NULL) {
1995 listener->notifyIdle();
1996 }
1997}
1998
Shuzhen Wang758c2152017-01-10 18:26:18 -08001999status_t Camera3Device::setConsumerSurfaces(int streamId,
2000 const std::vector<sp<Surface>>& consumers) {
Zhijun He5d677d12016-05-29 16:52:39 -07002001 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002002 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2003 __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07002004 Mutex::Autolock il(mInterfaceLock);
2005 Mutex::Autolock l(mLock);
2006
Shuzhen Wang758c2152017-01-10 18:26:18 -08002007 if (consumers.size() == 0) {
2008 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002009 return BAD_VALUE;
2010 }
2011
2012 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2013 if (idx == NAME_NOT_FOUND) {
2014 CLOGE("Stream %d is unknown", streamId);
2015 return idx;
2016 }
2017 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
Shuzhen Wang758c2152017-01-10 18:26:18 -08002018 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002019 if (res != OK) {
2020 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2021 return res;
2022 }
2023
Shuzhen Wang0129d522016-10-30 22:43:41 -07002024 if (stream->isConsumerConfigurationDeferred()) {
2025 if (!stream->isConfiguring()) {
2026 CLOGE("Stream %d was already fully configured.", streamId);
2027 return INVALID_OPERATION;
2028 }
Zhijun He5d677d12016-05-29 16:52:39 -07002029
Shuzhen Wang0129d522016-10-30 22:43:41 -07002030 res = stream->finishConfiguration();
2031 if (res != OK) {
2032 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2033 stream->getId(), strerror(-res), res);
2034 return res;
2035 }
Zhijun He5d677d12016-05-29 16:52:39 -07002036 }
2037
2038 return OK;
2039}
2040
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002041/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002042 * Camera3Device private methods
2043 */
2044
2045sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Shuzhen Wang0129d522016-10-30 22:43:41 -07002046 const CameraMetadata &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047 ATRACE_CALL();
2048 status_t res;
2049
2050 sp<CaptureRequest> newRequest = new CaptureRequest;
2051 newRequest->mSettings = request;
2052
2053 camera_metadata_entry_t inputStreams =
2054 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
2055 if (inputStreams.count > 0) {
2056 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002057 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002058 CLOGE("Request references unknown input stream %d",
2059 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002060 return NULL;
2061 }
2062 // Lazy completion of stream configuration (allocation/registration)
2063 // on first use
2064 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002065 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002067 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002068 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002069 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002070 return NULL;
2071 }
2072 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002073 // Check if stream is being prepared
2074 if (mInputStream->isPreparing()) {
2075 CLOGE("Request references an input stream that's being prepared!");
2076 return NULL;
2077 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002078
2079 newRequest->mInputStream = mInputStream;
2080 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
2081 }
2082
2083 camera_metadata_entry_t streams =
2084 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2085 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002086 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087 return NULL;
2088 }
2089
2090 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002091 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002093 CLOGE("Request references unknown stream %d",
2094 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002095 return NULL;
2096 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002097 sp<Camera3OutputStreamInterface> stream =
2098 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002099
Zhijun He5d677d12016-05-29 16:52:39 -07002100 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002101 auto iter = surfaceMap.find(streams.data.i32[i]);
2102 if (iter != surfaceMap.end()) {
2103 const std::vector<size_t>& surfaces = iter->second;
2104 for (const auto& surface : surfaces) {
2105 if (stream->isConsumerConfigurationDeferred(surface)) {
2106 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2107 "due to deferred consumer", stream->getId(), surface);
2108 return NULL;
2109 }
2110 }
2111 newRequest->mOutputSurfaces[i] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002112 }
2113
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002114 // Lazy completion of stream configuration (allocation/registration)
2115 // on first use
2116 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002117 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002119 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2120 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002121 return NULL;
2122 }
2123 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002124 // Check if stream is being prepared
2125 if (stream->isPreparing()) {
2126 CLOGE("Request references an output stream that's being prepared!");
2127 return NULL;
2128 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002129
2130 newRequest->mOutputStreams.push(stream);
2131 }
2132 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002133 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002134
2135 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002136}
2137
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002138bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2139 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2140 Size size = mSupportedOpaqueInputSizes[i];
2141 if (size.width == width && size.height == height) {
2142 return true;
2143 }
2144 }
2145
2146 return false;
2147}
2148
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002149void Camera3Device::cancelStreamsConfigurationLocked() {
2150 int res = OK;
2151 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2152 res = mInputStream->cancelConfiguration();
2153 if (res != OK) {
2154 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2155 mInputStream->getId(), strerror(-res), res);
2156 }
2157 }
2158
2159 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2160 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2161 if (outputStream->isConfiguring()) {
2162 res = outputStream->cancelConfiguration();
2163 if (res != OK) {
2164 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2165 outputStream->getId(), strerror(-res), res);
2166 }
2167 }
2168 }
2169
2170 // Return state to that at start of call, so that future configures
2171 // properly clean things up
2172 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2173 mNeedConfig = true;
2174}
2175
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002176status_t Camera3Device::configureStreamsLocked(int operatingMode) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002177 ATRACE_CALL();
2178 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002179
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002180 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002181 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002182 return INVALID_OPERATION;
2183 }
2184
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002185 if (operatingMode < 0) {
2186 CLOGE("Invalid operating mode: %d", operatingMode);
2187 return BAD_VALUE;
2188 }
2189
2190 bool isConstrainedHighSpeed =
2191 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2192 operatingMode;
2193
2194 if (mOperatingMode != operatingMode) {
2195 mNeedConfig = true;
2196 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2197 mOperatingMode = operatingMode;
2198 }
2199
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002200 if (!mNeedConfig) {
2201 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2202 return OK;
2203 }
2204
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002205 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2206 // adding a dummy stream instead.
2207 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2208 if (mOutputStreams.size() == 0) {
2209 addDummyStreamLocked();
2210 } else {
2211 tryRemoveDummyStreamLocked();
2212 }
2213
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002214 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002215 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002216
2217 camera3_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002218 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002219 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2220
2221 Vector<camera3_stream_t*> streams;
2222 streams.setCapacity(config.num_streams);
2223
2224 if (mInputStream != NULL) {
2225 camera3_stream_t *inputStream;
2226 inputStream = mInputStream->startConfiguration();
2227 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002228 CLOGE("Can't start input stream configuration");
2229 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002230 return INVALID_OPERATION;
2231 }
2232 streams.add(inputStream);
2233 }
2234
2235 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002236
2237 // Don't configure bidi streams twice, nor add them twice to the list
2238 if (mOutputStreams[i].get() ==
2239 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2240
2241 config.num_streams--;
2242 continue;
2243 }
2244
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002245 camera3_stream_t *outputStream;
2246 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2247 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002248 CLOGE("Can't start output stream configuration");
2249 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002250 return INVALID_OPERATION;
2251 }
2252 streams.add(outputStream);
2253 }
2254
2255 config.streams = streams.editArray();
2256
2257 // Do the HAL configuration; will potentially touch stream
2258 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002259
2260 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002261
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002262 if (res == BAD_VALUE) {
2263 // HAL rejected this set of streams as unsupported, clean up config
2264 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002265 CLOGE("Set of requested inputs/outputs not supported by HAL");
2266 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002267 return BAD_VALUE;
2268 } else if (res != OK) {
2269 // Some other kind of error from configure_streams - this is not
2270 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002271 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2272 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002273 return res;
2274 }
2275
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002276 // Finish all stream configuration immediately.
2277 // TODO: Try to relax this later back to lazy completion, which should be
2278 // faster
2279
Igor Murashkin073f8572013-05-02 14:59:28 -07002280 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002281 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002282 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002283 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002284 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002285 cancelStreamsConfigurationLocked();
2286 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002287 }
2288 }
2289
2290 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002291 sp<Camera3OutputStreamInterface> outputStream =
2292 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002293 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002294 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002295 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002296 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002297 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002298 cancelStreamsConfigurationLocked();
2299 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002300 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002301 }
2302 }
2303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002304 // Request thread needs to know to avoid using repeat-last-settings protocol
2305 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002306 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002307
Zhijun He90f7c372016-08-16 16:19:43 -07002308 char value[PROPERTY_VALUE_MAX];
2309 property_get("camera.fifo.disable", value, "0");
2310 int32_t disableFifo = atoi(value);
2311 if (disableFifo != 1) {
2312 // Boost priority of request thread to SCHED_FIFO.
2313 pid_t requestThreadTid = mRequestThread->getTid();
2314 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002315 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002316 if (res != OK) {
2317 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2318 strerror(-res), res);
2319 } else {
2320 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2321 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002322 }
2323
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002324 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002325
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002326 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002327
Ruben Brunk183f0562015-08-12 12:55:02 -07002328 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2329 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002330
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002331 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002332
Zhijun He0a210512014-07-24 13:45:15 -07002333 // tear down the deleted streams after configure streams.
2334 mDeletedStreams.clear();
2335
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002336 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002337}
2338
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002339status_t Camera3Device::addDummyStreamLocked() {
2340 ATRACE_CALL();
2341 status_t res;
2342
2343 if (mDummyStreamId != NO_STREAM) {
2344 // Should never be adding a second dummy stream when one is already
2345 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002346 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2347 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002348 return INVALID_OPERATION;
2349 }
2350
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002351 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002352
2353 sp<Camera3OutputStreamInterface> dummyStream =
2354 new Camera3DummyStream(mNextStreamId);
2355
2356 res = mOutputStreams.add(mNextStreamId, dummyStream);
2357 if (res < 0) {
2358 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2359 return res;
2360 }
2361
2362 mDummyStreamId = mNextStreamId;
2363 mNextStreamId++;
2364
2365 return OK;
2366}
2367
2368status_t Camera3Device::tryRemoveDummyStreamLocked() {
2369 ATRACE_CALL();
2370 status_t res;
2371
2372 if (mDummyStreamId == NO_STREAM) return OK;
2373 if (mOutputStreams.size() == 1) return OK;
2374
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002375 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002376
2377 // Ok, have a dummy stream and there's at least one other output stream,
2378 // so remove the dummy
2379
2380 sp<Camera3StreamInterface> deletedStream;
2381 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2382 if (outputStreamIdx == NAME_NOT_FOUND) {
2383 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2384 return INVALID_OPERATION;
2385 }
2386
2387 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2388 mOutputStreams.removeItemsAt(outputStreamIdx);
2389
2390 // Free up the stream endpoint so that it can be used by some other stream
2391 res = deletedStream->disconnect();
2392 if (res != OK) {
2393 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2394 // fall through since we want to still list the stream as deleted.
2395 }
2396 mDeletedStreams.add(deletedStream);
2397 mDummyStreamId = NO_STREAM;
2398
2399 return res;
2400}
2401
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002402void Camera3Device::setErrorState(const char *fmt, ...) {
2403 Mutex::Autolock l(mLock);
2404 va_list args;
2405 va_start(args, fmt);
2406
2407 setErrorStateLockedV(fmt, args);
2408
2409 va_end(args);
2410}
2411
2412void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2413 Mutex::Autolock l(mLock);
2414 setErrorStateLockedV(fmt, args);
2415}
2416
2417void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2418 va_list args;
2419 va_start(args, fmt);
2420
2421 setErrorStateLockedV(fmt, args);
2422
2423 va_end(args);
2424}
2425
2426void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002427 // Print out all error messages to log
2428 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002429 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002430
2431 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002432 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002433
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002434 mErrorCause = errorCause;
2435
2436 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002437 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002438
2439 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002440 sp<NotificationListener> listener = mListener.promote();
2441 if (listener != NULL) {
2442 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002443 CaptureResultExtras());
2444 }
2445
2446 // Save stack trace. View by dumping it later.
2447 CameraTraces::saveTrace();
2448 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002449}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002450
2451/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002452 * In-flight request management
2453 */
2454
Jianing Weicb0652e2014-03-12 18:29:36 -07002455status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002456 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002457 const AeTriggerCancelOverride_t &aeTriggerCancelOverride,
2458 bool hasAppCallback) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002459 ATRACE_CALL();
2460 Mutex::Autolock l(mInFlightLock);
2461
2462 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002463 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002464 aeTriggerCancelOverride, hasAppCallback));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002465 if (res < 0) return res;
2466
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002467 if (mInFlightMap.size() == 1) {
2468 mStatusTracker->markComponentActive(mInFlightStatusId);
2469 }
2470
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002471 return OK;
2472}
2473
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002474void Camera3Device::returnOutputBuffers(
2475 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2476 nsecs_t timestamp) {
2477 for (size_t i = 0; i < numBuffers; i++)
2478 {
2479 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2480 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2481 // Note: stream may be deallocated at this point, if this buffer was
2482 // the last reference to it.
2483 if (res != OK) {
2484 ALOGE("Can't return buffer to its stream: %s (%d)",
2485 strerror(-res), res);
2486 }
2487 }
2488}
2489
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002490void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2491 mInFlightMap.removeItemsAt(idx, 1);
2492
2493 // Indicate idle inFlightMap to the status tracker
2494 if (mInFlightMap.size() == 0) {
2495 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2496 }
2497}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002498
2499void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2500
2501 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2502 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2503
2504 nsecs_t sensorTimestamp = request.sensorTimestamp;
2505 nsecs_t shutterTimestamp = request.shutterTimestamp;
2506
2507 // Check if it's okay to remove the request from InFlightMap:
2508 // In the case of a successful request:
2509 // all input and output buffers, all result metadata, shutter callback
2510 // arrived.
2511 // In the case of a unsuccessful request:
2512 // all input and output buffers arrived.
2513 if (request.numBuffersLeft == 0 &&
2514 (request.requestStatus != OK ||
2515 (request.haveResultMetadata && shutterTimestamp != 0))) {
2516 ATRACE_ASYNC_END("frame capture", frameNumber);
2517
Shuzhen Wang403044a2017-02-26 23:29:04 -08002518 // Sanity check - if sensor timestamp matches shutter timestamp in the
2519 // case of request having callback.
2520 if (request.hasCallback && request.requestStatus == OK &&
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002521 sensorTimestamp != shutterTimestamp) {
2522 SET_ERR("sensor timestamp (%" PRId64
2523 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2524 sensorTimestamp, frameNumber, shutterTimestamp);
2525 }
2526
2527 // for an unsuccessful request, it may have pending output buffers to
2528 // return.
2529 assert(request.requestStatus != OK ||
2530 request.pendingOutputBuffers.size() == 0);
2531 returnOutputBuffers(request.pendingOutputBuffers.array(),
2532 request.pendingOutputBuffers.size(), 0);
2533
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002534 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002535 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2536 }
2537
2538 // Sanity check - if we have too many in-flight frames, something has
2539 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002540 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002541 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002542 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2543 kInFlightWarnLimitHighSpeed) {
2544 CLOGE("In-flight list too large for high speed configuration: %zu",
2545 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002546 }
2547}
2548
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002549void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2550 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2551 if (result == nullptr) return;
2552
2553 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2554 (int32_t*)&frameNumber, 1) != OK) {
2555 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2556 return;
2557 }
2558
2559 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2560 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2561 return;
2562 }
2563
2564 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2565
2566 // Valid result, insert into queue
2567 List<CaptureResult>::iterator queuedResult =
2568 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2569 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2570 ", burstId = %" PRId32, __FUNCTION__,
2571 queuedResult->mResultExtras.requestId,
2572 queuedResult->mResultExtras.frameNumber,
2573 queuedResult->mResultExtras.burstId);
2574
2575 mResultSignal.signal();
2576}
2577
2578
2579void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2580 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2581 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2582 Mutex::Autolock l(mOutputLock);
2583
2584 CaptureResult captureResult;
2585 captureResult.mResultExtras = resultExtras;
2586 captureResult.mMetadata = partialResult;
2587
2588 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2589}
2590
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002591
2592void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2593 CaptureResultExtras &resultExtras,
2594 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002595 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002596 bool reprocess,
2597 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002598 if (pendingMetadata.isEmpty())
2599 return;
2600
2601 Mutex::Autolock l(mOutputLock);
2602
2603 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002604 if (reprocess) {
2605 if (frameNumber < mNextReprocessResultFrameNumber) {
2606 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002607 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002608 frameNumber, mNextReprocessResultFrameNumber);
2609 return;
2610 }
2611 mNextReprocessResultFrameNumber = frameNumber + 1;
2612 } else {
2613 if (frameNumber < mNextResultFrameNumber) {
2614 SET_ERR("Out-of-order capture result metadata submitted! "
2615 "(got frame number %d, expecting %d)",
2616 frameNumber, mNextResultFrameNumber);
2617 return;
2618 }
2619 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002620 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002621
2622 CaptureResult captureResult;
2623 captureResult.mResultExtras = resultExtras;
2624 captureResult.mMetadata = pendingMetadata;
2625
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002626 // Append any previous partials to form a complete result
2627 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2628 captureResult.mMetadata.append(collectedPartialResult);
2629 }
2630
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002631 // Derive some new keys for backward compaibility
2632 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2633 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2634 int32_t defaultBoost[1] = {100};
2635 captureResult.mMetadata.update(
2636 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2637 defaultBoost, 1);
2638 }
2639
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002640 captureResult.mMetadata.sort();
2641
2642 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002643 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2644 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002645 SET_ERR("No timestamp provided by HAL for frame %d!",
2646 frameNumber);
2647 return;
2648 }
2649
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002650 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2651 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2652
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002653 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002654}
2655
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002656/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002657 * Camera HAL device callback methods
2658 */
2659
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002660void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002661 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002662
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002663 status_t res;
2664
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002665 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002666 if (result->result == NULL && result->num_output_buffers == 0 &&
2667 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002668 SET_ERR("No result data provided by HAL for frame %d",
2669 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002670 return;
2671 }
Zhijun He204e3292014-07-14 17:09:23 -07002672
2673 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2674 // partial_result to 1 when metadata is included in this result.
2675 if (!mUsePartialResult &&
2676 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2677 result->result != NULL &&
2678 result->partial_result != 1) {
2679 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2680 " if partial result is not supported",
2681 frameNumber, result->partial_result);
2682 return;
2683 }
2684
2685 bool isPartialResult = false;
2686 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002687 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002688 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002689
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002690 // Get shutter timestamp and resultExtras from list of in-flight requests,
2691 // where it was added by the shutter notification for this frame. If the
2692 // shutter timestamp isn't received yet, append the output buffers to the
2693 // in-flight request and they will be returned when the shutter timestamp
2694 // arrives. Update the in-flight status and remove the in-flight entry if
2695 // all result data and shutter timestamp have been received.
2696 nsecs_t shutterTimestamp = 0;
2697
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002698 {
2699 Mutex::Autolock l(mInFlightLock);
2700 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2701 if (idx == NAME_NOT_FOUND) {
2702 SET_ERR("Unknown frame number for capture result: %d",
2703 frameNumber);
2704 return;
2705 }
2706 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002707 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2708 ", frameNumber = %" PRId64 ", burstId = %" PRId32
Shuzhen Wang4a472662017-02-26 23:29:04 -08002709 ", partialResultCount = %d, hasCallback = %d",
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002710 __FUNCTION__, request.resultExtras.requestId,
2711 request.resultExtras.frameNumber, request.resultExtras.burstId,
Shuzhen Wang4a472662017-02-26 23:29:04 -08002712 result->partial_result, request.hasCallback);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002713 // Always update the partial count to the latest one if it's not 0
2714 // (buffers only). When framework aggregates adjacent partial results
2715 // into one, the latest partial count will be used.
2716 if (result->partial_result != 0)
2717 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002718
2719 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002720 if (mUsePartialResult && result->result != NULL) {
2721 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2722 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2723 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2724 " the range of [1, %d] when metadata is included in the result",
2725 frameNumber, result->partial_result, mNumPartialResults);
2726 return;
2727 }
2728 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002729 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002730 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002731 }
Zhijun He204e3292014-07-14 17:09:23 -07002732 } else {
2733 camera_metadata_ro_entry_t partialResultEntry;
2734 res = find_camera_metadata_ro_entry(result->result,
2735 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2736 if (res != NAME_NOT_FOUND &&
2737 partialResultEntry.count > 0 &&
2738 partialResultEntry.data.u8[0] ==
2739 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2740 // A partial result. Flag this as such, and collect this
2741 // set of metadata into the in-flight entry.
2742 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002743 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002744 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002745 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002746 ANDROID_QUIRKS_PARTIAL_RESULT);
2747 }
2748 }
2749
Shuzhen Wang4a472662017-02-26 23:29:04 -08002750 if (isPartialResult && request.hasCallback) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002751 // Send partial capture result
2752 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2753 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002754 }
2755 }
2756
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002757 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002758 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002759
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002760 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002761 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002762 if (request.haveResultMetadata) {
2763 SET_ERR("Called multiple times with metadata for frame %d",
2764 frameNumber);
2765 return;
2766 }
Zhijun He204e3292014-07-14 17:09:23 -07002767 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002768 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002769 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002770 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002771 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002772 request.haveResultMetadata = true;
2773 }
2774
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002775 uint32_t numBuffersReturned = result->num_output_buffers;
2776 if (result->input_buffer != NULL) {
2777 if (hasInputBufferInRequest) {
2778 numBuffersReturned += 1;
2779 } else {
2780 ALOGW("%s: Input buffer should be NULL if there is no input"
2781 " buffer sent in the request",
2782 __FUNCTION__);
2783 }
2784 }
2785 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002786 if (request.numBuffersLeft < 0) {
2787 SET_ERR("Too many buffers returned for frame %d",
2788 frameNumber);
2789 return;
2790 }
2791
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002792 camera_metadata_ro_entry_t entry;
2793 res = find_camera_metadata_ro_entry(result->result,
2794 ANDROID_SENSOR_TIMESTAMP, &entry);
2795 if (res == OK && entry.count == 1) {
2796 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002797 }
2798
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002799 // If shutter event isn't received yet, append the output buffers to
2800 // the in-flight request. Otherwise, return the output buffers to
2801 // streams.
2802 if (shutterTimestamp == 0) {
2803 request.pendingOutputBuffers.appendArray(result->output_buffers,
2804 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002805 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002806 returnOutputBuffers(result->output_buffers,
2807 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002808 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002809
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002810 if (result->result != NULL && !isPartialResult) {
2811 if (shutterTimestamp == 0) {
2812 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002813 request.collectedPartialResult = collectedPartialResult;
Shuzhen Wang4a472662017-02-26 23:29:04 -08002814 } else if (request.hasCallback) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002815 CameraMetadata metadata;
2816 metadata = result->result;
2817 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002818 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2819 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002820 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002821 }
2822
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002823 removeInFlightRequestIfReadyLocked(idx);
2824 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002825
Zhijun Hef0d962a2014-06-30 10:24:11 -07002826 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002827 if (hasInputBufferInRequest) {
2828 Camera3Stream *stream =
2829 Camera3Stream::cast(result->input_buffer->stream);
2830 res = stream->returnInputBuffer(*(result->input_buffer));
2831 // Note: stream may be deallocated at this point, if this buffer was the
2832 // last reference to it.
2833 if (res != OK) {
2834 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2835 " its stream:%s (%d)", __FUNCTION__,
2836 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002837 }
2838 } else {
2839 ALOGW("%s: Input buffer should be NULL if there is no input"
2840 " buffer sent in the request, skipping input buffer return.",
2841 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002842 }
2843 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002844}
2845
2846void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002847 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002848 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002849 {
2850 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002851 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002852 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002853
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002854 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002855 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002856 return;
2857 }
2858
2859 switch (msg->type) {
2860 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002861 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002862 break;
2863 }
2864 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002865 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002866 break;
2867 }
2868 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002869 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002870 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002871 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002872}
2873
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002874void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002875 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002876
2877 // Map camera HAL error codes to ICameraDeviceCallback error codes
2878 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002879 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002880 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002881 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002882 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002883 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002884 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002885 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002886 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002887 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002888 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002889 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002890 };
2891
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002892 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002893 ((msg.error_code >= 0) &&
2894 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2895 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002896 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002897
2898 int streamId = 0;
2899 if (msg.error_stream != NULL) {
2900 Camera3Stream *stream =
2901 Camera3Stream::cast(msg.error_stream);
2902 streamId = stream->getId();
2903 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002904 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2905 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002906 streamId, msg.error_code);
2907
2908 CaptureResultExtras resultExtras;
2909 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002910 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002911 // SET_ERR calls notifyError
2912 SET_ERR("Camera HAL reported serious device error");
2913 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002914 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2915 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2916 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002917 {
2918 Mutex::Autolock l(mInFlightLock);
2919 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2920 if (idx >= 0) {
2921 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2922 r.requestStatus = msg.error_code;
2923 resultExtras = r.resultExtras;
2924 } else {
2925 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002926 ALOGE("Camera %s: %s: cannot find in-flight request on "
2927 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002928 resultExtras.frameNumber);
2929 }
2930 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002931 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002932 if (listener != NULL) {
2933 listener->notifyError(errorCode, resultExtras);
2934 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002935 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002936 }
2937 break;
2938 default:
2939 // SET_ERR calls notifyError
2940 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2941 break;
2942 }
2943}
2944
2945void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002946 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002947 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002948
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002949 // Set timestamp for the request in the in-flight tracking
2950 // and get the request ID to send upstream
2951 {
2952 Mutex::Autolock l(mInFlightLock);
2953 idx = mInFlightMap.indexOfKey(msg.frame_number);
2954 if (idx >= 0) {
2955 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002956
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002957 // Verify ordering of shutter notifications
2958 {
2959 Mutex::Autolock l(mOutputLock);
2960 // TODO: need to track errors for tighter bounds on expected frame number.
2961 if (r.hasInputBuffer) {
2962 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2963 SET_ERR("Shutter notification out-of-order. Expected "
2964 "notification for frame %d, got frame %d",
2965 mNextReprocessShutterFrameNumber, msg.frame_number);
2966 return;
2967 }
2968 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2969 } else {
2970 if (msg.frame_number < mNextShutterFrameNumber) {
2971 SET_ERR("Shutter notification out-of-order. Expected "
2972 "notification for frame %d, got frame %d",
2973 mNextShutterFrameNumber, msg.frame_number);
2974 return;
2975 }
2976 mNextShutterFrameNumber = msg.frame_number + 1;
2977 }
2978 }
2979
Shuzhen Wang4a472662017-02-26 23:29:04 -08002980 r.shutterTimestamp = msg.timestamp;
2981 if (r.hasCallback) {
2982 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002983 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002984 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
Shuzhen Wang4a472662017-02-26 23:29:04 -08002985 // Call listener, if any
2986 if (listener != NULL) {
2987 listener->notifyShutter(r.resultExtras, msg.timestamp);
2988 }
2989 // send pending result and buffers
2990 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2991 r.collectedPartialResult, msg.frame_number,
2992 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002993 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002994 returnOutputBuffers(r.pendingOutputBuffers.array(),
2995 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2996 r.pendingOutputBuffers.clear();
2997
2998 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002999 }
3000 }
3001 if (idx < 0) {
3002 SET_ERR("Shutter notification for non-existent frame number %d",
3003 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003004 }
3005}
3006
3007
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003008CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003009 ALOGV("%s", __FUNCTION__);
3010
Igor Murashkin1e479c02013-09-06 16:55:14 -07003011 CameraMetadata retVal;
3012
3013 if (mRequestThread != NULL) {
3014 retVal = mRequestThread->getLatestRequest();
3015 }
3016
Igor Murashkin1e479c02013-09-06 16:55:14 -07003017 return retVal;
3018}
3019
Jianing Weicb0652e2014-03-12 18:29:36 -07003020
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003021void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3022 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3023 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3024}
3025
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003026/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003027 * HalInterface inner class methods
3028 */
3029
3030Camera3Device::HalInterface::HalInterface(camera3_device_t *device) :
3031 mHal3Device(device) {}
3032
3033Camera3Device::HalInterface::HalInterface(sp<ICameraDeviceSession> &session) :
3034 mHal3Device(nullptr),
3035 mHidlSession(session) {}
3036
3037Camera3Device::HalInterface::HalInterface() :
3038 mHal3Device(nullptr) {}
3039
3040Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003041 mHal3Device(other.mHal3Device),
3042 mHidlSession(other.mHidlSession) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003043
3044bool Camera3Device::HalInterface::valid() {
3045 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
3046}
3047
3048void Camera3Device::HalInterface::clear() {
3049 mHal3Device = nullptr;
3050 mHidlSession.clear();
3051}
3052
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003053bool Camera3Device::HalInterface::supportBatchRequest() {
3054 return mHidlSession != nullptr;
3055}
3056
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003057status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3058 camera3_request_template_t templateId,
3059 /*out*/ camera_metadata_t **requestTemplate) {
3060 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3061 if (!valid()) return INVALID_OPERATION;
3062 status_t res = OK;
3063
3064 if (mHal3Device != nullptr) {
3065 const camera_metadata *r;
3066 r = mHal3Device->ops->construct_default_request_settings(
3067 mHal3Device, templateId);
3068 if (r == nullptr) return BAD_VALUE;
3069 *requestTemplate = clone_camera_metadata(r);
3070 if (requestTemplate == nullptr) {
3071 ALOGE("%s: Unable to clone camera metadata received from HAL",
3072 __FUNCTION__);
3073 return INVALID_OPERATION;
3074 }
3075 } else {
3076 common::V1_0::Status status;
3077 RequestTemplate id;
3078 switch (templateId) {
3079 case CAMERA3_TEMPLATE_PREVIEW:
3080 id = RequestTemplate::PREVIEW;
3081 break;
3082 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3083 id = RequestTemplate::STILL_CAPTURE;
3084 break;
3085 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3086 id = RequestTemplate::VIDEO_RECORD;
3087 break;
3088 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3089 id = RequestTemplate::VIDEO_SNAPSHOT;
3090 break;
3091 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3092 id = RequestTemplate::ZERO_SHUTTER_LAG;
3093 break;
3094 case CAMERA3_TEMPLATE_MANUAL:
3095 id = RequestTemplate::MANUAL;
3096 break;
3097 default:
3098 // Unknown template ID
3099 return BAD_VALUE;
3100 }
3101 mHidlSession->constructDefaultRequestSettings(id,
3102 [&status, &requestTemplate]
3103 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3104 status = s;
3105 if (status == common::V1_0::Status::OK) {
3106 const camera_metadata *r =
3107 reinterpret_cast<const camera_metadata_t*>(request.data());
3108 size_t expectedSize = request.size();
3109 int ret = validate_camera_metadata_structure(r, &expectedSize);
3110 if (ret == OK) {
3111 *requestTemplate = clone_camera_metadata(r);
3112 if (*requestTemplate == nullptr) {
3113 ALOGE("%s: Unable to clone camera metadata received from HAL",
3114 __FUNCTION__);
3115 status = common::V1_0::Status::INTERNAL_ERROR;
3116 }
3117 } else {
3118 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3119 status = common::V1_0::Status::INTERNAL_ERROR;
3120 }
3121 }
3122 });
3123 res = CameraProviderManager::mapToStatusT(status);
3124 }
3125 return res;
3126}
3127
3128status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3129 ATRACE_NAME("CameraHal::configureStreams");
3130 if (!valid()) return INVALID_OPERATION;
3131 status_t res = OK;
3132
3133 if (mHal3Device != nullptr) {
3134 res = mHal3Device->ops->configure_streams(mHal3Device, config);
3135 } else {
3136 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003137 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003138 StreamConfiguration requestedConfiguration;
3139 requestedConfiguration.streams.resize(config->num_streams);
3140 for (size_t i = 0; i < config->num_streams; i++) {
3141 Stream &dst = requestedConfiguration.streams[i];
3142 camera3_stream_t *src = config->streams[i];
3143
3144 int streamId = Camera3Stream::cast(src)->getId();
3145 StreamType streamType;
3146 switch (src->stream_type) {
3147 case CAMERA3_STREAM_OUTPUT:
3148 streamType = StreamType::OUTPUT;
3149 break;
3150 case CAMERA3_STREAM_INPUT:
3151 streamType = StreamType::INPUT;
3152 break;
3153 default:
3154 ALOGE("%s: Stream %d: Unsupported stream type %d",
3155 __FUNCTION__, streamId, config->streams[i]->stream_type);
3156 return BAD_VALUE;
3157 }
3158 dst.id = streamId;
3159 dst.streamType = streamType;
3160 dst.width = src->width;
3161 dst.height = src->height;
3162 dst.format = mapToPixelFormat(src->format);
Eino-Ville Talvala1a2f15d2017-03-23 15:37:22 -07003163 uint64_t consumerUsage, producerUsage;
3164 ::android_convertGralloc0To1Usage(src->usage, &producerUsage, &consumerUsage);
3165 dst.usage = consumerUsage;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003166 dst.dataSpace = mapToHidlDataspace(src->data_space);
3167 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003168
3169 activeStreams.insert(streamId);
3170 // Create Buffer ID map if necessary
3171 if (mBufferIdMaps.count(streamId) == 0) {
3172 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3173 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003174 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003175 // remove BufferIdMap for deleted streams
3176 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3177 int streamId = it->first;
3178 bool active = activeStreams.count(streamId) > 0;
3179 if (!active) {
3180 it = mBufferIdMaps.erase(it);
3181 } else {
3182 ++it;
3183 }
3184 }
3185
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08003186 res = mapToStreamConfigurationMode(
3187 (camera3_stream_configuration_mode_t) config->operation_mode,
3188 /*out*/ &requestedConfiguration.operationMode);
3189 if (res != OK) {
3190 return res;
3191 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003192
3193 // Invoke configureStreams
3194
3195 HalStreamConfiguration finalConfiguration;
3196 common::V1_0::Status status;
3197 mHidlSession->configureStreams(requestedConfiguration,
3198 [&status, &finalConfiguration]
3199 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3200 finalConfiguration = halConfiguration;
3201 status = s;
3202 });
3203 if (status != common::V1_0::Status::OK ) {
3204 return CameraProviderManager::mapToStatusT(status);
3205 }
3206
3207 // And convert output stream configuration from HIDL
3208
3209 for (size_t i = 0; i < config->num_streams; i++) {
3210 camera3_stream_t *dst = config->streams[i];
3211 int streamId = Camera3Stream::cast(dst)->getId();
3212
3213 // Start scan at i, with the assumption that the stream order matches
3214 size_t realIdx = i;
3215 bool found = false;
3216 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3217 if (finalConfiguration.streams[realIdx].id == streamId) {
3218 found = true;
3219 break;
3220 }
3221 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3222 }
3223 if (!found) {
3224 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3225 __FUNCTION__, streamId);
3226 return INVALID_OPERATION;
3227 }
3228 HalStream &src = finalConfiguration.streams[realIdx];
3229
3230 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3231 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3232 if (dst->format != overrideFormat) {
3233 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3234 streamId, dst->format);
3235 }
3236 } else {
3237 // Override allowed with IMPLEMENTATION_DEFINED
3238 dst->format = overrideFormat;
3239 }
3240
3241 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3242 if (src.producerUsage != 0) {
3243 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3244 __FUNCTION__, streamId);
3245 return INVALID_OPERATION;
3246 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003247 } else {
3248 // OUTPUT
3249 if (src.consumerUsage != 0) {
3250 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3251 __FUNCTION__, streamId);
3252 return INVALID_OPERATION;
3253 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003254 }
Eino-Ville Talvala1a2f15d2017-03-23 15:37:22 -07003255 dst->usage = ::android_convertGralloc1To0Usage(src.producerUsage, src.consumerUsage);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003256 dst->max_buffers = src.maxBuffers;
3257 }
3258 }
3259 return res;
3260}
3261
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003262void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3263 /*out*/device::V3_2::CaptureRequest* captureRequest,
3264 /*out*/std::vector<native_handle_t*>* handlesCreated) {
3265
3266 if (captureRequest == nullptr || handlesCreated == nullptr) {
3267 ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3268 __FUNCTION__, captureRequest, handlesCreated);
3269 return;
3270 }
3271
3272 captureRequest->frameNumber = request->frame_number;
3273 // A null request settings maps to a size-0 CameraMetadata
3274 if (request->settings != nullptr) {
3275 captureRequest->settings.setToExternal(
3276 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3277 get_camera_metadata_size(request->settings));
3278 }
3279
3280 {
3281 std::lock_guard<std::mutex> lock(mInflightLock);
3282 if (request->input_buffer != nullptr) {
3283 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3284 buffer_handle_t buf = *(request->input_buffer->buffer);
3285 auto pair = getBufferId(buf, streamId);
3286 bool isNewBuffer = pair.first;
3287 uint64_t bufferId = pair.second;
3288 captureRequest->inputBuffer.streamId = streamId;
3289 captureRequest->inputBuffer.bufferId = bufferId;
3290 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3291 captureRequest->inputBuffer.status = BufferStatus::OK;
3292 native_handle_t *acquireFence = nullptr;
3293 if (request->input_buffer->acquire_fence != -1) {
3294 acquireFence = native_handle_create(1,0);
3295 acquireFence->data[0] = request->input_buffer->acquire_fence;
3296 handlesCreated->push_back(acquireFence);
3297 }
3298 captureRequest->inputBuffer.acquireFence = acquireFence;
3299 captureRequest->inputBuffer.releaseFence = nullptr;
3300
3301 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3302 request->input_buffer->buffer,
3303 request->input_buffer->acquire_fence);
3304 } else {
3305 captureRequest->inputBuffer.streamId = -1;
3306 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3307 }
3308
3309 captureRequest->outputBuffers.resize(request->num_output_buffers);
3310 for (size_t i = 0; i < request->num_output_buffers; i++) {
3311 const camera3_stream_buffer_t *src = request->output_buffers + i;
3312 StreamBuffer &dst = captureRequest->outputBuffers[i];
3313 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3314 buffer_handle_t buf = *(src->buffer);
3315 auto pair = getBufferId(buf, streamId);
3316 bool isNewBuffer = pair.first;
3317 dst.streamId = streamId;
3318 dst.bufferId = pair.second;
3319 dst.buffer = isNewBuffer ? buf : nullptr;
3320 dst.status = BufferStatus::OK;
3321 native_handle_t *acquireFence = nullptr;
3322 if (src->acquire_fence != -1) {
3323 acquireFence = native_handle_create(1,0);
3324 acquireFence->data[0] = src->acquire_fence;
3325 handlesCreated->push_back(acquireFence);
3326 }
3327 dst.acquireFence = acquireFence;
3328 dst.releaseFence = nullptr;
3329
3330 pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3331 src->buffer, src->acquire_fence);
3332 }
3333 }
3334}
3335
3336status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3337 std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3338 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3339 if (!valid()) return INVALID_OPERATION;
3340
3341 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3342 size_t batchSize = requests.size();
3343 captureRequests.resize(batchSize);
3344 std::vector<native_handle_t*> handlesCreated;
3345
3346 for (size_t i = 0; i < batchSize; i++) {
3347 wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3348 }
3349
3350 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3351 *numRequestProcessed = 0;
3352 mHidlSession->processCaptureRequest(captureRequests,
3353 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3354 status = s;
3355 *numRequestProcessed = n;
3356 });
3357
3358 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3359 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3360 __FUNCTION__, *numRequestProcessed, batchSize);
3361 status = common::V1_0::Status::INTERNAL_ERROR;
3362 }
3363
3364 for (auto& handle : handlesCreated) {
3365 native_handle_delete(handle);
3366 }
3367 return CameraProviderManager::mapToStatusT(status);
3368}
3369
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003370status_t Camera3Device::HalInterface::processCaptureRequest(
3371 camera3_capture_request_t *request) {
3372 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003373 if (!valid()) return INVALID_OPERATION;
3374 status_t res = OK;
3375
3376 if (mHal3Device != nullptr) {
3377 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3378 } else {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003379 uint32_t numRequestProcessed = 0;
3380 std::vector<camera3_capture_request_t*> requests(1);
3381 requests[0] = request;
3382 res = processBatchCaptureRequests(requests, &numRequestProcessed);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003383 }
3384 return res;
3385}
3386
3387status_t Camera3Device::HalInterface::flush() {
3388 ATRACE_NAME("CameraHal::flush");
3389 if (!valid()) return INVALID_OPERATION;
3390 status_t res = OK;
3391
3392 if (mHal3Device != nullptr) {
3393 res = mHal3Device->ops->flush(mHal3Device);
3394 } else {
3395 res = CameraProviderManager::mapToStatusT(mHidlSession->flush());
3396 }
3397 return res;
3398}
3399
3400status_t Camera3Device::HalInterface::dump(int fd) {
3401 ATRACE_NAME("CameraHal::dump");
3402 if (!valid()) return INVALID_OPERATION;
3403 status_t res = OK;
3404
3405 if (mHal3Device != nullptr) {
3406 mHal3Device->ops->dump(mHal3Device, fd);
3407 } else {
3408 // Handled by CameraProviderManager::dump
3409 }
3410 return res;
3411}
3412
3413status_t Camera3Device::HalInterface::close() {
3414 ATRACE_NAME("CameraHal::close()");
3415 if (!valid()) return INVALID_OPERATION;
3416 status_t res = OK;
3417
3418 if (mHal3Device != nullptr) {
3419 mHal3Device->common.close(&mHal3Device->common);
3420 } else {
3421 mHidlSession->close();
3422 }
3423 return res;
3424}
3425
3426status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003427 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003428 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003429 auto pair = std::make_pair(buffer, acquireFence);
3430 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003431 return OK;
3432}
3433
3434status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003435 int32_t frameNumber, int32_t streamId,
3436 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003437 std::lock_guard<std::mutex> lock(mInflightLock);
3438
3439 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3440 auto it = mInflightBufferMap.find(key);
3441 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003442 auto pair = it->second;
3443 *buffer = pair.first;
3444 int acquireFence = pair.second;
3445 if (acquireFence > 0) {
3446 ::close(acquireFence);
3447 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003448 mInflightBufferMap.erase(it);
3449 return OK;
3450}
3451
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003452std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3453 const buffer_handle_t& buf, int streamId) {
3454 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3455
3456 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3457 auto it = bIdMap.find(buf);
3458 if (it == bIdMap.end()) {
3459 bIdMap[buf] = mNextBufferId++;
3460 return std::make_pair(true, mNextBufferId - 1);
3461 } else {
3462 return std::make_pair(false, it->second);
3463 }
3464}
3465
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003466/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003467 * RequestThread inner class methods
3468 */
3469
3470Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003471 sp<StatusTracker> statusTracker,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003472 HalInterface* interface,
3473 uint32_t deviceVersion,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07003474 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003475 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003476 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003477 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003478 mInterface(interface),
3479 mDeviceVersion(deviceVersion),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003480 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003481 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003482 mReconfigured(false),
3483 mDoPause(false),
3484 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003485 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003486 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003487 mCurrentAfTriggerId(0),
3488 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003489 mRepeatingLastFrameNumber(
3490 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003491 mAeLockAvailable(aeLockAvailable),
3492 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003493 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003494}
3495
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003496Camera3Device::RequestThread::~RequestThread() {}
3497
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003498void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003499 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003500 Mutex::Autolock l(mRequestLock);
3501 mListener = listener;
3502}
3503
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003504void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003505 Mutex::Autolock l(mRequestLock);
3506 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003507 // Prepare video stream for high speed recording.
3508 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003509}
3510
Jianing Wei90e59c92014-03-12 18:29:36 -07003511status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003512 List<sp<CaptureRequest> > &requests,
3513 /*out*/
3514 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003515 Mutex::Autolock l(mRequestLock);
3516 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3517 ++it) {
3518 mRequestQueue.push_back(*it);
3519 }
3520
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003521 if (lastFrameNumber != NULL) {
3522 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3523 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3524 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3525 *lastFrameNumber);
3526 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003527
Jianing Wei90e59c92014-03-12 18:29:36 -07003528 unpauseForNewRequests();
3529
3530 return OK;
3531}
3532
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003533
3534status_t Camera3Device::RequestThread::queueTrigger(
3535 RequestTrigger trigger[],
3536 size_t count) {
3537
3538 Mutex::Autolock l(mTriggerMutex);
3539 status_t ret;
3540
3541 for (size_t i = 0; i < count; ++i) {
3542 ret = queueTriggerLocked(trigger[i]);
3543
3544 if (ret != OK) {
3545 return ret;
3546 }
3547 }
3548
3549 return OK;
3550}
3551
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003552const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3553 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003554 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003555 if (d != nullptr) return d->mId;
3556 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003557}
3558
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003559status_t Camera3Device::RequestThread::queueTriggerLocked(
3560 RequestTrigger trigger) {
3561
3562 uint32_t tag = trigger.metadataTag;
3563 ssize_t index = mTriggerMap.indexOfKey(tag);
3564
3565 switch (trigger.getTagType()) {
3566 case TYPE_BYTE:
3567 // fall-through
3568 case TYPE_INT32:
3569 break;
3570 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003571 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3572 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003573 return INVALID_OPERATION;
3574 }
3575
3576 /**
3577 * Collect only the latest trigger, since we only have 1 field
3578 * in the request settings per trigger tag, and can't send more than 1
3579 * trigger per request.
3580 */
3581 if (index != NAME_NOT_FOUND) {
3582 mTriggerMap.editValueAt(index) = trigger;
3583 } else {
3584 mTriggerMap.add(tag, trigger);
3585 }
3586
3587 return OK;
3588}
3589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003590status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003591 const RequestList &requests,
3592 /*out*/
3593 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003594 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003595 if (lastFrameNumber != NULL) {
3596 *lastFrameNumber = mRepeatingLastFrameNumber;
3597 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003598 mRepeatingRequests.clear();
3599 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3600 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003601
3602 unpauseForNewRequests();
3603
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003604 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003605 return OK;
3606}
3607
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003608bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003609 if (mRepeatingRequests.empty()) {
3610 return false;
3611 }
3612 int32_t requestId = requestIn->mResultExtras.requestId;
3613 const RequestList &repeatRequests = mRepeatingRequests;
3614 // All repeating requests are guaranteed to have same id so only check first quest
3615 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3616 return (firstRequest->mResultExtras.requestId == requestId);
3617}
3618
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003619status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003620 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003621 return clearRepeatingRequestsLocked(lastFrameNumber);
3622
3623}
3624
3625status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003626 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003627 if (lastFrameNumber != NULL) {
3628 *lastFrameNumber = mRepeatingLastFrameNumber;
3629 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003630 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003631 return OK;
3632}
3633
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003634status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003635 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003636 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003637 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003638
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003639 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003640
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003641 // Send errors for all requests pending in the request queue, including
3642 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003643 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003644 if (listener != NULL) {
3645 for (RequestList::iterator it = mRequestQueue.begin();
3646 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003647 // Abort the input buffers for reprocess requests.
3648 if ((*it)->mInputStream != NULL) {
3649 camera3_stream_buffer_t inputBuffer;
3650 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
3651 if (res != OK) {
3652 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3653 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3654 } else {
3655 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3656 if (res != OK) {
3657 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3658 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3659 }
3660 }
3661 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003662 // Set the frame number this request would have had, if it
3663 // had been submitted; this frame number will not be reused.
3664 // The requestId and burstId fields were set when the request was
3665 // submitted originally (in convertMetadataListToRequestListLocked)
3666 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003667 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003668 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003669 }
3670 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003671 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003672
3673 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003674 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003675 if (lastFrameNumber != NULL) {
3676 *lastFrameNumber = mRepeatingLastFrameNumber;
3677 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003678 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003679 return OK;
3680}
3681
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003682status_t Camera3Device::RequestThread::flush() {
3683 ATRACE_CALL();
3684 Mutex::Autolock l(mFlushLock);
3685
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003686 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
3687 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003688 }
3689
3690 return -ENOTSUP;
3691}
3692
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003693void Camera3Device::RequestThread::setPaused(bool paused) {
3694 Mutex::Autolock l(mPauseLock);
3695 mDoPause = paused;
3696 mDoPauseSignal.signal();
3697}
3698
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003699status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3700 int32_t requestId, nsecs_t timeout) {
3701 Mutex::Autolock l(mLatestRequestMutex);
3702 status_t res;
3703 while (mLatestRequestId != requestId) {
3704 nsecs_t startTime = systemTime();
3705
3706 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3707 if (res != OK) return res;
3708
3709 timeout -= (systemTime() - startTime);
3710 }
3711
3712 return OK;
3713}
3714
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003715void Camera3Device::RequestThread::requestExit() {
3716 // Call parent to set up shutdown
3717 Thread::requestExit();
3718 // The exit from any possible waits
3719 mDoPauseSignal.signal();
3720 mRequestSignal.signal();
3721}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003722
Chien-Yu Chend196d612015-06-22 19:49:01 -07003723
3724/**
3725 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3726 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3727 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3728 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3729 * request.
3730 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003731void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003732 request->mAeTriggerCancelOverride.applyAeLock = false;
3733 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3734
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003735 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003736 return;
3737 }
3738
3739 camera_metadata_entry_t aePrecaptureTrigger =
3740 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3741 if (aePrecaptureTrigger.count > 0 &&
3742 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3743 // Always override CANCEL to IDLE
3744 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3745 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3746 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3747 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3748 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3749
3750 if (mAeLockAvailable == true) {
3751 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3752 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3753 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3754 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3755 request->mAeTriggerCancelOverride.applyAeLock = true;
3756 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3757 }
3758 }
3759 }
3760}
3761
3762/**
3763 * Override result metadata for cancelling AE precapture trigger applied in
3764 * handleAePrecaptureCancelRequest().
3765 */
3766void Camera3Device::overrideResultForPrecaptureCancel(
3767 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3768 if (aeTriggerCancelOverride.applyAeLock) {
3769 // Only devices <= v3.2 should have this override
3770 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3771 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3772 }
3773
3774 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3775 // Only devices <= v3.2 should have this override
3776 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3777 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3778 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3779 }
3780}
3781
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003782void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003783 bool surfaceAbandoned = false;
3784 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003785 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003786 {
3787 Mutex::Autolock l(mRequestLock);
3788 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3789 // repeating requests.
3790 for (const auto& request : mRepeatingRequests) {
3791 for (const auto& s : request->mOutputStreams) {
3792 if (s->isAbandoned()) {
3793 surfaceAbandoned = true;
3794 clearRepeatingRequestsLocked(&lastFrameNumber);
3795 break;
3796 }
3797 }
3798 if (surfaceAbandoned) {
3799 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003800 }
3801 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003802 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003803 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003804
3805 if (listener != NULL && surfaceAbandoned) {
3806 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003807 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003808}
3809
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003810bool Camera3Device::RequestThread::sendRequestsBatch() {
3811 status_t res;
3812 size_t batchSize = mNextRequests.size();
3813 std::vector<camera3_capture_request_t*> requests(batchSize);
3814 uint32_t numRequestProcessed = 0;
3815 for (size_t i = 0; i < batchSize; i++) {
3816 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3817 }
3818
3819 ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
3820 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3821
3822 bool triggerRemoveFailed = false;
3823 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3824 for (size_t i = 0; i < numRequestProcessed; i++) {
3825 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3826 nextRequest.submitted = true;
3827
3828
3829 // Update the latest request sent to HAL
3830 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3831 Mutex::Autolock al(mLatestRequestMutex);
3832
3833 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3834 mLatestRequest.acquire(cloned);
3835
3836 sp<Camera3Device> parent = mParent.promote();
3837 if (parent != NULL) {
3838 parent->monitorMetadata(TagMonitor::REQUEST,
3839 nextRequest.halRequest.frame_number,
3840 0, mLatestRequest);
3841 }
3842 }
3843
3844 if (nextRequest.halRequest.settings != NULL) {
3845 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3846 }
3847
3848 if (!triggerRemoveFailed) {
3849 // Remove any previously queued triggers (after unlock)
3850 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3851 if (removeTriggerRes != OK) {
3852 triggerRemoveFailed = true;
3853 triggerFailedRequest = nextRequest;
3854 }
3855 }
3856 }
3857
3858 if (triggerRemoveFailed) {
3859 SET_ERR("RequestThread: Unable to remove triggers "
3860 "(capture request %d, HAL device: %s (%d)",
3861 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3862 cleanUpFailedRequests(/*sendRequestError*/ false);
3863 return false;
3864 }
3865
3866 if (res != OK) {
3867 // Should only get a failure here for malformed requests or device-level
3868 // errors, so consider all errors fatal. Bad metadata failures should
3869 // come through notify.
3870 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3871 mNextRequests[numRequestProcessed].halRequest.frame_number,
3872 strerror(-res), res);
3873 cleanUpFailedRequests(/*sendRequestError*/ false);
3874 return false;
3875 }
3876 return true;
3877}
3878
3879bool Camera3Device::RequestThread::sendRequestsOneByOne() {
3880 status_t res;
3881
3882 for (auto& nextRequest : mNextRequests) {
3883 // Submit request and block until ready for next one
3884 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
3885 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
3886
3887 if (res != OK) {
3888 // Should only get a failure here for malformed requests or device-level
3889 // errors, so consider all errors fatal. Bad metadata failures should
3890 // come through notify.
3891 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3892 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3893 res);
3894 cleanUpFailedRequests(/*sendRequestError*/ false);
3895 return false;
3896 }
3897
3898 // Mark that the request has be submitted successfully.
3899 nextRequest.submitted = true;
3900
3901 // Update the latest request sent to HAL
3902 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3903 Mutex::Autolock al(mLatestRequestMutex);
3904
3905 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3906 mLatestRequest.acquire(cloned);
3907
3908 sp<Camera3Device> parent = mParent.promote();
3909 if (parent != NULL) {
3910 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3911 0, mLatestRequest);
3912 }
3913 }
3914
3915 if (nextRequest.halRequest.settings != NULL) {
3916 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3917 }
3918
3919 // Remove any previously queued triggers (after unlock)
3920 res = removeTriggers(mPrevRequest);
3921 if (res != OK) {
3922 SET_ERR("RequestThread: Unable to remove triggers "
3923 "(capture request %d, HAL device: %s (%d)",
3924 nextRequest.halRequest.frame_number, strerror(-res), res);
3925 cleanUpFailedRequests(/*sendRequestError*/ false);
3926 return false;
3927 }
3928 }
3929 return true;
3930}
3931
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003932bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003933 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003934 status_t res;
3935
3936 // Handle paused state.
3937 if (waitIfPaused()) {
3938 return true;
3939 }
3940
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003941 // Wait for the next batch of requests.
3942 waitForNextRequestBatch();
3943 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003944 return true;
3945 }
3946
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003947 // Get the latest request ID, if any
3948 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003949 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003950 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003951 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003952 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003953 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003954 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3955 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003956 }
3957
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003958 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003959 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003960 if (res == TIMED_OUT) {
3961 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003962 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003963 // Check if any stream is abandoned.
3964 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003965 return true;
3966 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003967 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003968 return false;
3969 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003970
Zhijun Hecc27e112013-10-03 16:12:43 -07003971 // Inform waitUntilRequestProcessed thread of a new request ID
3972 {
3973 Mutex::Autolock al(mLatestRequestMutex);
3974
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003975 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003976 mLatestRequestSignal.signal();
3977 }
3978
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003979 // Submit a batch of requests to HAL.
3980 // Use flush lock only when submitting multilple requests in a batch.
3981 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3982 // which may take a long time to finish so synchronizing flush() and
3983 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3984 // For now, only synchronize for high speed recording and we should figure something out for
3985 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003986 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003987
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003988 if (useFlushLock) {
3989 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003990 }
3991
Zhijun Hef0645c12016-08-02 00:58:11 -07003992 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003993 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003994
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003995 bool submitRequestSuccess = false;
3996 if (mInterface->supportBatchRequest()) {
3997 submitRequestSuccess = sendRequestsBatch();
3998 } else {
3999 submitRequestSuccess = sendRequestsOneByOne();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004000 }
4001
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004002 if (useFlushLock) {
4003 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004004 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004005
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004006 // Unset as current request
4007 {
4008 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004009 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004010 }
4011
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004012 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004013}
4014
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004015status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004016 ATRACE_CALL();
4017
Shuzhen Wang4a472662017-02-26 23:29:04 -08004018 for (size_t i = 0; i < mNextRequests.size(); i++) {
4019 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004020 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4021 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4022 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4023
4024 // Prepare a request to HAL
4025 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4026
4027 // Insert any queued triggers (before metadata is locked)
4028 status_t res = insertTriggers(captureRequest);
4029
4030 if (res < 0) {
4031 SET_ERR("RequestThread: Unable to insert triggers "
4032 "(capture request %d, HAL device: %s (%d)",
4033 halRequest->frame_number, strerror(-res), res);
4034 return INVALID_OPERATION;
4035 }
4036 int triggerCount = res;
4037 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4038 mPrevTriggers = triggerCount;
4039
4040 // If the request is the same as last, or we had triggers last time
4041 if (mPrevRequest != captureRequest || triggersMixedIn) {
4042 /**
4043 * HAL workaround:
4044 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4045 */
4046 res = addDummyTriggerIds(captureRequest);
4047 if (res != OK) {
4048 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4049 "(capture request %d, HAL device: %s (%d)",
4050 halRequest->frame_number, strerror(-res), res);
4051 return INVALID_OPERATION;
4052 }
4053
4054 /**
4055 * The request should be presorted so accesses in HAL
4056 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4057 */
4058 captureRequest->mSettings.sort();
4059 halRequest->settings = captureRequest->mSettings.getAndLock();
4060 mPrevRequest = captureRequest;
4061 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4062
4063 IF_ALOGV() {
4064 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4065 find_camera_metadata_ro_entry(
4066 halRequest->settings,
4067 ANDROID_CONTROL_AF_TRIGGER,
4068 &e
4069 );
4070 if (e.count > 0) {
4071 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4072 __FUNCTION__,
4073 halRequest->frame_number,
4074 e.data.u8[0]);
4075 }
4076 }
4077 } else {
4078 // leave request.settings NULL to indicate 'reuse latest given'
4079 ALOGVV("%s: Request settings are REUSED",
4080 __FUNCTION__);
4081 }
4082
4083 uint32_t totalNumBuffers = 0;
4084
4085 // Fill in buffers
4086 if (captureRequest->mInputStream != NULL) {
4087 halRequest->input_buffer = &captureRequest->mInputBuffer;
4088 totalNumBuffers += 1;
4089 } else {
4090 halRequest->input_buffer = NULL;
4091 }
4092
4093 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4094 captureRequest->mOutputStreams.size());
4095 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang4a472662017-02-26 23:29:04 -08004096 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4097 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004098
4099 // Prepare video buffers for high speed recording on the first video request.
4100 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4101 // Only try to prepare video stream on the first video request.
4102 mPrepareVideoStream = false;
4103
4104 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4105 while (res == NOT_ENOUGH_DATA) {
4106 res = outputStream->prepareNextBuffer();
4107 }
4108 if (res != OK) {
4109 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4110 __FUNCTION__, strerror(-res), res);
4111 outputStream->cancelPrepare();
4112 }
4113 }
4114
Shuzhen Wang4a472662017-02-26 23:29:04 -08004115 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4116 captureRequest->mOutputSurfaces[j]);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004117 if (res != OK) {
4118 // Can't get output buffer from gralloc queue - this could be due to
4119 // abandoned queue or other consumer misbehavior, so not a fatal
4120 // error
4121 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4122 " %s (%d)", strerror(-res), res);
4123
4124 return TIMED_OUT;
4125 }
4126 halRequest->num_output_buffers++;
Shuzhen Wang0129d522016-10-30 22:43:41 -07004127
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004128 }
4129 totalNumBuffers += halRequest->num_output_buffers;
4130
4131 // Log request in the in-flight queue
4132 sp<Camera3Device> parent = mParent.promote();
4133 if (parent == NULL) {
4134 // Should not happen, and nowhere to send errors to, so just log it
4135 CLOGE("RequestThread: Parent is gone");
4136 return INVALID_OPERATION;
4137 }
Shuzhen Wang4a472662017-02-26 23:29:04 -08004138
4139 // If this request list is for constrained high speed recording (not
4140 // preview), and the current request is not the last one in the batch,
4141 // do not send callback to the app.
4142 bool hasCallback = true;
4143 if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4144 hasCallback = false;
4145 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004146 res = parent->registerInFlight(halRequest->frame_number,
4147 totalNumBuffers, captureRequest->mResultExtras,
4148 /*hasInput*/halRequest->input_buffer != NULL,
Shuzhen Wang4a472662017-02-26 23:29:04 -08004149 captureRequest->mAeTriggerCancelOverride,
4150 hasCallback);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004151 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4152 ", burstId = %" PRId32 ".",
4153 __FUNCTION__,
4154 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4155 captureRequest->mResultExtras.burstId);
4156 if (res != OK) {
4157 SET_ERR("RequestThread: Unable to register new in-flight request:"
4158 " %s (%d)", strerror(-res), res);
4159 return INVALID_OPERATION;
4160 }
4161 }
4162
4163 return OK;
4164}
4165
Igor Murashkin1e479c02013-09-06 16:55:14 -07004166CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4167 Mutex::Autolock al(mLatestRequestMutex);
4168
4169 ALOGV("RequestThread::%s", __FUNCTION__);
4170
4171 return mLatestRequest;
4172}
4173
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004174bool Camera3Device::RequestThread::isStreamPending(
4175 sp<Camera3StreamInterface>& stream) {
4176 Mutex::Autolock l(mRequestLock);
4177
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004178 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004179 if (!nextRequest.submitted) {
4180 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4181 if (stream == s) return true;
4182 }
4183 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004184 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004185 }
4186
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004187 for (const auto& request : mRequestQueue) {
4188 for (const auto& s : request->mOutputStreams) {
4189 if (stream == s) return true;
4190 }
4191 if (stream == request->mInputStream) return true;
4192 }
4193
4194 for (const auto& request : mRepeatingRequests) {
4195 for (const auto& s : request->mOutputStreams) {
4196 if (stream == s) return true;
4197 }
4198 if (stream == request->mInputStream) return true;
4199 }
4200
4201 return false;
4202}
Jianing Weicb0652e2014-03-12 18:29:36 -07004203
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004204void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4205 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004206 return;
4207 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004208
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004209 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004210 // Skip the ones that have been submitted successfully.
4211 if (nextRequest.submitted) {
4212 continue;
4213 }
4214
4215 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4216 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4217 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4218
4219 if (halRequest->settings != NULL) {
4220 captureRequest->mSettings.unlock(halRequest->settings);
4221 }
4222
4223 if (captureRequest->mInputStream != NULL) {
4224 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4225 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4226 }
4227
4228 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4229 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4230 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4231 }
4232
4233 if (sendRequestError) {
4234 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004235 sp<NotificationListener> listener = mListener.promote();
4236 if (listener != NULL) {
4237 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004238 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004239 captureRequest->mResultExtras);
4240 }
4241 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004242
4243 // Remove yet-to-be submitted inflight request from inflightMap
4244 {
4245 sp<Camera3Device> parent = mParent.promote();
4246 if (parent != NULL) {
4247 Mutex::Autolock l(parent->mInFlightLock);
4248 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4249 if (idx >= 0) {
4250 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4251 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4252 parent->removeInFlightMapEntryLocked(idx);
4253 }
4254 }
4255 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004256 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004257
4258 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004259 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004260}
4261
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004262void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004263 // Optimized a bit for the simple steady-state case (single repeating
4264 // request), to avoid putting that request in the queue temporarily.
4265 Mutex::Autolock l(mRequestLock);
4266
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004267 assert(mNextRequests.empty());
4268
4269 NextRequest nextRequest;
4270 nextRequest.captureRequest = waitForNextRequestLocked();
4271 if (nextRequest.captureRequest == nullptr) {
4272 return;
4273 }
4274
4275 nextRequest.halRequest = camera3_capture_request_t();
4276 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004277 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004278
4279 // Wait for additional requests
4280 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4281
4282 for (size_t i = 1; i < batchSize; i++) {
4283 NextRequest additionalRequest;
4284 additionalRequest.captureRequest = waitForNextRequestLocked();
4285 if (additionalRequest.captureRequest == nullptr) {
4286 break;
4287 }
4288
4289 additionalRequest.halRequest = camera3_capture_request_t();
4290 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004291 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004292 }
4293
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004294 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004295 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004296 mNextRequests.size(), batchSize);
4297 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004298 }
4299
4300 return;
4301}
4302
4303sp<Camera3Device::CaptureRequest>
4304 Camera3Device::RequestThread::waitForNextRequestLocked() {
4305 status_t res;
4306 sp<CaptureRequest> nextRequest;
4307
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004308 while (mRequestQueue.empty()) {
4309 if (!mRepeatingRequests.empty()) {
4310 // Always atomically enqueue all requests in a repeating request
4311 // list. Guarantees a complete in-sequence set of captures to
4312 // application.
4313 const RequestList &requests = mRepeatingRequests;
4314 RequestList::const_iterator firstRequest =
4315 requests.begin();
4316 nextRequest = *firstRequest;
4317 mRequestQueue.insert(mRequestQueue.end(),
4318 ++firstRequest,
4319 requests.end());
4320 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004321
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004322 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004323
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004324 break;
4325 }
4326
4327 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4328
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004329 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4330 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004331 Mutex::Autolock pl(mPauseLock);
4332 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004333 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004334 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004335 // Let the tracker know
4336 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4337 if (statusTracker != 0) {
4338 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4339 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004340 }
4341 // Stop waiting for now and let thread management happen
4342 return NULL;
4343 }
4344 }
4345
4346 if (nextRequest == NULL) {
4347 // Don't have a repeating request already in hand, so queue
4348 // must have an entry now.
4349 RequestList::iterator firstRequest =
4350 mRequestQueue.begin();
4351 nextRequest = *firstRequest;
4352 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004353 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4354 sp<NotificationListener> listener = mListener.promote();
4355 if (listener != NULL) {
4356 listener->notifyRequestQueueEmpty();
4357 }
4358 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004359 }
4360
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004361 // In case we've been unpaused by setPaused clearing mDoPause, need to
4362 // update internal pause state (capture/setRepeatingRequest unpause
4363 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004364 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004365 if (mPaused) {
4366 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4367 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4368 if (statusTracker != 0) {
4369 statusTracker->markComponentActive(mStatusId);
4370 }
4371 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004372 mPaused = false;
4373
4374 // Check if we've reconfigured since last time, and reset the preview
4375 // request if so. Can't use 'NULL request == repeat' across configure calls.
4376 if (mReconfigured) {
4377 mPrevRequest.clear();
4378 mReconfigured = false;
4379 }
4380
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004381 if (nextRequest != NULL) {
4382 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004383 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4384 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004385
4386 // Since RequestThread::clear() removes buffers from the input stream,
4387 // get the right buffer here before unlocking mRequestLock
4388 if (nextRequest->mInputStream != NULL) {
4389 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4390 if (res != OK) {
4391 // Can't get input buffer from gralloc queue - this could be due to
4392 // disconnected queue or other producer misbehavior, so not a fatal
4393 // error
4394 ALOGE("%s: Can't get input buffer, skipping request:"
4395 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004396
4397 sp<NotificationListener> listener = mListener.promote();
4398 if (listener != NULL) {
4399 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004400 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004401 nextRequest->mResultExtras);
4402 }
4403 return NULL;
4404 }
4405 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004406 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004407
4408 handleAePrecaptureCancelRequest(nextRequest);
4409
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004410 return nextRequest;
4411}
4412
4413bool Camera3Device::RequestThread::waitIfPaused() {
4414 status_t res;
4415 Mutex::Autolock l(mPauseLock);
4416 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004417 if (mPaused == false) {
4418 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004419 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4420 // Let the tracker know
4421 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4422 if (statusTracker != 0) {
4423 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4424 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004425 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004426
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004427 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004428 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004429 return true;
4430 }
4431 }
4432 // We don't set mPaused to false here, because waitForNextRequest needs
4433 // to further manage the paused state in case of starvation.
4434 return false;
4435}
4436
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004437void Camera3Device::RequestThread::unpauseForNewRequests() {
4438 // With work to do, mark thread as unpaused.
4439 // If paused by request (setPaused), don't resume, to avoid
4440 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004441 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004442 Mutex::Autolock p(mPauseLock);
4443 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004444 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4445 if (mPaused) {
4446 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4447 if (statusTracker != 0) {
4448 statusTracker->markComponentActive(mStatusId);
4449 }
4450 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004451 mPaused = false;
4452 }
4453}
4454
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004455void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4456 sp<Camera3Device> parent = mParent.promote();
4457 if (parent != NULL) {
4458 va_list args;
4459 va_start(args, fmt);
4460
4461 parent->setErrorStateV(fmt, args);
4462
4463 va_end(args);
4464 }
4465}
4466
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004467status_t Camera3Device::RequestThread::insertTriggers(
4468 const sp<CaptureRequest> &request) {
4469
4470 Mutex::Autolock al(mTriggerMutex);
4471
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004472 sp<Camera3Device> parent = mParent.promote();
4473 if (parent == NULL) {
4474 CLOGE("RequestThread: Parent is gone");
4475 return DEAD_OBJECT;
4476 }
4477
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004478 CameraMetadata &metadata = request->mSettings;
4479 size_t count = mTriggerMap.size();
4480
4481 for (size_t i = 0; i < count; ++i) {
4482 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004483 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004484
4485 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4486 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4487 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004488 if (isAeTrigger) {
4489 request->mResultExtras.precaptureTriggerId = triggerId;
4490 mCurrentPreCaptureTriggerId = triggerId;
4491 } else {
4492 request->mResultExtras.afTriggerId = triggerId;
4493 mCurrentAfTriggerId = triggerId;
4494 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004495 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
4496 continue; // Trigger ID tag is deprecated since device HAL 3.2
4497 }
4498 }
4499
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004500 camera_metadata_entry entry = metadata.find(tag);
4501
4502 if (entry.count > 0) {
4503 /**
4504 * Already has an entry for this trigger in the request.
4505 * Rewrite it with our requested trigger value.
4506 */
4507 RequestTrigger oldTrigger = trigger;
4508
4509 oldTrigger.entryValue = entry.data.u8[0];
4510
4511 mTriggerReplacedMap.add(tag, oldTrigger);
4512 } else {
4513 /**
4514 * More typical, no trigger entry, so we just add it
4515 */
4516 mTriggerRemovedMap.add(tag, trigger);
4517 }
4518
4519 status_t res;
4520
4521 switch (trigger.getTagType()) {
4522 case TYPE_BYTE: {
4523 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4524 res = metadata.update(tag,
4525 &entryValue,
4526 /*count*/1);
4527 break;
4528 }
4529 case TYPE_INT32:
4530 res = metadata.update(tag,
4531 &trigger.entryValue,
4532 /*count*/1);
4533 break;
4534 default:
4535 ALOGE("%s: Type not supported: 0x%x",
4536 __FUNCTION__,
4537 trigger.getTagType());
4538 return INVALID_OPERATION;
4539 }
4540
4541 if (res != OK) {
4542 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4543 ", value %d", __FUNCTION__, trigger.getTagName(),
4544 trigger.entryValue);
4545 return res;
4546 }
4547
4548 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4549 trigger.getTagName(),
4550 trigger.entryValue);
4551 }
4552
4553 mTriggerMap.clear();
4554
4555 return count;
4556}
4557
4558status_t Camera3Device::RequestThread::removeTriggers(
4559 const sp<CaptureRequest> &request) {
4560 Mutex::Autolock al(mTriggerMutex);
4561
4562 CameraMetadata &metadata = request->mSettings;
4563
4564 /**
4565 * Replace all old entries with their old values.
4566 */
4567 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4568 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4569
4570 status_t res;
4571
4572 uint32_t tag = trigger.metadataTag;
4573 switch (trigger.getTagType()) {
4574 case TYPE_BYTE: {
4575 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4576 res = metadata.update(tag,
4577 &entryValue,
4578 /*count*/1);
4579 break;
4580 }
4581 case TYPE_INT32:
4582 res = metadata.update(tag,
4583 &trigger.entryValue,
4584 /*count*/1);
4585 break;
4586 default:
4587 ALOGE("%s: Type not supported: 0x%x",
4588 __FUNCTION__,
4589 trigger.getTagType());
4590 return INVALID_OPERATION;
4591 }
4592
4593 if (res != OK) {
4594 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4595 ", trigger value %d", __FUNCTION__,
4596 trigger.getTagName(), trigger.entryValue);
4597 return res;
4598 }
4599 }
4600 mTriggerReplacedMap.clear();
4601
4602 /**
4603 * Remove all new entries.
4604 */
4605 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4606 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4607 status_t res = metadata.erase(trigger.metadataTag);
4608
4609 if (res != OK) {
4610 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4611 ", trigger value %d", __FUNCTION__,
4612 trigger.getTagName(), trigger.entryValue);
4613 return res;
4614 }
4615 }
4616 mTriggerRemovedMap.clear();
4617
4618 return OK;
4619}
4620
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004621status_t Camera3Device::RequestThread::addDummyTriggerIds(
4622 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004623 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004624 static const int32_t dummyTriggerId = 1;
4625 status_t res;
4626
4627 CameraMetadata &metadata = request->mSettings;
4628
4629 // If AF trigger is active, insert a dummy AF trigger ID if none already
4630 // exists
4631 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4632 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4633 if (afTrigger.count > 0 &&
4634 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4635 afId.count == 0) {
4636 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4637 if (res != OK) return res;
4638 }
4639
4640 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4641 // if none already exists
4642 camera_metadata_entry pcTrigger =
4643 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4644 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4645 if (pcTrigger.count > 0 &&
4646 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4647 pcId.count == 0) {
4648 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4649 &dummyTriggerId, 1);
4650 if (res != OK) return res;
4651 }
4652
4653 return OK;
4654}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004655
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004656/**
4657 * PreparerThread inner class methods
4658 */
4659
4660Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004661 Thread(/*canCallJava*/false), mListener(nullptr),
4662 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004663}
4664
4665Camera3Device::PreparerThread::~PreparerThread() {
4666 Thread::requestExitAndWait();
4667 if (mCurrentStream != nullptr) {
4668 mCurrentStream->cancelPrepare();
4669 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4670 mCurrentStream.clear();
4671 }
4672 clear();
4673}
4674
Ruben Brunkc78ac262015-08-13 17:58:46 -07004675status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004676 status_t res;
4677
4678 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004679 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004680
Ruben Brunkc78ac262015-08-13 17:58:46 -07004681 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004682 if (res == OK) {
4683 // No preparation needed, fire listener right off
4684 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004685 if (listener != NULL) {
4686 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004687 }
4688 return OK;
4689 } else if (res != NOT_ENOUGH_DATA) {
4690 return res;
4691 }
4692
4693 // Need to prepare, start up thread if necessary
4694 if (!mActive) {
4695 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4696 // isn't running
4697 Thread::requestExitAndWait();
4698 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4699 if (res != OK) {
4700 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004701 if (listener != NULL) {
4702 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004703 }
4704 return res;
4705 }
4706 mCancelNow = false;
4707 mActive = true;
4708 ALOGV("%s: Preparer stream started", __FUNCTION__);
4709 }
4710
4711 // queue up the work
4712 mPendingStreams.push_back(stream);
4713 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4714
4715 return OK;
4716}
4717
4718status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004719 Mutex::Autolock l(mLock);
4720
4721 for (const auto& stream : mPendingStreams) {
4722 stream->cancelPrepare();
4723 }
4724 mPendingStreams.clear();
4725 mCancelNow = true;
4726
4727 return OK;
4728}
4729
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004730void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004731 Mutex::Autolock l(mLock);
4732 mListener = listener;
4733}
4734
4735bool Camera3Device::PreparerThread::threadLoop() {
4736 status_t res;
4737 {
4738 Mutex::Autolock l(mLock);
4739 if (mCurrentStream == nullptr) {
4740 // End thread if done with work
4741 if (mPendingStreams.empty()) {
4742 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4743 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4744 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4745 mActive = false;
4746 return false;
4747 }
4748
4749 // Get next stream to prepare
4750 auto it = mPendingStreams.begin();
4751 mCurrentStream = *it;
4752 mPendingStreams.erase(it);
4753 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4754 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4755 } else if (mCancelNow) {
4756 mCurrentStream->cancelPrepare();
4757 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4758 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4759 mCurrentStream.clear();
4760 mCancelNow = false;
4761 return true;
4762 }
4763 }
4764
4765 res = mCurrentStream->prepareNextBuffer();
4766 if (res == NOT_ENOUGH_DATA) return true;
4767 if (res != OK) {
4768 // Something bad happened; try to recover by cancelling prepare and
4769 // signalling listener anyway
4770 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4771 mCurrentStream->getId(), res, strerror(-res));
4772 mCurrentStream->cancelPrepare();
4773 }
4774
4775 // This stream has finished, notify listener
4776 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004777 sp<NotificationListener> listener = mListener.promote();
4778 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004779 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4780 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004781 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004782 }
4783
4784 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4785 mCurrentStream.clear();
4786
4787 return true;
4788}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004789
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004790/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004791 * Static callback forwarding methods from HAL to instance
4792 */
4793
4794void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4795 const camera3_capture_result *result) {
4796 Camera3Device *d =
4797 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004798
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004799 d->processCaptureResult(result);
4800}
4801
4802void Camera3Device::sNotify(const camera3_callback_ops *cb,
4803 const camera3_notify_msg *msg) {
4804 Camera3Device *d =
4805 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4806 d->notify(msg);
4807}
4808
4809}; // namespace android