blob: 0a4a24c1446455417d8c90361f3732f924b1a663 [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
44#include "device3/Camera3Device.h"
45#include "device3/Camera3OutputStream.h"
46#include "device3/Camera3InputStream.h"
47#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080048
49using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080050
51namespace android {
52
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053Camera3Device::Camera3Device(int id):
54 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080055 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070056 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070057 mNextResultFrameNumber(0),
58 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070059 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060{
61 ATRACE_CALL();
62 camera3_callback_ops::notify = &sNotify;
63 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
64 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
65}
66
67Camera3Device::~Camera3Device()
68{
69 ATRACE_CALL();
70 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
71 disconnect();
72}
73
Igor Murashkin71381052013-03-04 14:53:08 -080074int Camera3Device::getId() const {
75 return mId;
76}
77
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080078/**
79 * CameraDeviceBase interface
80 */
81
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080082status_t Camera3Device::initialize(camera_module_t *module)
83{
84 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080085 Mutex::Autolock l(mLock);
86
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080087 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070089 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 return INVALID_OPERATION;
91 }
92
93 /** Open HAL device */
94
95 status_t res;
96 String8 deviceName = String8::format("%d", mId);
97
98 camera3_device_t *device;
99
100 res = module->common.methods->open(&module->common, deviceName.string(),
101 reinterpret_cast<hw_device_t**>(&device));
102
103 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700104 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800105 return res;
106 }
107
108 /** Cross-check device version */
109
110 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700113 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114 device->common.version);
115 device->common.close(&device->common);
116 return BAD_VALUE;
117 }
118
119 camera_info info;
120 res = module->get_camera_info(mId, &info);
121 if (res != OK) return res;
122
123 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
125 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126 device->common.version, info.device_version);
127 device->common.close(&device->common);
128 return BAD_VALUE;
129 }
130
131 /** Initialize device with callback functions */
132
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700133 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700135 ATRACE_END();
136
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800137 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700138 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
139 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 device->common.close(&device->common);
141 return BAD_VALUE;
142 }
143
144 /** Get vendor metadata tags */
145
146 mVendorTagOps.get_camera_vendor_section_name = NULL;
147
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151
152 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
153 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
154 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700155 SET_ERR_L("Unable to set tag ops: %s (%d)",
156 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800157 device->common.close(&device->common);
158 return res;
159 }
160 }
161
162 /** Start up request queue thread */
163
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800164 mRequestThread = new RequestThread(this, device);
165 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700167 SET_ERR_L("Unable to start request queue thread: %s (%d)",
168 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 return res;
172 }
173
174 /** Everything is good to go */
175
176 mDeviceInfo = info.static_camera_characteristics;
177 mHal3Device = device;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800178 mStatus = STATUS_IDLE;
179 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700180 mNeedConfig = true;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181
182 return OK;
183}
184
185status_t Camera3Device::disconnect() {
186 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800189 ALOGV("%s: E", __FUNCTION__);
190
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700191 status_t res = OK;
192 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800193
194 if (mStatus == STATUS_ACTIVE ||
195 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
196 res = mRequestThread->clearRepeatingRequests();
197 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700198 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700199 // Continue to close device even in case of error
200 } else {
201 res = waitUntilDrainedLocked();
202 if (res != OK) {
203 SET_ERR_L("Timeout waiting for HAL to drain");
204 // Continue to close device even in case of error
205 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800206 }
207 }
208 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
209
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700210 if (mStatus == STATUS_ERROR) {
211 CLOGE("Shutting down in an error state");
212 }
213
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800214 if (mRequestThread != NULL) {
215 mRequestThread->requestExit();
216 }
217
218 mOutputStreams.clear();
219 mInputStream.clear();
220
221 if (mRequestThread != NULL) {
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700222 if (mStatus != STATUS_ERROR) {
223 // HAL may be in a bad state, so waiting for request thread
224 // (which may be stuck in the HAL processCaptureRequest call)
225 // could be dangerous.
226 mRequestThread->join();
227 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800228 mRequestThread.clear();
229 }
230
231 if (mHal3Device != NULL) {
232 mHal3Device->common.close(&mHal3Device->common);
233 mHal3Device = NULL;
234 }
235
236 mStatus = STATUS_UNINITIALIZED;
237
238 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800240}
241
242status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
243 ATRACE_CALL();
244 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800245 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800246
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800247 const char *status =
248 mStatus == STATUS_ERROR ? "ERROR" :
249 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
250 mStatus == STATUS_IDLE ? "IDLE" :
251 mStatus == STATUS_ACTIVE ? "ACTIVE" :
252 "Unknown";
253 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700254 if (mStatus == STATUS_ERROR) {
255 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
256 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257 lines.appendFormat(" Stream configuration:\n");
258
259 if (mInputStream != NULL) {
260 write(fd, lines.string(), lines.size());
261 mInputStream->dump(fd, args);
262 } else {
263 lines.appendFormat(" No input stream.\n");
264 write(fd, lines.string(), lines.size());
265 }
266 for (size_t i = 0; i < mOutputStreams.size(); i++) {
267 mOutputStreams[i]->dump(fd,args);
268 }
269
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700270 lines = String8(" In-flight requests:\n");
271 if (mInFlightMap.size() == 0) {
272 lines.append(" None\n");
273 } else {
274 for (size_t i = 0; i < mInFlightMap.size(); i++) {
275 InFlightRequest r = mInFlightMap.valueAt(i);
276 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
277 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
278 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
279 r.numBuffersLeft);
280 }
281 }
282 write(fd, lines.string(), lines.size());
283
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700285 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800286 write(fd, lines.string(), lines.size());
287 mHal3Device->ops->dump(mHal3Device, fd);
288 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800289
290 return OK;
291}
292
293const CameraMetadata& Camera3Device::info() const {
294 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800295 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
296 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700297 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800298 mStatus == STATUS_ERROR ?
299 "when in error state" : "before init");
300 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800301 return mDeviceInfo;
302}
303
304status_t Camera3Device::capture(CameraMetadata &request) {
305 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800307
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700308 // TODO: take ownership of the request
309
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800310 switch (mStatus) {
311 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700312 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800313 return INVALID_OPERATION;
314 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700315 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800316 return INVALID_OPERATION;
317 case STATUS_IDLE:
318 case STATUS_ACTIVE:
319 // OK
320 break;
321 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700322 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800323 return INVALID_OPERATION;
324 }
325
326 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
327 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700328 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 return BAD_VALUE;
330 }
331
332 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800333}
334
335
336status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
337 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800339
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800340 switch (mStatus) {
341 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700342 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800343 return INVALID_OPERATION;
344 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700345 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800346 return INVALID_OPERATION;
347 case STATUS_IDLE:
348 case STATUS_ACTIVE:
349 // OK
350 break;
351 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700352 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800353 return INVALID_OPERATION;
354 }
355
356 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
357 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700358 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800359 return BAD_VALUE;
360 }
361
362 RequestList newRepeatingRequests;
363 newRepeatingRequests.push_back(newRepeatingRequest);
364
365 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
366}
367
368
369sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
370 const CameraMetadata &request) {
371 status_t res;
372
373 if (mStatus == STATUS_IDLE) {
374 res = configureStreamsLocked();
375 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700376 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800377 return NULL;
378 }
379 }
380
381 sp<CaptureRequest> newRequest = createCaptureRequest(request);
382 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800383}
384
385status_t Camera3Device::clearStreamingRequest() {
386 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800387 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800388
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800389 switch (mStatus) {
390 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700391 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800392 return INVALID_OPERATION;
393 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700394 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 return INVALID_OPERATION;
396 case STATUS_IDLE:
397 case STATUS_ACTIVE:
398 // OK
399 break;
400 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700401 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 return INVALID_OPERATION;
403 }
404
405 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800406}
407
408status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
409 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800410
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700411 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800412}
413
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700414status_t Camera3Device::createInputStream(
415 uint32_t width, uint32_t height, int format, int *id) {
416 ATRACE_CALL();
417 Mutex::Autolock l(mLock);
418
419 status_t res;
420 bool wasActive = false;
421
422 switch (mStatus) {
423 case STATUS_ERROR:
424 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
425 return INVALID_OPERATION;
426 case STATUS_UNINITIALIZED:
427 ALOGE("%s: Device not initialized", __FUNCTION__);
428 return INVALID_OPERATION;
429 case STATUS_IDLE:
430 // OK
431 break;
432 case STATUS_ACTIVE:
433 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
434 mRequestThread->setPaused(true);
435 res = waitUntilDrainedLocked();
436 if (res != OK) {
437 ALOGE("%s: Can't pause captures to reconfigure streams!",
438 __FUNCTION__);
439 mStatus = STATUS_ERROR;
440 return res;
441 }
442 wasActive = true;
443 break;
444 default:
445 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
446 return INVALID_OPERATION;
447 }
448 assert(mStatus == STATUS_IDLE);
449
450 if (mInputStream != 0) {
451 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
452 return INVALID_OPERATION;
453 }
454
455 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
456 width, height, format);
457
458 mInputStream = newStream;
459
460 *id = mNextStreamId++;
461
462 // Continue captures if active at start
463 if (wasActive) {
464 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
465 res = configureStreamsLocked();
466 if (res != OK) {
467 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
468 __FUNCTION__, mNextStreamId, strerror(-res), res);
469 return res;
470 }
471 mRequestThread->setPaused(false);
472 }
473
474 return OK;
475}
476
Igor Murashkin2fba5842013-04-22 14:03:54 -0700477
478status_t Camera3Device::createZslStream(
479 uint32_t width, uint32_t height,
480 int depth,
481 /*out*/
482 int *id,
483 sp<Camera3ZslStream>* zslStream) {
484 ATRACE_CALL();
485 Mutex::Autolock l(mLock);
486
487 status_t res;
488 bool wasActive = false;
489
490 switch (mStatus) {
491 case STATUS_ERROR:
492 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
493 return INVALID_OPERATION;
494 case STATUS_UNINITIALIZED:
495 ALOGE("%s: Device not initialized", __FUNCTION__);
496 return INVALID_OPERATION;
497 case STATUS_IDLE:
498 // OK
499 break;
500 case STATUS_ACTIVE:
501 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
502 mRequestThread->setPaused(true);
503 res = waitUntilDrainedLocked();
504 if (res != OK) {
505 ALOGE("%s: Can't pause captures to reconfigure streams!",
506 __FUNCTION__);
507 mStatus = STATUS_ERROR;
508 return res;
509 }
510 wasActive = true;
511 break;
512 default:
513 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
514 return INVALID_OPERATION;
515 }
516 assert(mStatus == STATUS_IDLE);
517
518 if (mInputStream != 0) {
519 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
520 return INVALID_OPERATION;
521 }
522
523 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
524 width, height, depth);
525
526 res = mOutputStreams.add(mNextStreamId, newStream);
527 if (res < 0) {
528 ALOGE("%s: Can't add new stream to set: %s (%d)",
529 __FUNCTION__, strerror(-res), res);
530 return res;
531 }
532 mInputStream = newStream;
533
534 *id = mNextStreamId++;
535 *zslStream = newStream;
536
537 // Continue captures if active at start
538 if (wasActive) {
539 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
540 res = configureStreamsLocked();
541 if (res != OK) {
542 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
543 __FUNCTION__, mNextStreamId, strerror(-res), res);
544 return res;
545 }
546 mRequestThread->setPaused(false);
547 }
548
549 return OK;
550}
551
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800552status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
553 uint32_t width, uint32_t height, int format, size_t size, int *id) {
554 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800556
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800557 status_t res;
558 bool wasActive = false;
559
560 switch (mStatus) {
561 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700562 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800563 return INVALID_OPERATION;
564 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700565 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 return INVALID_OPERATION;
567 case STATUS_IDLE:
568 // OK
569 break;
570 case STATUS_ACTIVE:
571 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
572 mRequestThread->setPaused(true);
573 res = waitUntilDrainedLocked();
574 if (res != OK) {
575 ALOGE("%s: Can't pause captures to reconfigure streams!",
576 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800577 return res;
578 }
579 wasActive = true;
580 break;
581 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700582 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800583 return INVALID_OPERATION;
584 }
585 assert(mStatus == STATUS_IDLE);
586
587 sp<Camera3OutputStream> newStream;
588 if (format == HAL_PIXEL_FORMAT_BLOB) {
589 newStream = new Camera3OutputStream(mNextStreamId, consumer,
590 width, height, size, format);
591 } else {
592 newStream = new Camera3OutputStream(mNextStreamId, consumer,
593 width, height, format);
594 }
595
596 res = mOutputStreams.add(mNextStreamId, newStream);
597 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700598 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800599 return res;
600 }
601
602 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700603 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604
605 // Continue captures if active at start
606 if (wasActive) {
607 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
608 res = configureStreamsLocked();
609 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700610 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
611 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612 return res;
613 }
614 mRequestThread->setPaused(false);
615 }
616
617 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800618}
619
620status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
621 ATRACE_CALL();
622 (void)outputId; (void)id;
623
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700624 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625 return INVALID_OPERATION;
626}
627
628
629status_t Camera3Device::getStreamInfo(int id,
630 uint32_t *width, uint32_t *height, uint32_t *format) {
631 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800633
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 switch (mStatus) {
635 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700636 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 return INVALID_OPERATION;
638 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700639 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800640 return INVALID_OPERATION;
641 case STATUS_IDLE:
642 case STATUS_ACTIVE:
643 // OK
644 break;
645 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700646 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 return INVALID_OPERATION;
648 }
649
650 ssize_t idx = mOutputStreams.indexOfKey(id);
651 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700652 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800653 return idx;
654 }
655
656 if (width) *width = mOutputStreams[idx]->getWidth();
657 if (height) *height = mOutputStreams[idx]->getHeight();
658 if (format) *format = mOutputStreams[idx]->getFormat();
659
660 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800661}
662
663status_t Camera3Device::setStreamTransform(int id,
664 int transform) {
665 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800667
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 switch (mStatus) {
669 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700670 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800671 return INVALID_OPERATION;
672 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700673 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 return INVALID_OPERATION;
675 case STATUS_IDLE:
676 case STATUS_ACTIVE:
677 // OK
678 break;
679 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700680 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 return INVALID_OPERATION;
682 }
683
684 ssize_t idx = mOutputStreams.indexOfKey(id);
685 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700686 CLOGE("Stream %d does not exist",
687 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800688 return BAD_VALUE;
689 }
690
691 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800692}
693
694status_t Camera3Device::deleteStream(int id) {
695 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696 Mutex::Autolock l(mLock);
697 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800698
Igor Murashkine2172be2013-05-28 15:31:39 -0700699 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
700
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800701 // CameraDevice semantics require device to already be idle before
702 // deleteStream is called, unlike for createStream.
703 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700704 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
705 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800706 }
707
Igor Murashkin2fba5842013-04-22 14:03:54 -0700708 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709 if (mInputStream != NULL && id == mInputStream->getId()) {
710 deletedStream = mInputStream;
711 mInputStream.clear();
712 } else {
713 ssize_t idx = mOutputStreams.indexOfKey(id);
714 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700715 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800716 return BAD_VALUE;
717 }
718 deletedStream = mOutputStreams.editValueAt(idx);
719 mOutputStreams.removeItem(id);
720 }
721
722 // Free up the stream endpoint so that it can be used by some other stream
723 res = deletedStream->disconnect();
724 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700725 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800726 // fall through since we want to still list the stream as deleted.
727 }
728 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700729 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730
731 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800732}
733
734status_t Camera3Device::deleteReprocessStream(int id) {
735 ATRACE_CALL();
736 (void)id;
737
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700738 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739 return INVALID_OPERATION;
740}
741
742
743status_t Camera3Device::createDefaultRequest(int templateId,
744 CameraMetadata *request) {
745 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700746 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747 Mutex::Autolock l(mLock);
748
749 switch (mStatus) {
750 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700751 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800752 return INVALID_OPERATION;
753 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700754 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755 return INVALID_OPERATION;
756 case STATUS_IDLE:
757 case STATUS_ACTIVE:
758 // OK
759 break;
760 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700761 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800762 return INVALID_OPERATION;
763 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800764
765 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700766 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800767 rawRequest = mHal3Device->ops->construct_default_request_settings(
768 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700769 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700770 if (rawRequest == NULL) {
771 SET_ERR_L("HAL is unable to construct default settings for template %d",
772 templateId);
773 return DEAD_OBJECT;
774 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800775 *request = rawRequest;
776
777 return OK;
778}
779
780status_t Camera3Device::waitUntilDrained() {
781 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800784 return waitUntilDrainedLocked();
785}
786
787status_t Camera3Device::waitUntilDrainedLocked() {
788 ATRACE_CALL();
789 status_t res;
790
791 switch (mStatus) {
792 case STATUS_UNINITIALIZED:
793 case STATUS_IDLE:
794 ALOGV("%s: Already idle", __FUNCTION__);
795 return OK;
796 case STATUS_ERROR:
797 case STATUS_ACTIVE:
798 // Need to shut down
799 break;
800 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700801 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802 return INVALID_OPERATION;
803 }
804
805 if (mRequestThread != NULL) {
806 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
807 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700808 SET_ERR_L("Can't stop request thread in %f seconds!",
809 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800810 return res;
811 }
812 }
813 if (mInputStream != NULL) {
814 res = mInputStream->waitUntilIdle(kShutdownTimeout);
815 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700816 SET_ERR_L("Can't idle input stream %d in %f seconds!",
817 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 return res;
819 }
820 }
821 for (size_t i = 0; i < mOutputStreams.size(); i++) {
822 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
823 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700824 SET_ERR_L("Can't idle output stream %d in %f seconds!",
825 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 return res;
827 }
828 }
829
830 if (mStatus != STATUS_ERROR) {
831 mStatus = STATUS_IDLE;
832 }
833
834 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800835}
836
837status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
838 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700839 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800840
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700841 if (listener != NULL && mListener != NULL) {
842 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
843 }
844 mListener = listener;
845
846 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800847}
848
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700849bool Camera3Device::willNotify3A() {
850 return false;
851}
852
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800853status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700854 ATRACE_CALL();
855 status_t res;
856 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800857
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700858 while (mResultQueue.empty()) {
859 res = mResultSignal.waitRelative(mOutputLock, timeout);
860 if (res == TIMED_OUT) {
861 return res;
862 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700863 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
864 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700865 return res;
866 }
867 }
868 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800869}
870
871status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
872 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700873 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800874
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700875 if (mResultQueue.empty()) {
876 return NOT_ENOUGH_DATA;
877 }
878
879 CameraMetadata &result = *(mResultQueue.begin());
880 frame->acquire(result);
881 mResultQueue.erase(mResultQueue.begin());
882
883 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800884}
885
886status_t Camera3Device::triggerAutofocus(uint32_t id) {
887 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800888
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700889 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
890 // Mix-in this trigger into the next request and only the next request.
891 RequestTrigger trigger[] = {
892 {
893 ANDROID_CONTROL_AF_TRIGGER,
894 ANDROID_CONTROL_AF_TRIGGER_START
895 },
896 {
897 ANDROID_CONTROL_AF_TRIGGER_ID,
898 static_cast<int32_t>(id)
899 },
900 };
901
902 return mRequestThread->queueTrigger(trigger,
903 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800904}
905
906status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
907 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800908
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700909 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
910 // Mix-in this trigger into the next request and only the next request.
911 RequestTrigger trigger[] = {
912 {
913 ANDROID_CONTROL_AF_TRIGGER,
914 ANDROID_CONTROL_AF_TRIGGER_CANCEL
915 },
916 {
917 ANDROID_CONTROL_AF_TRIGGER_ID,
918 static_cast<int32_t>(id)
919 },
920 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800921
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700922 return mRequestThread->queueTrigger(trigger,
923 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924}
925
926status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
927 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800928
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700929 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
930 // Mix-in this trigger into the next request and only the next request.
931 RequestTrigger trigger[] = {
932 {
933 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
934 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
935 },
936 {
937 ANDROID_CONTROL_AE_PRECAPTURE_ID,
938 static_cast<int32_t>(id)
939 },
940 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800941
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700942 return mRequestThread->queueTrigger(trigger,
943 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800944}
945
946status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
947 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
948 ATRACE_CALL();
949 (void)reprocessStreamId; (void)buffer; (void)listener;
950
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700951 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800952 return INVALID_OPERATION;
953}
954
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955/**
956 * Camera3Device private methods
957 */
958
959sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
960 const CameraMetadata &request) {
961 ATRACE_CALL();
962 status_t res;
963
964 sp<CaptureRequest> newRequest = new CaptureRequest;
965 newRequest->mSettings = request;
966
967 camera_metadata_entry_t inputStreams =
968 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
969 if (inputStreams.count > 0) {
970 if (mInputStream == NULL ||
971 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 CLOGE("Request references unknown input stream %d",
973 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 return NULL;
975 }
976 // Lazy completion of stream configuration (allocation/registration)
977 // on first use
978 if (mInputStream->isConfiguring()) {
979 res = mInputStream->finishConfiguration(mHal3Device);
980 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700981 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700983 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 return NULL;
985 }
986 }
987
988 newRequest->mInputStream = mInputStream;
989 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
990 }
991
992 camera_metadata_entry_t streams =
993 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
994 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700995 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800996 return NULL;
997 }
998
999 for (size_t i = 0; i < streams.count; i++) {
1000 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
1001 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001002 CLOGE("Request references unknown stream %d",
1003 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001004 return NULL;
1005 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001006 sp<Camera3OutputStreamInterface> stream =
1007 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008
1009 // Lazy completion of stream configuration (allocation/registration)
1010 // on first use
1011 if (stream->isConfiguring()) {
1012 res = stream->finishConfiguration(mHal3Device);
1013 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001014 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1015 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001016 return NULL;
1017 }
1018 }
1019
1020 newRequest->mOutputStreams.push(stream);
1021 }
1022 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1023
1024 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001025}
1026
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001027status_t Camera3Device::configureStreamsLocked() {
1028 ATRACE_CALL();
1029 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001030
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001032 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 return INVALID_OPERATION;
1034 }
1035
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001036 if (!mNeedConfig) {
1037 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001038 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001039 return OK;
1040 }
1041
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001042 // Start configuring the streams
1043
1044 camera3_stream_configuration config;
1045
1046 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1047
1048 Vector<camera3_stream_t*> streams;
1049 streams.setCapacity(config.num_streams);
1050
1051 if (mInputStream != NULL) {
1052 camera3_stream_t *inputStream;
1053 inputStream = mInputStream->startConfiguration();
1054 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001055 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return INVALID_OPERATION;
1057 }
1058 streams.add(inputStream);
1059 }
1060
1061 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001062
1063 // Don't configure bidi streams twice, nor add them twice to the list
1064 if (mOutputStreams[i].get() ==
1065 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1066
1067 config.num_streams--;
1068 continue;
1069 }
1070
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 camera3_stream_t *outputStream;
1072 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1073 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001074 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 return INVALID_OPERATION;
1076 }
1077 streams.add(outputStream);
1078 }
1079
1080 config.streams = streams.editArray();
1081
1082 // Do the HAL configuration; will potentially touch stream
1083 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001084 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001086 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087
1088 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001089 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1090 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 return res;
1092 }
1093
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001094 // Finish all stream configuration immediately.
1095 // TODO: Try to relax this later back to lazy completion, which should be
1096 // faster
1097
Igor Murashkin073f8572013-05-02 14:59:28 -07001098 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001099 res = mInputStream->finishConfiguration(mHal3Device);
1100 if (res != OK) {
1101 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1102 mInputStream->getId(), strerror(-res), res);
1103 return res;
1104 }
1105 }
1106
1107 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001108 sp<Camera3OutputStreamInterface> outputStream =
1109 mOutputStreams.editValueAt(i);
1110 if (outputStream->isConfiguring()) {
1111 res = outputStream->finishConfiguration(mHal3Device);
1112 if (res != OK) {
1113 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1114 outputStream->getId(), strerror(-res), res);
1115 return res;
1116 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001117 }
1118 }
1119
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 // Request thread needs to know to avoid using repeat-last-settings protocol
1121 // across configure_streams() calls
1122 mRequestThread->configurationComplete();
1123
1124 // Finish configuring the streams lazily on first reference
1125
1126 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001127 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001128
1129 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001130}
1131
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001132void Camera3Device::setErrorState(const char *fmt, ...) {
1133 Mutex::Autolock l(mLock);
1134 va_list args;
1135 va_start(args, fmt);
1136
1137 setErrorStateLockedV(fmt, args);
1138
1139 va_end(args);
1140}
1141
1142void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1143 Mutex::Autolock l(mLock);
1144 setErrorStateLockedV(fmt, args);
1145}
1146
1147void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1148 va_list args;
1149 va_start(args, fmt);
1150
1151 setErrorStateLockedV(fmt, args);
1152
1153 va_end(args);
1154}
1155
1156void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001157 // Print out all error messages to log
1158 String8 errorCause = String8::formatV(fmt, args);
1159 ALOGE("Camera %d: %s", mId, errorCause.string());
1160
1161 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001162 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001163
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001164 mErrorCause = errorCause;
1165
1166 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 mStatus = STATUS_ERROR;
1168}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169
1170/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001171 * In-flight request management
1172 */
1173
1174status_t Camera3Device::registerInFlight(int32_t frameNumber,
1175 int32_t numBuffers) {
1176 ATRACE_CALL();
1177 Mutex::Autolock l(mInFlightLock);
1178
1179 ssize_t res;
1180 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1181 if (res < 0) return res;
1182
1183 return OK;
1184}
1185
1186/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001187 * Camera HAL device callback methods
1188 */
1189
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001191 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001193 status_t res;
1194
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001195 uint32_t frameNumber = result->frame_number;
1196 if (result->result == NULL && result->num_output_buffers == 0) {
1197 SET_ERR("No result data provided by HAL for frame %d",
1198 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001199 return;
1200 }
1201
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001202 // Get capture timestamp from list of in-flight requests, where it was added
1203 // by the shutter notification for this frame. Then update the in-flight
1204 // status and remove the in-flight entry if all result data has been
1205 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001207 {
1208 Mutex::Autolock l(mInFlightLock);
1209 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1210 if (idx == NAME_NOT_FOUND) {
1211 SET_ERR("Unknown frame number for capture result: %d",
1212 frameNumber);
1213 return;
1214 }
1215 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1216 timestamp = request.captureTimestamp;
1217 if (timestamp == 0) {
1218 SET_ERR("Called before shutter notify for frame %d",
1219 frameNumber);
1220 return;
1221 }
1222
1223 if (result->result != NULL) {
1224 if (request.haveResultMetadata) {
1225 SET_ERR("Called multiple times with metadata for frame %d",
1226 frameNumber);
1227 return;
1228 }
1229 request.haveResultMetadata = true;
1230 }
1231
1232 request.numBuffersLeft -= result->num_output_buffers;
1233
1234 if (request.numBuffersLeft < 0) {
1235 SET_ERR("Too many buffers returned for frame %d",
1236 frameNumber);
1237 return;
1238 }
1239
1240 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001241 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001242 mInFlightMap.removeItemsAt(idx, 1);
1243 }
1244
1245 // Sanity check - if we have too many in-flight frames, something has
1246 // likely gone wrong
1247 if (mInFlightMap.size() > kInFlightWarnLimit) {
1248 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1249 }
1250
1251 }
1252
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001253 // Process the result metadata, if provided
1254 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001255 Mutex::Autolock l(mOutputLock);
1256
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001257 if (frameNumber != mNextResultFrameNumber) {
1258 SET_ERR("Out-of-order capture result metadata submitted! "
1259 "(got frame number %d, expecting %d)",
1260 frameNumber, mNextResultFrameNumber);
1261 return;
1262 }
1263 mNextResultFrameNumber++;
1264
1265 CameraMetadata &captureResult =
1266 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001267
1268 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001269 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001270 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001271 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001272 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001273 } else {
1274 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001275 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001276 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001277
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001278 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001279
1280 camera_metadata_entry entry =
1281 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1282 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001284 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001285 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001286 SET_ERR("Timestamp mismatch between shutter notify and result"
1287 " metadata for frame %d (%lld vs %lld respectively)",
1288 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001289 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001290 } // scope for mOutputLock
1291
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001292 // Return completed buffers to their streams with the timestamp
1293
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001294 for (size_t i = 0; i < result->num_output_buffers; i++) {
1295 Camera3Stream *stream =
1296 Camera3Stream::cast(result->output_buffers[i].stream);
1297 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1298 // Note: stream may be deallocated at this point, if this buffer was the
1299 // last reference to it.
1300 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001301 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001302 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001303 }
1304 }
1305
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001306 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001307
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001308 if (result->result != NULL) {
1309 mResultSignal.signal();
1310 }
1311
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001312}
1313
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001314
1315
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001316void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001317 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001318 NotificationListener *listener;
1319 {
1320 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001321 listener = mListener;
1322 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001323
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001324 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001325 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001326 return;
1327 }
1328
1329 switch (msg->type) {
1330 case CAMERA3_MSG_ERROR: {
1331 int streamId = 0;
1332 if (msg->message.error.error_stream != NULL) {
1333 Camera3Stream *stream =
1334 Camera3Stream::cast(
1335 msg->message.error.error_stream);
1336 streamId = stream->getId();
1337 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001338 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1339 mId, __FUNCTION__, msg->message.error.frame_number,
1340 streamId, msg->message.error.error_code);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001341 if (listener != NULL) {
1342 listener->notifyError(msg->message.error.error_code,
1343 msg->message.error.frame_number, streamId);
1344 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001345 break;
1346 }
1347 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001348 ssize_t idx;
1349 uint32_t frameNumber = msg->message.shutter.frame_number;
1350 nsecs_t timestamp = msg->message.shutter.timestamp;
1351 // Verify ordering of shutter notifications
1352 {
1353 Mutex::Autolock l(mOutputLock);
1354 if (frameNumber != mNextShutterFrameNumber) {
1355 SET_ERR("Shutter notification out-of-order. Expected "
1356 "notification for frame %d, got frame %d",
1357 mNextShutterFrameNumber, frameNumber);
1358 break;
1359 }
1360 mNextShutterFrameNumber++;
1361 }
1362
1363 // Set timestamp for the request in the in-flight tracking
1364 {
1365 Mutex::Autolock l(mInFlightLock);
1366 idx = mInFlightMap.indexOfKey(frameNumber);
1367 if (idx >= 0) {
1368 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1369 }
1370 }
1371 if (idx < 0) {
1372 SET_ERR("Shutter notification for non-existent frame number %d",
1373 frameNumber);
1374 break;
1375 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001376 ALOGVV("Camera %d: %s: Shutter fired for frame %d at %lld",
1377 mId, __FUNCTION__, frameNumber, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 // Call listener, if any
1379 if (listener != NULL) {
1380 listener->notifyShutter(frameNumber, timestamp);
1381 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001382 break;
1383 }
1384 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001385 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001386 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001387 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001388}
1389
1390/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 * RequestThread inner class methods
1392 */
1393
1394Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1395 camera3_device_t *hal3Device) :
1396 Thread(false),
1397 mParent(parent),
1398 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001399 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001400 mReconfigured(false),
1401 mDoPause(false),
1402 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001403 mFrameNumber(0),
1404 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001405}
1406
1407void Camera3Device::RequestThread::configurationComplete() {
1408 Mutex::Autolock l(mRequestLock);
1409 mReconfigured = true;
1410}
1411
1412status_t Camera3Device::RequestThread::queueRequest(
1413 sp<CaptureRequest> request) {
1414 Mutex::Autolock l(mRequestLock);
1415 mRequestQueue.push_back(request);
1416
1417 return OK;
1418}
1419
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001420
1421status_t Camera3Device::RequestThread::queueTrigger(
1422 RequestTrigger trigger[],
1423 size_t count) {
1424
1425 Mutex::Autolock l(mTriggerMutex);
1426 status_t ret;
1427
1428 for (size_t i = 0; i < count; ++i) {
1429 ret = queueTriggerLocked(trigger[i]);
1430
1431 if (ret != OK) {
1432 return ret;
1433 }
1434 }
1435
1436 return OK;
1437}
1438
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001439int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1440 sp<Camera3Device> d = device.promote();
1441 if (d != NULL) return d->mId;
1442 return 0;
1443}
1444
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001445status_t Camera3Device::RequestThread::queueTriggerLocked(
1446 RequestTrigger trigger) {
1447
1448 uint32_t tag = trigger.metadataTag;
1449 ssize_t index = mTriggerMap.indexOfKey(tag);
1450
1451 switch (trigger.getTagType()) {
1452 case TYPE_BYTE:
1453 // fall-through
1454 case TYPE_INT32:
1455 break;
1456 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001457 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1458 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001459 return INVALID_OPERATION;
1460 }
1461
1462 /**
1463 * Collect only the latest trigger, since we only have 1 field
1464 * in the request settings per trigger tag, and can't send more than 1
1465 * trigger per request.
1466 */
1467 if (index != NAME_NOT_FOUND) {
1468 mTriggerMap.editValueAt(index) = trigger;
1469 } else {
1470 mTriggerMap.add(tag, trigger);
1471 }
1472
1473 return OK;
1474}
1475
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001476status_t Camera3Device::RequestThread::setRepeatingRequests(
1477 const RequestList &requests) {
1478 Mutex::Autolock l(mRequestLock);
1479 mRepeatingRequests.clear();
1480 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1481 requests.begin(), requests.end());
1482 return OK;
1483}
1484
1485status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1486 Mutex::Autolock l(mRequestLock);
1487 mRepeatingRequests.clear();
1488 return OK;
1489}
1490
1491void Camera3Device::RequestThread::setPaused(bool paused) {
1492 Mutex::Autolock l(mPauseLock);
1493 mDoPause = paused;
1494 mDoPauseSignal.signal();
1495}
1496
1497status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001498 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001499 status_t res;
1500 Mutex::Autolock l(mPauseLock);
1501 while (!mPaused) {
1502 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1503 if (res == TIMED_OUT) {
1504 return res;
1505 }
1506 }
1507 return OK;
1508}
1509
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001510status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1511 int32_t requestId, nsecs_t timeout) {
1512 Mutex::Autolock l(mLatestRequestMutex);
1513 status_t res;
1514 while (mLatestRequestId != requestId) {
1515 nsecs_t startTime = systemTime();
1516
1517 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1518 if (res != OK) return res;
1519
1520 timeout -= (systemTime() - startTime);
1521 }
1522
1523 return OK;
1524}
1525
1526
1527
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001528bool Camera3Device::RequestThread::threadLoop() {
1529
1530 status_t res;
1531
1532 // Handle paused state.
1533 if (waitIfPaused()) {
1534 return true;
1535 }
1536
1537 // Get work to do
1538
1539 sp<CaptureRequest> nextRequest = waitForNextRequest();
1540 if (nextRequest == NULL) {
1541 return true;
1542 }
1543
1544 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001546 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001547
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001548 // Insert any queued triggers (before metadata is locked)
1549 int32_t triggerCount;
1550 res = insertTriggers(nextRequest);
1551 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001552 SET_ERR("RequestThread: Unable to insert triggers "
1553 "(capture request %d, HAL device: %s (%d)",
1554 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001555 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1556 return false;
1557 }
1558 triggerCount = res;
1559
1560 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1561
1562 // If the request is the same as last, or we had triggers last time
1563 if (mPrevRequest != nextRequest || triggersMixedIn) {
1564 /**
1565 * The request should be presorted so accesses in HAL
1566 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1567 */
1568 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569 request.settings = nextRequest->mSettings.getAndLock();
1570 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001571 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1572
1573 IF_ALOGV() {
1574 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1575 find_camera_metadata_ro_entry(
1576 request.settings,
1577 ANDROID_CONTROL_AF_TRIGGER,
1578 &e
1579 );
1580 if (e.count > 0) {
1581 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1582 __FUNCTION__,
1583 mFrameNumber+1,
1584 e.data.u8[0]);
1585 }
1586 }
1587 } else {
1588 // leave request.settings NULL to indicate 'reuse latest given'
1589 ALOGVV("%s: Request settings are REUSED",
1590 __FUNCTION__);
1591 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592
1593 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001594
1595 // Fill in buffers
1596
1597 if (nextRequest->mInputStream != NULL) {
1598 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001599 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001601 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001602 " %s (%d)", strerror(-res), res);
1603 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1604 return true;
1605 }
1606 } else {
1607 request.input_buffer = NULL;
1608 }
1609
1610 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1611 nextRequest->mOutputStreams.size());
1612 request.output_buffers = outputBuffers.array();
1613 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1614 res = nextRequest->mOutputStreams.editItemAt(i)->
1615 getBuffer(&outputBuffers.editItemAt(i));
1616 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001617 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001618 "%s (%d)", strerror(-res), res);
1619 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1620 return true;
1621 }
1622 request.num_output_buffers++;
1623 }
1624
1625 request.frame_number = mFrameNumber++;
1626
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001627 // Log request in the in-flight queue
1628 sp<Camera3Device> parent = mParent.promote();
1629 if (parent == NULL) {
1630 CLOGE("RequestThread: Parent is gone");
1631 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1632 return false;
1633 }
1634
1635 res = parent->registerInFlight(request.frame_number,
1636 request.num_output_buffers);
1637 if (res != OK) {
1638 SET_ERR("RequestThread: Unable to register new in-flight request:"
1639 " %s (%d)", strerror(-res), res);
1640 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1641 return false;
1642 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001643
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001644 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001645 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1646 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001647 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001648 ATRACE_END();
1649
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001650 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001651 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 " device: %s (%d)", request.frame_number, strerror(-res), res);
1653 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1654 return false;
1655 }
1656
1657 if (request.settings != NULL) {
1658 nextRequest->mSettings.unlock(request.settings);
1659 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001660
1661 // Remove any previously queued triggers (after unlock)
1662 res = removeTriggers(mPrevRequest);
1663 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001664 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001665 "(capture request %d, HAL device: %s (%d)",
1666 request.frame_number, strerror(-res), res);
1667 return false;
1668 }
1669 mPrevTriggers = triggerCount;
1670
1671 // Read android.request.id from the request settings metadata
1672 // - inform waitUntilRequestProcessed thread of a new request ID
1673 {
1674 Mutex::Autolock al(mLatestRequestMutex);
1675
1676 camera_metadata_entry_t requestIdEntry =
1677 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1678 if (requestIdEntry.count > 0) {
1679 mLatestRequestId = requestIdEntry.data.i32[0];
1680 } else {
1681 ALOGW("%s: Did not have android.request.id set in the request",
1682 __FUNCTION__);
1683 mLatestRequestId = NAME_NOT_FOUND;
1684 }
1685
1686 mLatestRequestSignal.signal();
1687 }
1688
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001689 // Return input buffer back to framework
1690 if (request.input_buffer != NULL) {
1691 Camera3Stream *stream =
1692 Camera3Stream::cast(request.input_buffer->stream);
1693 res = stream->returnInputBuffer(*(request.input_buffer));
1694 // Note: stream may be deallocated at this point, if this buffer was the
1695 // last reference to it.
1696 if (res != OK) {
1697 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1698 " its stream:%s (%d)", __FUNCTION__,
1699 request.frame_number, strerror(-res), res);
1700 // TODO: Report error upstream
1701 }
1702 }
1703
1704
1705
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001706 return true;
1707}
1708
1709void Camera3Device::RequestThread::cleanUpFailedRequest(
1710 camera3_capture_request_t &request,
1711 sp<CaptureRequest> &nextRequest,
1712 Vector<camera3_stream_buffer_t> &outputBuffers) {
1713
1714 if (request.settings != NULL) {
1715 nextRequest->mSettings.unlock(request.settings);
1716 }
1717 if (request.input_buffer != NULL) {
1718 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001719 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001720 }
1721 for (size_t i = 0; i < request.num_output_buffers; i++) {
1722 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1723 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1724 outputBuffers[i], 0);
1725 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001726}
1727
1728sp<Camera3Device::CaptureRequest>
1729 Camera3Device::RequestThread::waitForNextRequest() {
1730 status_t res;
1731 sp<CaptureRequest> nextRequest;
1732
1733 // Optimized a bit for the simple steady-state case (single repeating
1734 // request), to avoid putting that request in the queue temporarily.
1735 Mutex::Autolock l(mRequestLock);
1736
1737 while (mRequestQueue.empty()) {
1738 if (!mRepeatingRequests.empty()) {
1739 // Always atomically enqueue all requests in a repeating request
1740 // list. Guarantees a complete in-sequence set of captures to
1741 // application.
1742 const RequestList &requests = mRepeatingRequests;
1743 RequestList::const_iterator firstRequest =
1744 requests.begin();
1745 nextRequest = *firstRequest;
1746 mRequestQueue.insert(mRequestQueue.end(),
1747 ++firstRequest,
1748 requests.end());
1749 // No need to wait any longer
1750 break;
1751 }
1752
1753 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1754
1755 if (res == TIMED_OUT) {
1756 // Signal that we're paused by starvation
1757 Mutex::Autolock pl(mPauseLock);
1758 if (mPaused == false) {
1759 mPaused = true;
1760 mPausedSignal.signal();
1761 }
1762 // Stop waiting for now and let thread management happen
1763 return NULL;
1764 }
1765 }
1766
1767 if (nextRequest == NULL) {
1768 // Don't have a repeating request already in hand, so queue
1769 // must have an entry now.
1770 RequestList::iterator firstRequest =
1771 mRequestQueue.begin();
1772 nextRequest = *firstRequest;
1773 mRequestQueue.erase(firstRequest);
1774 }
1775
1776 // Not paused
1777 Mutex::Autolock pl(mPauseLock);
1778 mPaused = false;
1779
1780 // Check if we've reconfigured since last time, and reset the preview
1781 // request if so. Can't use 'NULL request == repeat' across configure calls.
1782 if (mReconfigured) {
1783 mPrevRequest.clear();
1784 mReconfigured = false;
1785 }
1786
1787 return nextRequest;
1788}
1789
1790bool Camera3Device::RequestThread::waitIfPaused() {
1791 status_t res;
1792 Mutex::Autolock l(mPauseLock);
1793 while (mDoPause) {
1794 // Signal that we're paused by request
1795 if (mPaused == false) {
1796 mPaused = true;
1797 mPausedSignal.signal();
1798 }
1799 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1800 if (res == TIMED_OUT) {
1801 return true;
1802 }
1803 }
1804 // We don't set mPaused to false here, because waitForNextRequest needs
1805 // to further manage the paused state in case of starvation.
1806 return false;
1807}
1808
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001809void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1810 sp<Camera3Device> parent = mParent.promote();
1811 if (parent != NULL) {
1812 va_list args;
1813 va_start(args, fmt);
1814
1815 parent->setErrorStateV(fmt, args);
1816
1817 va_end(args);
1818 }
1819}
1820
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001821status_t Camera3Device::RequestThread::insertTriggers(
1822 const sp<CaptureRequest> &request) {
1823
1824 Mutex::Autolock al(mTriggerMutex);
1825
1826 CameraMetadata &metadata = request->mSettings;
1827 size_t count = mTriggerMap.size();
1828
1829 for (size_t i = 0; i < count; ++i) {
1830 RequestTrigger trigger = mTriggerMap.valueAt(i);
1831
1832 uint32_t tag = trigger.metadataTag;
1833 camera_metadata_entry entry = metadata.find(tag);
1834
1835 if (entry.count > 0) {
1836 /**
1837 * Already has an entry for this trigger in the request.
1838 * Rewrite it with our requested trigger value.
1839 */
1840 RequestTrigger oldTrigger = trigger;
1841
1842 oldTrigger.entryValue = entry.data.u8[0];
1843
1844 mTriggerReplacedMap.add(tag, oldTrigger);
1845 } else {
1846 /**
1847 * More typical, no trigger entry, so we just add it
1848 */
1849 mTriggerRemovedMap.add(tag, trigger);
1850 }
1851
1852 status_t res;
1853
1854 switch (trigger.getTagType()) {
1855 case TYPE_BYTE: {
1856 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1857 res = metadata.update(tag,
1858 &entryValue,
1859 /*count*/1);
1860 break;
1861 }
1862 case TYPE_INT32:
1863 res = metadata.update(tag,
1864 &trigger.entryValue,
1865 /*count*/1);
1866 break;
1867 default:
1868 ALOGE("%s: Type not supported: 0x%x",
1869 __FUNCTION__,
1870 trigger.getTagType());
1871 return INVALID_OPERATION;
1872 }
1873
1874 if (res != OK) {
1875 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1876 ", value %d", __FUNCTION__, trigger.getTagName(),
1877 trigger.entryValue);
1878 return res;
1879 }
1880
1881 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1882 trigger.getTagName(),
1883 trigger.entryValue);
1884 }
1885
1886 mTriggerMap.clear();
1887
1888 return count;
1889}
1890
1891status_t Camera3Device::RequestThread::removeTriggers(
1892 const sp<CaptureRequest> &request) {
1893 Mutex::Autolock al(mTriggerMutex);
1894
1895 CameraMetadata &metadata = request->mSettings;
1896
1897 /**
1898 * Replace all old entries with their old values.
1899 */
1900 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1901 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1902
1903 status_t res;
1904
1905 uint32_t tag = trigger.metadataTag;
1906 switch (trigger.getTagType()) {
1907 case TYPE_BYTE: {
1908 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1909 res = metadata.update(tag,
1910 &entryValue,
1911 /*count*/1);
1912 break;
1913 }
1914 case TYPE_INT32:
1915 res = metadata.update(tag,
1916 &trigger.entryValue,
1917 /*count*/1);
1918 break;
1919 default:
1920 ALOGE("%s: Type not supported: 0x%x",
1921 __FUNCTION__,
1922 trigger.getTagType());
1923 return INVALID_OPERATION;
1924 }
1925
1926 if (res != OK) {
1927 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1928 ", trigger value %d", __FUNCTION__,
1929 trigger.getTagName(), trigger.entryValue);
1930 return res;
1931 }
1932 }
1933 mTriggerReplacedMap.clear();
1934
1935 /**
1936 * Remove all new entries.
1937 */
1938 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1939 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1940 status_t res = metadata.erase(trigger.metadataTag);
1941
1942 if (res != OK) {
1943 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1944 ", trigger value %d", __FUNCTION__,
1945 trigger.getTagName(), trigger.entryValue);
1946 return res;
1947 }
1948 }
1949 mTriggerRemovedMap.clear();
1950
1951 return OK;
1952}
1953
1954
1955
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001956/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001957 * Static callback forwarding methods from HAL to instance
1958 */
1959
1960void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1961 const camera3_capture_result *result) {
1962 Camera3Device *d =
1963 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1964 d->processCaptureResult(result);
1965}
1966
1967void Camera3Device::sNotify(const camera3_callback_ops *cb,
1968 const camera3_notify_msg *msg) {
1969 Camera3Device *d =
1970 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1971 d->notify(msg);
1972}
1973
1974}; // namespace android