blob: f586e751bc1face6c4e83979c50fc9275564ea50 [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
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080051
52using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053
54namespace android {
55
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056Camera3Device::Camera3Device(int id):
57 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080058 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070059 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070060 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070061 mNextResultFrameNumber(0),
62 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070063 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064{
65 ATRACE_CALL();
66 camera3_callback_ops::notify = &sNotify;
67 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
68 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
69}
70
71Camera3Device::~Camera3Device()
72{
73 ATRACE_CALL();
74 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
75 disconnect();
76}
77
Igor Murashkin71381052013-03-04 14:53:08 -080078int Camera3Device::getId() const {
79 return mId;
80}
81
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080082/**
83 * CameraDeviceBase interface
84 */
85
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086status_t Camera3Device::initialize(camera_module_t *module)
87{
88 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070089 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080090 Mutex::Autolock l(mLock);
91
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070094 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 return INVALID_OPERATION;
96 }
97
98 /** Open HAL device */
99
100 status_t res;
101 String8 deviceName = String8::format("%d", mId);
102
103 camera3_device_t *device;
104
Zhijun He213ce792013-11-19 08:45:15 -0800105 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 res = module->common.methods->open(&module->common, deviceName.string(),
107 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800108 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109
110 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 return res;
113 }
114
115 /** Cross-check device version */
116
Zhijun He95dd5ba2014-03-26 18:18:00 -0700117 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700119 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700120 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121 device->common.version);
122 device->common.close(&device->common);
123 return BAD_VALUE;
124 }
125
126 camera_info info;
127 res = module->get_camera_info(mId, &info);
128 if (res != OK) return res;
129
130 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
132 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700133 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 device->common.close(&device->common);
135 return BAD_VALUE;
136 }
137
138 /** Initialize device with callback functions */
139
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700142 ATRACE_END();
143
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700145 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
146 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 device->common.close(&device->common);
148 return BAD_VALUE;
149 }
150
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700151 /** Start up status tracker thread */
152 mStatusTracker = new StatusTracker(this);
153 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
154 if (res != OK) {
155 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
156 strerror(-res), res);
157 device->common.close(&device->common);
158 mStatusTracker.clear();
159 return res;
160 }
161
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 /** Start up request queue thread */
163
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700164 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800165 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700167 SET_ERR_L("Unable to start request queue thread: %s (%d)",
168 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 return res;
172 }
173
174 /** Everything is good to go */
175
176 mDeviceInfo = info.static_camera_characteristics;
177 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700178 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800179 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700180 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700181 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700183 /** Check for quirks */
184
185 // Will the HAL be sending in early partial result metadata?
186 camera_metadata_entry partialResultsQuirk =
187 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
188 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
189 mUsePartialResultQuirk = true;
190 }
191
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return OK;
193}
194
195status_t Camera3Device::disconnect() {
196 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700197 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800199 ALOGV("%s: E", __FUNCTION__);
200
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700201 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800202
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700203 {
204 Mutex::Autolock l(mLock);
205 if (mStatus == STATUS_UNINITIALIZED) return res;
206
207 if (mStatus == STATUS_ACTIVE ||
208 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
209 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700210 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700212 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 } else {
214 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
215 if (res != OK) {
216 SET_ERR_L("Timeout waiting for HAL to drain");
217 // Continue to close device even in case of error
218 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700219 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800220 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800221
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700222 if (mStatus == STATUS_ERROR) {
223 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700224 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225
226 if (mStatusTracker != NULL) {
227 mStatusTracker->requestExit();
228 }
229
230 if (mRequestThread != NULL) {
231 mRequestThread->requestExit();
232 }
233
234 mOutputStreams.clear();
235 mInputStream.clear();
236 }
237
238 // Joining done without holding mLock, otherwise deadlocks may ensue
239 // as the threads try to access parent state
240 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
241 // HAL may be in a bad state, so waiting for request thread
242 // (which may be stuck in the HAL processCaptureRequest call)
243 // could be dangerous.
244 mRequestThread->join();
245 }
246
247 if (mStatusTracker != NULL) {
248 mStatusTracker->join();
249 }
250
251 {
252 Mutex::Autolock l(mLock);
253
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800256
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800258 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800260 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 mHal3Device = NULL;
262 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 mStatus = STATUS_UNINITIALIZED;
265 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266
267 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700268 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800269}
270
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271// For dumping/debugging only -
272// try to acquire a lock a few times, eventually give up to proceed with
273// debug/dump operations
274bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
275 bool gotLock = false;
276 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
277 if (lock.tryLock() == NO_ERROR) {
278 gotLock = true;
279 break;
280 } else {
281 usleep(kDumpSleepDuration);
282 }
283 }
284 return gotLock;
285}
286
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800287status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
288 ATRACE_CALL();
289 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700290
291 // Try to lock, but continue in case of failure (to avoid blocking in
292 // deadlocks)
293 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
294 bool gotLock = tryLockSpinRightRound(mLock);
295
296 ALOGW_IF(!gotInterfaceLock,
297 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
298 mId, __FUNCTION__);
299 ALOGW_IF(!gotLock,
300 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
301 mId, __FUNCTION__);
302
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800304
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305 const char *status =
306 mStatus == STATUS_ERROR ? "ERROR" :
307 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700308 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
309 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310 mStatus == STATUS_ACTIVE ? "ACTIVE" :
311 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700312
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700314 if (mStatus == STATUS_ERROR) {
315 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
316 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800317 lines.appendFormat(" Stream configuration:\n");
318
319 if (mInputStream != NULL) {
320 write(fd, lines.string(), lines.size());
321 mInputStream->dump(fd, args);
322 } else {
323 lines.appendFormat(" No input stream.\n");
324 write(fd, lines.string(), lines.size());
325 }
326 for (size_t i = 0; i < mOutputStreams.size(); i++) {
327 mOutputStreams[i]->dump(fd,args);
328 }
329
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700330 lines = String8(" In-flight requests:\n");
331 if (mInFlightMap.size() == 0) {
332 lines.append(" None\n");
333 } else {
334 for (size_t i = 0; i < mInFlightMap.size(); i++) {
335 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700336 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700337 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
338 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
339 r.numBuffersLeft);
340 }
341 }
342 write(fd, lines.string(), lines.size());
343
Igor Murashkin1e479c02013-09-06 16:55:14 -0700344 {
345 lines = String8(" Last request sent:\n");
346 write(fd, lines.string(), lines.size());
347
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700348 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700349 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
350 }
351
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800352 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700353 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800354 write(fd, lines.string(), lines.size());
355 mHal3Device->ops->dump(mHal3Device, fd);
356 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800357
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700358 if (gotLock) mLock.unlock();
359 if (gotInterfaceLock) mInterfaceLock.unlock();
360
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800361 return OK;
362}
363
364const CameraMetadata& Camera3Device::info() const {
365 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800366 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
367 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700368 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800369 mStatus == STATUS_ERROR ?
370 "when in error state" : "before init");
371 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800372 return mDeviceInfo;
373}
374
Jianing Wei90e59c92014-03-12 18:29:36 -0700375status_t Camera3Device::checkStatusOkToCaptureLocked() {
376 switch (mStatus) {
377 case STATUS_ERROR:
378 CLOGE("Device has encountered a serious error");
379 return INVALID_OPERATION;
380 case STATUS_UNINITIALIZED:
381 CLOGE("Device not initialized");
382 return INVALID_OPERATION;
383 case STATUS_UNCONFIGURED:
384 case STATUS_CONFIGURED:
385 case STATUS_ACTIVE:
386 // OK
387 break;
388 default:
389 SET_ERR_L("Unexpected status: %d", mStatus);
390 return INVALID_OPERATION;
391 }
392 return OK;
393}
394
395status_t Camera3Device::convertMetadataListToRequestListLocked(
396 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
397 if (requestList == NULL) {
398 CLOGE("requestList cannot be NULL.");
399 return BAD_VALUE;
400 }
401
402 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
403 it != metadataList.end(); ++it) {
404 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
405 if (newRequest == 0) {
406 CLOGE("Can't create capture request");
407 return BAD_VALUE;
408 }
409 requestList->push_back(newRequest);
410 }
411 return OK;
412}
413
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800414status_t Camera3Device::capture(CameraMetadata &request) {
415 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700416 status_t res;
417 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800418 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800419
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700420 // TODO: take ownership of the request
421
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800422 switch (mStatus) {
423 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700424 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800425 return INVALID_OPERATION;
426 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700427 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800428 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700429 case STATUS_UNCONFIGURED:
430 // May be lazily configuring streams, will check during setup
431 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800432 case STATUS_ACTIVE:
433 // OK
434 break;
435 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700436 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800437 return INVALID_OPERATION;
438 }
439
440 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
441 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700442 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800443 return BAD_VALUE;
444 }
445
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700446 res = mRequestThread->queueRequest(newRequest);
447 if (res == OK) {
448 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
449 if (res != OK) {
450 SET_ERR_L("Can't transition to active in %f seconds!",
451 kActiveTimeout/1e9);
452 }
453 ALOGV("Camera %d: Capture request enqueued", mId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700454 } else {
455 CLOGE("Cannot queue request. Impossible."); // queueRequest always returns OK.
456 return BAD_VALUE;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700457 }
458 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800459}
460
Jianing Wei90e59c92014-03-12 18:29:36 -0700461status_t Camera3Device::submitRequestsHelper(
462 const List<const CameraMetadata> &requests, bool repeating) {
463 ATRACE_CALL();
464 Mutex::Autolock il(mInterfaceLock);
465 Mutex::Autolock l(mLock);
466
467 status_t res = checkStatusOkToCaptureLocked();
468 if (res != OK) {
469 // error logged by previous call
470 return res;
471 }
472
473 RequestList requestList;
474
475 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
476 if (res != OK) {
477 // error logged by previous call
478 return res;
479 }
480
481 if (repeating) {
482 res = mRequestThread->setRepeatingRequests(requestList);
483 } else {
484 res = mRequestThread->queueRequestList(requestList);
485 }
486
487 if (res == OK) {
488 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
489 if (res != OK) {
490 SET_ERR_L("Can't transition to active in %f seconds!",
491 kActiveTimeout/1e9);
492 }
493 ALOGV("Camera %d: Capture request enqueued", mId);
494 } else {
495 CLOGE("Cannot queue request. Impossible.");
496 return BAD_VALUE;
497 }
498
499 return res;
500}
501
502status_t Camera3Device::captureList(const List<const CameraMetadata> &requests) {
503 ATRACE_CALL();
504
505 return submitRequestsHelper(requests, /*repeating*/false);
506}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800507
508status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
509 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700510 status_t res;
511 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800512 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800513
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800514 switch (mStatus) {
515 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700516 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800517 return INVALID_OPERATION;
518 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700519 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800520 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700521 case STATUS_UNCONFIGURED:
522 // May be lazily configuring streams, will check during setup
523 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800524 case STATUS_ACTIVE:
525 // OK
526 break;
527 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700528 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800529 return INVALID_OPERATION;
530 }
531
532 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
533 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700534 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800535 return BAD_VALUE;
536 }
537
538 RequestList newRepeatingRequests;
539 newRepeatingRequests.push_back(newRepeatingRequest);
540
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700541 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
542 if (res == OK) {
543 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
544 if (res != OK) {
545 SET_ERR_L("Can't transition to active in %f seconds!",
546 kActiveTimeout/1e9);
547 }
548 ALOGV("Camera %d: Repeating request set", mId);
549 }
550 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800551}
552
Jianing Wei90e59c92014-03-12 18:29:36 -0700553status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests) {
554 ATRACE_CALL();
555
556 return submitRequestsHelper(requests, /*repeating*/true);
557}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558
559sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
560 const CameraMetadata &request) {
561 status_t res;
562
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700563 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800564 res = configureStreamsLocked();
565 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700566 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800567 return NULL;
568 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700569 if (mStatus == STATUS_UNCONFIGURED) {
570 CLOGE("No streams configured");
571 return NULL;
572 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573 }
574
575 sp<CaptureRequest> newRequest = createCaptureRequest(request);
576 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800577}
578
579status_t Camera3Device::clearStreamingRequest() {
580 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700581 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800582 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800583
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800584 switch (mStatus) {
585 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700586 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800587 return INVALID_OPERATION;
588 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700589 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700591 case STATUS_UNCONFIGURED:
592 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800593 case STATUS_ACTIVE:
594 // OK
595 break;
596 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700597 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800598 return INVALID_OPERATION;
599 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700600 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800601 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800602}
603
604status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
605 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700606 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800607
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700608 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800609}
610
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700611status_t Camera3Device::createInputStream(
612 uint32_t width, uint32_t height, int format, int *id) {
613 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700615 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
617 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700618
619 status_t res;
620 bool wasActive = false;
621
622 switch (mStatus) {
623 case STATUS_ERROR:
624 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
625 return INVALID_OPERATION;
626 case STATUS_UNINITIALIZED:
627 ALOGE("%s: Device not initialized", __FUNCTION__);
628 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629 case STATUS_UNCONFIGURED:
630 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700631 // OK
632 break;
633 case STATUS_ACTIVE:
634 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700635 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700636 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700637 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700638 return res;
639 }
640 wasActive = true;
641 break;
642 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700644 return INVALID_OPERATION;
645 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700646 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700647
648 if (mInputStream != 0) {
649 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
650 return INVALID_OPERATION;
651 }
652
653 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
654 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700655 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700656
657 mInputStream = newStream;
658
659 *id = mNextStreamId++;
660
661 // Continue captures if active at start
662 if (wasActive) {
663 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
664 res = configureStreamsLocked();
665 if (res != OK) {
666 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
667 __FUNCTION__, mNextStreamId, strerror(-res), res);
668 return res;
669 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700671 }
672
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700673 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700674 return OK;
675}
676
Igor Murashkin2fba5842013-04-22 14:03:54 -0700677
678status_t Camera3Device::createZslStream(
679 uint32_t width, uint32_t height,
680 int depth,
681 /*out*/
682 int *id,
683 sp<Camera3ZslStream>* zslStream) {
684 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700686 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
688 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700689
690 status_t res;
691 bool wasActive = false;
692
693 switch (mStatus) {
694 case STATUS_ERROR:
695 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
696 return INVALID_OPERATION;
697 case STATUS_UNINITIALIZED:
698 ALOGE("%s: Device not initialized", __FUNCTION__);
699 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 case STATUS_UNCONFIGURED:
701 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700702 // OK
703 break;
704 case STATUS_ACTIVE:
705 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700707 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700709 return res;
710 }
711 wasActive = true;
712 break;
713 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700715 return INVALID_OPERATION;
716 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700717 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700718
719 if (mInputStream != 0) {
720 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
721 return INVALID_OPERATION;
722 }
723
724 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
725 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700726 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700727
728 res = mOutputStreams.add(mNextStreamId, newStream);
729 if (res < 0) {
730 ALOGE("%s: Can't add new stream to set: %s (%d)",
731 __FUNCTION__, strerror(-res), res);
732 return res;
733 }
734 mInputStream = newStream;
735
736 *id = mNextStreamId++;
737 *zslStream = newStream;
738
739 // Continue captures if active at start
740 if (wasActive) {
741 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
742 res = configureStreamsLocked();
743 if (res != OK) {
744 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
745 __FUNCTION__, mNextStreamId, strerror(-res), res);
746 return res;
747 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 }
750
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700751 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700752 return OK;
753}
754
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800755status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
756 uint32_t width, uint32_t height, int format, size_t size, int *id) {
757 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800759 Mutex::Autolock l(mLock);
Colin Crosse5729fa2014-03-21 15:04:25 -0700760 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu",
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700761 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800762
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800763 status_t res;
764 bool wasActive = false;
765
766 switch (mStatus) {
767 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700768 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 return INVALID_OPERATION;
770 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700771 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800772 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700773 case STATUS_UNCONFIGURED:
774 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800775 // OK
776 break;
777 case STATUS_ACTIVE:
778 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700781 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782 return res;
783 }
784 wasActive = true;
785 break;
786 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700787 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 return INVALID_OPERATION;
789 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700790 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791
792 sp<Camera3OutputStream> newStream;
793 if (format == HAL_PIXEL_FORMAT_BLOB) {
794 newStream = new Camera3OutputStream(mNextStreamId, consumer,
795 width, height, size, format);
796 } else {
797 newStream = new Camera3OutputStream(mNextStreamId, consumer,
798 width, height, format);
799 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700800 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800801
802 res = mOutputStreams.add(mNextStreamId, newStream);
803 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700804 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 return res;
806 }
807
808 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700809 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800810
811 // Continue captures if active at start
812 if (wasActive) {
813 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
814 res = configureStreamsLocked();
815 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700816 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
817 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 return res;
819 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700820 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800824}
825
826status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
827 ATRACE_CALL();
828 (void)outputId; (void)id;
829
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700830 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831 return INVALID_OPERATION;
832}
833
834
835status_t Camera3Device::getStreamInfo(int id,
836 uint32_t *width, uint32_t *height, uint32_t *format) {
837 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 switch (mStatus) {
842 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700843 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844 return INVALID_OPERATION;
845 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 case STATUS_UNCONFIGURED:
849 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 case STATUS_ACTIVE:
851 // OK
852 break;
853 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700854 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 return INVALID_OPERATION;
856 }
857
858 ssize_t idx = mOutputStreams.indexOfKey(id);
859 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return idx;
862 }
863
864 if (width) *width = mOutputStreams[idx]->getWidth();
865 if (height) *height = mOutputStreams[idx]->getHeight();
866 if (format) *format = mOutputStreams[idx]->getFormat();
867
868 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800869}
870
871status_t Camera3Device::setStreamTransform(int id,
872 int transform) {
873 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700874 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800876
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 switch (mStatus) {
878 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700879 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 return INVALID_OPERATION;
881 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700882 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700884 case STATUS_UNCONFIGURED:
885 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 case STATUS_ACTIVE:
887 // OK
888 break;
889 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700890 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 return INVALID_OPERATION;
892 }
893
894 ssize_t idx = mOutputStreams.indexOfKey(id);
895 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 CLOGE("Stream %d does not exist",
897 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 return BAD_VALUE;
899 }
900
901 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902}
903
904status_t Camera3Device::deleteStream(int id) {
905 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700906 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 Mutex::Autolock l(mLock);
908 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800909
Igor Murashkine2172be2013-05-28 15:31:39 -0700910 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
911
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 // CameraDevice semantics require device to already be idle before
913 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700914 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700915 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
916 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 }
918
Igor Murashkin2fba5842013-04-22 14:03:54 -0700919 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800920 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 if (mInputStream != NULL && id == mInputStream->getId()) {
922 deletedStream = mInputStream;
923 mInputStream.clear();
924 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800925 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700926 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 return BAD_VALUE;
928 }
Zhijun He5f446352014-01-22 09:49:33 -0800929 }
930
931 // Delete output stream or the output part of a bi-directional stream.
932 if (outputStreamIdx != NAME_NOT_FOUND) {
933 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800934 mOutputStreams.removeItem(id);
935 }
936
937 // Free up the stream endpoint so that it can be used by some other stream
938 res = deletedStream->disconnect();
939 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700940 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941 // fall through since we want to still list the stream as deleted.
942 }
943 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700944 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945
946 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800947}
948
949status_t Camera3Device::deleteReprocessStream(int id) {
950 ATRACE_CALL();
951 (void)id;
952
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700953 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800954 return INVALID_OPERATION;
955}
956
957
958status_t Camera3Device::createDefaultRequest(int templateId,
959 CameraMetadata *request) {
960 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700961 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 Mutex::Autolock l(mLock);
964
965 switch (mStatus) {
966 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700967 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 return INVALID_OPERATION;
969 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700970 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800971 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700972 case STATUS_UNCONFIGURED:
973 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 case STATUS_ACTIVE:
975 // OK
976 break;
977 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700978 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 return INVALID_OPERATION;
980 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800981
982 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700983 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800984 rawRequest = mHal3Device->ops->construct_default_request_settings(
985 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700986 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700987 if (rawRequest == NULL) {
988 SET_ERR_L("HAL is unable to construct default settings for template %d",
989 templateId);
990 return DEAD_OBJECT;
991 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800992 *request = rawRequest;
993
994 return OK;
995}
996
997status_t Camera3Device::waitUntilDrained() {
998 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700999 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001000 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001001
Zhijun He69a37482014-03-23 18:44:49 -07001002 return waitUntilDrainedLocked();
1003}
1004
1005status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 switch (mStatus) {
1007 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001008 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009 ALOGV("%s: Already idle", __FUNCTION__);
1010 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001011 case STATUS_CONFIGURED:
1012 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 case STATUS_ERROR:
1014 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 break;
1017 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001018 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 return INVALID_OPERATION;
1020 }
1021
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001022 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1023 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1024 return res;
1025}
1026
1027// Pause to reconfigure
1028status_t Camera3Device::internalPauseAndWaitLocked() {
1029 mRequestThread->setPaused(true);
1030 mPauseStateNotify = true;
1031
1032 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1033 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1034 if (res != OK) {
1035 SET_ERR_L("Can't idle device in %f seconds!",
1036 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 }
1038
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001039 return res;
1040}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001042// Resume after internalPauseAndWaitLocked
1043status_t Camera3Device::internalResumeLocked() {
1044 status_t res;
1045
1046 mRequestThread->setPaused(false);
1047
1048 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1049 if (res != OK) {
1050 SET_ERR_L("Can't transition to active in %f seconds!",
1051 kActiveTimeout/1e9);
1052 }
1053 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001055}
1056
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001057status_t Camera3Device::waitUntilStateThenRelock(bool active,
1058 nsecs_t timeout) {
1059 status_t res = OK;
1060 if (active == (mStatus == STATUS_ACTIVE)) {
1061 // Desired state already reached
1062 return res;
1063 }
1064
1065 bool stateSeen = false;
1066 do {
1067 mRecentStatusUpdates.clear();
1068
1069 res = mStatusChanged.waitRelative(mLock, timeout);
1070 if (res != OK) break;
1071
1072 // Check state change history during wait
1073 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1074 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1075 stateSeen = true;
1076 break;
1077 }
1078 }
1079 } while (!stateSeen);
1080
1081 return res;
1082}
1083
1084
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001085status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1086 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001087 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001088
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001089 if (listener != NULL && mListener != NULL) {
1090 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1091 }
1092 mListener = listener;
1093
1094 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095}
1096
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001097bool Camera3Device::willNotify3A() {
1098 return false;
1099}
1100
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001101status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001102 status_t res;
1103 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001104
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001105 while (mResultQueue.empty()) {
1106 res = mResultSignal.waitRelative(mOutputLock, timeout);
1107 if (res == TIMED_OUT) {
1108 return res;
1109 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001110 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001111 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001112 return res;
1113 }
1114 }
1115 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116}
1117
1118status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1119 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001120 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001122 if (mResultQueue.empty()) {
1123 return NOT_ENOUGH_DATA;
1124 }
1125
1126 CameraMetadata &result = *(mResultQueue.begin());
1127 frame->acquire(result);
1128 mResultQueue.erase(mResultQueue.begin());
1129
1130 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001131}
1132
1133status_t Camera3Device::triggerAutofocus(uint32_t id) {
1134 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001136
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001137 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1138 // Mix-in this trigger into the next request and only the next request.
1139 RequestTrigger trigger[] = {
1140 {
1141 ANDROID_CONTROL_AF_TRIGGER,
1142 ANDROID_CONTROL_AF_TRIGGER_START
1143 },
1144 {
1145 ANDROID_CONTROL_AF_TRIGGER_ID,
1146 static_cast<int32_t>(id)
1147 },
1148 };
1149
1150 return mRequestThread->queueTrigger(trigger,
1151 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001152}
1153
1154status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1155 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001156 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001158 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1159 // Mix-in this trigger into the next request and only the next request.
1160 RequestTrigger trigger[] = {
1161 {
1162 ANDROID_CONTROL_AF_TRIGGER,
1163 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1164 },
1165 {
1166 ANDROID_CONTROL_AF_TRIGGER_ID,
1167 static_cast<int32_t>(id)
1168 },
1169 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001170
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001171 return mRequestThread->queueTrigger(trigger,
1172 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001173}
1174
1175status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1176 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001177 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001178
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001179 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1180 // Mix-in this trigger into the next request and only the next request.
1181 RequestTrigger trigger[] = {
1182 {
1183 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1184 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1185 },
1186 {
1187 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1188 static_cast<int32_t>(id)
1189 },
1190 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001191
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001192 return mRequestThread->queueTrigger(trigger,
1193 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001194}
1195
1196status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1197 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1198 ATRACE_CALL();
1199 (void)reprocessStreamId; (void)buffer; (void)listener;
1200
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001201 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202 return INVALID_OPERATION;
1203}
1204
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001205status_t Camera3Device::flush() {
1206 ATRACE_CALL();
1207 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001208 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001209 Mutex::Autolock l(mLock);
1210
1211 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001212 status_t res;
1213 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1214 res = mHal3Device->ops->flush(mHal3Device);
1215 } else {
Zhijun He69a37482014-03-23 18:44:49 -07001216 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001217 }
1218
1219 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001220}
1221
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001223 * Methods called by subclasses
1224 */
1225
1226void Camera3Device::notifyStatus(bool idle) {
1227 {
1228 // Need mLock to safely update state and synchronize to current
1229 // state of methods in flight.
1230 Mutex::Autolock l(mLock);
1231 // We can get various system-idle notices from the status tracker
1232 // while starting up. Only care about them if we've actually sent
1233 // in some requests recently.
1234 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1235 return;
1236 }
1237 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1238 idle ? "idle" : "active");
1239 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1240 mRecentStatusUpdates.add(mStatus);
1241 mStatusChanged.signal();
1242
1243 // Skip notifying listener if we're doing some user-transparent
1244 // state changes
1245 if (mPauseStateNotify) return;
1246 }
1247 NotificationListener *listener;
1248 {
1249 Mutex::Autolock l(mOutputLock);
1250 listener = mListener;
1251 }
1252 if (idle && listener != NULL) {
1253 listener->notifyIdle();
1254 }
1255}
1256
1257/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001258 * Camera3Device private methods
1259 */
1260
1261sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1262 const CameraMetadata &request) {
1263 ATRACE_CALL();
1264 status_t res;
1265
1266 sp<CaptureRequest> newRequest = new CaptureRequest;
1267 newRequest->mSettings = request;
1268
1269 camera_metadata_entry_t inputStreams =
1270 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1271 if (inputStreams.count > 0) {
1272 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001273 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 CLOGE("Request references unknown input stream %d",
1275 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001276 return NULL;
1277 }
1278 // Lazy completion of stream configuration (allocation/registration)
1279 // on first use
1280 if (mInputStream->isConfiguring()) {
1281 res = mInputStream->finishConfiguration(mHal3Device);
1282 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001284 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001285 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 return NULL;
1287 }
1288 }
1289
1290 newRequest->mInputStream = mInputStream;
1291 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1292 }
1293
1294 camera_metadata_entry_t streams =
1295 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1296 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001297 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001298 return NULL;
1299 }
1300
1301 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001302 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001303 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001304 CLOGE("Request references unknown stream %d",
1305 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 return NULL;
1307 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001308 sp<Camera3OutputStreamInterface> stream =
1309 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310
1311 // Lazy completion of stream configuration (allocation/registration)
1312 // on first use
1313 if (stream->isConfiguring()) {
1314 res = stream->finishConfiguration(mHal3Device);
1315 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1317 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001318 return NULL;
1319 }
1320 }
1321
1322 newRequest->mOutputStreams.push(stream);
1323 }
1324 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1325
1326 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001327}
1328
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001329status_t Camera3Device::configureStreamsLocked() {
1330 ATRACE_CALL();
1331 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001332
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001334 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001335 return INVALID_OPERATION;
1336 }
1337
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001338 if (!mNeedConfig) {
1339 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1340 return OK;
1341 }
1342
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001343 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001344 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345
1346 camera3_stream_configuration config;
1347
1348 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1349
1350 Vector<camera3_stream_t*> streams;
1351 streams.setCapacity(config.num_streams);
1352
1353 if (mInputStream != NULL) {
1354 camera3_stream_t *inputStream;
1355 inputStream = mInputStream->startConfiguration();
1356 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001357 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001358 return INVALID_OPERATION;
1359 }
1360 streams.add(inputStream);
1361 }
1362
1363 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001364
1365 // Don't configure bidi streams twice, nor add them twice to the list
1366 if (mOutputStreams[i].get() ==
1367 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1368
1369 config.num_streams--;
1370 continue;
1371 }
1372
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373 camera3_stream_t *outputStream;
1374 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1375 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001376 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377 return INVALID_OPERATION;
1378 }
1379 streams.add(outputStream);
1380 }
1381
1382 config.streams = streams.editArray();
1383
1384 // Do the HAL configuration; will potentially touch stream
1385 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001386 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001388 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389
1390 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001391 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1392 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393 return res;
1394 }
1395
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001396 // Finish all stream configuration immediately.
1397 // TODO: Try to relax this later back to lazy completion, which should be
1398 // faster
1399
Igor Murashkin073f8572013-05-02 14:59:28 -07001400 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001401 res = mInputStream->finishConfiguration(mHal3Device);
1402 if (res != OK) {
1403 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1404 mInputStream->getId(), strerror(-res), res);
1405 return res;
1406 }
1407 }
1408
1409 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001410 sp<Camera3OutputStreamInterface> outputStream =
1411 mOutputStreams.editValueAt(i);
1412 if (outputStream->isConfiguring()) {
1413 res = outputStream->finishConfiguration(mHal3Device);
1414 if (res != OK) {
1415 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1416 outputStream->getId(), strerror(-res), res);
1417 return res;
1418 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001419 }
1420 }
1421
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001422 // Request thread needs to know to avoid using repeat-last-settings protocol
1423 // across configure_streams() calls
1424 mRequestThread->configurationComplete();
1425
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001426 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001427
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001428 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001430 if (config.num_streams > 0) {
1431 mStatus = STATUS_CONFIGURED;
1432 } else {
1433 mStatus = STATUS_UNCONFIGURED;
1434 }
1435
1436 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1437
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001439}
1440
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001441void Camera3Device::setErrorState(const char *fmt, ...) {
1442 Mutex::Autolock l(mLock);
1443 va_list args;
1444 va_start(args, fmt);
1445
1446 setErrorStateLockedV(fmt, args);
1447
1448 va_end(args);
1449}
1450
1451void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1452 Mutex::Autolock l(mLock);
1453 setErrorStateLockedV(fmt, args);
1454}
1455
1456void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1457 va_list args;
1458 va_start(args, fmt);
1459
1460 setErrorStateLockedV(fmt, args);
1461
1462 va_end(args);
1463}
1464
1465void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001466 // Print out all error messages to log
1467 String8 errorCause = String8::formatV(fmt, args);
1468 ALOGE("Camera %d: %s", mId, errorCause.string());
1469
1470 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001471 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001472
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001473 // Save stack trace. View by dumping it later.
1474 CameraTraces::saveTrace();
1475 // TODO: consider adding errorCause and client pid/procname
1476
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001477 mErrorCause = errorCause;
1478
1479 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001480 mStatus = STATUS_ERROR;
1481}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482
1483/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001484 * In-flight request management
1485 */
1486
1487status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001488 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001489 ATRACE_CALL();
1490 Mutex::Autolock l(mInFlightLock);
1491
1492 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001493 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001494 if (res < 0) return res;
1495
1496 return OK;
1497}
1498
1499/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001500 * QUIRK(partial results)
1501 * Check if all 3A fields are ready, and send off a partial 3A-only result
1502 * to the output frame queue
1503 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001504bool Camera3Device::processPartial3AQuirk(
1505 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001506 const CameraMetadata& partial) {
1507
1508 // Check if all 3A states are present
1509 // The full list of fields is
1510 // android.control.afMode
1511 // android.control.awbMode
1512 // android.control.aeState
1513 // android.control.awbState
1514 // android.control.afState
1515 // android.control.afTriggerID
1516 // android.control.aePrecaptureID
1517 // TODO: Add android.control.aeMode
1518
1519 bool gotAllStates = true;
1520
1521 uint8_t afMode;
1522 uint8_t awbMode;
1523 uint8_t aeState;
1524 uint8_t afState;
1525 uint8_t awbState;
1526 int32_t afTriggerId;
1527 int32_t aeTriggerId;
1528
1529 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1530 &afMode, frameNumber);
1531
1532 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1533 &awbMode, frameNumber);
1534
1535 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1536 &aeState, frameNumber);
1537
1538 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1539 &afState, frameNumber);
1540
1541 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1542 &awbState, frameNumber);
1543
1544 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1545 &afTriggerId, frameNumber);
1546
1547 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1548 &aeTriggerId, frameNumber);
1549
1550 if (!gotAllStates) return false;
1551
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001552 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001553 "AF state %d, AE state %d, AWB state %d, "
1554 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001555 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001556 afMode, awbMode,
1557 afState, aeState, awbState,
1558 afTriggerId, aeTriggerId);
1559
1560 // Got all states, so construct a minimal result to send
1561 // In addition to the above fields, this means adding in
1562 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001563 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001564 // android.quirks.partialResult
1565
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001566 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001567
1568 Mutex::Autolock l(mOutputLock);
1569
1570 CameraMetadata& min3AResult =
1571 *mResultQueue.insert(
1572 mResultQueue.end(),
1573 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1574
1575 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1576 &frameNumber, frameNumber)) {
1577 return false;
1578 }
1579
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001580 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1581 &requestId, frameNumber)) {
1582 return false;
1583 }
1584
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001585 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1586 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1587 &partialResult, frameNumber)) {
1588 return false;
1589 }
1590
1591 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1592 &afMode, frameNumber)) {
1593 return false;
1594 }
1595
1596 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1597 &awbMode, frameNumber)) {
1598 return false;
1599 }
1600
1601 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1602 &aeState, frameNumber)) {
1603 return false;
1604 }
1605
1606 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1607 &afState, frameNumber)) {
1608 return false;
1609 }
1610
1611 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1612 &awbState, frameNumber)) {
1613 return false;
1614 }
1615
1616 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1617 &afTriggerId, frameNumber)) {
1618 return false;
1619 }
1620
1621 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1622 &aeTriggerId, frameNumber)) {
1623 return false;
1624 }
1625
1626 mResultSignal.signal();
1627
1628 return true;
1629}
1630
1631template<typename T>
1632bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1633 T* value, int32_t frameNumber) {
1634 (void) frameNumber;
1635
1636 camera_metadata_ro_entry_t entry;
1637
1638 entry = result.find(tag);
1639 if (entry.count == 0) {
1640 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1641 mId, frameNumber, get_camera_metadata_tag_name(tag));
1642 return false;
1643 }
1644
1645 if (sizeof(T) == sizeof(uint8_t)) {
1646 *value = entry.data.u8[0];
1647 } else if (sizeof(T) == sizeof(int32_t)) {
1648 *value = entry.data.i32[0];
1649 } else {
1650 ALOGE("%s: Unexpected type", __FUNCTION__);
1651 return false;
1652 }
1653 return true;
1654}
1655
1656template<typename T>
1657bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1658 const T* value, int32_t frameNumber) {
1659 if (result.update(tag, value, 1) != NO_ERROR) {
1660 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1661 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1662 frameNumber, get_camera_metadata_tag_name(tag));
1663 return false;
1664 }
1665 return true;
1666}
1667
1668/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001669 * Camera HAL device callback methods
1670 */
1671
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001672void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001673 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001674
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001675 status_t res;
1676
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001677 uint32_t frameNumber = result->frame_number;
1678 if (result->result == NULL && result->num_output_buffers == 0) {
1679 SET_ERR("No result data provided by HAL for frame %d",
1680 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001681 return;
1682 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001683 bool partialResultQuirk = false;
1684 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001685
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001686 // Get capture timestamp from list of in-flight requests, where it was added
1687 // by the shutter notification for this frame. Then update the in-flight
1688 // status and remove the in-flight entry if all result data has been
1689 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001690 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001691 {
1692 Mutex::Autolock l(mInFlightLock);
1693 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1694 if (idx == NAME_NOT_FOUND) {
1695 SET_ERR("Unknown frame number for capture result: %d",
1696 frameNumber);
1697 return;
1698 }
1699 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001700
1701 // Check if this result carries only partial metadata
1702 if (mUsePartialResultQuirk && result->result != NULL) {
1703 camera_metadata_ro_entry_t partialResultEntry;
1704 res = find_camera_metadata_ro_entry(result->result,
1705 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1706 if (res != NAME_NOT_FOUND &&
1707 partialResultEntry.count > 0 &&
1708 partialResultEntry.data.u8[0] ==
1709 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1710 // A partial result. Flag this as such, and collect this
1711 // set of metadata into the in-flight entry.
1712 partialResultQuirk = true;
1713 request.partialResultQuirk.collectedResult.append(
1714 result->result);
1715 request.partialResultQuirk.collectedResult.erase(
1716 ANDROID_QUIRKS_PARTIAL_RESULT);
1717 // Fire off a 3A-only result if possible
1718 if (!request.partialResultQuirk.haveSent3A) {
1719 request.partialResultQuirk.haveSent3A =
1720 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001721 request.requestId,
1722 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001723 }
1724 }
1725 }
1726
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001727 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001728 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001729 * One of the following must happen before it's legal to call process_capture_result,
1730 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001731 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1732 * - CAMERA3_MSG_ERROR (expected during flush)
1733 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001734 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001735 SET_ERR("Called before shutter notify for frame %d",
1736 frameNumber);
1737 return;
1738 }
1739
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001740 // Did we get the (final) result metadata for this capture?
1741 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001742 if (request.haveResultMetadata) {
1743 SET_ERR("Called multiple times with metadata for frame %d",
1744 frameNumber);
1745 return;
1746 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001747 if (mUsePartialResultQuirk &&
1748 !request.partialResultQuirk.collectedResult.isEmpty()) {
1749 collectedQuirkResult.acquire(
1750 request.partialResultQuirk.collectedResult);
1751 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001752 request.haveResultMetadata = true;
1753 }
1754
1755 request.numBuffersLeft -= result->num_output_buffers;
1756
1757 if (request.numBuffersLeft < 0) {
1758 SET_ERR("Too many buffers returned for frame %d",
1759 frameNumber);
1760 return;
1761 }
1762
Zhijun He1b05dfc2013-11-21 12:57:51 -08001763 // Check if everything has arrived for this result (buffers and metadata), remove it from
1764 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1765 if ((request.requestStatus != OK) ||
1766 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001767 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001768 mInFlightMap.removeItemsAt(idx, 1);
1769 }
1770
1771 // Sanity check - if we have too many in-flight frames, something has
1772 // likely gone wrong
1773 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001774 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001775 }
1776
1777 }
1778
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001779 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001780 bool gotResult = false;
1781 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001782 Mutex::Autolock l(mOutputLock);
1783
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001784 gotResult = true;
1785
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001786 if (frameNumber != mNextResultFrameNumber) {
1787 SET_ERR("Out-of-order capture result metadata submitted! "
1788 "(got frame number %d, expecting %d)",
1789 frameNumber, mNextResultFrameNumber);
1790 return;
1791 }
1792 mNextResultFrameNumber++;
1793
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001794 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001795 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001796
Igor Murashkind2c90692013-04-02 12:32:32 -07001797 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001798 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001799 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001800 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001801 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001802 } else {
1803 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001804 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001805 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001806
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001807 // Append any previous partials to form a complete result
1808 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1809 captureResult.append(collectedQuirkResult);
1810 }
1811
1812 captureResult.sort();
1813
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001814 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001815
1816 camera_metadata_entry entry =
1817 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1818 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001819 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001820 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001821 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001822 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001823 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001824 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001825 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001826 gotResult = false;
1827 }
1828
1829 if (gotResult) {
1830 // Valid result, insert into queue
1831 CameraMetadata& queuedResult =
1832 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1833 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001834 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001835 } // scope for mOutputLock
1836
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001837 // Return completed buffers to their streams with the timestamp
1838
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001839 for (size_t i = 0; i < result->num_output_buffers; i++) {
1840 Camera3Stream *stream =
1841 Camera3Stream::cast(result->output_buffers[i].stream);
1842 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1843 // Note: stream may be deallocated at this point, if this buffer was the
1844 // last reference to it.
1845 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001846 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001847 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001848 }
1849 }
1850
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001851 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001852
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001853 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001854 mResultSignal.signal();
1855 }
1856
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001857}
1858
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001859
1860
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001861void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001862 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001863 NotificationListener *listener;
1864 {
1865 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001866 listener = mListener;
1867 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001868
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001869 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001870 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001871 return;
1872 }
1873
1874 switch (msg->type) {
1875 case CAMERA3_MSG_ERROR: {
1876 int streamId = 0;
1877 if (msg->message.error.error_stream != NULL) {
1878 Camera3Stream *stream =
1879 Camera3Stream::cast(
1880 msg->message.error.error_stream);
1881 streamId = stream->getId();
1882 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001883 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1884 mId, __FUNCTION__, msg->message.error.frame_number,
1885 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001886
1887 // Set request error status for the request in the in-flight tracking
1888 {
1889 Mutex::Autolock l(mInFlightLock);
1890 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1891 if (idx >= 0) {
1892 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1893 }
1894 }
1895
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001896 if (listener != NULL) {
1897 listener->notifyError(msg->message.error.error_code,
1898 msg->message.error.frame_number, streamId);
1899 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001900 break;
1901 }
1902 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001903 ssize_t idx;
1904 uint32_t frameNumber = msg->message.shutter.frame_number;
1905 nsecs_t timestamp = msg->message.shutter.timestamp;
1906 // Verify ordering of shutter notifications
1907 {
1908 Mutex::Autolock l(mOutputLock);
1909 if (frameNumber != mNextShutterFrameNumber) {
1910 SET_ERR("Shutter notification out-of-order. Expected "
1911 "notification for frame %d, got frame %d",
1912 mNextShutterFrameNumber, frameNumber);
1913 break;
1914 }
1915 mNextShutterFrameNumber++;
1916 }
1917
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001918 int32_t requestId = -1;
1919
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001920 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001921 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001922 {
1923 Mutex::Autolock l(mInFlightLock);
1924 idx = mInFlightMap.indexOfKey(frameNumber);
1925 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001926 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1927 r.captureTimestamp = timestamp;
1928 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001929 }
1930 }
1931 if (idx < 0) {
1932 SET_ERR("Shutter notification for non-existent frame number %d",
1933 frameNumber);
1934 break;
1935 }
Colin Crosse5729fa2014-03-21 15:04:25 -07001936 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001937 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001938 // Call listener, if any
1939 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001940 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001941 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001942 break;
1943 }
1944 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001945 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001946 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001947 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001948}
1949
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001950CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001951 ALOGV("%s", __FUNCTION__);
1952
Igor Murashkin1e479c02013-09-06 16:55:14 -07001953 CameraMetadata retVal;
1954
1955 if (mRequestThread != NULL) {
1956 retVal = mRequestThread->getLatestRequest();
1957 }
1958
Igor Murashkin1e479c02013-09-06 16:55:14 -07001959 return retVal;
1960}
1961
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001962/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001963 * RequestThread inner class methods
1964 */
1965
1966Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001967 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001968 camera3_device_t *hal3Device) :
1969 Thread(false),
1970 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001971 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001972 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001973 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001974 mReconfigured(false),
1975 mDoPause(false),
1976 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001977 mFrameNumber(0),
1978 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001979 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980}
1981
1982void Camera3Device::RequestThread::configurationComplete() {
1983 Mutex::Autolock l(mRequestLock);
1984 mReconfigured = true;
1985}
1986
1987status_t Camera3Device::RequestThread::queueRequest(
1988 sp<CaptureRequest> request) {
1989 Mutex::Autolock l(mRequestLock);
1990 mRequestQueue.push_back(request);
1991
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001992 unpauseForNewRequests();
1993
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001994 return OK;
1995}
1996
Jianing Wei90e59c92014-03-12 18:29:36 -07001997status_t Camera3Device::RequestThread::queueRequestList(
1998 List<sp<CaptureRequest> > &requests) {
1999 Mutex::Autolock l(mRequestLock);
2000 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2001 ++it) {
2002 mRequestQueue.push_back(*it);
2003 }
2004
2005 unpauseForNewRequests();
2006
2007 return OK;
2008}
2009
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002010
2011status_t Camera3Device::RequestThread::queueTrigger(
2012 RequestTrigger trigger[],
2013 size_t count) {
2014
2015 Mutex::Autolock l(mTriggerMutex);
2016 status_t ret;
2017
2018 for (size_t i = 0; i < count; ++i) {
2019 ret = queueTriggerLocked(trigger[i]);
2020
2021 if (ret != OK) {
2022 return ret;
2023 }
2024 }
2025
2026 return OK;
2027}
2028
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002029int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2030 sp<Camera3Device> d = device.promote();
2031 if (d != NULL) return d->mId;
2032 return 0;
2033}
2034
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002035status_t Camera3Device::RequestThread::queueTriggerLocked(
2036 RequestTrigger trigger) {
2037
2038 uint32_t tag = trigger.metadataTag;
2039 ssize_t index = mTriggerMap.indexOfKey(tag);
2040
2041 switch (trigger.getTagType()) {
2042 case TYPE_BYTE:
2043 // fall-through
2044 case TYPE_INT32:
2045 break;
2046 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002047 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2048 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002049 return INVALID_OPERATION;
2050 }
2051
2052 /**
2053 * Collect only the latest trigger, since we only have 1 field
2054 * in the request settings per trigger tag, and can't send more than 1
2055 * trigger per request.
2056 */
2057 if (index != NAME_NOT_FOUND) {
2058 mTriggerMap.editValueAt(index) = trigger;
2059 } else {
2060 mTriggerMap.add(tag, trigger);
2061 }
2062
2063 return OK;
2064}
2065
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066status_t Camera3Device::RequestThread::setRepeatingRequests(
2067 const RequestList &requests) {
2068 Mutex::Autolock l(mRequestLock);
2069 mRepeatingRequests.clear();
2070 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2071 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002072
2073 unpauseForNewRequests();
2074
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002075 return OK;
2076}
2077
2078status_t Camera3Device::RequestThread::clearRepeatingRequests() {
2079 Mutex::Autolock l(mRequestLock);
2080 mRepeatingRequests.clear();
2081 return OK;
2082}
2083
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002084status_t Camera3Device::RequestThread::clear() {
2085 Mutex::Autolock l(mRequestLock);
2086 mRepeatingRequests.clear();
2087 mRequestQueue.clear();
2088 mTriggerMap.clear();
2089 return OK;
2090}
2091
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092void Camera3Device::RequestThread::setPaused(bool paused) {
2093 Mutex::Autolock l(mPauseLock);
2094 mDoPause = paused;
2095 mDoPauseSignal.signal();
2096}
2097
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002098status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2099 int32_t requestId, nsecs_t timeout) {
2100 Mutex::Autolock l(mLatestRequestMutex);
2101 status_t res;
2102 while (mLatestRequestId != requestId) {
2103 nsecs_t startTime = systemTime();
2104
2105 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2106 if (res != OK) return res;
2107
2108 timeout -= (systemTime() - startTime);
2109 }
2110
2111 return OK;
2112}
2113
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002114void Camera3Device::RequestThread::requestExit() {
2115 // Call parent to set up shutdown
2116 Thread::requestExit();
2117 // The exit from any possible waits
2118 mDoPauseSignal.signal();
2119 mRequestSignal.signal();
2120}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002121
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002122bool Camera3Device::RequestThread::threadLoop() {
2123
2124 status_t res;
2125
2126 // Handle paused state.
2127 if (waitIfPaused()) {
2128 return true;
2129 }
2130
2131 // Get work to do
2132
2133 sp<CaptureRequest> nextRequest = waitForNextRequest();
2134 if (nextRequest == NULL) {
2135 return true;
2136 }
2137
2138 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002139 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002140 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002142 // Get the request ID, if any
2143 int requestId;
2144 camera_metadata_entry_t requestIdEntry =
2145 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2146 if (requestIdEntry.count > 0) {
2147 requestId = requestIdEntry.data.i32[0];
2148 } else {
2149 ALOGW("%s: Did not have android.request.id set in the request",
2150 __FUNCTION__);
2151 requestId = NAME_NOT_FOUND;
2152 }
2153
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002154 // Insert any queued triggers (before metadata is locked)
2155 int32_t triggerCount;
2156 res = insertTriggers(nextRequest);
2157 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002158 SET_ERR("RequestThread: Unable to insert triggers "
2159 "(capture request %d, HAL device: %s (%d)",
2160 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002161 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2162 return false;
2163 }
2164 triggerCount = res;
2165
2166 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2167
2168 // If the request is the same as last, or we had triggers last time
2169 if (mPrevRequest != nextRequest || triggersMixedIn) {
2170 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002171 * HAL workaround:
2172 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2173 */
2174 res = addDummyTriggerIds(nextRequest);
2175 if (res != OK) {
2176 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2177 "(capture request %d, HAL device: %s (%d)",
2178 (mFrameNumber+1), strerror(-res), res);
2179 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2180 return false;
2181 }
2182
2183 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002184 * The request should be presorted so accesses in HAL
2185 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2186 */
2187 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002188 request.settings = nextRequest->mSettings.getAndLock();
2189 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002190 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2191
2192 IF_ALOGV() {
2193 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2194 find_camera_metadata_ro_entry(
2195 request.settings,
2196 ANDROID_CONTROL_AF_TRIGGER,
2197 &e
2198 );
2199 if (e.count > 0) {
2200 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2201 __FUNCTION__,
2202 mFrameNumber+1,
2203 e.data.u8[0]);
2204 }
2205 }
2206 } else {
2207 // leave request.settings NULL to indicate 'reuse latest given'
2208 ALOGVV("%s: Request settings are REUSED",
2209 __FUNCTION__);
2210 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002211
2212 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213
2214 // Fill in buffers
2215
2216 if (nextRequest->mInputStream != NULL) {
2217 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002218 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002219 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002220 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002221 " %s (%d)", strerror(-res), res);
2222 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2223 return true;
2224 }
2225 } else {
2226 request.input_buffer = NULL;
2227 }
2228
2229 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2230 nextRequest->mOutputStreams.size());
2231 request.output_buffers = outputBuffers.array();
2232 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2233 res = nextRequest->mOutputStreams.editItemAt(i)->
2234 getBuffer(&outputBuffers.editItemAt(i));
2235 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002236 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2237 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002238 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2239 return true;
2240 }
2241 request.num_output_buffers++;
2242 }
2243
2244 request.frame_number = mFrameNumber++;
2245
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002246 // Log request in the in-flight queue
2247 sp<Camera3Device> parent = mParent.promote();
2248 if (parent == NULL) {
2249 CLOGE("RequestThread: Parent is gone");
2250 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2251 return false;
2252 }
2253
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002254 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002255 request.num_output_buffers);
2256 if (res != OK) {
2257 SET_ERR("RequestThread: Unable to register new in-flight request:"
2258 " %s (%d)", strerror(-res), res);
2259 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2260 return false;
2261 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002262
Zhijun Hecc27e112013-10-03 16:12:43 -07002263 // Inform waitUntilRequestProcessed thread of a new request ID
2264 {
2265 Mutex::Autolock al(mLatestRequestMutex);
2266
2267 mLatestRequestId = requestId;
2268 mLatestRequestSignal.signal();
2269 }
2270
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002271 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002272 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2273 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002274 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002275 ATRACE_END();
2276
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002277 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002278 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002279 " device: %s (%d)", request.frame_number, strerror(-res), res);
2280 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2281 return false;
2282 }
2283
Igor Murashkin1e479c02013-09-06 16:55:14 -07002284 // Update the latest request sent to HAL
2285 if (request.settings != NULL) { // Don't update them if they were unchanged
2286 Mutex::Autolock al(mLatestRequestMutex);
2287
2288 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2289 mLatestRequest.acquire(cloned);
2290 }
2291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002292 if (request.settings != NULL) {
2293 nextRequest->mSettings.unlock(request.settings);
2294 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002295
2296 // Remove any previously queued triggers (after unlock)
2297 res = removeTriggers(mPrevRequest);
2298 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002299 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002300 "(capture request %d, HAL device: %s (%d)",
2301 request.frame_number, strerror(-res), res);
2302 return false;
2303 }
2304 mPrevTriggers = triggerCount;
2305
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002306 // Return input buffer back to framework
2307 if (request.input_buffer != NULL) {
2308 Camera3Stream *stream =
2309 Camera3Stream::cast(request.input_buffer->stream);
2310 res = stream->returnInputBuffer(*(request.input_buffer));
2311 // Note: stream may be deallocated at this point, if this buffer was the
2312 // last reference to it.
2313 if (res != OK) {
2314 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2315 " its stream:%s (%d)", __FUNCTION__,
2316 request.frame_number, strerror(-res), res);
2317 // TODO: Report error upstream
2318 }
2319 }
2320
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002321 return true;
2322}
2323
Igor Murashkin1e479c02013-09-06 16:55:14 -07002324CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2325 Mutex::Autolock al(mLatestRequestMutex);
2326
2327 ALOGV("RequestThread::%s", __FUNCTION__);
2328
2329 return mLatestRequest;
2330}
2331
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002332void Camera3Device::RequestThread::cleanUpFailedRequest(
2333 camera3_capture_request_t &request,
2334 sp<CaptureRequest> &nextRequest,
2335 Vector<camera3_stream_buffer_t> &outputBuffers) {
2336
2337 if (request.settings != NULL) {
2338 nextRequest->mSettings.unlock(request.settings);
2339 }
2340 if (request.input_buffer != NULL) {
2341 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002342 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002343 }
2344 for (size_t i = 0; i < request.num_output_buffers; i++) {
2345 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2346 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2347 outputBuffers[i], 0);
2348 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349}
2350
2351sp<Camera3Device::CaptureRequest>
2352 Camera3Device::RequestThread::waitForNextRequest() {
2353 status_t res;
2354 sp<CaptureRequest> nextRequest;
2355
2356 // Optimized a bit for the simple steady-state case (single repeating
2357 // request), to avoid putting that request in the queue temporarily.
2358 Mutex::Autolock l(mRequestLock);
2359
2360 while (mRequestQueue.empty()) {
2361 if (!mRepeatingRequests.empty()) {
2362 // Always atomically enqueue all requests in a repeating request
2363 // list. Guarantees a complete in-sequence set of captures to
2364 // application.
2365 const RequestList &requests = mRepeatingRequests;
2366 RequestList::const_iterator firstRequest =
2367 requests.begin();
2368 nextRequest = *firstRequest;
2369 mRequestQueue.insert(mRequestQueue.end(),
2370 ++firstRequest,
2371 requests.end());
2372 // No need to wait any longer
2373 break;
2374 }
2375
2376 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2377
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002378 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2379 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002380 Mutex::Autolock pl(mPauseLock);
2381 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002382 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002384 // Let the tracker know
2385 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2386 if (statusTracker != 0) {
2387 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2388 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002389 }
2390 // Stop waiting for now and let thread management happen
2391 return NULL;
2392 }
2393 }
2394
2395 if (nextRequest == NULL) {
2396 // Don't have a repeating request already in hand, so queue
2397 // must have an entry now.
2398 RequestList::iterator firstRequest =
2399 mRequestQueue.begin();
2400 nextRequest = *firstRequest;
2401 mRequestQueue.erase(firstRequest);
2402 }
2403
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002404 // In case we've been unpaused by setPaused clearing mDoPause, need to
2405 // update internal pause state (capture/setRepeatingRequest unpause
2406 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002407 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002408 if (mPaused) {
2409 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2410 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2411 if (statusTracker != 0) {
2412 statusTracker->markComponentActive(mStatusId);
2413 }
2414 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002415 mPaused = false;
2416
2417 // Check if we've reconfigured since last time, and reset the preview
2418 // request if so. Can't use 'NULL request == repeat' across configure calls.
2419 if (mReconfigured) {
2420 mPrevRequest.clear();
2421 mReconfigured = false;
2422 }
2423
2424 return nextRequest;
2425}
2426
2427bool Camera3Device::RequestThread::waitIfPaused() {
2428 status_t res;
2429 Mutex::Autolock l(mPauseLock);
2430 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002431 if (mPaused == false) {
2432 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002433 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2434 // Let the tracker know
2435 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2436 if (statusTracker != 0) {
2437 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2438 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002439 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002441 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002442 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002443 return true;
2444 }
2445 }
2446 // We don't set mPaused to false here, because waitForNextRequest needs
2447 // to further manage the paused state in case of starvation.
2448 return false;
2449}
2450
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002451void Camera3Device::RequestThread::unpauseForNewRequests() {
2452 // With work to do, mark thread as unpaused.
2453 // If paused by request (setPaused), don't resume, to avoid
2454 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002455 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002456 Mutex::Autolock p(mPauseLock);
2457 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002458 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2459 if (mPaused) {
2460 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2461 if (statusTracker != 0) {
2462 statusTracker->markComponentActive(mStatusId);
2463 }
2464 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002465 mPaused = false;
2466 }
2467}
2468
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002469void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2470 sp<Camera3Device> parent = mParent.promote();
2471 if (parent != NULL) {
2472 va_list args;
2473 va_start(args, fmt);
2474
2475 parent->setErrorStateV(fmt, args);
2476
2477 va_end(args);
2478 }
2479}
2480
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002481status_t Camera3Device::RequestThread::insertTriggers(
2482 const sp<CaptureRequest> &request) {
2483
2484 Mutex::Autolock al(mTriggerMutex);
2485
2486 CameraMetadata &metadata = request->mSettings;
2487 size_t count = mTriggerMap.size();
2488
2489 for (size_t i = 0; i < count; ++i) {
2490 RequestTrigger trigger = mTriggerMap.valueAt(i);
2491
2492 uint32_t tag = trigger.metadataTag;
2493 camera_metadata_entry entry = metadata.find(tag);
2494
2495 if (entry.count > 0) {
2496 /**
2497 * Already has an entry for this trigger in the request.
2498 * Rewrite it with our requested trigger value.
2499 */
2500 RequestTrigger oldTrigger = trigger;
2501
2502 oldTrigger.entryValue = entry.data.u8[0];
2503
2504 mTriggerReplacedMap.add(tag, oldTrigger);
2505 } else {
2506 /**
2507 * More typical, no trigger entry, so we just add it
2508 */
2509 mTriggerRemovedMap.add(tag, trigger);
2510 }
2511
2512 status_t res;
2513
2514 switch (trigger.getTagType()) {
2515 case TYPE_BYTE: {
2516 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2517 res = metadata.update(tag,
2518 &entryValue,
2519 /*count*/1);
2520 break;
2521 }
2522 case TYPE_INT32:
2523 res = metadata.update(tag,
2524 &trigger.entryValue,
2525 /*count*/1);
2526 break;
2527 default:
2528 ALOGE("%s: Type not supported: 0x%x",
2529 __FUNCTION__,
2530 trigger.getTagType());
2531 return INVALID_OPERATION;
2532 }
2533
2534 if (res != OK) {
2535 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2536 ", value %d", __FUNCTION__, trigger.getTagName(),
2537 trigger.entryValue);
2538 return res;
2539 }
2540
2541 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2542 trigger.getTagName(),
2543 trigger.entryValue);
2544 }
2545
2546 mTriggerMap.clear();
2547
2548 return count;
2549}
2550
2551status_t Camera3Device::RequestThread::removeTriggers(
2552 const sp<CaptureRequest> &request) {
2553 Mutex::Autolock al(mTriggerMutex);
2554
2555 CameraMetadata &metadata = request->mSettings;
2556
2557 /**
2558 * Replace all old entries with their old values.
2559 */
2560 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2561 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2562
2563 status_t res;
2564
2565 uint32_t tag = trigger.metadataTag;
2566 switch (trigger.getTagType()) {
2567 case TYPE_BYTE: {
2568 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2569 res = metadata.update(tag,
2570 &entryValue,
2571 /*count*/1);
2572 break;
2573 }
2574 case TYPE_INT32:
2575 res = metadata.update(tag,
2576 &trigger.entryValue,
2577 /*count*/1);
2578 break;
2579 default:
2580 ALOGE("%s: Type not supported: 0x%x",
2581 __FUNCTION__,
2582 trigger.getTagType());
2583 return INVALID_OPERATION;
2584 }
2585
2586 if (res != OK) {
2587 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2588 ", trigger value %d", __FUNCTION__,
2589 trigger.getTagName(), trigger.entryValue);
2590 return res;
2591 }
2592 }
2593 mTriggerReplacedMap.clear();
2594
2595 /**
2596 * Remove all new entries.
2597 */
2598 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2599 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2600 status_t res = metadata.erase(trigger.metadataTag);
2601
2602 if (res != OK) {
2603 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2604 ", trigger value %d", __FUNCTION__,
2605 trigger.getTagName(), trigger.entryValue);
2606 return res;
2607 }
2608 }
2609 mTriggerRemovedMap.clear();
2610
2611 return OK;
2612}
2613
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002614status_t Camera3Device::RequestThread::addDummyTriggerIds(
2615 const sp<CaptureRequest> &request) {
2616 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2617 static const int32_t dummyTriggerId = 1;
2618 status_t res;
2619
2620 CameraMetadata &metadata = request->mSettings;
2621
2622 // If AF trigger is active, insert a dummy AF trigger ID if none already
2623 // exists
2624 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2625 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2626 if (afTrigger.count > 0 &&
2627 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2628 afId.count == 0) {
2629 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2630 if (res != OK) return res;
2631 }
2632
2633 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2634 // if none already exists
2635 camera_metadata_entry pcTrigger =
2636 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2637 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2638 if (pcTrigger.count > 0 &&
2639 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2640 pcId.count == 0) {
2641 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2642 &dummyTriggerId, 1);
2643 if (res != OK) return res;
2644 }
2645
2646 return OK;
2647}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002648
2649
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002651 * Static callback forwarding methods from HAL to instance
2652 */
2653
2654void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2655 const camera3_capture_result *result) {
2656 Camera3Device *d =
2657 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2658 d->processCaptureResult(result);
2659}
2660
2661void Camera3Device::sNotify(const camera3_callback_ops *cb,
2662 const camera3_notify_msg *msg) {
2663 Camera3Device *d =
2664 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2665 d->notify(msg);
2666}
2667
2668}; // namespace android