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