blob: 6f4f4c85cdeaf3b6dbd5b84914aa00f507cb1e35 [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
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680 // CameraDevice semantics require device to already be idle before
681 // deleteStream is called, unlike for createStream.
682 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700683 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
684 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800685 }
686
Igor Murashkin2fba5842013-04-22 14:03:54 -0700687 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800688 if (mInputStream != NULL && id == mInputStream->getId()) {
689 deletedStream = mInputStream;
690 mInputStream.clear();
691 } else {
692 ssize_t idx = mOutputStreams.indexOfKey(id);
693 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700694 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695 return BAD_VALUE;
696 }
697 deletedStream = mOutputStreams.editValueAt(idx);
698 mOutputStreams.removeItem(id);
699 }
700
701 // Free up the stream endpoint so that it can be used by some other stream
702 res = deletedStream->disconnect();
703 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700704 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800705 // fall through since we want to still list the stream as deleted.
706 }
707 mDeletedStreams.add(deletedStream);
708
709 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800710}
711
712status_t Camera3Device::deleteReprocessStream(int id) {
713 ATRACE_CALL();
714 (void)id;
715
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700716 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800717 return INVALID_OPERATION;
718}
719
720
721status_t Camera3Device::createDefaultRequest(int templateId,
722 CameraMetadata *request) {
723 ATRACE_CALL();
724 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800725 Mutex::Autolock l(mLock);
726
727 switch (mStatus) {
728 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700729 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730 return INVALID_OPERATION;
731 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700732 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800733 return INVALID_OPERATION;
734 case STATUS_IDLE:
735 case STATUS_ACTIVE:
736 // OK
737 break;
738 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700739 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800740 return INVALID_OPERATION;
741 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800742
743 const camera_metadata_t *rawRequest;
744 rawRequest = mHal3Device->ops->construct_default_request_settings(
745 mHal3Device, templateId);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700746 if (rawRequest == NULL) {
747 SET_ERR_L("HAL is unable to construct default settings for template %d",
748 templateId);
749 return DEAD_OBJECT;
750 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800751 *request = rawRequest;
752
753 return OK;
754}
755
756status_t Camera3Device::waitUntilDrained() {
757 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800758 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 return waitUntilDrainedLocked();
761}
762
763status_t Camera3Device::waitUntilDrainedLocked() {
764 ATRACE_CALL();
765 status_t res;
766
767 switch (mStatus) {
768 case STATUS_UNINITIALIZED:
769 case STATUS_IDLE:
770 ALOGV("%s: Already idle", __FUNCTION__);
771 return OK;
772 case STATUS_ERROR:
773 case STATUS_ACTIVE:
774 // Need to shut down
775 break;
776 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700777 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 return INVALID_OPERATION;
779 }
780
781 if (mRequestThread != NULL) {
782 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
783 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700784 SET_ERR_L("Can't stop request thread in %f seconds!",
785 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800786 return res;
787 }
788 }
789 if (mInputStream != NULL) {
790 res = mInputStream->waitUntilIdle(kShutdownTimeout);
791 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700792 SET_ERR_L("Can't idle input stream %d in %f seconds!",
793 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800794 return res;
795 }
796 }
797 for (size_t i = 0; i < mOutputStreams.size(); i++) {
798 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
799 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700800 SET_ERR_L("Can't idle output stream %d in %f seconds!",
801 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802 return res;
803 }
804 }
805
806 if (mStatus != STATUS_ERROR) {
807 mStatus = STATUS_IDLE;
808 }
809
810 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800811}
812
813status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
814 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700815 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800816
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700817 if (listener != NULL && mListener != NULL) {
818 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
819 }
820 mListener = listener;
821
822 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800823}
824
825status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700826 ATRACE_CALL();
827 status_t res;
828 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800829
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700830 while (mResultQueue.empty()) {
831 res = mResultSignal.waitRelative(mOutputLock, timeout);
832 if (res == TIMED_OUT) {
833 return res;
834 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700835 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
836 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700837 return res;
838 }
839 }
840 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800841}
842
843status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
844 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700845 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700847 if (mResultQueue.empty()) {
848 return NOT_ENOUGH_DATA;
849 }
850
851 CameraMetadata &result = *(mResultQueue.begin());
852 frame->acquire(result);
853 mResultQueue.erase(mResultQueue.begin());
854
855 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800856}
857
858status_t Camera3Device::triggerAutofocus(uint32_t id) {
859 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700861 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
862 // Mix-in this trigger into the next request and only the next request.
863 RequestTrigger trigger[] = {
864 {
865 ANDROID_CONTROL_AF_TRIGGER,
866 ANDROID_CONTROL_AF_TRIGGER_START
867 },
868 {
869 ANDROID_CONTROL_AF_TRIGGER_ID,
870 static_cast<int32_t>(id)
871 },
872 };
873
874 return mRequestThread->queueTrigger(trigger,
875 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800876}
877
878status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
879 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800880
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700881 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
882 // Mix-in this trigger into the next request and only the next request.
883 RequestTrigger trigger[] = {
884 {
885 ANDROID_CONTROL_AF_TRIGGER,
886 ANDROID_CONTROL_AF_TRIGGER_CANCEL
887 },
888 {
889 ANDROID_CONTROL_AF_TRIGGER_ID,
890 static_cast<int32_t>(id)
891 },
892 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800893
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700894 return mRequestThread->queueTrigger(trigger,
895 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896}
897
898status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
899 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800900
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700901 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
902 // Mix-in this trigger into the next request and only the next request.
903 RequestTrigger trigger[] = {
904 {
905 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
906 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
907 },
908 {
909 ANDROID_CONTROL_AE_PRECAPTURE_ID,
910 static_cast<int32_t>(id)
911 },
912 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800913
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700914 return mRequestThread->queueTrigger(trigger,
915 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800916}
917
918status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
919 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
920 ATRACE_CALL();
921 (void)reprocessStreamId; (void)buffer; (void)listener;
922
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700923 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924 return INVALID_OPERATION;
925}
926
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927/**
928 * Camera3Device private methods
929 */
930
931sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
932 const CameraMetadata &request) {
933 ATRACE_CALL();
934 status_t res;
935
936 sp<CaptureRequest> newRequest = new CaptureRequest;
937 newRequest->mSettings = request;
938
939 camera_metadata_entry_t inputStreams =
940 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
941 if (inputStreams.count > 0) {
942 if (mInputStream == NULL ||
943 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700944 CLOGE("Request references unknown input stream %d",
945 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 return NULL;
947 }
948 // Lazy completion of stream configuration (allocation/registration)
949 // on first use
950 if (mInputStream->isConfiguring()) {
951 res = mInputStream->finishConfiguration(mHal3Device);
952 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700953 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700955 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 return NULL;
957 }
958 }
959
960 newRequest->mInputStream = mInputStream;
961 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
962 }
963
964 camera_metadata_entry_t streams =
965 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
966 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700967 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 return NULL;
969 }
970
971 for (size_t i = 0; i < streams.count; i++) {
972 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
973 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700974 CLOGE("Request references unknown stream %d",
975 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800976 return NULL;
977 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700978 sp<Camera3OutputStreamInterface> stream =
979 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980
981 // Lazy completion of stream configuration (allocation/registration)
982 // on first use
983 if (stream->isConfiguring()) {
984 res = stream->finishConfiguration(mHal3Device);
985 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
987 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800988 return NULL;
989 }
990 }
991
992 newRequest->mOutputStreams.push(stream);
993 }
994 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
995
996 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800997}
998
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999status_t Camera3Device::configureStreamsLocked() {
1000 ATRACE_CALL();
1001 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001004 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001005 return INVALID_OPERATION;
1006 }
1007
1008 // Start configuring the streams
1009
1010 camera3_stream_configuration config;
1011
1012 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1013
1014 Vector<camera3_stream_t*> streams;
1015 streams.setCapacity(config.num_streams);
1016
1017 if (mInputStream != NULL) {
1018 camera3_stream_t *inputStream;
1019 inputStream = mInputStream->startConfiguration();
1020 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001021 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 return INVALID_OPERATION;
1023 }
1024 streams.add(inputStream);
1025 }
1026
1027 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001028
1029 // Don't configure bidi streams twice, nor add them twice to the list
1030 if (mOutputStreams[i].get() ==
1031 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1032
1033 config.num_streams--;
1034 continue;
1035 }
1036
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001037 camera3_stream_t *outputStream;
1038 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1039 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001040 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 return INVALID_OPERATION;
1042 }
1043 streams.add(outputStream);
1044 }
1045
1046 config.streams = streams.editArray();
1047
1048 // Do the HAL configuration; will potentially touch stream
1049 // max_buffers, usage, priv fields.
1050
1051 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1052
1053 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001054 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1055 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001056 return res;
1057 }
1058
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001059 // Finish all stream configuration immediately.
1060 // TODO: Try to relax this later back to lazy completion, which should be
1061 // faster
1062
Igor Murashkin073f8572013-05-02 14:59:28 -07001063 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001064 res = mInputStream->finishConfiguration(mHal3Device);
1065 if (res != OK) {
1066 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1067 mInputStream->getId(), strerror(-res), res);
1068 return res;
1069 }
1070 }
1071
1072 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001073 sp<Camera3OutputStreamInterface> outputStream =
1074 mOutputStreams.editValueAt(i);
1075 if (outputStream->isConfiguring()) {
1076 res = outputStream->finishConfiguration(mHal3Device);
1077 if (res != OK) {
1078 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1079 outputStream->getId(), strerror(-res), res);
1080 return res;
1081 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001082 }
1083 }
1084
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 // Request thread needs to know to avoid using repeat-last-settings protocol
1086 // across configure_streams() calls
1087 mRequestThread->configurationComplete();
1088
1089 // Finish configuring the streams lazily on first reference
1090
1091 mStatus = STATUS_ACTIVE;
1092
1093 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001094}
1095
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096void Camera3Device::setErrorState(const char *fmt, ...) {
1097 Mutex::Autolock l(mLock);
1098 va_list args;
1099 va_start(args, fmt);
1100
1101 setErrorStateLockedV(fmt, args);
1102
1103 va_end(args);
1104}
1105
1106void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1107 Mutex::Autolock l(mLock);
1108 setErrorStateLockedV(fmt, args);
1109}
1110
1111void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1112 va_list args;
1113 va_start(args, fmt);
1114
1115 setErrorStateLockedV(fmt, args);
1116
1117 va_end(args);
1118}
1119
1120void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001121 // Print out all error messages to log
1122 String8 errorCause = String8::formatV(fmt, args);
1123 ALOGE("Camera %d: %s", mId, errorCause.string());
1124
1125 // But only do error state transition steps for the first error
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001126 if (mStatus == STATUS_ERROR) return;
1127
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001128 mErrorCause = errorCause;
1129
1130 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001131 mStatus = STATUS_ERROR;
1132}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001133
1134/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001135 * In-flight request management
1136 */
1137
1138status_t Camera3Device::registerInFlight(int32_t frameNumber,
1139 int32_t numBuffers) {
1140 ATRACE_CALL();
1141 Mutex::Autolock l(mInFlightLock);
1142
1143 ssize_t res;
1144 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1145 if (res < 0) return res;
1146
1147 return OK;
1148}
1149
1150/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 * Camera HAL device callback methods
1152 */
1153
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001155 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001156
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001157 status_t res;
1158
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001159 uint32_t frameNumber = result->frame_number;
1160 if (result->result == NULL && result->num_output_buffers == 0) {
1161 SET_ERR("No result data provided by HAL for frame %d",
1162 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001163 return;
1164 }
1165
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001166 // Get capture timestamp from list of in-flight requests, where it was added
1167 // by the shutter notification for this frame. Then update the in-flight
1168 // status and remove the in-flight entry if all result data has been
1169 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001170 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001171 {
1172 Mutex::Autolock l(mInFlightLock);
1173 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1174 if (idx == NAME_NOT_FOUND) {
1175 SET_ERR("Unknown frame number for capture result: %d",
1176 frameNumber);
1177 return;
1178 }
1179 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1180 timestamp = request.captureTimestamp;
1181 if (timestamp == 0) {
1182 SET_ERR("Called before shutter notify for frame %d",
1183 frameNumber);
1184 return;
1185 }
1186
1187 if (result->result != NULL) {
1188 if (request.haveResultMetadata) {
1189 SET_ERR("Called multiple times with metadata for frame %d",
1190 frameNumber);
1191 return;
1192 }
1193 request.haveResultMetadata = true;
1194 }
1195
1196 request.numBuffersLeft -= result->num_output_buffers;
1197
1198 if (request.numBuffersLeft < 0) {
1199 SET_ERR("Too many buffers returned for frame %d",
1200 frameNumber);
1201 return;
1202 }
1203
1204 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1205 mInFlightMap.removeItemsAt(idx, 1);
1206 }
1207
1208 // Sanity check - if we have too many in-flight frames, something has
1209 // likely gone wrong
1210 if (mInFlightMap.size() > kInFlightWarnLimit) {
1211 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1212 }
1213
1214 }
1215
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001216 AlgState cur3aState;
1217 AlgState new3aState;
1218 int32_t aeTriggerId = 0;
1219 int32_t afTriggerId = 0;
1220
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001221 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001222
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001223 // Process the result metadata, if provided
1224 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001225 Mutex::Autolock l(mOutputLock);
1226
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001227 if (frameNumber != mNextResultFrameNumber) {
1228 SET_ERR("Out-of-order capture result metadata submitted! "
1229 "(got frame number %d, expecting %d)",
1230 frameNumber, mNextResultFrameNumber);
1231 return;
1232 }
1233 mNextResultFrameNumber++;
1234
1235 CameraMetadata &captureResult =
1236 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001237
1238 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001239 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001240 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001241 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001242 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001243 } else {
1244 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001245 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001246 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001247
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001248 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001249
1250 camera_metadata_entry entry =
1251 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1252 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001253 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001254 frameNumber);
1255 }
1256 if (timestamp != entry.data.i64[0]) {
1257 SET_ERR("Timestamp mismatch between shutter notify and result"
1258 " metadata for frame %d (%lld vs %lld respectively)",
1259 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001260 }
1261
1262 // Get 3A states from result metadata
1263
1264 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1265 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001266 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001267 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001268 } else {
1269 new3aState.aeState =
1270 static_cast<camera_metadata_enum_android_control_ae_state>(
1271 entry.data.u8[0]);
1272 }
1273
1274 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1275 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001276 CLOGE("No AF state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001277 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001278 } else {
1279 new3aState.afState =
1280 static_cast<camera_metadata_enum_android_control_af_state>(
1281 entry.data.u8[0]);
1282 }
1283
1284 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1285 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001286 CLOGE("No AWB state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001287 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001288 } else {
1289 new3aState.awbState =
1290 static_cast<camera_metadata_enum_android_control_awb_state>(
1291 entry.data.u8[0]);
1292 }
1293
1294 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1295 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001296 CLOGE("No AF trigger ID provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001297 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001298 } else {
1299 afTriggerId = entry.data.i32[0];
1300 }
1301
1302 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1303 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001304 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001305 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001306 } else {
1307 aeTriggerId = entry.data.i32[0];
1308 }
1309
1310 listener = mListener;
1311 cur3aState = m3AState;
1312
1313 m3AState = new3aState;
1314 } // scope for mOutputLock
1315
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001316 // Return completed buffers to their streams with the timestamp
1317
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001318 for (size_t i = 0; i < result->num_output_buffers; i++) {
1319 Camera3Stream *stream =
1320 Camera3Stream::cast(result->output_buffers[i].stream);
1321 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1322 // Note: stream may be deallocated at this point, if this buffer was the
1323 // last reference to it.
1324 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001325 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001326 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001327 }
1328 }
1329
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001330 // Finally, dispatch any 3A change events to listeners if we got metadata
1331
1332 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001333 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001334 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001335 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001336 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1337 }
1338 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001339 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001340 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001341 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1342 }
1343 if (new3aState.awbState != cur3aState.awbState) {
1344 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1345 }
1346 }
1347
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348}
1349
1350void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001351 NotificationListener *listener;
1352 {
1353 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001354 listener = mListener;
1355 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001356
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001357 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001358 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001359 return;
1360 }
1361
1362 switch (msg->type) {
1363 case CAMERA3_MSG_ERROR: {
1364 int streamId = 0;
1365 if (msg->message.error.error_stream != NULL) {
1366 Camera3Stream *stream =
1367 Camera3Stream::cast(
1368 msg->message.error.error_stream);
1369 streamId = stream->getId();
1370 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001371 if (listener != NULL) {
1372 listener->notifyError(msg->message.error.error_code,
1373 msg->message.error.frame_number, streamId);
1374 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001375 break;
1376 }
1377 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 ssize_t idx;
1379 uint32_t frameNumber = msg->message.shutter.frame_number;
1380 nsecs_t timestamp = msg->message.shutter.timestamp;
1381 // Verify ordering of shutter notifications
1382 {
1383 Mutex::Autolock l(mOutputLock);
1384 if (frameNumber != mNextShutterFrameNumber) {
1385 SET_ERR("Shutter notification out-of-order. Expected "
1386 "notification for frame %d, got frame %d",
1387 mNextShutterFrameNumber, frameNumber);
1388 break;
1389 }
1390 mNextShutterFrameNumber++;
1391 }
1392
1393 // Set timestamp for the request in the in-flight tracking
1394 {
1395 Mutex::Autolock l(mInFlightLock);
1396 idx = mInFlightMap.indexOfKey(frameNumber);
1397 if (idx >= 0) {
1398 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1399 }
1400 }
1401 if (idx < 0) {
1402 SET_ERR("Shutter notification for non-existent frame number %d",
1403 frameNumber);
1404 break;
1405 }
1406
1407 // Call listener, if any
1408 if (listener != NULL) {
1409 listener->notifyShutter(frameNumber, timestamp);
1410 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001411 break;
1412 }
1413 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001414 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001415 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001416 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001417}
1418
1419/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001420 * RequestThread inner class methods
1421 */
1422
1423Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1424 camera3_device_t *hal3Device) :
1425 Thread(false),
1426 mParent(parent),
1427 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001428 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001429 mReconfigured(false),
1430 mDoPause(false),
1431 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001432 mFrameNumber(0),
1433 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434}
1435
1436void Camera3Device::RequestThread::configurationComplete() {
1437 Mutex::Autolock l(mRequestLock);
1438 mReconfigured = true;
1439}
1440
1441status_t Camera3Device::RequestThread::queueRequest(
1442 sp<CaptureRequest> request) {
1443 Mutex::Autolock l(mRequestLock);
1444 mRequestQueue.push_back(request);
1445
1446 return OK;
1447}
1448
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001449
1450status_t Camera3Device::RequestThread::queueTrigger(
1451 RequestTrigger trigger[],
1452 size_t count) {
1453
1454 Mutex::Autolock l(mTriggerMutex);
1455 status_t ret;
1456
1457 for (size_t i = 0; i < count; ++i) {
1458 ret = queueTriggerLocked(trigger[i]);
1459
1460 if (ret != OK) {
1461 return ret;
1462 }
1463 }
1464
1465 return OK;
1466}
1467
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001468int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1469 sp<Camera3Device> d = device.promote();
1470 if (d != NULL) return d->mId;
1471 return 0;
1472}
1473
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001474status_t Camera3Device::RequestThread::queueTriggerLocked(
1475 RequestTrigger trigger) {
1476
1477 uint32_t tag = trigger.metadataTag;
1478 ssize_t index = mTriggerMap.indexOfKey(tag);
1479
1480 switch (trigger.getTagType()) {
1481 case TYPE_BYTE:
1482 // fall-through
1483 case TYPE_INT32:
1484 break;
1485 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001486 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1487 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001488 return INVALID_OPERATION;
1489 }
1490
1491 /**
1492 * Collect only the latest trigger, since we only have 1 field
1493 * in the request settings per trigger tag, and can't send more than 1
1494 * trigger per request.
1495 */
1496 if (index != NAME_NOT_FOUND) {
1497 mTriggerMap.editValueAt(index) = trigger;
1498 } else {
1499 mTriggerMap.add(tag, trigger);
1500 }
1501
1502 return OK;
1503}
1504
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001505status_t Camera3Device::RequestThread::setRepeatingRequests(
1506 const RequestList &requests) {
1507 Mutex::Autolock l(mRequestLock);
1508 mRepeatingRequests.clear();
1509 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1510 requests.begin(), requests.end());
1511 return OK;
1512}
1513
1514status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1515 Mutex::Autolock l(mRequestLock);
1516 mRepeatingRequests.clear();
1517 return OK;
1518}
1519
1520void Camera3Device::RequestThread::setPaused(bool paused) {
1521 Mutex::Autolock l(mPauseLock);
1522 mDoPause = paused;
1523 mDoPauseSignal.signal();
1524}
1525
1526status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1527 status_t res;
1528 Mutex::Autolock l(mPauseLock);
1529 while (!mPaused) {
1530 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1531 if (res == TIMED_OUT) {
1532 return res;
1533 }
1534 }
1535 return OK;
1536}
1537
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001538status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1539 int32_t requestId, nsecs_t timeout) {
1540 Mutex::Autolock l(mLatestRequestMutex);
1541 status_t res;
1542 while (mLatestRequestId != requestId) {
1543 nsecs_t startTime = systemTime();
1544
1545 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1546 if (res != OK) return res;
1547
1548 timeout -= (systemTime() - startTime);
1549 }
1550
1551 return OK;
1552}
1553
1554
1555
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001556bool Camera3Device::RequestThread::threadLoop() {
1557
1558 status_t res;
1559
1560 // Handle paused state.
1561 if (waitIfPaused()) {
1562 return true;
1563 }
1564
1565 // Get work to do
1566
1567 sp<CaptureRequest> nextRequest = waitForNextRequest();
1568 if (nextRequest == NULL) {
1569 return true;
1570 }
1571
1572 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001574 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001575
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001576 // Insert any queued triggers (before metadata is locked)
1577 int32_t triggerCount;
1578 res = insertTriggers(nextRequest);
1579 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001580 SET_ERR("RequestThread: Unable to insert triggers "
1581 "(capture request %d, HAL device: %s (%d)",
1582 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001583 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1584 return false;
1585 }
1586 triggerCount = res;
1587
1588 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1589
1590 // If the request is the same as last, or we had triggers last time
1591 if (mPrevRequest != nextRequest || triggersMixedIn) {
1592 /**
1593 * The request should be presorted so accesses in HAL
1594 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1595 */
1596 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001597 request.settings = nextRequest->mSettings.getAndLock();
1598 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001599 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1600
1601 IF_ALOGV() {
1602 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1603 find_camera_metadata_ro_entry(
1604 request.settings,
1605 ANDROID_CONTROL_AF_TRIGGER,
1606 &e
1607 );
1608 if (e.count > 0) {
1609 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1610 __FUNCTION__,
1611 mFrameNumber+1,
1612 e.data.u8[0]);
1613 }
1614 }
1615 } else {
1616 // leave request.settings NULL to indicate 'reuse latest given'
1617 ALOGVV("%s: Request settings are REUSED",
1618 __FUNCTION__);
1619 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001620
1621 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001622
1623 // Fill in buffers
1624
1625 if (nextRequest->mInputStream != NULL) {
1626 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001627 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001629 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 " %s (%d)", strerror(-res), res);
1631 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1632 return true;
1633 }
1634 } else {
1635 request.input_buffer = NULL;
1636 }
1637
1638 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1639 nextRequest->mOutputStreams.size());
1640 request.output_buffers = outputBuffers.array();
1641 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1642 res = nextRequest->mOutputStreams.editItemAt(i)->
1643 getBuffer(&outputBuffers.editItemAt(i));
1644 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001645 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646 "%s (%d)", strerror(-res), res);
1647 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1648 return true;
1649 }
1650 request.num_output_buffers++;
1651 }
1652
1653 request.frame_number = mFrameNumber++;
1654
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001655 // Log request in the in-flight queue
1656 sp<Camera3Device> parent = mParent.promote();
1657 if (parent == NULL) {
1658 CLOGE("RequestThread: Parent is gone");
1659 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1660 return false;
1661 }
1662
1663 res = parent->registerInFlight(request.frame_number,
1664 request.num_output_buffers);
1665 if (res != OK) {
1666 SET_ERR("RequestThread: Unable to register new in-flight request:"
1667 " %s (%d)", strerror(-res), res);
1668 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1669 return false;
1670 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001672 // Submit request and block until ready for next one
1673
1674 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1675 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001676 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001677 " device: %s (%d)", request.frame_number, strerror(-res), res);
1678 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1679 return false;
1680 }
1681
1682 if (request.settings != NULL) {
1683 nextRequest->mSettings.unlock(request.settings);
1684 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001685
1686 // Remove any previously queued triggers (after unlock)
1687 res = removeTriggers(mPrevRequest);
1688 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001689 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001690 "(capture request %d, HAL device: %s (%d)",
1691 request.frame_number, strerror(-res), res);
1692 return false;
1693 }
1694 mPrevTriggers = triggerCount;
1695
1696 // Read android.request.id from the request settings metadata
1697 // - inform waitUntilRequestProcessed thread of a new request ID
1698 {
1699 Mutex::Autolock al(mLatestRequestMutex);
1700
1701 camera_metadata_entry_t requestIdEntry =
1702 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1703 if (requestIdEntry.count > 0) {
1704 mLatestRequestId = requestIdEntry.data.i32[0];
1705 } else {
1706 ALOGW("%s: Did not have android.request.id set in the request",
1707 __FUNCTION__);
1708 mLatestRequestId = NAME_NOT_FOUND;
1709 }
1710
1711 mLatestRequestSignal.signal();
1712 }
1713
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001714 // Return input buffer back to framework
1715 if (request.input_buffer != NULL) {
1716 Camera3Stream *stream =
1717 Camera3Stream::cast(request.input_buffer->stream);
1718 res = stream->returnInputBuffer(*(request.input_buffer));
1719 // Note: stream may be deallocated at this point, if this buffer was the
1720 // last reference to it.
1721 if (res != OK) {
1722 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1723 " its stream:%s (%d)", __FUNCTION__,
1724 request.frame_number, strerror(-res), res);
1725 // TODO: Report error upstream
1726 }
1727 }
1728
1729
1730
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001731 return true;
1732}
1733
1734void Camera3Device::RequestThread::cleanUpFailedRequest(
1735 camera3_capture_request_t &request,
1736 sp<CaptureRequest> &nextRequest,
1737 Vector<camera3_stream_buffer_t> &outputBuffers) {
1738
1739 if (request.settings != NULL) {
1740 nextRequest->mSettings.unlock(request.settings);
1741 }
1742 if (request.input_buffer != NULL) {
1743 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001744 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001745 }
1746 for (size_t i = 0; i < request.num_output_buffers; i++) {
1747 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1748 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1749 outputBuffers[i], 0);
1750 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001751}
1752
1753sp<Camera3Device::CaptureRequest>
1754 Camera3Device::RequestThread::waitForNextRequest() {
1755 status_t res;
1756 sp<CaptureRequest> nextRequest;
1757
1758 // Optimized a bit for the simple steady-state case (single repeating
1759 // request), to avoid putting that request in the queue temporarily.
1760 Mutex::Autolock l(mRequestLock);
1761
1762 while (mRequestQueue.empty()) {
1763 if (!mRepeatingRequests.empty()) {
1764 // Always atomically enqueue all requests in a repeating request
1765 // list. Guarantees a complete in-sequence set of captures to
1766 // application.
1767 const RequestList &requests = mRepeatingRequests;
1768 RequestList::const_iterator firstRequest =
1769 requests.begin();
1770 nextRequest = *firstRequest;
1771 mRequestQueue.insert(mRequestQueue.end(),
1772 ++firstRequest,
1773 requests.end());
1774 // No need to wait any longer
1775 break;
1776 }
1777
1778 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1779
1780 if (res == TIMED_OUT) {
1781 // Signal that we're paused by starvation
1782 Mutex::Autolock pl(mPauseLock);
1783 if (mPaused == false) {
1784 mPaused = true;
1785 mPausedSignal.signal();
1786 }
1787 // Stop waiting for now and let thread management happen
1788 return NULL;
1789 }
1790 }
1791
1792 if (nextRequest == NULL) {
1793 // Don't have a repeating request already in hand, so queue
1794 // must have an entry now.
1795 RequestList::iterator firstRequest =
1796 mRequestQueue.begin();
1797 nextRequest = *firstRequest;
1798 mRequestQueue.erase(firstRequest);
1799 }
1800
1801 // Not paused
1802 Mutex::Autolock pl(mPauseLock);
1803 mPaused = false;
1804
1805 // Check if we've reconfigured since last time, and reset the preview
1806 // request if so. Can't use 'NULL request == repeat' across configure calls.
1807 if (mReconfigured) {
1808 mPrevRequest.clear();
1809 mReconfigured = false;
1810 }
1811
1812 return nextRequest;
1813}
1814
1815bool Camera3Device::RequestThread::waitIfPaused() {
1816 status_t res;
1817 Mutex::Autolock l(mPauseLock);
1818 while (mDoPause) {
1819 // Signal that we're paused by request
1820 if (mPaused == false) {
1821 mPaused = true;
1822 mPausedSignal.signal();
1823 }
1824 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1825 if (res == TIMED_OUT) {
1826 return true;
1827 }
1828 }
1829 // We don't set mPaused to false here, because waitForNextRequest needs
1830 // to further manage the paused state in case of starvation.
1831 return false;
1832}
1833
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001834void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1835 sp<Camera3Device> parent = mParent.promote();
1836 if (parent != NULL) {
1837 va_list args;
1838 va_start(args, fmt);
1839
1840 parent->setErrorStateV(fmt, args);
1841
1842 va_end(args);
1843 }
1844}
1845
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001846status_t Camera3Device::RequestThread::insertTriggers(
1847 const sp<CaptureRequest> &request) {
1848
1849 Mutex::Autolock al(mTriggerMutex);
1850
1851 CameraMetadata &metadata = request->mSettings;
1852 size_t count = mTriggerMap.size();
1853
1854 for (size_t i = 0; i < count; ++i) {
1855 RequestTrigger trigger = mTriggerMap.valueAt(i);
1856
1857 uint32_t tag = trigger.metadataTag;
1858 camera_metadata_entry entry = metadata.find(tag);
1859
1860 if (entry.count > 0) {
1861 /**
1862 * Already has an entry for this trigger in the request.
1863 * Rewrite it with our requested trigger value.
1864 */
1865 RequestTrigger oldTrigger = trigger;
1866
1867 oldTrigger.entryValue = entry.data.u8[0];
1868
1869 mTriggerReplacedMap.add(tag, oldTrigger);
1870 } else {
1871 /**
1872 * More typical, no trigger entry, so we just add it
1873 */
1874 mTriggerRemovedMap.add(tag, trigger);
1875 }
1876
1877 status_t res;
1878
1879 switch (trigger.getTagType()) {
1880 case TYPE_BYTE: {
1881 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1882 res = metadata.update(tag,
1883 &entryValue,
1884 /*count*/1);
1885 break;
1886 }
1887 case TYPE_INT32:
1888 res = metadata.update(tag,
1889 &trigger.entryValue,
1890 /*count*/1);
1891 break;
1892 default:
1893 ALOGE("%s: Type not supported: 0x%x",
1894 __FUNCTION__,
1895 trigger.getTagType());
1896 return INVALID_OPERATION;
1897 }
1898
1899 if (res != OK) {
1900 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1901 ", value %d", __FUNCTION__, trigger.getTagName(),
1902 trigger.entryValue);
1903 return res;
1904 }
1905
1906 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1907 trigger.getTagName(),
1908 trigger.entryValue);
1909 }
1910
1911 mTriggerMap.clear();
1912
1913 return count;
1914}
1915
1916status_t Camera3Device::RequestThread::removeTriggers(
1917 const sp<CaptureRequest> &request) {
1918 Mutex::Autolock al(mTriggerMutex);
1919
1920 CameraMetadata &metadata = request->mSettings;
1921
1922 /**
1923 * Replace all old entries with their old values.
1924 */
1925 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1926 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1927
1928 status_t res;
1929
1930 uint32_t tag = trigger.metadataTag;
1931 switch (trigger.getTagType()) {
1932 case TYPE_BYTE: {
1933 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1934 res = metadata.update(tag,
1935 &entryValue,
1936 /*count*/1);
1937 break;
1938 }
1939 case TYPE_INT32:
1940 res = metadata.update(tag,
1941 &trigger.entryValue,
1942 /*count*/1);
1943 break;
1944 default:
1945 ALOGE("%s: Type not supported: 0x%x",
1946 __FUNCTION__,
1947 trigger.getTagType());
1948 return INVALID_OPERATION;
1949 }
1950
1951 if (res != OK) {
1952 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1953 ", trigger value %d", __FUNCTION__,
1954 trigger.getTagName(), trigger.entryValue);
1955 return res;
1956 }
1957 }
1958 mTriggerReplacedMap.clear();
1959
1960 /**
1961 * Remove all new entries.
1962 */
1963 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1964 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1965 status_t res = metadata.erase(trigger.metadataTag);
1966
1967 if (res != OK) {
1968 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1969 ", trigger value %d", __FUNCTION__,
1970 trigger.getTagName(), trigger.entryValue);
1971 return res;
1972 }
1973 }
1974 mTriggerRemovedMap.clear();
1975
1976 return OK;
1977}
1978
1979
1980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001982 * Static callback forwarding methods from HAL to instance
1983 */
1984
1985void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1986 const camera3_capture_result *result) {
1987 Camera3Device *d =
1988 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1989 d->processCaptureResult(result);
1990}
1991
1992void Camera3Device::sNotify(const camera3_callback_ops *cb,
1993 const camera3_notify_msg *msg) {
1994 Camera3Device *d =
1995 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1996 d->notify(msg);
1997}
1998
1999}; // namespace android