blob: 25e97f3dc02c906111edbb4848ba22f1a1f8e3ab [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
Jianing Weicb0652e2014-03-12 18:29:36 -0700402 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700403 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
404 it != metadataList.end(); ++it) {
405 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
406 if (newRequest == 0) {
407 CLOGE("Can't create capture request");
408 return BAD_VALUE;
409 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700410
411 // Setup burst Id and request Id
412 newRequest->mResultExtras.burstId = burstId++;
413 if (it->exists(ANDROID_REQUEST_ID)) {
414 if (it->find(ANDROID_REQUEST_ID).count == 0) {
415 CLOGE("RequestID entry exists; but must not be empty in metadata");
416 return BAD_VALUE;
417 }
418 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
419 } else {
420 CLOGE("RequestID does not exist in metadata");
421 return BAD_VALUE;
422 }
423
Jianing Wei90e59c92014-03-12 18:29:36 -0700424 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700425
426 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700427 }
428 return OK;
429}
430
Jianing Weicb0652e2014-03-12 18:29:36 -0700431status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800432 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800433
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700434 List<const CameraMetadata> requests;
435 requests.push_back(request);
436 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800437}
438
Jianing Wei90e59c92014-03-12 18:29:36 -0700439status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700440 const List<const CameraMetadata> &requests, bool repeating,
441 /*out*/
442 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700443 ATRACE_CALL();
444 Mutex::Autolock il(mInterfaceLock);
445 Mutex::Autolock l(mLock);
446
447 status_t res = checkStatusOkToCaptureLocked();
448 if (res != OK) {
449 // error logged by previous call
450 return res;
451 }
452
453 RequestList requestList;
454
455 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
456 if (res != OK) {
457 // error logged by previous call
458 return res;
459 }
460
461 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700462 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700463 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700464 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700465 }
466
467 if (res == OK) {
468 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
469 if (res != OK) {
470 SET_ERR_L("Can't transition to active in %f seconds!",
471 kActiveTimeout/1e9);
472 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700473 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
474 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700475 } else {
476 CLOGE("Cannot queue request. Impossible.");
477 return BAD_VALUE;
478 }
479
480 return res;
481}
482
Jianing Weicb0652e2014-03-12 18:29:36 -0700483status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
484 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700485 ATRACE_CALL();
486
Jianing Weicb0652e2014-03-12 18:29:36 -0700487 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700488}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800489
Jianing Weicb0652e2014-03-12 18:29:36 -0700490status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
491 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800492 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800493
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700494 List<const CameraMetadata> requests;
495 requests.push_back(request);
496 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800497}
498
Jianing Weicb0652e2014-03-12 18:29:36 -0700499status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
500 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700501 ATRACE_CALL();
502
Jianing Weicb0652e2014-03-12 18:29:36 -0700503 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700504}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800505
506sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
507 const CameraMetadata &request) {
508 status_t res;
509
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700510 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800511 res = configureStreamsLocked();
512 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700513 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800514 return NULL;
515 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700516 if (mStatus == STATUS_UNCONFIGURED) {
517 CLOGE("No streams configured");
518 return NULL;
519 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800520 }
521
522 sp<CaptureRequest> newRequest = createCaptureRequest(request);
523 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524}
525
Jianing Weicb0652e2014-03-12 18:29:36 -0700526status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800527 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700528 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800529 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800531 switch (mStatus) {
532 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700533 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800534 return INVALID_OPERATION;
535 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700536 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800537 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 case STATUS_UNCONFIGURED:
539 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 case STATUS_ACTIVE:
541 // OK
542 break;
543 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800545 return INVALID_OPERATION;
546 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700547 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700548
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700549 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800550}
551
552status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
553 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700554 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800555
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700556 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800557}
558
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700559status_t Camera3Device::createInputStream(
560 uint32_t width, uint32_t height, int format, int *id) {
561 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700562 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700563 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
565 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700566
567 status_t res;
568 bool wasActive = false;
569
570 switch (mStatus) {
571 case STATUS_ERROR:
572 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
573 return INVALID_OPERATION;
574 case STATUS_UNINITIALIZED:
575 ALOGE("%s: Device not initialized", __FUNCTION__);
576 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700577 case STATUS_UNCONFIGURED:
578 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700579 // OK
580 break;
581 case STATUS_ACTIVE:
582 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700583 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700584 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700585 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700586 return res;
587 }
588 wasActive = true;
589 break;
590 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700591 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700592 return INVALID_OPERATION;
593 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700595
596 if (mInputStream != 0) {
597 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
598 return INVALID_OPERATION;
599 }
600
601 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
602 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700603 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700604
605 mInputStream = newStream;
606
607 *id = mNextStreamId++;
608
609 // Continue captures if active at start
610 if (wasActive) {
611 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
612 res = configureStreamsLocked();
613 if (res != OK) {
614 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
615 __FUNCTION__, mNextStreamId, strerror(-res), res);
616 return res;
617 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700618 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700619 }
620
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700621 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700622 return OK;
623}
624
Igor Murashkin2fba5842013-04-22 14:03:54 -0700625
626status_t Camera3Device::createZslStream(
627 uint32_t width, uint32_t height,
628 int depth,
629 /*out*/
630 int *id,
631 sp<Camera3ZslStream>* zslStream) {
632 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700633 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700634 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700635 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
636 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700637
638 status_t res;
639 bool wasActive = false;
640
641 switch (mStatus) {
642 case STATUS_ERROR:
643 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
644 return INVALID_OPERATION;
645 case STATUS_UNINITIALIZED:
646 ALOGE("%s: Device not initialized", __FUNCTION__);
647 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648 case STATUS_UNCONFIGURED:
649 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700650 // OK
651 break;
652 case STATUS_ACTIVE:
653 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700655 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700657 return res;
658 }
659 wasActive = true;
660 break;
661 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700663 return INVALID_OPERATION;
664 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700666
667 if (mInputStream != 0) {
668 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
669 return INVALID_OPERATION;
670 }
671
672 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
673 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700674 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700675
676 res = mOutputStreams.add(mNextStreamId, newStream);
677 if (res < 0) {
678 ALOGE("%s: Can't add new stream to set: %s (%d)",
679 __FUNCTION__, strerror(-res), res);
680 return res;
681 }
682 mInputStream = newStream;
683
684 *id = mNextStreamId++;
685 *zslStream = newStream;
686
687 // Continue captures if active at start
688 if (wasActive) {
689 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
690 res = configureStreamsLocked();
691 if (res != OK) {
692 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
693 __FUNCTION__, mNextStreamId, strerror(-res), res);
694 return res;
695 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700696 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700697 }
698
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700699 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700700 return OK;
701}
702
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800703status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
704 uint32_t width, uint32_t height, int format, size_t size, int *id) {
705 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700706 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 Mutex::Autolock l(mLock);
Colin Crosse5729fa2014-03-21 15:04:25 -0700708 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu",
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800710
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800711 status_t res;
712 bool wasActive = false;
713
714 switch (mStatus) {
715 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700716 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800717 return INVALID_OPERATION;
718 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700719 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800720 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 case STATUS_UNCONFIGURED:
722 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800723 // OK
724 break;
725 case STATUS_ACTIVE:
726 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700727 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700729 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730 return res;
731 }
732 wasActive = true;
733 break;
734 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700735 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800736 return INVALID_OPERATION;
737 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700738 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800739
740 sp<Camera3OutputStream> newStream;
741 if (format == HAL_PIXEL_FORMAT_BLOB) {
742 newStream = new Camera3OutputStream(mNextStreamId, consumer,
743 width, height, size, format);
744 } else {
745 newStream = new Camera3OutputStream(mNextStreamId, consumer,
746 width, height, format);
747 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749
750 res = mOutputStreams.add(mNextStreamId, newStream);
751 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700752 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 return res;
754 }
755
756 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700757 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800758
759 // Continue captures if active at start
760 if (wasActive) {
761 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
762 res = configureStreamsLocked();
763 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700764 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
765 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800766 return res;
767 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700770 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800771 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800772}
773
774status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
775 ATRACE_CALL();
776 (void)outputId; (void)id;
777
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700778 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800779 return INVALID_OPERATION;
780}
781
782
783status_t Camera3Device::getStreamInfo(int id,
784 uint32_t *width, uint32_t *height, uint32_t *format) {
785 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700786 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800787 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800788
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 switch (mStatus) {
790 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700791 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 return INVALID_OPERATION;
793 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700794 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700796 case STATUS_UNCONFIGURED:
797 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 case STATUS_ACTIVE:
799 // OK
800 break;
801 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 }
805
806 ssize_t idx = mOutputStreams.indexOfKey(id);
807 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700808 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 return idx;
810 }
811
812 if (width) *width = mOutputStreams[idx]->getWidth();
813 if (height) *height = mOutputStreams[idx]->getHeight();
814 if (format) *format = mOutputStreams[idx]->getFormat();
815
816 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817}
818
819status_t Camera3Device::setStreamTransform(int id,
820 int transform) {
821 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800824
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 switch (mStatus) {
826 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700827 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800828 return INVALID_OPERATION;
829 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700830 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 case STATUS_UNCONFIGURED:
833 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 case STATUS_ACTIVE:
835 // OK
836 break;
837 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return INVALID_OPERATION;
840 }
841
842 ssize_t idx = mOutputStreams.indexOfKey(id);
843 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700844 CLOGE("Stream %d does not exist",
845 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 return BAD_VALUE;
847 }
848
849 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800850}
851
852status_t Camera3Device::deleteStream(int id) {
853 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 Mutex::Autolock l(mLock);
856 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800857
Igor Murashkine2172be2013-05-28 15:31:39 -0700858 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
859
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860 // CameraDevice semantics require device to already be idle before
861 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700862 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700863 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
864 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800865 }
866
Igor Murashkin2fba5842013-04-22 14:03:54 -0700867 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800868 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869 if (mInputStream != NULL && id == mInputStream->getId()) {
870 deletedStream = mInputStream;
871 mInputStream.clear();
872 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800873 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700874 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 return BAD_VALUE;
876 }
Zhijun He5f446352014-01-22 09:49:33 -0800877 }
878
879 // Delete output stream or the output part of a bi-directional stream.
880 if (outputStreamIdx != NAME_NOT_FOUND) {
881 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882 mOutputStreams.removeItem(id);
883 }
884
885 // Free up the stream endpoint so that it can be used by some other stream
886 res = deletedStream->disconnect();
887 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 // fall through since we want to still list the stream as deleted.
890 }
891 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700892 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893
894 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895}
896
897status_t Camera3Device::deleteReprocessStream(int id) {
898 ATRACE_CALL();
899 (void)id;
900
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902 return INVALID_OPERATION;
903}
904
905
906status_t Camera3Device::createDefaultRequest(int templateId,
907 CameraMetadata *request) {
908 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700909 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700910 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 Mutex::Autolock l(mLock);
912
913 switch (mStatus) {
914 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700915 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 return INVALID_OPERATION;
917 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700918 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 case STATUS_UNCONFIGURED:
921 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800922 case STATUS_ACTIVE:
923 // OK
924 break;
925 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700926 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 return INVALID_OPERATION;
928 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800929
930 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700931 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800932 rawRequest = mHal3Device->ops->construct_default_request_settings(
933 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700934 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 if (rawRequest == NULL) {
936 SET_ERR_L("HAL is unable to construct default settings for template %d",
937 templateId);
938 return DEAD_OBJECT;
939 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800940 *request = rawRequest;
941
942 return OK;
943}
944
945status_t Camera3Device::waitUntilDrained() {
946 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700947 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800949
Zhijun He69a37482014-03-23 18:44:49 -0700950 return waitUntilDrainedLocked();
951}
952
953status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 switch (mStatus) {
955 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700956 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800957 ALOGV("%s: Already idle", __FUNCTION__);
958 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700959 case STATUS_CONFIGURED:
960 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 case STATUS_ERROR:
962 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 break;
965 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700966 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800967 return INVALID_OPERATION;
968 }
969
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
971 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
972 return res;
973}
974
975// Pause to reconfigure
976status_t Camera3Device::internalPauseAndWaitLocked() {
977 mRequestThread->setPaused(true);
978 mPauseStateNotify = true;
979
980 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
981 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
982 if (res != OK) {
983 SET_ERR_L("Can't idle device in %f seconds!",
984 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800985 }
986
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700987 return res;
988}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700990// Resume after internalPauseAndWaitLocked
991status_t Camera3Device::internalResumeLocked() {
992 status_t res;
993
994 mRequestThread->setPaused(false);
995
996 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
997 if (res != OK) {
998 SET_ERR_L("Can't transition to active in %f seconds!",
999 kActiveTimeout/1e9);
1000 }
1001 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001003}
1004
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001005status_t Camera3Device::waitUntilStateThenRelock(bool active,
1006 nsecs_t timeout) {
1007 status_t res = OK;
1008 if (active == (mStatus == STATUS_ACTIVE)) {
1009 // Desired state already reached
1010 return res;
1011 }
1012
1013 bool stateSeen = false;
1014 do {
1015 mRecentStatusUpdates.clear();
1016
1017 res = mStatusChanged.waitRelative(mLock, timeout);
1018 if (res != OK) break;
1019
1020 // Check state change history during wait
1021 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1022 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1023 stateSeen = true;
1024 break;
1025 }
1026 }
1027 } while (!stateSeen);
1028
1029 return res;
1030}
1031
1032
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001033status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1034 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001035 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001036
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001037 if (listener != NULL && mListener != NULL) {
1038 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1039 }
1040 mListener = listener;
1041
1042 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001043}
1044
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001045bool Camera3Device::willNotify3A() {
1046 return false;
1047}
1048
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001049status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001050 status_t res;
1051 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001053 while (mResultQueue.empty()) {
1054 res = mResultSignal.waitRelative(mOutputLock, timeout);
1055 if (res == TIMED_OUT) {
1056 return res;
1057 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001058 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001059 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001060 return res;
1061 }
1062 }
1063 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001064}
1065
Jianing Weicb0652e2014-03-12 18:29:36 -07001066status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001067 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001068 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001069
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001070 if (mResultQueue.empty()) {
1071 return NOT_ENOUGH_DATA;
1072 }
1073
Jianing Weicb0652e2014-03-12 18:29:36 -07001074 if (frame == NULL) {
1075 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1076 return BAD_VALUE;
1077 }
1078
1079 CaptureResult &result = *(mResultQueue.begin());
1080 frame->mResultExtras = result.mResultExtras;
1081 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001082 mResultQueue.erase(mResultQueue.begin());
1083
1084 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001085}
1086
1087status_t Camera3Device::triggerAutofocus(uint32_t id) {
1088 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001089 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001090
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001091 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1092 // Mix-in this trigger into the next request and only the next request.
1093 RequestTrigger trigger[] = {
1094 {
1095 ANDROID_CONTROL_AF_TRIGGER,
1096 ANDROID_CONTROL_AF_TRIGGER_START
1097 },
1098 {
1099 ANDROID_CONTROL_AF_TRIGGER_ID,
1100 static_cast<int32_t>(id)
1101 },
1102 };
1103
1104 return mRequestThread->queueTrigger(trigger,
1105 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001106}
1107
1108status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1109 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001110 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001111
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001112 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1113 // Mix-in this trigger into the next request and only the next request.
1114 RequestTrigger trigger[] = {
1115 {
1116 ANDROID_CONTROL_AF_TRIGGER,
1117 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1118 },
1119 {
1120 ANDROID_CONTROL_AF_TRIGGER_ID,
1121 static_cast<int32_t>(id)
1122 },
1123 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001124
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001125 return mRequestThread->queueTrigger(trigger,
1126 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001127}
1128
1129status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1130 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001131 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001132
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001133 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1134 // Mix-in this trigger into the next request and only the next request.
1135 RequestTrigger trigger[] = {
1136 {
1137 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1138 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1139 },
1140 {
1141 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1142 static_cast<int32_t>(id)
1143 },
1144 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001145
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001146 return mRequestThread->queueTrigger(trigger,
1147 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001148}
1149
1150status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1151 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1152 ATRACE_CALL();
1153 (void)reprocessStreamId; (void)buffer; (void)listener;
1154
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001155 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156 return INVALID_OPERATION;
1157}
1158
Jianing Weicb0652e2014-03-12 18:29:36 -07001159status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001160 ATRACE_CALL();
1161 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001162 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001163 Mutex::Autolock l(mLock);
1164
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001165 mRequestThread->clear(/*out*/frameNumber);
Zhijun He491e3412013-12-27 10:57:44 -08001166 status_t res;
1167 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1168 res = mHal3Device->ops->flush(mHal3Device);
1169 } else {
Zhijun He69a37482014-03-23 18:44:49 -07001170 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001171 }
1172
1173 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001174}
1175
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001176/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001177 * Methods called by subclasses
1178 */
1179
1180void Camera3Device::notifyStatus(bool idle) {
1181 {
1182 // Need mLock to safely update state and synchronize to current
1183 // state of methods in flight.
1184 Mutex::Autolock l(mLock);
1185 // We can get various system-idle notices from the status tracker
1186 // while starting up. Only care about them if we've actually sent
1187 // in some requests recently.
1188 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1189 return;
1190 }
1191 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1192 idle ? "idle" : "active");
1193 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1194 mRecentStatusUpdates.add(mStatus);
1195 mStatusChanged.signal();
1196
1197 // Skip notifying listener if we're doing some user-transparent
1198 // state changes
1199 if (mPauseStateNotify) return;
1200 }
1201 NotificationListener *listener;
1202 {
1203 Mutex::Autolock l(mOutputLock);
1204 listener = mListener;
1205 }
1206 if (idle && listener != NULL) {
1207 listener->notifyIdle();
1208 }
1209}
1210
1211/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 * Camera3Device private methods
1213 */
1214
1215sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1216 const CameraMetadata &request) {
1217 ATRACE_CALL();
1218 status_t res;
1219
1220 sp<CaptureRequest> newRequest = new CaptureRequest;
1221 newRequest->mSettings = request;
1222
1223 camera_metadata_entry_t inputStreams =
1224 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1225 if (inputStreams.count > 0) {
1226 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001227 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001228 CLOGE("Request references unknown input stream %d",
1229 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 return NULL;
1231 }
1232 // Lazy completion of stream configuration (allocation/registration)
1233 // on first use
1234 if (mInputStream->isConfiguring()) {
1235 res = mInputStream->finishConfiguration(mHal3Device);
1236 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001237 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001239 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 return NULL;
1241 }
1242 }
1243
1244 newRequest->mInputStream = mInputStream;
1245 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1246 }
1247
1248 camera_metadata_entry_t streams =
1249 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1250 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001251 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 return NULL;
1253 }
1254
1255 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001256 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001258 CLOGE("Request references unknown stream %d",
1259 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001260 return NULL;
1261 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001262 sp<Camera3OutputStreamInterface> stream =
1263 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264
1265 // Lazy completion of stream configuration (allocation/registration)
1266 // on first use
1267 if (stream->isConfiguring()) {
1268 res = stream->finishConfiguration(mHal3Device);
1269 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1271 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001272 return NULL;
1273 }
1274 }
1275
1276 newRequest->mOutputStreams.push(stream);
1277 }
1278 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1279
1280 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001281}
1282
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001283status_t Camera3Device::configureStreamsLocked() {
1284 ATRACE_CALL();
1285 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001288 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001289 return INVALID_OPERATION;
1290 }
1291
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001292 if (!mNeedConfig) {
1293 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1294 return OK;
1295 }
1296
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001297 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001298 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299
1300 camera3_stream_configuration config;
1301
1302 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1303
1304 Vector<camera3_stream_t*> streams;
1305 streams.setCapacity(config.num_streams);
1306
1307 if (mInputStream != NULL) {
1308 camera3_stream_t *inputStream;
1309 inputStream = mInputStream->startConfiguration();
1310 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001311 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 return INVALID_OPERATION;
1313 }
1314 streams.add(inputStream);
1315 }
1316
1317 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001318
1319 // Don't configure bidi streams twice, nor add them twice to the list
1320 if (mOutputStreams[i].get() ==
1321 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1322
1323 config.num_streams--;
1324 continue;
1325 }
1326
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001327 camera3_stream_t *outputStream;
1328 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1329 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001330 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 return INVALID_OPERATION;
1332 }
1333 streams.add(outputStream);
1334 }
1335
1336 config.streams = streams.editArray();
1337
1338 // Do the HAL configuration; will potentially touch stream
1339 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001340 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001342 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001343
1344 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001345 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1346 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 return res;
1348 }
1349
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001350 // Finish all stream configuration immediately.
1351 // TODO: Try to relax this later back to lazy completion, which should be
1352 // faster
1353
Igor Murashkin073f8572013-05-02 14:59:28 -07001354 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001355 res = mInputStream->finishConfiguration(mHal3Device);
1356 if (res != OK) {
1357 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1358 mInputStream->getId(), strerror(-res), res);
1359 return res;
1360 }
1361 }
1362
1363 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001364 sp<Camera3OutputStreamInterface> outputStream =
1365 mOutputStreams.editValueAt(i);
1366 if (outputStream->isConfiguring()) {
1367 res = outputStream->finishConfiguration(mHal3Device);
1368 if (res != OK) {
1369 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1370 outputStream->getId(), strerror(-res), res);
1371 return res;
1372 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001373 }
1374 }
1375
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 // Request thread needs to know to avoid using repeat-last-settings protocol
1377 // across configure_streams() calls
1378 mRequestThread->configurationComplete();
1379
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001380 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001382 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001384 if (config.num_streams > 0) {
1385 mStatus = STATUS_CONFIGURED;
1386 } else {
1387 mStatus = STATUS_UNCONFIGURED;
1388 }
1389
1390 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1391
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001392 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393}
1394
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001395void Camera3Device::setErrorState(const char *fmt, ...) {
1396 Mutex::Autolock l(mLock);
1397 va_list args;
1398 va_start(args, fmt);
1399
1400 setErrorStateLockedV(fmt, args);
1401
1402 va_end(args);
1403}
1404
1405void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1406 Mutex::Autolock l(mLock);
1407 setErrorStateLockedV(fmt, args);
1408}
1409
1410void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1411 va_list args;
1412 va_start(args, fmt);
1413
1414 setErrorStateLockedV(fmt, args);
1415
1416 va_end(args);
1417}
1418
1419void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001420 // Print out all error messages to log
1421 String8 errorCause = String8::formatV(fmt, args);
1422 ALOGE("Camera %d: %s", mId, errorCause.string());
1423
1424 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001425 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001426
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001427 // Save stack trace. View by dumping it later.
1428 CameraTraces::saveTrace();
1429 // TODO: consider adding errorCause and client pid/procname
1430
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001431 mErrorCause = errorCause;
1432
1433 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001434 mStatus = STATUS_ERROR;
1435}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436
1437/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001438 * In-flight request management
1439 */
1440
Jianing Weicb0652e2014-03-12 18:29:36 -07001441status_t Camera3Device::registerInFlight(uint32_t frameNumber,
1442 int32_t numBuffers, CaptureResultExtras resultExtras) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001443 ATRACE_CALL();
1444 Mutex::Autolock l(mInFlightLock);
1445
1446 ssize_t res;
Jianing Weicb0652e2014-03-12 18:29:36 -07001447 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001448 if (res < 0) return res;
1449
1450 return OK;
1451}
1452
1453/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001454 * QUIRK(partial results)
1455 * Check if all 3A fields are ready, and send off a partial 3A-only result
1456 * to the output frame queue
1457 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001458bool Camera3Device::processPartial3AQuirk(
Jianing Weicb0652e2014-03-12 18:29:36 -07001459 uint32_t frameNumber,
1460 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001461
1462 // Check if all 3A states are present
1463 // The full list of fields is
1464 // android.control.afMode
1465 // android.control.awbMode
1466 // android.control.aeState
1467 // android.control.awbState
1468 // android.control.afState
1469 // android.control.afTriggerID
1470 // android.control.aePrecaptureID
1471 // TODO: Add android.control.aeMode
1472
1473 bool gotAllStates = true;
1474
1475 uint8_t afMode;
1476 uint8_t awbMode;
1477 uint8_t aeState;
1478 uint8_t afState;
1479 uint8_t awbState;
1480 int32_t afTriggerId;
1481 int32_t aeTriggerId;
1482
1483 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1484 &afMode, frameNumber);
1485
1486 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1487 &awbMode, frameNumber);
1488
1489 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1490 &aeState, frameNumber);
1491
1492 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1493 &afState, frameNumber);
1494
1495 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1496 &awbState, frameNumber);
1497
1498 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1499 &afTriggerId, frameNumber);
1500
1501 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1502 &aeTriggerId, frameNumber);
1503
1504 if (!gotAllStates) return false;
1505
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001506 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001507 "AF state %d, AE state %d, AWB state %d, "
1508 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001509 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001510 afMode, awbMode,
1511 afState, aeState, awbState,
1512 afTriggerId, aeTriggerId);
1513
1514 // Got all states, so construct a minimal result to send
1515 // In addition to the above fields, this means adding in
1516 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001517 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001518 // android.quirks.partialResult
1519
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001520 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001521
1522 Mutex::Autolock l(mOutputLock);
1523
Jianing Weicb0652e2014-03-12 18:29:36 -07001524 CaptureResult captureResult;
1525 captureResult.mResultExtras = resultExtras;
1526 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1527 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1528 // but not limited to CameraDeviceBase::getNextResult
1529 CaptureResult& min3AResult =
1530 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001531
Jianing Weicb0652e2014-03-12 18:29:36 -07001532 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1533 // TODO: This is problematic casting. Need to fix CameraMetadata.
1534 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001535 return false;
1536 }
1537
Jianing Weicb0652e2014-03-12 18:29:36 -07001538 int32_t requestId = resultExtras.requestId;
1539 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001540 &requestId, frameNumber)) {
1541 return false;
1542 }
1543
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001544 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
Jianing Weicb0652e2014-03-12 18:29:36 -07001545 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001546 &partialResult, frameNumber)) {
1547 return false;
1548 }
1549
Jianing Weicb0652e2014-03-12 18:29:36 -07001550 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001551 &afMode, frameNumber)) {
1552 return false;
1553 }
1554
Jianing Weicb0652e2014-03-12 18:29:36 -07001555 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001556 &awbMode, frameNumber)) {
1557 return false;
1558 }
1559
Jianing Weicb0652e2014-03-12 18:29:36 -07001560 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001561 &aeState, frameNumber)) {
1562 return false;
1563 }
1564
Jianing Weicb0652e2014-03-12 18:29:36 -07001565 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001566 &afState, frameNumber)) {
1567 return false;
1568 }
1569
Jianing Weicb0652e2014-03-12 18:29:36 -07001570 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001571 &awbState, frameNumber)) {
1572 return false;
1573 }
1574
Jianing Weicb0652e2014-03-12 18:29:36 -07001575 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001576 &afTriggerId, frameNumber)) {
1577 return false;
1578 }
1579
Jianing Weicb0652e2014-03-12 18:29:36 -07001580 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001581 &aeTriggerId, frameNumber)) {
1582 return false;
1583 }
1584
1585 mResultSignal.signal();
1586
1587 return true;
1588}
1589
1590template<typename T>
1591bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001592 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001593 (void) frameNumber;
1594
1595 camera_metadata_ro_entry_t entry;
1596
1597 entry = result.find(tag);
1598 if (entry.count == 0) {
1599 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1600 mId, frameNumber, get_camera_metadata_tag_name(tag));
1601 return false;
1602 }
1603
1604 if (sizeof(T) == sizeof(uint8_t)) {
1605 *value = entry.data.u8[0];
1606 } else if (sizeof(T) == sizeof(int32_t)) {
1607 *value = entry.data.i32[0];
1608 } else {
1609 ALOGE("%s: Unexpected type", __FUNCTION__);
1610 return false;
1611 }
1612 return true;
1613}
1614
1615template<typename T>
1616bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001617 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001618 if (result.update(tag, value, 1) != NO_ERROR) {
1619 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1620 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1621 frameNumber, get_camera_metadata_tag_name(tag));
1622 return false;
1623 }
1624 return true;
1625}
1626
1627/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 * Camera HAL device callback methods
1629 */
1630
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001631void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001632 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001633
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001634 status_t res;
1635
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001636 uint32_t frameNumber = result->frame_number;
1637 if (result->result == NULL && result->num_output_buffers == 0) {
1638 SET_ERR("No result data provided by HAL for frame %d",
1639 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001640 return;
1641 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001642 bool partialResultQuirk = false;
1643 CameraMetadata collectedQuirkResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001644 CaptureResultExtras resultExtras;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001645
Jianing Weicb0652e2014-03-12 18:29:36 -07001646 // Get capture timestamp and resultExtras from list of in-flight requests,
1647 // where it was added by the shutter notification for this frame.
1648 // Then update the in-flight status and remove the in-flight entry if
1649 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001650 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001651 {
1652 Mutex::Autolock l(mInFlightLock);
1653 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1654 if (idx == NAME_NOT_FOUND) {
1655 SET_ERR("Unknown frame number for capture result: %d",
1656 frameNumber);
1657 return;
1658 }
1659 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001660 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1661 ", burstId = %" PRId32,
1662 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1663 request.resultExtras.burstId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001664
1665 // Check if this result carries only partial metadata
1666 if (mUsePartialResultQuirk && result->result != NULL) {
1667 camera_metadata_ro_entry_t partialResultEntry;
1668 res = find_camera_metadata_ro_entry(result->result,
1669 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1670 if (res != NAME_NOT_FOUND &&
1671 partialResultEntry.count > 0 &&
1672 partialResultEntry.data.u8[0] ==
1673 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1674 // A partial result. Flag this as such, and collect this
1675 // set of metadata into the in-flight entry.
1676 partialResultQuirk = true;
1677 request.partialResultQuirk.collectedResult.append(
1678 result->result);
1679 request.partialResultQuirk.collectedResult.erase(
1680 ANDROID_QUIRKS_PARTIAL_RESULT);
1681 // Fire off a 3A-only result if possible
1682 if (!request.partialResultQuirk.haveSent3A) {
1683 request.partialResultQuirk.haveSent3A =
1684 processPartial3AQuirk(frameNumber,
Jianing Weicb0652e2014-03-12 18:29:36 -07001685 request.partialResultQuirk.collectedResult,
1686 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001687 }
1688 }
1689 }
1690
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001691 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001692 resultExtras = request.resultExtras;
Jianing Weicb0652e2014-03-12 18:29:36 -07001693
Zhijun He1d1f8462013-10-02 16:29:51 -07001694 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001695 * One of the following must happen before it's legal to call process_capture_result,
1696 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001697 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1698 * - CAMERA3_MSG_ERROR (expected during flush)
1699 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001700 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001701 SET_ERR("Called before shutter notify for frame %d",
1702 frameNumber);
1703 return;
1704 }
1705
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001706 // Did we get the (final) result metadata for this capture?
1707 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001708 if (request.haveResultMetadata) {
1709 SET_ERR("Called multiple times with metadata for frame %d",
1710 frameNumber);
1711 return;
1712 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001713 if (mUsePartialResultQuirk &&
1714 !request.partialResultQuirk.collectedResult.isEmpty()) {
1715 collectedQuirkResult.acquire(
1716 request.partialResultQuirk.collectedResult);
1717 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001718 request.haveResultMetadata = true;
1719 }
1720
1721 request.numBuffersLeft -= result->num_output_buffers;
1722
1723 if (request.numBuffersLeft < 0) {
1724 SET_ERR("Too many buffers returned for frame %d",
1725 frameNumber);
1726 return;
1727 }
1728
Zhijun He1b05dfc2013-11-21 12:57:51 -08001729 // Check if everything has arrived for this result (buffers and metadata), remove it from
1730 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1731 if ((request.requestStatus != OK) ||
1732 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001733 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001734 mInFlightMap.removeItemsAt(idx, 1);
1735 }
1736
1737 // Sanity check - if we have too many in-flight frames, something has
1738 // likely gone wrong
1739 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001740 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001741 }
1742
1743 }
1744
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001745 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001746 bool gotResult = false;
1747 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001748 Mutex::Autolock l(mOutputLock);
1749
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001750 gotResult = true;
1751
Jianing Wei3c76fa32014-04-21 11:34:34 -07001752 // TODO: need to track errors for tighter bounds on expected frame number
1753 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001754 SET_ERR("Out-of-order capture result metadata submitted! "
1755 "(got frame number %d, expecting %d)",
1756 frameNumber, mNextResultFrameNumber);
1757 return;
1758 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001759 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001760
Jianing Weicb0652e2014-03-12 18:29:36 -07001761 CaptureResult captureResult;
1762 captureResult.mResultExtras = resultExtras;
1763 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001764
Jianing Weicb0652e2014-03-12 18:29:36 -07001765 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1766 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001767 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001768 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001769 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001770 } else {
1771 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001772 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001773 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001774
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001775 // Append any previous partials to form a complete result
1776 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001777 captureResult.mMetadata.append(collectedQuirkResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001778 }
1779
Jianing Weicb0652e2014-03-12 18:29:36 -07001780 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001781
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001782 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001783
1784 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07001785 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001786 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001787 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001788 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001789 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001790 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001791 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001792 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001793 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001794 gotResult = false;
1795 }
1796
1797 if (gotResult) {
1798 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07001799 List<CaptureResult>::iterator queuedResult =
1800 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1801 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1802 ", burstId = %" PRId32, __FUNCTION__,
1803 queuedResult->mResultExtras.requestId,
1804 queuedResult->mResultExtras.frameNumber,
1805 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001806 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001807 } // scope for mOutputLock
1808
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001809 // Return completed buffers to their streams with the timestamp
1810
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001811 for (size_t i = 0; i < result->num_output_buffers; i++) {
1812 Camera3Stream *stream =
1813 Camera3Stream::cast(result->output_buffers[i].stream);
1814 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1815 // Note: stream may be deallocated at this point, if this buffer was the
1816 // last reference to it.
1817 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001818 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001819 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001820 }
1821 }
1822
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001823 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001824
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001825 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001826 mResultSignal.signal();
1827 }
1828
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001829}
1830
1831void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001832 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001833 NotificationListener *listener;
1834 {
1835 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001836 listener = mListener;
1837 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001838
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001839 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001840 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001841 return;
1842 }
1843
1844 switch (msg->type) {
1845 case CAMERA3_MSG_ERROR: {
1846 int streamId = 0;
1847 if (msg->message.error.error_stream != NULL) {
1848 Camera3Stream *stream =
1849 Camera3Stream::cast(
1850 msg->message.error.error_stream);
1851 streamId = stream->getId();
1852 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001853 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1854 mId, __FUNCTION__, msg->message.error.frame_number,
1855 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001856
Jianing Weicb0652e2014-03-12 18:29:36 -07001857 CaptureResultExtras resultExtras;
Zhijun He1d1f8462013-10-02 16:29:51 -07001858 // Set request error status for the request in the in-flight tracking
1859 {
1860 Mutex::Autolock l(mInFlightLock);
1861 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1862 if (idx >= 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001863 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1864 r.requestStatus = msg->message.error.error_code;
1865 resultExtras = r.resultExtras;
1866 } else {
1867 resultExtras.frameNumber = msg->message.error.frame_number;
1868 ALOGE("Camera %d: %s: cannot find in-flight request on frame %" PRId64
1869 " error", mId, __FUNCTION__, resultExtras.frameNumber);
Zhijun He1d1f8462013-10-02 16:29:51 -07001870 }
1871 }
1872
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001873 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001874 if (msg->message.error.error_code == CAMERA3_MSG_ERROR_DEVICE) {
1875 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1876 resultExtras);
1877 } else {
1878 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE,
1879 resultExtras);
1880 }
1881 } else {
1882 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001883 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001884 break;
1885 }
1886 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001887 ssize_t idx;
1888 uint32_t frameNumber = msg->message.shutter.frame_number;
1889 nsecs_t timestamp = msg->message.shutter.timestamp;
1890 // Verify ordering of shutter notifications
1891 {
1892 Mutex::Autolock l(mOutputLock);
Jianing Wei3c76fa32014-04-21 11:34:34 -07001893 // TODO: need to track errors for tighter bounds on expected frame number.
1894 if (frameNumber < mNextShutterFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001895 SET_ERR("Shutter notification out-of-order. Expected "
1896 "notification for frame %d, got frame %d",
1897 mNextShutterFrameNumber, frameNumber);
1898 break;
1899 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001900 mNextShutterFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001901 }
1902
Jianing Weicb0652e2014-03-12 18:29:36 -07001903 CaptureResultExtras resultExtras;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001904
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001905 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001906 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001907 {
1908 Mutex::Autolock l(mInFlightLock);
1909 idx = mInFlightMap.indexOfKey(frameNumber);
1910 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001911 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1912 r.captureTimestamp = timestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001913 resultExtras = r.resultExtras;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001914 }
1915 }
1916 if (idx < 0) {
1917 SET_ERR("Shutter notification for non-existent frame number %d",
1918 frameNumber);
1919 break;
1920 }
Colin Crosse5729fa2014-03-21 15:04:25 -07001921 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Jianing Weicb0652e2014-03-12 18:29:36 -07001922 mId, __FUNCTION__, frameNumber, resultExtras.requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001923 // Call listener, if any
1924 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 listener->notifyShutter(resultExtras, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001926 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001927 break;
1928 }
1929 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001930 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001931 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001932 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001933}
1934
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001935CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001936 ALOGV("%s", __FUNCTION__);
1937
Igor Murashkin1e479c02013-09-06 16:55:14 -07001938 CameraMetadata retVal;
1939
1940 if (mRequestThread != NULL) {
1941 retVal = mRequestThread->getLatestRequest();
1942 }
1943
Igor Murashkin1e479c02013-09-06 16:55:14 -07001944 return retVal;
1945}
1946
Jianing Weicb0652e2014-03-12 18:29:36 -07001947
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001948/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001949 * RequestThread inner class methods
1950 */
1951
1952Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001953 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954 camera3_device_t *hal3Device) :
1955 Thread(false),
1956 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001957 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001958 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001959 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001960 mReconfigured(false),
1961 mDoPause(false),
1962 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001963 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07001964 mLatestRequestId(NAME_NOT_FOUND),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001965 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001966 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001967}
1968
1969void Camera3Device::RequestThread::configurationComplete() {
1970 Mutex::Autolock l(mRequestLock);
1971 mReconfigured = true;
1972}
1973
Jianing Wei90e59c92014-03-12 18:29:36 -07001974status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001975 List<sp<CaptureRequest> > &requests,
1976 /*out*/
1977 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001978 Mutex::Autolock l(mRequestLock);
1979 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
1980 ++it) {
1981 mRequestQueue.push_back(*it);
1982 }
1983
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001984 if (lastFrameNumber != NULL) {
1985 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
1986 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
1987 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
1988 *lastFrameNumber);
1989 }
Jianing Weicb0652e2014-03-12 18:29:36 -07001990
Jianing Wei90e59c92014-03-12 18:29:36 -07001991 unpauseForNewRequests();
1992
1993 return OK;
1994}
1995
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001996
1997status_t Camera3Device::RequestThread::queueTrigger(
1998 RequestTrigger trigger[],
1999 size_t count) {
2000
2001 Mutex::Autolock l(mTriggerMutex);
2002 status_t ret;
2003
2004 for (size_t i = 0; i < count; ++i) {
2005 ret = queueTriggerLocked(trigger[i]);
2006
2007 if (ret != OK) {
2008 return ret;
2009 }
2010 }
2011
2012 return OK;
2013}
2014
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002015int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2016 sp<Camera3Device> d = device.promote();
2017 if (d != NULL) return d->mId;
2018 return 0;
2019}
2020
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002021status_t Camera3Device::RequestThread::queueTriggerLocked(
2022 RequestTrigger trigger) {
2023
2024 uint32_t tag = trigger.metadataTag;
2025 ssize_t index = mTriggerMap.indexOfKey(tag);
2026
2027 switch (trigger.getTagType()) {
2028 case TYPE_BYTE:
2029 // fall-through
2030 case TYPE_INT32:
2031 break;
2032 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002033 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2034 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002035 return INVALID_OPERATION;
2036 }
2037
2038 /**
2039 * Collect only the latest trigger, since we only have 1 field
2040 * in the request settings per trigger tag, and can't send more than 1
2041 * trigger per request.
2042 */
2043 if (index != NAME_NOT_FOUND) {
2044 mTriggerMap.editValueAt(index) = trigger;
2045 } else {
2046 mTriggerMap.add(tag, trigger);
2047 }
2048
2049 return OK;
2050}
2051
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002052status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002053 const RequestList &requests,
2054 /*out*/
2055 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002056 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002057 if (lastFrameNumber != NULL) {
2058 *lastFrameNumber = mRepeatingLastFrameNumber;
2059 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002060 mRepeatingRequests.clear();
2061 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2062 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002063
2064 unpauseForNewRequests();
2065
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002066 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002067 return OK;
2068}
2069
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002070status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002071 Mutex::Autolock l(mRequestLock);
2072 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002073 if (lastFrameNumber != NULL) {
2074 *lastFrameNumber = mRepeatingLastFrameNumber;
2075 }
2076 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002077 return OK;
2078}
2079
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002080status_t Camera3Device::RequestThread::clear(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002081 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002082 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002083 mRepeatingRequests.clear();
2084 mRequestQueue.clear();
2085 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002086 if (lastFrameNumber != NULL) {
2087 *lastFrameNumber = mRepeatingLastFrameNumber;
2088 }
2089 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002090 return OK;
2091}
2092
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093void Camera3Device::RequestThread::setPaused(bool paused) {
2094 Mutex::Autolock l(mPauseLock);
2095 mDoPause = paused;
2096 mDoPauseSignal.signal();
2097}
2098
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002099status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2100 int32_t requestId, nsecs_t timeout) {
2101 Mutex::Autolock l(mLatestRequestMutex);
2102 status_t res;
2103 while (mLatestRequestId != requestId) {
2104 nsecs_t startTime = systemTime();
2105
2106 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2107 if (res != OK) return res;
2108
2109 timeout -= (systemTime() - startTime);
2110 }
2111
2112 return OK;
2113}
2114
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002115void Camera3Device::RequestThread::requestExit() {
2116 // Call parent to set up shutdown
2117 Thread::requestExit();
2118 // The exit from any possible waits
2119 mDoPauseSignal.signal();
2120 mRequestSignal.signal();
2121}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002122
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002123bool Camera3Device::RequestThread::threadLoop() {
2124
2125 status_t res;
2126
2127 // Handle paused state.
2128 if (waitIfPaused()) {
2129 return true;
2130 }
2131
2132 // Get work to do
2133
2134 sp<CaptureRequest> nextRequest = waitForNextRequest();
2135 if (nextRequest == NULL) {
2136 return true;
2137 }
2138
2139 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002140 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002141 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002142 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002143
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002144 // Get the request ID, if any
2145 int requestId;
2146 camera_metadata_entry_t requestIdEntry =
2147 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2148 if (requestIdEntry.count > 0) {
2149 requestId = requestIdEntry.data.i32[0];
2150 } else {
2151 ALOGW("%s: Did not have android.request.id set in the request",
2152 __FUNCTION__);
2153 requestId = NAME_NOT_FOUND;
2154 }
2155
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002156 // Insert any queued triggers (before metadata is locked)
2157 int32_t triggerCount;
2158 res = insertTriggers(nextRequest);
2159 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002160 SET_ERR("RequestThread: Unable to insert triggers "
2161 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002162 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002163 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2164 return false;
2165 }
2166 triggerCount = res;
2167
2168 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2169
2170 // If the request is the same as last, or we had triggers last time
2171 if (mPrevRequest != nextRequest || triggersMixedIn) {
2172 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002173 * HAL workaround:
2174 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2175 */
2176 res = addDummyTriggerIds(nextRequest);
2177 if (res != OK) {
2178 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2179 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002180 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002181 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2182 return false;
2183 }
2184
2185 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002186 * The request should be presorted so accesses in HAL
2187 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2188 */
2189 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002190 request.settings = nextRequest->mSettings.getAndLock();
2191 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002192 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2193
2194 IF_ALOGV() {
2195 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2196 find_camera_metadata_ro_entry(
2197 request.settings,
2198 ANDROID_CONTROL_AF_TRIGGER,
2199 &e
2200 );
2201 if (e.count > 0) {
2202 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2203 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002204 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002205 e.data.u8[0]);
2206 }
2207 }
2208 } else {
2209 // leave request.settings NULL to indicate 'reuse latest given'
2210 ALOGVV("%s: Request settings are REUSED",
2211 __FUNCTION__);
2212 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213
2214 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002215
2216 // Fill in buffers
2217
2218 if (nextRequest->mInputStream != NULL) {
2219 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002220 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002221 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002222 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002223 " %s (%d)", strerror(-res), res);
2224 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2225 return true;
2226 }
2227 } else {
2228 request.input_buffer = NULL;
2229 }
2230
2231 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2232 nextRequest->mOutputStreams.size());
2233 request.output_buffers = outputBuffers.array();
2234 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2235 res = nextRequest->mOutputStreams.editItemAt(i)->
2236 getBuffer(&outputBuffers.editItemAt(i));
2237 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002238 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2239 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002240 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2241 return true;
2242 }
2243 request.num_output_buffers++;
2244 }
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
Jianing Weicb0652e2014-03-12 18:29:36 -07002254 res = parent->registerInFlight(request.frame_number,
2255 request.num_output_buffers, nextRequest->mResultExtras);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002256 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2257 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002258 __FUNCTION__,
2259 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2260 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002261 if (res != OK) {
2262 SET_ERR("RequestThread: Unable to register new in-flight request:"
2263 " %s (%d)", strerror(-res), res);
2264 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2265 return false;
2266 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002267
Zhijun Hecc27e112013-10-03 16:12:43 -07002268 // Inform waitUntilRequestProcessed thread of a new request ID
2269 {
2270 Mutex::Autolock al(mLatestRequestMutex);
2271
2272 mLatestRequestId = requestId;
2273 mLatestRequestSignal.signal();
2274 }
2275
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002276 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002277 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2278 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002279 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002280 ATRACE_END();
2281
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002282 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002283 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002284 " device: %s (%d)", request.frame_number, strerror(-res), res);
2285 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2286 return false;
2287 }
2288
Igor Murashkin1e479c02013-09-06 16:55:14 -07002289 // Update the latest request sent to HAL
2290 if (request.settings != NULL) { // Don't update them if they were unchanged
2291 Mutex::Autolock al(mLatestRequestMutex);
2292
2293 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2294 mLatestRequest.acquire(cloned);
2295 }
2296
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002297 if (request.settings != NULL) {
2298 nextRequest->mSettings.unlock(request.settings);
2299 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002300
2301 // Remove any previously queued triggers (after unlock)
2302 res = removeTriggers(mPrevRequest);
2303 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002304 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002305 "(capture request %d, HAL device: %s (%d)",
2306 request.frame_number, strerror(-res), res);
2307 return false;
2308 }
2309 mPrevTriggers = triggerCount;
2310
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002311 // Return input buffer back to framework
2312 if (request.input_buffer != NULL) {
2313 Camera3Stream *stream =
2314 Camera3Stream::cast(request.input_buffer->stream);
2315 res = stream->returnInputBuffer(*(request.input_buffer));
2316 // Note: stream may be deallocated at this point, if this buffer was the
2317 // last reference to it.
2318 if (res != OK) {
2319 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2320 " its stream:%s (%d)", __FUNCTION__,
2321 request.frame_number, strerror(-res), res);
2322 // TODO: Report error upstream
2323 }
2324 }
2325
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002326 return true;
2327}
2328
Igor Murashkin1e479c02013-09-06 16:55:14 -07002329CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2330 Mutex::Autolock al(mLatestRequestMutex);
2331
2332 ALOGV("RequestThread::%s", __FUNCTION__);
2333
2334 return mLatestRequest;
2335}
2336
Jianing Weicb0652e2014-03-12 18:29:36 -07002337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002338void Camera3Device::RequestThread::cleanUpFailedRequest(
2339 camera3_capture_request_t &request,
2340 sp<CaptureRequest> &nextRequest,
2341 Vector<camera3_stream_buffer_t> &outputBuffers) {
2342
2343 if (request.settings != NULL) {
2344 nextRequest->mSettings.unlock(request.settings);
2345 }
2346 if (request.input_buffer != NULL) {
2347 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002348 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349 }
2350 for (size_t i = 0; i < request.num_output_buffers; i++) {
2351 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2352 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2353 outputBuffers[i], 0);
2354 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002355}
2356
2357sp<Camera3Device::CaptureRequest>
2358 Camera3Device::RequestThread::waitForNextRequest() {
2359 status_t res;
2360 sp<CaptureRequest> nextRequest;
2361
2362 // Optimized a bit for the simple steady-state case (single repeating
2363 // request), to avoid putting that request in the queue temporarily.
2364 Mutex::Autolock l(mRequestLock);
2365
2366 while (mRequestQueue.empty()) {
2367 if (!mRepeatingRequests.empty()) {
2368 // Always atomically enqueue all requests in a repeating request
2369 // list. Guarantees a complete in-sequence set of captures to
2370 // application.
2371 const RequestList &requests = mRepeatingRequests;
2372 RequestList::const_iterator firstRequest =
2373 requests.begin();
2374 nextRequest = *firstRequest;
2375 mRequestQueue.insert(mRequestQueue.end(),
2376 ++firstRequest,
2377 requests.end());
2378 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002379
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002380 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002381
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382 break;
2383 }
2384
2385 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2386
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002387 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2388 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002389 Mutex::Autolock pl(mPauseLock);
2390 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002391 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002392 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002393 // Let the tracker know
2394 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2395 if (statusTracker != 0) {
2396 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2397 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002398 }
2399 // Stop waiting for now and let thread management happen
2400 return NULL;
2401 }
2402 }
2403
2404 if (nextRequest == NULL) {
2405 // Don't have a repeating request already in hand, so queue
2406 // must have an entry now.
2407 RequestList::iterator firstRequest =
2408 mRequestQueue.begin();
2409 nextRequest = *firstRequest;
2410 mRequestQueue.erase(firstRequest);
2411 }
2412
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002413 // In case we've been unpaused by setPaused clearing mDoPause, need to
2414 // update internal pause state (capture/setRepeatingRequest unpause
2415 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002416 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002417 if (mPaused) {
2418 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2419 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2420 if (statusTracker != 0) {
2421 statusTracker->markComponentActive(mStatusId);
2422 }
2423 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002424 mPaused = false;
2425
2426 // Check if we've reconfigured since last time, and reset the preview
2427 // request if so. Can't use 'NULL request == repeat' across configure calls.
2428 if (mReconfigured) {
2429 mPrevRequest.clear();
2430 mReconfigured = false;
2431 }
2432
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002433 if (nextRequest != NULL) {
2434 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
2435 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002436 return nextRequest;
2437}
2438
2439bool Camera3Device::RequestThread::waitIfPaused() {
2440 status_t res;
2441 Mutex::Autolock l(mPauseLock);
2442 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002443 if (mPaused == false) {
2444 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002445 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2446 // Let the tracker know
2447 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2448 if (statusTracker != 0) {
2449 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2450 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002451 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002452
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002454 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002455 return true;
2456 }
2457 }
2458 // We don't set mPaused to false here, because waitForNextRequest needs
2459 // to further manage the paused state in case of starvation.
2460 return false;
2461}
2462
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002463void Camera3Device::RequestThread::unpauseForNewRequests() {
2464 // With work to do, mark thread as unpaused.
2465 // If paused by request (setPaused), don't resume, to avoid
2466 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002467 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002468 Mutex::Autolock p(mPauseLock);
2469 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002470 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2471 if (mPaused) {
2472 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2473 if (statusTracker != 0) {
2474 statusTracker->markComponentActive(mStatusId);
2475 }
2476 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002477 mPaused = false;
2478 }
2479}
2480
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002481void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2482 sp<Camera3Device> parent = mParent.promote();
2483 if (parent != NULL) {
2484 va_list args;
2485 va_start(args, fmt);
2486
2487 parent->setErrorStateV(fmt, args);
2488
2489 va_end(args);
2490 }
2491}
2492
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002493status_t Camera3Device::RequestThread::insertTriggers(
2494 const sp<CaptureRequest> &request) {
2495
2496 Mutex::Autolock al(mTriggerMutex);
2497
2498 CameraMetadata &metadata = request->mSettings;
2499 size_t count = mTriggerMap.size();
2500
2501 for (size_t i = 0; i < count; ++i) {
2502 RequestTrigger trigger = mTriggerMap.valueAt(i);
2503
2504 uint32_t tag = trigger.metadataTag;
2505 camera_metadata_entry entry = metadata.find(tag);
2506
2507 if (entry.count > 0) {
2508 /**
2509 * Already has an entry for this trigger in the request.
2510 * Rewrite it with our requested trigger value.
2511 */
2512 RequestTrigger oldTrigger = trigger;
2513
2514 oldTrigger.entryValue = entry.data.u8[0];
2515
2516 mTriggerReplacedMap.add(tag, oldTrigger);
2517 } else {
2518 /**
2519 * More typical, no trigger entry, so we just add it
2520 */
2521 mTriggerRemovedMap.add(tag, trigger);
2522 }
2523
2524 status_t res;
2525
2526 switch (trigger.getTagType()) {
2527 case TYPE_BYTE: {
2528 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2529 res = metadata.update(tag,
2530 &entryValue,
2531 /*count*/1);
2532 break;
2533 }
2534 case TYPE_INT32:
2535 res = metadata.update(tag,
2536 &trigger.entryValue,
2537 /*count*/1);
2538 break;
2539 default:
2540 ALOGE("%s: Type not supported: 0x%x",
2541 __FUNCTION__,
2542 trigger.getTagType());
2543 return INVALID_OPERATION;
2544 }
2545
2546 if (res != OK) {
2547 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2548 ", value %d", __FUNCTION__, trigger.getTagName(),
2549 trigger.entryValue);
2550 return res;
2551 }
2552
2553 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2554 trigger.getTagName(),
2555 trigger.entryValue);
2556 }
2557
2558 mTriggerMap.clear();
2559
2560 return count;
2561}
2562
2563status_t Camera3Device::RequestThread::removeTriggers(
2564 const sp<CaptureRequest> &request) {
2565 Mutex::Autolock al(mTriggerMutex);
2566
2567 CameraMetadata &metadata = request->mSettings;
2568
2569 /**
2570 * Replace all old entries with their old values.
2571 */
2572 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2573 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2574
2575 status_t res;
2576
2577 uint32_t tag = trigger.metadataTag;
2578 switch (trigger.getTagType()) {
2579 case TYPE_BYTE: {
2580 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2581 res = metadata.update(tag,
2582 &entryValue,
2583 /*count*/1);
2584 break;
2585 }
2586 case TYPE_INT32:
2587 res = metadata.update(tag,
2588 &trigger.entryValue,
2589 /*count*/1);
2590 break;
2591 default:
2592 ALOGE("%s: Type not supported: 0x%x",
2593 __FUNCTION__,
2594 trigger.getTagType());
2595 return INVALID_OPERATION;
2596 }
2597
2598 if (res != OK) {
2599 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2600 ", trigger value %d", __FUNCTION__,
2601 trigger.getTagName(), trigger.entryValue);
2602 return res;
2603 }
2604 }
2605 mTriggerReplacedMap.clear();
2606
2607 /**
2608 * Remove all new entries.
2609 */
2610 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2611 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2612 status_t res = metadata.erase(trigger.metadataTag);
2613
2614 if (res != OK) {
2615 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2616 ", trigger value %d", __FUNCTION__,
2617 trigger.getTagName(), trigger.entryValue);
2618 return res;
2619 }
2620 }
2621 mTriggerRemovedMap.clear();
2622
2623 return OK;
2624}
2625
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002626status_t Camera3Device::RequestThread::addDummyTriggerIds(
2627 const sp<CaptureRequest> &request) {
2628 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2629 static const int32_t dummyTriggerId = 1;
2630 status_t res;
2631
2632 CameraMetadata &metadata = request->mSettings;
2633
2634 // If AF trigger is active, insert a dummy AF trigger ID if none already
2635 // exists
2636 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2637 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2638 if (afTrigger.count > 0 &&
2639 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2640 afId.count == 0) {
2641 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2642 if (res != OK) return res;
2643 }
2644
2645 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2646 // if none already exists
2647 camera_metadata_entry pcTrigger =
2648 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2649 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2650 if (pcTrigger.count > 0 &&
2651 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2652 pcId.count == 0) {
2653 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2654 &dummyTriggerId, 1);
2655 if (res != OK) return res;
2656 }
2657
2658 return OK;
2659}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002660
2661
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002662/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002663 * Static callback forwarding methods from HAL to instance
2664 */
2665
2666void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2667 const camera3_capture_result *result) {
2668 Camera3Device *d =
2669 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2670 d->processCaptureResult(result);
2671}
2672
2673void Camera3Device::sNotify(const camera3_callback_ops *cb,
2674 const camera3_notify_msg *msg) {
2675 Camera3Device *d =
2676 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2677 d->notify(msg);
2678}
2679
2680}; // namespace android