blob: 08e03ce64c79eb1f2bfe387bac0c056433ac6384 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Igor Murashkinff3e31d2013-10-23 16:40:06 -070044#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045#include "device3/Camera3Device.h"
46#include "device3/Camera3OutputStream.h"
47#include "device3/Camera3InputStream.h"
48#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080049
50using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051
52namespace android {
53
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054Camera3Device::Camera3Device(int id):
55 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070058 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070059 mNextResultFrameNumber(0),
60 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062{
63 ATRACE_CALL();
64 camera3_callback_ops::notify = &sNotify;
65 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
66 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
67}
68
69Camera3Device::~Camera3Device()
70{
71 ATRACE_CALL();
72 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
73 disconnect();
74}
75
Igor Murashkin71381052013-03-04 14:53:08 -080076int Camera3Device::getId() const {
77 return mId;
78}
79
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080080/**
81 * CameraDeviceBase interface
82 */
83
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084status_t Camera3Device::initialize(camera_module_t *module)
85{
86 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070087 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088 Mutex::Autolock l(mLock);
89
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070092 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 return INVALID_OPERATION;
94 }
95
96 /** Open HAL device */
97
98 status_t res;
99 String8 deviceName = String8::format("%d", mId);
100
101 camera3_device_t *device;
102
Zhijun He213ce792013-11-19 08:45:15 -0800103 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 res = module->common.methods->open(&module->common, deviceName.string(),
105 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800106 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107
108 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 return res;
111 }
112
113 /** Cross-check device version */
114
115 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 device->common.version);
120 device->common.close(&device->common);
121 return BAD_VALUE;
122 }
123
124 camera_info info;
125 res = module->get_camera_info(mId, &info);
126 if (res != OK) return res;
127
128 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
130 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version, info.device_version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 /** Initialize device with callback functions */
137
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700138 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_END();
141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
144 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700149 /** Start up status tracker thread */
150 mStatusTracker = new StatusTracker(this);
151 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
152 if (res != OK) {
153 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
154 strerror(-res), res);
155 device->common.close(&device->common);
156 mStatusTracker.clear();
157 return res;
158 }
159
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800160 /** Start up request queue thread */
161
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700162 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800163 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700165 SET_ERR_L("Unable to start request queue thread: %s (%d)",
166 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800168 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 return res;
170 }
171
172 /** Everything is good to go */
173
174 mDeviceInfo = info.static_camera_characteristics;
175 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700176 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800177 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700178 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700181 /** Check for quirks */
182
183 // Will the HAL be sending in early partial result metadata?
184 camera_metadata_entry partialResultsQuirk =
185 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
186 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
187 mUsePartialResultQuirk = true;
188 }
189
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190 return OK;
191}
192
193status_t Camera3Device::disconnect() {
194 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700195 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800196
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800197 ALOGV("%s: E", __FUNCTION__);
198
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700199 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800200
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 {
202 Mutex::Autolock l(mLock);
203 if (mStatus == STATUS_UNINITIALIZED) return res;
204
205 if (mStatus == STATUS_ACTIVE ||
206 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
207 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700208 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700209 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700210 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 } else {
212 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
213 if (res != OK) {
214 SET_ERR_L("Timeout waiting for HAL to drain");
215 // Continue to close device even in case of error
216 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700217 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800218 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800219
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700220 if (mStatus == STATUS_ERROR) {
221 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700222 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700223
224 if (mStatusTracker != NULL) {
225 mStatusTracker->requestExit();
226 }
227
228 if (mRequestThread != NULL) {
229 mRequestThread->requestExit();
230 }
231
232 mOutputStreams.clear();
233 mInputStream.clear();
234 }
235
236 // Joining done without holding mLock, otherwise deadlocks may ensue
237 // as the threads try to access parent state
238 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
239 // HAL may be in a bad state, so waiting for request thread
240 // (which may be stuck in the HAL processCaptureRequest call)
241 // could be dangerous.
242 mRequestThread->join();
243 }
244
245 if (mStatusTracker != NULL) {
246 mStatusTracker->join();
247 }
248
249 {
250 Mutex::Autolock l(mLock);
251
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800252 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800256 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800258 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 mHal3Device = NULL;
260 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800261
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 mStatus = STATUS_UNINITIALIZED;
263 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
265 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700266 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800267}
268
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269// For dumping/debugging only -
270// try to acquire a lock a few times, eventually give up to proceed with
271// debug/dump operations
272bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
273 bool gotLock = false;
274 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
275 if (lock.tryLock() == NO_ERROR) {
276 gotLock = true;
277 break;
278 } else {
279 usleep(kDumpSleepDuration);
280 }
281 }
282 return gotLock;
283}
284
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800285status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
286 ATRACE_CALL();
287 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700288
289 // Try to lock, but continue in case of failure (to avoid blocking in
290 // deadlocks)
291 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
292 bool gotLock = tryLockSpinRightRound(mLock);
293
294 ALOGW_IF(!gotInterfaceLock,
295 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
296 mId, __FUNCTION__);
297 ALOGW_IF(!gotLock,
298 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
299 mId, __FUNCTION__);
300
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800301 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800302
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303 const char *status =
304 mStatus == STATUS_ERROR ? "ERROR" :
305 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
307 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 mStatus == STATUS_ACTIVE ? "ACTIVE" :
309 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700312 if (mStatus == STATUS_ERROR) {
313 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
314 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315 lines.appendFormat(" Stream configuration:\n");
316
317 if (mInputStream != NULL) {
318 write(fd, lines.string(), lines.size());
319 mInputStream->dump(fd, args);
320 } else {
321 lines.appendFormat(" No input stream.\n");
322 write(fd, lines.string(), lines.size());
323 }
324 for (size_t i = 0; i < mOutputStreams.size(); i++) {
325 mOutputStreams[i]->dump(fd,args);
326 }
327
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700328 lines = String8(" In-flight requests:\n");
329 if (mInFlightMap.size() == 0) {
330 lines.append(" None\n");
331 } else {
332 for (size_t i = 0; i < mInFlightMap.size(); i++) {
333 InFlightRequest r = mInFlightMap.valueAt(i);
334 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
335 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
336 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
337 r.numBuffersLeft);
338 }
339 }
340 write(fd, lines.string(), lines.size());
341
Igor Murashkin1e479c02013-09-06 16:55:14 -0700342 {
343 lines = String8(" Last request sent:\n");
344 write(fd, lines.string(), lines.size());
345
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700346 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700347 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
348 }
349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800350 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700351 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800352 write(fd, lines.string(), lines.size());
353 mHal3Device->ops->dump(mHal3Device, fd);
354 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800355
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700356 if (gotLock) mLock.unlock();
357 if (gotInterfaceLock) mInterfaceLock.unlock();
358
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800359 return OK;
360}
361
362const CameraMetadata& Camera3Device::info() const {
363 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800364 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
365 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700366 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367 mStatus == STATUS_ERROR ?
368 "when in error state" : "before init");
369 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800370 return mDeviceInfo;
371}
372
373status_t Camera3Device::capture(CameraMetadata &request) {
374 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700375 status_t res;
376 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800377 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800378
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700379 // TODO: take ownership of the request
380
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800381 switch (mStatus) {
382 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700383 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800384 return INVALID_OPERATION;
385 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700386 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800387 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700388 case STATUS_UNCONFIGURED:
389 // May be lazily configuring streams, will check during setup
390 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800391 case STATUS_ACTIVE:
392 // OK
393 break;
394 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700395 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396 return INVALID_OPERATION;
397 }
398
399 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
400 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700401 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 return BAD_VALUE;
403 }
404
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700405 res = mRequestThread->queueRequest(newRequest);
406 if (res == OK) {
407 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
408 if (res != OK) {
409 SET_ERR_L("Can't transition to active in %f seconds!",
410 kActiveTimeout/1e9);
411 }
412 ALOGV("Camera %d: Capture request enqueued", mId);
413 }
414 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800415}
416
417
418status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
419 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700420 status_t res;
421 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800422 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800423
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800424 switch (mStatus) {
425 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700426 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800427 return INVALID_OPERATION;
428 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700429 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800430 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700431 case STATUS_UNCONFIGURED:
432 // May be lazily configuring streams, will check during setup
433 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800434 case STATUS_ACTIVE:
435 // OK
436 break;
437 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700438 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800439 return INVALID_OPERATION;
440 }
441
442 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
443 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700444 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 return BAD_VALUE;
446 }
447
448 RequestList newRepeatingRequests;
449 newRepeatingRequests.push_back(newRepeatingRequest);
450
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700451 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
452 if (res == OK) {
453 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
454 if (res != OK) {
455 SET_ERR_L("Can't transition to active in %f seconds!",
456 kActiveTimeout/1e9);
457 }
458 ALOGV("Camera %d: Repeating request set", mId);
459 }
460 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461}
462
463
464sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
465 const CameraMetadata &request) {
466 status_t res;
467
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700468 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800469 res = configureStreamsLocked();
470 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700471 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800472 return NULL;
473 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700474 if (mStatus == STATUS_UNCONFIGURED) {
475 CLOGE("No streams configured");
476 return NULL;
477 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800478 }
479
480 sp<CaptureRequest> newRequest = createCaptureRequest(request);
481 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800482}
483
484status_t Camera3Device::clearStreamingRequest() {
485 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800487 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800488
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800489 switch (mStatus) {
490 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700491 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800492 return INVALID_OPERATION;
493 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700494 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800495 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700496 case STATUS_UNCONFIGURED:
497 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800498 case STATUS_ACTIVE:
499 // OK
500 break;
501 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700502 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800503 return INVALID_OPERATION;
504 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700505 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800506 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800507}
508
509status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
510 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700511 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800512
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700513 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800514}
515
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700516status_t Camera3Device::createInputStream(
517 uint32_t width, uint32_t height, int format, int *id) {
518 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700519 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700520 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700521 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
522 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700523
524 status_t res;
525 bool wasActive = false;
526
527 switch (mStatus) {
528 case STATUS_ERROR:
529 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
530 return INVALID_OPERATION;
531 case STATUS_UNINITIALIZED:
532 ALOGE("%s: Device not initialized", __FUNCTION__);
533 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700534 case STATUS_UNCONFIGURED:
535 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700536 // OK
537 break;
538 case STATUS_ACTIVE:
539 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700540 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700541 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700542 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700543 return res;
544 }
545 wasActive = true;
546 break;
547 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700548 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700549 return INVALID_OPERATION;
550 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700551 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700552
553 if (mInputStream != 0) {
554 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
555 return INVALID_OPERATION;
556 }
557
558 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
559 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700560 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700561
562 mInputStream = newStream;
563
564 *id = mNextStreamId++;
565
566 // Continue captures if active at start
567 if (wasActive) {
568 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
569 res = configureStreamsLocked();
570 if (res != OK) {
571 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
572 __FUNCTION__, mNextStreamId, strerror(-res), res);
573 return res;
574 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700575 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700576 }
577
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700578 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700579 return OK;
580}
581
Igor Murashkin2fba5842013-04-22 14:03:54 -0700582
583status_t Camera3Device::createZslStream(
584 uint32_t width, uint32_t height,
585 int depth,
586 /*out*/
587 int *id,
588 sp<Camera3ZslStream>* zslStream) {
589 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700590 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700591 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700592 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
593 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594
595 status_t res;
596 bool wasActive = false;
597
598 switch (mStatus) {
599 case STATUS_ERROR:
600 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
601 return INVALID_OPERATION;
602 case STATUS_UNINITIALIZED:
603 ALOGE("%s: Device not initialized", __FUNCTION__);
604 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700605 case STATUS_UNCONFIGURED:
606 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700607 // OK
608 break;
609 case STATUS_ACTIVE:
610 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700611 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700612 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700613 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700614 return res;
615 }
616 wasActive = true;
617 break;
618 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700619 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700620 return INVALID_OPERATION;
621 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700622 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700623
624 if (mInputStream != 0) {
625 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
626 return INVALID_OPERATION;
627 }
628
629 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
630 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700632
633 res = mOutputStreams.add(mNextStreamId, newStream);
634 if (res < 0) {
635 ALOGE("%s: Can't add new stream to set: %s (%d)",
636 __FUNCTION__, strerror(-res), res);
637 return res;
638 }
639 mInputStream = newStream;
640
641 *id = mNextStreamId++;
642 *zslStream = newStream;
643
644 // Continue captures if active at start
645 if (wasActive) {
646 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
647 res = configureStreamsLocked();
648 if (res != OK) {
649 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
650 __FUNCTION__, mNextStreamId, strerror(-res), res);
651 return res;
652 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700653 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700654 }
655
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700657 return OK;
658}
659
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
661 uint32_t width, uint32_t height, int format, size_t size, int *id) {
662 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700663 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
666 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 status_t res;
669 bool wasActive = false;
670
671 switch (mStatus) {
672 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700673 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 return INVALID_OPERATION;
675 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700676 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800677 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 case STATUS_UNCONFIGURED:
679 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680 // OK
681 break;
682 case STATUS_ACTIVE:
683 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700684 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800685 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700686 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800687 return res;
688 }
689 wasActive = true;
690 break;
691 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700692 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800693 return INVALID_OPERATION;
694 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696
697 sp<Camera3OutputStream> newStream;
698 if (format == HAL_PIXEL_FORMAT_BLOB) {
699 newStream = new Camera3OutputStream(mNextStreamId, consumer,
700 width, height, size, format);
701 } else {
702 newStream = new Camera3OutputStream(mNextStreamId, consumer,
703 width, height, format);
704 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700705 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800706
707 res = mOutputStreams.add(mNextStreamId, newStream);
708 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700709 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800710 return res;
711 }
712
713 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700714 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800715
716 // Continue captures if active at start
717 if (wasActive) {
718 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
719 res = configureStreamsLocked();
720 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700721 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
722 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800723 return res;
724 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700725 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800726 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700727 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800729}
730
731status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
732 ATRACE_CALL();
733 (void)outputId; (void)id;
734
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700735 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800736 return INVALID_OPERATION;
737}
738
739
740status_t Camera3Device::getStreamInfo(int id,
741 uint32_t *width, uint32_t *height, uint32_t *format) {
742 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700743 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800744 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800745
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800746 switch (mStatus) {
747 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700748 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749 return INVALID_OPERATION;
750 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700751 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800752 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700753 case STATUS_UNCONFIGURED:
754 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755 case STATUS_ACTIVE:
756 // OK
757 break;
758 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700759 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 return INVALID_OPERATION;
761 }
762
763 ssize_t idx = mOutputStreams.indexOfKey(id);
764 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700765 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800766 return idx;
767 }
768
769 if (width) *width = mOutputStreams[idx]->getWidth();
770 if (height) *height = mOutputStreams[idx]->getHeight();
771 if (format) *format = mOutputStreams[idx]->getFormat();
772
773 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800774}
775
776status_t Camera3Device::setStreamTransform(int id,
777 int transform) {
778 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700779 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800781
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782 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 case STATUS_ACTIVE:
792 // OK
793 break;
794 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700795 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 return INVALID_OPERATION;
797 }
798
799 ssize_t idx = mOutputStreams.indexOfKey(id);
800 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700801 CLOGE("Stream %d does not exist",
802 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return BAD_VALUE;
804 }
805
806 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807}
808
809status_t Camera3Device::deleteStream(int id) {
810 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700811 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800812 Mutex::Autolock l(mLock);
813 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800814
Igor Murashkine2172be2013-05-28 15:31:39 -0700815 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
816
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 // CameraDevice semantics require device to already be idle before
818 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700820 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
821 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 }
823
Igor Murashkin2fba5842013-04-22 14:03:54 -0700824 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800825 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 if (mInputStream != NULL && id == mInputStream->getId()) {
827 deletedStream = mInputStream;
828 mInputStream.clear();
829 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800830 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700831 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 return BAD_VALUE;
833 }
Zhijun He5f446352014-01-22 09:49:33 -0800834 }
835
836 // Delete output stream or the output part of a bi-directional stream.
837 if (outputStreamIdx != NAME_NOT_FOUND) {
838 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 mOutputStreams.removeItem(id);
840 }
841
842 // Free up the stream endpoint so that it can be used by some other stream
843 res = deletedStream->disconnect();
844 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700845 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 // fall through since we want to still list the stream as deleted.
847 }
848 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700849 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850
851 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852}
853
854status_t Camera3Device::deleteReprocessStream(int id) {
855 ATRACE_CALL();
856 (void)id;
857
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700858 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800859 return INVALID_OPERATION;
860}
861
862
863status_t Camera3Device::createDefaultRequest(int templateId,
864 CameraMetadata *request) {
865 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700866 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700867 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 Mutex::Autolock l(mLock);
869
870 switch (mStatus) {
871 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700872 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 return INVALID_OPERATION;
874 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700875 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 case STATUS_UNCONFIGURED:
878 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 case STATUS_ACTIVE:
880 // OK
881 break;
882 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700883 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 return INVALID_OPERATION;
885 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886
887 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700888 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800889 rawRequest = mHal3Device->ops->construct_default_request_settings(
890 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700891 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700892 if (rawRequest == NULL) {
893 SET_ERR_L("HAL is unable to construct default settings for template %d",
894 templateId);
895 return DEAD_OBJECT;
896 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800897 *request = rawRequest;
898
899 return OK;
900}
901
902status_t Camera3Device::waitUntilDrained() {
903 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800906
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 switch (mStatus) {
908 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700909 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 ALOGV("%s: Already idle", __FUNCTION__);
911 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 case STATUS_CONFIGURED:
913 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 case STATUS_ERROR:
915 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700916 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 break;
918 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700919 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 return INVALID_OPERATION;
921 }
922
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
924 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
925 return res;
926}
927
928// Pause to reconfigure
929status_t Camera3Device::internalPauseAndWaitLocked() {
930 mRequestThread->setPaused(true);
931 mPauseStateNotify = true;
932
933 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
934 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
935 if (res != OK) {
936 SET_ERR_L("Can't idle device in %f seconds!",
937 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800938 }
939
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 return res;
941}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700943// Resume after internalPauseAndWaitLocked
944status_t Camera3Device::internalResumeLocked() {
945 status_t res;
946
947 mRequestThread->setPaused(false);
948
949 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
950 if (res != OK) {
951 SET_ERR_L("Can't transition to active in %f seconds!",
952 kActiveTimeout/1e9);
953 }
954 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800956}
957
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958status_t Camera3Device::waitUntilStateThenRelock(bool active,
959 nsecs_t timeout) {
960 status_t res = OK;
961 if (active == (mStatus == STATUS_ACTIVE)) {
962 // Desired state already reached
963 return res;
964 }
965
966 bool stateSeen = false;
967 do {
968 mRecentStatusUpdates.clear();
969
970 res = mStatusChanged.waitRelative(mLock, timeout);
971 if (res != OK) break;
972
973 // Check state change history during wait
974 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
975 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
976 stateSeen = true;
977 break;
978 }
979 }
980 } while (!stateSeen);
981
982 return res;
983}
984
985
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800986status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
987 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700988 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800989
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700990 if (listener != NULL && mListener != NULL) {
991 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
992 }
993 mListener = listener;
994
995 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800996}
997
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700998bool Camera3Device::willNotify3A() {
999 return false;
1000}
1001
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001003 status_t res;
1004 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001005
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001006 while (mResultQueue.empty()) {
1007 res = mResultSignal.waitRelative(mOutputLock, timeout);
1008 if (res == TIMED_OUT) {
1009 return res;
1010 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001011 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1012 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001013 return res;
1014 }
1015 }
1016 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001017}
1018
1019status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1020 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001021 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001022
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001023 if (mResultQueue.empty()) {
1024 return NOT_ENOUGH_DATA;
1025 }
1026
1027 CameraMetadata &result = *(mResultQueue.begin());
1028 frame->acquire(result);
1029 mResultQueue.erase(mResultQueue.begin());
1030
1031 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001032}
1033
1034status_t Camera3Device::triggerAutofocus(uint32_t id) {
1035 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001037
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001038 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1039 // Mix-in this trigger into the next request and only the next request.
1040 RequestTrigger trigger[] = {
1041 {
1042 ANDROID_CONTROL_AF_TRIGGER,
1043 ANDROID_CONTROL_AF_TRIGGER_START
1044 },
1045 {
1046 ANDROID_CONTROL_AF_TRIGGER_ID,
1047 static_cast<int32_t>(id)
1048 },
1049 };
1050
1051 return mRequestThread->queueTrigger(trigger,
1052 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053}
1054
1055status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1056 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001057 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001059 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1060 // Mix-in this trigger into the next request and only the next request.
1061 RequestTrigger trigger[] = {
1062 {
1063 ANDROID_CONTROL_AF_TRIGGER,
1064 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1065 },
1066 {
1067 ANDROID_CONTROL_AF_TRIGGER_ID,
1068 static_cast<int32_t>(id)
1069 },
1070 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001072 return mRequestThread->queueTrigger(trigger,
1073 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001074}
1075
1076status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1077 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001080 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1081 // Mix-in this trigger into the next request and only the next request.
1082 RequestTrigger trigger[] = {
1083 {
1084 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1085 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1086 },
1087 {
1088 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1089 static_cast<int32_t>(id)
1090 },
1091 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001093 return mRequestThread->queueTrigger(trigger,
1094 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095}
1096
1097status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1098 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1099 ATRACE_CALL();
1100 (void)reprocessStreamId; (void)buffer; (void)listener;
1101
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001102 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001103 return INVALID_OPERATION;
1104}
1105
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001106status_t Camera3Device::flush() {
1107 ATRACE_CALL();
1108 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001109 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001110 Mutex::Autolock l(mLock);
1111
1112 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001113 status_t res;
1114 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1115 res = mHal3Device->ops->flush(mHal3Device);
1116 } else {
1117 res = waitUntilDrained();
1118 }
1119
1120 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001121}
1122
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124 * Methods called by subclasses
1125 */
1126
1127void Camera3Device::notifyStatus(bool idle) {
1128 {
1129 // Need mLock to safely update state and synchronize to current
1130 // state of methods in flight.
1131 Mutex::Autolock l(mLock);
1132 // We can get various system-idle notices from the status tracker
1133 // while starting up. Only care about them if we've actually sent
1134 // in some requests recently.
1135 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1136 return;
1137 }
1138 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1139 idle ? "idle" : "active");
1140 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1141 mRecentStatusUpdates.add(mStatus);
1142 mStatusChanged.signal();
1143
1144 // Skip notifying listener if we're doing some user-transparent
1145 // state changes
1146 if (mPauseStateNotify) return;
1147 }
1148 NotificationListener *listener;
1149 {
1150 Mutex::Autolock l(mOutputLock);
1151 listener = mListener;
1152 }
1153 if (idle && listener != NULL) {
1154 listener->notifyIdle();
1155 }
1156}
1157
1158/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159 * Camera3Device private methods
1160 */
1161
1162sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1163 const CameraMetadata &request) {
1164 ATRACE_CALL();
1165 status_t res;
1166
1167 sp<CaptureRequest> newRequest = new CaptureRequest;
1168 newRequest->mSettings = request;
1169
1170 camera_metadata_entry_t inputStreams =
1171 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1172 if (inputStreams.count > 0) {
1173 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001174 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001175 CLOGE("Request references unknown input stream %d",
1176 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 return NULL;
1178 }
1179 // Lazy completion of stream configuration (allocation/registration)
1180 // on first use
1181 if (mInputStream->isConfiguring()) {
1182 res = mInputStream->finishConfiguration(mHal3Device);
1183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001184 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001186 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 return NULL;
1188 }
1189 }
1190
1191 newRequest->mInputStream = mInputStream;
1192 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1193 }
1194
1195 camera_metadata_entry_t streams =
1196 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1197 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001198 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 return NULL;
1200 }
1201
1202 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001203 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001205 CLOGE("Request references unknown stream %d",
1206 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001207 return NULL;
1208 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001209 sp<Camera3OutputStreamInterface> stream =
1210 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211
1212 // Lazy completion of stream configuration (allocation/registration)
1213 // on first use
1214 if (stream->isConfiguring()) {
1215 res = stream->finishConfiguration(mHal3Device);
1216 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001217 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1218 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001219 return NULL;
1220 }
1221 }
1222
1223 newRequest->mOutputStreams.push(stream);
1224 }
1225 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1226
1227 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228}
1229
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230status_t Camera3Device::configureStreamsLocked() {
1231 ATRACE_CALL();
1232 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001234 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001235 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 return INVALID_OPERATION;
1237 }
1238
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001239 if (!mNeedConfig) {
1240 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1241 return OK;
1242 }
1243
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001244 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246
1247 camera3_stream_configuration config;
1248
1249 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1250
1251 Vector<camera3_stream_t*> streams;
1252 streams.setCapacity(config.num_streams);
1253
1254 if (mInputStream != NULL) {
1255 camera3_stream_t *inputStream;
1256 inputStream = mInputStream->startConfiguration();
1257 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001258 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001259 return INVALID_OPERATION;
1260 }
1261 streams.add(inputStream);
1262 }
1263
1264 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001265
1266 // Don't configure bidi streams twice, nor add them twice to the list
1267 if (mOutputStreams[i].get() ==
1268 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1269
1270 config.num_streams--;
1271 continue;
1272 }
1273
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001274 camera3_stream_t *outputStream;
1275 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1276 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 return INVALID_OPERATION;
1279 }
1280 streams.add(outputStream);
1281 }
1282
1283 config.streams = streams.editArray();
1284
1285 // Do the HAL configuration; will potentially touch stream
1286 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001287 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001288 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001289 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001290
1291 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001292 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1293 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001294 return res;
1295 }
1296
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001297 // Finish all stream configuration immediately.
1298 // TODO: Try to relax this later back to lazy completion, which should be
1299 // faster
1300
Igor Murashkin073f8572013-05-02 14:59:28 -07001301 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001302 res = mInputStream->finishConfiguration(mHal3Device);
1303 if (res != OK) {
1304 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1305 mInputStream->getId(), strerror(-res), res);
1306 return res;
1307 }
1308 }
1309
1310 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001311 sp<Camera3OutputStreamInterface> outputStream =
1312 mOutputStreams.editValueAt(i);
1313 if (outputStream->isConfiguring()) {
1314 res = outputStream->finishConfiguration(mHal3Device);
1315 if (res != OK) {
1316 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1317 outputStream->getId(), strerror(-res), res);
1318 return res;
1319 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001320 }
1321 }
1322
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323 // Request thread needs to know to avoid using repeat-last-settings protocol
1324 // across configure_streams() calls
1325 mRequestThread->configurationComplete();
1326
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001329 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001330
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001331 if (config.num_streams > 0) {
1332 mStatus = STATUS_CONFIGURED;
1333 } else {
1334 mStatus = STATUS_UNCONFIGURED;
1335 }
1336
1337 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1338
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001340}
1341
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001342void Camera3Device::setErrorState(const char *fmt, ...) {
1343 Mutex::Autolock l(mLock);
1344 va_list args;
1345 va_start(args, fmt);
1346
1347 setErrorStateLockedV(fmt, args);
1348
1349 va_end(args);
1350}
1351
1352void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1353 Mutex::Autolock l(mLock);
1354 setErrorStateLockedV(fmt, args);
1355}
1356
1357void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1358 va_list args;
1359 va_start(args, fmt);
1360
1361 setErrorStateLockedV(fmt, args);
1362
1363 va_end(args);
1364}
1365
1366void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001367 // Print out all error messages to log
1368 String8 errorCause = String8::formatV(fmt, args);
1369 ALOGE("Camera %d: %s", mId, errorCause.string());
1370
1371 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001372 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001373
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001374 // Save stack trace. View by dumping it later.
1375 CameraTraces::saveTrace();
1376 // TODO: consider adding errorCause and client pid/procname
1377
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 mErrorCause = errorCause;
1379
1380 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001381 mStatus = STATUS_ERROR;
1382}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383
1384/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001385 * In-flight request management
1386 */
1387
1388status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001389 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001390 ATRACE_CALL();
1391 Mutex::Autolock l(mInFlightLock);
1392
1393 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001394 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001395 if (res < 0) return res;
1396
1397 return OK;
1398}
1399
1400/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001401 * QUIRK(partial results)
1402 * Check if all 3A fields are ready, and send off a partial 3A-only result
1403 * to the output frame queue
1404 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001405bool Camera3Device::processPartial3AQuirk(
1406 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001407 const CameraMetadata& partial) {
1408
1409 // Check if all 3A states are present
1410 // The full list of fields is
1411 // android.control.afMode
1412 // android.control.awbMode
1413 // android.control.aeState
1414 // android.control.awbState
1415 // android.control.afState
1416 // android.control.afTriggerID
1417 // android.control.aePrecaptureID
1418 // TODO: Add android.control.aeMode
1419
1420 bool gotAllStates = true;
1421
1422 uint8_t afMode;
1423 uint8_t awbMode;
1424 uint8_t aeState;
1425 uint8_t afState;
1426 uint8_t awbState;
1427 int32_t afTriggerId;
1428 int32_t aeTriggerId;
1429
1430 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1431 &afMode, frameNumber);
1432
1433 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1434 &awbMode, frameNumber);
1435
1436 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1437 &aeState, frameNumber);
1438
1439 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1440 &afState, frameNumber);
1441
1442 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1443 &awbState, frameNumber);
1444
1445 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1446 &afTriggerId, frameNumber);
1447
1448 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1449 &aeTriggerId, frameNumber);
1450
1451 if (!gotAllStates) return false;
1452
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001453 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001454 "AF state %d, AE state %d, AWB state %d, "
1455 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001456 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001457 afMode, awbMode,
1458 afState, aeState, awbState,
1459 afTriggerId, aeTriggerId);
1460
1461 // Got all states, so construct a minimal result to send
1462 // In addition to the above fields, this means adding in
1463 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001464 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001465 // android.quirks.partialResult
1466
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001467 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001468
1469 Mutex::Autolock l(mOutputLock);
1470
1471 CameraMetadata& min3AResult =
1472 *mResultQueue.insert(
1473 mResultQueue.end(),
1474 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1475
1476 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1477 &frameNumber, frameNumber)) {
1478 return false;
1479 }
1480
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001481 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1482 &requestId, frameNumber)) {
1483 return false;
1484 }
1485
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001486 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1487 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1488 &partialResult, frameNumber)) {
1489 return false;
1490 }
1491
1492 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1493 &afMode, frameNumber)) {
1494 return false;
1495 }
1496
1497 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1498 &awbMode, frameNumber)) {
1499 return false;
1500 }
1501
1502 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1503 &aeState, frameNumber)) {
1504 return false;
1505 }
1506
1507 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1508 &afState, frameNumber)) {
1509 return false;
1510 }
1511
1512 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1513 &awbState, frameNumber)) {
1514 return false;
1515 }
1516
1517 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1518 &afTriggerId, frameNumber)) {
1519 return false;
1520 }
1521
1522 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1523 &aeTriggerId, frameNumber)) {
1524 return false;
1525 }
1526
1527 mResultSignal.signal();
1528
1529 return true;
1530}
1531
1532template<typename T>
1533bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1534 T* value, int32_t frameNumber) {
1535 (void) frameNumber;
1536
1537 camera_metadata_ro_entry_t entry;
1538
1539 entry = result.find(tag);
1540 if (entry.count == 0) {
1541 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1542 mId, frameNumber, get_camera_metadata_tag_name(tag));
1543 return false;
1544 }
1545
1546 if (sizeof(T) == sizeof(uint8_t)) {
1547 *value = entry.data.u8[0];
1548 } else if (sizeof(T) == sizeof(int32_t)) {
1549 *value = entry.data.i32[0];
1550 } else {
1551 ALOGE("%s: Unexpected type", __FUNCTION__);
1552 return false;
1553 }
1554 return true;
1555}
1556
1557template<typename T>
1558bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1559 const T* value, int32_t frameNumber) {
1560 if (result.update(tag, value, 1) != NO_ERROR) {
1561 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1562 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1563 frameNumber, get_camera_metadata_tag_name(tag));
1564 return false;
1565 }
1566 return true;
1567}
1568
1569/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 * Camera HAL device callback methods
1571 */
1572
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001573void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001574 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001575
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001576 status_t res;
1577
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001578 uint32_t frameNumber = result->frame_number;
1579 if (result->result == NULL && result->num_output_buffers == 0) {
1580 SET_ERR("No result data provided by HAL for frame %d",
1581 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001582 return;
1583 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001584 bool partialResultQuirk = false;
1585 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001586
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001587 // Get capture timestamp from list of in-flight requests, where it was added
1588 // by the shutter notification for this frame. Then update the in-flight
1589 // status and remove the in-flight entry if all result data has been
1590 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001591 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001592 {
1593 Mutex::Autolock l(mInFlightLock);
1594 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1595 if (idx == NAME_NOT_FOUND) {
1596 SET_ERR("Unknown frame number for capture result: %d",
1597 frameNumber);
1598 return;
1599 }
1600 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001601
1602 // Check if this result carries only partial metadata
1603 if (mUsePartialResultQuirk && result->result != NULL) {
1604 camera_metadata_ro_entry_t partialResultEntry;
1605 res = find_camera_metadata_ro_entry(result->result,
1606 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1607 if (res != NAME_NOT_FOUND &&
1608 partialResultEntry.count > 0 &&
1609 partialResultEntry.data.u8[0] ==
1610 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1611 // A partial result. Flag this as such, and collect this
1612 // set of metadata into the in-flight entry.
1613 partialResultQuirk = true;
1614 request.partialResultQuirk.collectedResult.append(
1615 result->result);
1616 request.partialResultQuirk.collectedResult.erase(
1617 ANDROID_QUIRKS_PARTIAL_RESULT);
1618 // Fire off a 3A-only result if possible
1619 if (!request.partialResultQuirk.haveSent3A) {
1620 request.partialResultQuirk.haveSent3A =
1621 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001622 request.requestId,
1623 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001624 }
1625 }
1626 }
1627
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001628 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001629 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001630 * One of the following must happen before it's legal to call process_capture_result,
1631 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001632 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1633 * - CAMERA3_MSG_ERROR (expected during flush)
1634 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001635 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001636 SET_ERR("Called before shutter notify for frame %d",
1637 frameNumber);
1638 return;
1639 }
1640
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001641 // Did we get the (final) result metadata for this capture?
1642 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001643 if (request.haveResultMetadata) {
1644 SET_ERR("Called multiple times with metadata for frame %d",
1645 frameNumber);
1646 return;
1647 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001648 if (mUsePartialResultQuirk &&
1649 !request.partialResultQuirk.collectedResult.isEmpty()) {
1650 collectedQuirkResult.acquire(
1651 request.partialResultQuirk.collectedResult);
1652 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001653 request.haveResultMetadata = true;
1654 }
1655
1656 request.numBuffersLeft -= result->num_output_buffers;
1657
1658 if (request.numBuffersLeft < 0) {
1659 SET_ERR("Too many buffers returned for frame %d",
1660 frameNumber);
1661 return;
1662 }
1663
Zhijun He1b05dfc2013-11-21 12:57:51 -08001664 // Check if everything has arrived for this result (buffers and metadata), remove it from
1665 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1666 if ((request.requestStatus != OK) ||
1667 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001668 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001669 mInFlightMap.removeItemsAt(idx, 1);
1670 }
1671
1672 // Sanity check - if we have too many in-flight frames, something has
1673 // likely gone wrong
1674 if (mInFlightMap.size() > kInFlightWarnLimit) {
1675 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1676 }
1677
1678 }
1679
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001680 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001681 bool gotResult = false;
1682 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001683 Mutex::Autolock l(mOutputLock);
1684
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001685 gotResult = true;
1686
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001687 if (frameNumber != mNextResultFrameNumber) {
1688 SET_ERR("Out-of-order capture result metadata submitted! "
1689 "(got frame number %d, expecting %d)",
1690 frameNumber, mNextResultFrameNumber);
1691 return;
1692 }
1693 mNextResultFrameNumber++;
1694
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001695 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001696 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001697
Igor Murashkind2c90692013-04-02 12:32:32 -07001698 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001699 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001700 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001701 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001702 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001703 } else {
1704 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001705 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001706 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001707
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001708 // Append any previous partials to form a complete result
1709 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1710 captureResult.append(collectedQuirkResult);
1711 }
1712
1713 captureResult.sort();
1714
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001715 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001716
1717 camera_metadata_entry entry =
1718 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1719 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001720 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001721 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001722 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001723 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001724 SET_ERR("Timestamp mismatch between shutter notify and result"
1725 " metadata for frame %d (%lld vs %lld respectively)",
1726 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001727 gotResult = false;
1728 }
1729
1730 if (gotResult) {
1731 // Valid result, insert into queue
1732 CameraMetadata& queuedResult =
1733 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1734 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001735 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001736 } // scope for mOutputLock
1737
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001738 // Return completed buffers to their streams with the timestamp
1739
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001740 for (size_t i = 0; i < result->num_output_buffers; i++) {
1741 Camera3Stream *stream =
1742 Camera3Stream::cast(result->output_buffers[i].stream);
1743 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1744 // Note: stream may be deallocated at this point, if this buffer was the
1745 // last reference to it.
1746 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001747 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001748 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001749 }
1750 }
1751
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001752 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001753
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001754 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001755 mResultSignal.signal();
1756 }
1757
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001758}
1759
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001760
1761
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001762void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001763 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001764 NotificationListener *listener;
1765 {
1766 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001767 listener = mListener;
1768 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001769
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001770 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001771 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001772 return;
1773 }
1774
1775 switch (msg->type) {
1776 case CAMERA3_MSG_ERROR: {
1777 int streamId = 0;
1778 if (msg->message.error.error_stream != NULL) {
1779 Camera3Stream *stream =
1780 Camera3Stream::cast(
1781 msg->message.error.error_stream);
1782 streamId = stream->getId();
1783 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001784 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1785 mId, __FUNCTION__, msg->message.error.frame_number,
1786 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001787
1788 // Set request error status for the request in the in-flight tracking
1789 {
1790 Mutex::Autolock l(mInFlightLock);
1791 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1792 if (idx >= 0) {
1793 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1794 }
1795 }
1796
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001797 if (listener != NULL) {
1798 listener->notifyError(msg->message.error.error_code,
1799 msg->message.error.frame_number, streamId);
1800 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001801 break;
1802 }
1803 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001804 ssize_t idx;
1805 uint32_t frameNumber = msg->message.shutter.frame_number;
1806 nsecs_t timestamp = msg->message.shutter.timestamp;
1807 // Verify ordering of shutter notifications
1808 {
1809 Mutex::Autolock l(mOutputLock);
1810 if (frameNumber != mNextShutterFrameNumber) {
1811 SET_ERR("Shutter notification out-of-order. Expected "
1812 "notification for frame %d, got frame %d",
1813 mNextShutterFrameNumber, frameNumber);
1814 break;
1815 }
1816 mNextShutterFrameNumber++;
1817 }
1818
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001819 int32_t requestId = -1;
1820
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001821 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001822 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001823 {
1824 Mutex::Autolock l(mInFlightLock);
1825 idx = mInFlightMap.indexOfKey(frameNumber);
1826 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001827 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1828 r.captureTimestamp = timestamp;
1829 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001830 }
1831 }
1832 if (idx < 0) {
1833 SET_ERR("Shutter notification for non-existent frame number %d",
1834 frameNumber);
1835 break;
1836 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001837 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1838 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001839 // Call listener, if any
1840 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001841 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001842 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001843 break;
1844 }
1845 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001846 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001847 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001848 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001849}
1850
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001851CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001852 ALOGV("%s", __FUNCTION__);
1853
Igor Murashkin1e479c02013-09-06 16:55:14 -07001854 CameraMetadata retVal;
1855
1856 if (mRequestThread != NULL) {
1857 retVal = mRequestThread->getLatestRequest();
1858 }
1859
Igor Murashkin1e479c02013-09-06 16:55:14 -07001860 return retVal;
1861}
1862
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001863/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001864 * RequestThread inner class methods
1865 */
1866
1867Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001868 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 camera3_device_t *hal3Device) :
1870 Thread(false),
1871 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001872 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001874 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001875 mReconfigured(false),
1876 mDoPause(false),
1877 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001878 mFrameNumber(0),
1879 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001880 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001881}
1882
1883void Camera3Device::RequestThread::configurationComplete() {
1884 Mutex::Autolock l(mRequestLock);
1885 mReconfigured = true;
1886}
1887
1888status_t Camera3Device::RequestThread::queueRequest(
1889 sp<CaptureRequest> request) {
1890 Mutex::Autolock l(mRequestLock);
1891 mRequestQueue.push_back(request);
1892
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001893 unpauseForNewRequests();
1894
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001895 return OK;
1896}
1897
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001898
1899status_t Camera3Device::RequestThread::queueTrigger(
1900 RequestTrigger trigger[],
1901 size_t count) {
1902
1903 Mutex::Autolock l(mTriggerMutex);
1904 status_t ret;
1905
1906 for (size_t i = 0; i < count; ++i) {
1907 ret = queueTriggerLocked(trigger[i]);
1908
1909 if (ret != OK) {
1910 return ret;
1911 }
1912 }
1913
1914 return OK;
1915}
1916
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001917int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1918 sp<Camera3Device> d = device.promote();
1919 if (d != NULL) return d->mId;
1920 return 0;
1921}
1922
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001923status_t Camera3Device::RequestThread::queueTriggerLocked(
1924 RequestTrigger trigger) {
1925
1926 uint32_t tag = trigger.metadataTag;
1927 ssize_t index = mTriggerMap.indexOfKey(tag);
1928
1929 switch (trigger.getTagType()) {
1930 case TYPE_BYTE:
1931 // fall-through
1932 case TYPE_INT32:
1933 break;
1934 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001935 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1936 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001937 return INVALID_OPERATION;
1938 }
1939
1940 /**
1941 * Collect only the latest trigger, since we only have 1 field
1942 * in the request settings per trigger tag, and can't send more than 1
1943 * trigger per request.
1944 */
1945 if (index != NAME_NOT_FOUND) {
1946 mTriggerMap.editValueAt(index) = trigger;
1947 } else {
1948 mTriggerMap.add(tag, trigger);
1949 }
1950
1951 return OK;
1952}
1953
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954status_t Camera3Device::RequestThread::setRepeatingRequests(
1955 const RequestList &requests) {
1956 Mutex::Autolock l(mRequestLock);
1957 mRepeatingRequests.clear();
1958 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1959 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001960
1961 unpauseForNewRequests();
1962
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001963 return OK;
1964}
1965
1966status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1967 Mutex::Autolock l(mRequestLock);
1968 mRepeatingRequests.clear();
1969 return OK;
1970}
1971
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001972status_t Camera3Device::RequestThread::clear() {
1973 Mutex::Autolock l(mRequestLock);
1974 mRepeatingRequests.clear();
1975 mRequestQueue.clear();
1976 mTriggerMap.clear();
1977 return OK;
1978}
1979
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001980void Camera3Device::RequestThread::setPaused(bool paused) {
1981 Mutex::Autolock l(mPauseLock);
1982 mDoPause = paused;
1983 mDoPauseSignal.signal();
1984}
1985
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001986status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1987 int32_t requestId, nsecs_t timeout) {
1988 Mutex::Autolock l(mLatestRequestMutex);
1989 status_t res;
1990 while (mLatestRequestId != requestId) {
1991 nsecs_t startTime = systemTime();
1992
1993 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1994 if (res != OK) return res;
1995
1996 timeout -= (systemTime() - startTime);
1997 }
1998
1999 return OK;
2000}
2001
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002002void Camera3Device::RequestThread::requestExit() {
2003 // Call parent to set up shutdown
2004 Thread::requestExit();
2005 // The exit from any possible waits
2006 mDoPauseSignal.signal();
2007 mRequestSignal.signal();
2008}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002009
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002010bool Camera3Device::RequestThread::threadLoop() {
2011
2012 status_t res;
2013
2014 // Handle paused state.
2015 if (waitIfPaused()) {
2016 return true;
2017 }
2018
2019 // Get work to do
2020
2021 sp<CaptureRequest> nextRequest = waitForNextRequest();
2022 if (nextRequest == NULL) {
2023 return true;
2024 }
2025
2026 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002027 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002028 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002029
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002030 // Get the request ID, if any
2031 int requestId;
2032 camera_metadata_entry_t requestIdEntry =
2033 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2034 if (requestIdEntry.count > 0) {
2035 requestId = requestIdEntry.data.i32[0];
2036 } else {
2037 ALOGW("%s: Did not have android.request.id set in the request",
2038 __FUNCTION__);
2039 requestId = NAME_NOT_FOUND;
2040 }
2041
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002042 // Insert any queued triggers (before metadata is locked)
2043 int32_t triggerCount;
2044 res = insertTriggers(nextRequest);
2045 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002046 SET_ERR("RequestThread: Unable to insert triggers "
2047 "(capture request %d, HAL device: %s (%d)",
2048 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002049 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2050 return false;
2051 }
2052 triggerCount = res;
2053
2054 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2055
2056 // If the request is the same as last, or we had triggers last time
2057 if (mPrevRequest != nextRequest || triggersMixedIn) {
2058 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002059 * HAL workaround:
2060 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2061 */
2062 res = addDummyTriggerIds(nextRequest);
2063 if (res != OK) {
2064 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2065 "(capture request %d, HAL device: %s (%d)",
2066 (mFrameNumber+1), strerror(-res), res);
2067 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2068 return false;
2069 }
2070
2071 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002072 * The request should be presorted so accesses in HAL
2073 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2074 */
2075 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002076 request.settings = nextRequest->mSettings.getAndLock();
2077 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002078 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2079
2080 IF_ALOGV() {
2081 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2082 find_camera_metadata_ro_entry(
2083 request.settings,
2084 ANDROID_CONTROL_AF_TRIGGER,
2085 &e
2086 );
2087 if (e.count > 0) {
2088 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2089 __FUNCTION__,
2090 mFrameNumber+1,
2091 e.data.u8[0]);
2092 }
2093 }
2094 } else {
2095 // leave request.settings NULL to indicate 'reuse latest given'
2096 ALOGVV("%s: Request settings are REUSED",
2097 __FUNCTION__);
2098 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002099
2100 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002101
2102 // Fill in buffers
2103
2104 if (nextRequest->mInputStream != NULL) {
2105 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002106 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002107 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002108 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002109 " %s (%d)", strerror(-res), res);
2110 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2111 return true;
2112 }
2113 } else {
2114 request.input_buffer = NULL;
2115 }
2116
2117 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2118 nextRequest->mOutputStreams.size());
2119 request.output_buffers = outputBuffers.array();
2120 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2121 res = nextRequest->mOutputStreams.editItemAt(i)->
2122 getBuffer(&outputBuffers.editItemAt(i));
2123 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002124 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2125 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002126 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2127 return true;
2128 }
2129 request.num_output_buffers++;
2130 }
2131
2132 request.frame_number = mFrameNumber++;
2133
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002134 // Log request in the in-flight queue
2135 sp<Camera3Device> parent = mParent.promote();
2136 if (parent == NULL) {
2137 CLOGE("RequestThread: Parent is gone");
2138 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2139 return false;
2140 }
2141
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002142 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002143 request.num_output_buffers);
2144 if (res != OK) {
2145 SET_ERR("RequestThread: Unable to register new in-flight request:"
2146 " %s (%d)", strerror(-res), res);
2147 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2148 return false;
2149 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002150
Zhijun Hecc27e112013-10-03 16:12:43 -07002151 // Inform waitUntilRequestProcessed thread of a new request ID
2152 {
2153 Mutex::Autolock al(mLatestRequestMutex);
2154
2155 mLatestRequestId = requestId;
2156 mLatestRequestSignal.signal();
2157 }
2158
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002159 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002160 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2161 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002162 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002163 ATRACE_END();
2164
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002165 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002166 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002167 " device: %s (%d)", request.frame_number, strerror(-res), res);
2168 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2169 return false;
2170 }
2171
Igor Murashkin1e479c02013-09-06 16:55:14 -07002172 // Update the latest request sent to HAL
2173 if (request.settings != NULL) { // Don't update them if they were unchanged
2174 Mutex::Autolock al(mLatestRequestMutex);
2175
2176 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2177 mLatestRequest.acquire(cloned);
2178 }
2179
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002180 if (request.settings != NULL) {
2181 nextRequest->mSettings.unlock(request.settings);
2182 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002183
2184 // Remove any previously queued triggers (after unlock)
2185 res = removeTriggers(mPrevRequest);
2186 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002187 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002188 "(capture request %d, HAL device: %s (%d)",
2189 request.frame_number, strerror(-res), res);
2190 return false;
2191 }
2192 mPrevTriggers = triggerCount;
2193
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002194 // Return input buffer back to framework
2195 if (request.input_buffer != NULL) {
2196 Camera3Stream *stream =
2197 Camera3Stream::cast(request.input_buffer->stream);
2198 res = stream->returnInputBuffer(*(request.input_buffer));
2199 // Note: stream may be deallocated at this point, if this buffer was the
2200 // last reference to it.
2201 if (res != OK) {
2202 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2203 " its stream:%s (%d)", __FUNCTION__,
2204 request.frame_number, strerror(-res), res);
2205 // TODO: Report error upstream
2206 }
2207 }
2208
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002209 return true;
2210}
2211
Igor Murashkin1e479c02013-09-06 16:55:14 -07002212CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2213 Mutex::Autolock al(mLatestRequestMutex);
2214
2215 ALOGV("RequestThread::%s", __FUNCTION__);
2216
2217 return mLatestRequest;
2218}
2219
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002220void Camera3Device::RequestThread::cleanUpFailedRequest(
2221 camera3_capture_request_t &request,
2222 sp<CaptureRequest> &nextRequest,
2223 Vector<camera3_stream_buffer_t> &outputBuffers) {
2224
2225 if (request.settings != NULL) {
2226 nextRequest->mSettings.unlock(request.settings);
2227 }
2228 if (request.input_buffer != NULL) {
2229 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002230 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002231 }
2232 for (size_t i = 0; i < request.num_output_buffers; i++) {
2233 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2234 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2235 outputBuffers[i], 0);
2236 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002237}
2238
2239sp<Camera3Device::CaptureRequest>
2240 Camera3Device::RequestThread::waitForNextRequest() {
2241 status_t res;
2242 sp<CaptureRequest> nextRequest;
2243
2244 // Optimized a bit for the simple steady-state case (single repeating
2245 // request), to avoid putting that request in the queue temporarily.
2246 Mutex::Autolock l(mRequestLock);
2247
2248 while (mRequestQueue.empty()) {
2249 if (!mRepeatingRequests.empty()) {
2250 // Always atomically enqueue all requests in a repeating request
2251 // list. Guarantees a complete in-sequence set of captures to
2252 // application.
2253 const RequestList &requests = mRepeatingRequests;
2254 RequestList::const_iterator firstRequest =
2255 requests.begin();
2256 nextRequest = *firstRequest;
2257 mRequestQueue.insert(mRequestQueue.end(),
2258 ++firstRequest,
2259 requests.end());
2260 // No need to wait any longer
2261 break;
2262 }
2263
2264 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2265
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002266 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2267 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002268 Mutex::Autolock pl(mPauseLock);
2269 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002270 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002271 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002272 // Let the tracker know
2273 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2274 if (statusTracker != 0) {
2275 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2276 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002277 }
2278 // Stop waiting for now and let thread management happen
2279 return NULL;
2280 }
2281 }
2282
2283 if (nextRequest == NULL) {
2284 // Don't have a repeating request already in hand, so queue
2285 // must have an entry now.
2286 RequestList::iterator firstRequest =
2287 mRequestQueue.begin();
2288 nextRequest = *firstRequest;
2289 mRequestQueue.erase(firstRequest);
2290 }
2291
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002292 // In case we've been unpaused by setPaused clearing mDoPause, need to
2293 // update internal pause state (capture/setRepeatingRequest unpause
2294 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002296 if (mPaused) {
2297 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2298 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2299 if (statusTracker != 0) {
2300 statusTracker->markComponentActive(mStatusId);
2301 }
2302 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002303 mPaused = false;
2304
2305 // Check if we've reconfigured since last time, and reset the preview
2306 // request if so. Can't use 'NULL request == repeat' across configure calls.
2307 if (mReconfigured) {
2308 mPrevRequest.clear();
2309 mReconfigured = false;
2310 }
2311
2312 return nextRequest;
2313}
2314
2315bool Camera3Device::RequestThread::waitIfPaused() {
2316 status_t res;
2317 Mutex::Autolock l(mPauseLock);
2318 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002319 if (mPaused == false) {
2320 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002321 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2322 // Let the tracker know
2323 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2324 if (statusTracker != 0) {
2325 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2326 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002327 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002328
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002329 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002330 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002331 return true;
2332 }
2333 }
2334 // We don't set mPaused to false here, because waitForNextRequest needs
2335 // to further manage the paused state in case of starvation.
2336 return false;
2337}
2338
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002339void Camera3Device::RequestThread::unpauseForNewRequests() {
2340 // With work to do, mark thread as unpaused.
2341 // If paused by request (setPaused), don't resume, to avoid
2342 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002343 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002344 Mutex::Autolock p(mPauseLock);
2345 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002346 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2347 if (mPaused) {
2348 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2349 if (statusTracker != 0) {
2350 statusTracker->markComponentActive(mStatusId);
2351 }
2352 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002353 mPaused = false;
2354 }
2355}
2356
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002357void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2358 sp<Camera3Device> parent = mParent.promote();
2359 if (parent != NULL) {
2360 va_list args;
2361 va_start(args, fmt);
2362
2363 parent->setErrorStateV(fmt, args);
2364
2365 va_end(args);
2366 }
2367}
2368
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002369status_t Camera3Device::RequestThread::insertTriggers(
2370 const sp<CaptureRequest> &request) {
2371
2372 Mutex::Autolock al(mTriggerMutex);
2373
2374 CameraMetadata &metadata = request->mSettings;
2375 size_t count = mTriggerMap.size();
2376
2377 for (size_t i = 0; i < count; ++i) {
2378 RequestTrigger trigger = mTriggerMap.valueAt(i);
2379
2380 uint32_t tag = trigger.metadataTag;
2381 camera_metadata_entry entry = metadata.find(tag);
2382
2383 if (entry.count > 0) {
2384 /**
2385 * Already has an entry for this trigger in the request.
2386 * Rewrite it with our requested trigger value.
2387 */
2388 RequestTrigger oldTrigger = trigger;
2389
2390 oldTrigger.entryValue = entry.data.u8[0];
2391
2392 mTriggerReplacedMap.add(tag, oldTrigger);
2393 } else {
2394 /**
2395 * More typical, no trigger entry, so we just add it
2396 */
2397 mTriggerRemovedMap.add(tag, trigger);
2398 }
2399
2400 status_t res;
2401
2402 switch (trigger.getTagType()) {
2403 case TYPE_BYTE: {
2404 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2405 res = metadata.update(tag,
2406 &entryValue,
2407 /*count*/1);
2408 break;
2409 }
2410 case TYPE_INT32:
2411 res = metadata.update(tag,
2412 &trigger.entryValue,
2413 /*count*/1);
2414 break;
2415 default:
2416 ALOGE("%s: Type not supported: 0x%x",
2417 __FUNCTION__,
2418 trigger.getTagType());
2419 return INVALID_OPERATION;
2420 }
2421
2422 if (res != OK) {
2423 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2424 ", value %d", __FUNCTION__, trigger.getTagName(),
2425 trigger.entryValue);
2426 return res;
2427 }
2428
2429 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2430 trigger.getTagName(),
2431 trigger.entryValue);
2432 }
2433
2434 mTriggerMap.clear();
2435
2436 return count;
2437}
2438
2439status_t Camera3Device::RequestThread::removeTriggers(
2440 const sp<CaptureRequest> &request) {
2441 Mutex::Autolock al(mTriggerMutex);
2442
2443 CameraMetadata &metadata = request->mSettings;
2444
2445 /**
2446 * Replace all old entries with their old values.
2447 */
2448 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2449 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2450
2451 status_t res;
2452
2453 uint32_t tag = trigger.metadataTag;
2454 switch (trigger.getTagType()) {
2455 case TYPE_BYTE: {
2456 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2457 res = metadata.update(tag,
2458 &entryValue,
2459 /*count*/1);
2460 break;
2461 }
2462 case TYPE_INT32:
2463 res = metadata.update(tag,
2464 &trigger.entryValue,
2465 /*count*/1);
2466 break;
2467 default:
2468 ALOGE("%s: Type not supported: 0x%x",
2469 __FUNCTION__,
2470 trigger.getTagType());
2471 return INVALID_OPERATION;
2472 }
2473
2474 if (res != OK) {
2475 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2476 ", trigger value %d", __FUNCTION__,
2477 trigger.getTagName(), trigger.entryValue);
2478 return res;
2479 }
2480 }
2481 mTriggerReplacedMap.clear();
2482
2483 /**
2484 * Remove all new entries.
2485 */
2486 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2487 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2488 status_t res = metadata.erase(trigger.metadataTag);
2489
2490 if (res != OK) {
2491 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2492 ", trigger value %d", __FUNCTION__,
2493 trigger.getTagName(), trigger.entryValue);
2494 return res;
2495 }
2496 }
2497 mTriggerRemovedMap.clear();
2498
2499 return OK;
2500}
2501
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002502status_t Camera3Device::RequestThread::addDummyTriggerIds(
2503 const sp<CaptureRequest> &request) {
2504 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2505 static const int32_t dummyTriggerId = 1;
2506 status_t res;
2507
2508 CameraMetadata &metadata = request->mSettings;
2509
2510 // If AF trigger is active, insert a dummy AF trigger ID if none already
2511 // exists
2512 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2513 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2514 if (afTrigger.count > 0 &&
2515 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2516 afId.count == 0) {
2517 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2518 if (res != OK) return res;
2519 }
2520
2521 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2522 // if none already exists
2523 camera_metadata_entry pcTrigger =
2524 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2525 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2526 if (pcTrigger.count > 0 &&
2527 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2528 pcId.count == 0) {
2529 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2530 &dummyTriggerId, 1);
2531 if (res != OK) return res;
2532 }
2533
2534 return OK;
2535}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002536
2537
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002538/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002539 * Static callback forwarding methods from HAL to instance
2540 */
2541
2542void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2543 const camera3_capture_result *result) {
2544 Camera3Device *d =
2545 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2546 d->processCaptureResult(result);
2547}
2548
2549void Camera3Device::sNotify(const camera3_callback_ops *cb,
2550 const camera3_notify_msg *msg) {
2551 Camera3Device *d =
2552 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2553 d->notify(msg);
2554}
2555
2556}; // namespace android