blob: a700f308d798d7875405a0feef5d44a8e625adff [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Igor Murashkinff3e31d2013-10-23 16:40:06 -070044#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045#include "device3/Camera3Device.h"
46#include "device3/Camera3OutputStream.h"
47#include "device3/Camera3InputStream.h"
48#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080049
50using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051
52namespace android {
53
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054Camera3Device::Camera3Device(int id):
55 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070058 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070059 mNextResultFrameNumber(0),
60 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062{
63 ATRACE_CALL();
64 camera3_callback_ops::notify = &sNotify;
65 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
66 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
67}
68
69Camera3Device::~Camera3Device()
70{
71 ATRACE_CALL();
72 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
73 disconnect();
74}
75
Igor Murashkin71381052013-03-04 14:53:08 -080076int Camera3Device::getId() const {
77 return mId;
78}
79
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080080/**
81 * CameraDeviceBase interface
82 */
83
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084status_t Camera3Device::initialize(camera_module_t *module)
85{
86 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070087 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088 Mutex::Autolock l(mLock);
89
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070092 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 return INVALID_OPERATION;
94 }
95
96 /** Open HAL device */
97
98 status_t res;
99 String8 deviceName = String8::format("%d", mId);
100
101 camera3_device_t *device;
102
Zhijun He213ce792013-11-19 08:45:15 -0800103 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 res = module->common.methods->open(&module->common, deviceName.string(),
105 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800106 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107
108 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 return res;
111 }
112
113 /** Cross-check device version */
114
115 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 device->common.version);
120 device->common.close(&device->common);
121 return BAD_VALUE;
122 }
123
124 camera_info info;
125 res = module->get_camera_info(mId, &info);
126 if (res != OK) return res;
127
128 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
130 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version, info.device_version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 /** Initialize device with callback functions */
137
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700138 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_END();
141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
144 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Get vendor metadata tags */
150
151 mVendorTagOps.get_camera_vendor_section_name = NULL;
152
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700155 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156
157 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
158 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
159 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700160 SET_ERR_L("Unable to set tag ops: %s (%d)",
161 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 device->common.close(&device->common);
163 return res;
164 }
165 }
166
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700167 /** Start up status tracker thread */
168 mStatusTracker = new StatusTracker(this);
169 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
170 if (res != OK) {
171 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
172 strerror(-res), res);
173 device->common.close(&device->common);
174 mStatusTracker.clear();
175 return res;
176 }
177
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178 /** Start up request queue thread */
179
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700180 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800181 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700183 SET_ERR_L("Unable to start request queue thread: %s (%d)",
184 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 return res;
188 }
189
190 /** Everything is good to go */
191
192 mDeviceInfo = info.static_camera_characteristics;
193 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700194 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700196 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700197 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700199 /** Check for quirks */
200
201 // Will the HAL be sending in early partial result metadata?
202 camera_metadata_entry partialResultsQuirk =
203 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
204 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
205 mUsePartialResultQuirk = true;
206 }
207
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208 return OK;
209}
210
211status_t Camera3Device::disconnect() {
212 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800214
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215 ALOGV("%s: E", __FUNCTION__);
216
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700217 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800218
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 {
220 Mutex::Autolock l(mLock);
221 if (mStatus == STATUS_UNINITIALIZED) return res;
222
223 if (mStatus == STATUS_ACTIVE ||
224 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
225 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700226 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700227 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700228 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229 } else {
230 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
231 if (res != OK) {
232 SET_ERR_L("Timeout waiting for HAL to drain");
233 // Continue to close device even in case of error
234 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700235 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800236 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800237
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 if (mStatus == STATUS_ERROR) {
239 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700240 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700241
242 if (mStatusTracker != NULL) {
243 mStatusTracker->requestExit();
244 }
245
246 if (mRequestThread != NULL) {
247 mRequestThread->requestExit();
248 }
249
250 mOutputStreams.clear();
251 mInputStream.clear();
252 }
253
254 // Joining done without holding mLock, otherwise deadlocks may ensue
255 // as the threads try to access parent state
256 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
257 // HAL may be in a bad state, so waiting for request thread
258 // (which may be stuck in the HAL processCaptureRequest call)
259 // could be dangerous.
260 mRequestThread->join();
261 }
262
263 if (mStatusTracker != NULL) {
264 mStatusTracker->join();
265 }
266
267 {
268 Mutex::Autolock l(mLock);
269
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800270 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800274 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800276 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 mHal3Device = NULL;
278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700280 mStatus = STATUS_UNINITIALIZED;
281 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
283 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700284 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800285}
286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287// For dumping/debugging only -
288// try to acquire a lock a few times, eventually give up to proceed with
289// debug/dump operations
290bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
291 bool gotLock = false;
292 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
293 if (lock.tryLock() == NO_ERROR) {
294 gotLock = true;
295 break;
296 } else {
297 usleep(kDumpSleepDuration);
298 }
299 }
300 return gotLock;
301}
302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
304 ATRACE_CALL();
305 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306
307 // Try to lock, but continue in case of failure (to avoid blocking in
308 // deadlocks)
309 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
310 bool gotLock = tryLockSpinRightRound(mLock);
311
312 ALOGW_IF(!gotInterfaceLock,
313 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
314 mId, __FUNCTION__);
315 ALOGW_IF(!gotLock,
316 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
317 mId, __FUNCTION__);
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800320
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321 const char *status =
322 mStatus == STATUS_ERROR ? "ERROR" :
323 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700324 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
325 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 mStatus == STATUS_ACTIVE ? "ACTIVE" :
327 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700328
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700330 if (mStatus == STATUS_ERROR) {
331 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
332 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333 lines.appendFormat(" Stream configuration:\n");
334
335 if (mInputStream != NULL) {
336 write(fd, lines.string(), lines.size());
337 mInputStream->dump(fd, args);
338 } else {
339 lines.appendFormat(" No input stream.\n");
340 write(fd, lines.string(), lines.size());
341 }
342 for (size_t i = 0; i < mOutputStreams.size(); i++) {
343 mOutputStreams[i]->dump(fd,args);
344 }
345
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700346 lines = String8(" In-flight requests:\n");
347 if (mInFlightMap.size() == 0) {
348 lines.append(" None\n");
349 } else {
350 for (size_t i = 0; i < mInFlightMap.size(); i++) {
351 InFlightRequest r = mInFlightMap.valueAt(i);
352 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
353 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
354 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
355 r.numBuffersLeft);
356 }
357 }
358 write(fd, lines.string(), lines.size());
359
Igor Murashkin1e479c02013-09-06 16:55:14 -0700360 {
361 lines = String8(" Last request sent:\n");
362 write(fd, lines.string(), lines.size());
363
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700364 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700365 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
366 }
367
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800368 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700369 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370 write(fd, lines.string(), lines.size());
371 mHal3Device->ops->dump(mHal3Device, fd);
372 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800373
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700374 if (gotLock) mLock.unlock();
375 if (gotInterfaceLock) mInterfaceLock.unlock();
376
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800377 return OK;
378}
379
380const CameraMetadata& Camera3Device::info() const {
381 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800382 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
383 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700384 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 mStatus == STATUS_ERROR ?
386 "when in error state" : "before init");
387 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800388 return mDeviceInfo;
389}
390
Jianing Wei90e59c92014-03-12 18:29:36 -0700391status_t Camera3Device::checkStatusOkToCaptureLocked() {
392 switch (mStatus) {
393 case STATUS_ERROR:
394 CLOGE("Device has encountered a serious error");
395 return INVALID_OPERATION;
396 case STATUS_UNINITIALIZED:
397 CLOGE("Device not initialized");
398 return INVALID_OPERATION;
399 case STATUS_UNCONFIGURED:
400 case STATUS_CONFIGURED:
401 case STATUS_ACTIVE:
402 // OK
403 break;
404 default:
405 SET_ERR_L("Unexpected status: %d", mStatus);
406 return INVALID_OPERATION;
407 }
408 return OK;
409}
410
411status_t Camera3Device::convertMetadataListToRequestListLocked(
412 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
413 if (requestList == NULL) {
414 CLOGE("requestList cannot be NULL.");
415 return BAD_VALUE;
416 }
417
418 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
419 it != metadataList.end(); ++it) {
420 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
421 if (newRequest == 0) {
422 CLOGE("Can't create capture request");
423 return BAD_VALUE;
424 }
425 requestList->push_back(newRequest);
426 }
427 return OK;
428}
429
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800430status_t Camera3Device::capture(CameraMetadata &request) {
431 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700432 status_t res;
433 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800434 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800435
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700436 // TODO: take ownership of the request
437
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800438 switch (mStatus) {
439 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700440 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800441 return INVALID_OPERATION;
442 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700443 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700445 case STATUS_UNCONFIGURED:
446 // May be lazily configuring streams, will check during setup
447 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 case STATUS_ACTIVE:
449 // OK
450 break;
451 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700452 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800453 return INVALID_OPERATION;
454 }
455
456 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
457 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700458 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 return BAD_VALUE;
460 }
461
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700462 res = mRequestThread->queueRequest(newRequest);
463 if (res == OK) {
464 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
465 if (res != OK) {
466 SET_ERR_L("Can't transition to active in %f seconds!",
467 kActiveTimeout/1e9);
468 }
469 ALOGV("Camera %d: Capture request enqueued", mId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700470 } else {
471 CLOGE("Cannot queue request. Impossible."); // queueRequest always returns OK.
472 return BAD_VALUE;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700473 }
474 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800475}
476
Jianing Wei90e59c92014-03-12 18:29:36 -0700477status_t Camera3Device::submitRequestsHelper(
478 const List<const CameraMetadata> &requests, bool repeating) {
479 ATRACE_CALL();
480 Mutex::Autolock il(mInterfaceLock);
481 Mutex::Autolock l(mLock);
482
483 status_t res = checkStatusOkToCaptureLocked();
484 if (res != OK) {
485 // error logged by previous call
486 return res;
487 }
488
489 RequestList requestList;
490
491 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
492 if (res != OK) {
493 // error logged by previous call
494 return res;
495 }
496
497 if (repeating) {
498 res = mRequestThread->setRepeatingRequests(requestList);
499 } else {
500 res = mRequestThread->queueRequestList(requestList);
501 }
502
503 if (res == OK) {
504 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
505 if (res != OK) {
506 SET_ERR_L("Can't transition to active in %f seconds!",
507 kActiveTimeout/1e9);
508 }
509 ALOGV("Camera %d: Capture request enqueued", mId);
510 } else {
511 CLOGE("Cannot queue request. Impossible.");
512 return BAD_VALUE;
513 }
514
515 return res;
516}
517
518status_t Camera3Device::captureList(const List<const CameraMetadata> &requests) {
519 ATRACE_CALL();
520
521 return submitRequestsHelper(requests, /*repeating*/false);
522}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800523
524status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
525 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700526 status_t res;
527 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800528 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800530 switch (mStatus) {
531 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700532 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800533 return INVALID_OPERATION;
534 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700535 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800536 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700537 case STATUS_UNCONFIGURED:
538 // May be lazily configuring streams, will check during setup
539 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 case STATUS_ACTIVE:
541 // OK
542 break;
543 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800545 return INVALID_OPERATION;
546 }
547
548 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
549 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700550 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800551 return BAD_VALUE;
552 }
553
554 RequestList newRepeatingRequests;
555 newRepeatingRequests.push_back(newRepeatingRequest);
556
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700557 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
558 if (res == OK) {
559 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
560 if (res != OK) {
561 SET_ERR_L("Can't transition to active in %f seconds!",
562 kActiveTimeout/1e9);
563 }
564 ALOGV("Camera %d: Repeating request set", mId);
565 }
566 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800567}
568
Jianing Wei90e59c92014-03-12 18:29:36 -0700569status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests) {
570 ATRACE_CALL();
571
572 return submitRequestsHelper(requests, /*repeating*/true);
573}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800574
575sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
576 const CameraMetadata &request) {
577 status_t res;
578
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700579 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800580 res = configureStreamsLocked();
581 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700582 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800583 return NULL;
584 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700585 if (mStatus == STATUS_UNCONFIGURED) {
586 CLOGE("No streams configured");
587 return NULL;
588 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589 }
590
591 sp<CaptureRequest> newRequest = createCaptureRequest(request);
592 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800593}
594
595status_t Camera3Device::clearStreamingRequest() {
596 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700597 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800598 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800600 switch (mStatus) {
601 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700602 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 return INVALID_OPERATION;
604 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700605 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800606 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700607 case STATUS_UNCONFIGURED:
608 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800609 case STATUS_ACTIVE:
610 // OK
611 break;
612 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700613 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800614 return INVALID_OPERATION;
615 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800618}
619
620status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
621 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700622 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800623
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700624 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625}
626
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700627status_t Camera3Device::createInputStream(
628 uint32_t width, uint32_t height, int format, int *id) {
629 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700630 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700631 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700632 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
633 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700634
635 status_t res;
636 bool wasActive = false;
637
638 switch (mStatus) {
639 case STATUS_ERROR:
640 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
641 return INVALID_OPERATION;
642 case STATUS_UNINITIALIZED:
643 ALOGE("%s: Device not initialized", __FUNCTION__);
644 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700645 case STATUS_UNCONFIGURED:
646 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700647 // OK
648 break;
649 case STATUS_ACTIVE:
650 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700651 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700652 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700654 return res;
655 }
656 wasActive = true;
657 break;
658 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700659 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700660 return INVALID_OPERATION;
661 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700663
664 if (mInputStream != 0) {
665 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
666 return INVALID_OPERATION;
667 }
668
669 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
670 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700671 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700672
673 mInputStream = newStream;
674
675 *id = mNextStreamId++;
676
677 // Continue captures if active at start
678 if (wasActive) {
679 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
680 res = configureStreamsLocked();
681 if (res != OK) {
682 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
683 __FUNCTION__, mNextStreamId, strerror(-res), res);
684 return res;
685 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700686 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700687 }
688
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700689 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700690 return OK;
691}
692
Igor Murashkin2fba5842013-04-22 14:03:54 -0700693
694status_t Camera3Device::createZslStream(
695 uint32_t width, uint32_t height,
696 int depth,
697 /*out*/
698 int *id,
699 sp<Camera3ZslStream>* zslStream) {
700 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700702 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
704 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700705
706 status_t res;
707 bool wasActive = false;
708
709 switch (mStatus) {
710 case STATUS_ERROR:
711 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
712 return INVALID_OPERATION;
713 case STATUS_UNINITIALIZED:
714 ALOGE("%s: Device not initialized", __FUNCTION__);
715 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716 case STATUS_UNCONFIGURED:
717 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700718 // OK
719 break;
720 case STATUS_ACTIVE:
721 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700722 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700723 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700724 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700725 return res;
726 }
727 wasActive = true;
728 break;
729 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700730 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700731 return INVALID_OPERATION;
732 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700734
735 if (mInputStream != 0) {
736 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
737 return INVALID_OPERATION;
738 }
739
740 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
741 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700742 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700743
744 res = mOutputStreams.add(mNextStreamId, newStream);
745 if (res < 0) {
746 ALOGE("%s: Can't add new stream to set: %s (%d)",
747 __FUNCTION__, strerror(-res), res);
748 return res;
749 }
750 mInputStream = newStream;
751
752 *id = mNextStreamId++;
753 *zslStream = newStream;
754
755 // Continue captures if active at start
756 if (wasActive) {
757 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
758 res = configureStreamsLocked();
759 if (res != OK) {
760 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
761 __FUNCTION__, mNextStreamId, strerror(-res), res);
762 return res;
763 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700764 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 }
766
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700767 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700768 return OK;
769}
770
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800771status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
772 uint32_t width, uint32_t height, int format, size_t size, int *id) {
773 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800775 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
777 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800778
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800779 status_t res;
780 bool wasActive = false;
781
782 switch (mStatus) {
783 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700784 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800785 return INVALID_OPERATION;
786 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700787 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 case STATUS_UNCONFIGURED:
790 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791 // OK
792 break;
793 case STATUS_ACTIVE:
794 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700795 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 return res;
799 }
800 wasActive = true;
801 break;
802 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700803 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800804 return INVALID_OPERATION;
805 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700806 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800807
808 sp<Camera3OutputStream> newStream;
809 if (format == HAL_PIXEL_FORMAT_BLOB) {
810 newStream = new Camera3OutputStream(mNextStreamId, consumer,
811 width, height, size, format);
812 } else {
813 newStream = new Camera3OutputStream(mNextStreamId, consumer,
814 width, height, format);
815 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700816 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817
818 res = mOutputStreams.add(mNextStreamId, newStream);
819 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700820 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 return res;
822 }
823
824 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700825 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826
827 // Continue captures if active at start
828 if (wasActive) {
829 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
830 res = configureStreamsLocked();
831 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700832 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
833 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 return res;
835 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700836 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840}
841
842status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
843 ATRACE_CALL();
844 (void)outputId; (void)id;
845
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800847 return INVALID_OPERATION;
848}
849
850
851status_t Camera3Device::getStreamInfo(int id,
852 uint32_t *width, uint32_t *height, uint32_t *format) {
853 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800855 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800856
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857 switch (mStatus) {
858 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700859 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800860 return INVALID_OPERATION;
861 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700862 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800863 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700864 case STATUS_UNCONFIGURED:
865 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 case STATUS_ACTIVE:
867 // OK
868 break;
869 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700870 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 return INVALID_OPERATION;
872 }
873
874 ssize_t idx = mOutputStreams.indexOfKey(id);
875 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 return idx;
878 }
879
880 if (width) *width = mOutputStreams[idx]->getWidth();
881 if (height) *height = mOutputStreams[idx]->getHeight();
882 if (format) *format = mOutputStreams[idx]->getFormat();
883
884 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800885}
886
887status_t Camera3Device::setStreamTransform(int id,
888 int transform) {
889 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800892
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 switch (mStatus) {
894 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700895 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 return INVALID_OPERATION;
897 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700898 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700900 case STATUS_UNCONFIGURED:
901 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 case STATUS_ACTIVE:
903 // OK
904 break;
905 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700906 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 return INVALID_OPERATION;
908 }
909
910 ssize_t idx = mOutputStreams.indexOfKey(id);
911 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700912 CLOGE("Stream %d does not exist",
913 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 return BAD_VALUE;
915 }
916
917 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918}
919
920status_t Camera3Device::deleteStream(int id) {
921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 Mutex::Autolock l(mLock);
924 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800925
Igor Murashkine2172be2013-05-28 15:31:39 -0700926 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 // CameraDevice semantics require device to already be idle before
929 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700931 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
932 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 }
934
Igor Murashkin2fba5842013-04-22 14:03:54 -0700935 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800936 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937 if (mInputStream != NULL && id == mInputStream->getId()) {
938 deletedStream = mInputStream;
939 mInputStream.clear();
940 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800941 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700942 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800943 return BAD_VALUE;
944 }
Zhijun He5f446352014-01-22 09:49:33 -0800945 }
946
947 // Delete output stream or the output part of a bi-directional stream.
948 if (outputStreamIdx != NAME_NOT_FOUND) {
949 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 mOutputStreams.removeItem(id);
951 }
952
953 // Free up the stream endpoint so that it can be used by some other stream
954 res = deletedStream->disconnect();
955 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700956 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800957 // fall through since we want to still list the stream as deleted.
958 }
959 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700960 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961
962 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800963}
964
965status_t Camera3Device::deleteReprocessStream(int id) {
966 ATRACE_CALL();
967 (void)id;
968
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700969 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800970 return INVALID_OPERATION;
971}
972
973
974status_t Camera3Device::createDefaultRequest(int templateId,
975 CameraMetadata *request) {
976 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700977 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 Mutex::Autolock l(mLock);
980
981 switch (mStatus) {
982 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700983 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 return INVALID_OPERATION;
985 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700988 case STATUS_UNCONFIGURED:
989 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 case STATUS_ACTIVE:
991 // OK
992 break;
993 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700994 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995 return INVALID_OPERATION;
996 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800997
998 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700999 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001000 rawRequest = mHal3Device->ops->construct_default_request_settings(
1001 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001002 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001003 if (rawRequest == NULL) {
1004 SET_ERR_L("HAL is unable to construct default settings for template %d",
1005 templateId);
1006 return DEAD_OBJECT;
1007 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001008 *request = rawRequest;
1009
1010 return OK;
1011}
1012
1013status_t Camera3Device::waitUntilDrained() {
1014 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001015 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 switch (mStatus) {
1019 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001020 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001021 ALOGV("%s: Already idle", __FUNCTION__);
1022 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001023 case STATUS_CONFIGURED:
1024 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 case STATUS_ERROR:
1026 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001027 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001028 break;
1029 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001030 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 return INVALID_OPERATION;
1032 }
1033
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001034 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1035 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1036 return res;
1037}
1038
1039// Pause to reconfigure
1040status_t Camera3Device::internalPauseAndWaitLocked() {
1041 mRequestThread->setPaused(true);
1042 mPauseStateNotify = true;
1043
1044 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1045 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1046 if (res != OK) {
1047 SET_ERR_L("Can't idle device in %f seconds!",
1048 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001049 }
1050
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001051 return res;
1052}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001053
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054// Resume after internalPauseAndWaitLocked
1055status_t Camera3Device::internalResumeLocked() {
1056 status_t res;
1057
1058 mRequestThread->setPaused(false);
1059
1060 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1061 if (res != OK) {
1062 SET_ERR_L("Can't transition to active in %f seconds!",
1063 kActiveTimeout/1e9);
1064 }
1065 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001067}
1068
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001069status_t Camera3Device::waitUntilStateThenRelock(bool active,
1070 nsecs_t timeout) {
1071 status_t res = OK;
1072 if (active == (mStatus == STATUS_ACTIVE)) {
1073 // Desired state already reached
1074 return res;
1075 }
1076
1077 bool stateSeen = false;
1078 do {
1079 mRecentStatusUpdates.clear();
1080
1081 res = mStatusChanged.waitRelative(mLock, timeout);
1082 if (res != OK) break;
1083
1084 // Check state change history during wait
1085 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1086 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1087 stateSeen = true;
1088 break;
1089 }
1090 }
1091 } while (!stateSeen);
1092
1093 return res;
1094}
1095
1096
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001097status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1098 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001099 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001100
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001101 if (listener != NULL && mListener != NULL) {
1102 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1103 }
1104 mListener = listener;
1105
1106 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001107}
1108
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001109bool Camera3Device::willNotify3A() {
1110 return false;
1111}
1112
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001113status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001114 status_t res;
1115 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001117 while (mResultQueue.empty()) {
1118 res = mResultSignal.waitRelative(mOutputLock, timeout);
1119 if (res == TIMED_OUT) {
1120 return res;
1121 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001122 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1123 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001124 return res;
1125 }
1126 }
1127 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001128}
1129
1130status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1131 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001132 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001133
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001134 if (mResultQueue.empty()) {
1135 return NOT_ENOUGH_DATA;
1136 }
1137
1138 CameraMetadata &result = *(mResultQueue.begin());
1139 frame->acquire(result);
1140 mResultQueue.erase(mResultQueue.begin());
1141
1142 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143}
1144
1145status_t Camera3Device::triggerAutofocus(uint32_t id) {
1146 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001147 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001148
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001149 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1150 // Mix-in this trigger into the next request and only the next request.
1151 RequestTrigger trigger[] = {
1152 {
1153 ANDROID_CONTROL_AF_TRIGGER,
1154 ANDROID_CONTROL_AF_TRIGGER_START
1155 },
1156 {
1157 ANDROID_CONTROL_AF_TRIGGER_ID,
1158 static_cast<int32_t>(id)
1159 },
1160 };
1161
1162 return mRequestThread->queueTrigger(trigger,
1163 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164}
1165
1166status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1167 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001169
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001170 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1171 // Mix-in this trigger into the next request and only the next request.
1172 RequestTrigger trigger[] = {
1173 {
1174 ANDROID_CONTROL_AF_TRIGGER,
1175 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1176 },
1177 {
1178 ANDROID_CONTROL_AF_TRIGGER_ID,
1179 static_cast<int32_t>(id)
1180 },
1181 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001182
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001183 return mRequestThread->queueTrigger(trigger,
1184 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185}
1186
1187status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1188 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001189 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001191 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1192 // Mix-in this trigger into the next request and only the next request.
1193 RequestTrigger trigger[] = {
1194 {
1195 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1196 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1197 },
1198 {
1199 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1200 static_cast<int32_t>(id)
1201 },
1202 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001204 return mRequestThread->queueTrigger(trigger,
1205 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001206}
1207
1208status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1209 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1210 ATRACE_CALL();
1211 (void)reprocessStreamId; (void)buffer; (void)listener;
1212
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001213 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214 return INVALID_OPERATION;
1215}
1216
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001217status_t Camera3Device::flush() {
1218 ATRACE_CALL();
1219 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001221 Mutex::Autolock l(mLock);
1222
1223 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001224 status_t res;
1225 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1226 res = mHal3Device->ops->flush(mHal3Device);
1227 } else {
1228 res = waitUntilDrained();
1229 }
1230
1231 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001232}
1233
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001234/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001235 * Methods called by subclasses
1236 */
1237
1238void Camera3Device::notifyStatus(bool idle) {
1239 {
1240 // Need mLock to safely update state and synchronize to current
1241 // state of methods in flight.
1242 Mutex::Autolock l(mLock);
1243 // We can get various system-idle notices from the status tracker
1244 // while starting up. Only care about them if we've actually sent
1245 // in some requests recently.
1246 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1247 return;
1248 }
1249 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1250 idle ? "idle" : "active");
1251 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1252 mRecentStatusUpdates.add(mStatus);
1253 mStatusChanged.signal();
1254
1255 // Skip notifying listener if we're doing some user-transparent
1256 // state changes
1257 if (mPauseStateNotify) return;
1258 }
1259 NotificationListener *listener;
1260 {
1261 Mutex::Autolock l(mOutputLock);
1262 listener = mListener;
1263 }
1264 if (idle && listener != NULL) {
1265 listener->notifyIdle();
1266 }
1267}
1268
1269/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 * Camera3Device private methods
1271 */
1272
1273sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1274 const CameraMetadata &request) {
1275 ATRACE_CALL();
1276 status_t res;
1277
1278 sp<CaptureRequest> newRequest = new CaptureRequest;
1279 newRequest->mSettings = request;
1280
1281 camera_metadata_entry_t inputStreams =
1282 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1283 if (inputStreams.count > 0) {
1284 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001285 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001286 CLOGE("Request references unknown input stream %d",
1287 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001288 return NULL;
1289 }
1290 // Lazy completion of stream configuration (allocation/registration)
1291 // on first use
1292 if (mInputStream->isConfiguring()) {
1293 res = mInputStream->finishConfiguration(mHal3Device);
1294 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001295 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001296 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001297 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001298 return NULL;
1299 }
1300 }
1301
1302 newRequest->mInputStream = mInputStream;
1303 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1304 }
1305
1306 camera_metadata_entry_t streams =
1307 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1308 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001309 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001310 return NULL;
1311 }
1312
1313 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001314 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001315 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 CLOGE("Request references unknown stream %d",
1317 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001318 return NULL;
1319 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001320 sp<Camera3OutputStreamInterface> stream =
1321 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322
1323 // Lazy completion of stream configuration (allocation/registration)
1324 // on first use
1325 if (stream->isConfiguring()) {
1326 res = stream->finishConfiguration(mHal3Device);
1327 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001328 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1329 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330 return NULL;
1331 }
1332 }
1333
1334 newRequest->mOutputStreams.push(stream);
1335 }
1336 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1337
1338 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001339}
1340
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341status_t Camera3Device::configureStreamsLocked() {
1342 ATRACE_CALL();
1343 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001344
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001345 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001346 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001347 return INVALID_OPERATION;
1348 }
1349
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001350 if (!mNeedConfig) {
1351 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1352 return OK;
1353 }
1354
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001355 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001356 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357
1358 camera3_stream_configuration config;
1359
1360 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1361
1362 Vector<camera3_stream_t*> streams;
1363 streams.setCapacity(config.num_streams);
1364
1365 if (mInputStream != NULL) {
1366 camera3_stream_t *inputStream;
1367 inputStream = mInputStream->startConfiguration();
1368 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 return INVALID_OPERATION;
1371 }
1372 streams.add(inputStream);
1373 }
1374
1375 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001376
1377 // Don't configure bidi streams twice, nor add them twice to the list
1378 if (mOutputStreams[i].get() ==
1379 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1380
1381 config.num_streams--;
1382 continue;
1383 }
1384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385 camera3_stream_t *outputStream;
1386 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1387 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001388 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 return INVALID_OPERATION;
1390 }
1391 streams.add(outputStream);
1392 }
1393
1394 config.streams = streams.editArray();
1395
1396 // Do the HAL configuration; will potentially touch stream
1397 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001398 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001399 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001400 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001401
1402 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001403 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1404 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001405 return res;
1406 }
1407
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001408 // Finish all stream configuration immediately.
1409 // TODO: Try to relax this later back to lazy completion, which should be
1410 // faster
1411
Igor Murashkin073f8572013-05-02 14:59:28 -07001412 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001413 res = mInputStream->finishConfiguration(mHal3Device);
1414 if (res != OK) {
1415 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1416 mInputStream->getId(), strerror(-res), res);
1417 return res;
1418 }
1419 }
1420
1421 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001422 sp<Camera3OutputStreamInterface> outputStream =
1423 mOutputStreams.editValueAt(i);
1424 if (outputStream->isConfiguring()) {
1425 res = outputStream->finishConfiguration(mHal3Device);
1426 if (res != OK) {
1427 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1428 outputStream->getId(), strerror(-res), res);
1429 return res;
1430 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001431 }
1432 }
1433
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434 // Request thread needs to know to avoid using repeat-last-settings protocol
1435 // across configure_streams() calls
1436 mRequestThread->configurationComplete();
1437
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001438 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001440 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001441
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001442 if (config.num_streams > 0) {
1443 mStatus = STATUS_CONFIGURED;
1444 } else {
1445 mStatus = STATUS_UNCONFIGURED;
1446 }
1447
1448 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1449
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001450 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001451}
1452
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001453void Camera3Device::setErrorState(const char *fmt, ...) {
1454 Mutex::Autolock l(mLock);
1455 va_list args;
1456 va_start(args, fmt);
1457
1458 setErrorStateLockedV(fmt, args);
1459
1460 va_end(args);
1461}
1462
1463void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1464 Mutex::Autolock l(mLock);
1465 setErrorStateLockedV(fmt, args);
1466}
1467
1468void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1469 va_list args;
1470 va_start(args, fmt);
1471
1472 setErrorStateLockedV(fmt, args);
1473
1474 va_end(args);
1475}
1476
1477void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001478 // Print out all error messages to log
1479 String8 errorCause = String8::formatV(fmt, args);
1480 ALOGE("Camera %d: %s", mId, errorCause.string());
1481
1482 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001483 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001484
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001485 // Save stack trace. View by dumping it later.
1486 CameraTraces::saveTrace();
1487 // TODO: consider adding errorCause and client pid/procname
1488
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001489 mErrorCause = errorCause;
1490
1491 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001492 mStatus = STATUS_ERROR;
1493}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494
1495/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001496 * In-flight request management
1497 */
1498
1499status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001500 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001501 ATRACE_CALL();
1502 Mutex::Autolock l(mInFlightLock);
1503
1504 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001505 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001506 if (res < 0) return res;
1507
1508 return OK;
1509}
1510
1511/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001512 * QUIRK(partial results)
1513 * Check if all 3A fields are ready, and send off a partial 3A-only result
1514 * to the output frame queue
1515 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001516bool Camera3Device::processPartial3AQuirk(
1517 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001518 const CameraMetadata& partial) {
1519
1520 // Check if all 3A states are present
1521 // The full list of fields is
1522 // android.control.afMode
1523 // android.control.awbMode
1524 // android.control.aeState
1525 // android.control.awbState
1526 // android.control.afState
1527 // android.control.afTriggerID
1528 // android.control.aePrecaptureID
1529 // TODO: Add android.control.aeMode
1530
1531 bool gotAllStates = true;
1532
1533 uint8_t afMode;
1534 uint8_t awbMode;
1535 uint8_t aeState;
1536 uint8_t afState;
1537 uint8_t awbState;
1538 int32_t afTriggerId;
1539 int32_t aeTriggerId;
1540
1541 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1542 &afMode, frameNumber);
1543
1544 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1545 &awbMode, frameNumber);
1546
1547 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1548 &aeState, frameNumber);
1549
1550 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1551 &afState, frameNumber);
1552
1553 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1554 &awbState, frameNumber);
1555
1556 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1557 &afTriggerId, frameNumber);
1558
1559 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1560 &aeTriggerId, frameNumber);
1561
1562 if (!gotAllStates) return false;
1563
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001564 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001565 "AF state %d, AE state %d, AWB state %d, "
1566 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001567 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001568 afMode, awbMode,
1569 afState, aeState, awbState,
1570 afTriggerId, aeTriggerId);
1571
1572 // Got all states, so construct a minimal result to send
1573 // In addition to the above fields, this means adding in
1574 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001575 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001576 // android.quirks.partialResult
1577
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001578 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001579
1580 Mutex::Autolock l(mOutputLock);
1581
1582 CameraMetadata& min3AResult =
1583 *mResultQueue.insert(
1584 mResultQueue.end(),
1585 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1586
1587 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1588 &frameNumber, frameNumber)) {
1589 return false;
1590 }
1591
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001592 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1593 &requestId, frameNumber)) {
1594 return false;
1595 }
1596
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001597 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1598 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1599 &partialResult, frameNumber)) {
1600 return false;
1601 }
1602
1603 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1604 &afMode, frameNumber)) {
1605 return false;
1606 }
1607
1608 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1609 &awbMode, frameNumber)) {
1610 return false;
1611 }
1612
1613 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1614 &aeState, frameNumber)) {
1615 return false;
1616 }
1617
1618 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1619 &afState, frameNumber)) {
1620 return false;
1621 }
1622
1623 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1624 &awbState, frameNumber)) {
1625 return false;
1626 }
1627
1628 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1629 &afTriggerId, frameNumber)) {
1630 return false;
1631 }
1632
1633 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1634 &aeTriggerId, frameNumber)) {
1635 return false;
1636 }
1637
1638 mResultSignal.signal();
1639
1640 return true;
1641}
1642
1643template<typename T>
1644bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1645 T* value, int32_t frameNumber) {
1646 (void) frameNumber;
1647
1648 camera_metadata_ro_entry_t entry;
1649
1650 entry = result.find(tag);
1651 if (entry.count == 0) {
1652 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1653 mId, frameNumber, get_camera_metadata_tag_name(tag));
1654 return false;
1655 }
1656
1657 if (sizeof(T) == sizeof(uint8_t)) {
1658 *value = entry.data.u8[0];
1659 } else if (sizeof(T) == sizeof(int32_t)) {
1660 *value = entry.data.i32[0];
1661 } else {
1662 ALOGE("%s: Unexpected type", __FUNCTION__);
1663 return false;
1664 }
1665 return true;
1666}
1667
1668template<typename T>
1669bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1670 const T* value, int32_t frameNumber) {
1671 if (result.update(tag, value, 1) != NO_ERROR) {
1672 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1673 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1674 frameNumber, get_camera_metadata_tag_name(tag));
1675 return false;
1676 }
1677 return true;
1678}
1679
1680/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001681 * Camera HAL device callback methods
1682 */
1683
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001684void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001685 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001686
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001687 status_t res;
1688
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001689 uint32_t frameNumber = result->frame_number;
1690 if (result->result == NULL && result->num_output_buffers == 0) {
1691 SET_ERR("No result data provided by HAL for frame %d",
1692 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001693 return;
1694 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001695 bool partialResultQuirk = false;
1696 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001697
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001698 // Get capture timestamp from list of in-flight requests, where it was added
1699 // by the shutter notification for this frame. Then update the in-flight
1700 // status and remove the in-flight entry if all result data has been
1701 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001702 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001703 {
1704 Mutex::Autolock l(mInFlightLock);
1705 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1706 if (idx == NAME_NOT_FOUND) {
1707 SET_ERR("Unknown frame number for capture result: %d",
1708 frameNumber);
1709 return;
1710 }
1711 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001712
1713 // Check if this result carries only partial metadata
1714 if (mUsePartialResultQuirk && result->result != NULL) {
1715 camera_metadata_ro_entry_t partialResultEntry;
1716 res = find_camera_metadata_ro_entry(result->result,
1717 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1718 if (res != NAME_NOT_FOUND &&
1719 partialResultEntry.count > 0 &&
1720 partialResultEntry.data.u8[0] ==
1721 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1722 // A partial result. Flag this as such, and collect this
1723 // set of metadata into the in-flight entry.
1724 partialResultQuirk = true;
1725 request.partialResultQuirk.collectedResult.append(
1726 result->result);
1727 request.partialResultQuirk.collectedResult.erase(
1728 ANDROID_QUIRKS_PARTIAL_RESULT);
1729 // Fire off a 3A-only result if possible
1730 if (!request.partialResultQuirk.haveSent3A) {
1731 request.partialResultQuirk.haveSent3A =
1732 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001733 request.requestId,
1734 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001735 }
1736 }
1737 }
1738
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001739 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001740 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001741 * One of the following must happen before it's legal to call process_capture_result,
1742 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001743 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1744 * - CAMERA3_MSG_ERROR (expected during flush)
1745 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001746 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001747 SET_ERR("Called before shutter notify for frame %d",
1748 frameNumber);
1749 return;
1750 }
1751
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001752 // Did we get the (final) result metadata for this capture?
1753 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001754 if (request.haveResultMetadata) {
1755 SET_ERR("Called multiple times with metadata for frame %d",
1756 frameNumber);
1757 return;
1758 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001759 if (mUsePartialResultQuirk &&
1760 !request.partialResultQuirk.collectedResult.isEmpty()) {
1761 collectedQuirkResult.acquire(
1762 request.partialResultQuirk.collectedResult);
1763 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001764 request.haveResultMetadata = true;
1765 }
1766
1767 request.numBuffersLeft -= result->num_output_buffers;
1768
1769 if (request.numBuffersLeft < 0) {
1770 SET_ERR("Too many buffers returned for frame %d",
1771 frameNumber);
1772 return;
1773 }
1774
Zhijun He1b05dfc2013-11-21 12:57:51 -08001775 // Check if everything has arrived for this result (buffers and metadata), remove it from
1776 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1777 if ((request.requestStatus != OK) ||
1778 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001779 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001780 mInFlightMap.removeItemsAt(idx, 1);
1781 }
1782
1783 // Sanity check - if we have too many in-flight frames, something has
1784 // likely gone wrong
1785 if (mInFlightMap.size() > kInFlightWarnLimit) {
1786 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1787 }
1788
1789 }
1790
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001791 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001792 bool gotResult = false;
1793 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001794 Mutex::Autolock l(mOutputLock);
1795
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001796 gotResult = true;
1797
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001798 if (frameNumber != mNextResultFrameNumber) {
1799 SET_ERR("Out-of-order capture result metadata submitted! "
1800 "(got frame number %d, expecting %d)",
1801 frameNumber, mNextResultFrameNumber);
1802 return;
1803 }
1804 mNextResultFrameNumber++;
1805
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001806 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001807 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808
Igor Murashkind2c90692013-04-02 12:32:32 -07001809 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001810 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001811 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001812 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001813 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001814 } else {
1815 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001816 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001817 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001818
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001819 // Append any previous partials to form a complete result
1820 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1821 captureResult.append(collectedQuirkResult);
1822 }
1823
1824 captureResult.sort();
1825
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001826 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001827
1828 camera_metadata_entry entry =
1829 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1830 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001831 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001832 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001833 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001834 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001835 SET_ERR("Timestamp mismatch between shutter notify and result"
1836 " metadata for frame %d (%lld vs %lld respectively)",
1837 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001838 gotResult = false;
1839 }
1840
1841 if (gotResult) {
1842 // Valid result, insert into queue
1843 CameraMetadata& queuedResult =
1844 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1845 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001846 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001847 } // scope for mOutputLock
1848
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001849 // Return completed buffers to their streams with the timestamp
1850
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001851 for (size_t i = 0; i < result->num_output_buffers; i++) {
1852 Camera3Stream *stream =
1853 Camera3Stream::cast(result->output_buffers[i].stream);
1854 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1855 // Note: stream may be deallocated at this point, if this buffer was the
1856 // last reference to it.
1857 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001858 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001859 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001860 }
1861 }
1862
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001863 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001864
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001865 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001866 mResultSignal.signal();
1867 }
1868
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001869}
1870
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001871
1872
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001873void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001874 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001875 NotificationListener *listener;
1876 {
1877 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001878 listener = mListener;
1879 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001880
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001881 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001882 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001883 return;
1884 }
1885
1886 switch (msg->type) {
1887 case CAMERA3_MSG_ERROR: {
1888 int streamId = 0;
1889 if (msg->message.error.error_stream != NULL) {
1890 Camera3Stream *stream =
1891 Camera3Stream::cast(
1892 msg->message.error.error_stream);
1893 streamId = stream->getId();
1894 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001895 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1896 mId, __FUNCTION__, msg->message.error.frame_number,
1897 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001898
1899 // Set request error status for the request in the in-flight tracking
1900 {
1901 Mutex::Autolock l(mInFlightLock);
1902 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1903 if (idx >= 0) {
1904 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1905 }
1906 }
1907
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001908 if (listener != NULL) {
1909 listener->notifyError(msg->message.error.error_code,
1910 msg->message.error.frame_number, streamId);
1911 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001912 break;
1913 }
1914 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001915 ssize_t idx;
1916 uint32_t frameNumber = msg->message.shutter.frame_number;
1917 nsecs_t timestamp = msg->message.shutter.timestamp;
1918 // Verify ordering of shutter notifications
1919 {
1920 Mutex::Autolock l(mOutputLock);
1921 if (frameNumber != mNextShutterFrameNumber) {
1922 SET_ERR("Shutter notification out-of-order. Expected "
1923 "notification for frame %d, got frame %d",
1924 mNextShutterFrameNumber, frameNumber);
1925 break;
1926 }
1927 mNextShutterFrameNumber++;
1928 }
1929
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001930 int32_t requestId = -1;
1931
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001932 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001933 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001934 {
1935 Mutex::Autolock l(mInFlightLock);
1936 idx = mInFlightMap.indexOfKey(frameNumber);
1937 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001938 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1939 r.captureTimestamp = timestamp;
1940 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001941 }
1942 }
1943 if (idx < 0) {
1944 SET_ERR("Shutter notification for non-existent frame number %d",
1945 frameNumber);
1946 break;
1947 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001948 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1949 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001950 // Call listener, if any
1951 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001952 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001953 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001954 break;
1955 }
1956 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001957 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001958 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001959 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001960}
1961
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001962CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001963 ALOGV("%s", __FUNCTION__);
1964
Igor Murashkin1e479c02013-09-06 16:55:14 -07001965 CameraMetadata retVal;
1966
1967 if (mRequestThread != NULL) {
1968 retVal = mRequestThread->getLatestRequest();
1969 }
1970
Igor Murashkin1e479c02013-09-06 16:55:14 -07001971 return retVal;
1972}
1973
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001974/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001975 * RequestThread inner class methods
1976 */
1977
1978Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001979 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980 camera3_device_t *hal3Device) :
1981 Thread(false),
1982 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001983 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001984 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001985 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001986 mReconfigured(false),
1987 mDoPause(false),
1988 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001989 mFrameNumber(0),
1990 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001992}
1993
1994void Camera3Device::RequestThread::configurationComplete() {
1995 Mutex::Autolock l(mRequestLock);
1996 mReconfigured = true;
1997}
1998
1999status_t Camera3Device::RequestThread::queueRequest(
2000 sp<CaptureRequest> request) {
2001 Mutex::Autolock l(mRequestLock);
2002 mRequestQueue.push_back(request);
2003
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002004 unpauseForNewRequests();
2005
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002006 return OK;
2007}
2008
Jianing Wei90e59c92014-03-12 18:29:36 -07002009status_t Camera3Device::RequestThread::queueRequestList(
2010 List<sp<CaptureRequest> > &requests) {
2011 Mutex::Autolock l(mRequestLock);
2012 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2013 ++it) {
2014 mRequestQueue.push_back(*it);
2015 }
2016
2017 unpauseForNewRequests();
2018
2019 return OK;
2020}
2021
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002022
2023status_t Camera3Device::RequestThread::queueTrigger(
2024 RequestTrigger trigger[],
2025 size_t count) {
2026
2027 Mutex::Autolock l(mTriggerMutex);
2028 status_t ret;
2029
2030 for (size_t i = 0; i < count; ++i) {
2031 ret = queueTriggerLocked(trigger[i]);
2032
2033 if (ret != OK) {
2034 return ret;
2035 }
2036 }
2037
2038 return OK;
2039}
2040
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002041int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2042 sp<Camera3Device> d = device.promote();
2043 if (d != NULL) return d->mId;
2044 return 0;
2045}
2046
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002047status_t Camera3Device::RequestThread::queueTriggerLocked(
2048 RequestTrigger trigger) {
2049
2050 uint32_t tag = trigger.metadataTag;
2051 ssize_t index = mTriggerMap.indexOfKey(tag);
2052
2053 switch (trigger.getTagType()) {
2054 case TYPE_BYTE:
2055 // fall-through
2056 case TYPE_INT32:
2057 break;
2058 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002059 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2060 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002061 return INVALID_OPERATION;
2062 }
2063
2064 /**
2065 * Collect only the latest trigger, since we only have 1 field
2066 * in the request settings per trigger tag, and can't send more than 1
2067 * trigger per request.
2068 */
2069 if (index != NAME_NOT_FOUND) {
2070 mTriggerMap.editValueAt(index) = trigger;
2071 } else {
2072 mTriggerMap.add(tag, trigger);
2073 }
2074
2075 return OK;
2076}
2077
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002078status_t Camera3Device::RequestThread::setRepeatingRequests(
2079 const RequestList &requests) {
2080 Mutex::Autolock l(mRequestLock);
2081 mRepeatingRequests.clear();
2082 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2083 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002084
2085 unpauseForNewRequests();
2086
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002087 return OK;
2088}
2089
2090status_t Camera3Device::RequestThread::clearRepeatingRequests() {
2091 Mutex::Autolock l(mRequestLock);
2092 mRepeatingRequests.clear();
2093 return OK;
2094}
2095
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002096status_t Camera3Device::RequestThread::clear() {
2097 Mutex::Autolock l(mRequestLock);
2098 mRepeatingRequests.clear();
2099 mRequestQueue.clear();
2100 mTriggerMap.clear();
2101 return OK;
2102}
2103
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002104void Camera3Device::RequestThread::setPaused(bool paused) {
2105 Mutex::Autolock l(mPauseLock);
2106 mDoPause = paused;
2107 mDoPauseSignal.signal();
2108}
2109
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002110status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2111 int32_t requestId, nsecs_t timeout) {
2112 Mutex::Autolock l(mLatestRequestMutex);
2113 status_t res;
2114 while (mLatestRequestId != requestId) {
2115 nsecs_t startTime = systemTime();
2116
2117 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2118 if (res != OK) return res;
2119
2120 timeout -= (systemTime() - startTime);
2121 }
2122
2123 return OK;
2124}
2125
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002126void Camera3Device::RequestThread::requestExit() {
2127 // Call parent to set up shutdown
2128 Thread::requestExit();
2129 // The exit from any possible waits
2130 mDoPauseSignal.signal();
2131 mRequestSignal.signal();
2132}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002133
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002134bool Camera3Device::RequestThread::threadLoop() {
2135
2136 status_t res;
2137
2138 // Handle paused state.
2139 if (waitIfPaused()) {
2140 return true;
2141 }
2142
2143 // Get work to do
2144
2145 sp<CaptureRequest> nextRequest = waitForNextRequest();
2146 if (nextRequest == NULL) {
2147 return true;
2148 }
2149
2150 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002151 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002152 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002153
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002154 // Get the request ID, if any
2155 int requestId;
2156 camera_metadata_entry_t requestIdEntry =
2157 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2158 if (requestIdEntry.count > 0) {
2159 requestId = requestIdEntry.data.i32[0];
2160 } else {
2161 ALOGW("%s: Did not have android.request.id set in the request",
2162 __FUNCTION__);
2163 requestId = NAME_NOT_FOUND;
2164 }
2165
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002166 // Insert any queued triggers (before metadata is locked)
2167 int32_t triggerCount;
2168 res = insertTriggers(nextRequest);
2169 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002170 SET_ERR("RequestThread: Unable to insert triggers "
2171 "(capture request %d, HAL device: %s (%d)",
2172 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002173 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2174 return false;
2175 }
2176 triggerCount = res;
2177
2178 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2179
2180 // If the request is the same as last, or we had triggers last time
2181 if (mPrevRequest != nextRequest || triggersMixedIn) {
2182 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002183 * HAL workaround:
2184 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2185 */
2186 res = addDummyTriggerIds(nextRequest);
2187 if (res != OK) {
2188 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2189 "(capture request %d, HAL device: %s (%d)",
2190 (mFrameNumber+1), strerror(-res), res);
2191 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2192 return false;
2193 }
2194
2195 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002196 * The request should be presorted so accesses in HAL
2197 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2198 */
2199 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002200 request.settings = nextRequest->mSettings.getAndLock();
2201 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002202 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2203
2204 IF_ALOGV() {
2205 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2206 find_camera_metadata_ro_entry(
2207 request.settings,
2208 ANDROID_CONTROL_AF_TRIGGER,
2209 &e
2210 );
2211 if (e.count > 0) {
2212 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2213 __FUNCTION__,
2214 mFrameNumber+1,
2215 e.data.u8[0]);
2216 }
2217 }
2218 } else {
2219 // leave request.settings NULL to indicate 'reuse latest given'
2220 ALOGVV("%s: Request settings are REUSED",
2221 __FUNCTION__);
2222 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002223
2224 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002225
2226 // Fill in buffers
2227
2228 if (nextRequest->mInputStream != NULL) {
2229 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002230 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002231 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002232 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002233 " %s (%d)", strerror(-res), res);
2234 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2235 return true;
2236 }
2237 } else {
2238 request.input_buffer = NULL;
2239 }
2240
2241 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2242 nextRequest->mOutputStreams.size());
2243 request.output_buffers = outputBuffers.array();
2244 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2245 res = nextRequest->mOutputStreams.editItemAt(i)->
2246 getBuffer(&outputBuffers.editItemAt(i));
2247 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002248 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2249 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002250 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2251 return true;
2252 }
2253 request.num_output_buffers++;
2254 }
2255
2256 request.frame_number = mFrameNumber++;
2257
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002258 // Log request in the in-flight queue
2259 sp<Camera3Device> parent = mParent.promote();
2260 if (parent == NULL) {
2261 CLOGE("RequestThread: Parent is gone");
2262 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2263 return false;
2264 }
2265
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002266 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002267 request.num_output_buffers);
2268 if (res != OK) {
2269 SET_ERR("RequestThread: Unable to register new in-flight request:"
2270 " %s (%d)", strerror(-res), res);
2271 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2272 return false;
2273 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002274
Zhijun Hecc27e112013-10-03 16:12:43 -07002275 // Inform waitUntilRequestProcessed thread of a new request ID
2276 {
2277 Mutex::Autolock al(mLatestRequestMutex);
2278
2279 mLatestRequestId = requestId;
2280 mLatestRequestSignal.signal();
2281 }
2282
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002283 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002284 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2285 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002286 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002287 ATRACE_END();
2288
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002289 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002290 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002291 " device: %s (%d)", request.frame_number, strerror(-res), res);
2292 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2293 return false;
2294 }
2295
Igor Murashkin1e479c02013-09-06 16:55:14 -07002296 // Update the latest request sent to HAL
2297 if (request.settings != NULL) { // Don't update them if they were unchanged
2298 Mutex::Autolock al(mLatestRequestMutex);
2299
2300 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2301 mLatestRequest.acquire(cloned);
2302 }
2303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002304 if (request.settings != NULL) {
2305 nextRequest->mSettings.unlock(request.settings);
2306 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002307
2308 // Remove any previously queued triggers (after unlock)
2309 res = removeTriggers(mPrevRequest);
2310 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002311 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002312 "(capture request %d, HAL device: %s (%d)",
2313 request.frame_number, strerror(-res), res);
2314 return false;
2315 }
2316 mPrevTriggers = triggerCount;
2317
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002318 // Return input buffer back to framework
2319 if (request.input_buffer != NULL) {
2320 Camera3Stream *stream =
2321 Camera3Stream::cast(request.input_buffer->stream);
2322 res = stream->returnInputBuffer(*(request.input_buffer));
2323 // Note: stream may be deallocated at this point, if this buffer was the
2324 // last reference to it.
2325 if (res != OK) {
2326 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2327 " its stream:%s (%d)", __FUNCTION__,
2328 request.frame_number, strerror(-res), res);
2329 // TODO: Report error upstream
2330 }
2331 }
2332
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333 return true;
2334}
2335
Igor Murashkin1e479c02013-09-06 16:55:14 -07002336CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2337 Mutex::Autolock al(mLatestRequestMutex);
2338
2339 ALOGV("RequestThread::%s", __FUNCTION__);
2340
2341 return mLatestRequest;
2342}
2343
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002344void Camera3Device::RequestThread::cleanUpFailedRequest(
2345 camera3_capture_request_t &request,
2346 sp<CaptureRequest> &nextRequest,
2347 Vector<camera3_stream_buffer_t> &outputBuffers) {
2348
2349 if (request.settings != NULL) {
2350 nextRequest->mSettings.unlock(request.settings);
2351 }
2352 if (request.input_buffer != NULL) {
2353 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002354 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002355 }
2356 for (size_t i = 0; i < request.num_output_buffers; i++) {
2357 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2358 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2359 outputBuffers[i], 0);
2360 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002361}
2362
2363sp<Camera3Device::CaptureRequest>
2364 Camera3Device::RequestThread::waitForNextRequest() {
2365 status_t res;
2366 sp<CaptureRequest> nextRequest;
2367
2368 // Optimized a bit for the simple steady-state case (single repeating
2369 // request), to avoid putting that request in the queue temporarily.
2370 Mutex::Autolock l(mRequestLock);
2371
2372 while (mRequestQueue.empty()) {
2373 if (!mRepeatingRequests.empty()) {
2374 // Always atomically enqueue all requests in a repeating request
2375 // list. Guarantees a complete in-sequence set of captures to
2376 // application.
2377 const RequestList &requests = mRepeatingRequests;
2378 RequestList::const_iterator firstRequest =
2379 requests.begin();
2380 nextRequest = *firstRequest;
2381 mRequestQueue.insert(mRequestQueue.end(),
2382 ++firstRequest,
2383 requests.end());
2384 // No need to wait any longer
2385 break;
2386 }
2387
2388 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2389
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002390 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2391 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002392 Mutex::Autolock pl(mPauseLock);
2393 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002394 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002395 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002396 // Let the tracker know
2397 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2398 if (statusTracker != 0) {
2399 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2400 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002401 }
2402 // Stop waiting for now and let thread management happen
2403 return NULL;
2404 }
2405 }
2406
2407 if (nextRequest == NULL) {
2408 // Don't have a repeating request already in hand, so queue
2409 // must have an entry now.
2410 RequestList::iterator firstRequest =
2411 mRequestQueue.begin();
2412 nextRequest = *firstRequest;
2413 mRequestQueue.erase(firstRequest);
2414 }
2415
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002416 // In case we've been unpaused by setPaused clearing mDoPause, need to
2417 // update internal pause state (capture/setRepeatingRequest unpause
2418 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002419 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002420 if (mPaused) {
2421 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2422 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2423 if (statusTracker != 0) {
2424 statusTracker->markComponentActive(mStatusId);
2425 }
2426 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002427 mPaused = false;
2428
2429 // Check if we've reconfigured since last time, and reset the preview
2430 // request if so. Can't use 'NULL request == repeat' across configure calls.
2431 if (mReconfigured) {
2432 mPrevRequest.clear();
2433 mReconfigured = false;
2434 }
2435
2436 return nextRequest;
2437}
2438
2439bool Camera3Device::RequestThread::waitIfPaused() {
2440 status_t res;
2441 Mutex::Autolock l(mPauseLock);
2442 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002443 if (mPaused == false) {
2444 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002445 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2446 // Let the tracker know
2447 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2448 if (statusTracker != 0) {
2449 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2450 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002451 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002452
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002454 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002455 return true;
2456 }
2457 }
2458 // We don't set mPaused to false here, because waitForNextRequest needs
2459 // to further manage the paused state in case of starvation.
2460 return false;
2461}
2462
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002463void Camera3Device::RequestThread::unpauseForNewRequests() {
2464 // With work to do, mark thread as unpaused.
2465 // If paused by request (setPaused), don't resume, to avoid
2466 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002467 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002468 Mutex::Autolock p(mPauseLock);
2469 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002470 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2471 if (mPaused) {
2472 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2473 if (statusTracker != 0) {
2474 statusTracker->markComponentActive(mStatusId);
2475 }
2476 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002477 mPaused = false;
2478 }
2479}
2480
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002481void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2482 sp<Camera3Device> parent = mParent.promote();
2483 if (parent != NULL) {
2484 va_list args;
2485 va_start(args, fmt);
2486
2487 parent->setErrorStateV(fmt, args);
2488
2489 va_end(args);
2490 }
2491}
2492
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002493status_t Camera3Device::RequestThread::insertTriggers(
2494 const sp<CaptureRequest> &request) {
2495
2496 Mutex::Autolock al(mTriggerMutex);
2497
2498 CameraMetadata &metadata = request->mSettings;
2499 size_t count = mTriggerMap.size();
2500
2501 for (size_t i = 0; i < count; ++i) {
2502 RequestTrigger trigger = mTriggerMap.valueAt(i);
2503
2504 uint32_t tag = trigger.metadataTag;
2505 camera_metadata_entry entry = metadata.find(tag);
2506
2507 if (entry.count > 0) {
2508 /**
2509 * Already has an entry for this trigger in the request.
2510 * Rewrite it with our requested trigger value.
2511 */
2512 RequestTrigger oldTrigger = trigger;
2513
2514 oldTrigger.entryValue = entry.data.u8[0];
2515
2516 mTriggerReplacedMap.add(tag, oldTrigger);
2517 } else {
2518 /**
2519 * More typical, no trigger entry, so we just add it
2520 */
2521 mTriggerRemovedMap.add(tag, trigger);
2522 }
2523
2524 status_t res;
2525
2526 switch (trigger.getTagType()) {
2527 case TYPE_BYTE: {
2528 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2529 res = metadata.update(tag,
2530 &entryValue,
2531 /*count*/1);
2532 break;
2533 }
2534 case TYPE_INT32:
2535 res = metadata.update(tag,
2536 &trigger.entryValue,
2537 /*count*/1);
2538 break;
2539 default:
2540 ALOGE("%s: Type not supported: 0x%x",
2541 __FUNCTION__,
2542 trigger.getTagType());
2543 return INVALID_OPERATION;
2544 }
2545
2546 if (res != OK) {
2547 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2548 ", value %d", __FUNCTION__, trigger.getTagName(),
2549 trigger.entryValue);
2550 return res;
2551 }
2552
2553 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2554 trigger.getTagName(),
2555 trigger.entryValue);
2556 }
2557
2558 mTriggerMap.clear();
2559
2560 return count;
2561}
2562
2563status_t Camera3Device::RequestThread::removeTriggers(
2564 const sp<CaptureRequest> &request) {
2565 Mutex::Autolock al(mTriggerMutex);
2566
2567 CameraMetadata &metadata = request->mSettings;
2568
2569 /**
2570 * Replace all old entries with their old values.
2571 */
2572 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2573 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2574
2575 status_t res;
2576
2577 uint32_t tag = trigger.metadataTag;
2578 switch (trigger.getTagType()) {
2579 case TYPE_BYTE: {
2580 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2581 res = metadata.update(tag,
2582 &entryValue,
2583 /*count*/1);
2584 break;
2585 }
2586 case TYPE_INT32:
2587 res = metadata.update(tag,
2588 &trigger.entryValue,
2589 /*count*/1);
2590 break;
2591 default:
2592 ALOGE("%s: Type not supported: 0x%x",
2593 __FUNCTION__,
2594 trigger.getTagType());
2595 return INVALID_OPERATION;
2596 }
2597
2598 if (res != OK) {
2599 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2600 ", trigger value %d", __FUNCTION__,
2601 trigger.getTagName(), trigger.entryValue);
2602 return res;
2603 }
2604 }
2605 mTriggerReplacedMap.clear();
2606
2607 /**
2608 * Remove all new entries.
2609 */
2610 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2611 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2612 status_t res = metadata.erase(trigger.metadataTag);
2613
2614 if (res != OK) {
2615 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2616 ", trigger value %d", __FUNCTION__,
2617 trigger.getTagName(), trigger.entryValue);
2618 return res;
2619 }
2620 }
2621 mTriggerRemovedMap.clear();
2622
2623 return OK;
2624}
2625
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002626status_t Camera3Device::RequestThread::addDummyTriggerIds(
2627 const sp<CaptureRequest> &request) {
2628 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2629 static const int32_t dummyTriggerId = 1;
2630 status_t res;
2631
2632 CameraMetadata &metadata = request->mSettings;
2633
2634 // If AF trigger is active, insert a dummy AF trigger ID if none already
2635 // exists
2636 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2637 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2638 if (afTrigger.count > 0 &&
2639 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2640 afId.count == 0) {
2641 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2642 if (res != OK) return res;
2643 }
2644
2645 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2646 // if none already exists
2647 camera_metadata_entry pcTrigger =
2648 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2649 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2650 if (pcTrigger.count > 0 &&
2651 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2652 pcId.count == 0) {
2653 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2654 &dummyTriggerId, 1);
2655 if (res != OK) return res;
2656 }
2657
2658 return OK;
2659}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002660
2661
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002662/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002663 * Static callback forwarding methods from HAL to instance
2664 */
2665
2666void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2667 const camera3_capture_result *result) {
2668 Camera3Device *d =
2669 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2670 d->processCaptureResult(result);
2671}
2672
2673void Camera3Device::sNotify(const camera3_callback_ops *cb,
2674 const camera3_notify_msg *msg) {
2675 Camera3Device *d =
2676 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2677 d->notify(msg);
2678}
2679
2680}; // namespace android