blob: bc4db91e06e7154e4bb12dc30b88be6ce640a9fc [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__);
1015 return OK;
1016 }
1017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 // Start configuring the streams
1019
1020 camera3_stream_configuration config;
1021
1022 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1023
1024 Vector<camera3_stream_t*> streams;
1025 streams.setCapacity(config.num_streams);
1026
1027 if (mInputStream != NULL) {
1028 camera3_stream_t *inputStream;
1029 inputStream = mInputStream->startConfiguration();
1030 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001031 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001032 return INVALID_OPERATION;
1033 }
1034 streams.add(inputStream);
1035 }
1036
1037 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001038
1039 // Don't configure bidi streams twice, nor add them twice to the list
1040 if (mOutputStreams[i].get() ==
1041 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1042
1043 config.num_streams--;
1044 continue;
1045 }
1046
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 camera3_stream_t *outputStream;
1048 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1049 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001050 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 return INVALID_OPERATION;
1052 }
1053 streams.add(outputStream);
1054 }
1055
1056 config.streams = streams.editArray();
1057
1058 // Do the HAL configuration; will potentially touch stream
1059 // max_buffers, usage, priv fields.
1060
1061 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1062
1063 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001064 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1065 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 return res;
1067 }
1068
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001069 // Finish all stream configuration immediately.
1070 // TODO: Try to relax this later back to lazy completion, which should be
1071 // faster
1072
Igor Murashkin073f8572013-05-02 14:59:28 -07001073 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001074 res = mInputStream->finishConfiguration(mHal3Device);
1075 if (res != OK) {
1076 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1077 mInputStream->getId(), strerror(-res), res);
1078 return res;
1079 }
1080 }
1081
1082 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001083 sp<Camera3OutputStreamInterface> outputStream =
1084 mOutputStreams.editValueAt(i);
1085 if (outputStream->isConfiguring()) {
1086 res = outputStream->finishConfiguration(mHal3Device);
1087 if (res != OK) {
1088 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1089 outputStream->getId(), strerror(-res), res);
1090 return res;
1091 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001092 }
1093 }
1094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001095 // Request thread needs to know to avoid using repeat-last-settings protocol
1096 // across configure_streams() calls
1097 mRequestThread->configurationComplete();
1098
1099 // Finish configuring the streams lazily on first reference
1100
1101 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001102 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001103
1104 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001105}
1106
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001107void Camera3Device::setErrorState(const char *fmt, ...) {
1108 Mutex::Autolock l(mLock);
1109 va_list args;
1110 va_start(args, fmt);
1111
1112 setErrorStateLockedV(fmt, args);
1113
1114 va_end(args);
1115}
1116
1117void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1118 Mutex::Autolock l(mLock);
1119 setErrorStateLockedV(fmt, args);
1120}
1121
1122void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1123 va_list args;
1124 va_start(args, fmt);
1125
1126 setErrorStateLockedV(fmt, args);
1127
1128 va_end(args);
1129}
1130
1131void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001132 // Print out all error messages to log
1133 String8 errorCause = String8::formatV(fmt, args);
1134 ALOGE("Camera %d: %s", mId, errorCause.string());
1135
1136 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001137 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001138
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001139 mErrorCause = errorCause;
1140
1141 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001142 mStatus = STATUS_ERROR;
1143}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001144
1145/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001146 * In-flight request management
1147 */
1148
1149status_t Camera3Device::registerInFlight(int32_t frameNumber,
1150 int32_t numBuffers) {
1151 ATRACE_CALL();
1152 Mutex::Autolock l(mInFlightLock);
1153
1154 ssize_t res;
1155 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1156 if (res < 0) return res;
1157
1158 return OK;
1159}
1160
1161/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001162 * Camera HAL device callback methods
1163 */
1164
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001165void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001166 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001167
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001168 status_t res;
1169
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001170 uint32_t frameNumber = result->frame_number;
1171 if (result->result == NULL && result->num_output_buffers == 0) {
1172 SET_ERR("No result data provided by HAL for frame %d",
1173 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001174 return;
1175 }
1176
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001177 // Get capture timestamp from list of in-flight requests, where it was added
1178 // by the shutter notification for this frame. Then update the in-flight
1179 // status and remove the in-flight entry if all result data has been
1180 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001181 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001182 {
1183 Mutex::Autolock l(mInFlightLock);
1184 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1185 if (idx == NAME_NOT_FOUND) {
1186 SET_ERR("Unknown frame number for capture result: %d",
1187 frameNumber);
1188 return;
1189 }
1190 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1191 timestamp = request.captureTimestamp;
1192 if (timestamp == 0) {
1193 SET_ERR("Called before shutter notify for frame %d",
1194 frameNumber);
1195 return;
1196 }
1197
1198 if (result->result != NULL) {
1199 if (request.haveResultMetadata) {
1200 SET_ERR("Called multiple times with metadata for frame %d",
1201 frameNumber);
1202 return;
1203 }
1204 request.haveResultMetadata = true;
1205 }
1206
1207 request.numBuffersLeft -= result->num_output_buffers;
1208
1209 if (request.numBuffersLeft < 0) {
1210 SET_ERR("Too many buffers returned for frame %d",
1211 frameNumber);
1212 return;
1213 }
1214
1215 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1216 mInFlightMap.removeItemsAt(idx, 1);
1217 }
1218
1219 // Sanity check - if we have too many in-flight frames, something has
1220 // likely gone wrong
1221 if (mInFlightMap.size() > kInFlightWarnLimit) {
1222 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1223 }
1224
1225 }
1226
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001227 AlgState cur3aState;
1228 AlgState new3aState;
1229 int32_t aeTriggerId = 0;
1230 int32_t afTriggerId = 0;
1231
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001232 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001233
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001234 // Process the result metadata, if provided
1235 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001236 Mutex::Autolock l(mOutputLock);
1237
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001238 if (frameNumber != mNextResultFrameNumber) {
1239 SET_ERR("Out-of-order capture result metadata submitted! "
1240 "(got frame number %d, expecting %d)",
1241 frameNumber, mNextResultFrameNumber);
1242 return;
1243 }
1244 mNextResultFrameNumber++;
1245
1246 CameraMetadata &captureResult =
1247 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001248
1249 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001250 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001251 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001252 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001253 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001254 } else {
1255 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001256 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001257 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001258
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001259 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001260
1261 camera_metadata_entry entry =
1262 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1263 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001264 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001265 frameNumber);
1266 }
1267 if (timestamp != entry.data.i64[0]) {
1268 SET_ERR("Timestamp mismatch between shutter notify and result"
1269 " metadata for frame %d (%lld vs %lld respectively)",
1270 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001271 }
1272
1273 // Get 3A states from result metadata
1274
1275 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1276 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001278 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001279 } else {
1280 new3aState.aeState =
1281 static_cast<camera_metadata_enum_android_control_ae_state>(
1282 entry.data.u8[0]);
1283 }
1284
1285 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1286 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001287 CLOGE("No AF state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001288 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001289 } else {
1290 new3aState.afState =
1291 static_cast<camera_metadata_enum_android_control_af_state>(
1292 entry.data.u8[0]);
1293 }
1294
1295 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1296 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001297 CLOGE("No AWB state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001298 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001299 } else {
1300 new3aState.awbState =
1301 static_cast<camera_metadata_enum_android_control_awb_state>(
1302 entry.data.u8[0]);
1303 }
1304
1305 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1306 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001307 CLOGE("No AF trigger ID provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001308 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001309 } else {
1310 afTriggerId = entry.data.i32[0];
1311 }
1312
1313 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1314 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001315 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001316 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001317 } else {
1318 aeTriggerId = entry.data.i32[0];
1319 }
1320
1321 listener = mListener;
1322 cur3aState = m3AState;
1323
1324 m3AState = new3aState;
1325 } // scope for mOutputLock
1326
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001327 // Return completed buffers to their streams with the timestamp
1328
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001329 for (size_t i = 0; i < result->num_output_buffers; i++) {
1330 Camera3Stream *stream =
1331 Camera3Stream::cast(result->output_buffers[i].stream);
1332 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1333 // Note: stream may be deallocated at this point, if this buffer was the
1334 // last reference to it.
1335 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001336 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001337 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001338 }
1339 }
1340
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001341 // Finally, dispatch any 3A change events to listeners if we got metadata
1342
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001343 if (result->result != NULL) {
1344 mResultSignal.signal();
1345 }
1346
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001347 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001348 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001349 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001350 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001351 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1352 }
1353 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001354 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001355 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001356 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1357 }
1358 if (new3aState.awbState != cur3aState.awbState) {
1359 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1360 }
1361 }
1362
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001363}
1364
1365void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001366 NotificationListener *listener;
1367 {
1368 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001369 listener = mListener;
1370 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001371
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001372 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001373 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001374 return;
1375 }
1376
1377 switch (msg->type) {
1378 case CAMERA3_MSG_ERROR: {
1379 int streamId = 0;
1380 if (msg->message.error.error_stream != NULL) {
1381 Camera3Stream *stream =
1382 Camera3Stream::cast(
1383 msg->message.error.error_stream);
1384 streamId = stream->getId();
1385 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001386 if (listener != NULL) {
1387 listener->notifyError(msg->message.error.error_code,
1388 msg->message.error.frame_number, streamId);
1389 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001390 break;
1391 }
1392 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001393 ssize_t idx;
1394 uint32_t frameNumber = msg->message.shutter.frame_number;
1395 nsecs_t timestamp = msg->message.shutter.timestamp;
1396 // Verify ordering of shutter notifications
1397 {
1398 Mutex::Autolock l(mOutputLock);
1399 if (frameNumber != mNextShutterFrameNumber) {
1400 SET_ERR("Shutter notification out-of-order. Expected "
1401 "notification for frame %d, got frame %d",
1402 mNextShutterFrameNumber, frameNumber);
1403 break;
1404 }
1405 mNextShutterFrameNumber++;
1406 }
1407
1408 // Set timestamp for the request in the in-flight tracking
1409 {
1410 Mutex::Autolock l(mInFlightLock);
1411 idx = mInFlightMap.indexOfKey(frameNumber);
1412 if (idx >= 0) {
1413 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1414 }
1415 }
1416 if (idx < 0) {
1417 SET_ERR("Shutter notification for non-existent frame number %d",
1418 frameNumber);
1419 break;
1420 }
1421
1422 // Call listener, if any
1423 if (listener != NULL) {
1424 listener->notifyShutter(frameNumber, timestamp);
1425 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001426 break;
1427 }
1428 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001429 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001430 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001431 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001432}
1433
1434/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435 * RequestThread inner class methods
1436 */
1437
1438Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1439 camera3_device_t *hal3Device) :
1440 Thread(false),
1441 mParent(parent),
1442 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001443 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 mReconfigured(false),
1445 mDoPause(false),
1446 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001447 mFrameNumber(0),
1448 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001449}
1450
1451void Camera3Device::RequestThread::configurationComplete() {
1452 Mutex::Autolock l(mRequestLock);
1453 mReconfigured = true;
1454}
1455
1456status_t Camera3Device::RequestThread::queueRequest(
1457 sp<CaptureRequest> request) {
1458 Mutex::Autolock l(mRequestLock);
1459 mRequestQueue.push_back(request);
1460
1461 return OK;
1462}
1463
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001464
1465status_t Camera3Device::RequestThread::queueTrigger(
1466 RequestTrigger trigger[],
1467 size_t count) {
1468
1469 Mutex::Autolock l(mTriggerMutex);
1470 status_t ret;
1471
1472 for (size_t i = 0; i < count; ++i) {
1473 ret = queueTriggerLocked(trigger[i]);
1474
1475 if (ret != OK) {
1476 return ret;
1477 }
1478 }
1479
1480 return OK;
1481}
1482
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001483int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1484 sp<Camera3Device> d = device.promote();
1485 if (d != NULL) return d->mId;
1486 return 0;
1487}
1488
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001489status_t Camera3Device::RequestThread::queueTriggerLocked(
1490 RequestTrigger trigger) {
1491
1492 uint32_t tag = trigger.metadataTag;
1493 ssize_t index = mTriggerMap.indexOfKey(tag);
1494
1495 switch (trigger.getTagType()) {
1496 case TYPE_BYTE:
1497 // fall-through
1498 case TYPE_INT32:
1499 break;
1500 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001501 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1502 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001503 return INVALID_OPERATION;
1504 }
1505
1506 /**
1507 * Collect only the latest trigger, since we only have 1 field
1508 * in the request settings per trigger tag, and can't send more than 1
1509 * trigger per request.
1510 */
1511 if (index != NAME_NOT_FOUND) {
1512 mTriggerMap.editValueAt(index) = trigger;
1513 } else {
1514 mTriggerMap.add(tag, trigger);
1515 }
1516
1517 return OK;
1518}
1519
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001520status_t Camera3Device::RequestThread::setRepeatingRequests(
1521 const RequestList &requests) {
1522 Mutex::Autolock l(mRequestLock);
1523 mRepeatingRequests.clear();
1524 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1525 requests.begin(), requests.end());
1526 return OK;
1527}
1528
1529status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1530 Mutex::Autolock l(mRequestLock);
1531 mRepeatingRequests.clear();
1532 return OK;
1533}
1534
1535void Camera3Device::RequestThread::setPaused(bool paused) {
1536 Mutex::Autolock l(mPauseLock);
1537 mDoPause = paused;
1538 mDoPauseSignal.signal();
1539}
1540
1541status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1542 status_t res;
1543 Mutex::Autolock l(mPauseLock);
1544 while (!mPaused) {
1545 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1546 if (res == TIMED_OUT) {
1547 return res;
1548 }
1549 }
1550 return OK;
1551}
1552
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001553status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1554 int32_t requestId, nsecs_t timeout) {
1555 Mutex::Autolock l(mLatestRequestMutex);
1556 status_t res;
1557 while (mLatestRequestId != requestId) {
1558 nsecs_t startTime = systemTime();
1559
1560 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1561 if (res != OK) return res;
1562
1563 timeout -= (systemTime() - startTime);
1564 }
1565
1566 return OK;
1567}
1568
1569
1570
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571bool Camera3Device::RequestThread::threadLoop() {
1572
1573 status_t res;
1574
1575 // Handle paused state.
1576 if (waitIfPaused()) {
1577 return true;
1578 }
1579
1580 // Get work to do
1581
1582 sp<CaptureRequest> nextRequest = waitForNextRequest();
1583 if (nextRequest == NULL) {
1584 return true;
1585 }
1586
1587 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001589 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001591 // Insert any queued triggers (before metadata is locked)
1592 int32_t triggerCount;
1593 res = insertTriggers(nextRequest);
1594 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001595 SET_ERR("RequestThread: Unable to insert triggers "
1596 "(capture request %d, HAL device: %s (%d)",
1597 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001598 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1599 return false;
1600 }
1601 triggerCount = res;
1602
1603 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1604
1605 // If the request is the same as last, or we had triggers last time
1606 if (mPrevRequest != nextRequest || triggersMixedIn) {
1607 /**
1608 * The request should be presorted so accesses in HAL
1609 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1610 */
1611 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001612 request.settings = nextRequest->mSettings.getAndLock();
1613 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001614 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1615
1616 IF_ALOGV() {
1617 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1618 find_camera_metadata_ro_entry(
1619 request.settings,
1620 ANDROID_CONTROL_AF_TRIGGER,
1621 &e
1622 );
1623 if (e.count > 0) {
1624 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1625 __FUNCTION__,
1626 mFrameNumber+1,
1627 e.data.u8[0]);
1628 }
1629 }
1630 } else {
1631 // leave request.settings NULL to indicate 'reuse latest given'
1632 ALOGVV("%s: Request settings are REUSED",
1633 __FUNCTION__);
1634 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001635
1636 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001637
1638 // Fill in buffers
1639
1640 if (nextRequest->mInputStream != NULL) {
1641 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001642 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001643 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001644 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001645 " %s (%d)", strerror(-res), res);
1646 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1647 return true;
1648 }
1649 } else {
1650 request.input_buffer = NULL;
1651 }
1652
1653 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1654 nextRequest->mOutputStreams.size());
1655 request.output_buffers = outputBuffers.array();
1656 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1657 res = nextRequest->mOutputStreams.editItemAt(i)->
1658 getBuffer(&outputBuffers.editItemAt(i));
1659 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001660 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001661 "%s (%d)", strerror(-res), res);
1662 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1663 return true;
1664 }
1665 request.num_output_buffers++;
1666 }
1667
1668 request.frame_number = mFrameNumber++;
1669
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001670 // Log request in the in-flight queue
1671 sp<Camera3Device> parent = mParent.promote();
1672 if (parent == NULL) {
1673 CLOGE("RequestThread: Parent is gone");
1674 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1675 return false;
1676 }
1677
1678 res = parent->registerInFlight(request.frame_number,
1679 request.num_output_buffers);
1680 if (res != OK) {
1681 SET_ERR("RequestThread: Unable to register new in-flight request:"
1682 " %s (%d)", strerror(-res), res);
1683 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1684 return false;
1685 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001686
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001687 // Submit request and block until ready for next one
1688
1689 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1690 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001691 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001692 " device: %s (%d)", request.frame_number, strerror(-res), res);
1693 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1694 return false;
1695 }
1696
1697 if (request.settings != NULL) {
1698 nextRequest->mSettings.unlock(request.settings);
1699 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001700
1701 // Remove any previously queued triggers (after unlock)
1702 res = removeTriggers(mPrevRequest);
1703 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001704 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001705 "(capture request %d, HAL device: %s (%d)",
1706 request.frame_number, strerror(-res), res);
1707 return false;
1708 }
1709 mPrevTriggers = triggerCount;
1710
1711 // Read android.request.id from the request settings metadata
1712 // - inform waitUntilRequestProcessed thread of a new request ID
1713 {
1714 Mutex::Autolock al(mLatestRequestMutex);
1715
1716 camera_metadata_entry_t requestIdEntry =
1717 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1718 if (requestIdEntry.count > 0) {
1719 mLatestRequestId = requestIdEntry.data.i32[0];
1720 } else {
1721 ALOGW("%s: Did not have android.request.id set in the request",
1722 __FUNCTION__);
1723 mLatestRequestId = NAME_NOT_FOUND;
1724 }
1725
1726 mLatestRequestSignal.signal();
1727 }
1728
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001729 // Return input buffer back to framework
1730 if (request.input_buffer != NULL) {
1731 Camera3Stream *stream =
1732 Camera3Stream::cast(request.input_buffer->stream);
1733 res = stream->returnInputBuffer(*(request.input_buffer));
1734 // Note: stream may be deallocated at this point, if this buffer was the
1735 // last reference to it.
1736 if (res != OK) {
1737 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1738 " its stream:%s (%d)", __FUNCTION__,
1739 request.frame_number, strerror(-res), res);
1740 // TODO: Report error upstream
1741 }
1742 }
1743
1744
1745
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001746 return true;
1747}
1748
1749void Camera3Device::RequestThread::cleanUpFailedRequest(
1750 camera3_capture_request_t &request,
1751 sp<CaptureRequest> &nextRequest,
1752 Vector<camera3_stream_buffer_t> &outputBuffers) {
1753
1754 if (request.settings != NULL) {
1755 nextRequest->mSettings.unlock(request.settings);
1756 }
1757 if (request.input_buffer != NULL) {
1758 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001759 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760 }
1761 for (size_t i = 0; i < request.num_output_buffers; i++) {
1762 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1763 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1764 outputBuffers[i], 0);
1765 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001766}
1767
1768sp<Camera3Device::CaptureRequest>
1769 Camera3Device::RequestThread::waitForNextRequest() {
1770 status_t res;
1771 sp<CaptureRequest> nextRequest;
1772
1773 // Optimized a bit for the simple steady-state case (single repeating
1774 // request), to avoid putting that request in the queue temporarily.
1775 Mutex::Autolock l(mRequestLock);
1776
1777 while (mRequestQueue.empty()) {
1778 if (!mRepeatingRequests.empty()) {
1779 // Always atomically enqueue all requests in a repeating request
1780 // list. Guarantees a complete in-sequence set of captures to
1781 // application.
1782 const RequestList &requests = mRepeatingRequests;
1783 RequestList::const_iterator firstRequest =
1784 requests.begin();
1785 nextRequest = *firstRequest;
1786 mRequestQueue.insert(mRequestQueue.end(),
1787 ++firstRequest,
1788 requests.end());
1789 // No need to wait any longer
1790 break;
1791 }
1792
1793 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1794
1795 if (res == TIMED_OUT) {
1796 // Signal that we're paused by starvation
1797 Mutex::Autolock pl(mPauseLock);
1798 if (mPaused == false) {
1799 mPaused = true;
1800 mPausedSignal.signal();
1801 }
1802 // Stop waiting for now and let thread management happen
1803 return NULL;
1804 }
1805 }
1806
1807 if (nextRequest == NULL) {
1808 // Don't have a repeating request already in hand, so queue
1809 // must have an entry now.
1810 RequestList::iterator firstRequest =
1811 mRequestQueue.begin();
1812 nextRequest = *firstRequest;
1813 mRequestQueue.erase(firstRequest);
1814 }
1815
1816 // Not paused
1817 Mutex::Autolock pl(mPauseLock);
1818 mPaused = false;
1819
1820 // Check if we've reconfigured since last time, and reset the preview
1821 // request if so. Can't use 'NULL request == repeat' across configure calls.
1822 if (mReconfigured) {
1823 mPrevRequest.clear();
1824 mReconfigured = false;
1825 }
1826
1827 return nextRequest;
1828}
1829
1830bool Camera3Device::RequestThread::waitIfPaused() {
1831 status_t res;
1832 Mutex::Autolock l(mPauseLock);
1833 while (mDoPause) {
1834 // Signal that we're paused by request
1835 if (mPaused == false) {
1836 mPaused = true;
1837 mPausedSignal.signal();
1838 }
1839 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1840 if (res == TIMED_OUT) {
1841 return true;
1842 }
1843 }
1844 // We don't set mPaused to false here, because waitForNextRequest needs
1845 // to further manage the paused state in case of starvation.
1846 return false;
1847}
1848
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001849void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1850 sp<Camera3Device> parent = mParent.promote();
1851 if (parent != NULL) {
1852 va_list args;
1853 va_start(args, fmt);
1854
1855 parent->setErrorStateV(fmt, args);
1856
1857 va_end(args);
1858 }
1859}
1860
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001861status_t Camera3Device::RequestThread::insertTriggers(
1862 const sp<CaptureRequest> &request) {
1863
1864 Mutex::Autolock al(mTriggerMutex);
1865
1866 CameraMetadata &metadata = request->mSettings;
1867 size_t count = mTriggerMap.size();
1868
1869 for (size_t i = 0; i < count; ++i) {
1870 RequestTrigger trigger = mTriggerMap.valueAt(i);
1871
1872 uint32_t tag = trigger.metadataTag;
1873 camera_metadata_entry entry = metadata.find(tag);
1874
1875 if (entry.count > 0) {
1876 /**
1877 * Already has an entry for this trigger in the request.
1878 * Rewrite it with our requested trigger value.
1879 */
1880 RequestTrigger oldTrigger = trigger;
1881
1882 oldTrigger.entryValue = entry.data.u8[0];
1883
1884 mTriggerReplacedMap.add(tag, oldTrigger);
1885 } else {
1886 /**
1887 * More typical, no trigger entry, so we just add it
1888 */
1889 mTriggerRemovedMap.add(tag, trigger);
1890 }
1891
1892 status_t res;
1893
1894 switch (trigger.getTagType()) {
1895 case TYPE_BYTE: {
1896 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1897 res = metadata.update(tag,
1898 &entryValue,
1899 /*count*/1);
1900 break;
1901 }
1902 case TYPE_INT32:
1903 res = metadata.update(tag,
1904 &trigger.entryValue,
1905 /*count*/1);
1906 break;
1907 default:
1908 ALOGE("%s: Type not supported: 0x%x",
1909 __FUNCTION__,
1910 trigger.getTagType());
1911 return INVALID_OPERATION;
1912 }
1913
1914 if (res != OK) {
1915 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1916 ", value %d", __FUNCTION__, trigger.getTagName(),
1917 trigger.entryValue);
1918 return res;
1919 }
1920
1921 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1922 trigger.getTagName(),
1923 trigger.entryValue);
1924 }
1925
1926 mTriggerMap.clear();
1927
1928 return count;
1929}
1930
1931status_t Camera3Device::RequestThread::removeTriggers(
1932 const sp<CaptureRequest> &request) {
1933 Mutex::Autolock al(mTriggerMutex);
1934
1935 CameraMetadata &metadata = request->mSettings;
1936
1937 /**
1938 * Replace all old entries with their old values.
1939 */
1940 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1941 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1942
1943 status_t res;
1944
1945 uint32_t tag = trigger.metadataTag;
1946 switch (trigger.getTagType()) {
1947 case TYPE_BYTE: {
1948 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1949 res = metadata.update(tag,
1950 &entryValue,
1951 /*count*/1);
1952 break;
1953 }
1954 case TYPE_INT32:
1955 res = metadata.update(tag,
1956 &trigger.entryValue,
1957 /*count*/1);
1958 break;
1959 default:
1960 ALOGE("%s: Type not supported: 0x%x",
1961 __FUNCTION__,
1962 trigger.getTagType());
1963 return INVALID_OPERATION;
1964 }
1965
1966 if (res != OK) {
1967 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1968 ", trigger value %d", __FUNCTION__,
1969 trigger.getTagName(), trigger.entryValue);
1970 return res;
1971 }
1972 }
1973 mTriggerReplacedMap.clear();
1974
1975 /**
1976 * Remove all new entries.
1977 */
1978 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1979 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1980 status_t res = metadata.erase(trigger.metadataTag);
1981
1982 if (res != OK) {
1983 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1984 ", trigger value %d", __FUNCTION__,
1985 trigger.getTagName(), trigger.entryValue);
1986 return res;
1987 }
1988 }
1989 mTriggerRemovedMap.clear();
1990
1991 return OK;
1992}
1993
1994
1995
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001996/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001997 * Static callback forwarding methods from HAL to instance
1998 */
1999
2000void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2001 const camera3_capture_result *result) {
2002 Camera3Device *d =
2003 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2004 d->processCaptureResult(result);
2005}
2006
2007void Camera3Device::sNotify(const camera3_callback_ops *cb,
2008 const camera3_notify_msg *msg) {
2009 Camera3Device *d =
2010 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2011 d->notify(msg);
2012}
2013
2014}; // namespace android