blob: c60032894d841669e10199be420cc0d899f8da0a [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070045#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080047#include <android/hardware/camera2/ICameraDeviceUser.h>
48
Igor Murashkinff3e31d2013-10-23 16:40:06 -070049#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070050#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051#include "device3/Camera3Device.h"
52#include "device3/Camera3OutputStream.h"
53#include "device3/Camera3InputStream.h"
54#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070055#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070056#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057
58using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080059using namespace android::hardware::camera;
60using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62namespace android {
63
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080064Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080065 mId(id),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070066 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070067 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070068 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070069 mUsePartialResult(false),
70 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080071 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070072 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070073 mNextReprocessResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070074 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070075 mNextReprocessShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070076 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080077{
78 ATRACE_CALL();
79 camera3_callback_ops::notify = &sNotify;
80 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080081 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080082}
83
84Camera3Device::~Camera3Device()
85{
86 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080087 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 disconnect();
89}
90
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080091const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -080092 return mId;
93}
94
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080095/**
96 * CameraDeviceBase interface
97 */
98
Yin-Chia Yehe074a932015-01-30 10:29:02 -080099status_t Camera3Device::initialize(CameraModule *module)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100{
101 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700102 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800103 Mutex::Autolock l(mLock);
104
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800105 ALOGV("%s: Initializing device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800106 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700107 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108 return INVALID_OPERATION;
109 }
110
111 /** Open HAL device */
112
113 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 camera3_device_t *device;
116
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800117 ATRACE_BEGIN("CameraHal::open");
118 res = module->open(mId.string(),
Chien-Yu Chend231fd62015-02-25 16:04:22 -0800119 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800120 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121
122 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 return res;
125 }
126
127 /** Cross-check device version */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800128 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700130 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800131 CAMERA_DEVICE_API_VERSION_3_2,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 device->common.version);
133 device->common.close(&device->common);
134 return BAD_VALUE;
135 }
136
137 camera_info info;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800138 res = module->getCameraInfo(atoi(mId), &info);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 if (res != OK) return res;
140
141 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
143 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700144 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Initialize device with callback functions */
150
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800151 ATRACE_BEGIN("CameraHal::initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_END();
154
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700156 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
157 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 device->common.close(&device->common);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800159 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800160 }
161
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800162 /** Everything is good to go */
163
164 mDeviceVersion = device->common.version;
165 mDeviceInfo = info.static_camera_characteristics;
166 mInterface = std::make_unique<HalInterface>(device);
167
168 return initializeCommonLocked();
169}
170
171status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
172 ATRACE_CALL();
173 Mutex::Autolock il(mInterfaceLock);
174 Mutex::Autolock l(mLock);
175
176 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
177 if (mStatus != STATUS_UNINITIALIZED) {
178 CLOGE("Already initialized!");
179 return INVALID_OPERATION;
180 }
181 if (manager == nullptr) return INVALID_OPERATION;
182
183 sp<ICameraDeviceSession> session;
184 ATRACE_BEGIN("CameraHal::openSession");
185 status_t res = manager->openSession(String8::std_string(mId), this,
186 /*out*/ &session);
187 ATRACE_END();
188 if (res != OK) {
189 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
190 return res;
191 }
192
193 res = manager->getCameraCharacteristics(String8::std_string(mId), &mDeviceInfo);
194 if (res != OK) {
195 SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
196 session->close();
197 return res;
198 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800199
200 // TODO: camera service will absorb 3_2/3_3/3_4 differences in the future
201 // for now use 3_4 to keep legacy devices working
202 mDeviceVersion = CAMERA_DEVICE_API_VERSION_3_4;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800203 mInterface = std::make_unique<HalInterface>(session);
204
205 return initializeCommonLocked();
206}
207
208status_t Camera3Device::initializeCommonLocked() {
209
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700210 /** Start up status tracker thread */
211 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800212 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 if (res != OK) {
214 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
215 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800216 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700217 mStatusTracker.clear();
218 return res;
219 }
220
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700221 /** Register in-flight map to the status tracker */
222 mInFlightStatusId = mStatusTracker->addComponent();
223
Zhijun He125684a2015-12-26 15:07:30 -0800224 /** Create buffer manager */
225 mBufferManager = new Camera3BufferManager();
226
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700227 bool aeLockAvailable = false;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800228 camera_metadata_entry aeLockAvailableEntry = mDeviceInfo.find(
229 ANDROID_CONTROL_AE_LOCK_AVAILABLE);
230 if (aeLockAvailableEntry.count > 0) {
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700231 aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
232 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
233 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800234
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700235 /** Start up request queue thread */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800236 mRequestThread = new RequestThread(this, mStatusTracker, mInterface.get(), mDeviceVersion,
237 aeLockAvailable);
238 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700240 SET_ERR_L("Unable to start request queue thread: %s (%d)",
241 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800242 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800243 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244 return res;
245 }
246
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700247 mPreparerThread = new PreparerThread();
248
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700249 // Determine whether we need to derive sensitivity boost values for older devices.
250 // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
251 // be listed (as the default value 100)
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800252 if (mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
Yin-Chia Yeh4c060992016-04-11 17:40:12 -0700253 mDerivePostRawSensKey = true;
254 }
255
Ruben Brunk183f0562015-08-12 12:55:02 -0700256 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700258 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700259 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800261
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800262 // Measure the clock domain offset between camera and video/hw_composer
263 camera_metadata_entry timestampSource =
264 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
265 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
266 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
267 mTimestampOffset = getMonoToBoottimeOffset();
268 }
269
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700270 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700271 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
272 camera_metadata_entry partialResultsCount =
273 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
274 if (partialResultsCount.count > 0) {
275 mNumPartialResults = partialResultsCount.data.i32[0];
276 mUsePartialResult = (mNumPartialResults > 1);
277 }
278 } else {
279 camera_metadata_entry partialResultsQuirk =
280 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
281 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
282 mUsePartialResult = true;
283 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700284 }
285
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700286 camera_metadata_entry configs =
287 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
288 for (uint32_t i = 0; i < configs.count; i += 4) {
289 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
290 configs.data.i32[i + 3] ==
291 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
292 mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
293 configs.data.i32[i + 2]));
294 }
295 }
296
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800297 return OK;
298}
299
300status_t Camera3Device::disconnect() {
301 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700302 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700304 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700306 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 {
309 Mutex::Autolock l(mLock);
310 if (mStatus == STATUS_UNINITIALIZED) return res;
311
312 if (mStatus == STATUS_ACTIVE ||
313 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
314 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700315 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700316 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700317 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700318 } else {
319 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
320 if (res != OK) {
321 SET_ERR_L("Timeout waiting for HAL to drain");
322 // Continue to close device even in case of error
323 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700324 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700327 if (mStatus == STATUS_ERROR) {
328 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700329 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700330
331 if (mStatusTracker != NULL) {
332 mStatusTracker->requestExit();
333 }
334
335 if (mRequestThread != NULL) {
336 mRequestThread->requestExit();
337 }
338
339 mOutputStreams.clear();
340 mInputStream.clear();
341 }
342
343 // Joining done without holding mLock, otherwise deadlocks may ensue
344 // as the threads try to access parent state
345 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
346 // HAL may be in a bad state, so waiting for request thread
347 // (which may be stuck in the HAL processCaptureRequest call)
348 // could be dangerous.
349 mRequestThread->join();
350 }
351
352 if (mStatusTracker != NULL) {
353 mStatusTracker->join();
354 }
355
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800356 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700357 {
358 Mutex::Autolock l(mLock);
359
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800360 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700361 mStatusTracker.clear();
Zhijun He125684a2015-12-26 15:07:30 -0800362 mBufferManager.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800363
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800364 interface = mInterface.get();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700365 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800366
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700367 // Call close without internal mutex held, as the HAL close may need to
368 // wait on assorted callbacks,etc, to complete before it can return.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800369 interface->close();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700370
371 {
372 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800373 mInterface->clear();
Ruben Brunk183f0562015-08-12 12:55:02 -0700374 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700375 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800376
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700377 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700378 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800379}
380
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700381// For dumping/debugging only -
382// try to acquire a lock a few times, eventually give up to proceed with
383// debug/dump operations
384bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
385 bool gotLock = false;
386 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
387 if (lock.tryLock() == NO_ERROR) {
388 gotLock = true;
389 break;
390 } else {
391 usleep(kDumpSleepDuration);
392 }
393 }
394 return gotLock;
395}
396
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700397Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
398 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
399 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
400 const int STREAM_CONFIGURATION_SIZE = 4;
401 const int STREAM_FORMAT_OFFSET = 0;
402 const int STREAM_WIDTH_OFFSET = 1;
403 const int STREAM_HEIGHT_OFFSET = 2;
404 const int STREAM_IS_INPUT_OFFSET = 3;
405 camera_metadata_ro_entry_t availableStreamConfigs =
406 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
407 if (availableStreamConfigs.count == 0 ||
408 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
409 return Size(0, 0);
410 }
411
412 // Get max jpeg size (area-wise).
413 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
414 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
415 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
416 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
417 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
418 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
419 && format == HAL_PIXEL_FORMAT_BLOB &&
420 (width * height > maxJpegWidth * maxJpegHeight)) {
421 maxJpegWidth = width;
422 maxJpegHeight = height;
423 }
424 }
425 } else {
426 camera_metadata_ro_entry availableJpegSizes =
427 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
428 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
429 return Size(0, 0);
430 }
431
432 // Get max jpeg size (area-wise).
433 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
434 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
435 > (maxJpegWidth * maxJpegHeight)) {
436 maxJpegWidth = availableJpegSizes.data.i32[i];
437 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
438 }
439 }
440 }
441 return Size(maxJpegWidth, maxJpegHeight);
442}
443
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800444nsecs_t Camera3Device::getMonoToBoottimeOffset() {
445 // try three times to get the clock offset, choose the one
446 // with the minimum gap in measurements.
447 const int tries = 3;
448 nsecs_t bestGap, measured;
449 for (int i = 0; i < tries; ++i) {
450 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
451 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
452 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
453 const nsecs_t gap = tmono2 - tmono;
454 if (i == 0 || gap < bestGap) {
455 bestGap = gap;
456 measured = tbase - ((tmono + tmono2) >> 1);
457 }
458 }
459 return measured;
460}
461
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -0700462/**
463 * Map Android N dataspace definitions back to Android M definitions, for
464 * use with HALv3.3 or older.
465 *
466 * Only map where correspondences exist, and otherwise preserve the value.
467 */
468android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
469 switch (dataSpace) {
470 case HAL_DATASPACE_V0_SRGB_LINEAR:
471 return HAL_DATASPACE_SRGB_LINEAR;
472 case HAL_DATASPACE_V0_SRGB:
473 return HAL_DATASPACE_SRGB;
474 case HAL_DATASPACE_V0_JFIF:
475 return HAL_DATASPACE_JFIF;
476 case HAL_DATASPACE_V0_BT601_625:
477 return HAL_DATASPACE_BT601_625;
478 case HAL_DATASPACE_V0_BT601_525:
479 return HAL_DATASPACE_BT601_525;
480 case HAL_DATASPACE_V0_BT709:
481 return HAL_DATASPACE_BT709;
482 default:
483 return dataSpace;
484 }
485}
486
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800487hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
488 int frameworkFormat) {
489 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
490}
491
492DataspaceFlags Camera3Device::mapToHidlDataspace(
493 android_dataspace dataSpace) {
494 return dataSpace;
495}
496
497ConsumerUsageFlags Camera3Device::mapToConsumerUsage(
498 uint32_t usage) {
499 return usage;
500}
501
502StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
503 switch (rotation) {
504 case CAMERA3_STREAM_ROTATION_0:
505 return StreamRotation::ROTATION_0;
506 case CAMERA3_STREAM_ROTATION_90:
507 return StreamRotation::ROTATION_90;
508 case CAMERA3_STREAM_ROTATION_180:
509 return StreamRotation::ROTATION_180;
510 case CAMERA3_STREAM_ROTATION_270:
511 return StreamRotation::ROTATION_270;
512 }
513 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
514 return StreamRotation::ROTATION_0;
515}
516
517StreamConfigurationMode Camera3Device::mapToStreamConfigurationMode(
518 camera3_stream_configuration_mode_t operationMode) {
519 switch(operationMode) {
520 case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
521 return StreamConfigurationMode::NORMAL_MODE;
522 case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
523 return StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
524 case CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START:
525 // Needs to be mapped by vendor extensions
526 break;
527 }
528 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
529 return StreamConfigurationMode::NORMAL_MODE;
530}
531
532camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
533 switch (status) {
534 case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
535 case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
536 }
537 return CAMERA3_BUFFER_STATUS_ERROR;
538}
539
540int Camera3Device::mapToFrameworkFormat(
541 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
542 return static_cast<uint32_t>(pixelFormat);
543}
544
545uint32_t Camera3Device::mapConsumerToFrameworkUsage(
546 ConsumerUsageFlags usage) {
547 return usage;
548}
549
550uint32_t Camera3Device::mapProducerToFrameworkUsage(
551 ProducerUsageFlags usage) {
552 return usage;
553}
554
Zhijun Hef7da0962014-04-24 13:27:56 -0700555ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700556 // Get max jpeg size (area-wise).
557 Size maxJpegResolution = getMaxJpegResolution();
558 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800559 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
560 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700561 return BAD_VALUE;
562 }
563
Zhijun Hef7da0962014-04-24 13:27:56 -0700564 // Get max jpeg buffer size
565 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700566 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
567 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800568 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
569 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700570 return BAD_VALUE;
571 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700572 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800573 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700574
575 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700576 float scaleFactor = ((float) (width * height)) /
577 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800578 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
579 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700580 if (jpegBufferSize > maxJpegBufferSize) {
581 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700582 }
583
584 return jpegBufferSize;
585}
586
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700587ssize_t Camera3Device::getPointCloudBufferSize() const {
588 const int FLOATS_PER_POINT=4;
589 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
590 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800591 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
592 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700593 return BAD_VALUE;
594 }
595 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
596 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
597 return maxBytesForPointCloud;
598}
599
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800600ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800601 const int PER_CONFIGURATION_SIZE = 3;
602 const int WIDTH_OFFSET = 0;
603 const int HEIGHT_OFFSET = 1;
604 const int SIZE_OFFSET = 2;
605 camera_metadata_ro_entry rawOpaqueSizes =
606 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800607 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800608 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
610 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800611 return BAD_VALUE;
612 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700613
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800614 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
615 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
616 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
617 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
618 }
619 }
620
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800621 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
622 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800623 return BAD_VALUE;
624}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700625
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800626status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
627 ATRACE_CALL();
628 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629
630 // Try to lock, but continue in case of failure (to avoid blocking in
631 // deadlocks)
632 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
633 bool gotLock = tryLockSpinRightRound(mLock);
634
635 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800636 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
637 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800639 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
640 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700641
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800642 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700643
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800644 String16 templatesOption("-t");
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700645 String16 monitorOption("-m");
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800646 int n = args.size();
647 for (int i = 0; i < n; i++) {
648 if (args[i] == templatesOption) {
649 dumpTemplates = true;
650 }
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700651 if (args[i] == monitorOption) {
652 if (i + 1 < n) {
653 String8 monitorTags = String8(args[i + 1]);
654 if (monitorTags == "off") {
655 mTagMonitor.disableMonitoring();
656 } else {
657 mTagMonitor.parseTagsToMonitor(monitorTags);
658 }
659 } else {
660 mTagMonitor.disableMonitoring();
661 }
662 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800663 }
664
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800665 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800666
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 const char *status =
668 mStatus == STATUS_ERROR ? "ERROR" :
669 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
671 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 mStatus == STATUS_ACTIVE ? "ACTIVE" :
673 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700674
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800675 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700676 if (mStatus == STATUS_ERROR) {
677 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
678 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 lines.appendFormat(" Stream configuration:\n");
Zhijun He1fa89992015-06-01 15:44:31 -0700680 lines.appendFormat(" Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
Eino-Ville Talvala9a179412015-06-09 13:15:16 -0700681 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800682
683 if (mInputStream != NULL) {
684 write(fd, lines.string(), lines.size());
685 mInputStream->dump(fd, args);
686 } else {
687 lines.appendFormat(" No input stream.\n");
688 write(fd, lines.string(), lines.size());
689 }
690 for (size_t i = 0; i < mOutputStreams.size(); i++) {
691 mOutputStreams[i]->dump(fd,args);
692 }
693
Zhijun He431503c2016-03-07 17:30:16 -0800694 if (mBufferManager != NULL) {
695 lines = String8(" Camera3 Buffer Manager:\n");
696 write(fd, lines.string(), lines.size());
697 mBufferManager->dump(fd, args);
698 }
Zhijun He125684a2015-12-26 15:07:30 -0800699
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700700 lines = String8(" In-flight requests:\n");
701 if (mInFlightMap.size() == 0) {
702 lines.append(" None\n");
703 } else {
704 for (size_t i = 0; i < mInFlightMap.size(); i++) {
705 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700706 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700707 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800708 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700709 r.numBuffersLeft);
710 }
711 }
712 write(fd, lines.string(), lines.size());
713
Igor Murashkin1e479c02013-09-06 16:55:14 -0700714 {
715 lines = String8(" Last request sent:\n");
716 write(fd, lines.string(), lines.size());
717
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700719 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
720 }
721
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800722 if (dumpTemplates) {
723 const char *templateNames[] = {
724 "TEMPLATE_PREVIEW",
725 "TEMPLATE_STILL_CAPTURE",
726 "TEMPLATE_VIDEO_RECORD",
727 "TEMPLATE_VIDEO_SNAPSHOT",
728 "TEMPLATE_ZERO_SHUTTER_LAG",
729 "TEMPLATE_MANUAL"
730 };
731
732 for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800733 camera_metadata_t *templateRequest = nullptr;
734 mInterface->constructDefaultRequestSettings(
735 (camera3_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800736 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800737 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800738 lines.append(" Not supported\n");
739 write(fd, lines.string(), lines.size());
740 } else {
741 write(fd, lines.string(), lines.size());
742 dump_indented_camera_metadata(templateRequest,
743 fd, /*verbosity*/2, /*indentation*/8);
744 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800745 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800746 }
747 }
748
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700749 mTagMonitor.dumpMonitoredMetadata(fd);
750
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800751 if (mInterface->valid()) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700752 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800754 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800756
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700757 if (gotLock) mLock.unlock();
758 if (gotInterfaceLock) mInterfaceLock.unlock();
759
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760 return OK;
761}
762
763const CameraMetadata& Camera3Device::info() const {
764 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800765 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
766 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700767 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800768 mStatus == STATUS_ERROR ?
769 "when in error state" : "before init");
770 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800771 return mDeviceInfo;
772}
773
Jianing Wei90e59c92014-03-12 18:29:36 -0700774status_t Camera3Device::checkStatusOkToCaptureLocked() {
775 switch (mStatus) {
776 case STATUS_ERROR:
777 CLOGE("Device has encountered a serious error");
778 return INVALID_OPERATION;
779 case STATUS_UNINITIALIZED:
780 CLOGE("Device not initialized");
781 return INVALID_OPERATION;
782 case STATUS_UNCONFIGURED:
783 case STATUS_CONFIGURED:
784 case STATUS_ACTIVE:
785 // OK
786 break;
787 default:
788 SET_ERR_L("Unexpected status: %d", mStatus);
789 return INVALID_OPERATION;
790 }
791 return OK;
792}
793
794status_t Camera3Device::convertMetadataListToRequestListLocked(
Shuzhen Wang9d066012016-09-30 11:30:20 -0700795 const List<const CameraMetadata> &metadataList, bool repeating,
796 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;
Jianing Wei90e59c92014-03-12 18:29:36 -0700803 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
804 it != metadataList.end(); ++it) {
805 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
806 if (newRequest == 0) {
807 CLOGE("Can't create capture request");
808 return BAD_VALUE;
809 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700810
Shuzhen Wang9d066012016-09-30 11:30:20 -0700811 newRequest->mRepeating = repeating;
812
Jianing Weicb0652e2014-03-12 18:29:36 -0700813 // Setup burst Id and request Id
814 newRequest->mResultExtras.burstId = burstId++;
815 if (it->exists(ANDROID_REQUEST_ID)) {
816 if (it->find(ANDROID_REQUEST_ID).count == 0) {
817 CLOGE("RequestID entry exists; but must not be empty in metadata");
818 return BAD_VALUE;
819 }
820 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
821 } else {
822 CLOGE("RequestID does not exist in metadata");
823 return BAD_VALUE;
824 }
825
Jianing Wei90e59c92014-03-12 18:29:36 -0700826 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700827
828 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700829 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700830
831 // Setup batch size if this is a high speed video recording request.
832 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
833 auto firstRequest = requestList->begin();
834 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
835 if (outputStream->isVideoStream()) {
836 (*firstRequest)->mBatchSize = requestList->size();
837 break;
838 }
839 }
840 }
841
Jianing Wei90e59c92014-03-12 18:29:36 -0700842 return OK;
843}
844
Jianing Weicb0652e2014-03-12 18:29:36 -0700845status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800847
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700848 List<const CameraMetadata> requests;
849 requests.push_back(request);
850 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851}
852
Jianing Wei90e59c92014-03-12 18:29:36 -0700853status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700854 const List<const CameraMetadata> &requests, bool repeating,
855 /*out*/
856 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700857 ATRACE_CALL();
858 Mutex::Autolock il(mInterfaceLock);
859 Mutex::Autolock l(mLock);
860
861 status_t res = checkStatusOkToCaptureLocked();
862 if (res != OK) {
863 // error logged by previous call
864 return res;
865 }
866
867 RequestList requestList;
868
Shuzhen Wang9d066012016-09-30 11:30:20 -0700869 res = convertMetadataListToRequestListLocked(requests, repeating,
870 /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700871 if (res != OK) {
872 // error logged by previous call
873 return res;
874 }
875
876 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700877 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700878 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700879 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700880 }
881
882 if (res == OK) {
883 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
884 if (res != OK) {
885 SET_ERR_L("Can't transition to active in %f seconds!",
886 kActiveTimeout/1e9);
887 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800888 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700889 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700890 } else {
891 CLOGE("Cannot queue request. Impossible.");
892 return BAD_VALUE;
893 }
894
895 return res;
896}
897
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800898hardware::Return<void> Camera3Device::processCaptureResult(
899 const device::V3_2::CaptureResult& result) {
900 camera3_capture_result r;
901 status_t res;
902 r.frame_number = result.frameNumber;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800903 if (result.result.size() != 0) {
904 r.result = reinterpret_cast<const camera_metadata_t*>(result.result.data());
905 size_t expected_metadata_size = result.result.size();
906 if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
907 ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
908 __FUNCTION__, result.frameNumber, strerror(-res), res);
909 return hardware::Void();
910 }
911 } else {
912 r.result = nullptr;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800913 }
914
915 std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
916 std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
917 for (size_t i = 0; i < result.outputBuffers.size(); i++) {
918 auto& bDst = outputBuffers[i];
919 const StreamBuffer &bSrc = result.outputBuffers[i];
920
921 ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
922 if (idx == -1) {
923 ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
924 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
925 return hardware::Void();
926 }
927 bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
928
929 buffer_handle_t *buffer;
Yin-Chia Yehf4650602017-01-10 13:13:39 -0800930 res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800931 if (res != OK) {
932 ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
933 __FUNCTION__, result.frameNumber, i, bSrc.streamId);
934 return hardware::Void();
935 }
936 bDst.buffer = buffer;
937 bDst.status = mapHidlBufferStatus(bSrc.status);
938 bDst.acquire_fence = -1;
939 if (bSrc.releaseFence == nullptr) {
940 bDst.release_fence = -1;
941 } else if (bSrc.releaseFence->numFds == 1) {
942 bDst.release_fence = dup(bSrc.releaseFence->data[0]);
943 } else {
944 ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
945 __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
946 return hardware::Void();
947 }
948 }
949 r.num_output_buffers = outputBuffers.size();
950 r.output_buffers = outputBuffers.data();
951
952 camera3_stream_buffer_t inputBuffer;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800953 if (result.inputBuffer.streamId == -1) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800954 r.input_buffer = nullptr;
955 } else {
956 if (mInputStream->getId() != result.inputBuffer.streamId) {
957 ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
958 result.frameNumber, result.inputBuffer.streamId);
959 return hardware::Void();
960 }
961 inputBuffer.stream = mInputStream->asHalStream();
962 buffer_handle_t *buffer;
963 res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
964 &buffer);
965 if (res != OK) {
966 ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
967 __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
968 return hardware::Void();
969 }
970 inputBuffer.buffer = buffer;
971 inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
972 inputBuffer.acquire_fence = -1;
973 if (result.inputBuffer.releaseFence == nullptr) {
974 inputBuffer.release_fence = -1;
975 } else if (result.inputBuffer.releaseFence->numFds == 1) {
976 inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
977 } else {
978 ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
979 __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
980 return hardware::Void();
981 }
982 r.input_buffer = &inputBuffer;
983 }
984
985 r.partial_result = result.partialResult;
986
987 processCaptureResult(&r);
988
989 return hardware::Void();
990}
991
992hardware::Return<void> Camera3Device::notify(
993 const NotifyMsg& msg) {
994 camera3_notify_msg m;
995 switch (msg.type) {
996 case MsgType::ERROR:
997 m.type = CAMERA3_MSG_ERROR;
998 m.message.error.frame_number = msg.msg.error.frameNumber;
999 if (msg.msg.error.errorStreamId >= 0) {
1000 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
1001 if (idx == -1) {
1002 ALOGE("%s: Frame %d: Invalid error stream id %d",
1003 __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
1004 return hardware::Void();
1005 }
1006 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
1007 } else {
1008 m.message.error.error_stream = nullptr;
1009 }
1010 switch (msg.msg.error.errorCode) {
1011 case ErrorCode::ERROR_DEVICE:
1012 m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
1013 break;
1014 case ErrorCode::ERROR_REQUEST:
1015 m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
1016 break;
1017 case ErrorCode::ERROR_RESULT:
1018 m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
1019 break;
1020 case ErrorCode::ERROR_BUFFER:
1021 m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
1022 break;
1023 }
1024 break;
1025 case MsgType::SHUTTER:
1026 m.type = CAMERA3_MSG_SHUTTER;
1027 m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
1028 m.message.shutter.timestamp = msg.msg.shutter.timestamp;
1029 break;
1030 }
1031 notify(&m);
1032
1033 return hardware::Void();
1034}
1035
Jianing Weicb0652e2014-03-12 18:29:36 -07001036status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
1037 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001038 ATRACE_CALL();
1039
Jianing Weicb0652e2014-03-12 18:29:36 -07001040 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001041}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001042
Jianing Weicb0652e2014-03-12 18:29:36 -07001043status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1044 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001045 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001046
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001047 List<const CameraMetadata> requests;
1048 requests.push_back(request);
1049 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050}
1051
Jianing Weicb0652e2014-03-12 18:29:36 -07001052status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
1053 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001054 ATRACE_CALL();
1055
Jianing Weicb0652e2014-03-12 18:29:36 -07001056 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001057}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058
1059sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
1060 const CameraMetadata &request) {
1061 status_t res;
1062
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001064 res = configureStreamsLocked();
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001065 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001067 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001068 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001069 } else if (mStatus == STATUS_UNCONFIGURED) {
1070 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001071 CLOGE("No streams configured");
1072 return NULL;
1073 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001074 }
1075
1076 sp<CaptureRequest> newRequest = createCaptureRequest(request);
1077 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001078}
1079
Jianing Weicb0652e2014-03-12 18:29:36 -07001080status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001081 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001082 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001084
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 switch (mStatus) {
1086 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001087 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 return INVALID_OPERATION;
1089 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001090 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001092 case STATUS_UNCONFIGURED:
1093 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 case STATUS_ACTIVE:
1095 // OK
1096 break;
1097 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001098 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099 return INVALID_OPERATION;
1100 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001101 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001102
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001103 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001104}
1105
1106status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1107 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001108 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001109
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001110 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001111}
1112
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001113status_t Camera3Device::createInputStream(
1114 uint32_t width, uint32_t height, int format, int *id) {
1115 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001116 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001117 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001118 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1119 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001120
1121 status_t res;
1122 bool wasActive = false;
1123
1124 switch (mStatus) {
1125 case STATUS_ERROR:
1126 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1127 return INVALID_OPERATION;
1128 case STATUS_UNINITIALIZED:
1129 ALOGE("%s: Device not initialized", __FUNCTION__);
1130 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001131 case STATUS_UNCONFIGURED:
1132 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001133 // OK
1134 break;
1135 case STATUS_ACTIVE:
1136 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001137 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001138 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001140 return res;
1141 }
1142 wasActive = true;
1143 break;
1144 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001145 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001146 return INVALID_OPERATION;
1147 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001148 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001149
1150 if (mInputStream != 0) {
1151 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1152 return INVALID_OPERATION;
1153 }
1154
1155 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1156 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001157 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001158
1159 mInputStream = newStream;
1160
1161 *id = mNextStreamId++;
1162
1163 // Continue captures if active at start
1164 if (wasActive) {
1165 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1166 res = configureStreamsLocked();
1167 if (res != OK) {
1168 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1169 __FUNCTION__, mNextStreamId, strerror(-res), res);
1170 return res;
1171 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001172 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001173 }
1174
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001175 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001176 return OK;
1177}
1178
Igor Murashkin2fba5842013-04-22 14:03:54 -07001179
1180status_t Camera3Device::createZslStream(
1181 uint32_t width, uint32_t height,
1182 int depth,
1183 /*out*/
1184 int *id,
1185 sp<Camera3ZslStream>* zslStream) {
1186 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001187 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001188 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001189 ALOGV("Camera %s: Creating ZSL stream %d: %d x %d, depth %d",
1190 mId.string(), mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001191
1192 status_t res;
1193 bool wasActive = false;
1194
1195 switch (mStatus) {
1196 case STATUS_ERROR:
1197 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1198 return INVALID_OPERATION;
1199 case STATUS_UNINITIALIZED:
1200 ALOGE("%s: Device not initialized", __FUNCTION__);
1201 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001202 case STATUS_UNCONFIGURED:
1203 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -07001204 // OK
1205 break;
1206 case STATUS_ACTIVE:
1207 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001209 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001210 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -07001211 return res;
1212 }
1213 wasActive = true;
1214 break;
1215 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001216 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001217 return INVALID_OPERATION;
1218 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001219 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001220
1221 if (mInputStream != 0) {
1222 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1223 return INVALID_OPERATION;
1224 }
1225
1226 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
1227 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001228 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -07001229
1230 res = mOutputStreams.add(mNextStreamId, newStream);
1231 if (res < 0) {
1232 ALOGE("%s: Can't add new stream to set: %s (%d)",
1233 __FUNCTION__, strerror(-res), res);
1234 return res;
1235 }
1236 mInputStream = newStream;
1237
Yuvraj Pasie5e3d082014-04-15 18:37:45 +05301238 mNeedConfig = true;
1239
Igor Murashkin2fba5842013-04-22 14:03:54 -07001240 *id = mNextStreamId++;
1241 *zslStream = newStream;
1242
1243 // Continue captures if active at start
1244 if (wasActive) {
1245 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1246 res = configureStreamsLocked();
1247 if (res != OK) {
1248 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1249 __FUNCTION__, mNextStreamId, strerror(-res), res);
1250 return res;
1251 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -07001253 }
1254
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001255 ALOGV("Camera %s: Created ZSL stream", mId.string());
Igor Murashkin2fba5842013-04-22 14:03:54 -07001256 return OK;
1257}
1258
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001259status_t Camera3Device::createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -08001260 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
Zhijun He5d677d12016-05-29 16:52:39 -07001261 camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001262 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001263 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001265 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1266 " consumer usage 0x%x", mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Zhijun He5d677d12016-05-29 16:52:39 -07001267 consumerUsage);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269 status_t res;
1270 bool wasActive = false;
1271
1272 switch (mStatus) {
1273 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 return INVALID_OPERATION;
1276 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 case STATUS_UNCONFIGURED:
1280 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 // OK
1282 break;
1283 case STATUS_ACTIVE:
1284 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001285 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001288 return res;
1289 }
1290 wasActive = true;
1291 break;
1292 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001293 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001294 return INVALID_OPERATION;
1295 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001296 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297
1298 sp<Camera3OutputStream> newStream;
Zhijun Heedd41ae2016-02-03 14:45:53 -08001299 // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
Zhijun He125684a2015-12-26 15:07:30 -08001300 // such devices.
Zhijun Heedd41ae2016-02-03 14:45:53 -08001301 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001302 streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
1303 }
Zhijun He5d677d12016-05-29 16:52:39 -07001304
1305 // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
1306 // which requires a consumer surface to be available.
1307 if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1308 ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
1309 return BAD_VALUE;
1310 }
1311
1312 if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1313 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1314 return BAD_VALUE;
1315 }
1316
Eino-Ville Talvala2cbf6ce2016-03-14 13:03:25 -07001317 // Use legacy dataspace values for older HALs
1318 if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
1319 dataSpace = mapToLegacyDataspace(dataSpace);
1320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001321 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001322 ssize_t blobBufferSize;
1323 if (dataSpace != HAL_DATASPACE_DEPTH) {
1324 blobBufferSize = getJpegBufferSize(width, height);
1325 if (blobBufferSize <= 0) {
1326 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1327 return BAD_VALUE;
1328 }
1329 } else {
1330 blobBufferSize = getPointCloudBufferSize();
1331 if (blobBufferSize <= 0) {
1332 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1333 return BAD_VALUE;
1334 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001335 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001336 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001337 width, height, blobBufferSize, format, dataSpace, rotation,
1338 mTimestampOffset, streamSetId);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001339 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1340 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1341 if (rawOpaqueBufferSize <= 0) {
1342 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1343 return BAD_VALUE;
1344 }
1345 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001346 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1347 mTimestampOffset, streamSetId);
Zhijun He5d677d12016-05-29 16:52:39 -07001348 } else if (consumer == nullptr) {
1349 newStream = new Camera3OutputStream(mNextStreamId,
1350 width, height, format, consumerUsage, dataSpace, rotation,
1351 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001352 } else {
1353 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001354 width, height, format, dataSpace, rotation,
1355 mTimestampOffset, streamSetId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001357 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001358
Zhijun He125684a2015-12-26 15:07:30 -08001359 /**
Zhijun Heedd41ae2016-02-03 14:45:53 -08001360 * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
1361 * requires buffers to be statically allocated for internal static buffer registration, while
1362 * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
1363 * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
Zhijun He125684a2015-12-26 15:07:30 -08001364 */
Zhijun Heedd41ae2016-02-03 14:45:53 -08001365 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Zhijun He125684a2015-12-26 15:07:30 -08001366 newStream->setBufferManager(mBufferManager);
1367 }
1368
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001369 res = mOutputStreams.add(mNextStreamId, newStream);
1370 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001371 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001372 return res;
1373 }
1374
1375 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001376 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377
1378 // Continue captures if active at start
1379 if (wasActive) {
1380 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1381 res = configureStreamsLocked();
1382 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001383 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1384 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385 return res;
1386 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001387 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001388 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001389 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001391}
1392
1393status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
1394 ATRACE_CALL();
1395 (void)outputId; (void)id;
1396
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001397 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001398 return INVALID_OPERATION;
1399}
1400
1401
1402status_t Camera3Device::getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001403 uint32_t *width, uint32_t *height,
1404 uint32_t *format, android_dataspace *dataSpace) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001405 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001406 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001407 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 switch (mStatus) {
1410 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001411 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 return INVALID_OPERATION;
1413 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001414 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001416 case STATUS_UNCONFIGURED:
1417 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001418 case STATUS_ACTIVE:
1419 // OK
1420 break;
1421 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001422 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 return INVALID_OPERATION;
1424 }
1425
1426 ssize_t idx = mOutputStreams.indexOfKey(id);
1427 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001428 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 return idx;
1430 }
1431
1432 if (width) *width = mOutputStreams[idx]->getWidth();
1433 if (height) *height = mOutputStreams[idx]->getHeight();
1434 if (format) *format = mOutputStreams[idx]->getFormat();
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -07001435 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437}
1438
1439status_t Camera3Device::setStreamTransform(int id,
1440 int transform) {
1441 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001442 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001443 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 switch (mStatus) {
1446 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001447 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001448 return INVALID_OPERATION;
1449 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001450 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001451 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001452 case STATUS_UNCONFIGURED:
1453 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001454 case STATUS_ACTIVE:
1455 // OK
1456 break;
1457 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001458 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459 return INVALID_OPERATION;
1460 }
1461
1462 ssize_t idx = mOutputStreams.indexOfKey(id);
1463 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001464 CLOGE("Stream %d does not exist",
1465 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return BAD_VALUE;
1467 }
1468
1469 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001470}
1471
1472status_t Camera3Device::deleteStream(int id) {
1473 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001474 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001475 Mutex::Autolock l(mLock);
1476 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001477
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001478 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001479
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001480 // CameraDevice semantics require device to already be idle before
1481 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001482 if (mStatus == STATUS_ACTIVE) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001483 ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001484 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001485 }
1486
Igor Murashkin2fba5842013-04-22 14:03:54 -07001487 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -08001488 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001489 if (mInputStream != NULL && id == mInputStream->getId()) {
1490 deletedStream = mInputStream;
1491 mInputStream.clear();
1492 } else {
Zhijun He5f446352014-01-22 09:49:33 -08001493 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001494 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001495 return BAD_VALUE;
1496 }
Zhijun He5f446352014-01-22 09:49:33 -08001497 }
1498
1499 // Delete output stream or the output part of a bi-directional stream.
1500 if (outputStreamIdx != NAME_NOT_FOUND) {
1501 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001502 mOutputStreams.removeItem(id);
1503 }
1504
1505 // Free up the stream endpoint so that it can be used by some other stream
1506 res = deletedStream->disconnect();
1507 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001508 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509 // fall through since we want to still list the stream as deleted.
1510 }
1511 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001512 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513
1514 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001515}
1516
1517status_t Camera3Device::deleteReprocessStream(int id) {
1518 ATRACE_CALL();
1519 (void)id;
1520
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001521 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001522 return INVALID_OPERATION;
1523}
1524
Zhijun He1fa89992015-06-01 15:44:31 -07001525status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001526 ATRACE_CALL();
1527 ALOGV("%s: E", __FUNCTION__);
1528
1529 Mutex::Autolock il(mInterfaceLock);
1530 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001531
1532 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
1533 mNeedConfig = true;
1534 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
1535 }
Igor Murashkine2d167e2014-08-19 16:19:59 -07001536
1537 return configureStreamsLocked();
1538}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001539
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001540status_t Camera3Device::getInputBufferProducer(
1541 sp<IGraphicBufferProducer> *producer) {
1542 Mutex::Autolock il(mInterfaceLock);
1543 Mutex::Autolock l(mLock);
1544
1545 if (producer == NULL) {
1546 return BAD_VALUE;
1547 } else if (mInputStream == NULL) {
1548 return INVALID_OPERATION;
1549 }
1550
1551 return mInputStream->getInputBufferProducer(producer);
1552}
1553
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001554status_t Camera3Device::createDefaultRequest(int templateId,
1555 CameraMetadata *request) {
1556 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001557 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001558
1559 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1560 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1561 IPCThreadState::self()->getCallingUid(), nullptr, 0);
1562 return BAD_VALUE;
1563 }
1564
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566 Mutex::Autolock l(mLock);
1567
1568 switch (mStatus) {
1569 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001570 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 return INVALID_OPERATION;
1572 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001573 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001574 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001575 case STATUS_UNCONFIGURED:
1576 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577 case STATUS_ACTIVE:
1578 // OK
1579 break;
1580 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001581 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582 return INVALID_OPERATION;
1583 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001584
Zhijun Hea1530f12014-09-14 12:44:20 -07001585 if (!mRequestTemplateCache[templateId].isEmpty()) {
1586 *request = mRequestTemplateCache[templateId];
1587 return OK;
1588 }
1589
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001590 camera_metadata_t *rawRequest;
1591 status_t res = mInterface->constructDefaultRequestSettings(
1592 (camera3_request_template_t) templateId, &rawRequest);
1593 if (res == BAD_VALUE) {
Yin-Chia Yeh0336d362015-04-14 12:34:22 -07001594 ALOGI("%s: template %d is not supported on this camera device",
1595 __FUNCTION__, templateId);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001596 return res;
1597 } else if (res != OK) {
1598 CLOGE("Unable to construct request template %d: %s (%d)",
1599 templateId, strerror(-res), res);
1600 return res;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001601 }
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001602
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001603 mRequestTemplateCache[templateId].acquire(rawRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001604
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07001605 // Derive some new keys for backward compatibility
1606 if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
1607 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
1608 int32_t defaultBoost[1] = {100};
1609 mRequestTemplateCache[templateId].update(
1610 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1611 defaultBoost, 1);
1612 }
1613
1614 *request = mRequestTemplateCache[templateId];
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001615 return OK;
1616}
1617
1618status_t Camera3Device::waitUntilDrained() {
1619 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001620 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001621 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001622
Zhijun He69a37482014-03-23 18:44:49 -07001623 return waitUntilDrainedLocked();
1624}
1625
1626status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001627 switch (mStatus) {
1628 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001629 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 ALOGV("%s: Already idle", __FUNCTION__);
1631 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001632 case STATUS_CONFIGURED:
1633 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 case STATUS_ERROR:
1635 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001636 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001637 break;
1638 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001639 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001640 return INVALID_OPERATION;
1641 }
1642
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001643 ALOGV("%s: Camera %s: Waiting until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001644 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001645 if (res != OK) {
1646 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1647 res);
1648 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001649 return res;
1650}
1651
Ruben Brunk183f0562015-08-12 12:55:02 -07001652
1653void Camera3Device::internalUpdateStatusLocked(Status status) {
1654 mStatus = status;
1655 mRecentStatusUpdates.add(mStatus);
1656 mStatusChanged.broadcast();
1657}
1658
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001659// Pause to reconfigure
1660status_t Camera3Device::internalPauseAndWaitLocked() {
1661 mRequestThread->setPaused(true);
1662 mPauseStateNotify = true;
1663
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001664 ALOGV("%s: Camera %s: Internal wait until idle", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001665 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1666 if (res != OK) {
1667 SET_ERR_L("Can't idle device in %f seconds!",
1668 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001669 }
1670
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001671 return res;
1672}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001673
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001674// Resume after internalPauseAndWaitLocked
1675status_t Camera3Device::internalResumeLocked() {
1676 status_t res;
1677
1678 mRequestThread->setPaused(false);
1679
1680 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1681 if (res != OK) {
1682 SET_ERR_L("Can't transition to active in %f seconds!",
1683 kActiveTimeout/1e9);
1684 }
1685 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001686 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001687}
1688
Ruben Brunk183f0562015-08-12 12:55:02 -07001689status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001690 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001691
1692 size_t startIndex = 0;
1693 if (mStatusWaiters == 0) {
1694 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1695 // this status list
1696 mRecentStatusUpdates.clear();
1697 } else {
1698 // If other threads are waiting on updates to this status list, set the position of the
1699 // first element that this list will check rather than clearing the list.
1700 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001701 }
1702
Ruben Brunk183f0562015-08-12 12:55:02 -07001703 mStatusWaiters++;
1704
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001705 bool stateSeen = false;
1706 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001707 if (active == (mStatus == STATUS_ACTIVE)) {
1708 // Desired state is current
1709 break;
1710 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001711
1712 res = mStatusChanged.waitRelative(mLock, timeout);
1713 if (res != OK) break;
1714
Ruben Brunk183f0562015-08-12 12:55:02 -07001715 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1716 // transitions.
1717 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1718 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1719 __FUNCTION__);
1720
1721 // Encountered desired state since we began waiting
1722 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001723 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1724 stateSeen = true;
1725 break;
1726 }
1727 }
1728 } while (!stateSeen);
1729
Ruben Brunk183f0562015-08-12 12:55:02 -07001730 mStatusWaiters--;
1731
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001732 return res;
1733}
1734
1735
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001736status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001737 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001738 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001739
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001740 if (listener != NULL && mListener != NULL) {
1741 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1742 }
1743 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001744 mRequestThread->setNotificationListener(listener);
1745 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001746
1747 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001748}
1749
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001750bool Camera3Device::willNotify3A() {
1751 return false;
1752}
1753
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001754status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001755 status_t res;
1756 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001757
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001758 while (mResultQueue.empty()) {
1759 res = mResultSignal.waitRelative(mOutputLock, timeout);
1760 if (res == TIMED_OUT) {
1761 return res;
1762 } else if (res != OK) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001763 ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
1764 __FUNCTION__, mId.string(), timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001765 return res;
1766 }
1767 }
1768 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001769}
1770
Jianing Weicb0652e2014-03-12 18:29:36 -07001771status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001772 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001773 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001774
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001775 if (mResultQueue.empty()) {
1776 return NOT_ENOUGH_DATA;
1777 }
1778
Jianing Weicb0652e2014-03-12 18:29:36 -07001779 if (frame == NULL) {
1780 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1781 return BAD_VALUE;
1782 }
1783
1784 CaptureResult &result = *(mResultQueue.begin());
1785 frame->mResultExtras = result.mResultExtras;
1786 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001787 mResultQueue.erase(mResultQueue.begin());
1788
1789 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001790}
1791
1792status_t Camera3Device::triggerAutofocus(uint32_t id) {
1793 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001794 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001795
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001796 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1797 // Mix-in this trigger into the next request and only the next request.
1798 RequestTrigger trigger[] = {
1799 {
1800 ANDROID_CONTROL_AF_TRIGGER,
1801 ANDROID_CONTROL_AF_TRIGGER_START
1802 },
1803 {
1804 ANDROID_CONTROL_AF_TRIGGER_ID,
1805 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001806 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001807 };
1808
1809 return mRequestThread->queueTrigger(trigger,
1810 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001811}
1812
1813status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1814 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001815 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001816
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001817 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1818 // Mix-in this trigger into the next request and only the next request.
1819 RequestTrigger trigger[] = {
1820 {
1821 ANDROID_CONTROL_AF_TRIGGER,
1822 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1823 },
1824 {
1825 ANDROID_CONTROL_AF_TRIGGER_ID,
1826 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001827 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001828 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001829
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001830 return mRequestThread->queueTrigger(trigger,
1831 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001832}
1833
1834status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1835 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001836 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001837
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001838 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1839 // Mix-in this trigger into the next request and only the next request.
1840 RequestTrigger trigger[] = {
1841 {
1842 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1843 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1844 },
1845 {
1846 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1847 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001848 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001849 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001850
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001851 return mRequestThread->queueTrigger(trigger,
1852 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001853}
1854
1855status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1856 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1857 ATRACE_CALL();
1858 (void)reprocessStreamId; (void)buffer; (void)listener;
1859
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001860 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001861 return INVALID_OPERATION;
1862}
1863
Jianing Weicb0652e2014-03-12 18:29:36 -07001864status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001865 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001866 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001867 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001868
Zhijun He7ef20392014-04-21 16:04:17 -07001869 {
1870 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001871 mRequestThread->clear(/*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001872 }
1873
Zhijun He491e3412013-12-27 10:57:44 -08001874 status_t res;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001875 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07001876 res = mRequestThread->flush();
Zhijun He491e3412013-12-27 10:57:44 -08001877 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001878 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001879 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001880 }
1881
1882 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001883}
1884
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001885status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001886 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1887}
1888
1889status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001890 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001891 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001892 Mutex::Autolock il(mInterfaceLock);
1893 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001894
1895 sp<Camera3StreamInterface> stream;
1896 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1897 if (outputStreamIdx == NAME_NOT_FOUND) {
1898 CLOGE("Stream %d does not exist", streamId);
1899 return BAD_VALUE;
1900 }
1901
1902 stream = mOutputStreams.editValueAt(outputStreamIdx);
1903
1904 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001905 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001906 return BAD_VALUE;
1907 }
1908
1909 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001910 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001911 return BAD_VALUE;
1912 }
1913
Ruben Brunkc78ac262015-08-13 17:58:46 -07001914 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001915}
1916
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001917status_t Camera3Device::tearDown(int streamId) {
1918 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001919 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001920 Mutex::Autolock il(mInterfaceLock);
1921 Mutex::Autolock l(mLock);
1922
1923 // Teardown can only be accomplished on devices that don't require register_stream_buffers,
1924 // since we cannot call register_stream_buffers except right after configure_streams.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001925 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001926 ALOGE("%s: Unable to tear down streams on device HAL v%x",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001927 __FUNCTION__, mDeviceVersion);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001928 return NO_INIT;
1929 }
1930
1931 sp<Camera3StreamInterface> stream;
1932 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1933 if (outputStreamIdx == NAME_NOT_FOUND) {
1934 CLOGE("Stream %d does not exist", streamId);
1935 return BAD_VALUE;
1936 }
1937
1938 stream = mOutputStreams.editValueAt(outputStreamIdx);
1939
1940 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1941 CLOGE("Stream %d is a target of a in-progress request", streamId);
1942 return BAD_VALUE;
1943 }
1944
1945 return stream->tearDown();
1946}
1947
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001948status_t Camera3Device::addBufferListenerForStream(int streamId,
1949 wp<Camera3StreamBufferListener> listener) {
1950 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001951 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001952 Mutex::Autolock il(mInterfaceLock);
1953 Mutex::Autolock l(mLock);
1954
1955 sp<Camera3StreamInterface> stream;
1956 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
1957 if (outputStreamIdx == NAME_NOT_FOUND) {
1958 CLOGE("Stream %d does not exist", streamId);
1959 return BAD_VALUE;
1960 }
1961
1962 stream = mOutputStreams.editValueAt(outputStreamIdx);
1963 stream->addBufferListener(listener);
1964
1965 return OK;
1966}
1967
Zhijun He204e3292014-07-14 17:09:23 -07001968uint32_t Camera3Device::getDeviceVersion() {
1969 ATRACE_CALL();
1970 Mutex::Autolock il(mInterfaceLock);
1971 return mDeviceVersion;
1972}
1973
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001974/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001975 * Methods called by subclasses
1976 */
1977
1978void Camera3Device::notifyStatus(bool idle) {
1979 {
1980 // Need mLock to safely update state and synchronize to current
1981 // state of methods in flight.
1982 Mutex::Autolock l(mLock);
1983 // We can get various system-idle notices from the status tracker
1984 // while starting up. Only care about them if we've actually sent
1985 // in some requests recently.
1986 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1987 return;
1988 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001989 ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001990 idle ? "idle" : "active");
Ruben Brunk183f0562015-08-12 12:55:02 -07001991 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001992
1993 // Skip notifying listener if we're doing some user-transparent
1994 // state changes
1995 if (mPauseStateNotify) return;
1996 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001997
1998 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001999 {
2000 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002001 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002002 }
2003 if (idle && listener != NULL) {
2004 listener->notifyIdle();
2005 }
2006}
2007
Zhijun He5d677d12016-05-29 16:52:39 -07002008status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
2009 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002010 ALOGV("%s: Camera %s: set consumer surface for stream %d", __FUNCTION__, mId.string(), streamId);
Zhijun He5d677d12016-05-29 16:52:39 -07002011 Mutex::Autolock il(mInterfaceLock);
2012 Mutex::Autolock l(mLock);
2013
2014 if (consumer == nullptr) {
2015 CLOGE("Null consumer is passed!");
2016 return BAD_VALUE;
2017 }
2018
2019 ssize_t idx = mOutputStreams.indexOfKey(streamId);
2020 if (idx == NAME_NOT_FOUND) {
2021 CLOGE("Stream %d is unknown", streamId);
2022 return idx;
2023 }
2024 sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
2025 status_t res = stream->setConsumer(consumer);
2026 if (res != OK) {
2027 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2028 return res;
2029 }
2030
2031 if (!stream->isConfiguring()) {
2032 CLOGE("Stream %d was already fully configured.", streamId);
2033 return INVALID_OPERATION;
2034 }
2035
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002036 res = stream->finishConfiguration();
Zhijun He5d677d12016-05-29 16:52:39 -07002037 if (res != OK) {
2038 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2039 stream->getId(), strerror(-res), res);
2040 return res;
2041 }
2042
2043 return OK;
2044}
2045
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002046/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047 * Camera3Device private methods
2048 */
2049
2050sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
2051 const CameraMetadata &request) {
2052 ATRACE_CALL();
2053 status_t res;
2054
2055 sp<CaptureRequest> newRequest = new CaptureRequest;
2056 newRequest->mSettings = request;
2057
2058 camera_metadata_entry_t inputStreams =
2059 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
2060 if (inputStreams.count > 0) {
2061 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002062 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002063 CLOGE("Request references unknown input stream %d",
2064 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002065 return NULL;
2066 }
2067 // Lazy completion of stream configuration (allocation/registration)
2068 // on first use
2069 if (mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002070 res = mInputStream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002072 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002074 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075 return NULL;
2076 }
2077 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002078 // Check if stream is being prepared
2079 if (mInputStream->isPreparing()) {
2080 CLOGE("Request references an input stream that's being prepared!");
2081 return NULL;
2082 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002083
2084 newRequest->mInputStream = mInputStream;
2085 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
2086 }
2087
2088 camera_metadata_entry_t streams =
2089 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2090 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002091 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 return NULL;
2093 }
2094
2095 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07002096 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002098 CLOGE("Request references unknown stream %d",
2099 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002100 return NULL;
2101 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07002102 sp<Camera3OutputStreamInterface> stream =
2103 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002104
Zhijun He5d677d12016-05-29 16:52:39 -07002105 // It is illegal to include a deferred consumer output stream into a request
2106 if (stream->isConsumerConfigurationDeferred()) {
2107 CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
2108 stream->getId());
2109 return NULL;
2110 }
2111
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002112 // Lazy completion of stream configuration (allocation/registration)
2113 // on first use
2114 if (stream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002115 res = stream->finishConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002116 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002117 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
2118 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002119 return NULL;
2120 }
2121 }
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002122 // Check if stream is being prepared
2123 if (stream->isPreparing()) {
2124 CLOGE("Request references an output stream that's being prepared!");
2125 return NULL;
2126 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002127
2128 newRequest->mOutputStreams.push(stream);
2129 }
2130 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002131 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002132
2133 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002134}
2135
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002136bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
2137 for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
2138 Size size = mSupportedOpaqueInputSizes[i];
2139 if (size.width == width && size.height == height) {
2140 return true;
2141 }
2142 }
2143
2144 return false;
2145}
2146
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002147void Camera3Device::cancelStreamsConfigurationLocked() {
2148 int res = OK;
2149 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2150 res = mInputStream->cancelConfiguration();
2151 if (res != OK) {
2152 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2153 mInputStream->getId(), strerror(-res), res);
2154 }
2155 }
2156
2157 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2158 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
2159 if (outputStream->isConfiguring()) {
2160 res = outputStream->cancelConfiguration();
2161 if (res != OK) {
2162 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2163 outputStream->getId(), strerror(-res), res);
2164 }
2165 }
2166 }
2167
2168 // Return state to that at start of call, so that future configures
2169 // properly clean things up
2170 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2171 mNeedConfig = true;
2172}
2173
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002174status_t Camera3Device::configureStreamsLocked() {
2175 ATRACE_CALL();
2176 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002177
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002178 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002179 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002180 return INVALID_OPERATION;
2181 }
2182
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002183 if (!mNeedConfig) {
2184 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2185 return OK;
2186 }
2187
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002188 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2189 // adding a dummy stream instead.
2190 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2191 if (mOutputStreams.size() == 0) {
2192 addDummyStreamLocked();
2193 } else {
2194 tryRemoveDummyStreamLocked();
2195 }
2196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002197 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002198 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002199
2200 camera3_stream_configuration config;
Zhijun He1fa89992015-06-01 15:44:31 -07002201 config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
2202 CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
2203 CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002204 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2205
2206 Vector<camera3_stream_t*> streams;
2207 streams.setCapacity(config.num_streams);
2208
2209 if (mInputStream != NULL) {
2210 camera3_stream_t *inputStream;
2211 inputStream = mInputStream->startConfiguration();
2212 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002213 CLOGE("Can't start input stream configuration");
2214 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002215 return INVALID_OPERATION;
2216 }
2217 streams.add(inputStream);
2218 }
2219
2220 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002221
2222 // Don't configure bidi streams twice, nor add them twice to the list
2223 if (mOutputStreams[i].get() ==
2224 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2225
2226 config.num_streams--;
2227 continue;
2228 }
2229
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002230 camera3_stream_t *outputStream;
2231 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
2232 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002233 CLOGE("Can't start output stream configuration");
2234 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002235 return INVALID_OPERATION;
2236 }
2237 streams.add(outputStream);
2238 }
2239
2240 config.streams = streams.editArray();
2241
2242 // Do the HAL configuration; will potentially touch stream
2243 // max_buffers, usage, priv fields.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002244
2245 res = mInterface->configureStreams(&config);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002246
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002247 if (res == BAD_VALUE) {
2248 // HAL rejected this set of streams as unsupported, clean up config
2249 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002250 CLOGE("Set of requested inputs/outputs not supported by HAL");
2251 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002252 return BAD_VALUE;
2253 } else if (res != OK) {
2254 // Some other kind of error from configure_streams - this is not
2255 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002256 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2257 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002258 return res;
2259 }
2260
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002261 // Finish all stream configuration immediately.
2262 // TODO: Try to relax this later back to lazy completion, which should be
2263 // faster
2264
Igor Murashkin073f8572013-05-02 14:59:28 -07002265 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002266 res = mInputStream->finishConfiguration();
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002267 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002268 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002269 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002270 cancelStreamsConfigurationLocked();
2271 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002272 }
2273 }
2274
2275 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07002276 sp<Camera3OutputStreamInterface> outputStream =
2277 mOutputStreams.editValueAt(i);
Zhijun He5d677d12016-05-29 16:52:39 -07002278 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002279 res = outputStream->finishConfiguration();
Igor Murashkin073f8572013-05-02 14:59:28 -07002280 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002281 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002282 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002283 cancelStreamsConfigurationLocked();
2284 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002285 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002286 }
2287 }
2288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002289 // Request thread needs to know to avoid using repeat-last-settings protocol
2290 // across configure_streams() calls
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07002291 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002292
Zhijun He90f7c372016-08-16 16:19:43 -07002293 char value[PROPERTY_VALUE_MAX];
2294 property_get("camera.fifo.disable", value, "0");
2295 int32_t disableFifo = atoi(value);
2296 if (disableFifo != 1) {
2297 // Boost priority of request thread to SCHED_FIFO.
2298 pid_t requestThreadTid = mRequestThread->getTid();
2299 res = requestPriority(getpid(), requestThreadTid,
2300 kRequestThreadPriority, /*asynchronous*/ false);
2301 if (res != OK) {
2302 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2303 strerror(-res), res);
2304 } else {
2305 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2306 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002307 }
2308
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002309 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002310
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002311 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002312
Ruben Brunk183f0562015-08-12 12:55:02 -07002313 internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
2314 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002315
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002316 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002317
Zhijun He0a210512014-07-24 13:45:15 -07002318 // tear down the deleted streams after configure streams.
2319 mDeletedStreams.clear();
2320
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002321 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002322}
2323
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002324status_t Camera3Device::addDummyStreamLocked() {
2325 ATRACE_CALL();
2326 status_t res;
2327
2328 if (mDummyStreamId != NO_STREAM) {
2329 // Should never be adding a second dummy stream when one is already
2330 // active
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002331 SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
2332 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002333 return INVALID_OPERATION;
2334 }
2335
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002336 ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002337
2338 sp<Camera3OutputStreamInterface> dummyStream =
2339 new Camera3DummyStream(mNextStreamId);
2340
2341 res = mOutputStreams.add(mNextStreamId, dummyStream);
2342 if (res < 0) {
2343 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
2344 return res;
2345 }
2346
2347 mDummyStreamId = mNextStreamId;
2348 mNextStreamId++;
2349
2350 return OK;
2351}
2352
2353status_t Camera3Device::tryRemoveDummyStreamLocked() {
2354 ATRACE_CALL();
2355 status_t res;
2356
2357 if (mDummyStreamId == NO_STREAM) return OK;
2358 if (mOutputStreams.size() == 1) return OK;
2359
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002360 ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002361
2362 // Ok, have a dummy stream and there's at least one other output stream,
2363 // so remove the dummy
2364
2365 sp<Camera3StreamInterface> deletedStream;
2366 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
2367 if (outputStreamIdx == NAME_NOT_FOUND) {
2368 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
2369 return INVALID_OPERATION;
2370 }
2371
2372 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
2373 mOutputStreams.removeItemsAt(outputStreamIdx);
2374
2375 // Free up the stream endpoint so that it can be used by some other stream
2376 res = deletedStream->disconnect();
2377 if (res != OK) {
2378 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
2379 // fall through since we want to still list the stream as deleted.
2380 }
2381 mDeletedStreams.add(deletedStream);
2382 mDummyStreamId = NO_STREAM;
2383
2384 return res;
2385}
2386
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002387void Camera3Device::setErrorState(const char *fmt, ...) {
2388 Mutex::Autolock l(mLock);
2389 va_list args;
2390 va_start(args, fmt);
2391
2392 setErrorStateLockedV(fmt, args);
2393
2394 va_end(args);
2395}
2396
2397void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2398 Mutex::Autolock l(mLock);
2399 setErrorStateLockedV(fmt, args);
2400}
2401
2402void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2403 va_list args;
2404 va_start(args, fmt);
2405
2406 setErrorStateLockedV(fmt, args);
2407
2408 va_end(args);
2409}
2410
2411void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002412 // Print out all error messages to log
2413 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002414 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002415
2416 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002417 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002418
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002419 mErrorCause = errorCause;
2420
2421 mRequestThread->setPaused(true);
Ruben Brunk183f0562015-08-12 12:55:02 -07002422 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002423
2424 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002425 sp<NotificationListener> listener = mListener.promote();
2426 if (listener != NULL) {
2427 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002428 CaptureResultExtras());
2429 }
2430
2431 // Save stack trace. View by dumping it later.
2432 CameraTraces::saveTrace();
2433 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002434}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002435
2436/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002437 * In-flight request management
2438 */
2439
Jianing Weicb0652e2014-03-12 18:29:36 -07002440status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002441 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2442 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002443 ATRACE_CALL();
2444 Mutex::Autolock l(mInFlightLock);
2445
2446 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002447 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2448 aeTriggerCancelOverride));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002449 if (res < 0) return res;
2450
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002451 if (mInFlightMap.size() == 1) {
2452 mStatusTracker->markComponentActive(mInFlightStatusId);
2453 }
2454
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002455 return OK;
2456}
2457
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002458void Camera3Device::returnOutputBuffers(
2459 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
2460 nsecs_t timestamp) {
2461 for (size_t i = 0; i < numBuffers; i++)
2462 {
2463 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
2464 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
2465 // Note: stream may be deallocated at this point, if this buffer was
2466 // the last reference to it.
2467 if (res != OK) {
2468 ALOGE("Can't return buffer to its stream: %s (%d)",
2469 strerror(-res), res);
2470 }
2471 }
2472}
2473
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002474void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2475 mInFlightMap.removeItemsAt(idx, 1);
2476
2477 // Indicate idle inFlightMap to the status tracker
2478 if (mInFlightMap.size() == 0) {
2479 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2480 }
2481}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002482
2483void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
2484
2485 const InFlightRequest &request = mInFlightMap.valueAt(idx);
2486 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
2487
2488 nsecs_t sensorTimestamp = request.sensorTimestamp;
2489 nsecs_t shutterTimestamp = request.shutterTimestamp;
2490
2491 // Check if it's okay to remove the request from InFlightMap:
2492 // In the case of a successful request:
2493 // all input and output buffers, all result metadata, shutter callback
2494 // arrived.
2495 // In the case of a unsuccessful request:
2496 // all input and output buffers arrived.
2497 if (request.numBuffersLeft == 0 &&
2498 (request.requestStatus != OK ||
2499 (request.haveResultMetadata && shutterTimestamp != 0))) {
2500 ATRACE_ASYNC_END("frame capture", frameNumber);
2501
2502 // Sanity check - if sensor timestamp matches shutter timestamp
2503 if (request.requestStatus == OK &&
2504 sensorTimestamp != shutterTimestamp) {
2505 SET_ERR("sensor timestamp (%" PRId64
2506 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
2507 sensorTimestamp, frameNumber, shutterTimestamp);
2508 }
2509
2510 // for an unsuccessful request, it may have pending output buffers to
2511 // return.
2512 assert(request.requestStatus != OK ||
2513 request.pendingOutputBuffers.size() == 0);
2514 returnOutputBuffers(request.pendingOutputBuffers.array(),
2515 request.pendingOutputBuffers.size(), 0);
2516
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002517 removeInFlightMapEntryLocked(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002518 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
2519 }
2520
2521 // Sanity check - if we have too many in-flight frames, something has
2522 // likely gone wrong
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002523 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002524 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Chien-Yu Chenc96ac8d2015-08-12 16:46:24 -07002525 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2526 kInFlightWarnLimitHighSpeed) {
2527 CLOGE("In-flight list too large for high speed configuration: %zu",
2528 mInFlightMap.size());
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002529 }
2530}
2531
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002532void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
2533 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2534 if (result == nullptr) return;
2535
2536 if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2537 (int32_t*)&frameNumber, 1) != OK) {
2538 SET_ERR("Failed to set frame number %d in metadata", frameNumber);
2539 return;
2540 }
2541
2542 if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
2543 SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
2544 return;
2545 }
2546
2547 overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
2548
2549 // Valid result, insert into queue
2550 List<CaptureResult>::iterator queuedResult =
2551 mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
2552 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2553 ", burstId = %" PRId32, __FUNCTION__,
2554 queuedResult->mResultExtras.requestId,
2555 queuedResult->mResultExtras.frameNumber,
2556 queuedResult->mResultExtras.burstId);
2557
2558 mResultSignal.signal();
2559}
2560
2561
2562void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
2563 const CaptureResultExtras &resultExtras, uint32_t frameNumber,
2564 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
2565 Mutex::Autolock l(mOutputLock);
2566
2567 CaptureResult captureResult;
2568 captureResult.mResultExtras = resultExtras;
2569 captureResult.mMetadata = partialResult;
2570
2571 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
2572}
2573
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002574
2575void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
2576 CaptureResultExtras &resultExtras,
2577 CameraMetadata &collectedPartialResult,
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002578 uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002579 bool reprocess,
2580 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002581 if (pendingMetadata.isEmpty())
2582 return;
2583
2584 Mutex::Autolock l(mOutputLock);
2585
2586 // TODO: need to track errors for tighter bounds on expected frame number
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002587 if (reprocess) {
2588 if (frameNumber < mNextReprocessResultFrameNumber) {
2589 SET_ERR("Out-of-order reprocess capture result metadata submitted! "
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002590 "(got frame number %d, expecting %d)",
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07002591 frameNumber, mNextReprocessResultFrameNumber);
2592 return;
2593 }
2594 mNextReprocessResultFrameNumber = frameNumber + 1;
2595 } else {
2596 if (frameNumber < mNextResultFrameNumber) {
2597 SET_ERR("Out-of-order capture result metadata submitted! "
2598 "(got frame number %d, expecting %d)",
2599 frameNumber, mNextResultFrameNumber);
2600 return;
2601 }
2602 mNextResultFrameNumber = frameNumber + 1;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002603 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002604
2605 CaptureResult captureResult;
2606 captureResult.mResultExtras = resultExtras;
2607 captureResult.mMetadata = pendingMetadata;
2608
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002609 // Append any previous partials to form a complete result
2610 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2611 captureResult.mMetadata.append(collectedPartialResult);
2612 }
2613
Yin-Chia Yeh4c060992016-04-11 17:40:12 -07002614 // Derive some new keys for backward compaibility
2615 if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
2616 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
2617 int32_t defaultBoost[1] = {100};
2618 captureResult.mMetadata.update(
2619 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
2620 defaultBoost, 1);
2621 }
2622
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002623 captureResult.mMetadata.sort();
2624
2625 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002626 camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2627 if (timestamp.count == 0) {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002628 SET_ERR("No timestamp provided by HAL for frame %d!",
2629 frameNumber);
2630 return;
2631 }
2632
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002633 mTagMonitor.monitorMetadata(TagMonitor::RESULT,
2634 frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
2635
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002636 insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002637}
2638
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002639/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002640 * Camera HAL device callback methods
2641 */
2642
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002643void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002644 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002645
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002646 status_t res;
2647
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002648 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002649 if (result->result == NULL && result->num_output_buffers == 0 &&
2650 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002651 SET_ERR("No result data provided by HAL for frame %d",
2652 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002653 return;
2654 }
Zhijun He204e3292014-07-14 17:09:23 -07002655
2656 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2657 // partial_result to 1 when metadata is included in this result.
2658 if (!mUsePartialResult &&
2659 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2660 result->result != NULL &&
2661 result->partial_result != 1) {
2662 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2663 " if partial result is not supported",
2664 frameNumber, result->partial_result);
2665 return;
2666 }
2667
2668 bool isPartialResult = false;
2669 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002670 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002671 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002672
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002673 // Get shutter timestamp and resultExtras from list of in-flight requests,
2674 // where it was added by the shutter notification for this frame. If the
2675 // shutter timestamp isn't received yet, append the output buffers to the
2676 // in-flight request and they will be returned when the shutter timestamp
2677 // arrives. Update the in-flight status and remove the in-flight entry if
2678 // all result data and shutter timestamp have been received.
2679 nsecs_t shutterTimestamp = 0;
2680
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002681 {
2682 Mutex::Autolock l(mInFlightLock);
2683 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2684 if (idx == NAME_NOT_FOUND) {
2685 SET_ERR("Unknown frame number for capture result: %d",
2686 frameNumber);
2687 return;
2688 }
2689 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002690 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2691 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2692 ", partialResultCount = %d",
2693 __FUNCTION__, request.resultExtras.requestId,
2694 request.resultExtras.frameNumber, request.resultExtras.burstId,
2695 result->partial_result);
2696 // Always update the partial count to the latest one if it's not 0
2697 // (buffers only). When framework aggregates adjacent partial results
2698 // into one, the latest partial count will be used.
2699 if (result->partial_result != 0)
2700 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002701
2702 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002703 if (mUsePartialResult && result->result != NULL) {
2704 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2705 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2706 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2707 " the range of [1, %d] when metadata is included in the result",
2708 frameNumber, result->partial_result, mNumPartialResults);
2709 return;
2710 }
2711 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002712 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002713 request.collectedPartialResult.append(result->result);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002714 }
Zhijun He204e3292014-07-14 17:09:23 -07002715 } else {
2716 camera_metadata_ro_entry_t partialResultEntry;
2717 res = find_camera_metadata_ro_entry(result->result,
2718 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2719 if (res != NAME_NOT_FOUND &&
2720 partialResultEntry.count > 0 &&
2721 partialResultEntry.data.u8[0] ==
2722 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2723 // A partial result. Flag this as such, and collect this
2724 // set of metadata into the in-flight entry.
2725 isPartialResult = true;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002726 request.collectedPartialResult.append(
Zhijun He204e3292014-07-14 17:09:23 -07002727 result->result);
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002728 request.collectedPartialResult.erase(
Zhijun He204e3292014-07-14 17:09:23 -07002729 ANDROID_QUIRKS_PARTIAL_RESULT);
2730 }
2731 }
2732
2733 if (isPartialResult) {
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002734 // Send partial capture result
2735 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
2736 request.aeTriggerCancelOverride);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002737 }
2738 }
2739
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002740 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002741 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002742
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002743 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002744 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002745 if (request.haveResultMetadata) {
2746 SET_ERR("Called multiple times with metadata for frame %d",
2747 frameNumber);
2748 return;
2749 }
Zhijun He204e3292014-07-14 17:09:23 -07002750 if (mUsePartialResult &&
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002751 !request.collectedPartialResult.isEmpty()) {
Zhijun He204e3292014-07-14 17:09:23 -07002752 collectedPartialResult.acquire(
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002753 request.collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002754 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002755 request.haveResultMetadata = true;
2756 }
2757
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002758 uint32_t numBuffersReturned = result->num_output_buffers;
2759 if (result->input_buffer != NULL) {
2760 if (hasInputBufferInRequest) {
2761 numBuffersReturned += 1;
2762 } else {
2763 ALOGW("%s: Input buffer should be NULL if there is no input"
2764 " buffer sent in the request",
2765 __FUNCTION__);
2766 }
2767 }
2768 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002769 if (request.numBuffersLeft < 0) {
2770 SET_ERR("Too many buffers returned for frame %d",
2771 frameNumber);
2772 return;
2773 }
2774
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002775 camera_metadata_ro_entry_t entry;
2776 res = find_camera_metadata_ro_entry(result->result,
2777 ANDROID_SENSOR_TIMESTAMP, &entry);
2778 if (res == OK && entry.count == 1) {
2779 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002780 }
2781
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002782 // If shutter event isn't received yet, append the output buffers to
2783 // the in-flight request. Otherwise, return the output buffers to
2784 // streams.
2785 if (shutterTimestamp == 0) {
2786 request.pendingOutputBuffers.appendArray(result->output_buffers,
2787 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002788 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002789 returnOutputBuffers(result->output_buffers,
2790 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002791 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002792
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002793 if (result->result != NULL && !isPartialResult) {
2794 if (shutterTimestamp == 0) {
2795 request.pendingMetadata = result->result;
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002796 request.collectedPartialResult = collectedPartialResult;
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002797 } else {
2798 CameraMetadata metadata;
2799 metadata = result->result;
2800 sendCaptureResult(metadata, request.resultExtras,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002801 collectedPartialResult, frameNumber, hasInputBufferInRequest,
2802 request.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002803 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002804 }
2805
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002806 removeInFlightRequestIfReadyLocked(idx);
2807 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002808
Zhijun Hef0d962a2014-06-30 10:24:11 -07002809 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002810 if (hasInputBufferInRequest) {
2811 Camera3Stream *stream =
2812 Camera3Stream::cast(result->input_buffer->stream);
2813 res = stream->returnInputBuffer(*(result->input_buffer));
2814 // Note: stream may be deallocated at this point, if this buffer was the
2815 // last reference to it.
2816 if (res != OK) {
2817 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2818 " its stream:%s (%d)", __FUNCTION__,
2819 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002820 }
2821 } else {
2822 ALOGW("%s: Input buffer should be NULL if there is no input"
2823 " buffer sent in the request, skipping input buffer return.",
2824 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002825 }
2826 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002827}
2828
2829void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002830 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002831 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002832 {
2833 Mutex::Autolock l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002834 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002835 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002836
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002837 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002838 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002839 return;
2840 }
2841
2842 switch (msg->type) {
2843 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002844 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002845 break;
2846 }
2847 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002848 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002849 break;
2850 }
2851 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002852 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002853 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002854 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002855}
2856
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002857void Camera3Device::notifyError(const camera3_error_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002858 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002859
2860 // Map camera HAL error codes to ICameraDeviceCallback error codes
2861 // Index into this with the HAL error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002862 static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002863 // 0 = Unused error code
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002864 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002865 // 1 = CAMERA3_MSG_ERROR_DEVICE
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002866 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002867 // 2 = CAMERA3_MSG_ERROR_REQUEST
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002868 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002869 // 3 = CAMERA3_MSG_ERROR_RESULT
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002870 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002871 // 4 = CAMERA3_MSG_ERROR_BUFFER
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002872 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002873 };
2874
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002875 int32_t errorCode =
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002876 ((msg.error_code >= 0) &&
2877 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2878 halErrorMap[msg.error_code] :
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002879 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002880
2881 int streamId = 0;
2882 if (msg.error_stream != NULL) {
2883 Camera3Stream *stream =
2884 Camera3Stream::cast(msg.error_stream);
2885 streamId = stream->getId();
2886 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002887 ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
2888 mId.string(), __FUNCTION__, msg.frame_number,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002889 streamId, msg.error_code);
2890
2891 CaptureResultExtras resultExtras;
2892 switch (errorCode) {
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002893 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002894 // SET_ERR calls notifyError
2895 SET_ERR("Camera HAL reported serious device error");
2896 break;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08002897 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2898 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2899 case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002900 {
2901 Mutex::Autolock l(mInFlightLock);
2902 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2903 if (idx >= 0) {
2904 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2905 r.requestStatus = msg.error_code;
2906 resultExtras = r.resultExtras;
2907 } else {
2908 resultExtras.frameNumber = msg.frame_number;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002909 ALOGE("Camera %s: %s: cannot find in-flight request on "
2910 "frame %" PRId64 " error", mId.string(), __FUNCTION__,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002911 resultExtras.frameNumber);
2912 }
2913 }
Eino-Ville Talvalae95bb632016-03-06 19:55:44 -08002914 resultExtras.errorStreamId = streamId;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002915 if (listener != NULL) {
2916 listener->notifyError(errorCode, resultExtras);
2917 } else {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002918 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002919 }
2920 break;
2921 default:
2922 // SET_ERR calls notifyError
2923 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2924 break;
2925 }
2926}
2927
2928void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002929 sp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002930 ssize_t idx;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002931
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002932 // Set timestamp for the request in the in-flight tracking
2933 // and get the request ID to send upstream
2934 {
2935 Mutex::Autolock l(mInFlightLock);
2936 idx = mInFlightMap.indexOfKey(msg.frame_number);
2937 if (idx >= 0) {
2938 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002939
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -07002940 // Verify ordering of shutter notifications
2941 {
2942 Mutex::Autolock l(mOutputLock);
2943 // TODO: need to track errors for tighter bounds on expected frame number.
2944 if (r.hasInputBuffer) {
2945 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
2946 SET_ERR("Shutter notification out-of-order. Expected "
2947 "notification for frame %d, got frame %d",
2948 mNextReprocessShutterFrameNumber, msg.frame_number);
2949 return;
2950 }
2951 mNextReprocessShutterFrameNumber = msg.frame_number + 1;
2952 } else {
2953 if (msg.frame_number < mNextShutterFrameNumber) {
2954 SET_ERR("Shutter notification out-of-order. Expected "
2955 "notification for frame %d, got frame %d",
2956 mNextShutterFrameNumber, msg.frame_number);
2957 return;
2958 }
2959 mNextShutterFrameNumber = msg.frame_number + 1;
2960 }
2961 }
2962
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002963 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2964 mId.string(), __FUNCTION__,
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002965 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2966 // Call listener, if any
2967 if (listener != NULL) {
2968 listener->notifyShutter(r.resultExtras, msg.timestamp);
2969 }
2970
2971 r.shutterTimestamp = msg.timestamp;
2972
2973 // send pending result and buffers
2974 sendCaptureResult(r.pendingMetadata, r.resultExtras,
Chien-Yu Chen5cd8d642016-03-08 14:46:58 -08002975 r.collectedPartialResult, msg.frame_number,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002976 r.hasInputBuffer, r.aeTriggerCancelOverride);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002977 returnOutputBuffers(r.pendingOutputBuffers.array(),
2978 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2979 r.pendingOutputBuffers.clear();
2980
2981 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002982 }
2983 }
2984 if (idx < 0) {
2985 SET_ERR("Shutter notification for non-existent frame number %d",
2986 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002987 }
2988}
2989
2990
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002991CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002992 ALOGV("%s", __FUNCTION__);
2993
Igor Murashkin1e479c02013-09-06 16:55:14 -07002994 CameraMetadata retVal;
2995
2996 if (mRequestThread != NULL) {
2997 retVal = mRequestThread->getLatestRequest();
2998 }
2999
Igor Murashkin1e479c02013-09-06 16:55:14 -07003000 return retVal;
3001}
3002
Jianing Weicb0652e2014-03-12 18:29:36 -07003003
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003004void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3005 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3006 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3007}
3008
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003009/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003010 * HalInterface inner class methods
3011 */
3012
3013Camera3Device::HalInterface::HalInterface(camera3_device_t *device) :
3014 mHal3Device(device) {}
3015
3016Camera3Device::HalInterface::HalInterface(sp<ICameraDeviceSession> &session) :
3017 mHal3Device(nullptr),
3018 mHidlSession(session) {}
3019
3020Camera3Device::HalInterface::HalInterface() :
3021 mHal3Device(nullptr) {}
3022
3023Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
3024 mHal3Device(other.mHal3Device), mHidlSession(other.mHidlSession) {}
3025
3026bool Camera3Device::HalInterface::valid() {
3027 return (mHal3Device != nullptr) || (mHidlSession != nullptr);
3028}
3029
3030void Camera3Device::HalInterface::clear() {
3031 mHal3Device = nullptr;
3032 mHidlSession.clear();
3033}
3034
3035status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3036 camera3_request_template_t templateId,
3037 /*out*/ camera_metadata_t **requestTemplate) {
3038 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3039 if (!valid()) return INVALID_OPERATION;
3040 status_t res = OK;
3041
3042 if (mHal3Device != nullptr) {
3043 const camera_metadata *r;
3044 r = mHal3Device->ops->construct_default_request_settings(
3045 mHal3Device, templateId);
3046 if (r == nullptr) return BAD_VALUE;
3047 *requestTemplate = clone_camera_metadata(r);
3048 if (requestTemplate == nullptr) {
3049 ALOGE("%s: Unable to clone camera metadata received from HAL",
3050 __FUNCTION__);
3051 return INVALID_OPERATION;
3052 }
3053 } else {
3054 common::V1_0::Status status;
3055 RequestTemplate id;
3056 switch (templateId) {
3057 case CAMERA3_TEMPLATE_PREVIEW:
3058 id = RequestTemplate::PREVIEW;
3059 break;
3060 case CAMERA3_TEMPLATE_STILL_CAPTURE:
3061 id = RequestTemplate::STILL_CAPTURE;
3062 break;
3063 case CAMERA3_TEMPLATE_VIDEO_RECORD:
3064 id = RequestTemplate::VIDEO_RECORD;
3065 break;
3066 case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3067 id = RequestTemplate::VIDEO_SNAPSHOT;
3068 break;
3069 case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3070 id = RequestTemplate::ZERO_SHUTTER_LAG;
3071 break;
3072 case CAMERA3_TEMPLATE_MANUAL:
3073 id = RequestTemplate::MANUAL;
3074 break;
3075 default:
3076 // Unknown template ID
3077 return BAD_VALUE;
3078 }
3079 mHidlSession->constructDefaultRequestSettings(id,
3080 [&status, &requestTemplate]
3081 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3082 status = s;
3083 if (status == common::V1_0::Status::OK) {
3084 const camera_metadata *r =
3085 reinterpret_cast<const camera_metadata_t*>(request.data());
3086 size_t expectedSize = request.size();
3087 int ret = validate_camera_metadata_structure(r, &expectedSize);
3088 if (ret == OK) {
3089 *requestTemplate = clone_camera_metadata(r);
3090 if (*requestTemplate == nullptr) {
3091 ALOGE("%s: Unable to clone camera metadata received from HAL",
3092 __FUNCTION__);
3093 status = common::V1_0::Status::INTERNAL_ERROR;
3094 }
3095 } else {
3096 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3097 status = common::V1_0::Status::INTERNAL_ERROR;
3098 }
3099 }
3100 });
3101 res = CameraProviderManager::mapToStatusT(status);
3102 }
3103 return res;
3104}
3105
3106status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
3107 ATRACE_NAME("CameraHal::configureStreams");
3108 if (!valid()) return INVALID_OPERATION;
3109 status_t res = OK;
3110
3111 if (mHal3Device != nullptr) {
3112 res = mHal3Device->ops->configure_streams(mHal3Device, config);
3113 } else {
3114 // Convert stream config to HIDL
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003115 std::set<int> activeStreams;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003116 StreamConfiguration requestedConfiguration;
3117 requestedConfiguration.streams.resize(config->num_streams);
3118 for (size_t i = 0; i < config->num_streams; i++) {
3119 Stream &dst = requestedConfiguration.streams[i];
3120 camera3_stream_t *src = config->streams[i];
3121
3122 int streamId = Camera3Stream::cast(src)->getId();
3123 StreamType streamType;
3124 switch (src->stream_type) {
3125 case CAMERA3_STREAM_OUTPUT:
3126 streamType = StreamType::OUTPUT;
3127 break;
3128 case CAMERA3_STREAM_INPUT:
3129 streamType = StreamType::INPUT;
3130 break;
3131 default:
3132 ALOGE("%s: Stream %d: Unsupported stream type %d",
3133 __FUNCTION__, streamId, config->streams[i]->stream_type);
3134 return BAD_VALUE;
3135 }
3136 dst.id = streamId;
3137 dst.streamType = streamType;
3138 dst.width = src->width;
3139 dst.height = src->height;
3140 dst.format = mapToPixelFormat(src->format);
3141 dst.usage = mapToConsumerUsage(src->usage);
3142 dst.dataSpace = mapToHidlDataspace(src->data_space);
3143 dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003144
3145 activeStreams.insert(streamId);
3146 // Create Buffer ID map if necessary
3147 if (mBufferIdMaps.count(streamId) == 0) {
3148 mBufferIdMaps.emplace(streamId, BufferIdMap{});
3149 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003150 }
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003151 // remove BufferIdMap for deleted streams
3152 for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3153 int streamId = it->first;
3154 bool active = activeStreams.count(streamId) > 0;
3155 if (!active) {
3156 it = mBufferIdMaps.erase(it);
3157 } else {
3158 ++it;
3159 }
3160 }
3161
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003162 requestedConfiguration.operationMode = mapToStreamConfigurationMode(
3163 (camera3_stream_configuration_mode_t) config->operation_mode);
3164
3165 // Invoke configureStreams
3166
3167 HalStreamConfiguration finalConfiguration;
3168 common::V1_0::Status status;
3169 mHidlSession->configureStreams(requestedConfiguration,
3170 [&status, &finalConfiguration]
3171 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3172 finalConfiguration = halConfiguration;
3173 status = s;
3174 });
3175 if (status != common::V1_0::Status::OK ) {
3176 return CameraProviderManager::mapToStatusT(status);
3177 }
3178
3179 // And convert output stream configuration from HIDL
3180
3181 for (size_t i = 0; i < config->num_streams; i++) {
3182 camera3_stream_t *dst = config->streams[i];
3183 int streamId = Camera3Stream::cast(dst)->getId();
3184
3185 // Start scan at i, with the assumption that the stream order matches
3186 size_t realIdx = i;
3187 bool found = false;
3188 for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3189 if (finalConfiguration.streams[realIdx].id == streamId) {
3190 found = true;
3191 break;
3192 }
3193 realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3194 }
3195 if (!found) {
3196 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3197 __FUNCTION__, streamId);
3198 return INVALID_OPERATION;
3199 }
3200 HalStream &src = finalConfiguration.streams[realIdx];
3201
3202 int overrideFormat = mapToFrameworkFormat(src.overrideFormat);
3203 if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3204 if (dst->format != overrideFormat) {
3205 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3206 streamId, dst->format);
3207 }
3208 } else {
3209 // Override allowed with IMPLEMENTATION_DEFINED
3210 dst->format = overrideFormat;
3211 }
3212
3213 if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3214 if (src.producerUsage != 0) {
3215 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3216 __FUNCTION__, streamId);
3217 return INVALID_OPERATION;
3218 }
3219 dst->usage = mapConsumerToFrameworkUsage(src.consumerUsage);
3220 } else {
3221 // OUTPUT
3222 if (src.consumerUsage != 0) {
3223 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3224 __FUNCTION__, streamId);
3225 return INVALID_OPERATION;
3226 }
3227 dst->usage = mapProducerToFrameworkUsage(src.producerUsage);
3228 }
3229 dst->max_buffers = src.maxBuffers;
3230 }
3231 }
3232 return res;
3233}
3234
3235status_t Camera3Device::HalInterface::processCaptureRequest(
3236 camera3_capture_request_t *request) {
3237 ATRACE_NAME("CameraHal::processCaptureRequest");
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003238 if (!valid()) return INVALID_OPERATION;
3239 status_t res = OK;
3240
3241 if (mHal3Device != nullptr) {
3242 res = mHal3Device->ops->process_capture_request(mHal3Device, request);
3243 } else {
3244 device::V3_2::CaptureRequest captureRequest;
3245 captureRequest.frameNumber = request->frame_number;
3246 std::vector<native_handle_t*> handlesCreated;
3247 // A null request settings maps to a size-0 CameraMetadata
3248 if (request->settings != nullptr) {
3249 captureRequest.settings.setToExternal(
3250 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3251 get_camera_metadata_size(request->settings));
3252 }
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003253
3254 {
3255 std::lock_guard<std::mutex> lock(mInflightLock);
3256 if (request->input_buffer != nullptr) {
3257 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003258 buffer_handle_t buf = *(request->input_buffer->buffer);
3259 auto pair = getBufferId(buf, streamId);
3260 bool isNewBuffer = pair.first;
3261 uint64_t bufferId = pair.second;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003262 captureRequest.inputBuffer.streamId = streamId;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003263 captureRequest.inputBuffer.bufferId = bufferId;
3264 captureRequest.inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003265 captureRequest.inputBuffer.status = BufferStatus::OK;
3266 native_handle_t *acquireFence = nullptr;
3267 if (request->input_buffer->acquire_fence != -1) {
3268 acquireFence = native_handle_create(1,0);
3269 acquireFence->data[0] = request->input_buffer->acquire_fence;
3270 handlesCreated.push_back(acquireFence);
3271 }
3272 captureRequest.inputBuffer.acquireFence = acquireFence;
3273 captureRequest.inputBuffer.releaseFence = nullptr;
3274
3275 pushInflightBufferLocked(captureRequest.frameNumber, streamId,
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003276 request->input_buffer->buffer,
3277 request->input_buffer->acquire_fence);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003278 } else {
3279 captureRequest.inputBuffer.streamId = -1;
3280 captureRequest.inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003281 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003282
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003283 captureRequest.outputBuffers.resize(request->num_output_buffers);
3284 for (size_t i = 0; i < request->num_output_buffers; i++) {
3285 const camera3_stream_buffer_t *src = request->output_buffers + i;
3286 StreamBuffer &dst = captureRequest.outputBuffers[i];
3287 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003288 buffer_handle_t buf = *(src->buffer);
3289 auto pair = getBufferId(buf, streamId);
3290 bool isNewBuffer = pair.first;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003291 dst.streamId = streamId;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003292 dst.bufferId = pair.second;
3293 dst.buffer = isNewBuffer ? buf : nullptr;
Yin-Chia Yeh52778d42016-12-22 18:20:43 -08003294 dst.status = BufferStatus::OK;
3295 native_handle_t *acquireFence = nullptr;
3296 if (src->acquire_fence != -1) {
3297 acquireFence = native_handle_create(1,0);
3298 acquireFence->data[0] = src->acquire_fence;
3299 handlesCreated.push_back(acquireFence);
3300 }
3301 dst.acquireFence = acquireFence;
3302 dst.releaseFence = nullptr;
3303
3304 pushInflightBufferLocked(captureRequest.frameNumber, streamId,
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003305 src->buffer, src->acquire_fence);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003306 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003307 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003308 common::V1_0::Status status = mHidlSession->processCaptureRequest(captureRequest);
3309
3310 for (auto& handle : handlesCreated) {
3311 native_handle_delete(handle);
3312 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003313 res = CameraProviderManager::mapToStatusT(status);
3314 }
3315 return res;
3316}
3317
3318status_t Camera3Device::HalInterface::flush() {
3319 ATRACE_NAME("CameraHal::flush");
3320 if (!valid()) return INVALID_OPERATION;
3321 status_t res = OK;
3322
3323 if (mHal3Device != nullptr) {
3324 res = mHal3Device->ops->flush(mHal3Device);
3325 } else {
3326 res = CameraProviderManager::mapToStatusT(mHidlSession->flush());
3327 }
3328 return res;
3329}
3330
3331status_t Camera3Device::HalInterface::dump(int fd) {
3332 ATRACE_NAME("CameraHal::dump");
3333 if (!valid()) return INVALID_OPERATION;
3334 status_t res = OK;
3335
3336 if (mHal3Device != nullptr) {
3337 mHal3Device->ops->dump(mHal3Device, fd);
3338 } else {
3339 // Handled by CameraProviderManager::dump
3340 }
3341 return res;
3342}
3343
3344status_t Camera3Device::HalInterface::close() {
3345 ATRACE_NAME("CameraHal::close()");
3346 if (!valid()) return INVALID_OPERATION;
3347 status_t res = OK;
3348
3349 if (mHal3Device != nullptr) {
3350 mHal3Device->common.close(&mHal3Device->common);
3351 } else {
3352 mHidlSession->close();
3353 }
3354 return res;
3355}
3356
3357status_t Camera3Device::HalInterface::pushInflightBufferLocked(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003358 int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003359 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003360 auto pair = std::make_pair(buffer, acquireFence);
3361 mInflightBufferMap[key] = pair;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003362 return OK;
3363}
3364
3365status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003366 int32_t frameNumber, int32_t streamId,
3367 /*out*/ buffer_handle_t **buffer) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003368 std::lock_guard<std::mutex> lock(mInflightLock);
3369
3370 uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3371 auto it = mInflightBufferMap.find(key);
3372 if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003373 auto pair = it->second;
3374 *buffer = pair.first;
3375 int acquireFence = pair.second;
3376 if (acquireFence > 0) {
3377 ::close(acquireFence);
3378 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003379 mInflightBufferMap.erase(it);
3380 return OK;
3381}
3382
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003383std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3384 const buffer_handle_t& buf, int streamId) {
3385 std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3386
3387 BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
3388 auto it = bIdMap.find(buf);
3389 if (it == bIdMap.end()) {
3390 bIdMap[buf] = mNextBufferId++;
3391 return std::make_pair(true, mNextBufferId - 1);
3392 } else {
3393 return std::make_pair(false, it->second);
3394 }
3395}
3396
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003397/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003398 * RequestThread inner class methods
3399 */
3400
3401Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003402 sp<StatusTracker> statusTracker,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003403 HalInterface* interface,
3404 uint32_t deviceVersion,
Chien-Yu Chenab5135b2015-06-30 11:20:58 -07003405 bool aeLockAvailable) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003406 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003407 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003408 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003409 mInterface(interface),
3410 mDeviceVersion(deviceVersion),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003411 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003412 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003413 mReconfigured(false),
3414 mDoPause(false),
3415 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003416 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003417 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003418 mCurrentAfTriggerId(0),
3419 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003420 mRepeatingLastFrameNumber(
3421 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003422 mAeLockAvailable(aeLockAvailable),
3423 mPrepareVideoStream(false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003424 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003425}
3426
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003427Camera3Device::RequestThread::~RequestThread() {}
3428
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003429void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003430 wp<NotificationListener> listener) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003431 Mutex::Autolock l(mRequestLock);
3432 mListener = listener;
3433}
3434
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003435void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003436 Mutex::Autolock l(mRequestLock);
3437 mReconfigured = true;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003438 // Prepare video stream for high speed recording.
3439 mPrepareVideoStream = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003440}
3441
Jianing Wei90e59c92014-03-12 18:29:36 -07003442status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003443 List<sp<CaptureRequest> > &requests,
3444 /*out*/
3445 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07003446 Mutex::Autolock l(mRequestLock);
3447 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3448 ++it) {
3449 mRequestQueue.push_back(*it);
3450 }
3451
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003452 if (lastFrameNumber != NULL) {
3453 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3454 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3455 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3456 *lastFrameNumber);
3457 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003458
Jianing Wei90e59c92014-03-12 18:29:36 -07003459 unpauseForNewRequests();
3460
3461 return OK;
3462}
3463
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003464
3465status_t Camera3Device::RequestThread::queueTrigger(
3466 RequestTrigger trigger[],
3467 size_t count) {
3468
3469 Mutex::Autolock l(mTriggerMutex);
3470 status_t ret;
3471
3472 for (size_t i = 0; i < count; ++i) {
3473 ret = queueTriggerLocked(trigger[i]);
3474
3475 if (ret != OK) {
3476 return ret;
3477 }
3478 }
3479
3480 return OK;
3481}
3482
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003483const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3484 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003485 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003486 if (d != nullptr) return d->mId;
3487 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003488}
3489
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003490status_t Camera3Device::RequestThread::queueTriggerLocked(
3491 RequestTrigger trigger) {
3492
3493 uint32_t tag = trigger.metadataTag;
3494 ssize_t index = mTriggerMap.indexOfKey(tag);
3495
3496 switch (trigger.getTagType()) {
3497 case TYPE_BYTE:
3498 // fall-through
3499 case TYPE_INT32:
3500 break;
3501 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003502 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3503 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003504 return INVALID_OPERATION;
3505 }
3506
3507 /**
3508 * Collect only the latest trigger, since we only have 1 field
3509 * in the request settings per trigger tag, and can't send more than 1
3510 * trigger per request.
3511 */
3512 if (index != NAME_NOT_FOUND) {
3513 mTriggerMap.editValueAt(index) = trigger;
3514 } else {
3515 mTriggerMap.add(tag, trigger);
3516 }
3517
3518 return OK;
3519}
3520
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003521status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003522 const RequestList &requests,
3523 /*out*/
3524 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003525 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003526 if (lastFrameNumber != NULL) {
3527 *lastFrameNumber = mRepeatingLastFrameNumber;
3528 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003529 mRepeatingRequests.clear();
3530 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3531 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003532
3533 unpauseForNewRequests();
3534
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003535 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003536 return OK;
3537}
3538
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003539bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003540 if (mRepeatingRequests.empty()) {
3541 return false;
3542 }
3543 int32_t requestId = requestIn->mResultExtras.requestId;
3544 const RequestList &repeatRequests = mRepeatingRequests;
3545 // All repeating requests are guaranteed to have same id so only check first quest
3546 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3547 return (firstRequest->mResultExtras.requestId == requestId);
3548}
3549
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003550status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003551 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003552 return clearRepeatingRequestsLocked(lastFrameNumber);
3553
3554}
3555
3556status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003557 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003558 if (lastFrameNumber != NULL) {
3559 *lastFrameNumber = mRepeatingLastFrameNumber;
3560 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003561 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003562 return OK;
3563}
3564
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003565status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003566 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003567 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003568 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003569
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003570 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003571
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003572 // Send errors for all requests pending in the request queue, including
3573 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003574 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003575 if (listener != NULL) {
3576 for (RequestList::iterator it = mRequestQueue.begin();
3577 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003578 // Abort the input buffers for reprocess requests.
3579 if ((*it)->mInputStream != NULL) {
3580 camera3_stream_buffer_t inputBuffer;
3581 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
3582 if (res != OK) {
3583 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3584 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3585 } else {
3586 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3587 if (res != OK) {
3588 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3589 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3590 }
3591 }
3592 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003593 // Set the frame number this request would have had, if it
3594 // had been submitted; this frame number will not be reused.
3595 // The requestId and burstId fields were set when the request was
3596 // submitted originally (in convertMetadataListToRequestListLocked)
3597 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003598 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003599 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003600 }
3601 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003602 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003603
3604 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003605 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003606 if (lastFrameNumber != NULL) {
3607 *lastFrameNumber = mRepeatingLastFrameNumber;
3608 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003609 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003610 return OK;
3611}
3612
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003613status_t Camera3Device::RequestThread::flush() {
3614 ATRACE_CALL();
3615 Mutex::Autolock l(mFlushLock);
3616
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003617 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_1) {
3618 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003619 }
3620
3621 return -ENOTSUP;
3622}
3623
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003624void Camera3Device::RequestThread::setPaused(bool paused) {
3625 Mutex::Autolock l(mPauseLock);
3626 mDoPause = paused;
3627 mDoPauseSignal.signal();
3628}
3629
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003630status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3631 int32_t requestId, nsecs_t timeout) {
3632 Mutex::Autolock l(mLatestRequestMutex);
3633 status_t res;
3634 while (mLatestRequestId != requestId) {
3635 nsecs_t startTime = systemTime();
3636
3637 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3638 if (res != OK) return res;
3639
3640 timeout -= (systemTime() - startTime);
3641 }
3642
3643 return OK;
3644}
3645
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003646void Camera3Device::RequestThread::requestExit() {
3647 // Call parent to set up shutdown
3648 Thread::requestExit();
3649 // The exit from any possible waits
3650 mDoPauseSignal.signal();
3651 mRequestSignal.signal();
3652}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003653
Chien-Yu Chend196d612015-06-22 19:49:01 -07003654
3655/**
3656 * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
3657 * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
3658 * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
3659 * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
3660 * request.
3661 */
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003662void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(const sp<CaptureRequest>& request) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003663 request->mAeTriggerCancelOverride.applyAeLock = false;
3664 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
3665
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003666 if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
Chien-Yu Chend196d612015-06-22 19:49:01 -07003667 return;
3668 }
3669
3670 camera_metadata_entry_t aePrecaptureTrigger =
3671 request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3672 if (aePrecaptureTrigger.count > 0 &&
3673 aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
3674 // Always override CANCEL to IDLE
3675 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
3676 request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
3677 request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
3678 request->mAeTriggerCancelOverride.aePrecaptureTrigger =
3679 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
3680
3681 if (mAeLockAvailable == true) {
3682 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
3683 if (aeLock.count == 0 || aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
3684 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
3685 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
3686 request->mAeTriggerCancelOverride.applyAeLock = true;
3687 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
3688 }
3689 }
3690 }
3691}
3692
3693/**
3694 * Override result metadata for cancelling AE precapture trigger applied in
3695 * handleAePrecaptureCancelRequest().
3696 */
3697void Camera3Device::overrideResultForPrecaptureCancel(
3698 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
3699 if (aeTriggerCancelOverride.applyAeLock) {
3700 // Only devices <= v3.2 should have this override
3701 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3702 result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
3703 }
3704
3705 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
3706 // Only devices <= v3.2 should have this override
3707 assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
3708 result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
3709 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
3710 }
3711}
3712
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003713void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003714 bool surfaceAbandoned = false;
3715 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003716 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003717 {
3718 Mutex::Autolock l(mRequestLock);
3719 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3720 // repeating requests.
3721 for (const auto& request : mRepeatingRequests) {
3722 for (const auto& s : request->mOutputStreams) {
3723 if (s->isAbandoned()) {
3724 surfaceAbandoned = true;
3725 clearRepeatingRequestsLocked(&lastFrameNumber);
3726 break;
3727 }
3728 }
3729 if (surfaceAbandoned) {
3730 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003731 }
3732 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003733 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003734 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003735
3736 if (listener != NULL && surfaceAbandoned) {
3737 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003738 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003739}
3740
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003741bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003742 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003743 status_t res;
3744
3745 // Handle paused state.
3746 if (waitIfPaused()) {
3747 return true;
3748 }
3749
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003750 // Wait for the next batch of requests.
3751 waitForNextRequestBatch();
3752 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003753 return true;
3754 }
3755
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003756 // Get the latest request ID, if any
3757 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003758 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003759 captureRequest->mSettings.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003760 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003761 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003762 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003763 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3764 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003765 }
3766
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003767 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003768 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003769 if (res == TIMED_OUT) {
3770 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003771 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003772 // Check if any stream is abandoned.
3773 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003774 return true;
3775 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003776 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003777 return false;
3778 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003779
Zhijun Hecc27e112013-10-03 16:12:43 -07003780 // Inform waitUntilRequestProcessed thread of a new request ID
3781 {
3782 Mutex::Autolock al(mLatestRequestMutex);
3783
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003784 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003785 mLatestRequestSignal.signal();
3786 }
3787
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003788 // Submit a batch of requests to HAL.
3789 // Use flush lock only when submitting multilple requests in a batch.
3790 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3791 // which may take a long time to finish so synchronizing flush() and
3792 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3793 // For now, only synchronize for high speed recording and we should figure something out for
3794 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003795 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003796
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003797 if (useFlushLock) {
3798 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003799 }
3800
Zhijun Hef0645c12016-08-02 00:58:11 -07003801 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003802 mNextRequests.size());
3803 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003804 // Submit request and block until ready for next one
3805 ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003806 res = mInterface->processCaptureRequest(&nextRequest.halRequest);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003807
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003808 if (res != OK) {
3809 // Should only get a failure here for malformed requests or device-level
3810 // errors, so consider all errors fatal. Bad metadata failures should
3811 // come through notify.
3812 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
3813 " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
3814 res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003815 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003816 if (useFlushLock) {
3817 mFlushLock.unlock();
3818 }
3819 return false;
3820 }
3821
3822 // Mark that the request has be submitted successfully.
3823 nextRequest.submitted = true;
3824
3825 // Update the latest request sent to HAL
3826 if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
3827 Mutex::Autolock al(mLatestRequestMutex);
3828
3829 camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
3830 mLatestRequest.acquire(cloned);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003831
3832 sp<Camera3Device> parent = mParent.promote();
3833 if (parent != NULL) {
3834 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
3835 0, mLatestRequest);
3836 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003837 }
3838
3839 if (nextRequest.halRequest.settings != NULL) {
3840 nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
3841 }
3842
3843 // Remove any previously queued triggers (after unlock)
3844 res = removeTriggers(mPrevRequest);
3845 if (res != OK) {
3846 SET_ERR("RequestThread: Unable to remove triggers "
3847 "(capture request %d, HAL device: %s (%d)",
3848 nextRequest.halRequest.frame_number, strerror(-res), res);
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003849 cleanUpFailedRequests(/*sendRequestError*/ false);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003850 if (useFlushLock) {
3851 mFlushLock.unlock();
3852 }
3853 return false;
3854 }
Igor Murashkin1e479c02013-09-06 16:55:14 -07003855 }
3856
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003857 if (useFlushLock) {
3858 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003859 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003860
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003861 // Unset as current request
3862 {
3863 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003864 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003865 }
3866
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003867 return true;
3868}
3869
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003870status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003871 ATRACE_CALL();
3872
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003873 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003874 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3875 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
3876 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3877
3878 // Prepare a request to HAL
3879 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3880
3881 // Insert any queued triggers (before metadata is locked)
3882 status_t res = insertTriggers(captureRequest);
3883
3884 if (res < 0) {
3885 SET_ERR("RequestThread: Unable to insert triggers "
3886 "(capture request %d, HAL device: %s (%d)",
3887 halRequest->frame_number, strerror(-res), res);
3888 return INVALID_OPERATION;
3889 }
3890 int triggerCount = res;
3891 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3892 mPrevTriggers = triggerCount;
3893
3894 // If the request is the same as last, or we had triggers last time
3895 if (mPrevRequest != captureRequest || triggersMixedIn) {
3896 /**
3897 * HAL workaround:
3898 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
3899 */
3900 res = addDummyTriggerIds(captureRequest);
3901 if (res != OK) {
3902 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
3903 "(capture request %d, HAL device: %s (%d)",
3904 halRequest->frame_number, strerror(-res), res);
3905 return INVALID_OPERATION;
3906 }
3907
3908 /**
3909 * The request should be presorted so accesses in HAL
3910 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3911 */
3912 captureRequest->mSettings.sort();
3913 halRequest->settings = captureRequest->mSettings.getAndLock();
3914 mPrevRequest = captureRequest;
3915 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3916
3917 IF_ALOGV() {
3918 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3919 find_camera_metadata_ro_entry(
3920 halRequest->settings,
3921 ANDROID_CONTROL_AF_TRIGGER,
3922 &e
3923 );
3924 if (e.count > 0) {
3925 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3926 __FUNCTION__,
3927 halRequest->frame_number,
3928 e.data.u8[0]);
3929 }
3930 }
3931 } else {
3932 // leave request.settings NULL to indicate 'reuse latest given'
3933 ALOGVV("%s: Request settings are REUSED",
3934 __FUNCTION__);
3935 }
3936
3937 uint32_t totalNumBuffers = 0;
3938
3939 // Fill in buffers
3940 if (captureRequest->mInputStream != NULL) {
3941 halRequest->input_buffer = &captureRequest->mInputBuffer;
3942 totalNumBuffers += 1;
3943 } else {
3944 halRequest->input_buffer = NULL;
3945 }
3946
3947 outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
3948 captureRequest->mOutputStreams.size());
3949 halRequest->output_buffers = outputBuffers->array();
3950 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003951 sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
3952
3953 // Prepare video buffers for high speed recording on the first video request.
3954 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3955 // Only try to prepare video stream on the first video request.
3956 mPrepareVideoStream = false;
3957
3958 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
3959 while (res == NOT_ENOUGH_DATA) {
3960 res = outputStream->prepareNextBuffer();
3961 }
3962 if (res != OK) {
3963 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3964 __FUNCTION__, strerror(-res), res);
3965 outputStream->cancelPrepare();
3966 }
3967 }
3968
3969 res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003970 if (res != OK) {
3971 // Can't get output buffer from gralloc queue - this could be due to
3972 // abandoned queue or other consumer misbehavior, so not a fatal
3973 // error
3974 ALOGE("RequestThread: Can't get output buffer, skipping request:"
3975 " %s (%d)", strerror(-res), res);
3976
3977 return TIMED_OUT;
3978 }
3979 halRequest->num_output_buffers++;
3980 }
3981 totalNumBuffers += halRequest->num_output_buffers;
3982
3983 // Log request in the in-flight queue
3984 sp<Camera3Device> parent = mParent.promote();
3985 if (parent == NULL) {
3986 // Should not happen, and nowhere to send errors to, so just log it
3987 CLOGE("RequestThread: Parent is gone");
3988 return INVALID_OPERATION;
3989 }
3990 res = parent->registerInFlight(halRequest->frame_number,
3991 totalNumBuffers, captureRequest->mResultExtras,
3992 /*hasInput*/halRequest->input_buffer != NULL,
3993 captureRequest->mAeTriggerCancelOverride);
3994 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
3995 ", burstId = %" PRId32 ".",
3996 __FUNCTION__,
3997 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
3998 captureRequest->mResultExtras.burstId);
3999 if (res != OK) {
4000 SET_ERR("RequestThread: Unable to register new in-flight request:"
4001 " %s (%d)", strerror(-res), res);
4002 return INVALID_OPERATION;
4003 }
4004 }
4005
4006 return OK;
4007}
4008
Igor Murashkin1e479c02013-09-06 16:55:14 -07004009CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4010 Mutex::Autolock al(mLatestRequestMutex);
4011
4012 ALOGV("RequestThread::%s", __FUNCTION__);
4013
4014 return mLatestRequest;
4015}
4016
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004017bool Camera3Device::RequestThread::isStreamPending(
4018 sp<Camera3StreamInterface>& stream) {
4019 Mutex::Autolock l(mRequestLock);
4020
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004021 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004022 if (!nextRequest.submitted) {
4023 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4024 if (stream == s) return true;
4025 }
4026 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004027 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004028 }
4029
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004030 for (const auto& request : mRequestQueue) {
4031 for (const auto& s : request->mOutputStreams) {
4032 if (stream == s) return true;
4033 }
4034 if (stream == request->mInputStream) return true;
4035 }
4036
4037 for (const auto& request : mRepeatingRequests) {
4038 for (const auto& s : request->mOutputStreams) {
4039 if (stream == s) return true;
4040 }
4041 if (stream == request->mInputStream) return true;
4042 }
4043
4044 return false;
4045}
Jianing Weicb0652e2014-03-12 18:29:36 -07004046
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004047void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4048 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004049 return;
4050 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004051
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004052 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004053 // Skip the ones that have been submitted successfully.
4054 if (nextRequest.submitted) {
4055 continue;
4056 }
4057
4058 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4059 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4060 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4061
4062 if (halRequest->settings != NULL) {
4063 captureRequest->mSettings.unlock(halRequest->settings);
4064 }
4065
4066 if (captureRequest->mInputStream != NULL) {
4067 captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
4068 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4069 }
4070
4071 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4072 outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
4073 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
4074 }
4075
4076 if (sendRequestError) {
4077 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004078 sp<NotificationListener> listener = mListener.promote();
4079 if (listener != NULL) {
4080 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004081 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004082 captureRequest->mResultExtras);
4083 }
4084 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004085
4086 // Remove yet-to-be submitted inflight request from inflightMap
4087 {
4088 sp<Camera3Device> parent = mParent.promote();
4089 if (parent != NULL) {
4090 Mutex::Autolock l(parent->mInFlightLock);
4091 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4092 if (idx >= 0) {
4093 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4094 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4095 parent->removeInFlightMapEntryLocked(idx);
4096 }
4097 }
4098 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004099 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004100
4101 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004102 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004103}
4104
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004105void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004106 // Optimized a bit for the simple steady-state case (single repeating
4107 // request), to avoid putting that request in the queue temporarily.
4108 Mutex::Autolock l(mRequestLock);
4109
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004110 assert(mNextRequests.empty());
4111
4112 NextRequest nextRequest;
4113 nextRequest.captureRequest = waitForNextRequestLocked();
4114 if (nextRequest.captureRequest == nullptr) {
4115 return;
4116 }
4117
4118 nextRequest.halRequest = camera3_capture_request_t();
4119 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004120 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004121
4122 // Wait for additional requests
4123 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4124
4125 for (size_t i = 1; i < batchSize; i++) {
4126 NextRequest additionalRequest;
4127 additionalRequest.captureRequest = waitForNextRequestLocked();
4128 if (additionalRequest.captureRequest == nullptr) {
4129 break;
4130 }
4131
4132 additionalRequest.halRequest = camera3_capture_request_t();
4133 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004134 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004135 }
4136
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004137 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004138 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004139 mNextRequests.size(), batchSize);
4140 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004141 }
4142
4143 return;
4144}
4145
4146sp<Camera3Device::CaptureRequest>
4147 Camera3Device::RequestThread::waitForNextRequestLocked() {
4148 status_t res;
4149 sp<CaptureRequest> nextRequest;
4150
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004151 while (mRequestQueue.empty()) {
4152 if (!mRepeatingRequests.empty()) {
4153 // Always atomically enqueue all requests in a repeating request
4154 // list. Guarantees a complete in-sequence set of captures to
4155 // application.
4156 const RequestList &requests = mRepeatingRequests;
4157 RequestList::const_iterator firstRequest =
4158 requests.begin();
4159 nextRequest = *firstRequest;
4160 mRequestQueue.insert(mRequestQueue.end(),
4161 ++firstRequest,
4162 requests.end());
4163 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004164
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004165 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004166
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004167 break;
4168 }
4169
4170 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4171
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004172 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4173 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004174 Mutex::Autolock pl(mPauseLock);
4175 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004176 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004177 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004178 // Let the tracker know
4179 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4180 if (statusTracker != 0) {
4181 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4182 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004183 }
4184 // Stop waiting for now and let thread management happen
4185 return NULL;
4186 }
4187 }
4188
4189 if (nextRequest == NULL) {
4190 // Don't have a repeating request already in hand, so queue
4191 // must have an entry now.
4192 RequestList::iterator firstRequest =
4193 mRequestQueue.begin();
4194 nextRequest = *firstRequest;
4195 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004196 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4197 sp<NotificationListener> listener = mListener.promote();
4198 if (listener != NULL) {
4199 listener->notifyRequestQueueEmpty();
4200 }
4201 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004202 }
4203
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004204 // In case we've been unpaused by setPaused clearing mDoPause, need to
4205 // update internal pause state (capture/setRepeatingRequest unpause
4206 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004207 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004208 if (mPaused) {
4209 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4210 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4211 if (statusTracker != 0) {
4212 statusTracker->markComponentActive(mStatusId);
4213 }
4214 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004215 mPaused = false;
4216
4217 // Check if we've reconfigured since last time, and reset the preview
4218 // request if so. Can't use 'NULL request == repeat' across configure calls.
4219 if (mReconfigured) {
4220 mPrevRequest.clear();
4221 mReconfigured = false;
4222 }
4223
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004224 if (nextRequest != NULL) {
4225 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004226 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4227 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004228
4229 // Since RequestThread::clear() removes buffers from the input stream,
4230 // get the right buffer here before unlocking mRequestLock
4231 if (nextRequest->mInputStream != NULL) {
4232 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
4233 if (res != OK) {
4234 // Can't get input buffer from gralloc queue - this could be due to
4235 // disconnected queue or other producer misbehavior, so not a fatal
4236 // error
4237 ALOGE("%s: Can't get input buffer, skipping request:"
4238 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004239
4240 sp<NotificationListener> listener = mListener.promote();
4241 if (listener != NULL) {
4242 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004243 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004244 nextRequest->mResultExtras);
4245 }
4246 return NULL;
4247 }
4248 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004249 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004250
4251 handleAePrecaptureCancelRequest(nextRequest);
4252
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004253 return nextRequest;
4254}
4255
4256bool Camera3Device::RequestThread::waitIfPaused() {
4257 status_t res;
4258 Mutex::Autolock l(mPauseLock);
4259 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004260 if (mPaused == false) {
4261 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004262 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4263 // Let the tracker know
4264 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4265 if (statusTracker != 0) {
4266 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4267 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004268 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004269
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004270 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004271 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004272 return true;
4273 }
4274 }
4275 // We don't set mPaused to false here, because waitForNextRequest needs
4276 // to further manage the paused state in case of starvation.
4277 return false;
4278}
4279
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004280void Camera3Device::RequestThread::unpauseForNewRequests() {
4281 // With work to do, mark thread as unpaused.
4282 // If paused by request (setPaused), don't resume, to avoid
4283 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004284 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004285 Mutex::Autolock p(mPauseLock);
4286 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004287 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4288 if (mPaused) {
4289 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4290 if (statusTracker != 0) {
4291 statusTracker->markComponentActive(mStatusId);
4292 }
4293 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004294 mPaused = false;
4295 }
4296}
4297
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004298void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4299 sp<Camera3Device> parent = mParent.promote();
4300 if (parent != NULL) {
4301 va_list args;
4302 va_start(args, fmt);
4303
4304 parent->setErrorStateV(fmt, args);
4305
4306 va_end(args);
4307 }
4308}
4309
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004310status_t Camera3Device::RequestThread::insertTriggers(
4311 const sp<CaptureRequest> &request) {
4312
4313 Mutex::Autolock al(mTriggerMutex);
4314
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004315 sp<Camera3Device> parent = mParent.promote();
4316 if (parent == NULL) {
4317 CLOGE("RequestThread: Parent is gone");
4318 return DEAD_OBJECT;
4319 }
4320
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004321 CameraMetadata &metadata = request->mSettings;
4322 size_t count = mTriggerMap.size();
4323
4324 for (size_t i = 0; i < count; ++i) {
4325 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004326 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004327
4328 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4329 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4330 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004331 if (isAeTrigger) {
4332 request->mResultExtras.precaptureTriggerId = triggerId;
4333 mCurrentPreCaptureTriggerId = triggerId;
4334 } else {
4335 request->mResultExtras.afTriggerId = triggerId;
4336 mCurrentAfTriggerId = triggerId;
4337 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004338 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
4339 continue; // Trigger ID tag is deprecated since device HAL 3.2
4340 }
4341 }
4342
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004343 camera_metadata_entry entry = metadata.find(tag);
4344
4345 if (entry.count > 0) {
4346 /**
4347 * Already has an entry for this trigger in the request.
4348 * Rewrite it with our requested trigger value.
4349 */
4350 RequestTrigger oldTrigger = trigger;
4351
4352 oldTrigger.entryValue = entry.data.u8[0];
4353
4354 mTriggerReplacedMap.add(tag, oldTrigger);
4355 } else {
4356 /**
4357 * More typical, no trigger entry, so we just add it
4358 */
4359 mTriggerRemovedMap.add(tag, trigger);
4360 }
4361
4362 status_t res;
4363
4364 switch (trigger.getTagType()) {
4365 case TYPE_BYTE: {
4366 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4367 res = metadata.update(tag,
4368 &entryValue,
4369 /*count*/1);
4370 break;
4371 }
4372 case TYPE_INT32:
4373 res = metadata.update(tag,
4374 &trigger.entryValue,
4375 /*count*/1);
4376 break;
4377 default:
4378 ALOGE("%s: Type not supported: 0x%x",
4379 __FUNCTION__,
4380 trigger.getTagType());
4381 return INVALID_OPERATION;
4382 }
4383
4384 if (res != OK) {
4385 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4386 ", value %d", __FUNCTION__, trigger.getTagName(),
4387 trigger.entryValue);
4388 return res;
4389 }
4390
4391 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4392 trigger.getTagName(),
4393 trigger.entryValue);
4394 }
4395
4396 mTriggerMap.clear();
4397
4398 return count;
4399}
4400
4401status_t Camera3Device::RequestThread::removeTriggers(
4402 const sp<CaptureRequest> &request) {
4403 Mutex::Autolock al(mTriggerMutex);
4404
4405 CameraMetadata &metadata = request->mSettings;
4406
4407 /**
4408 * Replace all old entries with their old values.
4409 */
4410 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4411 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4412
4413 status_t res;
4414
4415 uint32_t tag = trigger.metadataTag;
4416 switch (trigger.getTagType()) {
4417 case TYPE_BYTE: {
4418 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4419 res = metadata.update(tag,
4420 &entryValue,
4421 /*count*/1);
4422 break;
4423 }
4424 case TYPE_INT32:
4425 res = metadata.update(tag,
4426 &trigger.entryValue,
4427 /*count*/1);
4428 break;
4429 default:
4430 ALOGE("%s: Type not supported: 0x%x",
4431 __FUNCTION__,
4432 trigger.getTagType());
4433 return INVALID_OPERATION;
4434 }
4435
4436 if (res != OK) {
4437 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4438 ", trigger value %d", __FUNCTION__,
4439 trigger.getTagName(), trigger.entryValue);
4440 return res;
4441 }
4442 }
4443 mTriggerReplacedMap.clear();
4444
4445 /**
4446 * Remove all new entries.
4447 */
4448 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4449 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4450 status_t res = metadata.erase(trigger.metadataTag);
4451
4452 if (res != OK) {
4453 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4454 ", trigger value %d", __FUNCTION__,
4455 trigger.getTagName(), trigger.entryValue);
4456 return res;
4457 }
4458 }
4459 mTriggerRemovedMap.clear();
4460
4461 return OK;
4462}
4463
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004464status_t Camera3Device::RequestThread::addDummyTriggerIds(
4465 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004466 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004467 static const int32_t dummyTriggerId = 1;
4468 status_t res;
4469
4470 CameraMetadata &metadata = request->mSettings;
4471
4472 // If AF trigger is active, insert a dummy AF trigger ID if none already
4473 // exists
4474 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4475 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4476 if (afTrigger.count > 0 &&
4477 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4478 afId.count == 0) {
4479 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
4480 if (res != OK) return res;
4481 }
4482
4483 // If AE precapture trigger is active, insert a dummy precapture trigger ID
4484 // if none already exists
4485 camera_metadata_entry pcTrigger =
4486 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4487 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4488 if (pcTrigger.count > 0 &&
4489 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4490 pcId.count == 0) {
4491 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4492 &dummyTriggerId, 1);
4493 if (res != OK) return res;
4494 }
4495
4496 return OK;
4497}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004498
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004499/**
4500 * PreparerThread inner class methods
4501 */
4502
4503Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004504 Thread(/*canCallJava*/false), mListener(nullptr),
4505 mActive(false), mCancelNow(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004506}
4507
4508Camera3Device::PreparerThread::~PreparerThread() {
4509 Thread::requestExitAndWait();
4510 if (mCurrentStream != nullptr) {
4511 mCurrentStream->cancelPrepare();
4512 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4513 mCurrentStream.clear();
4514 }
4515 clear();
4516}
4517
Ruben Brunkc78ac262015-08-13 17:58:46 -07004518status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004519 status_t res;
4520
4521 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004522 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004523
Ruben Brunkc78ac262015-08-13 17:58:46 -07004524 res = stream->startPrepare(maxCount);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004525 if (res == OK) {
4526 // No preparation needed, fire listener right off
4527 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004528 if (listener != NULL) {
4529 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004530 }
4531 return OK;
4532 } else if (res != NOT_ENOUGH_DATA) {
4533 return res;
4534 }
4535
4536 // Need to prepare, start up thread if necessary
4537 if (!mActive) {
4538 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
4539 // isn't running
4540 Thread::requestExitAndWait();
4541 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
4542 if (res != OK) {
4543 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004544 if (listener != NULL) {
4545 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004546 }
4547 return res;
4548 }
4549 mCancelNow = false;
4550 mActive = true;
4551 ALOGV("%s: Preparer stream started", __FUNCTION__);
4552 }
4553
4554 // queue up the work
4555 mPendingStreams.push_back(stream);
4556 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
4557
4558 return OK;
4559}
4560
4561status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004562 Mutex::Autolock l(mLock);
4563
4564 for (const auto& stream : mPendingStreams) {
4565 stream->cancelPrepare();
4566 }
4567 mPendingStreams.clear();
4568 mCancelNow = true;
4569
4570 return OK;
4571}
4572
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004573void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004574 Mutex::Autolock l(mLock);
4575 mListener = listener;
4576}
4577
4578bool Camera3Device::PreparerThread::threadLoop() {
4579 status_t res;
4580 {
4581 Mutex::Autolock l(mLock);
4582 if (mCurrentStream == nullptr) {
4583 // End thread if done with work
4584 if (mPendingStreams.empty()) {
4585 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
4586 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
4587 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
4588 mActive = false;
4589 return false;
4590 }
4591
4592 // Get next stream to prepare
4593 auto it = mPendingStreams.begin();
4594 mCurrentStream = *it;
4595 mPendingStreams.erase(it);
4596 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
4597 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
4598 } else if (mCancelNow) {
4599 mCurrentStream->cancelPrepare();
4600 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4601 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
4602 mCurrentStream.clear();
4603 mCancelNow = false;
4604 return true;
4605 }
4606 }
4607
4608 res = mCurrentStream->prepareNextBuffer();
4609 if (res == NOT_ENOUGH_DATA) return true;
4610 if (res != OK) {
4611 // Something bad happened; try to recover by cancelling prepare and
4612 // signalling listener anyway
4613 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
4614 mCurrentStream->getId(), res, strerror(-res));
4615 mCurrentStream->cancelPrepare();
4616 }
4617
4618 // This stream has finished, notify listener
4619 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004620 sp<NotificationListener> listener = mListener.promote();
4621 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004622 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
4623 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004624 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004625 }
4626
4627 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
4628 mCurrentStream.clear();
4629
4630 return true;
4631}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004632
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004633/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004634 * Static callback forwarding methods from HAL to instance
4635 */
4636
4637void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
4638 const camera3_capture_result *result) {
4639 Camera3Device *d =
4640 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
Chien-Yu Chend196d612015-06-22 19:49:01 -07004641
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08004642 d->processCaptureResult(result);
4643}
4644
4645void Camera3Device::sNotify(const camera3_callback_ops *cb,
4646 const camera3_notify_msg *msg) {
4647 Camera3Device *d =
4648 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
4649 d->notify(msg);
4650}
4651
4652}; // namespace android