blob: 6ee6901e4f962f2424c43711f6fdf74628dc0eb0 [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>
43#include "Camera3Device.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080044#include "camera3/Camera3OutputStream.h"
Igor Murashkin5a269fa2013-04-15 14:59:22 -070045#include "camera3/Camera3InputStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080046
47using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080048
49namespace android {
50
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051Camera3Device::Camera3Device(int id):
52 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070054 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070055 mNextResultFrameNumber(0),
56 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058{
59 ATRACE_CALL();
60 camera3_callback_ops::notify = &sNotify;
61 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
62 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
63}
64
65Camera3Device::~Camera3Device()
66{
67 ATRACE_CALL();
68 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
69 disconnect();
70}
71
Igor Murashkin71381052013-03-04 14:53:08 -080072int Camera3Device::getId() const {
73 return mId;
74}
75
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080076/**
77 * CameraDeviceBase interface
78 */
79
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080status_t Camera3Device::initialize(camera_module_t *module)
81{
82 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080083 Mutex::Autolock l(mLock);
84
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070087 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 return INVALID_OPERATION;
89 }
90
91 /** Open HAL device */
92
93 status_t res;
94 String8 deviceName = String8::format("%d", mId);
95
96 camera3_device_t *device;
97
98 res = module->common.methods->open(&module->common, deviceName.string(),
99 reinterpret_cast<hw_device_t**>(&device));
100
101 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700102 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 return res;
104 }
105
106 /** Cross-check device version */
107
108 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 device->common.version);
113 device->common.close(&device->common);
114 return BAD_VALUE;
115 }
116
117 camera_info info;
118 res = module->get_camera_info(mId, &info);
119 if (res != OK) return res;
120
121 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
123 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version, info.device_version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 /** Initialize device with callback functions */
130
131 res = device->ops->initialize(device, this);
132 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700133 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
134 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 device->common.close(&device->common);
136 return BAD_VALUE;
137 }
138
139 /** Get vendor metadata tags */
140
141 mVendorTagOps.get_camera_vendor_section_name = NULL;
142
143 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
144
145 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
146 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
147 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700148 SET_ERR_L("Unable to set tag ops: %s (%d)",
149 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 device->common.close(&device->common);
151 return res;
152 }
153 }
154
155 /** Start up request queue thread */
156
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800157 mRequestThread = new RequestThread(this, device);
158 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800159 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700160 SET_ERR_L("Unable to start request queue thread: %s (%d)",
161 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800163 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 return res;
165 }
166
167 /** Everything is good to go */
168
169 mDeviceInfo = info.static_camera_characteristics;
170 mHal3Device = device;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800171 mStatus = STATUS_IDLE;
172 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700173 mNeedConfig = true;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174
175 return OK;
176}
177
178status_t Camera3Device::disconnect() {
179 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800180 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 ALOGV("%s: E", __FUNCTION__);
183
184 status_t res;
185 if (mStatus == STATUS_UNINITIALIZED) return OK;
186
187 if (mStatus == STATUS_ACTIVE ||
188 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
189 res = mRequestThread->clearRepeatingRequests();
190 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700191 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800192 return res;
193 }
194 res = waitUntilDrainedLocked();
195 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700196 SET_ERR_L("Timeout waiting for HAL to drain");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800197 return res;
198 }
199 }
200 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
201
202 if (mRequestThread != NULL) {
203 mRequestThread->requestExit();
204 }
205
206 mOutputStreams.clear();
207 mInputStream.clear();
208
209 if (mRequestThread != NULL) {
210 mRequestThread->join();
211 mRequestThread.clear();
212 }
213
214 if (mHal3Device != NULL) {
215 mHal3Device->common.close(&mHal3Device->common);
216 mHal3Device = NULL;
217 }
218
219 mStatus = STATUS_UNINITIALIZED;
220
221 ALOGV("%s: X", __FUNCTION__);
222 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800223}
224
225status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
226 ATRACE_CALL();
227 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800228 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800229
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800230 const char *status =
231 mStatus == STATUS_ERROR ? "ERROR" :
232 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
233 mStatus == STATUS_IDLE ? "IDLE" :
234 mStatus == STATUS_ACTIVE ? "ACTIVE" :
235 "Unknown";
236 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700237 if (mStatus == STATUS_ERROR) {
238 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
239 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240 lines.appendFormat(" Stream configuration:\n");
241
242 if (mInputStream != NULL) {
243 write(fd, lines.string(), lines.size());
244 mInputStream->dump(fd, args);
245 } else {
246 lines.appendFormat(" No input stream.\n");
247 write(fd, lines.string(), lines.size());
248 }
249 for (size_t i = 0; i < mOutputStreams.size(); i++) {
250 mOutputStreams[i]->dump(fd,args);
251 }
252
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700253 lines = String8(" In-flight requests:\n");
254 if (mInFlightMap.size() == 0) {
255 lines.append(" None\n");
256 } else {
257 for (size_t i = 0; i < mInFlightMap.size(); i++) {
258 InFlightRequest r = mInFlightMap.valueAt(i);
259 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
260 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
261 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
262 r.numBuffersLeft);
263 }
264 }
265 write(fd, lines.string(), lines.size());
266
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700268 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269 write(fd, lines.string(), lines.size());
270 mHal3Device->ops->dump(mHal3Device, fd);
271 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800272
273 return OK;
274}
275
276const CameraMetadata& Camera3Device::info() const {
277 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800278 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
279 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700280 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800281 mStatus == STATUS_ERROR ?
282 "when in error state" : "before init");
283 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800284 return mDeviceInfo;
285}
286
287status_t Camera3Device::capture(CameraMetadata &request) {
288 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800289 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800290
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700291 // TODO: take ownership of the request
292
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293 switch (mStatus) {
294 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700295 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800296 return INVALID_OPERATION;
297 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700298 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800299 return INVALID_OPERATION;
300 case STATUS_IDLE:
301 case STATUS_ACTIVE:
302 // OK
303 break;
304 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700305 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 return INVALID_OPERATION;
307 }
308
309 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
310 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700311 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800312 return BAD_VALUE;
313 }
314
315 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800316}
317
318
319status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
320 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800322
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800323 switch (mStatus) {
324 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700325 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 return INVALID_OPERATION;
327 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700328 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 return INVALID_OPERATION;
330 case STATUS_IDLE:
331 case STATUS_ACTIVE:
332 // OK
333 break;
334 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700335 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336 return INVALID_OPERATION;
337 }
338
339 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
340 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700341 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800342 return BAD_VALUE;
343 }
344
345 RequestList newRepeatingRequests;
346 newRepeatingRequests.push_back(newRepeatingRequest);
347
348 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
349}
350
351
352sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
353 const CameraMetadata &request) {
354 status_t res;
355
356 if (mStatus == STATUS_IDLE) {
357 res = configureStreamsLocked();
358 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700359 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800360 return NULL;
361 }
362 }
363
364 sp<CaptureRequest> newRequest = createCaptureRequest(request);
365 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800366}
367
368status_t Camera3Device::clearStreamingRequest() {
369 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800371
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800372 switch (mStatus) {
373 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700374 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800375 return INVALID_OPERATION;
376 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700377 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800378 return INVALID_OPERATION;
379 case STATUS_IDLE:
380 case STATUS_ACTIVE:
381 // OK
382 break;
383 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700384 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 return INVALID_OPERATION;
386 }
387
388 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800389}
390
391status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
392 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800393
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700394 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800395}
396
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700397status_t Camera3Device::createInputStream(
398 uint32_t width, uint32_t height, int format, int *id) {
399 ATRACE_CALL();
400 Mutex::Autolock l(mLock);
401
402 status_t res;
403 bool wasActive = false;
404
405 switch (mStatus) {
406 case STATUS_ERROR:
407 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
408 return INVALID_OPERATION;
409 case STATUS_UNINITIALIZED:
410 ALOGE("%s: Device not initialized", __FUNCTION__);
411 return INVALID_OPERATION;
412 case STATUS_IDLE:
413 // OK
414 break;
415 case STATUS_ACTIVE:
416 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
417 mRequestThread->setPaused(true);
418 res = waitUntilDrainedLocked();
419 if (res != OK) {
420 ALOGE("%s: Can't pause captures to reconfigure streams!",
421 __FUNCTION__);
422 mStatus = STATUS_ERROR;
423 return res;
424 }
425 wasActive = true;
426 break;
427 default:
428 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
429 return INVALID_OPERATION;
430 }
431 assert(mStatus == STATUS_IDLE);
432
433 if (mInputStream != 0) {
434 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
435 return INVALID_OPERATION;
436 }
437
438 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
439 width, height, format);
440
441 mInputStream = newStream;
442
443 *id = mNextStreamId++;
444
445 // Continue captures if active at start
446 if (wasActive) {
447 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
448 res = configureStreamsLocked();
449 if (res != OK) {
450 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
451 __FUNCTION__, mNextStreamId, strerror(-res), res);
452 return res;
453 }
454 mRequestThread->setPaused(false);
455 }
456
457 return OK;
458}
459
Igor Murashkin2fba5842013-04-22 14:03:54 -0700460
461status_t Camera3Device::createZslStream(
462 uint32_t width, uint32_t height,
463 int depth,
464 /*out*/
465 int *id,
466 sp<Camera3ZslStream>* zslStream) {
467 ATRACE_CALL();
468 Mutex::Autolock l(mLock);
469
470 status_t res;
471 bool wasActive = false;
472
473 switch (mStatus) {
474 case STATUS_ERROR:
475 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
476 return INVALID_OPERATION;
477 case STATUS_UNINITIALIZED:
478 ALOGE("%s: Device not initialized", __FUNCTION__);
479 return INVALID_OPERATION;
480 case STATUS_IDLE:
481 // OK
482 break;
483 case STATUS_ACTIVE:
484 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
485 mRequestThread->setPaused(true);
486 res = waitUntilDrainedLocked();
487 if (res != OK) {
488 ALOGE("%s: Can't pause captures to reconfigure streams!",
489 __FUNCTION__);
490 mStatus = STATUS_ERROR;
491 return res;
492 }
493 wasActive = true;
494 break;
495 default:
496 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
497 return INVALID_OPERATION;
498 }
499 assert(mStatus == STATUS_IDLE);
500
501 if (mInputStream != 0) {
502 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
503 return INVALID_OPERATION;
504 }
505
506 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
507 width, height, depth);
508
509 res = mOutputStreams.add(mNextStreamId, newStream);
510 if (res < 0) {
511 ALOGE("%s: Can't add new stream to set: %s (%d)",
512 __FUNCTION__, strerror(-res), res);
513 return res;
514 }
515 mInputStream = newStream;
516
517 *id = mNextStreamId++;
518 *zslStream = newStream;
519
520 // Continue captures if active at start
521 if (wasActive) {
522 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
523 res = configureStreamsLocked();
524 if (res != OK) {
525 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
526 __FUNCTION__, mNextStreamId, strerror(-res), res);
527 return res;
528 }
529 mRequestThread->setPaused(false);
530 }
531
532 return OK;
533}
534
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800535status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
536 uint32_t width, uint32_t height, int format, size_t size, int *id) {
537 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800538 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800540 status_t res;
541 bool wasActive = false;
542
543 switch (mStatus) {
544 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700545 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800546 return INVALID_OPERATION;
547 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700548 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800549 return INVALID_OPERATION;
550 case STATUS_IDLE:
551 // OK
552 break;
553 case STATUS_ACTIVE:
554 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
555 mRequestThread->setPaused(true);
556 res = waitUntilDrainedLocked();
557 if (res != OK) {
558 ALOGE("%s: Can't pause captures to reconfigure streams!",
559 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800560 return res;
561 }
562 wasActive = true;
563 break;
564 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700565 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800566 return INVALID_OPERATION;
567 }
568 assert(mStatus == STATUS_IDLE);
569
570 sp<Camera3OutputStream> newStream;
571 if (format == HAL_PIXEL_FORMAT_BLOB) {
572 newStream = new Camera3OutputStream(mNextStreamId, consumer,
573 width, height, size, format);
574 } else {
575 newStream = new Camera3OutputStream(mNextStreamId, consumer,
576 width, height, format);
577 }
578
579 res = mOutputStreams.add(mNextStreamId, newStream);
580 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700581 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800582 return res;
583 }
584
585 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700586 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800587
588 // Continue captures if active at start
589 if (wasActive) {
590 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
591 res = configureStreamsLocked();
592 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700593 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
594 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800595 return res;
596 }
597 mRequestThread->setPaused(false);
598 }
599
600 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800601}
602
603status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
604 ATRACE_CALL();
605 (void)outputId; (void)id;
606
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700607 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800608 return INVALID_OPERATION;
609}
610
611
612status_t Camera3Device::getStreamInfo(int id,
613 uint32_t *width, uint32_t *height, uint32_t *format) {
614 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800615 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800616
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800617 switch (mStatus) {
618 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700619 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620 return INVALID_OPERATION;
621 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700622 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800623 return INVALID_OPERATION;
624 case STATUS_IDLE:
625 case STATUS_ACTIVE:
626 // OK
627 break;
628 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700629 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 return INVALID_OPERATION;
631 }
632
633 ssize_t idx = mOutputStreams.indexOfKey(id);
634 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700635 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800636 return idx;
637 }
638
639 if (width) *width = mOutputStreams[idx]->getWidth();
640 if (height) *height = mOutputStreams[idx]->getHeight();
641 if (format) *format = mOutputStreams[idx]->getFormat();
642
643 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800644}
645
646status_t Camera3Device::setStreamTransform(int id,
647 int transform) {
648 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 switch (mStatus) {
652 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700653 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 return INVALID_OPERATION;
655 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700656 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800657 return INVALID_OPERATION;
658 case STATUS_IDLE:
659 case STATUS_ACTIVE:
660 // OK
661 break;
662 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700663 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 return INVALID_OPERATION;
665 }
666
667 ssize_t idx = mOutputStreams.indexOfKey(id);
668 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700669 CLOGE("Stream %d does not exist",
670 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800671 return BAD_VALUE;
672 }
673
674 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800675}
676
677status_t Camera3Device::deleteStream(int id) {
678 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 Mutex::Autolock l(mLock);
680 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800681
Igor Murashkine2172be2013-05-28 15:31:39 -0700682 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
683
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800684 // CameraDevice semantics require device to already be idle before
685 // deleteStream is called, unlike for createStream.
686 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700687 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
688 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800689 }
690
Igor Murashkin2fba5842013-04-22 14:03:54 -0700691 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800692 if (mInputStream != NULL && id == mInputStream->getId()) {
693 deletedStream = mInputStream;
694 mInputStream.clear();
695 } else {
696 ssize_t idx = mOutputStreams.indexOfKey(id);
697 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700698 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699 return BAD_VALUE;
700 }
701 deletedStream = mOutputStreams.editValueAt(idx);
702 mOutputStreams.removeItem(id);
703 }
704
705 // Free up the stream endpoint so that it can be used by some other stream
706 res = deletedStream->disconnect();
707 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700708 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709 // fall through since we want to still list the stream as deleted.
710 }
711 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700712 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800713
714 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715}
716
717status_t Camera3Device::deleteReprocessStream(int id) {
718 ATRACE_CALL();
719 (void)id;
720
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700721 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800722 return INVALID_OPERATION;
723}
724
725
726status_t Camera3Device::createDefaultRequest(int templateId,
727 CameraMetadata *request) {
728 ATRACE_CALL();
729 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730 Mutex::Autolock l(mLock);
731
732 switch (mStatus) {
733 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700734 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735 return INVALID_OPERATION;
736 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700737 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800738 return INVALID_OPERATION;
739 case STATUS_IDLE:
740 case STATUS_ACTIVE:
741 // OK
742 break;
743 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700744 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800745 return INVALID_OPERATION;
746 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800747
748 const camera_metadata_t *rawRequest;
749 rawRequest = mHal3Device->ops->construct_default_request_settings(
750 mHal3Device, templateId);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700751 if (rawRequest == NULL) {
752 SET_ERR_L("HAL is unable to construct default settings for template %d",
753 templateId);
754 return DEAD_OBJECT;
755 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800756 *request = rawRequest;
757
758 return OK;
759}
760
761status_t Camera3Device::waitUntilDrained() {
762 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800763 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800764
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800765 return waitUntilDrainedLocked();
766}
767
768status_t Camera3Device::waitUntilDrainedLocked() {
769 ATRACE_CALL();
770 status_t res;
771
772 switch (mStatus) {
773 case STATUS_UNINITIALIZED:
774 case STATUS_IDLE:
775 ALOGV("%s: Already idle", __FUNCTION__);
776 return OK;
777 case STATUS_ERROR:
778 case STATUS_ACTIVE:
779 // Need to shut down
780 break;
781 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700782 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800783 return INVALID_OPERATION;
784 }
785
786 if (mRequestThread != NULL) {
787 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
788 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700789 SET_ERR_L("Can't stop request thread in %f seconds!",
790 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791 return res;
792 }
793 }
794 if (mInputStream != NULL) {
795 res = mInputStream->waitUntilIdle(kShutdownTimeout);
796 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700797 SET_ERR_L("Can't idle input stream %d in %f seconds!",
798 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800799 return res;
800 }
801 }
802 for (size_t i = 0; i < mOutputStreams.size(); i++) {
803 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
804 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700805 SET_ERR_L("Can't idle output stream %d in %f seconds!",
806 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800807 return res;
808 }
809 }
810
811 if (mStatus != STATUS_ERROR) {
812 mStatus = STATUS_IDLE;
813 }
814
815 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800816}
817
818status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
819 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700820 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800821
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700822 if (listener != NULL && mListener != NULL) {
823 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
824 }
825 mListener = listener;
826
827 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800828}
829
830status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700831 ATRACE_CALL();
832 status_t res;
833 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800834
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700835 while (mResultQueue.empty()) {
836 res = mResultSignal.waitRelative(mOutputLock, timeout);
837 if (res == TIMED_OUT) {
838 return res;
839 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700840 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
841 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700842 return res;
843 }
844 }
845 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846}
847
848status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
849 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700850 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700852 if (mResultQueue.empty()) {
853 return NOT_ENOUGH_DATA;
854 }
855
856 CameraMetadata &result = *(mResultQueue.begin());
857 frame->acquire(result);
858 mResultQueue.erase(mResultQueue.begin());
859
860 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800861}
862
863status_t Camera3Device::triggerAutofocus(uint32_t id) {
864 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700866 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
867 // Mix-in this trigger into the next request and only the next request.
868 RequestTrigger trigger[] = {
869 {
870 ANDROID_CONTROL_AF_TRIGGER,
871 ANDROID_CONTROL_AF_TRIGGER_START
872 },
873 {
874 ANDROID_CONTROL_AF_TRIGGER_ID,
875 static_cast<int32_t>(id)
876 },
877 };
878
879 return mRequestThread->queueTrigger(trigger,
880 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800881}
882
883status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
884 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800885
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700886 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
887 // Mix-in this trigger into the next request and only the next request.
888 RequestTrigger trigger[] = {
889 {
890 ANDROID_CONTROL_AF_TRIGGER,
891 ANDROID_CONTROL_AF_TRIGGER_CANCEL
892 },
893 {
894 ANDROID_CONTROL_AF_TRIGGER_ID,
895 static_cast<int32_t>(id)
896 },
897 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800898
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700899 return mRequestThread->queueTrigger(trigger,
900 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800901}
902
903status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
904 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800905
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700906 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
907 // Mix-in this trigger into the next request and only the next request.
908 RequestTrigger trigger[] = {
909 {
910 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
911 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
912 },
913 {
914 ANDROID_CONTROL_AE_PRECAPTURE_ID,
915 static_cast<int32_t>(id)
916 },
917 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700919 return mRequestThread->queueTrigger(trigger,
920 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800921}
922
923status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
924 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
925 ATRACE_CALL();
926 (void)reprocessStreamId; (void)buffer; (void)listener;
927
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700928 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800929 return INVALID_OPERATION;
930}
931
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932/**
933 * Camera3Device private methods
934 */
935
936sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
937 const CameraMetadata &request) {
938 ATRACE_CALL();
939 status_t res;
940
941 sp<CaptureRequest> newRequest = new CaptureRequest;
942 newRequest->mSettings = request;
943
944 camera_metadata_entry_t inputStreams =
945 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
946 if (inputStreams.count > 0) {
947 if (mInputStream == NULL ||
948 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700949 CLOGE("Request references unknown input stream %d",
950 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951 return NULL;
952 }
953 // Lazy completion of stream configuration (allocation/registration)
954 // on first use
955 if (mInputStream->isConfiguring()) {
956 res = mInputStream->finishConfiguration(mHal3Device);
957 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700958 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700960 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return NULL;
962 }
963 }
964
965 newRequest->mInputStream = mInputStream;
966 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
967 }
968
969 camera_metadata_entry_t streams =
970 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
971 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 return NULL;
974 }
975
976 for (size_t i = 0; i < streams.count; i++) {
977 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
978 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700979 CLOGE("Request references unknown stream %d",
980 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800981 return NULL;
982 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700983 sp<Camera3OutputStreamInterface> stream =
984 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800985
986 // Lazy completion of stream configuration (allocation/registration)
987 // on first use
988 if (stream->isConfiguring()) {
989 res = stream->finishConfiguration(mHal3Device);
990 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700991 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
992 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993 return NULL;
994 }
995 }
996
997 newRequest->mOutputStreams.push(stream);
998 }
999 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1000
1001 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002}
1003
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001004status_t Camera3Device::configureStreamsLocked() {
1005 ATRACE_CALL();
1006 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001007
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001008 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001009 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001010 return INVALID_OPERATION;
1011 }
1012
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001013 if (!mNeedConfig) {
1014 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001015 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001016 return OK;
1017 }
1018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 // Start configuring the streams
1020
1021 camera3_stream_configuration config;
1022
1023 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1024
1025 Vector<camera3_stream_t*> streams;
1026 streams.setCapacity(config.num_streams);
1027
1028 if (mInputStream != NULL) {
1029 camera3_stream_t *inputStream;
1030 inputStream = mInputStream->startConfiguration();
1031 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001032 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 return INVALID_OPERATION;
1034 }
1035 streams.add(inputStream);
1036 }
1037
1038 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001039
1040 // Don't configure bidi streams twice, nor add them twice to the list
1041 if (mOutputStreams[i].get() ==
1042 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1043
1044 config.num_streams--;
1045 continue;
1046 }
1047
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001048 camera3_stream_t *outputStream;
1049 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1050 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001051 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001052 return INVALID_OPERATION;
1053 }
1054 streams.add(outputStream);
1055 }
1056
1057 config.streams = streams.editArray();
1058
1059 // Do the HAL configuration; will potentially touch stream
1060 // max_buffers, usage, priv fields.
1061
1062 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1063
1064 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1066 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001067 return res;
1068 }
1069
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001070 // Finish all stream configuration immediately.
1071 // TODO: Try to relax this later back to lazy completion, which should be
1072 // faster
1073
Igor Murashkin073f8572013-05-02 14:59:28 -07001074 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001075 res = mInputStream->finishConfiguration(mHal3Device);
1076 if (res != OK) {
1077 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1078 mInputStream->getId(), strerror(-res), res);
1079 return res;
1080 }
1081 }
1082
1083 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001084 sp<Camera3OutputStreamInterface> outputStream =
1085 mOutputStreams.editValueAt(i);
1086 if (outputStream->isConfiguring()) {
1087 res = outputStream->finishConfiguration(mHal3Device);
1088 if (res != OK) {
1089 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1090 outputStream->getId(), strerror(-res), res);
1091 return res;
1092 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001093 }
1094 }
1095
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 // Request thread needs to know to avoid using repeat-last-settings protocol
1097 // across configure_streams() calls
1098 mRequestThread->configurationComplete();
1099
1100 // Finish configuring the streams lazily on first reference
1101
1102 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001103 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001104
1105 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001106}
1107
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001108void Camera3Device::setErrorState(const char *fmt, ...) {
1109 Mutex::Autolock l(mLock);
1110 va_list args;
1111 va_start(args, fmt);
1112
1113 setErrorStateLockedV(fmt, args);
1114
1115 va_end(args);
1116}
1117
1118void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1119 Mutex::Autolock l(mLock);
1120 setErrorStateLockedV(fmt, args);
1121}
1122
1123void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1124 va_list args;
1125 va_start(args, fmt);
1126
1127 setErrorStateLockedV(fmt, args);
1128
1129 va_end(args);
1130}
1131
1132void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001133 // Print out all error messages to log
1134 String8 errorCause = String8::formatV(fmt, args);
1135 ALOGE("Camera %d: %s", mId, errorCause.string());
1136
1137 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001138 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001139
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001140 mErrorCause = errorCause;
1141
1142 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001143 mStatus = STATUS_ERROR;
1144}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001145
1146/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001147 * In-flight request management
1148 */
1149
1150status_t Camera3Device::registerInFlight(int32_t frameNumber,
1151 int32_t numBuffers) {
1152 ATRACE_CALL();
1153 Mutex::Autolock l(mInFlightLock);
1154
1155 ssize_t res;
1156 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1157 if (res < 0) return res;
1158
1159 return OK;
1160}
1161
1162/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001163 * Camera HAL device callback methods
1164 */
1165
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001166void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001167 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001168
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001169 status_t res;
1170
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001171 uint32_t frameNumber = result->frame_number;
1172 if (result->result == NULL && result->num_output_buffers == 0) {
1173 SET_ERR("No result data provided by HAL for frame %d",
1174 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001175 return;
1176 }
1177
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001178 // Get capture timestamp from list of in-flight requests, where it was added
1179 // by the shutter notification for this frame. Then update the in-flight
1180 // status and remove the in-flight entry if all result data has been
1181 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001182 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001183 {
1184 Mutex::Autolock l(mInFlightLock);
1185 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1186 if (idx == NAME_NOT_FOUND) {
1187 SET_ERR("Unknown frame number for capture result: %d",
1188 frameNumber);
1189 return;
1190 }
1191 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1192 timestamp = request.captureTimestamp;
1193 if (timestamp == 0) {
1194 SET_ERR("Called before shutter notify for frame %d",
1195 frameNumber);
1196 return;
1197 }
1198
1199 if (result->result != NULL) {
1200 if (request.haveResultMetadata) {
1201 SET_ERR("Called multiple times with metadata for frame %d",
1202 frameNumber);
1203 return;
1204 }
1205 request.haveResultMetadata = true;
1206 }
1207
1208 request.numBuffersLeft -= result->num_output_buffers;
1209
1210 if (request.numBuffersLeft < 0) {
1211 SET_ERR("Too many buffers returned for frame %d",
1212 frameNumber);
1213 return;
1214 }
1215
1216 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1217 mInFlightMap.removeItemsAt(idx, 1);
1218 }
1219
1220 // Sanity check - if we have too many in-flight frames, something has
1221 // likely gone wrong
1222 if (mInFlightMap.size() > kInFlightWarnLimit) {
1223 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1224 }
1225
1226 }
1227
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001228 AlgState cur3aState;
1229 AlgState new3aState;
1230 int32_t aeTriggerId = 0;
1231 int32_t afTriggerId = 0;
1232
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001233 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001234
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001235 // Process the result metadata, if provided
1236 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001237 Mutex::Autolock l(mOutputLock);
1238
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001239 if (frameNumber != mNextResultFrameNumber) {
1240 SET_ERR("Out-of-order capture result metadata submitted! "
1241 "(got frame number %d, expecting %d)",
1242 frameNumber, mNextResultFrameNumber);
1243 return;
1244 }
1245 mNextResultFrameNumber++;
1246
1247 CameraMetadata &captureResult =
1248 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001249
1250 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001251 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001252 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001253 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001254 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001255 } else {
1256 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001257 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001258 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001259
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001260 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001261
1262 camera_metadata_entry entry =
1263 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1264 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001265 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001266 frameNumber);
1267 }
1268 if (timestamp != entry.data.i64[0]) {
1269 SET_ERR("Timestamp mismatch between shutter notify and result"
1270 " metadata for frame %d (%lld vs %lld respectively)",
1271 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001272 }
1273
1274 // Get 3A states from result metadata
1275
1276 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1277 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001278 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001279 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001280 } else {
1281 new3aState.aeState =
1282 static_cast<camera_metadata_enum_android_control_ae_state>(
1283 entry.data.u8[0]);
1284 }
1285
1286 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1287 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001288 CLOGE("No AF state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001289 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001290 } else {
1291 new3aState.afState =
1292 static_cast<camera_metadata_enum_android_control_af_state>(
1293 entry.data.u8[0]);
1294 }
1295
1296 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1297 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001298 CLOGE("No AWB state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001299 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001300 } else {
1301 new3aState.awbState =
1302 static_cast<camera_metadata_enum_android_control_awb_state>(
1303 entry.data.u8[0]);
1304 }
1305
1306 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1307 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001308 CLOGE("No AF trigger ID provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001309 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001310 } else {
1311 afTriggerId = entry.data.i32[0];
1312 }
1313
1314 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1315 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001316 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001317 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001318 } else {
1319 aeTriggerId = entry.data.i32[0];
1320 }
1321
1322 listener = mListener;
1323 cur3aState = m3AState;
1324
1325 m3AState = new3aState;
1326 } // scope for mOutputLock
1327
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001328 // Return completed buffers to their streams with the timestamp
1329
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001330 for (size_t i = 0; i < result->num_output_buffers; i++) {
1331 Camera3Stream *stream =
1332 Camera3Stream::cast(result->output_buffers[i].stream);
1333 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1334 // Note: stream may be deallocated at this point, if this buffer was the
1335 // last reference to it.
1336 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001337 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001338 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001339 }
1340 }
1341
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001342 // Finally, dispatch any 3A change events to listeners if we got metadata
1343
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001344 if (result->result != NULL) {
1345 mResultSignal.signal();
1346 }
1347
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001348 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001349 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001350 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001351 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001352 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1353 }
1354 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001355 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001356 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001357 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1358 }
1359 if (new3aState.awbState != cur3aState.awbState) {
1360 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1361 }
1362 }
1363
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001364}
1365
1366void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001367 NotificationListener *listener;
1368 {
1369 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001370 listener = mListener;
1371 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001372
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001373 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001374 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001375 return;
1376 }
1377
1378 switch (msg->type) {
1379 case CAMERA3_MSG_ERROR: {
1380 int streamId = 0;
1381 if (msg->message.error.error_stream != NULL) {
1382 Camera3Stream *stream =
1383 Camera3Stream::cast(
1384 msg->message.error.error_stream);
1385 streamId = stream->getId();
1386 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001387 if (listener != NULL) {
1388 listener->notifyError(msg->message.error.error_code,
1389 msg->message.error.frame_number, streamId);
1390 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001391 break;
1392 }
1393 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001394 ssize_t idx;
1395 uint32_t frameNumber = msg->message.shutter.frame_number;
1396 nsecs_t timestamp = msg->message.shutter.timestamp;
1397 // Verify ordering of shutter notifications
1398 {
1399 Mutex::Autolock l(mOutputLock);
1400 if (frameNumber != mNextShutterFrameNumber) {
1401 SET_ERR("Shutter notification out-of-order. Expected "
1402 "notification for frame %d, got frame %d",
1403 mNextShutterFrameNumber, frameNumber);
1404 break;
1405 }
1406 mNextShutterFrameNumber++;
1407 }
1408
1409 // Set timestamp for the request in the in-flight tracking
1410 {
1411 Mutex::Autolock l(mInFlightLock);
1412 idx = mInFlightMap.indexOfKey(frameNumber);
1413 if (idx >= 0) {
1414 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1415 }
1416 }
1417 if (idx < 0) {
1418 SET_ERR("Shutter notification for non-existent frame number %d",
1419 frameNumber);
1420 break;
1421 }
1422
1423 // Call listener, if any
1424 if (listener != NULL) {
1425 listener->notifyShutter(frameNumber, timestamp);
1426 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001427 break;
1428 }
1429 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001430 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001431 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001432 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001433}
1434
1435/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436 * RequestThread inner class methods
1437 */
1438
1439Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1440 camera3_device_t *hal3Device) :
1441 Thread(false),
1442 mParent(parent),
1443 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001444 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445 mReconfigured(false),
1446 mDoPause(false),
1447 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001448 mFrameNumber(0),
1449 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001450}
1451
1452void Camera3Device::RequestThread::configurationComplete() {
1453 Mutex::Autolock l(mRequestLock);
1454 mReconfigured = true;
1455}
1456
1457status_t Camera3Device::RequestThread::queueRequest(
1458 sp<CaptureRequest> request) {
1459 Mutex::Autolock l(mRequestLock);
1460 mRequestQueue.push_back(request);
1461
1462 return OK;
1463}
1464
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001465
1466status_t Camera3Device::RequestThread::queueTrigger(
1467 RequestTrigger trigger[],
1468 size_t count) {
1469
1470 Mutex::Autolock l(mTriggerMutex);
1471 status_t ret;
1472
1473 for (size_t i = 0; i < count; ++i) {
1474 ret = queueTriggerLocked(trigger[i]);
1475
1476 if (ret != OK) {
1477 return ret;
1478 }
1479 }
1480
1481 return OK;
1482}
1483
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001484int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1485 sp<Camera3Device> d = device.promote();
1486 if (d != NULL) return d->mId;
1487 return 0;
1488}
1489
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001490status_t Camera3Device::RequestThread::queueTriggerLocked(
1491 RequestTrigger trigger) {
1492
1493 uint32_t tag = trigger.metadataTag;
1494 ssize_t index = mTriggerMap.indexOfKey(tag);
1495
1496 switch (trigger.getTagType()) {
1497 case TYPE_BYTE:
1498 // fall-through
1499 case TYPE_INT32:
1500 break;
1501 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001502 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1503 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001504 return INVALID_OPERATION;
1505 }
1506
1507 /**
1508 * Collect only the latest trigger, since we only have 1 field
1509 * in the request settings per trigger tag, and can't send more than 1
1510 * trigger per request.
1511 */
1512 if (index != NAME_NOT_FOUND) {
1513 mTriggerMap.editValueAt(index) = trigger;
1514 } else {
1515 mTriggerMap.add(tag, trigger);
1516 }
1517
1518 return OK;
1519}
1520
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001521status_t Camera3Device::RequestThread::setRepeatingRequests(
1522 const RequestList &requests) {
1523 Mutex::Autolock l(mRequestLock);
1524 mRepeatingRequests.clear();
1525 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1526 requests.begin(), requests.end());
1527 return OK;
1528}
1529
1530status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1531 Mutex::Autolock l(mRequestLock);
1532 mRepeatingRequests.clear();
1533 return OK;
1534}
1535
1536void Camera3Device::RequestThread::setPaused(bool paused) {
1537 Mutex::Autolock l(mPauseLock);
1538 mDoPause = paused;
1539 mDoPauseSignal.signal();
1540}
1541
1542status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1543 status_t res;
1544 Mutex::Autolock l(mPauseLock);
1545 while (!mPaused) {
1546 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1547 if (res == TIMED_OUT) {
1548 return res;
1549 }
1550 }
1551 return OK;
1552}
1553
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001554status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1555 int32_t requestId, nsecs_t timeout) {
1556 Mutex::Autolock l(mLatestRequestMutex);
1557 status_t res;
1558 while (mLatestRequestId != requestId) {
1559 nsecs_t startTime = systemTime();
1560
1561 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1562 if (res != OK) return res;
1563
1564 timeout -= (systemTime() - startTime);
1565 }
1566
1567 return OK;
1568}
1569
1570
1571
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001572bool Camera3Device::RequestThread::threadLoop() {
1573
1574 status_t res;
1575
1576 // Handle paused state.
1577 if (waitIfPaused()) {
1578 return true;
1579 }
1580
1581 // Get work to do
1582
1583 sp<CaptureRequest> nextRequest = waitForNextRequest();
1584 if (nextRequest == NULL) {
1585 return true;
1586 }
1587
1588 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001589 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001590 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001591
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001592 // Insert any queued triggers (before metadata is locked)
1593 int32_t triggerCount;
1594 res = insertTriggers(nextRequest);
1595 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001596 SET_ERR("RequestThread: Unable to insert triggers "
1597 "(capture request %d, HAL device: %s (%d)",
1598 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001599 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1600 return false;
1601 }
1602 triggerCount = res;
1603
1604 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1605
1606 // If the request is the same as last, or we had triggers last time
1607 if (mPrevRequest != nextRequest || triggersMixedIn) {
1608 /**
1609 * The request should be presorted so accesses in HAL
1610 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1611 */
1612 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001613 request.settings = nextRequest->mSettings.getAndLock();
1614 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001615 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1616
1617 IF_ALOGV() {
1618 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1619 find_camera_metadata_ro_entry(
1620 request.settings,
1621 ANDROID_CONTROL_AF_TRIGGER,
1622 &e
1623 );
1624 if (e.count > 0) {
1625 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1626 __FUNCTION__,
1627 mFrameNumber+1,
1628 e.data.u8[0]);
1629 }
1630 }
1631 } else {
1632 // leave request.settings NULL to indicate 'reuse latest given'
1633 ALOGVV("%s: Request settings are REUSED",
1634 __FUNCTION__);
1635 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001636
1637 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001638
1639 // Fill in buffers
1640
1641 if (nextRequest->mInputStream != NULL) {
1642 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001643 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001644 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001645 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646 " %s (%d)", strerror(-res), res);
1647 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1648 return true;
1649 }
1650 } else {
1651 request.input_buffer = NULL;
1652 }
1653
1654 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1655 nextRequest->mOutputStreams.size());
1656 request.output_buffers = outputBuffers.array();
1657 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1658 res = nextRequest->mOutputStreams.editItemAt(i)->
1659 getBuffer(&outputBuffers.editItemAt(i));
1660 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001661 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001662 "%s (%d)", strerror(-res), res);
1663 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1664 return true;
1665 }
1666 request.num_output_buffers++;
1667 }
1668
1669 request.frame_number = mFrameNumber++;
1670
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001671 // Log request in the in-flight queue
1672 sp<Camera3Device> parent = mParent.promote();
1673 if (parent == NULL) {
1674 CLOGE("RequestThread: Parent is gone");
1675 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1676 return false;
1677 }
1678
1679 res = parent->registerInFlight(request.frame_number,
1680 request.num_output_buffers);
1681 if (res != OK) {
1682 SET_ERR("RequestThread: Unable to register new in-flight request:"
1683 " %s (%d)", strerror(-res), res);
1684 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1685 return false;
1686 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001687
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001688 // Submit request and block until ready for next one
1689
1690 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1691 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001692 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001693 " device: %s (%d)", request.frame_number, strerror(-res), res);
1694 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1695 return false;
1696 }
1697
1698 if (request.settings != NULL) {
1699 nextRequest->mSettings.unlock(request.settings);
1700 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001701
1702 // Remove any previously queued triggers (after unlock)
1703 res = removeTriggers(mPrevRequest);
1704 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001705 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001706 "(capture request %d, HAL device: %s (%d)",
1707 request.frame_number, strerror(-res), res);
1708 return false;
1709 }
1710 mPrevTriggers = triggerCount;
1711
1712 // Read android.request.id from the request settings metadata
1713 // - inform waitUntilRequestProcessed thread of a new request ID
1714 {
1715 Mutex::Autolock al(mLatestRequestMutex);
1716
1717 camera_metadata_entry_t requestIdEntry =
1718 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1719 if (requestIdEntry.count > 0) {
1720 mLatestRequestId = requestIdEntry.data.i32[0];
1721 } else {
1722 ALOGW("%s: Did not have android.request.id set in the request",
1723 __FUNCTION__);
1724 mLatestRequestId = NAME_NOT_FOUND;
1725 }
1726
1727 mLatestRequestSignal.signal();
1728 }
1729
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001730 // Return input buffer back to framework
1731 if (request.input_buffer != NULL) {
1732 Camera3Stream *stream =
1733 Camera3Stream::cast(request.input_buffer->stream);
1734 res = stream->returnInputBuffer(*(request.input_buffer));
1735 // Note: stream may be deallocated at this point, if this buffer was the
1736 // last reference to it.
1737 if (res != OK) {
1738 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1739 " its stream:%s (%d)", __FUNCTION__,
1740 request.frame_number, strerror(-res), res);
1741 // TODO: Report error upstream
1742 }
1743 }
1744
1745
1746
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001747 return true;
1748}
1749
1750void Camera3Device::RequestThread::cleanUpFailedRequest(
1751 camera3_capture_request_t &request,
1752 sp<CaptureRequest> &nextRequest,
1753 Vector<camera3_stream_buffer_t> &outputBuffers) {
1754
1755 if (request.settings != NULL) {
1756 nextRequest->mSettings.unlock(request.settings);
1757 }
1758 if (request.input_buffer != NULL) {
1759 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001760 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001761 }
1762 for (size_t i = 0; i < request.num_output_buffers; i++) {
1763 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1764 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1765 outputBuffers[i], 0);
1766 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001767}
1768
1769sp<Camera3Device::CaptureRequest>
1770 Camera3Device::RequestThread::waitForNextRequest() {
1771 status_t res;
1772 sp<CaptureRequest> nextRequest;
1773
1774 // Optimized a bit for the simple steady-state case (single repeating
1775 // request), to avoid putting that request in the queue temporarily.
1776 Mutex::Autolock l(mRequestLock);
1777
1778 while (mRequestQueue.empty()) {
1779 if (!mRepeatingRequests.empty()) {
1780 // Always atomically enqueue all requests in a repeating request
1781 // list. Guarantees a complete in-sequence set of captures to
1782 // application.
1783 const RequestList &requests = mRepeatingRequests;
1784 RequestList::const_iterator firstRequest =
1785 requests.begin();
1786 nextRequest = *firstRequest;
1787 mRequestQueue.insert(mRequestQueue.end(),
1788 ++firstRequest,
1789 requests.end());
1790 // No need to wait any longer
1791 break;
1792 }
1793
1794 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1795
1796 if (res == TIMED_OUT) {
1797 // Signal that we're paused by starvation
1798 Mutex::Autolock pl(mPauseLock);
1799 if (mPaused == false) {
1800 mPaused = true;
1801 mPausedSignal.signal();
1802 }
1803 // Stop waiting for now and let thread management happen
1804 return NULL;
1805 }
1806 }
1807
1808 if (nextRequest == NULL) {
1809 // Don't have a repeating request already in hand, so queue
1810 // must have an entry now.
1811 RequestList::iterator firstRequest =
1812 mRequestQueue.begin();
1813 nextRequest = *firstRequest;
1814 mRequestQueue.erase(firstRequest);
1815 }
1816
1817 // Not paused
1818 Mutex::Autolock pl(mPauseLock);
1819 mPaused = false;
1820
1821 // Check if we've reconfigured since last time, and reset the preview
1822 // request if so. Can't use 'NULL request == repeat' across configure calls.
1823 if (mReconfigured) {
1824 mPrevRequest.clear();
1825 mReconfigured = false;
1826 }
1827
1828 return nextRequest;
1829}
1830
1831bool Camera3Device::RequestThread::waitIfPaused() {
1832 status_t res;
1833 Mutex::Autolock l(mPauseLock);
1834 while (mDoPause) {
1835 // Signal that we're paused by request
1836 if (mPaused == false) {
1837 mPaused = true;
1838 mPausedSignal.signal();
1839 }
1840 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1841 if (res == TIMED_OUT) {
1842 return true;
1843 }
1844 }
1845 // We don't set mPaused to false here, because waitForNextRequest needs
1846 // to further manage the paused state in case of starvation.
1847 return false;
1848}
1849
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001850void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1851 sp<Camera3Device> parent = mParent.promote();
1852 if (parent != NULL) {
1853 va_list args;
1854 va_start(args, fmt);
1855
1856 parent->setErrorStateV(fmt, args);
1857
1858 va_end(args);
1859 }
1860}
1861
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001862status_t Camera3Device::RequestThread::insertTriggers(
1863 const sp<CaptureRequest> &request) {
1864
1865 Mutex::Autolock al(mTriggerMutex);
1866
1867 CameraMetadata &metadata = request->mSettings;
1868 size_t count = mTriggerMap.size();
1869
1870 for (size_t i = 0; i < count; ++i) {
1871 RequestTrigger trigger = mTriggerMap.valueAt(i);
1872
1873 uint32_t tag = trigger.metadataTag;
1874 camera_metadata_entry entry = metadata.find(tag);
1875
1876 if (entry.count > 0) {
1877 /**
1878 * Already has an entry for this trigger in the request.
1879 * Rewrite it with our requested trigger value.
1880 */
1881 RequestTrigger oldTrigger = trigger;
1882
1883 oldTrigger.entryValue = entry.data.u8[0];
1884
1885 mTriggerReplacedMap.add(tag, oldTrigger);
1886 } else {
1887 /**
1888 * More typical, no trigger entry, so we just add it
1889 */
1890 mTriggerRemovedMap.add(tag, trigger);
1891 }
1892
1893 status_t res;
1894
1895 switch (trigger.getTagType()) {
1896 case TYPE_BYTE: {
1897 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1898 res = metadata.update(tag,
1899 &entryValue,
1900 /*count*/1);
1901 break;
1902 }
1903 case TYPE_INT32:
1904 res = metadata.update(tag,
1905 &trigger.entryValue,
1906 /*count*/1);
1907 break;
1908 default:
1909 ALOGE("%s: Type not supported: 0x%x",
1910 __FUNCTION__,
1911 trigger.getTagType());
1912 return INVALID_OPERATION;
1913 }
1914
1915 if (res != OK) {
1916 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1917 ", value %d", __FUNCTION__, trigger.getTagName(),
1918 trigger.entryValue);
1919 return res;
1920 }
1921
1922 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1923 trigger.getTagName(),
1924 trigger.entryValue);
1925 }
1926
1927 mTriggerMap.clear();
1928
1929 return count;
1930}
1931
1932status_t Camera3Device::RequestThread::removeTriggers(
1933 const sp<CaptureRequest> &request) {
1934 Mutex::Autolock al(mTriggerMutex);
1935
1936 CameraMetadata &metadata = request->mSettings;
1937
1938 /**
1939 * Replace all old entries with their old values.
1940 */
1941 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1942 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1943
1944 status_t res;
1945
1946 uint32_t tag = trigger.metadataTag;
1947 switch (trigger.getTagType()) {
1948 case TYPE_BYTE: {
1949 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1950 res = metadata.update(tag,
1951 &entryValue,
1952 /*count*/1);
1953 break;
1954 }
1955 case TYPE_INT32:
1956 res = metadata.update(tag,
1957 &trigger.entryValue,
1958 /*count*/1);
1959 break;
1960 default:
1961 ALOGE("%s: Type not supported: 0x%x",
1962 __FUNCTION__,
1963 trigger.getTagType());
1964 return INVALID_OPERATION;
1965 }
1966
1967 if (res != OK) {
1968 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1969 ", trigger value %d", __FUNCTION__,
1970 trigger.getTagName(), trigger.entryValue);
1971 return res;
1972 }
1973 }
1974 mTriggerReplacedMap.clear();
1975
1976 /**
1977 * Remove all new entries.
1978 */
1979 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1980 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1981 status_t res = metadata.erase(trigger.metadataTag);
1982
1983 if (res != OK) {
1984 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1985 ", trigger value %d", __FUNCTION__,
1986 trigger.getTagName(), trigger.entryValue);
1987 return res;
1988 }
1989 }
1990 mTriggerRemovedMap.clear();
1991
1992 return OK;
1993}
1994
1995
1996
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001997/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001998 * Static callback forwarding methods from HAL to instance
1999 */
2000
2001void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2002 const camera3_capture_result *result) {
2003 Camera3Device *d =
2004 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2005 d->processCaptureResult(result);
2006}
2007
2008void Camera3Device::sNotify(const camera3_callback_ops *cb,
2009 const camera3_notify_msg *msg) {
2010 Camera3Device *d =
2011 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2012 d->notify(msg);
2013}
2014
2015}; // namespace android