blob: 0b5e9c4ce6dba0e8f4d83d9f8bb2feb1df22ceb4 [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 Talvala7fa43f32013-02-06 17:20:07 -0800173
174 return OK;
175}
176
177status_t Camera3Device::disconnect() {
178 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800179 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800180
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800181 ALOGV("%s: E", __FUNCTION__);
182
183 status_t res;
184 if (mStatus == STATUS_UNINITIALIZED) return OK;
185
186 if (mStatus == STATUS_ACTIVE ||
187 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
188 res = mRequestThread->clearRepeatingRequests();
189 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700190 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 return res;
192 }
193 res = waitUntilDrainedLocked();
194 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700195 SET_ERR_L("Timeout waiting for HAL to drain");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800196 return res;
197 }
198 }
199 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
200
201 if (mRequestThread != NULL) {
202 mRequestThread->requestExit();
203 }
204
205 mOutputStreams.clear();
206 mInputStream.clear();
207
208 if (mRequestThread != NULL) {
209 mRequestThread->join();
210 mRequestThread.clear();
211 }
212
213 if (mHal3Device != NULL) {
214 mHal3Device->common.close(&mHal3Device->common);
215 mHal3Device = NULL;
216 }
217
218 mStatus = STATUS_UNINITIALIZED;
219
220 ALOGV("%s: X", __FUNCTION__);
221 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800222}
223
224status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
225 ATRACE_CALL();
226 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800227 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800228
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800229 const char *status =
230 mStatus == STATUS_ERROR ? "ERROR" :
231 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
232 mStatus == STATUS_IDLE ? "IDLE" :
233 mStatus == STATUS_ACTIVE ? "ACTIVE" :
234 "Unknown";
235 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700236 if (mStatus == STATUS_ERROR) {
237 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
238 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800239 lines.appendFormat(" Stream configuration:\n");
240
241 if (mInputStream != NULL) {
242 write(fd, lines.string(), lines.size());
243 mInputStream->dump(fd, args);
244 } else {
245 lines.appendFormat(" No input stream.\n");
246 write(fd, lines.string(), lines.size());
247 }
248 for (size_t i = 0; i < mOutputStreams.size(); i++) {
249 mOutputStreams[i]->dump(fd,args);
250 }
251
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700252 lines = String8(" In-flight requests:\n");
253 if (mInFlightMap.size() == 0) {
254 lines.append(" None\n");
255 } else {
256 for (size_t i = 0; i < mInFlightMap.size(); i++) {
257 InFlightRequest r = mInFlightMap.valueAt(i);
258 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
259 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
260 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
261 r.numBuffersLeft);
262 }
263 }
264 write(fd, lines.string(), lines.size());
265
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700267 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800268 write(fd, lines.string(), lines.size());
269 mHal3Device->ops->dump(mHal3Device, fd);
270 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800271
272 return OK;
273}
274
275const CameraMetadata& Camera3Device::info() const {
276 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
278 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700279 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280 mStatus == STATUS_ERROR ?
281 "when in error state" : "before init");
282 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283 return mDeviceInfo;
284}
285
286status_t Camera3Device::capture(CameraMetadata &request) {
287 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800288 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800289
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700290 // TODO: take ownership of the request
291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800292 switch (mStatus) {
293 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700294 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800295 return INVALID_OPERATION;
296 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700297 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800298 return INVALID_OPERATION;
299 case STATUS_IDLE:
300 case STATUS_ACTIVE:
301 // OK
302 break;
303 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700304 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305 return INVALID_OPERATION;
306 }
307
308 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
309 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700310 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 return BAD_VALUE;
312 }
313
314 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800315}
316
317
318status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
319 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800320 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800321
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322 switch (mStatus) {
323 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700324 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800325 return INVALID_OPERATION;
326 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700327 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800328 return INVALID_OPERATION;
329 case STATUS_IDLE:
330 case STATUS_ACTIVE:
331 // OK
332 break;
333 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700334 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800335 return INVALID_OPERATION;
336 }
337
338 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
339 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700340 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800341 return BAD_VALUE;
342 }
343
344 RequestList newRepeatingRequests;
345 newRepeatingRequests.push_back(newRepeatingRequest);
346
347 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
348}
349
350
351sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
352 const CameraMetadata &request) {
353 status_t res;
354
355 if (mStatus == STATUS_IDLE) {
356 res = configureStreamsLocked();
357 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700358 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800359 return NULL;
360 }
361 }
362
363 sp<CaptureRequest> newRequest = createCaptureRequest(request);
364 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800365}
366
367status_t Camera3Device::clearStreamingRequest() {
368 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800369 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800370
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800371 switch (mStatus) {
372 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700373 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800374 return INVALID_OPERATION;
375 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700376 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800377 return INVALID_OPERATION;
378 case STATUS_IDLE:
379 case STATUS_ACTIVE:
380 // OK
381 break;
382 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700383 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800384 return INVALID_OPERATION;
385 }
386
387 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800388}
389
390status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
391 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800392
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700393 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800394}
395
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700396status_t Camera3Device::createInputStream(
397 uint32_t width, uint32_t height, int format, int *id) {
398 ATRACE_CALL();
399 Mutex::Autolock l(mLock);
400
401 status_t res;
402 bool wasActive = false;
403
404 switch (mStatus) {
405 case STATUS_ERROR:
406 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
407 return INVALID_OPERATION;
408 case STATUS_UNINITIALIZED:
409 ALOGE("%s: Device not initialized", __FUNCTION__);
410 return INVALID_OPERATION;
411 case STATUS_IDLE:
412 // OK
413 break;
414 case STATUS_ACTIVE:
415 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
416 mRequestThread->setPaused(true);
417 res = waitUntilDrainedLocked();
418 if (res != OK) {
419 ALOGE("%s: Can't pause captures to reconfigure streams!",
420 __FUNCTION__);
421 mStatus = STATUS_ERROR;
422 return res;
423 }
424 wasActive = true;
425 break;
426 default:
427 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
428 return INVALID_OPERATION;
429 }
430 assert(mStatus == STATUS_IDLE);
431
432 if (mInputStream != 0) {
433 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
434 return INVALID_OPERATION;
435 }
436
437 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
438 width, height, format);
439
440 mInputStream = newStream;
441
442 *id = mNextStreamId++;
443
444 // Continue captures if active at start
445 if (wasActive) {
446 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
447 res = configureStreamsLocked();
448 if (res != OK) {
449 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
450 __FUNCTION__, mNextStreamId, strerror(-res), res);
451 return res;
452 }
453 mRequestThread->setPaused(false);
454 }
455
456 return OK;
457}
458
Igor Murashkin2fba5842013-04-22 14:03:54 -0700459
460status_t Camera3Device::createZslStream(
461 uint32_t width, uint32_t height,
462 int depth,
463 /*out*/
464 int *id,
465 sp<Camera3ZslStream>* zslStream) {
466 ATRACE_CALL();
467 Mutex::Autolock l(mLock);
468
469 status_t res;
470 bool wasActive = false;
471
472 switch (mStatus) {
473 case STATUS_ERROR:
474 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
475 return INVALID_OPERATION;
476 case STATUS_UNINITIALIZED:
477 ALOGE("%s: Device not initialized", __FUNCTION__);
478 return INVALID_OPERATION;
479 case STATUS_IDLE:
480 // OK
481 break;
482 case STATUS_ACTIVE:
483 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
484 mRequestThread->setPaused(true);
485 res = waitUntilDrainedLocked();
486 if (res != OK) {
487 ALOGE("%s: Can't pause captures to reconfigure streams!",
488 __FUNCTION__);
489 mStatus = STATUS_ERROR;
490 return res;
491 }
492 wasActive = true;
493 break;
494 default:
495 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
496 return INVALID_OPERATION;
497 }
498 assert(mStatus == STATUS_IDLE);
499
500 if (mInputStream != 0) {
501 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
502 return INVALID_OPERATION;
503 }
504
505 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
506 width, height, depth);
507
508 res = mOutputStreams.add(mNextStreamId, newStream);
509 if (res < 0) {
510 ALOGE("%s: Can't add new stream to set: %s (%d)",
511 __FUNCTION__, strerror(-res), res);
512 return res;
513 }
514 mInputStream = newStream;
515
516 *id = mNextStreamId++;
517 *zslStream = newStream;
518
519 // Continue captures if active at start
520 if (wasActive) {
521 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
522 res = configureStreamsLocked();
523 if (res != OK) {
524 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
525 __FUNCTION__, mNextStreamId, strerror(-res), res);
526 return res;
527 }
528 mRequestThread->setPaused(false);
529 }
530
531 return OK;
532}
533
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800534status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
535 uint32_t width, uint32_t height, int format, size_t size, int *id) {
536 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800537 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800538
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800539 status_t res;
540 bool wasActive = false;
541
542 switch (mStatus) {
543 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700544 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800545 return INVALID_OPERATION;
546 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700547 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548 return INVALID_OPERATION;
549 case STATUS_IDLE:
550 // OK
551 break;
552 case STATUS_ACTIVE:
553 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
554 mRequestThread->setPaused(true);
555 res = waitUntilDrainedLocked();
556 if (res != OK) {
557 ALOGE("%s: Can't pause captures to reconfigure streams!",
558 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800559 return res;
560 }
561 wasActive = true;
562 break;
563 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700564 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800565 return INVALID_OPERATION;
566 }
567 assert(mStatus == STATUS_IDLE);
568
569 sp<Camera3OutputStream> newStream;
570 if (format == HAL_PIXEL_FORMAT_BLOB) {
571 newStream = new Camera3OutputStream(mNextStreamId, consumer,
572 width, height, size, format);
573 } else {
574 newStream = new Camera3OutputStream(mNextStreamId, consumer,
575 width, height, format);
576 }
577
578 res = mOutputStreams.add(mNextStreamId, newStream);
579 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700580 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800581 return res;
582 }
583
584 *id = mNextStreamId++;
585
586 // Continue captures if active at start
587 if (wasActive) {
588 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
589 res = configureStreamsLocked();
590 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700591 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
592 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800593 return res;
594 }
595 mRequestThread->setPaused(false);
596 }
597
598 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800599}
600
601status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
602 ATRACE_CALL();
603 (void)outputId; (void)id;
604
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700605 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800606 return INVALID_OPERATION;
607}
608
609
610status_t Camera3Device::getStreamInfo(int id,
611 uint32_t *width, uint32_t *height, uint32_t *format) {
612 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800614
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800615 switch (mStatus) {
616 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700617 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800618 return INVALID_OPERATION;
619 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700620 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800621 return INVALID_OPERATION;
622 case STATUS_IDLE:
623 case STATUS_ACTIVE:
624 // OK
625 break;
626 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700627 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800628 return INVALID_OPERATION;
629 }
630
631 ssize_t idx = mOutputStreams.indexOfKey(id);
632 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700633 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 return idx;
635 }
636
637 if (width) *width = mOutputStreams[idx]->getWidth();
638 if (height) *height = mOutputStreams[idx]->getHeight();
639 if (format) *format = mOutputStreams[idx]->getFormat();
640
641 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642}
643
644status_t Camera3Device::setStreamTransform(int id,
645 int transform) {
646 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800647 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800648
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800649 switch (mStatus) {
650 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700651 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800652 return INVALID_OPERATION;
653 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700654 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800655 return INVALID_OPERATION;
656 case STATUS_IDLE:
657 case STATUS_ACTIVE:
658 // OK
659 break;
660 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700661 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800662 return INVALID_OPERATION;
663 }
664
665 ssize_t idx = mOutputStreams.indexOfKey(id);
666 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700667 CLOGE("Stream %d does not exist",
668 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 return BAD_VALUE;
670 }
671
672 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800673}
674
675status_t Camera3Device::deleteStream(int id) {
676 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800677 Mutex::Autolock l(mLock);
678 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800679
Igor Murashkine2172be2013-05-28 15:31:39 -0700680 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
681
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800682 // CameraDevice semantics require device to already be idle before
683 // deleteStream is called, unlike for createStream.
684 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700685 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
686 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800687 }
688
Igor Murashkin2fba5842013-04-22 14:03:54 -0700689 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800690 if (mInputStream != NULL && id == mInputStream->getId()) {
691 deletedStream = mInputStream;
692 mInputStream.clear();
693 } else {
694 ssize_t idx = mOutputStreams.indexOfKey(id);
695 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700696 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800697 return BAD_VALUE;
698 }
699 deletedStream = mOutputStreams.editValueAt(idx);
700 mOutputStreams.removeItem(id);
701 }
702
703 // Free up the stream endpoint so that it can be used by some other stream
704 res = deletedStream->disconnect();
705 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700706 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 // fall through since we want to still list the stream as deleted.
708 }
709 mDeletedStreams.add(deletedStream);
710
711 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800712}
713
714status_t Camera3Device::deleteReprocessStream(int id) {
715 ATRACE_CALL();
716 (void)id;
717
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700718 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800719 return INVALID_OPERATION;
720}
721
722
723status_t Camera3Device::createDefaultRequest(int templateId,
724 CameraMetadata *request) {
725 ATRACE_CALL();
726 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727 Mutex::Autolock l(mLock);
728
729 switch (mStatus) {
730 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700731 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800732 return INVALID_OPERATION;
733 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700734 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735 return INVALID_OPERATION;
736 case STATUS_IDLE:
737 case STATUS_ACTIVE:
738 // OK
739 break;
740 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700741 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800742 return INVALID_OPERATION;
743 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800744
745 const camera_metadata_t *rawRequest;
746 rawRequest = mHal3Device->ops->construct_default_request_settings(
747 mHal3Device, templateId);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700748 if (rawRequest == NULL) {
749 SET_ERR_L("HAL is unable to construct default settings for template %d",
750 templateId);
751 return DEAD_OBJECT;
752 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800753 *request = rawRequest;
754
755 return OK;
756}
757
758status_t Camera3Device::waitUntilDrained() {
759 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800761
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800762 return waitUntilDrainedLocked();
763}
764
765status_t Camera3Device::waitUntilDrainedLocked() {
766 ATRACE_CALL();
767 status_t res;
768
769 switch (mStatus) {
770 case STATUS_UNINITIALIZED:
771 case STATUS_IDLE:
772 ALOGV("%s: Already idle", __FUNCTION__);
773 return OK;
774 case STATUS_ERROR:
775 case STATUS_ACTIVE:
776 // Need to shut down
777 break;
778 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700779 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 return INVALID_OPERATION;
781 }
782
783 if (mRequestThread != NULL) {
784 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
785 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700786 SET_ERR_L("Can't stop request thread in %f seconds!",
787 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 return res;
789 }
790 }
791 if (mInputStream != NULL) {
792 res = mInputStream->waitUntilIdle(kShutdownTimeout);
793 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700794 SET_ERR_L("Can't idle input stream %d in %f seconds!",
795 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800796 return res;
797 }
798 }
799 for (size_t i = 0; i < mOutputStreams.size(); i++) {
800 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
801 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 SET_ERR_L("Can't idle output stream %d in %f seconds!",
803 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800804 return res;
805 }
806 }
807
808 if (mStatus != STATUS_ERROR) {
809 mStatus = STATUS_IDLE;
810 }
811
812 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813}
814
815status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
816 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700817 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800818
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700819 if (listener != NULL && mListener != NULL) {
820 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
821 }
822 mListener = listener;
823
824 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800825}
826
827status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700828 ATRACE_CALL();
829 status_t res;
830 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700832 while (mResultQueue.empty()) {
833 res = mResultSignal.waitRelative(mOutputLock, timeout);
834 if (res == TIMED_OUT) {
835 return res;
836 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
838 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700839 return res;
840 }
841 }
842 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800843}
844
845status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
846 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700847 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700849 if (mResultQueue.empty()) {
850 return NOT_ENOUGH_DATA;
851 }
852
853 CameraMetadata &result = *(mResultQueue.begin());
854 frame->acquire(result);
855 mResultQueue.erase(mResultQueue.begin());
856
857 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800858}
859
860status_t Camera3Device::triggerAutofocus(uint32_t id) {
861 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800862
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700863 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
864 // Mix-in this trigger into the next request and only the next request.
865 RequestTrigger trigger[] = {
866 {
867 ANDROID_CONTROL_AF_TRIGGER,
868 ANDROID_CONTROL_AF_TRIGGER_START
869 },
870 {
871 ANDROID_CONTROL_AF_TRIGGER_ID,
872 static_cast<int32_t>(id)
873 },
874 };
875
876 return mRequestThread->queueTrigger(trigger,
877 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800878}
879
880status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
881 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800882
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700883 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
884 // Mix-in this trigger into the next request and only the next request.
885 RequestTrigger trigger[] = {
886 {
887 ANDROID_CONTROL_AF_TRIGGER,
888 ANDROID_CONTROL_AF_TRIGGER_CANCEL
889 },
890 {
891 ANDROID_CONTROL_AF_TRIGGER_ID,
892 static_cast<int32_t>(id)
893 },
894 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700896 return mRequestThread->queueTrigger(trigger,
897 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800898}
899
900status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
901 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700903 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
904 // Mix-in this trigger into the next request and only the next request.
905 RequestTrigger trigger[] = {
906 {
907 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
908 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
909 },
910 {
911 ANDROID_CONTROL_AE_PRECAPTURE_ID,
912 static_cast<int32_t>(id)
913 },
914 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 return mRequestThread->queueTrigger(trigger,
917 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800918}
919
920status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
921 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
922 ATRACE_CALL();
923 (void)reprocessStreamId; (void)buffer; (void)listener;
924
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700925 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926 return INVALID_OPERATION;
927}
928
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929/**
930 * Camera3Device private methods
931 */
932
933sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
934 const CameraMetadata &request) {
935 ATRACE_CALL();
936 status_t res;
937
938 sp<CaptureRequest> newRequest = new CaptureRequest;
939 newRequest->mSettings = request;
940
941 camera_metadata_entry_t inputStreams =
942 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
943 if (inputStreams.count > 0) {
944 if (mInputStream == NULL ||
945 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700946 CLOGE("Request references unknown input stream %d",
947 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 return NULL;
949 }
950 // Lazy completion of stream configuration (allocation/registration)
951 // on first use
952 if (mInputStream->isConfiguring()) {
953 res = mInputStream->finishConfiguration(mHal3Device);
954 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700955 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700957 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800958 return NULL;
959 }
960 }
961
962 newRequest->mInputStream = mInputStream;
963 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
964 }
965
966 camera_metadata_entry_t streams =
967 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
968 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700969 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800970 return NULL;
971 }
972
973 for (size_t i = 0; i < streams.count; i++) {
974 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
975 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700976 CLOGE("Request references unknown stream %d",
977 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 return NULL;
979 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700980 sp<Camera3OutputStreamInterface> stream =
981 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982
983 // Lazy completion of stream configuration (allocation/registration)
984 // on first use
985 if (stream->isConfiguring()) {
986 res = stream->finishConfiguration(mHal3Device);
987 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700988 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
989 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 return NULL;
991 }
992 }
993
994 newRequest->mOutputStreams.push(stream);
995 }
996 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
997
998 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800999}
1000
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001status_t Camera3Device::configureStreamsLocked() {
1002 ATRACE_CALL();
1003 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001006 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 return INVALID_OPERATION;
1008 }
1009
1010 // Start configuring the streams
1011
1012 camera3_stream_configuration config;
1013
1014 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1015
1016 Vector<camera3_stream_t*> streams;
1017 streams.setCapacity(config.num_streams);
1018
1019 if (mInputStream != NULL) {
1020 camera3_stream_t *inputStream;
1021 inputStream = mInputStream->startConfiguration();
1022 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001023 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return INVALID_OPERATION;
1025 }
1026 streams.add(inputStream);
1027 }
1028
1029 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001030
1031 // Don't configure bidi streams twice, nor add them twice to the list
1032 if (mOutputStreams[i].get() ==
1033 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1034
1035 config.num_streams--;
1036 continue;
1037 }
1038
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 camera3_stream_t *outputStream;
1040 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1041 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
1044 }
1045 streams.add(outputStream);
1046 }
1047
1048 config.streams = streams.editArray();
1049
1050 // Do the HAL configuration; will potentially touch stream
1051 // max_buffers, usage, priv fields.
1052
1053 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1054
1055 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1057 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 return res;
1059 }
1060
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001061 // Finish all stream configuration immediately.
1062 // TODO: Try to relax this later back to lazy completion, which should be
1063 // faster
1064
Igor Murashkin073f8572013-05-02 14:59:28 -07001065 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001066 res = mInputStream->finishConfiguration(mHal3Device);
1067 if (res != OK) {
1068 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1069 mInputStream->getId(), strerror(-res), res);
1070 return res;
1071 }
1072 }
1073
1074 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001075 sp<Camera3OutputStreamInterface> outputStream =
1076 mOutputStreams.editValueAt(i);
1077 if (outputStream->isConfiguring()) {
1078 res = outputStream->finishConfiguration(mHal3Device);
1079 if (res != OK) {
1080 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1081 outputStream->getId(), strerror(-res), res);
1082 return res;
1083 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001084 }
1085 }
1086
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 // Request thread needs to know to avoid using repeat-last-settings protocol
1088 // across configure_streams() calls
1089 mRequestThread->configurationComplete();
1090
1091 // Finish configuring the streams lazily on first reference
1092
1093 mStatus = STATUS_ACTIVE;
1094
1095 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001096}
1097
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001098void Camera3Device::setErrorState(const char *fmt, ...) {
1099 Mutex::Autolock l(mLock);
1100 va_list args;
1101 va_start(args, fmt);
1102
1103 setErrorStateLockedV(fmt, args);
1104
1105 va_end(args);
1106}
1107
1108void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1109 Mutex::Autolock l(mLock);
1110 setErrorStateLockedV(fmt, args);
1111}
1112
1113void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1114 va_list args;
1115 va_start(args, fmt);
1116
1117 setErrorStateLockedV(fmt, args);
1118
1119 va_end(args);
1120}
1121
1122void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001123 // Print out all error messages to log
1124 String8 errorCause = String8::formatV(fmt, args);
1125 ALOGE("Camera %d: %s", mId, errorCause.string());
1126
1127 // But only do error state transition steps for the first error
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001128 if (mStatus == STATUS_ERROR) return;
1129
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001130 mErrorCause = errorCause;
1131
1132 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001133 mStatus = STATUS_ERROR;
1134}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001135
1136/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001137 * In-flight request management
1138 */
1139
1140status_t Camera3Device::registerInFlight(int32_t frameNumber,
1141 int32_t numBuffers) {
1142 ATRACE_CALL();
1143 Mutex::Autolock l(mInFlightLock);
1144
1145 ssize_t res;
1146 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1147 if (res < 0) return res;
1148
1149 return OK;
1150}
1151
1152/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001153 * Camera HAL device callback methods
1154 */
1155
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001157 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001158
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001159 status_t res;
1160
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001161 uint32_t frameNumber = result->frame_number;
1162 if (result->result == NULL && result->num_output_buffers == 0) {
1163 SET_ERR("No result data provided by HAL for frame %d",
1164 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001165 return;
1166 }
1167
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001168 // Get capture timestamp from list of in-flight requests, where it was added
1169 // by the shutter notification for this frame. Then update the in-flight
1170 // status and remove the in-flight entry if all result data has been
1171 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001172 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001173 {
1174 Mutex::Autolock l(mInFlightLock);
1175 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1176 if (idx == NAME_NOT_FOUND) {
1177 SET_ERR("Unknown frame number for capture result: %d",
1178 frameNumber);
1179 return;
1180 }
1181 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1182 timestamp = request.captureTimestamp;
1183 if (timestamp == 0) {
1184 SET_ERR("Called before shutter notify for frame %d",
1185 frameNumber);
1186 return;
1187 }
1188
1189 if (result->result != NULL) {
1190 if (request.haveResultMetadata) {
1191 SET_ERR("Called multiple times with metadata for frame %d",
1192 frameNumber);
1193 return;
1194 }
1195 request.haveResultMetadata = true;
1196 }
1197
1198 request.numBuffersLeft -= result->num_output_buffers;
1199
1200 if (request.numBuffersLeft < 0) {
1201 SET_ERR("Too many buffers returned for frame %d",
1202 frameNumber);
1203 return;
1204 }
1205
1206 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1207 mInFlightMap.removeItemsAt(idx, 1);
1208 }
1209
1210 // Sanity check - if we have too many in-flight frames, something has
1211 // likely gone wrong
1212 if (mInFlightMap.size() > kInFlightWarnLimit) {
1213 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1214 }
1215
1216 }
1217
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001218 AlgState cur3aState;
1219 AlgState new3aState;
1220 int32_t aeTriggerId = 0;
1221 int32_t afTriggerId = 0;
1222
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001223 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001224
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001225 // Process the result metadata, if provided
1226 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001227 Mutex::Autolock l(mOutputLock);
1228
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001229 if (frameNumber != mNextResultFrameNumber) {
1230 SET_ERR("Out-of-order capture result metadata submitted! "
1231 "(got frame number %d, expecting %d)",
1232 frameNumber, mNextResultFrameNumber);
1233 return;
1234 }
1235 mNextResultFrameNumber++;
1236
1237 CameraMetadata &captureResult =
1238 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001239
1240 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001241 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001242 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001243 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001244 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001245 } else {
1246 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001247 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001248 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001249
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001250 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001251
1252 camera_metadata_entry entry =
1253 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1254 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001255 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001256 frameNumber);
1257 }
1258 if (timestamp != entry.data.i64[0]) {
1259 SET_ERR("Timestamp mismatch between shutter notify and result"
1260 " metadata for frame %d (%lld vs %lld respectively)",
1261 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001262 }
1263
1264 // Get 3A states from result metadata
1265
1266 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1267 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001268 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001269 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001270 } else {
1271 new3aState.aeState =
1272 static_cast<camera_metadata_enum_android_control_ae_state>(
1273 entry.data.u8[0]);
1274 }
1275
1276 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1277 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001278 CLOGE("No AF 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.afState =
1282 static_cast<camera_metadata_enum_android_control_af_state>(
1283 entry.data.u8[0]);
1284 }
1285
1286 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1287 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001288 CLOGE("No AWB 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.awbState =
1292 static_cast<camera_metadata_enum_android_control_awb_state>(
1293 entry.data.u8[0]);
1294 }
1295
1296 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1297 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001298 CLOGE("No AF trigger ID 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 afTriggerId = entry.data.i32[0];
1302 }
1303
1304 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1305 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001306 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001307 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001308 } else {
1309 aeTriggerId = entry.data.i32[0];
1310 }
1311
1312 listener = mListener;
1313 cur3aState = m3AState;
1314
1315 m3AState = new3aState;
1316 } // scope for mOutputLock
1317
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001318 // Return completed buffers to their streams with the timestamp
1319
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001320 for (size_t i = 0; i < result->num_output_buffers; i++) {
1321 Camera3Stream *stream =
1322 Camera3Stream::cast(result->output_buffers[i].stream);
1323 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1324 // Note: stream may be deallocated at this point, if this buffer was the
1325 // last reference to it.
1326 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001327 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001328 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001329 }
1330 }
1331
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001332 // Finally, dispatch any 3A change events to listeners if we got metadata
1333
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001334 if (result->result != NULL) {
1335 mResultSignal.signal();
1336 }
1337
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001338 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001339 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001340 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001341 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001342 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1343 }
1344 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001345 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001346 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001347 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1348 }
1349 if (new3aState.awbState != cur3aState.awbState) {
1350 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1351 }
1352 }
1353
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001354}
1355
1356void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001357 NotificationListener *listener;
1358 {
1359 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001360 listener = mListener;
1361 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001362
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001363 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001364 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001365 return;
1366 }
1367
1368 switch (msg->type) {
1369 case CAMERA3_MSG_ERROR: {
1370 int streamId = 0;
1371 if (msg->message.error.error_stream != NULL) {
1372 Camera3Stream *stream =
1373 Camera3Stream::cast(
1374 msg->message.error.error_stream);
1375 streamId = stream->getId();
1376 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001377 if (listener != NULL) {
1378 listener->notifyError(msg->message.error.error_code,
1379 msg->message.error.frame_number, streamId);
1380 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001381 break;
1382 }
1383 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001384 ssize_t idx;
1385 uint32_t frameNumber = msg->message.shutter.frame_number;
1386 nsecs_t timestamp = msg->message.shutter.timestamp;
1387 // Verify ordering of shutter notifications
1388 {
1389 Mutex::Autolock l(mOutputLock);
1390 if (frameNumber != mNextShutterFrameNumber) {
1391 SET_ERR("Shutter notification out-of-order. Expected "
1392 "notification for frame %d, got frame %d",
1393 mNextShutterFrameNumber, frameNumber);
1394 break;
1395 }
1396 mNextShutterFrameNumber++;
1397 }
1398
1399 // Set timestamp for the request in the in-flight tracking
1400 {
1401 Mutex::Autolock l(mInFlightLock);
1402 idx = mInFlightMap.indexOfKey(frameNumber);
1403 if (idx >= 0) {
1404 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1405 }
1406 }
1407 if (idx < 0) {
1408 SET_ERR("Shutter notification for non-existent frame number %d",
1409 frameNumber);
1410 break;
1411 }
1412
1413 // Call listener, if any
1414 if (listener != NULL) {
1415 listener->notifyShutter(frameNumber, timestamp);
1416 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001417 break;
1418 }
1419 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001420 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001421 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001422 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001423}
1424
1425/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001426 * RequestThread inner class methods
1427 */
1428
1429Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1430 camera3_device_t *hal3Device) :
1431 Thread(false),
1432 mParent(parent),
1433 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001434 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435 mReconfigured(false),
1436 mDoPause(false),
1437 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001438 mFrameNumber(0),
1439 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440}
1441
1442void Camera3Device::RequestThread::configurationComplete() {
1443 Mutex::Autolock l(mRequestLock);
1444 mReconfigured = true;
1445}
1446
1447status_t Camera3Device::RequestThread::queueRequest(
1448 sp<CaptureRequest> request) {
1449 Mutex::Autolock l(mRequestLock);
1450 mRequestQueue.push_back(request);
1451
1452 return OK;
1453}
1454
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001455
1456status_t Camera3Device::RequestThread::queueTrigger(
1457 RequestTrigger trigger[],
1458 size_t count) {
1459
1460 Mutex::Autolock l(mTriggerMutex);
1461 status_t ret;
1462
1463 for (size_t i = 0; i < count; ++i) {
1464 ret = queueTriggerLocked(trigger[i]);
1465
1466 if (ret != OK) {
1467 return ret;
1468 }
1469 }
1470
1471 return OK;
1472}
1473
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001474int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1475 sp<Camera3Device> d = device.promote();
1476 if (d != NULL) return d->mId;
1477 return 0;
1478}
1479
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001480status_t Camera3Device::RequestThread::queueTriggerLocked(
1481 RequestTrigger trigger) {
1482
1483 uint32_t tag = trigger.metadataTag;
1484 ssize_t index = mTriggerMap.indexOfKey(tag);
1485
1486 switch (trigger.getTagType()) {
1487 case TYPE_BYTE:
1488 // fall-through
1489 case TYPE_INT32:
1490 break;
1491 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001492 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1493 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001494 return INVALID_OPERATION;
1495 }
1496
1497 /**
1498 * Collect only the latest trigger, since we only have 1 field
1499 * in the request settings per trigger tag, and can't send more than 1
1500 * trigger per request.
1501 */
1502 if (index != NAME_NOT_FOUND) {
1503 mTriggerMap.editValueAt(index) = trigger;
1504 } else {
1505 mTriggerMap.add(tag, trigger);
1506 }
1507
1508 return OK;
1509}
1510
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001511status_t Camera3Device::RequestThread::setRepeatingRequests(
1512 const RequestList &requests) {
1513 Mutex::Autolock l(mRequestLock);
1514 mRepeatingRequests.clear();
1515 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1516 requests.begin(), requests.end());
1517 return OK;
1518}
1519
1520status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1521 Mutex::Autolock l(mRequestLock);
1522 mRepeatingRequests.clear();
1523 return OK;
1524}
1525
1526void Camera3Device::RequestThread::setPaused(bool paused) {
1527 Mutex::Autolock l(mPauseLock);
1528 mDoPause = paused;
1529 mDoPauseSignal.signal();
1530}
1531
1532status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1533 status_t res;
1534 Mutex::Autolock l(mPauseLock);
1535 while (!mPaused) {
1536 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1537 if (res == TIMED_OUT) {
1538 return res;
1539 }
1540 }
1541 return OK;
1542}
1543
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001544status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1545 int32_t requestId, nsecs_t timeout) {
1546 Mutex::Autolock l(mLatestRequestMutex);
1547 status_t res;
1548 while (mLatestRequestId != requestId) {
1549 nsecs_t startTime = systemTime();
1550
1551 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1552 if (res != OK) return res;
1553
1554 timeout -= (systemTime() - startTime);
1555 }
1556
1557 return OK;
1558}
1559
1560
1561
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001562bool Camera3Device::RequestThread::threadLoop() {
1563
1564 status_t res;
1565
1566 // Handle paused state.
1567 if (waitIfPaused()) {
1568 return true;
1569 }
1570
1571 // Get work to do
1572
1573 sp<CaptureRequest> nextRequest = waitForNextRequest();
1574 if (nextRequest == NULL) {
1575 return true;
1576 }
1577
1578 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001580 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001581
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001582 // Insert any queued triggers (before metadata is locked)
1583 int32_t triggerCount;
1584 res = insertTriggers(nextRequest);
1585 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001586 SET_ERR("RequestThread: Unable to insert triggers "
1587 "(capture request %d, HAL device: %s (%d)",
1588 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001589 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1590 return false;
1591 }
1592 triggerCount = res;
1593
1594 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1595
1596 // If the request is the same as last, or we had triggers last time
1597 if (mPrevRequest != nextRequest || triggersMixedIn) {
1598 /**
1599 * The request should be presorted so accesses in HAL
1600 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1601 */
1602 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001603 request.settings = nextRequest->mSettings.getAndLock();
1604 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001605 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1606
1607 IF_ALOGV() {
1608 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1609 find_camera_metadata_ro_entry(
1610 request.settings,
1611 ANDROID_CONTROL_AF_TRIGGER,
1612 &e
1613 );
1614 if (e.count > 0) {
1615 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1616 __FUNCTION__,
1617 mFrameNumber+1,
1618 e.data.u8[0]);
1619 }
1620 }
1621 } else {
1622 // leave request.settings NULL to indicate 'reuse latest given'
1623 ALOGVV("%s: Request settings are REUSED",
1624 __FUNCTION__);
1625 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626
1627 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628
1629 // Fill in buffers
1630
1631 if (nextRequest->mInputStream != NULL) {
1632 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001633 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001635 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001636 " %s (%d)", strerror(-res), res);
1637 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1638 return true;
1639 }
1640 } else {
1641 request.input_buffer = NULL;
1642 }
1643
1644 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1645 nextRequest->mOutputStreams.size());
1646 request.output_buffers = outputBuffers.array();
1647 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1648 res = nextRequest->mOutputStreams.editItemAt(i)->
1649 getBuffer(&outputBuffers.editItemAt(i));
1650 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001651 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001652 "%s (%d)", strerror(-res), res);
1653 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1654 return true;
1655 }
1656 request.num_output_buffers++;
1657 }
1658
1659 request.frame_number = mFrameNumber++;
1660
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001661 // Log request in the in-flight queue
1662 sp<Camera3Device> parent = mParent.promote();
1663 if (parent == NULL) {
1664 CLOGE("RequestThread: Parent is gone");
1665 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1666 return false;
1667 }
1668
1669 res = parent->registerInFlight(request.frame_number,
1670 request.num_output_buffers);
1671 if (res != OK) {
1672 SET_ERR("RequestThread: Unable to register new in-flight request:"
1673 " %s (%d)", strerror(-res), res);
1674 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1675 return false;
1676 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001677
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001678 // Submit request and block until ready for next one
1679
1680 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1681 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001682 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001683 " device: %s (%d)", request.frame_number, strerror(-res), res);
1684 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1685 return false;
1686 }
1687
1688 if (request.settings != NULL) {
1689 nextRequest->mSettings.unlock(request.settings);
1690 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001691
1692 // Remove any previously queued triggers (after unlock)
1693 res = removeTriggers(mPrevRequest);
1694 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001695 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001696 "(capture request %d, HAL device: %s (%d)",
1697 request.frame_number, strerror(-res), res);
1698 return false;
1699 }
1700 mPrevTriggers = triggerCount;
1701
1702 // Read android.request.id from the request settings metadata
1703 // - inform waitUntilRequestProcessed thread of a new request ID
1704 {
1705 Mutex::Autolock al(mLatestRequestMutex);
1706
1707 camera_metadata_entry_t requestIdEntry =
1708 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1709 if (requestIdEntry.count > 0) {
1710 mLatestRequestId = requestIdEntry.data.i32[0];
1711 } else {
1712 ALOGW("%s: Did not have android.request.id set in the request",
1713 __FUNCTION__);
1714 mLatestRequestId = NAME_NOT_FOUND;
1715 }
1716
1717 mLatestRequestSignal.signal();
1718 }
1719
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001720 // Return input buffer back to framework
1721 if (request.input_buffer != NULL) {
1722 Camera3Stream *stream =
1723 Camera3Stream::cast(request.input_buffer->stream);
1724 res = stream->returnInputBuffer(*(request.input_buffer));
1725 // Note: stream may be deallocated at this point, if this buffer was the
1726 // last reference to it.
1727 if (res != OK) {
1728 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1729 " its stream:%s (%d)", __FUNCTION__,
1730 request.frame_number, strerror(-res), res);
1731 // TODO: Report error upstream
1732 }
1733 }
1734
1735
1736
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001737 return true;
1738}
1739
1740void Camera3Device::RequestThread::cleanUpFailedRequest(
1741 camera3_capture_request_t &request,
1742 sp<CaptureRequest> &nextRequest,
1743 Vector<camera3_stream_buffer_t> &outputBuffers) {
1744
1745 if (request.settings != NULL) {
1746 nextRequest->mSettings.unlock(request.settings);
1747 }
1748 if (request.input_buffer != NULL) {
1749 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001750 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751 }
1752 for (size_t i = 0; i < request.num_output_buffers; i++) {
1753 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1754 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1755 outputBuffers[i], 0);
1756 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001757}
1758
1759sp<Camera3Device::CaptureRequest>
1760 Camera3Device::RequestThread::waitForNextRequest() {
1761 status_t res;
1762 sp<CaptureRequest> nextRequest;
1763
1764 // Optimized a bit for the simple steady-state case (single repeating
1765 // request), to avoid putting that request in the queue temporarily.
1766 Mutex::Autolock l(mRequestLock);
1767
1768 while (mRequestQueue.empty()) {
1769 if (!mRepeatingRequests.empty()) {
1770 // Always atomically enqueue all requests in a repeating request
1771 // list. Guarantees a complete in-sequence set of captures to
1772 // application.
1773 const RequestList &requests = mRepeatingRequests;
1774 RequestList::const_iterator firstRequest =
1775 requests.begin();
1776 nextRequest = *firstRequest;
1777 mRequestQueue.insert(mRequestQueue.end(),
1778 ++firstRequest,
1779 requests.end());
1780 // No need to wait any longer
1781 break;
1782 }
1783
1784 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1785
1786 if (res == TIMED_OUT) {
1787 // Signal that we're paused by starvation
1788 Mutex::Autolock pl(mPauseLock);
1789 if (mPaused == false) {
1790 mPaused = true;
1791 mPausedSignal.signal();
1792 }
1793 // Stop waiting for now and let thread management happen
1794 return NULL;
1795 }
1796 }
1797
1798 if (nextRequest == NULL) {
1799 // Don't have a repeating request already in hand, so queue
1800 // must have an entry now.
1801 RequestList::iterator firstRequest =
1802 mRequestQueue.begin();
1803 nextRequest = *firstRequest;
1804 mRequestQueue.erase(firstRequest);
1805 }
1806
1807 // Not paused
1808 Mutex::Autolock pl(mPauseLock);
1809 mPaused = false;
1810
1811 // Check if we've reconfigured since last time, and reset the preview
1812 // request if so. Can't use 'NULL request == repeat' across configure calls.
1813 if (mReconfigured) {
1814 mPrevRequest.clear();
1815 mReconfigured = false;
1816 }
1817
1818 return nextRequest;
1819}
1820
1821bool Camera3Device::RequestThread::waitIfPaused() {
1822 status_t res;
1823 Mutex::Autolock l(mPauseLock);
1824 while (mDoPause) {
1825 // Signal that we're paused by request
1826 if (mPaused == false) {
1827 mPaused = true;
1828 mPausedSignal.signal();
1829 }
1830 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1831 if (res == TIMED_OUT) {
1832 return true;
1833 }
1834 }
1835 // We don't set mPaused to false here, because waitForNextRequest needs
1836 // to further manage the paused state in case of starvation.
1837 return false;
1838}
1839
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001840void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1841 sp<Camera3Device> parent = mParent.promote();
1842 if (parent != NULL) {
1843 va_list args;
1844 va_start(args, fmt);
1845
1846 parent->setErrorStateV(fmt, args);
1847
1848 va_end(args);
1849 }
1850}
1851
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001852status_t Camera3Device::RequestThread::insertTriggers(
1853 const sp<CaptureRequest> &request) {
1854
1855 Mutex::Autolock al(mTriggerMutex);
1856
1857 CameraMetadata &metadata = request->mSettings;
1858 size_t count = mTriggerMap.size();
1859
1860 for (size_t i = 0; i < count; ++i) {
1861 RequestTrigger trigger = mTriggerMap.valueAt(i);
1862
1863 uint32_t tag = trigger.metadataTag;
1864 camera_metadata_entry entry = metadata.find(tag);
1865
1866 if (entry.count > 0) {
1867 /**
1868 * Already has an entry for this trigger in the request.
1869 * Rewrite it with our requested trigger value.
1870 */
1871 RequestTrigger oldTrigger = trigger;
1872
1873 oldTrigger.entryValue = entry.data.u8[0];
1874
1875 mTriggerReplacedMap.add(tag, oldTrigger);
1876 } else {
1877 /**
1878 * More typical, no trigger entry, so we just add it
1879 */
1880 mTriggerRemovedMap.add(tag, trigger);
1881 }
1882
1883 status_t res;
1884
1885 switch (trigger.getTagType()) {
1886 case TYPE_BYTE: {
1887 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1888 res = metadata.update(tag,
1889 &entryValue,
1890 /*count*/1);
1891 break;
1892 }
1893 case TYPE_INT32:
1894 res = metadata.update(tag,
1895 &trigger.entryValue,
1896 /*count*/1);
1897 break;
1898 default:
1899 ALOGE("%s: Type not supported: 0x%x",
1900 __FUNCTION__,
1901 trigger.getTagType());
1902 return INVALID_OPERATION;
1903 }
1904
1905 if (res != OK) {
1906 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1907 ", value %d", __FUNCTION__, trigger.getTagName(),
1908 trigger.entryValue);
1909 return res;
1910 }
1911
1912 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1913 trigger.getTagName(),
1914 trigger.entryValue);
1915 }
1916
1917 mTriggerMap.clear();
1918
1919 return count;
1920}
1921
1922status_t Camera3Device::RequestThread::removeTriggers(
1923 const sp<CaptureRequest> &request) {
1924 Mutex::Autolock al(mTriggerMutex);
1925
1926 CameraMetadata &metadata = request->mSettings;
1927
1928 /**
1929 * Replace all old entries with their old values.
1930 */
1931 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1932 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1933
1934 status_t res;
1935
1936 uint32_t tag = trigger.metadataTag;
1937 switch (trigger.getTagType()) {
1938 case TYPE_BYTE: {
1939 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1940 res = metadata.update(tag,
1941 &entryValue,
1942 /*count*/1);
1943 break;
1944 }
1945 case TYPE_INT32:
1946 res = metadata.update(tag,
1947 &trigger.entryValue,
1948 /*count*/1);
1949 break;
1950 default:
1951 ALOGE("%s: Type not supported: 0x%x",
1952 __FUNCTION__,
1953 trigger.getTagType());
1954 return INVALID_OPERATION;
1955 }
1956
1957 if (res != OK) {
1958 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1959 ", trigger value %d", __FUNCTION__,
1960 trigger.getTagName(), trigger.entryValue);
1961 return res;
1962 }
1963 }
1964 mTriggerReplacedMap.clear();
1965
1966 /**
1967 * Remove all new entries.
1968 */
1969 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1970 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1971 status_t res = metadata.erase(trigger.metadataTag);
1972
1973 if (res != OK) {
1974 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1975 ", trigger value %d", __FUNCTION__,
1976 trigger.getTagName(), trigger.entryValue);
1977 return res;
1978 }
1979 }
1980 mTriggerRemovedMap.clear();
1981
1982 return OK;
1983}
1984
1985
1986
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001987/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001988 * Static callback forwarding methods from HAL to instance
1989 */
1990
1991void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1992 const camera3_capture_result *result) {
1993 Camera3Device *d =
1994 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1995 d->processCaptureResult(result);
1996}
1997
1998void Camera3Device::sNotify(const camera3_callback_ops *cb,
1999 const camera3_notify_msg *msg) {
2000 Camera3Device *d =
2001 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2002 d->notify(msg);
2003}
2004
2005}; // namespace android