blob: e5ac0370316183ce636f6725d8ee6c9b69e9fc01 [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
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__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
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Igor Murashkinff3e31d2013-10-23 16:40:06 -070044#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045#include "device3/Camera3Device.h"
46#include "device3/Camera3OutputStream.h"
47#include "device3/Camera3InputStream.h"
48#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080049
50using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051
52namespace android {
53
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054Camera3Device::Camera3Device(int id):
55 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070058 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070059 mNextResultFrameNumber(0),
60 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062{
63 ATRACE_CALL();
64 camera3_callback_ops::notify = &sNotify;
65 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
66 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
67}
68
69Camera3Device::~Camera3Device()
70{
71 ATRACE_CALL();
72 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
73 disconnect();
74}
75
Igor Murashkin71381052013-03-04 14:53:08 -080076int Camera3Device::getId() const {
77 return mId;
78}
79
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080080/**
81 * CameraDeviceBase interface
82 */
83
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084status_t Camera3Device::initialize(camera_module_t *module)
85{
86 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070087 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088 Mutex::Autolock l(mLock);
89
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070092 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 return INVALID_OPERATION;
94 }
95
96 /** Open HAL device */
97
98 status_t res;
99 String8 deviceName = String8::format("%d", mId);
100
101 camera3_device_t *device;
102
Zhijun He213ce792013-11-19 08:45:15 -0800103 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 res = module->common.methods->open(&module->common, deviceName.string(),
105 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800106 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107
108 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 return res;
111 }
112
113 /** Cross-check device version */
114
115 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 device->common.version);
120 device->common.close(&device->common);
121 return BAD_VALUE;
122 }
123
124 camera_info info;
125 res = module->get_camera_info(mId, &info);
126 if (res != OK) return res;
127
128 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
130 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version, info.device_version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 /** Initialize device with callback functions */
137
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700138 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_END();
141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
144 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700149 /** Start up status tracker thread */
150 mStatusTracker = new StatusTracker(this);
151 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
152 if (res != OK) {
153 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
154 strerror(-res), res);
155 device->common.close(&device->common);
156 mStatusTracker.clear();
157 return res;
158 }
159
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800160 /** Start up request queue thread */
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800163 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700165 SET_ERR_L("Unable to start request queue thread: %s (%d)",
166 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800168 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 return res;
170 }
171
172 /** Everything is good to go */
173
174 mDeviceInfo = info.static_camera_characteristics;
175 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700176 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800177 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700178 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700181 /** Check for quirks */
182
183 // Will the HAL be sending in early partial result metadata?
184 camera_metadata_entry partialResultsQuirk =
185 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
186 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
187 mUsePartialResultQuirk = true;
188 }
189
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 return OK;
191}
192
193status_t Camera3Device::disconnect() {
194 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700195 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800197 ALOGV("%s: E", __FUNCTION__);
198
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700199 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800200
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 {
202 Mutex::Autolock l(mLock);
203 if (mStatus == STATUS_UNINITIALIZED) return res;
204
205 if (mStatus == STATUS_ACTIVE ||
206 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
207 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700208 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700209 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700210 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 } else {
212 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
213 if (res != OK) {
214 SET_ERR_L("Timeout waiting for HAL to drain");
215 // Continue to close device even in case of error
216 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700217 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800218 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700220 if (mStatus == STATUS_ERROR) {
221 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700222 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223
224 if (mStatusTracker != NULL) {
225 mStatusTracker->requestExit();
226 }
227
228 if (mRequestThread != NULL) {
229 mRequestThread->requestExit();
230 }
231
232 mOutputStreams.clear();
233 mInputStream.clear();
234 }
235
236 // Joining done without holding mLock, otherwise deadlocks may ensue
237 // as the threads try to access parent state
238 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
239 // HAL may be in a bad state, so waiting for request thread
240 // (which may be stuck in the HAL processCaptureRequest call)
241 // could be dangerous.
242 mRequestThread->join();
243 }
244
245 if (mStatusTracker != NULL) {
246 mStatusTracker->join();
247 }
248
249 {
250 Mutex::Autolock l(mLock);
251
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800252 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800256 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800258 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 mHal3Device = NULL;
260 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800261
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 mStatus = STATUS_UNINITIALIZED;
263 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
265 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700266 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800267}
268
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269// For dumping/debugging only -
270// try to acquire a lock a few times, eventually give up to proceed with
271// debug/dump operations
272bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
273 bool gotLock = false;
274 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
275 if (lock.tryLock() == NO_ERROR) {
276 gotLock = true;
277 break;
278 } else {
279 usleep(kDumpSleepDuration);
280 }
281 }
282 return gotLock;
283}
284
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800285status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
286 ATRACE_CALL();
287 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288
289 // Try to lock, but continue in case of failure (to avoid blocking in
290 // deadlocks)
291 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
292 bool gotLock = tryLockSpinRightRound(mLock);
293
294 ALOGW_IF(!gotInterfaceLock,
295 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
296 mId, __FUNCTION__);
297 ALOGW_IF(!gotLock,
298 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
299 mId, __FUNCTION__);
300
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800302
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303 const char *status =
304 mStatus == STATUS_ERROR ? "ERROR" :
305 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
307 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 mStatus == STATUS_ACTIVE ? "ACTIVE" :
309 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700312 if (mStatus == STATUS_ERROR) {
313 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
314 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315 lines.appendFormat(" Stream configuration:\n");
316
317 if (mInputStream != NULL) {
318 write(fd, lines.string(), lines.size());
319 mInputStream->dump(fd, args);
320 } else {
321 lines.appendFormat(" No input stream.\n");
322 write(fd, lines.string(), lines.size());
323 }
324 for (size_t i = 0; i < mOutputStreams.size(); i++) {
325 mOutputStreams[i]->dump(fd,args);
326 }
327
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700328 lines = String8(" In-flight requests:\n");
329 if (mInFlightMap.size() == 0) {
330 lines.append(" None\n");
331 } else {
332 for (size_t i = 0; i < mInFlightMap.size(); i++) {
333 InFlightRequest r = mInFlightMap.valueAt(i);
334 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
335 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
336 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
337 r.numBuffersLeft);
338 }
339 }
340 write(fd, lines.string(), lines.size());
341
Igor Murashkin1e479c02013-09-06 16:55:14 -0700342 {
343 lines = String8(" Last request sent:\n");
344 write(fd, lines.string(), lines.size());
345
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700346 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700347 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
348 }
349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800350 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700351 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800352 write(fd, lines.string(), lines.size());
353 mHal3Device->ops->dump(mHal3Device, fd);
354 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800355
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700356 if (gotLock) mLock.unlock();
357 if (gotInterfaceLock) mInterfaceLock.unlock();
358
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800359 return OK;
360}
361
362const CameraMetadata& Camera3Device::info() const {
363 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800364 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
365 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700366 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367 mStatus == STATUS_ERROR ?
368 "when in error state" : "before init");
369 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800370 return mDeviceInfo;
371}
372
Jianing Wei90e59c92014-03-12 18:29:36 -0700373status_t Camera3Device::checkStatusOkToCaptureLocked() {
374 switch (mStatus) {
375 case STATUS_ERROR:
376 CLOGE("Device has encountered a serious error");
377 return INVALID_OPERATION;
378 case STATUS_UNINITIALIZED:
379 CLOGE("Device not initialized");
380 return INVALID_OPERATION;
381 case STATUS_UNCONFIGURED:
382 case STATUS_CONFIGURED:
383 case STATUS_ACTIVE:
384 // OK
385 break;
386 default:
387 SET_ERR_L("Unexpected status: %d", mStatus);
388 return INVALID_OPERATION;
389 }
390 return OK;
391}
392
393status_t Camera3Device::convertMetadataListToRequestListLocked(
394 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
395 if (requestList == NULL) {
396 CLOGE("requestList cannot be NULL.");
397 return BAD_VALUE;
398 }
399
400 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
401 it != metadataList.end(); ++it) {
402 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
403 if (newRequest == 0) {
404 CLOGE("Can't create capture request");
405 return BAD_VALUE;
406 }
407 requestList->push_back(newRequest);
408 }
409 return OK;
410}
411
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800412status_t Camera3Device::capture(CameraMetadata &request) {
413 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700414 status_t res;
415 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800416 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800417
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700418 // TODO: take ownership of the request
419
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800420 switch (mStatus) {
421 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700422 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800423 return INVALID_OPERATION;
424 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700425 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800426 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700427 case STATUS_UNCONFIGURED:
428 // May be lazily configuring streams, will check during setup
429 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800430 case STATUS_ACTIVE:
431 // OK
432 break;
433 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700434 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800435 return INVALID_OPERATION;
436 }
437
438 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
439 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700440 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800441 return BAD_VALUE;
442 }
443
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700444 res = mRequestThread->queueRequest(newRequest);
445 if (res == OK) {
446 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
447 if (res != OK) {
448 SET_ERR_L("Can't transition to active in %f seconds!",
449 kActiveTimeout/1e9);
450 }
451 ALOGV("Camera %d: Capture request enqueued", mId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700452 } else {
453 CLOGE("Cannot queue request. Impossible."); // queueRequest always returns OK.
454 return BAD_VALUE;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700455 }
456 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800457}
458
Jianing Wei90e59c92014-03-12 18:29:36 -0700459status_t Camera3Device::submitRequestsHelper(
460 const List<const CameraMetadata> &requests, bool repeating) {
461 ATRACE_CALL();
462 Mutex::Autolock il(mInterfaceLock);
463 Mutex::Autolock l(mLock);
464
465 status_t res = checkStatusOkToCaptureLocked();
466 if (res != OK) {
467 // error logged by previous call
468 return res;
469 }
470
471 RequestList requestList;
472
473 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
474 if (res != OK) {
475 // error logged by previous call
476 return res;
477 }
478
479 if (repeating) {
480 res = mRequestThread->setRepeatingRequests(requestList);
481 } else {
482 res = mRequestThread->queueRequestList(requestList);
483 }
484
485 if (res == OK) {
486 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
487 if (res != OK) {
488 SET_ERR_L("Can't transition to active in %f seconds!",
489 kActiveTimeout/1e9);
490 }
491 ALOGV("Camera %d: Capture request enqueued", mId);
492 } else {
493 CLOGE("Cannot queue request. Impossible.");
494 return BAD_VALUE;
495 }
496
497 return res;
498}
499
500status_t Camera3Device::captureList(const List<const CameraMetadata> &requests) {
501 ATRACE_CALL();
502
503 return submitRequestsHelper(requests, /*repeating*/false);
504}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800505
506status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
507 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700508 status_t res;
509 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800511
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800512 switch (mStatus) {
513 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700514 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800515 return INVALID_OPERATION;
516 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700517 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700519 case STATUS_UNCONFIGURED:
520 // May be lazily configuring streams, will check during setup
521 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800522 case STATUS_ACTIVE:
523 // OK
524 break;
525 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700526 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800527 return INVALID_OPERATION;
528 }
529
530 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
531 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700532 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800533 return BAD_VALUE;
534 }
535
536 RequestList newRepeatingRequests;
537 newRepeatingRequests.push_back(newRepeatingRequest);
538
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700539 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
540 if (res == OK) {
541 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
542 if (res != OK) {
543 SET_ERR_L("Can't transition to active in %f seconds!",
544 kActiveTimeout/1e9);
545 }
546 ALOGV("Camera %d: Repeating request set", mId);
547 }
548 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800549}
550
Jianing Wei90e59c92014-03-12 18:29:36 -0700551status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests) {
552 ATRACE_CALL();
553
554 return submitRequestsHelper(requests, /*repeating*/true);
555}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556
557sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
558 const CameraMetadata &request) {
559 status_t res;
560
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700561 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800562 res = configureStreamsLocked();
563 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700564 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 return NULL;
566 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700567 if (mStatus == STATUS_UNCONFIGURED) {
568 CLOGE("No streams configured");
569 return NULL;
570 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800571 }
572
573 sp<CaptureRequest> newRequest = createCaptureRequest(request);
574 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800575}
576
577status_t Camera3Device::clearStreamingRequest() {
578 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700579 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800580 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800581
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800582 switch (mStatus) {
583 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700584 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800585 return INVALID_OPERATION;
586 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700587 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800588 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700589 case STATUS_UNCONFIGURED:
590 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800591 case STATUS_ACTIVE:
592 // OK
593 break;
594 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700595 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800596 return INVALID_OPERATION;
597 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700598 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800599 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800600}
601
602status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
603 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700604 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800605
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700606 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800607}
608
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700609status_t Camera3Device::createInputStream(
610 uint32_t width, uint32_t height, int format, int *id) {
611 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700612 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700613 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
615 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700616
617 status_t res;
618 bool wasActive = false;
619
620 switch (mStatus) {
621 case STATUS_ERROR:
622 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
623 return INVALID_OPERATION;
624 case STATUS_UNINITIALIZED:
625 ALOGE("%s: Device not initialized", __FUNCTION__);
626 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700627 case STATUS_UNCONFIGURED:
628 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700629 // OK
630 break;
631 case STATUS_ACTIVE:
632 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700633 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700634 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700635 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700636 return res;
637 }
638 wasActive = true;
639 break;
640 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700641 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700642 return INVALID_OPERATION;
643 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700644 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700645
646 if (mInputStream != 0) {
647 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
648 return INVALID_OPERATION;
649 }
650
651 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
652 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700654
655 mInputStream = newStream;
656
657 *id = mNextStreamId++;
658
659 // Continue captures if active at start
660 if (wasActive) {
661 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
662 res = configureStreamsLocked();
663 if (res != OK) {
664 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
665 __FUNCTION__, mNextStreamId, strerror(-res), res);
666 return res;
667 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700669 }
670
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700671 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700672 return OK;
673}
674
Igor Murashkin2fba5842013-04-22 14:03:54 -0700675
676status_t Camera3Device::createZslStream(
677 uint32_t width, uint32_t height,
678 int depth,
679 /*out*/
680 int *id,
681 sp<Camera3ZslStream>* zslStream) {
682 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700684 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
686 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700687
688 status_t res;
689 bool wasActive = false;
690
691 switch (mStatus) {
692 case STATUS_ERROR:
693 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
694 return INVALID_OPERATION;
695 case STATUS_UNINITIALIZED:
696 ALOGE("%s: Device not initialized", __FUNCTION__);
697 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 case STATUS_UNCONFIGURED:
699 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700700 // OK
701 break;
702 case STATUS_ACTIVE:
703 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700705 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700707 return res;
708 }
709 wasActive = true;
710 break;
711 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700712 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700713 return INVALID_OPERATION;
714 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700715 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700716
717 if (mInputStream != 0) {
718 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
719 return INVALID_OPERATION;
720 }
721
722 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
723 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700725
726 res = mOutputStreams.add(mNextStreamId, newStream);
727 if (res < 0) {
728 ALOGE("%s: Can't add new stream to set: %s (%d)",
729 __FUNCTION__, strerror(-res), res);
730 return res;
731 }
732 mInputStream = newStream;
733
734 *id = mNextStreamId++;
735 *zslStream = newStream;
736
737 // Continue captures if active at start
738 if (wasActive) {
739 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
740 res = configureStreamsLocked();
741 if (res != OK) {
742 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
743 __FUNCTION__, mNextStreamId, strerror(-res), res);
744 return res;
745 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700747 }
748
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700749 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700750 return OK;
751}
752
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800753status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
754 uint32_t width, uint32_t height, int format, size_t size, int *id) {
755 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800757 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
759 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 status_t res;
762 bool wasActive = false;
763
764 switch (mStatus) {
765 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 return INVALID_OPERATION;
768 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700769 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 case STATUS_UNCONFIGURED:
772 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800773 // OK
774 break;
775 case STATUS_ACTIVE:
776 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 return res;
781 }
782 wasActive = true;
783 break;
784 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700785 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800786 return INVALID_OPERATION;
787 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700788 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789
790 sp<Camera3OutputStream> newStream;
791 if (format == HAL_PIXEL_FORMAT_BLOB) {
792 newStream = new Camera3OutputStream(mNextStreamId, consumer,
793 width, height, size, format);
794 } else {
795 newStream = new Camera3OutputStream(mNextStreamId, consumer,
796 width, height, format);
797 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800799
800 res = mOutputStreams.add(mNextStreamId, newStream);
801 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return res;
804 }
805
806 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700807 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800808
809 // Continue captures if active at start
810 if (wasActive) {
811 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
812 res = configureStreamsLocked();
813 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700814 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
815 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 return res;
817 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700818 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800822}
823
824status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
825 ATRACE_CALL();
826 (void)outputId; (void)id;
827
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700828 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800829 return INVALID_OPERATION;
830}
831
832
833status_t Camera3Device::getStreamInfo(int id,
834 uint32_t *width, uint32_t *height, uint32_t *format) {
835 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700836 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800838
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 switch (mStatus) {
840 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700841 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842 return INVALID_OPERATION;
843 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700844 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700846 case STATUS_UNCONFIGURED:
847 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848 case STATUS_ACTIVE:
849 // OK
850 break;
851 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700852 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 return INVALID_OPERATION;
854 }
855
856 ssize_t idx = mOutputStreams.indexOfKey(id);
857 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700858 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800859 return idx;
860 }
861
862 if (width) *width = mOutputStreams[idx]->getWidth();
863 if (height) *height = mOutputStreams[idx]->getHeight();
864 if (format) *format = mOutputStreams[idx]->getFormat();
865
866 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867}
868
869status_t Camera3Device::setStreamTransform(int id,
870 int transform) {
871 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 switch (mStatus) {
876 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700877 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 return INVALID_OPERATION;
879 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700880 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700882 case STATUS_UNCONFIGURED:
883 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 case STATUS_ACTIVE:
885 // OK
886 break;
887 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 return INVALID_OPERATION;
890 }
891
892 ssize_t idx = mOutputStreams.indexOfKey(id);
893 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700894 CLOGE("Stream %d does not exist",
895 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 return BAD_VALUE;
897 }
898
899 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800900}
901
902status_t Camera3Device::deleteStream(int id) {
903 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 Mutex::Autolock l(mLock);
906 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800907
Igor Murashkine2172be2013-05-28 15:31:39 -0700908 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
909
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 // CameraDevice semantics require device to already be idle before
911 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700913 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
914 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800915 }
916
Igor Murashkin2fba5842013-04-22 14:03:54 -0700917 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800918 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 if (mInputStream != NULL && id == mInputStream->getId()) {
920 deletedStream = mInputStream;
921 mInputStream.clear();
922 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800923 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700924 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 return BAD_VALUE;
926 }
Zhijun He5f446352014-01-22 09:49:33 -0800927 }
928
929 // Delete output stream or the output part of a bi-directional stream.
930 if (outputStreamIdx != NAME_NOT_FOUND) {
931 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 mOutputStreams.removeItem(id);
933 }
934
935 // Free up the stream endpoint so that it can be used by some other stream
936 res = deletedStream->disconnect();
937 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700938 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800939 // fall through since we want to still list the stream as deleted.
940 }
941 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700942 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800943
944 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800945}
946
947status_t Camera3Device::deleteReprocessStream(int id) {
948 ATRACE_CALL();
949 (void)id;
950
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700951 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800952 return INVALID_OPERATION;
953}
954
955
956status_t Camera3Device::createDefaultRequest(int templateId,
957 CameraMetadata *request) {
958 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700959 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700960 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 Mutex::Autolock l(mLock);
962
963 switch (mStatus) {
964 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700965 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800966 return INVALID_OPERATION;
967 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700968 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 case STATUS_UNCONFIGURED:
971 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 case STATUS_ACTIVE:
973 // OK
974 break;
975 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700976 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800977 return INVALID_OPERATION;
978 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800979
980 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700981 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800982 rawRequest = mHal3Device->ops->construct_default_request_settings(
983 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700984 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700985 if (rawRequest == NULL) {
986 SET_ERR_L("HAL is unable to construct default settings for template %d",
987 templateId);
988 return DEAD_OBJECT;
989 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800990 *request = rawRequest;
991
992 return OK;
993}
994
995status_t Camera3Device::waitUntilDrained() {
996 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700997 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800999
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000 switch (mStatus) {
1001 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001002 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 ALOGV("%s: Already idle", __FUNCTION__);
1004 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001005 case STATUS_CONFIGURED:
1006 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 case STATUS_ERROR:
1008 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001009 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001010 break;
1011 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001012 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 return INVALID_OPERATION;
1014 }
1015
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001016 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1017 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1018 return res;
1019}
1020
1021// Pause to reconfigure
1022status_t Camera3Device::internalPauseAndWaitLocked() {
1023 mRequestThread->setPaused(true);
1024 mPauseStateNotify = true;
1025
1026 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1027 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1028 if (res != OK) {
1029 SET_ERR_L("Can't idle device in %f seconds!",
1030 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 }
1032
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 return res;
1034}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036// Resume after internalPauseAndWaitLocked
1037status_t Camera3Device::internalResumeLocked() {
1038 status_t res;
1039
1040 mRequestThread->setPaused(false);
1041
1042 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1043 if (res != OK) {
1044 SET_ERR_L("Can't transition to active in %f seconds!",
1045 kActiveTimeout/1e9);
1046 }
1047 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001049}
1050
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001051status_t Camera3Device::waitUntilStateThenRelock(bool active,
1052 nsecs_t timeout) {
1053 status_t res = OK;
1054 if (active == (mStatus == STATUS_ACTIVE)) {
1055 // Desired state already reached
1056 return res;
1057 }
1058
1059 bool stateSeen = false;
1060 do {
1061 mRecentStatusUpdates.clear();
1062
1063 res = mStatusChanged.waitRelative(mLock, timeout);
1064 if (res != OK) break;
1065
1066 // Check state change history during wait
1067 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1068 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1069 stateSeen = true;
1070 break;
1071 }
1072 }
1073 } while (!stateSeen);
1074
1075 return res;
1076}
1077
1078
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1080 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001081 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001082
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001083 if (listener != NULL && mListener != NULL) {
1084 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1085 }
1086 mListener = listener;
1087
1088 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089}
1090
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001091bool Camera3Device::willNotify3A() {
1092 return false;
1093}
1094
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001096 status_t res;
1097 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001098
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001099 while (mResultQueue.empty()) {
1100 res = mResultSignal.waitRelative(mOutputLock, timeout);
1101 if (res == TIMED_OUT) {
1102 return res;
1103 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001104 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1105 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001106 return res;
1107 }
1108 }
1109 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001110}
1111
1112status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1113 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001114 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001115
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001116 if (mResultQueue.empty()) {
1117 return NOT_ENOUGH_DATA;
1118 }
1119
1120 CameraMetadata &result = *(mResultQueue.begin());
1121 frame->acquire(result);
1122 mResultQueue.erase(mResultQueue.begin());
1123
1124 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001125}
1126
1127status_t Camera3Device::triggerAutofocus(uint32_t id) {
1128 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001129 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001130
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001131 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1132 // Mix-in this trigger into the next request and only the next request.
1133 RequestTrigger trigger[] = {
1134 {
1135 ANDROID_CONTROL_AF_TRIGGER,
1136 ANDROID_CONTROL_AF_TRIGGER_START
1137 },
1138 {
1139 ANDROID_CONTROL_AF_TRIGGER_ID,
1140 static_cast<int32_t>(id)
1141 },
1142 };
1143
1144 return mRequestThread->queueTrigger(trigger,
1145 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001146}
1147
1148status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1149 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001150 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001151
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001152 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1153 // Mix-in this trigger into the next request and only the next request.
1154 RequestTrigger trigger[] = {
1155 {
1156 ANDROID_CONTROL_AF_TRIGGER,
1157 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1158 },
1159 {
1160 ANDROID_CONTROL_AF_TRIGGER_ID,
1161 static_cast<int32_t>(id)
1162 },
1163 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001165 return mRequestThread->queueTrigger(trigger,
1166 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001167}
1168
1169status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1170 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001171 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001172
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001173 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1174 // Mix-in this trigger into the next request and only the next request.
1175 RequestTrigger trigger[] = {
1176 {
1177 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1178 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1179 },
1180 {
1181 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1182 static_cast<int32_t>(id)
1183 },
1184 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001186 return mRequestThread->queueTrigger(trigger,
1187 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001188}
1189
1190status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1191 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1192 ATRACE_CALL();
1193 (void)reprocessStreamId; (void)buffer; (void)listener;
1194
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001195 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196 return INVALID_OPERATION;
1197}
1198
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001199status_t Camera3Device::flush() {
1200 ATRACE_CALL();
1201 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001202 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001203 Mutex::Autolock l(mLock);
1204
1205 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001206 status_t res;
1207 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1208 res = mHal3Device->ops->flush(mHal3Device);
1209 } else {
1210 res = waitUntilDrained();
1211 }
1212
1213 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001214}
1215
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 * Methods called by subclasses
1218 */
1219
1220void Camera3Device::notifyStatus(bool idle) {
1221 {
1222 // Need mLock to safely update state and synchronize to current
1223 // state of methods in flight.
1224 Mutex::Autolock l(mLock);
1225 // We can get various system-idle notices from the status tracker
1226 // while starting up. Only care about them if we've actually sent
1227 // in some requests recently.
1228 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1229 return;
1230 }
1231 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1232 idle ? "idle" : "active");
1233 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1234 mRecentStatusUpdates.add(mStatus);
1235 mStatusChanged.signal();
1236
1237 // Skip notifying listener if we're doing some user-transparent
1238 // state changes
1239 if (mPauseStateNotify) return;
1240 }
1241 NotificationListener *listener;
1242 {
1243 Mutex::Autolock l(mOutputLock);
1244 listener = mListener;
1245 }
1246 if (idle && listener != NULL) {
1247 listener->notifyIdle();
1248 }
1249}
1250
1251/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 * Camera3Device private methods
1253 */
1254
1255sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1256 const CameraMetadata &request) {
1257 ATRACE_CALL();
1258 status_t res;
1259
1260 sp<CaptureRequest> newRequest = new CaptureRequest;
1261 newRequest->mSettings = request;
1262
1263 camera_metadata_entry_t inputStreams =
1264 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1265 if (inputStreams.count > 0) {
1266 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001267 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001268 CLOGE("Request references unknown input stream %d",
1269 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 return NULL;
1271 }
1272 // Lazy completion of stream configuration (allocation/registration)
1273 // on first use
1274 if (mInputStream->isConfiguring()) {
1275 res = mInputStream->finishConfiguration(mHal3Device);
1276 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001279 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 return NULL;
1281 }
1282 }
1283
1284 newRequest->mInputStream = mInputStream;
1285 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1286 }
1287
1288 camera_metadata_entry_t streams =
1289 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1290 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001291 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 return NULL;
1293 }
1294
1295 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001296 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001298 CLOGE("Request references unknown stream %d",
1299 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 return NULL;
1301 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001302 sp<Camera3OutputStreamInterface> stream =
1303 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304
1305 // Lazy completion of stream configuration (allocation/registration)
1306 // on first use
1307 if (stream->isConfiguring()) {
1308 res = stream->finishConfiguration(mHal3Device);
1309 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001310 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1311 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 return NULL;
1313 }
1314 }
1315
1316 newRequest->mOutputStreams.push(stream);
1317 }
1318 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1319
1320 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321}
1322
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323status_t Camera3Device::configureStreamsLocked() {
1324 ATRACE_CALL();
1325 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001326
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001328 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001329 return INVALID_OPERATION;
1330 }
1331
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001332 if (!mNeedConfig) {
1333 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1334 return OK;
1335 }
1336
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001337 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001338 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339
1340 camera3_stream_configuration config;
1341
1342 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1343
1344 Vector<camera3_stream_t*> streams;
1345 streams.setCapacity(config.num_streams);
1346
1347 if (mInputStream != NULL) {
1348 camera3_stream_t *inputStream;
1349 inputStream = mInputStream->startConfiguration();
1350 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001351 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001352 return INVALID_OPERATION;
1353 }
1354 streams.add(inputStream);
1355 }
1356
1357 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001358
1359 // Don't configure bidi streams twice, nor add them twice to the list
1360 if (mOutputStreams[i].get() ==
1361 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1362
1363 config.num_streams--;
1364 continue;
1365 }
1366
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 camera3_stream_t *outputStream;
1368 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1369 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001370 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371 return INVALID_OPERATION;
1372 }
1373 streams.add(outputStream);
1374 }
1375
1376 config.streams = streams.editArray();
1377
1378 // Do the HAL configuration; will potentially touch stream
1379 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001380 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001382 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383
1384 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001385 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1386 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 return res;
1388 }
1389
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001390 // Finish all stream configuration immediately.
1391 // TODO: Try to relax this later back to lazy completion, which should be
1392 // faster
1393
Igor Murashkin073f8572013-05-02 14:59:28 -07001394 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001395 res = mInputStream->finishConfiguration(mHal3Device);
1396 if (res != OK) {
1397 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1398 mInputStream->getId(), strerror(-res), res);
1399 return res;
1400 }
1401 }
1402
1403 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001404 sp<Camera3OutputStreamInterface> outputStream =
1405 mOutputStreams.editValueAt(i);
1406 if (outputStream->isConfiguring()) {
1407 res = outputStream->finishConfiguration(mHal3Device);
1408 if (res != OK) {
1409 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1410 outputStream->getId(), strerror(-res), res);
1411 return res;
1412 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001413 }
1414 }
1415
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 // Request thread needs to know to avoid using repeat-last-settings protocol
1417 // across configure_streams() calls
1418 mRequestThread->configurationComplete();
1419
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001420 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001422 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001424 if (config.num_streams > 0) {
1425 mStatus = STATUS_CONFIGURED;
1426 } else {
1427 mStatus = STATUS_UNCONFIGURED;
1428 }
1429
1430 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1431
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001432 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001433}
1434
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001435void Camera3Device::setErrorState(const char *fmt, ...) {
1436 Mutex::Autolock l(mLock);
1437 va_list args;
1438 va_start(args, fmt);
1439
1440 setErrorStateLockedV(fmt, args);
1441
1442 va_end(args);
1443}
1444
1445void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1446 Mutex::Autolock l(mLock);
1447 setErrorStateLockedV(fmt, args);
1448}
1449
1450void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1451 va_list args;
1452 va_start(args, fmt);
1453
1454 setErrorStateLockedV(fmt, args);
1455
1456 va_end(args);
1457}
1458
1459void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001460 // Print out all error messages to log
1461 String8 errorCause = String8::formatV(fmt, args);
1462 ALOGE("Camera %d: %s", mId, errorCause.string());
1463
1464 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001465 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001466
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001467 // Save stack trace. View by dumping it later.
1468 CameraTraces::saveTrace();
1469 // TODO: consider adding errorCause and client pid/procname
1470
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001471 mErrorCause = errorCause;
1472
1473 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001474 mStatus = STATUS_ERROR;
1475}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001476
1477/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001478 * In-flight request management
1479 */
1480
1481status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001482 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001483 ATRACE_CALL();
1484 Mutex::Autolock l(mInFlightLock);
1485
1486 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001487 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001488 if (res < 0) return res;
1489
1490 return OK;
1491}
1492
1493/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001494 * QUIRK(partial results)
1495 * Check if all 3A fields are ready, and send off a partial 3A-only result
1496 * to the output frame queue
1497 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001498bool Camera3Device::processPartial3AQuirk(
1499 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001500 const CameraMetadata& partial) {
1501
1502 // Check if all 3A states are present
1503 // The full list of fields is
1504 // android.control.afMode
1505 // android.control.awbMode
1506 // android.control.aeState
1507 // android.control.awbState
1508 // android.control.afState
1509 // android.control.afTriggerID
1510 // android.control.aePrecaptureID
1511 // TODO: Add android.control.aeMode
1512
1513 bool gotAllStates = true;
1514
1515 uint8_t afMode;
1516 uint8_t awbMode;
1517 uint8_t aeState;
1518 uint8_t afState;
1519 uint8_t awbState;
1520 int32_t afTriggerId;
1521 int32_t aeTriggerId;
1522
1523 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1524 &afMode, frameNumber);
1525
1526 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1527 &awbMode, frameNumber);
1528
1529 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1530 &aeState, frameNumber);
1531
1532 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1533 &afState, frameNumber);
1534
1535 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1536 &awbState, frameNumber);
1537
1538 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1539 &afTriggerId, frameNumber);
1540
1541 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1542 &aeTriggerId, frameNumber);
1543
1544 if (!gotAllStates) return false;
1545
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001546 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001547 "AF state %d, AE state %d, AWB state %d, "
1548 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001549 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001550 afMode, awbMode,
1551 afState, aeState, awbState,
1552 afTriggerId, aeTriggerId);
1553
1554 // Got all states, so construct a minimal result to send
1555 // In addition to the above fields, this means adding in
1556 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001557 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001558 // android.quirks.partialResult
1559
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001560 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001561
1562 Mutex::Autolock l(mOutputLock);
1563
1564 CameraMetadata& min3AResult =
1565 *mResultQueue.insert(
1566 mResultQueue.end(),
1567 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1568
1569 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1570 &frameNumber, frameNumber)) {
1571 return false;
1572 }
1573
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001574 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1575 &requestId, frameNumber)) {
1576 return false;
1577 }
1578
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001579 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1580 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1581 &partialResult, frameNumber)) {
1582 return false;
1583 }
1584
1585 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1586 &afMode, frameNumber)) {
1587 return false;
1588 }
1589
1590 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1591 &awbMode, frameNumber)) {
1592 return false;
1593 }
1594
1595 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1596 &aeState, frameNumber)) {
1597 return false;
1598 }
1599
1600 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1601 &afState, frameNumber)) {
1602 return false;
1603 }
1604
1605 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1606 &awbState, frameNumber)) {
1607 return false;
1608 }
1609
1610 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1611 &afTriggerId, frameNumber)) {
1612 return false;
1613 }
1614
1615 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1616 &aeTriggerId, frameNumber)) {
1617 return false;
1618 }
1619
1620 mResultSignal.signal();
1621
1622 return true;
1623}
1624
1625template<typename T>
1626bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1627 T* value, int32_t frameNumber) {
1628 (void) frameNumber;
1629
1630 camera_metadata_ro_entry_t entry;
1631
1632 entry = result.find(tag);
1633 if (entry.count == 0) {
1634 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1635 mId, frameNumber, get_camera_metadata_tag_name(tag));
1636 return false;
1637 }
1638
1639 if (sizeof(T) == sizeof(uint8_t)) {
1640 *value = entry.data.u8[0];
1641 } else if (sizeof(T) == sizeof(int32_t)) {
1642 *value = entry.data.i32[0];
1643 } else {
1644 ALOGE("%s: Unexpected type", __FUNCTION__);
1645 return false;
1646 }
1647 return true;
1648}
1649
1650template<typename T>
1651bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1652 const T* value, int32_t frameNumber) {
1653 if (result.update(tag, value, 1) != NO_ERROR) {
1654 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1655 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1656 frameNumber, get_camera_metadata_tag_name(tag));
1657 return false;
1658 }
1659 return true;
1660}
1661
1662/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001663 * Camera HAL device callback methods
1664 */
1665
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001666void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001667 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001668
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001669 status_t res;
1670
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001671 uint32_t frameNumber = result->frame_number;
1672 if (result->result == NULL && result->num_output_buffers == 0) {
1673 SET_ERR("No result data provided by HAL for frame %d",
1674 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001675 return;
1676 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001677 bool partialResultQuirk = false;
1678 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001679
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001680 // Get capture timestamp from list of in-flight requests, where it was added
1681 // by the shutter notification for this frame. Then update the in-flight
1682 // status and remove the in-flight entry if all result data has been
1683 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001684 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001685 {
1686 Mutex::Autolock l(mInFlightLock);
1687 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1688 if (idx == NAME_NOT_FOUND) {
1689 SET_ERR("Unknown frame number for capture result: %d",
1690 frameNumber);
1691 return;
1692 }
1693 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001694
1695 // Check if this result carries only partial metadata
1696 if (mUsePartialResultQuirk && result->result != NULL) {
1697 camera_metadata_ro_entry_t partialResultEntry;
1698 res = find_camera_metadata_ro_entry(result->result,
1699 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1700 if (res != NAME_NOT_FOUND &&
1701 partialResultEntry.count > 0 &&
1702 partialResultEntry.data.u8[0] ==
1703 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1704 // A partial result. Flag this as such, and collect this
1705 // set of metadata into the in-flight entry.
1706 partialResultQuirk = true;
1707 request.partialResultQuirk.collectedResult.append(
1708 result->result);
1709 request.partialResultQuirk.collectedResult.erase(
1710 ANDROID_QUIRKS_PARTIAL_RESULT);
1711 // Fire off a 3A-only result if possible
1712 if (!request.partialResultQuirk.haveSent3A) {
1713 request.partialResultQuirk.haveSent3A =
1714 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001715 request.requestId,
1716 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001717 }
1718 }
1719 }
1720
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001721 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001722 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001723 * One of the following must happen before it's legal to call process_capture_result,
1724 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001725 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1726 * - CAMERA3_MSG_ERROR (expected during flush)
1727 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001728 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001729 SET_ERR("Called before shutter notify for frame %d",
1730 frameNumber);
1731 return;
1732 }
1733
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001734 // Did we get the (final) result metadata for this capture?
1735 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001736 if (request.haveResultMetadata) {
1737 SET_ERR("Called multiple times with metadata for frame %d",
1738 frameNumber);
1739 return;
1740 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001741 if (mUsePartialResultQuirk &&
1742 !request.partialResultQuirk.collectedResult.isEmpty()) {
1743 collectedQuirkResult.acquire(
1744 request.partialResultQuirk.collectedResult);
1745 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001746 request.haveResultMetadata = true;
1747 }
1748
1749 request.numBuffersLeft -= result->num_output_buffers;
1750
1751 if (request.numBuffersLeft < 0) {
1752 SET_ERR("Too many buffers returned for frame %d",
1753 frameNumber);
1754 return;
1755 }
1756
Zhijun He1b05dfc2013-11-21 12:57:51 -08001757 // Check if everything has arrived for this result (buffers and metadata), remove it from
1758 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1759 if ((request.requestStatus != OK) ||
1760 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001761 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001762 mInFlightMap.removeItemsAt(idx, 1);
1763 }
1764
1765 // Sanity check - if we have too many in-flight frames, something has
1766 // likely gone wrong
1767 if (mInFlightMap.size() > kInFlightWarnLimit) {
1768 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1769 }
1770
1771 }
1772
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001773 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001774 bool gotResult = false;
1775 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001776 Mutex::Autolock l(mOutputLock);
1777
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001778 gotResult = true;
1779
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001780 if (frameNumber != mNextResultFrameNumber) {
1781 SET_ERR("Out-of-order capture result metadata submitted! "
1782 "(got frame number %d, expecting %d)",
1783 frameNumber, mNextResultFrameNumber);
1784 return;
1785 }
1786 mNextResultFrameNumber++;
1787
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001788 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001789 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001790
Igor Murashkind2c90692013-04-02 12:32:32 -07001791 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001792 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001793 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001794 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001795 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001796 } else {
1797 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001798 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001799 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001800
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001801 // Append any previous partials to form a complete result
1802 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1803 captureResult.append(collectedQuirkResult);
1804 }
1805
1806 captureResult.sort();
1807
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001808 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001809
1810 camera_metadata_entry entry =
1811 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1812 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001813 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001814 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001815 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001816 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001817 SET_ERR("Timestamp mismatch between shutter notify and result"
1818 " metadata for frame %d (%lld vs %lld respectively)",
1819 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001820 gotResult = false;
1821 }
1822
1823 if (gotResult) {
1824 // Valid result, insert into queue
1825 CameraMetadata& queuedResult =
1826 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1827 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001828 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001829 } // scope for mOutputLock
1830
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001831 // Return completed buffers to their streams with the timestamp
1832
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001833 for (size_t i = 0; i < result->num_output_buffers; i++) {
1834 Camera3Stream *stream =
1835 Camera3Stream::cast(result->output_buffers[i].stream);
1836 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1837 // Note: stream may be deallocated at this point, if this buffer was the
1838 // last reference to it.
1839 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001840 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001841 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001842 }
1843 }
1844
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001845 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001846
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001847 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001848 mResultSignal.signal();
1849 }
1850
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001851}
1852
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001853
1854
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001855void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001856 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001857 NotificationListener *listener;
1858 {
1859 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001860 listener = mListener;
1861 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001862
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001863 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001864 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001865 return;
1866 }
1867
1868 switch (msg->type) {
1869 case CAMERA3_MSG_ERROR: {
1870 int streamId = 0;
1871 if (msg->message.error.error_stream != NULL) {
1872 Camera3Stream *stream =
1873 Camera3Stream::cast(
1874 msg->message.error.error_stream);
1875 streamId = stream->getId();
1876 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001877 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1878 mId, __FUNCTION__, msg->message.error.frame_number,
1879 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001880
1881 // Set request error status for the request in the in-flight tracking
1882 {
1883 Mutex::Autolock l(mInFlightLock);
1884 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1885 if (idx >= 0) {
1886 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1887 }
1888 }
1889
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001890 if (listener != NULL) {
1891 listener->notifyError(msg->message.error.error_code,
1892 msg->message.error.frame_number, streamId);
1893 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001894 break;
1895 }
1896 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001897 ssize_t idx;
1898 uint32_t frameNumber = msg->message.shutter.frame_number;
1899 nsecs_t timestamp = msg->message.shutter.timestamp;
1900 // Verify ordering of shutter notifications
1901 {
1902 Mutex::Autolock l(mOutputLock);
1903 if (frameNumber != mNextShutterFrameNumber) {
1904 SET_ERR("Shutter notification out-of-order. Expected "
1905 "notification for frame %d, got frame %d",
1906 mNextShutterFrameNumber, frameNumber);
1907 break;
1908 }
1909 mNextShutterFrameNumber++;
1910 }
1911
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001912 int32_t requestId = -1;
1913
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001914 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001915 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001916 {
1917 Mutex::Autolock l(mInFlightLock);
1918 idx = mInFlightMap.indexOfKey(frameNumber);
1919 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001920 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1921 r.captureTimestamp = timestamp;
1922 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001923 }
1924 }
1925 if (idx < 0) {
1926 SET_ERR("Shutter notification for non-existent frame number %d",
1927 frameNumber);
1928 break;
1929 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001930 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1931 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001932 // Call listener, if any
1933 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001934 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001935 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001936 break;
1937 }
1938 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001939 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001940 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001941 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001942}
1943
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001944CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001945 ALOGV("%s", __FUNCTION__);
1946
Igor Murashkin1e479c02013-09-06 16:55:14 -07001947 CameraMetadata retVal;
1948
1949 if (mRequestThread != NULL) {
1950 retVal = mRequestThread->getLatestRequest();
1951 }
1952
Igor Murashkin1e479c02013-09-06 16:55:14 -07001953 return retVal;
1954}
1955
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001956/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001957 * RequestThread inner class methods
1958 */
1959
1960Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001961 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001962 camera3_device_t *hal3Device) :
1963 Thread(false),
1964 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001965 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001966 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001967 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001968 mReconfigured(false),
1969 mDoPause(false),
1970 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001971 mFrameNumber(0),
1972 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001973 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001974}
1975
1976void Camera3Device::RequestThread::configurationComplete() {
1977 Mutex::Autolock l(mRequestLock);
1978 mReconfigured = true;
1979}
1980
1981status_t Camera3Device::RequestThread::queueRequest(
1982 sp<CaptureRequest> request) {
1983 Mutex::Autolock l(mRequestLock);
1984 mRequestQueue.push_back(request);
1985
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001986 unpauseForNewRequests();
1987
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001988 return OK;
1989}
1990
Jianing Wei90e59c92014-03-12 18:29:36 -07001991status_t Camera3Device::RequestThread::queueRequestList(
1992 List<sp<CaptureRequest> > &requests) {
1993 Mutex::Autolock l(mRequestLock);
1994 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
1995 ++it) {
1996 mRequestQueue.push_back(*it);
1997 }
1998
1999 unpauseForNewRequests();
2000
2001 return OK;
2002}
2003
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002004
2005status_t Camera3Device::RequestThread::queueTrigger(
2006 RequestTrigger trigger[],
2007 size_t count) {
2008
2009 Mutex::Autolock l(mTriggerMutex);
2010 status_t ret;
2011
2012 for (size_t i = 0; i < count; ++i) {
2013 ret = queueTriggerLocked(trigger[i]);
2014
2015 if (ret != OK) {
2016 return ret;
2017 }
2018 }
2019
2020 return OK;
2021}
2022
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002023int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2024 sp<Camera3Device> d = device.promote();
2025 if (d != NULL) return d->mId;
2026 return 0;
2027}
2028
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002029status_t Camera3Device::RequestThread::queueTriggerLocked(
2030 RequestTrigger trigger) {
2031
2032 uint32_t tag = trigger.metadataTag;
2033 ssize_t index = mTriggerMap.indexOfKey(tag);
2034
2035 switch (trigger.getTagType()) {
2036 case TYPE_BYTE:
2037 // fall-through
2038 case TYPE_INT32:
2039 break;
2040 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002041 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2042 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002043 return INVALID_OPERATION;
2044 }
2045
2046 /**
2047 * Collect only the latest trigger, since we only have 1 field
2048 * in the request settings per trigger tag, and can't send more than 1
2049 * trigger per request.
2050 */
2051 if (index != NAME_NOT_FOUND) {
2052 mTriggerMap.editValueAt(index) = trigger;
2053 } else {
2054 mTriggerMap.add(tag, trigger);
2055 }
2056
2057 return OK;
2058}
2059
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002060status_t Camera3Device::RequestThread::setRepeatingRequests(
2061 const RequestList &requests) {
2062 Mutex::Autolock l(mRequestLock);
2063 mRepeatingRequests.clear();
2064 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2065 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002066
2067 unpauseForNewRequests();
2068
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 return OK;
2070}
2071
2072status_t Camera3Device::RequestThread::clearRepeatingRequests() {
2073 Mutex::Autolock l(mRequestLock);
2074 mRepeatingRequests.clear();
2075 return OK;
2076}
2077
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002078status_t Camera3Device::RequestThread::clear() {
2079 Mutex::Autolock l(mRequestLock);
2080 mRepeatingRequests.clear();
2081 mRequestQueue.clear();
2082 mTriggerMap.clear();
2083 return OK;
2084}
2085
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002086void Camera3Device::RequestThread::setPaused(bool paused) {
2087 Mutex::Autolock l(mPauseLock);
2088 mDoPause = paused;
2089 mDoPauseSignal.signal();
2090}
2091
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002092status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2093 int32_t requestId, nsecs_t timeout) {
2094 Mutex::Autolock l(mLatestRequestMutex);
2095 status_t res;
2096 while (mLatestRequestId != requestId) {
2097 nsecs_t startTime = systemTime();
2098
2099 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2100 if (res != OK) return res;
2101
2102 timeout -= (systemTime() - startTime);
2103 }
2104
2105 return OK;
2106}
2107
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002108void Camera3Device::RequestThread::requestExit() {
2109 // Call parent to set up shutdown
2110 Thread::requestExit();
2111 // The exit from any possible waits
2112 mDoPauseSignal.signal();
2113 mRequestSignal.signal();
2114}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002115
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002116bool Camera3Device::RequestThread::threadLoop() {
2117
2118 status_t res;
2119
2120 // Handle paused state.
2121 if (waitIfPaused()) {
2122 return true;
2123 }
2124
2125 // Get work to do
2126
2127 sp<CaptureRequest> nextRequest = waitForNextRequest();
2128 if (nextRequest == NULL) {
2129 return true;
2130 }
2131
2132 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002133 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002134 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002136 // Get the request ID, if any
2137 int requestId;
2138 camera_metadata_entry_t requestIdEntry =
2139 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2140 if (requestIdEntry.count > 0) {
2141 requestId = requestIdEntry.data.i32[0];
2142 } else {
2143 ALOGW("%s: Did not have android.request.id set in the request",
2144 __FUNCTION__);
2145 requestId = NAME_NOT_FOUND;
2146 }
2147
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002148 // Insert any queued triggers (before metadata is locked)
2149 int32_t triggerCount;
2150 res = insertTriggers(nextRequest);
2151 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002152 SET_ERR("RequestThread: Unable to insert triggers "
2153 "(capture request %d, HAL device: %s (%d)",
2154 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002155 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2156 return false;
2157 }
2158 triggerCount = res;
2159
2160 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2161
2162 // If the request is the same as last, or we had triggers last time
2163 if (mPrevRequest != nextRequest || triggersMixedIn) {
2164 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002165 * HAL workaround:
2166 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2167 */
2168 res = addDummyTriggerIds(nextRequest);
2169 if (res != OK) {
2170 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2171 "(capture request %d, HAL device: %s (%d)",
2172 (mFrameNumber+1), strerror(-res), res);
2173 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2174 return false;
2175 }
2176
2177 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002178 * The request should be presorted so accesses in HAL
2179 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2180 */
2181 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002182 request.settings = nextRequest->mSettings.getAndLock();
2183 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002184 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2185
2186 IF_ALOGV() {
2187 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2188 find_camera_metadata_ro_entry(
2189 request.settings,
2190 ANDROID_CONTROL_AF_TRIGGER,
2191 &e
2192 );
2193 if (e.count > 0) {
2194 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2195 __FUNCTION__,
2196 mFrameNumber+1,
2197 e.data.u8[0]);
2198 }
2199 }
2200 } else {
2201 // leave request.settings NULL to indicate 'reuse latest given'
2202 ALOGVV("%s: Request settings are REUSED",
2203 __FUNCTION__);
2204 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002205
2206 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002207
2208 // Fill in buffers
2209
2210 if (nextRequest->mInputStream != NULL) {
2211 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002212 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002214 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002215 " %s (%d)", strerror(-res), res);
2216 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2217 return true;
2218 }
2219 } else {
2220 request.input_buffer = NULL;
2221 }
2222
2223 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2224 nextRequest->mOutputStreams.size());
2225 request.output_buffers = outputBuffers.array();
2226 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2227 res = nextRequest->mOutputStreams.editItemAt(i)->
2228 getBuffer(&outputBuffers.editItemAt(i));
2229 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002230 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2231 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002232 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2233 return true;
2234 }
2235 request.num_output_buffers++;
2236 }
2237
2238 request.frame_number = mFrameNumber++;
2239
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002240 // Log request in the in-flight queue
2241 sp<Camera3Device> parent = mParent.promote();
2242 if (parent == NULL) {
2243 CLOGE("RequestThread: Parent is gone");
2244 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2245 return false;
2246 }
2247
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002248 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002249 request.num_output_buffers);
2250 if (res != OK) {
2251 SET_ERR("RequestThread: Unable to register new in-flight request:"
2252 " %s (%d)", strerror(-res), res);
2253 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2254 return false;
2255 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002256
Zhijun Hecc27e112013-10-03 16:12:43 -07002257 // Inform waitUntilRequestProcessed thread of a new request ID
2258 {
2259 Mutex::Autolock al(mLatestRequestMutex);
2260
2261 mLatestRequestId = requestId;
2262 mLatestRequestSignal.signal();
2263 }
2264
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002265 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002266 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2267 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002268 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002269 ATRACE_END();
2270
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002271 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002272 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002273 " device: %s (%d)", request.frame_number, strerror(-res), res);
2274 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2275 return false;
2276 }
2277
Igor Murashkin1e479c02013-09-06 16:55:14 -07002278 // Update the latest request sent to HAL
2279 if (request.settings != NULL) { // Don't update them if they were unchanged
2280 Mutex::Autolock al(mLatestRequestMutex);
2281
2282 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2283 mLatestRequest.acquire(cloned);
2284 }
2285
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002286 if (request.settings != NULL) {
2287 nextRequest->mSettings.unlock(request.settings);
2288 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002289
2290 // Remove any previously queued triggers (after unlock)
2291 res = removeTriggers(mPrevRequest);
2292 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002293 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002294 "(capture request %d, HAL device: %s (%d)",
2295 request.frame_number, strerror(-res), res);
2296 return false;
2297 }
2298 mPrevTriggers = triggerCount;
2299
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002300 // Return input buffer back to framework
2301 if (request.input_buffer != NULL) {
2302 Camera3Stream *stream =
2303 Camera3Stream::cast(request.input_buffer->stream);
2304 res = stream->returnInputBuffer(*(request.input_buffer));
2305 // Note: stream may be deallocated at this point, if this buffer was the
2306 // last reference to it.
2307 if (res != OK) {
2308 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2309 " its stream:%s (%d)", __FUNCTION__,
2310 request.frame_number, strerror(-res), res);
2311 // TODO: Report error upstream
2312 }
2313 }
2314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002315 return true;
2316}
2317
Igor Murashkin1e479c02013-09-06 16:55:14 -07002318CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2319 Mutex::Autolock al(mLatestRequestMutex);
2320
2321 ALOGV("RequestThread::%s", __FUNCTION__);
2322
2323 return mLatestRequest;
2324}
2325
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002326void Camera3Device::RequestThread::cleanUpFailedRequest(
2327 camera3_capture_request_t &request,
2328 sp<CaptureRequest> &nextRequest,
2329 Vector<camera3_stream_buffer_t> &outputBuffers) {
2330
2331 if (request.settings != NULL) {
2332 nextRequest->mSettings.unlock(request.settings);
2333 }
2334 if (request.input_buffer != NULL) {
2335 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002336 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002337 }
2338 for (size_t i = 0; i < request.num_output_buffers; i++) {
2339 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2340 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2341 outputBuffers[i], 0);
2342 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002343}
2344
2345sp<Camera3Device::CaptureRequest>
2346 Camera3Device::RequestThread::waitForNextRequest() {
2347 status_t res;
2348 sp<CaptureRequest> nextRequest;
2349
2350 // Optimized a bit for the simple steady-state case (single repeating
2351 // request), to avoid putting that request in the queue temporarily.
2352 Mutex::Autolock l(mRequestLock);
2353
2354 while (mRequestQueue.empty()) {
2355 if (!mRepeatingRequests.empty()) {
2356 // Always atomically enqueue all requests in a repeating request
2357 // list. Guarantees a complete in-sequence set of captures to
2358 // application.
2359 const RequestList &requests = mRepeatingRequests;
2360 RequestList::const_iterator firstRequest =
2361 requests.begin();
2362 nextRequest = *firstRequest;
2363 mRequestQueue.insert(mRequestQueue.end(),
2364 ++firstRequest,
2365 requests.end());
2366 // No need to wait any longer
2367 break;
2368 }
2369
2370 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2371
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002372 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2373 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002374 Mutex::Autolock pl(mPauseLock);
2375 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002376 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002378 // Let the tracker know
2379 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2380 if (statusTracker != 0) {
2381 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2382 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 }
2384 // Stop waiting for now and let thread management happen
2385 return NULL;
2386 }
2387 }
2388
2389 if (nextRequest == NULL) {
2390 // Don't have a repeating request already in hand, so queue
2391 // must have an entry now.
2392 RequestList::iterator firstRequest =
2393 mRequestQueue.begin();
2394 nextRequest = *firstRequest;
2395 mRequestQueue.erase(firstRequest);
2396 }
2397
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002398 // In case we've been unpaused by setPaused clearing mDoPause, need to
2399 // update internal pause state (capture/setRepeatingRequest unpause
2400 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002401 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002402 if (mPaused) {
2403 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2404 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2405 if (statusTracker != 0) {
2406 statusTracker->markComponentActive(mStatusId);
2407 }
2408 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 mPaused = false;
2410
2411 // Check if we've reconfigured since last time, and reset the preview
2412 // request if so. Can't use 'NULL request == repeat' across configure calls.
2413 if (mReconfigured) {
2414 mPrevRequest.clear();
2415 mReconfigured = false;
2416 }
2417
2418 return nextRequest;
2419}
2420
2421bool Camera3Device::RequestThread::waitIfPaused() {
2422 status_t res;
2423 Mutex::Autolock l(mPauseLock);
2424 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002425 if (mPaused == false) {
2426 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002427 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2428 // Let the tracker know
2429 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2430 if (statusTracker != 0) {
2431 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2432 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002433 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002434
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002435 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002436 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002437 return true;
2438 }
2439 }
2440 // We don't set mPaused to false here, because waitForNextRequest needs
2441 // to further manage the paused state in case of starvation.
2442 return false;
2443}
2444
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002445void Camera3Device::RequestThread::unpauseForNewRequests() {
2446 // With work to do, mark thread as unpaused.
2447 // If paused by request (setPaused), don't resume, to avoid
2448 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002449 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002450 Mutex::Autolock p(mPauseLock);
2451 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002452 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2453 if (mPaused) {
2454 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2455 if (statusTracker != 0) {
2456 statusTracker->markComponentActive(mStatusId);
2457 }
2458 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002459 mPaused = false;
2460 }
2461}
2462
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002463void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2464 sp<Camera3Device> parent = mParent.promote();
2465 if (parent != NULL) {
2466 va_list args;
2467 va_start(args, fmt);
2468
2469 parent->setErrorStateV(fmt, args);
2470
2471 va_end(args);
2472 }
2473}
2474
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002475status_t Camera3Device::RequestThread::insertTriggers(
2476 const sp<CaptureRequest> &request) {
2477
2478 Mutex::Autolock al(mTriggerMutex);
2479
2480 CameraMetadata &metadata = request->mSettings;
2481 size_t count = mTriggerMap.size();
2482
2483 for (size_t i = 0; i < count; ++i) {
2484 RequestTrigger trigger = mTriggerMap.valueAt(i);
2485
2486 uint32_t tag = trigger.metadataTag;
2487 camera_metadata_entry entry = metadata.find(tag);
2488
2489 if (entry.count > 0) {
2490 /**
2491 * Already has an entry for this trigger in the request.
2492 * Rewrite it with our requested trigger value.
2493 */
2494 RequestTrigger oldTrigger = trigger;
2495
2496 oldTrigger.entryValue = entry.data.u8[0];
2497
2498 mTriggerReplacedMap.add(tag, oldTrigger);
2499 } else {
2500 /**
2501 * More typical, no trigger entry, so we just add it
2502 */
2503 mTriggerRemovedMap.add(tag, trigger);
2504 }
2505
2506 status_t res;
2507
2508 switch (trigger.getTagType()) {
2509 case TYPE_BYTE: {
2510 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2511 res = metadata.update(tag,
2512 &entryValue,
2513 /*count*/1);
2514 break;
2515 }
2516 case TYPE_INT32:
2517 res = metadata.update(tag,
2518 &trigger.entryValue,
2519 /*count*/1);
2520 break;
2521 default:
2522 ALOGE("%s: Type not supported: 0x%x",
2523 __FUNCTION__,
2524 trigger.getTagType());
2525 return INVALID_OPERATION;
2526 }
2527
2528 if (res != OK) {
2529 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2530 ", value %d", __FUNCTION__, trigger.getTagName(),
2531 trigger.entryValue);
2532 return res;
2533 }
2534
2535 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2536 trigger.getTagName(),
2537 trigger.entryValue);
2538 }
2539
2540 mTriggerMap.clear();
2541
2542 return count;
2543}
2544
2545status_t Camera3Device::RequestThread::removeTriggers(
2546 const sp<CaptureRequest> &request) {
2547 Mutex::Autolock al(mTriggerMutex);
2548
2549 CameraMetadata &metadata = request->mSettings;
2550
2551 /**
2552 * Replace all old entries with their old values.
2553 */
2554 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2555 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2556
2557 status_t res;
2558
2559 uint32_t tag = trigger.metadataTag;
2560 switch (trigger.getTagType()) {
2561 case TYPE_BYTE: {
2562 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2563 res = metadata.update(tag,
2564 &entryValue,
2565 /*count*/1);
2566 break;
2567 }
2568 case TYPE_INT32:
2569 res = metadata.update(tag,
2570 &trigger.entryValue,
2571 /*count*/1);
2572 break;
2573 default:
2574 ALOGE("%s: Type not supported: 0x%x",
2575 __FUNCTION__,
2576 trigger.getTagType());
2577 return INVALID_OPERATION;
2578 }
2579
2580 if (res != OK) {
2581 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2582 ", trigger value %d", __FUNCTION__,
2583 trigger.getTagName(), trigger.entryValue);
2584 return res;
2585 }
2586 }
2587 mTriggerReplacedMap.clear();
2588
2589 /**
2590 * Remove all new entries.
2591 */
2592 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2593 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2594 status_t res = metadata.erase(trigger.metadataTag);
2595
2596 if (res != OK) {
2597 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2598 ", trigger value %d", __FUNCTION__,
2599 trigger.getTagName(), trigger.entryValue);
2600 return res;
2601 }
2602 }
2603 mTriggerRemovedMap.clear();
2604
2605 return OK;
2606}
2607
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002608status_t Camera3Device::RequestThread::addDummyTriggerIds(
2609 const sp<CaptureRequest> &request) {
2610 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2611 static const int32_t dummyTriggerId = 1;
2612 status_t res;
2613
2614 CameraMetadata &metadata = request->mSettings;
2615
2616 // If AF trigger is active, insert a dummy AF trigger ID if none already
2617 // exists
2618 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2619 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2620 if (afTrigger.count > 0 &&
2621 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2622 afId.count == 0) {
2623 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2624 if (res != OK) return res;
2625 }
2626
2627 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2628 // if none already exists
2629 camera_metadata_entry pcTrigger =
2630 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2631 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2632 if (pcTrigger.count > 0 &&
2633 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2634 pcId.count == 0) {
2635 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2636 &dummyTriggerId, 1);
2637 if (res != OK) return res;
2638 }
2639
2640 return OK;
2641}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002642
2643
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002644/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002645 * Static callback forwarding methods from HAL to instance
2646 */
2647
2648void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2649 const camera3_capture_result *result) {
2650 Camera3Device *d =
2651 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2652 d->processCaptureResult(result);
2653}
2654
2655void Camera3Device::sNotify(const camera3_callback_ops *cb,
2656 const camera3_notify_msg *msg) {
2657 Camera3Device *d =
2658 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2659 d->notify(msg);
2660}
2661
2662}; // namespace android