blob: 1d389936db1739371ac0580afffd1e1e65038d88 [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
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001332 if (result->result != NULL) {
1333 mResultSignal.signal();
1334 }
1335
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001336 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001337 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001338 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001339 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001340 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1341 }
1342 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001343 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001344 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001345 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1346 }
1347 if (new3aState.awbState != cur3aState.awbState) {
1348 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1349 }
1350 }
1351
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001352}
1353
1354void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001355 NotificationListener *listener;
1356 {
1357 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001358 listener = mListener;
1359 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001360
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001361 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001362 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001363 return;
1364 }
1365
1366 switch (msg->type) {
1367 case CAMERA3_MSG_ERROR: {
1368 int streamId = 0;
1369 if (msg->message.error.error_stream != NULL) {
1370 Camera3Stream *stream =
1371 Camera3Stream::cast(
1372 msg->message.error.error_stream);
1373 streamId = stream->getId();
1374 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001375 if (listener != NULL) {
1376 listener->notifyError(msg->message.error.error_code,
1377 msg->message.error.frame_number, streamId);
1378 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001379 break;
1380 }
1381 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001382 ssize_t idx;
1383 uint32_t frameNumber = msg->message.shutter.frame_number;
1384 nsecs_t timestamp = msg->message.shutter.timestamp;
1385 // Verify ordering of shutter notifications
1386 {
1387 Mutex::Autolock l(mOutputLock);
1388 if (frameNumber != mNextShutterFrameNumber) {
1389 SET_ERR("Shutter notification out-of-order. Expected "
1390 "notification for frame %d, got frame %d",
1391 mNextShutterFrameNumber, frameNumber);
1392 break;
1393 }
1394 mNextShutterFrameNumber++;
1395 }
1396
1397 // Set timestamp for the request in the in-flight tracking
1398 {
1399 Mutex::Autolock l(mInFlightLock);
1400 idx = mInFlightMap.indexOfKey(frameNumber);
1401 if (idx >= 0) {
1402 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1403 }
1404 }
1405 if (idx < 0) {
1406 SET_ERR("Shutter notification for non-existent frame number %d",
1407 frameNumber);
1408 break;
1409 }
1410
1411 // Call listener, if any
1412 if (listener != NULL) {
1413 listener->notifyShutter(frameNumber, timestamp);
1414 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001415 break;
1416 }
1417 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001418 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001419 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001420 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001421}
1422
1423/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001424 * RequestThread inner class methods
1425 */
1426
1427Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1428 camera3_device_t *hal3Device) :
1429 Thread(false),
1430 mParent(parent),
1431 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001432 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433 mReconfigured(false),
1434 mDoPause(false),
1435 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001436 mFrameNumber(0),
1437 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438}
1439
1440void Camera3Device::RequestThread::configurationComplete() {
1441 Mutex::Autolock l(mRequestLock);
1442 mReconfigured = true;
1443}
1444
1445status_t Camera3Device::RequestThread::queueRequest(
1446 sp<CaptureRequest> request) {
1447 Mutex::Autolock l(mRequestLock);
1448 mRequestQueue.push_back(request);
1449
1450 return OK;
1451}
1452
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001453
1454status_t Camera3Device::RequestThread::queueTrigger(
1455 RequestTrigger trigger[],
1456 size_t count) {
1457
1458 Mutex::Autolock l(mTriggerMutex);
1459 status_t ret;
1460
1461 for (size_t i = 0; i < count; ++i) {
1462 ret = queueTriggerLocked(trigger[i]);
1463
1464 if (ret != OK) {
1465 return ret;
1466 }
1467 }
1468
1469 return OK;
1470}
1471
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001472int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1473 sp<Camera3Device> d = device.promote();
1474 if (d != NULL) return d->mId;
1475 return 0;
1476}
1477
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001478status_t Camera3Device::RequestThread::queueTriggerLocked(
1479 RequestTrigger trigger) {
1480
1481 uint32_t tag = trigger.metadataTag;
1482 ssize_t index = mTriggerMap.indexOfKey(tag);
1483
1484 switch (trigger.getTagType()) {
1485 case TYPE_BYTE:
1486 // fall-through
1487 case TYPE_INT32:
1488 break;
1489 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001490 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1491 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001492 return INVALID_OPERATION;
1493 }
1494
1495 /**
1496 * Collect only the latest trigger, since we only have 1 field
1497 * in the request settings per trigger tag, and can't send more than 1
1498 * trigger per request.
1499 */
1500 if (index != NAME_NOT_FOUND) {
1501 mTriggerMap.editValueAt(index) = trigger;
1502 } else {
1503 mTriggerMap.add(tag, trigger);
1504 }
1505
1506 return OK;
1507}
1508
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001509status_t Camera3Device::RequestThread::setRepeatingRequests(
1510 const RequestList &requests) {
1511 Mutex::Autolock l(mRequestLock);
1512 mRepeatingRequests.clear();
1513 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1514 requests.begin(), requests.end());
1515 return OK;
1516}
1517
1518status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1519 Mutex::Autolock l(mRequestLock);
1520 mRepeatingRequests.clear();
1521 return OK;
1522}
1523
1524void Camera3Device::RequestThread::setPaused(bool paused) {
1525 Mutex::Autolock l(mPauseLock);
1526 mDoPause = paused;
1527 mDoPauseSignal.signal();
1528}
1529
1530status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1531 status_t res;
1532 Mutex::Autolock l(mPauseLock);
1533 while (!mPaused) {
1534 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1535 if (res == TIMED_OUT) {
1536 return res;
1537 }
1538 }
1539 return OK;
1540}
1541
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001542status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1543 int32_t requestId, nsecs_t timeout) {
1544 Mutex::Autolock l(mLatestRequestMutex);
1545 status_t res;
1546 while (mLatestRequestId != requestId) {
1547 nsecs_t startTime = systemTime();
1548
1549 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1550 if (res != OK) return res;
1551
1552 timeout -= (systemTime() - startTime);
1553 }
1554
1555 return OK;
1556}
1557
1558
1559
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001560bool Camera3Device::RequestThread::threadLoop() {
1561
1562 status_t res;
1563
1564 // Handle paused state.
1565 if (waitIfPaused()) {
1566 return true;
1567 }
1568
1569 // Get work to do
1570
1571 sp<CaptureRequest> nextRequest = waitForNextRequest();
1572 if (nextRequest == NULL) {
1573 return true;
1574 }
1575
1576 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001578 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001580 // Insert any queued triggers (before metadata is locked)
1581 int32_t triggerCount;
1582 res = insertTriggers(nextRequest);
1583 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001584 SET_ERR("RequestThread: Unable to insert triggers "
1585 "(capture request %d, HAL device: %s (%d)",
1586 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001587 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1588 return false;
1589 }
1590 triggerCount = res;
1591
1592 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1593
1594 // If the request is the same as last, or we had triggers last time
1595 if (mPrevRequest != nextRequest || triggersMixedIn) {
1596 /**
1597 * The request should be presorted so accesses in HAL
1598 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1599 */
1600 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001601 request.settings = nextRequest->mSettings.getAndLock();
1602 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001603 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1604
1605 IF_ALOGV() {
1606 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1607 find_camera_metadata_ro_entry(
1608 request.settings,
1609 ANDROID_CONTROL_AF_TRIGGER,
1610 &e
1611 );
1612 if (e.count > 0) {
1613 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1614 __FUNCTION__,
1615 mFrameNumber+1,
1616 e.data.u8[0]);
1617 }
1618 }
1619 } else {
1620 // leave request.settings NULL to indicate 'reuse latest given'
1621 ALOGVV("%s: Request settings are REUSED",
1622 __FUNCTION__);
1623 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001624
1625 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626
1627 // Fill in buffers
1628
1629 if (nextRequest->mInputStream != NULL) {
1630 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001631 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001633 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 " %s (%d)", strerror(-res), res);
1635 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1636 return true;
1637 }
1638 } else {
1639 request.input_buffer = NULL;
1640 }
1641
1642 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1643 nextRequest->mOutputStreams.size());
1644 request.output_buffers = outputBuffers.array();
1645 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1646 res = nextRequest->mOutputStreams.editItemAt(i)->
1647 getBuffer(&outputBuffers.editItemAt(i));
1648 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001649 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001650 "%s (%d)", strerror(-res), res);
1651 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1652 return true;
1653 }
1654 request.num_output_buffers++;
1655 }
1656
1657 request.frame_number = mFrameNumber++;
1658
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001659 // Log request in the in-flight queue
1660 sp<Camera3Device> parent = mParent.promote();
1661 if (parent == NULL) {
1662 CLOGE("RequestThread: Parent is gone");
1663 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1664 return false;
1665 }
1666
1667 res = parent->registerInFlight(request.frame_number,
1668 request.num_output_buffers);
1669 if (res != OK) {
1670 SET_ERR("RequestThread: Unable to register new in-flight request:"
1671 " %s (%d)", strerror(-res), res);
1672 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1673 return false;
1674 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001675
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001676 // Submit request and block until ready for next one
1677
1678 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1679 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001680 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001681 " device: %s (%d)", request.frame_number, strerror(-res), res);
1682 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1683 return false;
1684 }
1685
1686 if (request.settings != NULL) {
1687 nextRequest->mSettings.unlock(request.settings);
1688 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001689
1690 // Remove any previously queued triggers (after unlock)
1691 res = removeTriggers(mPrevRequest);
1692 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001693 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001694 "(capture request %d, HAL device: %s (%d)",
1695 request.frame_number, strerror(-res), res);
1696 return false;
1697 }
1698 mPrevTriggers = triggerCount;
1699
1700 // Read android.request.id from the request settings metadata
1701 // - inform waitUntilRequestProcessed thread of a new request ID
1702 {
1703 Mutex::Autolock al(mLatestRequestMutex);
1704
1705 camera_metadata_entry_t requestIdEntry =
1706 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1707 if (requestIdEntry.count > 0) {
1708 mLatestRequestId = requestIdEntry.data.i32[0];
1709 } else {
1710 ALOGW("%s: Did not have android.request.id set in the request",
1711 __FUNCTION__);
1712 mLatestRequestId = NAME_NOT_FOUND;
1713 }
1714
1715 mLatestRequestSignal.signal();
1716 }
1717
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001718 // Return input buffer back to framework
1719 if (request.input_buffer != NULL) {
1720 Camera3Stream *stream =
1721 Camera3Stream::cast(request.input_buffer->stream);
1722 res = stream->returnInputBuffer(*(request.input_buffer));
1723 // Note: stream may be deallocated at this point, if this buffer was the
1724 // last reference to it.
1725 if (res != OK) {
1726 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1727 " its stream:%s (%d)", __FUNCTION__,
1728 request.frame_number, strerror(-res), res);
1729 // TODO: Report error upstream
1730 }
1731 }
1732
1733
1734
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001735 return true;
1736}
1737
1738void Camera3Device::RequestThread::cleanUpFailedRequest(
1739 camera3_capture_request_t &request,
1740 sp<CaptureRequest> &nextRequest,
1741 Vector<camera3_stream_buffer_t> &outputBuffers) {
1742
1743 if (request.settings != NULL) {
1744 nextRequest->mSettings.unlock(request.settings);
1745 }
1746 if (request.input_buffer != NULL) {
1747 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001748 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001749 }
1750 for (size_t i = 0; i < request.num_output_buffers; i++) {
1751 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1752 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1753 outputBuffers[i], 0);
1754 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001755}
1756
1757sp<Camera3Device::CaptureRequest>
1758 Camera3Device::RequestThread::waitForNextRequest() {
1759 status_t res;
1760 sp<CaptureRequest> nextRequest;
1761
1762 // Optimized a bit for the simple steady-state case (single repeating
1763 // request), to avoid putting that request in the queue temporarily.
1764 Mutex::Autolock l(mRequestLock);
1765
1766 while (mRequestQueue.empty()) {
1767 if (!mRepeatingRequests.empty()) {
1768 // Always atomically enqueue all requests in a repeating request
1769 // list. Guarantees a complete in-sequence set of captures to
1770 // application.
1771 const RequestList &requests = mRepeatingRequests;
1772 RequestList::const_iterator firstRequest =
1773 requests.begin();
1774 nextRequest = *firstRequest;
1775 mRequestQueue.insert(mRequestQueue.end(),
1776 ++firstRequest,
1777 requests.end());
1778 // No need to wait any longer
1779 break;
1780 }
1781
1782 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1783
1784 if (res == TIMED_OUT) {
1785 // Signal that we're paused by starvation
1786 Mutex::Autolock pl(mPauseLock);
1787 if (mPaused == false) {
1788 mPaused = true;
1789 mPausedSignal.signal();
1790 }
1791 // Stop waiting for now and let thread management happen
1792 return NULL;
1793 }
1794 }
1795
1796 if (nextRequest == NULL) {
1797 // Don't have a repeating request already in hand, so queue
1798 // must have an entry now.
1799 RequestList::iterator firstRequest =
1800 mRequestQueue.begin();
1801 nextRequest = *firstRequest;
1802 mRequestQueue.erase(firstRequest);
1803 }
1804
1805 // Not paused
1806 Mutex::Autolock pl(mPauseLock);
1807 mPaused = false;
1808
1809 // Check if we've reconfigured since last time, and reset the preview
1810 // request if so. Can't use 'NULL request == repeat' across configure calls.
1811 if (mReconfigured) {
1812 mPrevRequest.clear();
1813 mReconfigured = false;
1814 }
1815
1816 return nextRequest;
1817}
1818
1819bool Camera3Device::RequestThread::waitIfPaused() {
1820 status_t res;
1821 Mutex::Autolock l(mPauseLock);
1822 while (mDoPause) {
1823 // Signal that we're paused by request
1824 if (mPaused == false) {
1825 mPaused = true;
1826 mPausedSignal.signal();
1827 }
1828 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1829 if (res == TIMED_OUT) {
1830 return true;
1831 }
1832 }
1833 // We don't set mPaused to false here, because waitForNextRequest needs
1834 // to further manage the paused state in case of starvation.
1835 return false;
1836}
1837
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001838void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1839 sp<Camera3Device> parent = mParent.promote();
1840 if (parent != NULL) {
1841 va_list args;
1842 va_start(args, fmt);
1843
1844 parent->setErrorStateV(fmt, args);
1845
1846 va_end(args);
1847 }
1848}
1849
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001850status_t Camera3Device::RequestThread::insertTriggers(
1851 const sp<CaptureRequest> &request) {
1852
1853 Mutex::Autolock al(mTriggerMutex);
1854
1855 CameraMetadata &metadata = request->mSettings;
1856 size_t count = mTriggerMap.size();
1857
1858 for (size_t i = 0; i < count; ++i) {
1859 RequestTrigger trigger = mTriggerMap.valueAt(i);
1860
1861 uint32_t tag = trigger.metadataTag;
1862 camera_metadata_entry entry = metadata.find(tag);
1863
1864 if (entry.count > 0) {
1865 /**
1866 * Already has an entry for this trigger in the request.
1867 * Rewrite it with our requested trigger value.
1868 */
1869 RequestTrigger oldTrigger = trigger;
1870
1871 oldTrigger.entryValue = entry.data.u8[0];
1872
1873 mTriggerReplacedMap.add(tag, oldTrigger);
1874 } else {
1875 /**
1876 * More typical, no trigger entry, so we just add it
1877 */
1878 mTriggerRemovedMap.add(tag, trigger);
1879 }
1880
1881 status_t res;
1882
1883 switch (trigger.getTagType()) {
1884 case TYPE_BYTE: {
1885 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1886 res = metadata.update(tag,
1887 &entryValue,
1888 /*count*/1);
1889 break;
1890 }
1891 case TYPE_INT32:
1892 res = metadata.update(tag,
1893 &trigger.entryValue,
1894 /*count*/1);
1895 break;
1896 default:
1897 ALOGE("%s: Type not supported: 0x%x",
1898 __FUNCTION__,
1899 trigger.getTagType());
1900 return INVALID_OPERATION;
1901 }
1902
1903 if (res != OK) {
1904 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1905 ", value %d", __FUNCTION__, trigger.getTagName(),
1906 trigger.entryValue);
1907 return res;
1908 }
1909
1910 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1911 trigger.getTagName(),
1912 trigger.entryValue);
1913 }
1914
1915 mTriggerMap.clear();
1916
1917 return count;
1918}
1919
1920status_t Camera3Device::RequestThread::removeTriggers(
1921 const sp<CaptureRequest> &request) {
1922 Mutex::Autolock al(mTriggerMutex);
1923
1924 CameraMetadata &metadata = request->mSettings;
1925
1926 /**
1927 * Replace all old entries with their old values.
1928 */
1929 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1930 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1931
1932 status_t res;
1933
1934 uint32_t tag = trigger.metadataTag;
1935 switch (trigger.getTagType()) {
1936 case TYPE_BYTE: {
1937 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1938 res = metadata.update(tag,
1939 &entryValue,
1940 /*count*/1);
1941 break;
1942 }
1943 case TYPE_INT32:
1944 res = metadata.update(tag,
1945 &trigger.entryValue,
1946 /*count*/1);
1947 break;
1948 default:
1949 ALOGE("%s: Type not supported: 0x%x",
1950 __FUNCTION__,
1951 trigger.getTagType());
1952 return INVALID_OPERATION;
1953 }
1954
1955 if (res != OK) {
1956 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1957 ", trigger value %d", __FUNCTION__,
1958 trigger.getTagName(), trigger.entryValue);
1959 return res;
1960 }
1961 }
1962 mTriggerReplacedMap.clear();
1963
1964 /**
1965 * Remove all new entries.
1966 */
1967 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1968 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1969 status_t res = metadata.erase(trigger.metadataTag);
1970
1971 if (res != OK) {
1972 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1973 ", trigger value %d", __FUNCTION__,
1974 trigger.getTagName(), trigger.entryValue);
1975 return res;
1976 }
1977 }
1978 mTriggerRemovedMap.clear();
1979
1980 return OK;
1981}
1982
1983
1984
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001985/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001986 * Static callback forwarding methods from HAL to instance
1987 */
1988
1989void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1990 const camera3_capture_result *result) {
1991 Camera3Device *d =
1992 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1993 d->processCaptureResult(result);
1994}
1995
1996void Camera3Device::sNotify(const camera3_callback_ops *cb,
1997 const camera3_notify_msg *msg) {
1998 Camera3Device *d =
1999 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2000 d->notify(msg);
2001}
2002
2003}; // namespace android