blob: a64917dd6ac3cf1edb2a2f6e2a5b8526fbede68b [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 Weicb0652e2014-03-12 18:29:36 -0700425 if (newRequest->mResultExtras.requestId > 0) {
426 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
427 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700428 }
429 return OK;
430}
431
Jianing Weicb0652e2014-03-12 18:29:36 -0700432status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800433 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700434 status_t res;
435 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800436 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800437
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700438 // TODO: take ownership of the request
439
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800440 switch (mStatus) {
441 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700442 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800443 return INVALID_OPERATION;
444 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700445 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700447 case STATUS_UNCONFIGURED:
448 // May be lazily configuring streams, will check during setup
449 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800450 case STATUS_ACTIVE:
451 // OK
452 break;
453 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700454 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800455 return INVALID_OPERATION;
456 }
457
458 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
459 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700460 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 return BAD_VALUE;
462 }
463
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700464 res = mRequestThread->queueRequest(newRequest);
465 if (res == OK) {
466 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
467 if (res != OK) {
468 SET_ERR_L("Can't transition to active in %f seconds!",
469 kActiveTimeout/1e9);
470 }
471 ALOGV("Camera %d: Capture request enqueued", mId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700472 } else {
473 CLOGE("Cannot queue request. Impossible."); // queueRequest always returns OK.
474 return BAD_VALUE;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700475 }
476 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800477}
478
Jianing Wei90e59c92014-03-12 18:29:36 -0700479status_t Camera3Device::submitRequestsHelper(
Jianing Weicb0652e2014-03-12 18:29:36 -0700480 const List<const CameraMetadata> &requests, bool repeating, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700481 ATRACE_CALL();
482 Mutex::Autolock il(mInterfaceLock);
483 Mutex::Autolock l(mLock);
484
485 status_t res = checkStatusOkToCaptureLocked();
486 if (res != OK) {
487 // error logged by previous call
488 return res;
489 }
490
491 RequestList requestList;
492
493 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
494 if (res != OK) {
495 // error logged by previous call
496 return res;
497 }
498
499 if (repeating) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700500 if (lastFrameNumber != NULL) {
501 *lastFrameNumber = mRequestThread->getLastFrameNumber();
502 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700503 res = mRequestThread->setRepeatingRequests(requestList);
504 } else {
505 res = mRequestThread->queueRequestList(requestList);
Jianing Weicb0652e2014-03-12 18:29:36 -0700506 if (lastFrameNumber != NULL) {
507 *lastFrameNumber = mRequestThread->getLastFrameNumber();
508 }
Jianing Wei90e59c92014-03-12 18:29:36 -0700509 }
510
511 if (res == OK) {
512 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
513 if (res != OK) {
514 SET_ERR_L("Can't transition to active in %f seconds!",
515 kActiveTimeout/1e9);
516 }
517 ALOGV("Camera %d: Capture request enqueued", mId);
518 } else {
519 CLOGE("Cannot queue request. Impossible.");
520 return BAD_VALUE;
521 }
522
523 return res;
524}
525
Jianing Weicb0652e2014-03-12 18:29:36 -0700526status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
527 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700528 ATRACE_CALL();
529
Jianing Weicb0652e2014-03-12 18:29:36 -0700530 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700531}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800532
Jianing Weicb0652e2014-03-12 18:29:36 -0700533status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
534 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800535 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700536 status_t res;
537 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 switch (mStatus) {
541 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700542 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800543 return INVALID_OPERATION;
544 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700545 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800546 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700547 case STATUS_UNCONFIGURED:
548 // May be lazily configuring streams, will check during setup
549 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800550 case STATUS_ACTIVE:
551 // OK
552 break;
553 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700554 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 return INVALID_OPERATION;
556 }
557
558 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
559 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700560 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 return BAD_VALUE;
562 }
563
564 RequestList newRepeatingRequests;
565 newRepeatingRequests.push_back(newRepeatingRequest);
566
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700567 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
568 if (res == OK) {
569 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
570 if (res != OK) {
571 SET_ERR_L("Can't transition to active in %f seconds!",
572 kActiveTimeout/1e9);
573 }
574 ALOGV("Camera %d: Repeating request set", mId);
575 }
576 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800577}
578
Jianing Weicb0652e2014-03-12 18:29:36 -0700579status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
580 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700581 ATRACE_CALL();
582
Jianing Weicb0652e2014-03-12 18:29:36 -0700583 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700584}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800585
586sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
587 const CameraMetadata &request) {
588 status_t res;
589
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700590 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800591 res = configureStreamsLocked();
592 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700593 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800594 return NULL;
595 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 if (mStatus == STATUS_UNCONFIGURED) {
597 CLOGE("No streams configured");
598 return NULL;
599 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800600 }
601
602 sp<CaptureRequest> newRequest = createCaptureRequest(request);
603 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800604}
605
Jianing Weicb0652e2014-03-12 18:29:36 -0700606status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800607 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800609 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800610
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611 switch (mStatus) {
612 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700613 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800614 return INVALID_OPERATION;
615 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700616 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700618 case STATUS_UNCONFIGURED:
619 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620 case STATUS_ACTIVE:
621 // OK
622 break;
623 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700624 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 return INVALID_OPERATION;
626 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700627 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700628
629 if (lastFrameNumber != NULL) {
630 *lastFrameNumber = mRequestThread->getLastFrameNumber();
631 ALOGV("%s: lastFrameNumber address %p, value %" PRId64, __FUNCTION__, lastFrameNumber,
632 *lastFrameNumber);
633 }
634
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800636}
637
638status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
639 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800641
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700642 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800643}
644
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700645status_t Camera3Device::createInputStream(
646 uint32_t width, uint32_t height, int format, int *id) {
647 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700649 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700650 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
651 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700652
653 status_t res;
654 bool wasActive = false;
655
656 switch (mStatus) {
657 case STATUS_ERROR:
658 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
659 return INVALID_OPERATION;
660 case STATUS_UNINITIALIZED:
661 ALOGE("%s: Device not initialized", __FUNCTION__);
662 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700663 case STATUS_UNCONFIGURED:
664 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700665 // OK
666 break;
667 case STATUS_ACTIVE:
668 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700670 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700671 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700672 return res;
673 }
674 wasActive = true;
675 break;
676 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700678 return INVALID_OPERATION;
679 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700680 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700681
682 if (mInputStream != 0) {
683 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
684 return INVALID_OPERATION;
685 }
686
687 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
688 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700689 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690
691 mInputStream = newStream;
692
693 *id = mNextStreamId++;
694
695 // Continue captures if active at start
696 if (wasActive) {
697 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
698 res = configureStreamsLocked();
699 if (res != OK) {
700 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
701 __FUNCTION__, mNextStreamId, strerror(-res), res);
702 return res;
703 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700705 }
706
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700707 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700708 return OK;
709}
710
Igor Murashkin2fba5842013-04-22 14:03:54 -0700711
712status_t Camera3Device::createZslStream(
713 uint32_t width, uint32_t height,
714 int depth,
715 /*out*/
716 int *id,
717 sp<Camera3ZslStream>* zslStream) {
718 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700719 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700720 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
722 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700723
724 status_t res;
725 bool wasActive = false;
726
727 switch (mStatus) {
728 case STATUS_ERROR:
729 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
730 return INVALID_OPERATION;
731 case STATUS_UNINITIALIZED:
732 ALOGE("%s: Device not initialized", __FUNCTION__);
733 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700734 case STATUS_UNCONFIGURED:
735 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700736 // OK
737 break;
738 case STATUS_ACTIVE:
739 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700740 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700741 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700742 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700743 return res;
744 }
745 wasActive = true;
746 break;
747 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 return INVALID_OPERATION;
750 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700751 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700752
753 if (mInputStream != 0) {
754 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
755 return INVALID_OPERATION;
756 }
757
758 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
759 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700760 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700761
762 res = mOutputStreams.add(mNextStreamId, newStream);
763 if (res < 0) {
764 ALOGE("%s: Can't add new stream to set: %s (%d)",
765 __FUNCTION__, strerror(-res), res);
766 return res;
767 }
768 mInputStream = newStream;
769
770 *id = mNextStreamId++;
771 *zslStream = newStream;
772
773 // Continue captures if active at start
774 if (wasActive) {
775 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
776 res = configureStreamsLocked();
777 if (res != OK) {
778 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
779 __FUNCTION__, mNextStreamId, strerror(-res), res);
780 return res;
781 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700782 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700783 }
784
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700785 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 return OK;
787}
788
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800789status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
790 uint32_t width, uint32_t height, int format, size_t size, int *id) {
791 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700792 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800793 Mutex::Autolock l(mLock);
Colin Crosse5729fa2014-03-21 15:04:25 -0700794 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu",
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800796
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800797 status_t res;
798 bool wasActive = false;
799
800 switch (mStatus) {
801 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700805 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 case STATUS_UNCONFIGURED:
808 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 // OK
810 break;
811 case STATUS_ACTIVE:
812 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700813 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 return res;
817 }
818 wasActive = true;
819 break;
820 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700821 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return INVALID_OPERATION;
823 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825
826 sp<Camera3OutputStream> newStream;
827 if (format == HAL_PIXEL_FORMAT_BLOB) {
828 newStream = new Camera3OutputStream(mNextStreamId, consumer,
829 width, height, size, format);
830 } else {
831 newStream = new Camera3OutputStream(mNextStreamId, consumer,
832 width, height, format);
833 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835
836 res = mOutputStreams.add(mNextStreamId, newStream);
837 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return res;
840 }
841
842 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700843 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844
845 // Continue captures if active at start
846 if (wasActive) {
847 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
848 res = configureStreamsLocked();
849 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
851 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852 return res;
853 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800858}
859
860status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
861 ATRACE_CALL();
862 (void)outputId; (void)id;
863
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700864 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865 return INVALID_OPERATION;
866}
867
868
869status_t Camera3Device::getStreamInfo(int id,
870 uint32_t *width, uint32_t *height, uint32_t *format) {
871 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700872 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800874
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 switch (mStatus) {
876 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700877 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 return INVALID_OPERATION;
879 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700880 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700882 case STATUS_UNCONFIGURED:
883 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 case STATUS_ACTIVE:
885 // OK
886 break;
887 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 return INVALID_OPERATION;
890 }
891
892 ssize_t idx = mOutputStreams.indexOfKey(id);
893 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700894 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 return idx;
896 }
897
898 if (width) *width = mOutputStreams[idx]->getWidth();
899 if (height) *height = mOutputStreams[idx]->getHeight();
900 if (format) *format = mOutputStreams[idx]->getFormat();
901
902 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800903}
904
905status_t Camera3Device::setStreamTransform(int id,
906 int transform) {
907 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700908 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800909 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800910
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 switch (mStatus) {
912 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700913 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 return INVALID_OPERATION;
915 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700916 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700918 case STATUS_UNCONFIGURED:
919 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 case STATUS_ACTIVE:
921 // OK
922 break;
923 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700924 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 return INVALID_OPERATION;
926 }
927
928 ssize_t idx = mOutputStreams.indexOfKey(id);
929 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700930 CLOGE("Stream %d does not exist",
931 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 return BAD_VALUE;
933 }
934
935 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800936}
937
938status_t Camera3Device::deleteStream(int id) {
939 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941 Mutex::Autolock l(mLock);
942 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943
Igor Murashkine2172be2013-05-28 15:31:39 -0700944 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
945
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 // CameraDevice semantics require device to already be idle before
947 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700948 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700949 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
950 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951 }
952
Igor Murashkin2fba5842013-04-22 14:03:54 -0700953 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800954 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 if (mInputStream != NULL && id == mInputStream->getId()) {
956 deletedStream = mInputStream;
957 mInputStream.clear();
958 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800959 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return BAD_VALUE;
962 }
Zhijun He5f446352014-01-22 09:49:33 -0800963 }
964
965 // Delete output stream or the output part of a bi-directional stream.
966 if (outputStreamIdx != NAME_NOT_FOUND) {
967 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 mOutputStreams.removeItem(id);
969 }
970
971 // Free up the stream endpoint so that it can be used by some other stream
972 res = deletedStream->disconnect();
973 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700974 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800975 // fall through since we want to still list the stream as deleted.
976 }
977 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700978 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979
980 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800981}
982
983status_t Camera3Device::deleteReprocessStream(int id) {
984 ATRACE_CALL();
985 (void)id;
986
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700987 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800988 return INVALID_OPERATION;
989}
990
991
992status_t Camera3Device::createDefaultRequest(int templateId,
993 CameraMetadata *request) {
994 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700995 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700996 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 Mutex::Autolock l(mLock);
998
999 switch (mStatus) {
1000 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001001 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 return INVALID_OPERATION;
1003 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001004 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001006 case STATUS_UNCONFIGURED:
1007 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 case STATUS_ACTIVE:
1009 // OK
1010 break;
1011 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001012 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001013 return INVALID_OPERATION;
1014 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001015
1016 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001017 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001018 rawRequest = mHal3Device->ops->construct_default_request_settings(
1019 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001020 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001021 if (rawRequest == NULL) {
1022 SET_ERR_L("HAL is unable to construct default settings for template %d",
1023 templateId);
1024 return DEAD_OBJECT;
1025 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001026 *request = rawRequest;
1027
1028 return OK;
1029}
1030
1031status_t Camera3Device::waitUntilDrained() {
1032 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001033 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001034 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035
Zhijun He69a37482014-03-23 18:44:49 -07001036 return waitUntilDrainedLocked();
1037}
1038
1039status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 switch (mStatus) {
1041 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001042 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 ALOGV("%s: Already idle", __FUNCTION__);
1044 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001045 case STATUS_CONFIGURED:
1046 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 case STATUS_ERROR:
1048 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001049 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001050 break;
1051 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001052 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053 return INVALID_OPERATION;
1054 }
1055
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001056 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1057 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1058 return res;
1059}
1060
1061// Pause to reconfigure
1062status_t Camera3Device::internalPauseAndWaitLocked() {
1063 mRequestThread->setPaused(true);
1064 mPauseStateNotify = true;
1065
1066 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1067 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1068 if (res != OK) {
1069 SET_ERR_L("Can't idle device in %f seconds!",
1070 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 }
1072
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001073 return res;
1074}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001076// Resume after internalPauseAndWaitLocked
1077status_t Camera3Device::internalResumeLocked() {
1078 status_t res;
1079
1080 mRequestThread->setPaused(false);
1081
1082 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1083 if (res != OK) {
1084 SET_ERR_L("Can't transition to active in %f seconds!",
1085 kActiveTimeout/1e9);
1086 }
1087 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089}
1090
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001091status_t Camera3Device::waitUntilStateThenRelock(bool active,
1092 nsecs_t timeout) {
1093 status_t res = OK;
1094 if (active == (mStatus == STATUS_ACTIVE)) {
1095 // Desired state already reached
1096 return res;
1097 }
1098
1099 bool stateSeen = false;
1100 do {
1101 mRecentStatusUpdates.clear();
1102
1103 res = mStatusChanged.waitRelative(mLock, timeout);
1104 if (res != OK) break;
1105
1106 // Check state change history during wait
1107 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1108 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1109 stateSeen = true;
1110 break;
1111 }
1112 }
1113 } while (!stateSeen);
1114
1115 return res;
1116}
1117
1118
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001119status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1120 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001121 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001122
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001123 if (listener != NULL && mListener != NULL) {
1124 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1125 }
1126 mListener = listener;
1127
1128 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001129}
1130
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001131bool Camera3Device::willNotify3A() {
1132 return false;
1133}
1134
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001135status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001136 status_t res;
1137 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001138
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001139 while (mResultQueue.empty()) {
1140 res = mResultSignal.waitRelative(mOutputLock, timeout);
1141 if (res == TIMED_OUT) {
1142 return res;
1143 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001144 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001145 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001146 return res;
1147 }
1148 }
1149 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001150}
1151
Jianing Weicb0652e2014-03-12 18:29:36 -07001152status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001154 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001155
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001156 if (mResultQueue.empty()) {
1157 return NOT_ENOUGH_DATA;
1158 }
1159
Jianing Weicb0652e2014-03-12 18:29:36 -07001160 if (frame == NULL) {
1161 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1162 return BAD_VALUE;
1163 }
1164
1165 CaptureResult &result = *(mResultQueue.begin());
1166 frame->mResultExtras = result.mResultExtras;
1167 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001168 mResultQueue.erase(mResultQueue.begin());
1169
1170 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171}
1172
1173status_t Camera3Device::triggerAutofocus(uint32_t id) {
1174 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001175 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001177 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1178 // Mix-in this trigger into the next request and only the next request.
1179 RequestTrigger trigger[] = {
1180 {
1181 ANDROID_CONTROL_AF_TRIGGER,
1182 ANDROID_CONTROL_AF_TRIGGER_START
1183 },
1184 {
1185 ANDROID_CONTROL_AF_TRIGGER_ID,
1186 static_cast<int32_t>(id)
1187 },
1188 };
1189
1190 return mRequestThread->queueTrigger(trigger,
1191 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
1194status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1195 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001196 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001198 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1199 // Mix-in this trigger into the next request and only the next request.
1200 RequestTrigger trigger[] = {
1201 {
1202 ANDROID_CONTROL_AF_TRIGGER,
1203 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1204 },
1205 {
1206 ANDROID_CONTROL_AF_TRIGGER_ID,
1207 static_cast<int32_t>(id)
1208 },
1209 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001210
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001211 return mRequestThread->queueTrigger(trigger,
1212 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001213}
1214
1215status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1216 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001218
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001219 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1220 // Mix-in this trigger into the next request and only the next request.
1221 RequestTrigger trigger[] = {
1222 {
1223 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1224 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1225 },
1226 {
1227 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1228 static_cast<int32_t>(id)
1229 },
1230 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001231
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001232 return mRequestThread->queueTrigger(trigger,
1233 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234}
1235
1236status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1237 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1238 ATRACE_CALL();
1239 (void)reprocessStreamId; (void)buffer; (void)listener;
1240
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001241 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242 return INVALID_OPERATION;
1243}
1244
Jianing Weicb0652e2014-03-12 18:29:36 -07001245status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001246 ATRACE_CALL();
1247 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001248 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001249 Mutex::Autolock l(mLock);
1250
Jianing Weicb0652e2014-03-12 18:29:36 -07001251 if (frameNumber != NULL) {
1252 *frameNumber = mRequestThread->getLastFrameNumber();
1253 }
1254
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001255 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001256 status_t res;
1257 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1258 res = mHal3Device->ops->flush(mHal3Device);
1259 } else {
Zhijun He69a37482014-03-23 18:44:49 -07001260 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001261 }
1262
1263 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001264}
1265
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001267 * Methods called by subclasses
1268 */
1269
1270void Camera3Device::notifyStatus(bool idle) {
1271 {
1272 // Need mLock to safely update state and synchronize to current
1273 // state of methods in flight.
1274 Mutex::Autolock l(mLock);
1275 // We can get various system-idle notices from the status tracker
1276 // while starting up. Only care about them if we've actually sent
1277 // in some requests recently.
1278 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1279 return;
1280 }
1281 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1282 idle ? "idle" : "active");
1283 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1284 mRecentStatusUpdates.add(mStatus);
1285 mStatusChanged.signal();
1286
1287 // Skip notifying listener if we're doing some user-transparent
1288 // state changes
1289 if (mPauseStateNotify) return;
1290 }
1291 NotificationListener *listener;
1292 {
1293 Mutex::Autolock l(mOutputLock);
1294 listener = mListener;
1295 }
1296 if (idle && listener != NULL) {
1297 listener->notifyIdle();
1298 }
1299}
1300
1301/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 * Camera3Device private methods
1303 */
1304
1305sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1306 const CameraMetadata &request) {
1307 ATRACE_CALL();
1308 status_t res;
1309
1310 sp<CaptureRequest> newRequest = new CaptureRequest;
1311 newRequest->mSettings = request;
1312
1313 camera_metadata_entry_t inputStreams =
1314 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1315 if (inputStreams.count > 0) {
1316 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001317 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001318 CLOGE("Request references unknown input stream %d",
1319 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320 return NULL;
1321 }
1322 // Lazy completion of stream configuration (allocation/registration)
1323 // on first use
1324 if (mInputStream->isConfiguring()) {
1325 res = mInputStream->finishConfiguration(mHal3Device);
1326 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001327 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001329 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 return NULL;
1331 }
1332 }
1333
1334 newRequest->mInputStream = mInputStream;
1335 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1336 }
1337
1338 camera_metadata_entry_t streams =
1339 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1340 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001341 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001342 return NULL;
1343 }
1344
1345 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001346 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001348 CLOGE("Request references unknown stream %d",
1349 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001350 return NULL;
1351 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001352 sp<Camera3OutputStreamInterface> stream =
1353 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001354
1355 // Lazy completion of stream configuration (allocation/registration)
1356 // on first use
1357 if (stream->isConfiguring()) {
1358 res = stream->finishConfiguration(mHal3Device);
1359 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001360 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1361 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001362 return NULL;
1363 }
1364 }
1365
1366 newRequest->mOutputStreams.push(stream);
1367 }
1368 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1369
1370 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001371}
1372
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373status_t Camera3Device::configureStreamsLocked() {
1374 ATRACE_CALL();
1375 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001376
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001378 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001379 return INVALID_OPERATION;
1380 }
1381
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001382 if (!mNeedConfig) {
1383 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1384 return OK;
1385 }
1386
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001388 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389
1390 camera3_stream_configuration config;
1391
1392 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1393
1394 Vector<camera3_stream_t*> streams;
1395 streams.setCapacity(config.num_streams);
1396
1397 if (mInputStream != NULL) {
1398 camera3_stream_t *inputStream;
1399 inputStream = mInputStream->startConfiguration();
1400 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001401 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001402 return INVALID_OPERATION;
1403 }
1404 streams.add(inputStream);
1405 }
1406
1407 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001408
1409 // Don't configure bidi streams twice, nor add them twice to the list
1410 if (mOutputStreams[i].get() ==
1411 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1412
1413 config.num_streams--;
1414 continue;
1415 }
1416
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001417 camera3_stream_t *outputStream;
1418 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1419 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001420 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421 return INVALID_OPERATION;
1422 }
1423 streams.add(outputStream);
1424 }
1425
1426 config.streams = streams.editArray();
1427
1428 // Do the HAL configuration; will potentially touch stream
1429 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001430 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001431 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001432 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433
1434 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001435 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1436 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001437 return res;
1438 }
1439
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001440 // Finish all stream configuration immediately.
1441 // TODO: Try to relax this later back to lazy completion, which should be
1442 // faster
1443
Igor Murashkin073f8572013-05-02 14:59:28 -07001444 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001445 res = mInputStream->finishConfiguration(mHal3Device);
1446 if (res != OK) {
1447 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1448 mInputStream->getId(), strerror(-res), res);
1449 return res;
1450 }
1451 }
1452
1453 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001454 sp<Camera3OutputStreamInterface> outputStream =
1455 mOutputStreams.editValueAt(i);
1456 if (outputStream->isConfiguring()) {
1457 res = outputStream->finishConfiguration(mHal3Device);
1458 if (res != OK) {
1459 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1460 outputStream->getId(), strerror(-res), res);
1461 return res;
1462 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001463 }
1464 }
1465
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 // Request thread needs to know to avoid using repeat-last-settings protocol
1467 // across configure_streams() calls
1468 mRequestThread->configurationComplete();
1469
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001470 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001472 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001474 if (config.num_streams > 0) {
1475 mStatus = STATUS_CONFIGURED;
1476 } else {
1477 mStatus = STATUS_UNCONFIGURED;
1478 }
1479
1480 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001483}
1484
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001485void Camera3Device::setErrorState(const char *fmt, ...) {
1486 Mutex::Autolock l(mLock);
1487 va_list args;
1488 va_start(args, fmt);
1489
1490 setErrorStateLockedV(fmt, args);
1491
1492 va_end(args);
1493}
1494
1495void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1496 Mutex::Autolock l(mLock);
1497 setErrorStateLockedV(fmt, args);
1498}
1499
1500void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1501 va_list args;
1502 va_start(args, fmt);
1503
1504 setErrorStateLockedV(fmt, args);
1505
1506 va_end(args);
1507}
1508
1509void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001510 // Print out all error messages to log
1511 String8 errorCause = String8::formatV(fmt, args);
1512 ALOGE("Camera %d: %s", mId, errorCause.string());
1513
1514 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001515 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001516
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001517 // Save stack trace. View by dumping it later.
1518 CameraTraces::saveTrace();
1519 // TODO: consider adding errorCause and client pid/procname
1520
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001521 mErrorCause = errorCause;
1522
1523 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001524 mStatus = STATUS_ERROR;
1525}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001526
1527/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001528 * In-flight request management
1529 */
1530
Jianing Weicb0652e2014-03-12 18:29:36 -07001531status_t Camera3Device::registerInFlight(uint32_t frameNumber,
1532 int32_t numBuffers, CaptureResultExtras resultExtras) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001533 ATRACE_CALL();
1534 Mutex::Autolock l(mInFlightLock);
1535
1536 ssize_t res;
Jianing Weicb0652e2014-03-12 18:29:36 -07001537 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001538 if (res < 0) return res;
1539
1540 return OK;
1541}
1542
1543/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001544 * QUIRK(partial results)
1545 * Check if all 3A fields are ready, and send off a partial 3A-only result
1546 * to the output frame queue
1547 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001548bool Camera3Device::processPartial3AQuirk(
Jianing Weicb0652e2014-03-12 18:29:36 -07001549 uint32_t frameNumber,
1550 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001551
1552 // Check if all 3A states are present
1553 // The full list of fields is
1554 // android.control.afMode
1555 // android.control.awbMode
1556 // android.control.aeState
1557 // android.control.awbState
1558 // android.control.afState
1559 // android.control.afTriggerID
1560 // android.control.aePrecaptureID
1561 // TODO: Add android.control.aeMode
1562
1563 bool gotAllStates = true;
1564
1565 uint8_t afMode;
1566 uint8_t awbMode;
1567 uint8_t aeState;
1568 uint8_t afState;
1569 uint8_t awbState;
1570 int32_t afTriggerId;
1571 int32_t aeTriggerId;
1572
1573 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1574 &afMode, frameNumber);
1575
1576 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1577 &awbMode, frameNumber);
1578
1579 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1580 &aeState, frameNumber);
1581
1582 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1583 &afState, frameNumber);
1584
1585 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1586 &awbState, frameNumber);
1587
1588 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1589 &afTriggerId, frameNumber);
1590
1591 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1592 &aeTriggerId, frameNumber);
1593
1594 if (!gotAllStates) return false;
1595
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001596 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001597 "AF state %d, AE state %d, AWB state %d, "
1598 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001599 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001600 afMode, awbMode,
1601 afState, aeState, awbState,
1602 afTriggerId, aeTriggerId);
1603
1604 // Got all states, so construct a minimal result to send
1605 // In addition to the above fields, this means adding in
1606 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001607 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001608 // android.quirks.partialResult
1609
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001610 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001611
1612 Mutex::Autolock l(mOutputLock);
1613
Jianing Weicb0652e2014-03-12 18:29:36 -07001614 CaptureResult captureResult;
1615 captureResult.mResultExtras = resultExtras;
1616 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1617 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1618 // but not limited to CameraDeviceBase::getNextResult
1619 CaptureResult& min3AResult =
1620 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001621
Jianing Weicb0652e2014-03-12 18:29:36 -07001622 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1623 // TODO: This is problematic casting. Need to fix CameraMetadata.
1624 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001625 return false;
1626 }
1627
Jianing Weicb0652e2014-03-12 18:29:36 -07001628 int32_t requestId = resultExtras.requestId;
1629 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001630 &requestId, frameNumber)) {
1631 return false;
1632 }
1633
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001634 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
Jianing Weicb0652e2014-03-12 18:29:36 -07001635 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001636 &partialResult, frameNumber)) {
1637 return false;
1638 }
1639
Jianing Weicb0652e2014-03-12 18:29:36 -07001640 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001641 &afMode, frameNumber)) {
1642 return false;
1643 }
1644
Jianing Weicb0652e2014-03-12 18:29:36 -07001645 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001646 &awbMode, frameNumber)) {
1647 return false;
1648 }
1649
Jianing Weicb0652e2014-03-12 18:29:36 -07001650 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001651 &aeState, frameNumber)) {
1652 return false;
1653 }
1654
Jianing Weicb0652e2014-03-12 18:29:36 -07001655 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001656 &afState, frameNumber)) {
1657 return false;
1658 }
1659
Jianing Weicb0652e2014-03-12 18:29:36 -07001660 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001661 &awbState, frameNumber)) {
1662 return false;
1663 }
1664
Jianing Weicb0652e2014-03-12 18:29:36 -07001665 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001666 &afTriggerId, frameNumber)) {
1667 return false;
1668 }
1669
Jianing Weicb0652e2014-03-12 18:29:36 -07001670 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001671 &aeTriggerId, frameNumber)) {
1672 return false;
1673 }
1674
1675 mResultSignal.signal();
1676
1677 return true;
1678}
1679
1680template<typename T>
1681bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001682 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001683 (void) frameNumber;
1684
1685 camera_metadata_ro_entry_t entry;
1686
1687 entry = result.find(tag);
1688 if (entry.count == 0) {
1689 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1690 mId, frameNumber, get_camera_metadata_tag_name(tag));
1691 return false;
1692 }
1693
1694 if (sizeof(T) == sizeof(uint8_t)) {
1695 *value = entry.data.u8[0];
1696 } else if (sizeof(T) == sizeof(int32_t)) {
1697 *value = entry.data.i32[0];
1698 } else {
1699 ALOGE("%s: Unexpected type", __FUNCTION__);
1700 return false;
1701 }
1702 return true;
1703}
1704
1705template<typename T>
1706bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001707 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001708 if (result.update(tag, value, 1) != NO_ERROR) {
1709 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1710 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1711 frameNumber, get_camera_metadata_tag_name(tag));
1712 return false;
1713 }
1714 return true;
1715}
1716
1717/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001718 * Camera HAL device callback methods
1719 */
1720
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001721void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001722 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001723
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001724 status_t res;
1725
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001726 uint32_t frameNumber = result->frame_number;
1727 if (result->result == NULL && result->num_output_buffers == 0) {
1728 SET_ERR("No result data provided by HAL for frame %d",
1729 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001730 return;
1731 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001732 bool partialResultQuirk = false;
1733 CameraMetadata collectedQuirkResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001734 CaptureResultExtras resultExtras;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001735
Jianing Weicb0652e2014-03-12 18:29:36 -07001736 // Get capture timestamp and resultExtras from list of in-flight requests,
1737 // where it was added by the shutter notification for this frame.
1738 // Then update the in-flight status and remove the in-flight entry if
1739 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001740 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001741 {
1742 Mutex::Autolock l(mInFlightLock);
1743 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1744 if (idx == NAME_NOT_FOUND) {
1745 SET_ERR("Unknown frame number for capture result: %d",
1746 frameNumber);
1747 return;
1748 }
1749 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001750 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1751 ", burstId = %" PRId32,
1752 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1753 request.resultExtras.burstId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001754
1755 // Check if this result carries only partial metadata
1756 if (mUsePartialResultQuirk && result->result != NULL) {
1757 camera_metadata_ro_entry_t partialResultEntry;
1758 res = find_camera_metadata_ro_entry(result->result,
1759 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1760 if (res != NAME_NOT_FOUND &&
1761 partialResultEntry.count > 0 &&
1762 partialResultEntry.data.u8[0] ==
1763 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1764 // A partial result. Flag this as such, and collect this
1765 // set of metadata into the in-flight entry.
1766 partialResultQuirk = true;
1767 request.partialResultQuirk.collectedResult.append(
1768 result->result);
1769 request.partialResultQuirk.collectedResult.erase(
1770 ANDROID_QUIRKS_PARTIAL_RESULT);
1771 // Fire off a 3A-only result if possible
1772 if (!request.partialResultQuirk.haveSent3A) {
1773 request.partialResultQuirk.haveSent3A =
1774 processPartial3AQuirk(frameNumber,
Jianing Weicb0652e2014-03-12 18:29:36 -07001775 request.partialResultQuirk.collectedResult,
1776 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001777 }
1778 }
1779 }
1780
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001781 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001782 resultExtras = request.resultExtras;
1783 ALOGVV("%s: after checking partial burstId = %d, frameNumber %lld", __FUNCTION__,
1784 request.resultExtras.burstId, request.resultExtras.frameNumber);
1785
Zhijun He1d1f8462013-10-02 16:29:51 -07001786 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001787 * One of the following must happen before it's legal to call process_capture_result,
1788 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001789 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1790 * - CAMERA3_MSG_ERROR (expected during flush)
1791 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001792 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001793 SET_ERR("Called before shutter notify for frame %d",
1794 frameNumber);
1795 return;
1796 }
1797
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001798 // Did we get the (final) result metadata for this capture?
1799 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001800 if (request.haveResultMetadata) {
1801 SET_ERR("Called multiple times with metadata for frame %d",
1802 frameNumber);
1803 return;
1804 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001805 if (mUsePartialResultQuirk &&
1806 !request.partialResultQuirk.collectedResult.isEmpty()) {
1807 collectedQuirkResult.acquire(
1808 request.partialResultQuirk.collectedResult);
1809 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001810 request.haveResultMetadata = true;
1811 }
1812
1813 request.numBuffersLeft -= result->num_output_buffers;
1814
1815 if (request.numBuffersLeft < 0) {
1816 SET_ERR("Too many buffers returned for frame %d",
1817 frameNumber);
1818 return;
1819 }
1820
Zhijun He1b05dfc2013-11-21 12:57:51 -08001821 // Check if everything has arrived for this result (buffers and metadata), remove it from
1822 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1823 if ((request.requestStatus != OK) ||
1824 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001825 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001826 mInFlightMap.removeItemsAt(idx, 1);
1827 }
1828
1829 // Sanity check - if we have too many in-flight frames, something has
1830 // likely gone wrong
1831 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001832 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001833 }
1834
1835 }
1836
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001837 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001838 bool gotResult = false;
1839 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001840 Mutex::Autolock l(mOutputLock);
1841
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001842 gotResult = true;
1843
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001844 if (frameNumber != mNextResultFrameNumber) {
1845 SET_ERR("Out-of-order capture result metadata submitted! "
1846 "(got frame number %d, expecting %d)",
1847 frameNumber, mNextResultFrameNumber);
1848 return;
1849 }
1850 mNextResultFrameNumber++;
1851
Jianing Weicb0652e2014-03-12 18:29:36 -07001852 CaptureResult captureResult;
1853 captureResult.mResultExtras = resultExtras;
1854 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001855
Jianing Weicb0652e2014-03-12 18:29:36 -07001856 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1857 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001858 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001859 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001860 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001861 } else {
1862 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001863 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001864 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001865
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001866 // Append any previous partials to form a complete result
1867 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001868 captureResult.mMetadata.append(collectedQuirkResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001869 }
1870
Jianing Weicb0652e2014-03-12 18:29:36 -07001871 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001872
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001873 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001874
1875 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07001876 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001877 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001878 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001879 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001880 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001881 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001882 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001883 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001884 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001885 gotResult = false;
1886 }
1887
1888 if (gotResult) {
1889 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07001890 List<CaptureResult>::iterator queuedResult =
1891 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1892 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1893 ", burstId = %" PRId32, __FUNCTION__,
1894 queuedResult->mResultExtras.requestId,
1895 queuedResult->mResultExtras.frameNumber,
1896 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001897 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001898 } // scope for mOutputLock
1899
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001900 // Return completed buffers to their streams with the timestamp
1901
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001902 for (size_t i = 0; i < result->num_output_buffers; i++) {
1903 Camera3Stream *stream =
1904 Camera3Stream::cast(result->output_buffers[i].stream);
1905 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1906 // Note: stream may be deallocated at this point, if this buffer was the
1907 // last reference to it.
1908 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001909 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001910 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001911 }
1912 }
1913
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001914 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001915
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001916 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001917 mResultSignal.signal();
1918 }
1919
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001920}
1921
1922void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001923 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001924 NotificationListener *listener;
1925 {
1926 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001927 listener = mListener;
1928 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001929
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001930 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001931 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001932 return;
1933 }
1934
1935 switch (msg->type) {
1936 case CAMERA3_MSG_ERROR: {
1937 int streamId = 0;
1938 if (msg->message.error.error_stream != NULL) {
1939 Camera3Stream *stream =
1940 Camera3Stream::cast(
1941 msg->message.error.error_stream);
1942 streamId = stream->getId();
1943 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001944 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1945 mId, __FUNCTION__, msg->message.error.frame_number,
1946 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001947
Jianing Weicb0652e2014-03-12 18:29:36 -07001948 CaptureResultExtras resultExtras;
Zhijun He1d1f8462013-10-02 16:29:51 -07001949 // Set request error status for the request in the in-flight tracking
1950 {
1951 Mutex::Autolock l(mInFlightLock);
1952 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1953 if (idx >= 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001954 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1955 r.requestStatus = msg->message.error.error_code;
1956 resultExtras = r.resultExtras;
1957 } else {
1958 resultExtras.frameNumber = msg->message.error.frame_number;
1959 ALOGE("Camera %d: %s: cannot find in-flight request on frame %" PRId64
1960 " error", mId, __FUNCTION__, resultExtras.frameNumber);
Zhijun He1d1f8462013-10-02 16:29:51 -07001961 }
1962 }
1963
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001964 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001965 if (msg->message.error.error_code == CAMERA3_MSG_ERROR_DEVICE) {
1966 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1967 resultExtras);
1968 } else {
1969 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE,
1970 resultExtras);
1971 }
1972 } else {
1973 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001974 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001975 break;
1976 }
1977 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001978 ssize_t idx;
1979 uint32_t frameNumber = msg->message.shutter.frame_number;
1980 nsecs_t timestamp = msg->message.shutter.timestamp;
1981 // Verify ordering of shutter notifications
1982 {
1983 Mutex::Autolock l(mOutputLock);
1984 if (frameNumber != mNextShutterFrameNumber) {
1985 SET_ERR("Shutter notification out-of-order. Expected "
1986 "notification for frame %d, got frame %d",
1987 mNextShutterFrameNumber, frameNumber);
1988 break;
1989 }
1990 mNextShutterFrameNumber++;
1991 }
1992
Jianing Weicb0652e2014-03-12 18:29:36 -07001993 CaptureResultExtras resultExtras;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001994
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001995 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001996 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001997 {
1998 Mutex::Autolock l(mInFlightLock);
1999 idx = mInFlightMap.indexOfKey(frameNumber);
2000 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002001 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2002 r.captureTimestamp = timestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07002003 resultExtras = r.resultExtras;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002004 }
2005 }
2006 if (idx < 0) {
2007 SET_ERR("Shutter notification for non-existent frame number %d",
2008 frameNumber);
2009 break;
2010 }
Colin Crosse5729fa2014-03-21 15:04:25 -07002011 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Jianing Weicb0652e2014-03-12 18:29:36 -07002012 mId, __FUNCTION__, frameNumber, resultExtras.requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002013 // Call listener, if any
2014 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002015 listener->notifyShutter(resultExtras, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002016 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002017 break;
2018 }
2019 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002020 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002021 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002022 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002023}
2024
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002025CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002026 ALOGV("%s", __FUNCTION__);
2027
Igor Murashkin1e479c02013-09-06 16:55:14 -07002028 CameraMetadata retVal;
2029
2030 if (mRequestThread != NULL) {
2031 retVal = mRequestThread->getLatestRequest();
2032 }
2033
Igor Murashkin1e479c02013-09-06 16:55:14 -07002034 return retVal;
2035}
2036
Jianing Weicb0652e2014-03-12 18:29:36 -07002037
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002038/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002039 * RequestThread inner class methods
2040 */
2041
2042Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002043 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002044 camera3_device_t *hal3Device) :
2045 Thread(false),
2046 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002047 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002048 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002049 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002050 mReconfigured(false),
2051 mDoPause(false),
2052 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002053 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002054 mLatestRequestId(NAME_NOT_FOUND),
2055 mLastFrameNumber(-1) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002056 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057}
2058
2059void Camera3Device::RequestThread::configurationComplete() {
2060 Mutex::Autolock l(mRequestLock);
2061 mReconfigured = true;
2062}
2063
2064status_t Camera3Device::RequestThread::queueRequest(
2065 sp<CaptureRequest> request) {
2066 Mutex::Autolock l(mRequestLock);
2067 mRequestQueue.push_back(request);
2068
Jianing Weicb0652e2014-03-12 18:29:36 -07002069 mLastFrameNumber++;
2070
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002071 unpauseForNewRequests();
2072
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002073 return OK;
2074}
2075
Jianing Wei90e59c92014-03-12 18:29:36 -07002076status_t Camera3Device::RequestThread::queueRequestList(
2077 List<sp<CaptureRequest> > &requests) {
2078 Mutex::Autolock l(mRequestLock);
2079 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2080 ++it) {
2081 mRequestQueue.push_back(*it);
2082 }
2083
Jianing Weicb0652e2014-03-12 18:29:36 -07002084 mLastFrameNumber += requests.size();
2085
Jianing Wei90e59c92014-03-12 18:29:36 -07002086 unpauseForNewRequests();
2087
2088 return OK;
2089}
2090
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002091
2092status_t Camera3Device::RequestThread::queueTrigger(
2093 RequestTrigger trigger[],
2094 size_t count) {
2095
2096 Mutex::Autolock l(mTriggerMutex);
2097 status_t ret;
2098
2099 for (size_t i = 0; i < count; ++i) {
2100 ret = queueTriggerLocked(trigger[i]);
2101
2102 if (ret != OK) {
2103 return ret;
2104 }
2105 }
2106
2107 return OK;
2108}
2109
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002110int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2111 sp<Camera3Device> d = device.promote();
2112 if (d != NULL) return d->mId;
2113 return 0;
2114}
2115
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002116status_t Camera3Device::RequestThread::queueTriggerLocked(
2117 RequestTrigger trigger) {
2118
2119 uint32_t tag = trigger.metadataTag;
2120 ssize_t index = mTriggerMap.indexOfKey(tag);
2121
2122 switch (trigger.getTagType()) {
2123 case TYPE_BYTE:
2124 // fall-through
2125 case TYPE_INT32:
2126 break;
2127 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002128 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2129 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002130 return INVALID_OPERATION;
2131 }
2132
2133 /**
2134 * Collect only the latest trigger, since we only have 1 field
2135 * in the request settings per trigger tag, and can't send more than 1
2136 * trigger per request.
2137 */
2138 if (index != NAME_NOT_FOUND) {
2139 mTriggerMap.editValueAt(index) = trigger;
2140 } else {
2141 mTriggerMap.add(tag, trigger);
2142 }
2143
2144 return OK;
2145}
2146
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002147status_t Camera3Device::RequestThread::setRepeatingRequests(
2148 const RequestList &requests) {
2149 Mutex::Autolock l(mRequestLock);
2150 mRepeatingRequests.clear();
2151 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2152 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002153
2154 unpauseForNewRequests();
2155
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002156 return OK;
2157}
2158
2159status_t Camera3Device::RequestThread::clearRepeatingRequests() {
2160 Mutex::Autolock l(mRequestLock);
2161 mRepeatingRequests.clear();
2162 return OK;
2163}
2164
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002165status_t Camera3Device::RequestThread::clear() {
2166 Mutex::Autolock l(mRequestLock);
2167 mRepeatingRequests.clear();
2168 mRequestQueue.clear();
2169 mTriggerMap.clear();
Jianing Weicb0652e2014-03-12 18:29:36 -07002170 // Question: no need to reset frame number?
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002171 return OK;
2172}
2173
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002174void Camera3Device::RequestThread::setPaused(bool paused) {
2175 Mutex::Autolock l(mPauseLock);
2176 mDoPause = paused;
2177 mDoPauseSignal.signal();
2178}
2179
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002180status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2181 int32_t requestId, nsecs_t timeout) {
2182 Mutex::Autolock l(mLatestRequestMutex);
2183 status_t res;
2184 while (mLatestRequestId != requestId) {
2185 nsecs_t startTime = systemTime();
2186
2187 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2188 if (res != OK) return res;
2189
2190 timeout -= (systemTime() - startTime);
2191 }
2192
2193 return OK;
2194}
2195
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002196void Camera3Device::RequestThread::requestExit() {
2197 // Call parent to set up shutdown
2198 Thread::requestExit();
2199 // The exit from any possible waits
2200 mDoPauseSignal.signal();
2201 mRequestSignal.signal();
2202}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002203
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002204bool Camera3Device::RequestThread::threadLoop() {
2205
2206 status_t res;
2207
2208 // Handle paused state.
2209 if (waitIfPaused()) {
2210 return true;
2211 }
2212
2213 // Get work to do
2214
2215 sp<CaptureRequest> nextRequest = waitForNextRequest();
2216 if (nextRequest == NULL) {
2217 return true;
2218 }
2219
2220 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002221 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002222 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002223
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002224 // Get the request ID, if any
2225 int requestId;
2226 camera_metadata_entry_t requestIdEntry =
2227 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2228 if (requestIdEntry.count > 0) {
2229 requestId = requestIdEntry.data.i32[0];
2230 } else {
2231 ALOGW("%s: Did not have android.request.id set in the request",
2232 __FUNCTION__);
2233 requestId = NAME_NOT_FOUND;
2234 }
2235
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002236 // Insert any queued triggers (before metadata is locked)
2237 int32_t triggerCount;
2238 res = insertTriggers(nextRequest);
2239 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002240 SET_ERR("RequestThread: Unable to insert triggers "
2241 "(capture request %d, HAL device: %s (%d)",
2242 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002243 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2244 return false;
2245 }
2246 triggerCount = res;
2247
2248 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2249
2250 // If the request is the same as last, or we had triggers last time
2251 if (mPrevRequest != nextRequest || triggersMixedIn) {
2252 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002253 * HAL workaround:
2254 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2255 */
2256 res = addDummyTriggerIds(nextRequest);
2257 if (res != OK) {
2258 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2259 "(capture request %d, HAL device: %s (%d)",
2260 (mFrameNumber+1), strerror(-res), res);
2261 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2262 return false;
2263 }
2264
2265 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002266 * The request should be presorted so accesses in HAL
2267 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2268 */
2269 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002270 request.settings = nextRequest->mSettings.getAndLock();
2271 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002272 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2273
2274 IF_ALOGV() {
2275 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2276 find_camera_metadata_ro_entry(
2277 request.settings,
2278 ANDROID_CONTROL_AF_TRIGGER,
2279 &e
2280 );
2281 if (e.count > 0) {
2282 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2283 __FUNCTION__,
2284 mFrameNumber+1,
2285 e.data.u8[0]);
2286 }
2287 }
2288 } else {
2289 // leave request.settings NULL to indicate 'reuse latest given'
2290 ALOGVV("%s: Request settings are REUSED",
2291 __FUNCTION__);
2292 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002293
2294 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295
2296 // Fill in buffers
2297
2298 if (nextRequest->mInputStream != NULL) {
2299 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002300 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002301 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002302 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002303 " %s (%d)", strerror(-res), res);
2304 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2305 return true;
2306 }
2307 } else {
2308 request.input_buffer = NULL;
2309 }
2310
2311 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2312 nextRequest->mOutputStreams.size());
2313 request.output_buffers = outputBuffers.array();
2314 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2315 res = nextRequest->mOutputStreams.editItemAt(i)->
2316 getBuffer(&outputBuffers.editItemAt(i));
2317 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002318 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2319 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002320 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2321 return true;
2322 }
2323 request.num_output_buffers++;
2324 }
2325
2326 request.frame_number = mFrameNumber++;
Jianing Weicb0652e2014-03-12 18:29:36 -07002327 // Update frameNumber of CaptureResultExtras
2328 nextRequest->mResultExtras.frameNumber = request.frame_number;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002329
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002330 // Log request in the in-flight queue
2331 sp<Camera3Device> parent = mParent.promote();
2332 if (parent == NULL) {
2333 CLOGE("RequestThread: Parent is gone");
2334 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2335 return false;
2336 }
2337
Jianing Weicb0652e2014-03-12 18:29:36 -07002338 res = parent->registerInFlight(request.frame_number,
2339 request.num_output_buffers, nextRequest->mResultExtras);
2340 ALOGVV("%s: registered in flight requestId = %d, frameNumber = %lld, burstId = %d ",
2341 __FUNCTION__,
2342 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2343 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002344 if (res != OK) {
2345 SET_ERR("RequestThread: Unable to register new in-flight request:"
2346 " %s (%d)", strerror(-res), res);
2347 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2348 return false;
2349 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002350
Zhijun Hecc27e112013-10-03 16:12:43 -07002351 // Inform waitUntilRequestProcessed thread of a new request ID
2352 {
2353 Mutex::Autolock al(mLatestRequestMutex);
2354
2355 mLatestRequestId = requestId;
2356 mLatestRequestSignal.signal();
2357 }
2358
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002359 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002360 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2361 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002362 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002363 ATRACE_END();
2364
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002366 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002367 " device: %s (%d)", request.frame_number, strerror(-res), res);
2368 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2369 return false;
2370 }
2371
Igor Murashkin1e479c02013-09-06 16:55:14 -07002372 // Update the latest request sent to HAL
2373 if (request.settings != NULL) { // Don't update them if they were unchanged
2374 Mutex::Autolock al(mLatestRequestMutex);
2375
2376 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2377 mLatestRequest.acquire(cloned);
2378 }
2379
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002380 if (request.settings != NULL) {
2381 nextRequest->mSettings.unlock(request.settings);
2382 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002383
2384 // Remove any previously queued triggers (after unlock)
2385 res = removeTriggers(mPrevRequest);
2386 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002387 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002388 "(capture request %d, HAL device: %s (%d)",
2389 request.frame_number, strerror(-res), res);
2390 return false;
2391 }
2392 mPrevTriggers = triggerCount;
2393
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002394 // Return input buffer back to framework
2395 if (request.input_buffer != NULL) {
2396 Camera3Stream *stream =
2397 Camera3Stream::cast(request.input_buffer->stream);
2398 res = stream->returnInputBuffer(*(request.input_buffer));
2399 // Note: stream may be deallocated at this point, if this buffer was the
2400 // last reference to it.
2401 if (res != OK) {
2402 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2403 " its stream:%s (%d)", __FUNCTION__,
2404 request.frame_number, strerror(-res), res);
2405 // TODO: Report error upstream
2406 }
2407 }
2408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 return true;
2410}
2411
Igor Murashkin1e479c02013-09-06 16:55:14 -07002412CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2413 Mutex::Autolock al(mLatestRequestMutex);
2414
2415 ALOGV("RequestThread::%s", __FUNCTION__);
2416
2417 return mLatestRequest;
2418}
2419
Jianing Weicb0652e2014-03-12 18:29:36 -07002420int64_t Camera3Device::RequestThread::getLastFrameNumber() {
2421 Mutex::Autolock al(mRequestLock);
2422
2423 ALOGV("RequestThread::%s", __FUNCTION__);
2424
2425 return mLastFrameNumber;
2426}
2427
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002428void Camera3Device::RequestThread::cleanUpFailedRequest(
2429 camera3_capture_request_t &request,
2430 sp<CaptureRequest> &nextRequest,
2431 Vector<camera3_stream_buffer_t> &outputBuffers) {
2432
2433 if (request.settings != NULL) {
2434 nextRequest->mSettings.unlock(request.settings);
2435 }
2436 if (request.input_buffer != NULL) {
2437 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002438 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002439 }
2440 for (size_t i = 0; i < request.num_output_buffers; i++) {
2441 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2442 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2443 outputBuffers[i], 0);
2444 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002445}
2446
2447sp<Camera3Device::CaptureRequest>
2448 Camera3Device::RequestThread::waitForNextRequest() {
2449 status_t res;
2450 sp<CaptureRequest> nextRequest;
2451
2452 // Optimized a bit for the simple steady-state case (single repeating
2453 // request), to avoid putting that request in the queue temporarily.
2454 Mutex::Autolock l(mRequestLock);
2455
2456 while (mRequestQueue.empty()) {
2457 if (!mRepeatingRequests.empty()) {
2458 // Always atomically enqueue all requests in a repeating request
2459 // list. Guarantees a complete in-sequence set of captures to
2460 // application.
2461 const RequestList &requests = mRepeatingRequests;
2462 RequestList::const_iterator firstRequest =
2463 requests.begin();
2464 nextRequest = *firstRequest;
2465 mRequestQueue.insert(mRequestQueue.end(),
2466 ++firstRequest,
2467 requests.end());
2468 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002469
2470 mLastFrameNumber += requests.size();
2471
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002472 break;
2473 }
2474
2475 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2476
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002477 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2478 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002479 Mutex::Autolock pl(mPauseLock);
2480 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002481 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002483 // Let the tracker know
2484 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2485 if (statusTracker != 0) {
2486 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2487 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002488 }
2489 // Stop waiting for now and let thread management happen
2490 return NULL;
2491 }
2492 }
2493
2494 if (nextRequest == NULL) {
2495 // Don't have a repeating request already in hand, so queue
2496 // must have an entry now.
2497 RequestList::iterator firstRequest =
2498 mRequestQueue.begin();
2499 nextRequest = *firstRequest;
2500 mRequestQueue.erase(firstRequest);
2501 }
2502
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002503 // In case we've been unpaused by setPaused clearing mDoPause, need to
2504 // update internal pause state (capture/setRepeatingRequest unpause
2505 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002506 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002507 if (mPaused) {
2508 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2509 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2510 if (statusTracker != 0) {
2511 statusTracker->markComponentActive(mStatusId);
2512 }
2513 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002514 mPaused = false;
2515
2516 // Check if we've reconfigured since last time, and reset the preview
2517 // request if so. Can't use 'NULL request == repeat' across configure calls.
2518 if (mReconfigured) {
2519 mPrevRequest.clear();
2520 mReconfigured = false;
2521 }
2522
2523 return nextRequest;
2524}
2525
2526bool Camera3Device::RequestThread::waitIfPaused() {
2527 status_t res;
2528 Mutex::Autolock l(mPauseLock);
2529 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002530 if (mPaused == false) {
2531 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002532 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2533 // Let the tracker know
2534 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2535 if (statusTracker != 0) {
2536 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2537 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002538 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002539
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002540 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002541 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002542 return true;
2543 }
2544 }
2545 // We don't set mPaused to false here, because waitForNextRequest needs
2546 // to further manage the paused state in case of starvation.
2547 return false;
2548}
2549
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002550void Camera3Device::RequestThread::unpauseForNewRequests() {
2551 // With work to do, mark thread as unpaused.
2552 // If paused by request (setPaused), don't resume, to avoid
2553 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002554 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002555 Mutex::Autolock p(mPauseLock);
2556 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002557 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2558 if (mPaused) {
2559 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2560 if (statusTracker != 0) {
2561 statusTracker->markComponentActive(mStatusId);
2562 }
2563 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002564 mPaused = false;
2565 }
2566}
2567
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002568void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2569 sp<Camera3Device> parent = mParent.promote();
2570 if (parent != NULL) {
2571 va_list args;
2572 va_start(args, fmt);
2573
2574 parent->setErrorStateV(fmt, args);
2575
2576 va_end(args);
2577 }
2578}
2579
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002580status_t Camera3Device::RequestThread::insertTriggers(
2581 const sp<CaptureRequest> &request) {
2582
2583 Mutex::Autolock al(mTriggerMutex);
2584
2585 CameraMetadata &metadata = request->mSettings;
2586 size_t count = mTriggerMap.size();
2587
2588 for (size_t i = 0; i < count; ++i) {
2589 RequestTrigger trigger = mTriggerMap.valueAt(i);
2590
2591 uint32_t tag = trigger.metadataTag;
2592 camera_metadata_entry entry = metadata.find(tag);
2593
2594 if (entry.count > 0) {
2595 /**
2596 * Already has an entry for this trigger in the request.
2597 * Rewrite it with our requested trigger value.
2598 */
2599 RequestTrigger oldTrigger = trigger;
2600
2601 oldTrigger.entryValue = entry.data.u8[0];
2602
2603 mTriggerReplacedMap.add(tag, oldTrigger);
2604 } else {
2605 /**
2606 * More typical, no trigger entry, so we just add it
2607 */
2608 mTriggerRemovedMap.add(tag, trigger);
2609 }
2610
2611 status_t res;
2612
2613 switch (trigger.getTagType()) {
2614 case TYPE_BYTE: {
2615 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2616 res = metadata.update(tag,
2617 &entryValue,
2618 /*count*/1);
2619 break;
2620 }
2621 case TYPE_INT32:
2622 res = metadata.update(tag,
2623 &trigger.entryValue,
2624 /*count*/1);
2625 break;
2626 default:
2627 ALOGE("%s: Type not supported: 0x%x",
2628 __FUNCTION__,
2629 trigger.getTagType());
2630 return INVALID_OPERATION;
2631 }
2632
2633 if (res != OK) {
2634 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2635 ", value %d", __FUNCTION__, trigger.getTagName(),
2636 trigger.entryValue);
2637 return res;
2638 }
2639
2640 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2641 trigger.getTagName(),
2642 trigger.entryValue);
2643 }
2644
2645 mTriggerMap.clear();
2646
2647 return count;
2648}
2649
2650status_t Camera3Device::RequestThread::removeTriggers(
2651 const sp<CaptureRequest> &request) {
2652 Mutex::Autolock al(mTriggerMutex);
2653
2654 CameraMetadata &metadata = request->mSettings;
2655
2656 /**
2657 * Replace all old entries with their old values.
2658 */
2659 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2660 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2661
2662 status_t res;
2663
2664 uint32_t tag = trigger.metadataTag;
2665 switch (trigger.getTagType()) {
2666 case TYPE_BYTE: {
2667 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2668 res = metadata.update(tag,
2669 &entryValue,
2670 /*count*/1);
2671 break;
2672 }
2673 case TYPE_INT32:
2674 res = metadata.update(tag,
2675 &trigger.entryValue,
2676 /*count*/1);
2677 break;
2678 default:
2679 ALOGE("%s: Type not supported: 0x%x",
2680 __FUNCTION__,
2681 trigger.getTagType());
2682 return INVALID_OPERATION;
2683 }
2684
2685 if (res != OK) {
2686 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2687 ", trigger value %d", __FUNCTION__,
2688 trigger.getTagName(), trigger.entryValue);
2689 return res;
2690 }
2691 }
2692 mTriggerReplacedMap.clear();
2693
2694 /**
2695 * Remove all new entries.
2696 */
2697 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2698 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2699 status_t res = metadata.erase(trigger.metadataTag);
2700
2701 if (res != OK) {
2702 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2703 ", trigger value %d", __FUNCTION__,
2704 trigger.getTagName(), trigger.entryValue);
2705 return res;
2706 }
2707 }
2708 mTriggerRemovedMap.clear();
2709
2710 return OK;
2711}
2712
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002713status_t Camera3Device::RequestThread::addDummyTriggerIds(
2714 const sp<CaptureRequest> &request) {
2715 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2716 static const int32_t dummyTriggerId = 1;
2717 status_t res;
2718
2719 CameraMetadata &metadata = request->mSettings;
2720
2721 // If AF trigger is active, insert a dummy AF trigger ID if none already
2722 // exists
2723 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2724 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2725 if (afTrigger.count > 0 &&
2726 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2727 afId.count == 0) {
2728 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2729 if (res != OK) return res;
2730 }
2731
2732 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2733 // if none already exists
2734 camera_metadata_entry pcTrigger =
2735 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2736 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2737 if (pcTrigger.count > 0 &&
2738 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2739 pcId.count == 0) {
2740 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2741 &dummyTriggerId, 1);
2742 if (res != OK) return res;
2743 }
2744
2745 return OK;
2746}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002747
2748
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002749/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002750 * Static callback forwarding methods from HAL to instance
2751 */
2752
2753void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2754 const camera3_capture_result *result) {
2755 Camera3Device *d =
2756 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2757 d->processCaptureResult(result);
2758}
2759
2760void Camera3Device::sNotify(const camera3_callback_ops *cb,
2761 const camera3_notify_msg *msg) {
2762 Camera3Device *d =
2763 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2764 d->notify(msg);
2765}
2766
2767}; // namespace android