blob: cc7802bbf3ecd63e6ea835661f2fb9c7f0cb711e [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
43#include "Camera3Device.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080044#include "camera3/Camera3OutputStream.h"
Igor Murashkin5a269fa2013-04-15 14:59:22 -070045#include "camera3/Camera3InputStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080046
47using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080048
49namespace android {
50
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051Camera3Device::Camera3Device(int id):
52 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070054 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070055 mNextResultFrameNumber(0),
56 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058{
59 ATRACE_CALL();
60 camera3_callback_ops::notify = &sNotify;
61 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
62 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
63}
64
65Camera3Device::~Camera3Device()
66{
67 ATRACE_CALL();
68 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
69 disconnect();
70}
71
Igor Murashkin71381052013-03-04 14:53:08 -080072int Camera3Device::getId() const {
73 return mId;
74}
75
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080076/**
77 * CameraDeviceBase interface
78 */
79
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080status_t Camera3Device::initialize(camera_module_t *module)
81{
82 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080083 Mutex::Autolock l(mLock);
84
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070087 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 return INVALID_OPERATION;
89 }
90
91 /** Open HAL device */
92
93 status_t res;
94 String8 deviceName = String8::format("%d", mId);
95
96 camera3_device_t *device;
97
98 res = module->common.methods->open(&module->common, deviceName.string(),
99 reinterpret_cast<hw_device_t**>(&device));
100
101 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700102 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 return res;
104 }
105
106 /** Cross-check device version */
107
108 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 device->common.version);
113 device->common.close(&device->common);
114 return BAD_VALUE;
115 }
116
117 camera_info info;
118 res = module->get_camera_info(mId, &info);
119 if (res != OK) return res;
120
121 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
123 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version, info.device_version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 /** Initialize device with callback functions */
130
131 res = device->ops->initialize(device, this);
132 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700133 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
134 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 device->common.close(&device->common);
136 return BAD_VALUE;
137 }
138
139 /** Get vendor metadata tags */
140
141 mVendorTagOps.get_camera_vendor_section_name = NULL;
142
143 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
144
145 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
146 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
147 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700148 SET_ERR_L("Unable to set tag ops: %s (%d)",
149 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 device->common.close(&device->common);
151 return res;
152 }
153 }
154
155 /** Start up request queue thread */
156
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800157 mRequestThread = new RequestThread(this, device);
158 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800159 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700160 SET_ERR_L("Unable to start request queue thread: %s (%d)",
161 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800163 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 return res;
165 }
166
167 /** Everything is good to go */
168
169 mDeviceInfo = info.static_camera_characteristics;
170 mHal3Device = device;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800171 mStatus = STATUS_IDLE;
172 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700173 mNeedConfig = true;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174
175 return OK;
176}
177
178status_t Camera3Device::disconnect() {
179 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800180 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 ALOGV("%s: E", __FUNCTION__);
183
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700184 status_t res = OK;
185 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186
187 if (mStatus == STATUS_ACTIVE ||
188 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
189 res = mRequestThread->clearRepeatingRequests();
190 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700191 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700192 // Continue to close device even in case of error
193 } else {
194 res = waitUntilDrainedLocked();
195 if (res != OK) {
196 SET_ERR_L("Timeout waiting for HAL to drain");
197 // Continue to close device even in case of error
198 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800199 }
200 }
201 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
202
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700203 if (mStatus == STATUS_ERROR) {
204 CLOGE("Shutting down in an error state");
205 }
206
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800207 if (mRequestThread != NULL) {
208 mRequestThread->requestExit();
209 }
210
211 mOutputStreams.clear();
212 mInputStream.clear();
213
214 if (mRequestThread != NULL) {
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700215 if (mStatus != STATUS_ERROR) {
216 // HAL may be in a bad state, so waiting for request thread
217 // (which may be stuck in the HAL processCaptureRequest call)
218 // could be dangerous.
219 mRequestThread->join();
220 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800221 mRequestThread.clear();
222 }
223
224 if (mHal3Device != NULL) {
225 mHal3Device->common.close(&mHal3Device->common);
226 mHal3Device = NULL;
227 }
228
229 mStatus = STATUS_UNINITIALIZED;
230
231 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700232 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800233}
234
235status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
236 ATRACE_CALL();
237 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800238 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800240 const char *status =
241 mStatus == STATUS_ERROR ? "ERROR" :
242 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
243 mStatus == STATUS_IDLE ? "IDLE" :
244 mStatus == STATUS_ACTIVE ? "ACTIVE" :
245 "Unknown";
246 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700247 if (mStatus == STATUS_ERROR) {
248 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
249 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800250 lines.appendFormat(" Stream configuration:\n");
251
252 if (mInputStream != NULL) {
253 write(fd, lines.string(), lines.size());
254 mInputStream->dump(fd, args);
255 } else {
256 lines.appendFormat(" No input stream.\n");
257 write(fd, lines.string(), lines.size());
258 }
259 for (size_t i = 0; i < mOutputStreams.size(); i++) {
260 mOutputStreams[i]->dump(fd,args);
261 }
262
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700263 lines = String8(" In-flight requests:\n");
264 if (mInFlightMap.size() == 0) {
265 lines.append(" None\n");
266 } else {
267 for (size_t i = 0; i < mInFlightMap.size(); i++) {
268 InFlightRequest r = mInFlightMap.valueAt(i);
269 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
270 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
271 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
272 r.numBuffersLeft);
273 }
274 }
275 write(fd, lines.string(), lines.size());
276
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700278 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279 write(fd, lines.string(), lines.size());
280 mHal3Device->ops->dump(mHal3Device, fd);
281 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800282
283 return OK;
284}
285
286const CameraMetadata& Camera3Device::info() const {
287 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800288 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
289 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700290 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800291 mStatus == STATUS_ERROR ?
292 "when in error state" : "before init");
293 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800294 return mDeviceInfo;
295}
296
297status_t Camera3Device::capture(CameraMetadata &request) {
298 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800299 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700301 // TODO: take ownership of the request
302
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303 switch (mStatus) {
304 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700305 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 return INVALID_OPERATION;
307 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700308 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800309 return INVALID_OPERATION;
310 case STATUS_IDLE:
311 case STATUS_ACTIVE:
312 // OK
313 break;
314 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700315 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800316 return INVALID_OPERATION;
317 }
318
319 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
320 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700321 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800322 return BAD_VALUE;
323 }
324
325 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800326}
327
328
329status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
330 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800331 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800332
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333 switch (mStatus) {
334 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700335 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336 return INVALID_OPERATION;
337 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700338 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800339 return INVALID_OPERATION;
340 case STATUS_IDLE:
341 case STATUS_ACTIVE:
342 // OK
343 break;
344 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700345 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800346 return INVALID_OPERATION;
347 }
348
349 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
350 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700351 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800352 return BAD_VALUE;
353 }
354
355 RequestList newRepeatingRequests;
356 newRepeatingRequests.push_back(newRepeatingRequest);
357
358 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
359}
360
361
362sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
363 const CameraMetadata &request) {
364 status_t res;
365
366 if (mStatus == STATUS_IDLE) {
367 res = configureStreamsLocked();
368 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700369 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370 return NULL;
371 }
372 }
373
374 sp<CaptureRequest> newRequest = createCaptureRequest(request);
375 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800376}
377
378status_t Camera3Device::clearStreamingRequest() {
379 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800380 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800381
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800382 switch (mStatus) {
383 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700384 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 return INVALID_OPERATION;
386 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700387 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800388 return INVALID_OPERATION;
389 case STATUS_IDLE:
390 case STATUS_ACTIVE:
391 // OK
392 break;
393 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700394 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 return INVALID_OPERATION;
396 }
397
398 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800399}
400
401status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
402 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800403
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700404 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800405}
406
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700407status_t Camera3Device::createInputStream(
408 uint32_t width, uint32_t height, int format, int *id) {
409 ATRACE_CALL();
410 Mutex::Autolock l(mLock);
411
412 status_t res;
413 bool wasActive = false;
414
415 switch (mStatus) {
416 case STATUS_ERROR:
417 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
418 return INVALID_OPERATION;
419 case STATUS_UNINITIALIZED:
420 ALOGE("%s: Device not initialized", __FUNCTION__);
421 return INVALID_OPERATION;
422 case STATUS_IDLE:
423 // OK
424 break;
425 case STATUS_ACTIVE:
426 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
427 mRequestThread->setPaused(true);
428 res = waitUntilDrainedLocked();
429 if (res != OK) {
430 ALOGE("%s: Can't pause captures to reconfigure streams!",
431 __FUNCTION__);
432 mStatus = STATUS_ERROR;
433 return res;
434 }
435 wasActive = true;
436 break;
437 default:
438 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
439 return INVALID_OPERATION;
440 }
441 assert(mStatus == STATUS_IDLE);
442
443 if (mInputStream != 0) {
444 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
445 return INVALID_OPERATION;
446 }
447
448 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
449 width, height, format);
450
451 mInputStream = newStream;
452
453 *id = mNextStreamId++;
454
455 // Continue captures if active at start
456 if (wasActive) {
457 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
458 res = configureStreamsLocked();
459 if (res != OK) {
460 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
461 __FUNCTION__, mNextStreamId, strerror(-res), res);
462 return res;
463 }
464 mRequestThread->setPaused(false);
465 }
466
467 return OK;
468}
469
Igor Murashkin2fba5842013-04-22 14:03:54 -0700470
471status_t Camera3Device::createZslStream(
472 uint32_t width, uint32_t height,
473 int depth,
474 /*out*/
475 int *id,
476 sp<Camera3ZslStream>* zslStream) {
477 ATRACE_CALL();
478 Mutex::Autolock l(mLock);
479
480 status_t res;
481 bool wasActive = false;
482
483 switch (mStatus) {
484 case STATUS_ERROR:
485 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
486 return INVALID_OPERATION;
487 case STATUS_UNINITIALIZED:
488 ALOGE("%s: Device not initialized", __FUNCTION__);
489 return INVALID_OPERATION;
490 case STATUS_IDLE:
491 // OK
492 break;
493 case STATUS_ACTIVE:
494 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
495 mRequestThread->setPaused(true);
496 res = waitUntilDrainedLocked();
497 if (res != OK) {
498 ALOGE("%s: Can't pause captures to reconfigure streams!",
499 __FUNCTION__);
500 mStatus = STATUS_ERROR;
501 return res;
502 }
503 wasActive = true;
504 break;
505 default:
506 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
507 return INVALID_OPERATION;
508 }
509 assert(mStatus == STATUS_IDLE);
510
511 if (mInputStream != 0) {
512 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
513 return INVALID_OPERATION;
514 }
515
516 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
517 width, height, depth);
518
519 res = mOutputStreams.add(mNextStreamId, newStream);
520 if (res < 0) {
521 ALOGE("%s: Can't add new stream to set: %s (%d)",
522 __FUNCTION__, strerror(-res), res);
523 return res;
524 }
525 mInputStream = newStream;
526
527 *id = mNextStreamId++;
528 *zslStream = newStream;
529
530 // Continue captures if active at start
531 if (wasActive) {
532 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
533 res = configureStreamsLocked();
534 if (res != OK) {
535 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
536 __FUNCTION__, mNextStreamId, strerror(-res), res);
537 return res;
538 }
539 mRequestThread->setPaused(false);
540 }
541
542 return OK;
543}
544
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800545status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
546 uint32_t width, uint32_t height, int format, size_t size, int *id) {
547 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800548 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800549
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800550 status_t res;
551 bool wasActive = false;
552
553 switch (mStatus) {
554 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700555 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800556 return INVALID_OPERATION;
557 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700558 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800559 return INVALID_OPERATION;
560 case STATUS_IDLE:
561 // OK
562 break;
563 case STATUS_ACTIVE:
564 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
565 mRequestThread->setPaused(true);
566 res = waitUntilDrainedLocked();
567 if (res != OK) {
568 ALOGE("%s: Can't pause captures to reconfigure streams!",
569 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800570 return res;
571 }
572 wasActive = true;
573 break;
574 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700575 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576 return INVALID_OPERATION;
577 }
578 assert(mStatus == STATUS_IDLE);
579
580 sp<Camera3OutputStream> newStream;
581 if (format == HAL_PIXEL_FORMAT_BLOB) {
582 newStream = new Camera3OutputStream(mNextStreamId, consumer,
583 width, height, size, format);
584 } else {
585 newStream = new Camera3OutputStream(mNextStreamId, consumer,
586 width, height, format);
587 }
588
589 res = mOutputStreams.add(mNextStreamId, newStream);
590 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700591 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 return res;
593 }
594
595 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700596 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597
598 // Continue captures if active at start
599 if (wasActive) {
600 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
601 res = configureStreamsLocked();
602 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700603 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
604 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800605 return res;
606 }
607 mRequestThread->setPaused(false);
608 }
609
610 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800611}
612
613status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
614 ATRACE_CALL();
615 (void)outputId; (void)id;
616
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700617 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800618 return INVALID_OPERATION;
619}
620
621
622status_t Camera3Device::getStreamInfo(int id,
623 uint32_t *width, uint32_t *height, uint32_t *format) {
624 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800625 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800626
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800627 switch (mStatus) {
628 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700629 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 return INVALID_OPERATION;
631 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700632 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800633 return INVALID_OPERATION;
634 case STATUS_IDLE:
635 case STATUS_ACTIVE:
636 // OK
637 break;
638 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700639 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800640 return INVALID_OPERATION;
641 }
642
643 ssize_t idx = mOutputStreams.indexOfKey(id);
644 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700645 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 return idx;
647 }
648
649 if (width) *width = mOutputStreams[idx]->getWidth();
650 if (height) *height = mOutputStreams[idx]->getHeight();
651 if (format) *format = mOutputStreams[idx]->getFormat();
652
653 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800654}
655
656status_t Camera3Device::setStreamTransform(int id,
657 int transform) {
658 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800659 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800660
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800661 switch (mStatus) {
662 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700663 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 return INVALID_OPERATION;
665 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700666 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 return INVALID_OPERATION;
668 case STATUS_IDLE:
669 case STATUS_ACTIVE:
670 // OK
671 break;
672 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700673 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800674 return INVALID_OPERATION;
675 }
676
677 ssize_t idx = mOutputStreams.indexOfKey(id);
678 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700679 CLOGE("Stream %d does not exist",
680 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 return BAD_VALUE;
682 }
683
684 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800685}
686
687status_t Camera3Device::deleteStream(int id) {
688 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800689 Mutex::Autolock l(mLock);
690 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800691
Igor Murashkine2172be2013-05-28 15:31:39 -0700692 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
693
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 // CameraDevice semantics require device to already be idle before
695 // deleteStream is called, unlike for createStream.
696 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700697 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
698 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699 }
700
Igor Murashkin2fba5842013-04-22 14:03:54 -0700701 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800702 if (mInputStream != NULL && id == mInputStream->getId()) {
703 deletedStream = mInputStream;
704 mInputStream.clear();
705 } else {
706 ssize_t idx = mOutputStreams.indexOfKey(id);
707 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700708 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709 return BAD_VALUE;
710 }
711 deletedStream = mOutputStreams.editValueAt(idx);
712 mOutputStreams.removeItem(id);
713 }
714
715 // Free up the stream endpoint so that it can be used by some other stream
716 res = deletedStream->disconnect();
717 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700718 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800719 // fall through since we want to still list the stream as deleted.
720 }
721 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700722 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800723
724 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800725}
726
727status_t Camera3Device::deleteReprocessStream(int id) {
728 ATRACE_CALL();
729 (void)id;
730
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700731 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800732 return INVALID_OPERATION;
733}
734
735
736status_t Camera3Device::createDefaultRequest(int templateId,
737 CameraMetadata *request) {
738 ATRACE_CALL();
739 ALOGV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800740 Mutex::Autolock l(mLock);
741
742 switch (mStatus) {
743 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700744 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800745 return INVALID_OPERATION;
746 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700747 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800748 return INVALID_OPERATION;
749 case STATUS_IDLE:
750 case STATUS_ACTIVE:
751 // OK
752 break;
753 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700754 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755 return INVALID_OPERATION;
756 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800757
758 const camera_metadata_t *rawRequest;
759 rawRequest = mHal3Device->ops->construct_default_request_settings(
760 mHal3Device, templateId);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700761 if (rawRequest == NULL) {
762 SET_ERR_L("HAL is unable to construct default settings for template %d",
763 templateId);
764 return DEAD_OBJECT;
765 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800766 *request = rawRequest;
767
768 return OK;
769}
770
771status_t Camera3Device::waitUntilDrained() {
772 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800773 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800774
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800775 return waitUntilDrainedLocked();
776}
777
778status_t Camera3Device::waitUntilDrainedLocked() {
779 ATRACE_CALL();
780 status_t res;
781
782 switch (mStatus) {
783 case STATUS_UNINITIALIZED:
784 case STATUS_IDLE:
785 ALOGV("%s: Already idle", __FUNCTION__);
786 return OK;
787 case STATUS_ERROR:
788 case STATUS_ACTIVE:
789 // Need to shut down
790 break;
791 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700792 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800793 return INVALID_OPERATION;
794 }
795
796 if (mRequestThread != NULL) {
797 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
798 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700799 SET_ERR_L("Can't stop request thread in %f seconds!",
800 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800801 return res;
802 }
803 }
804 if (mInputStream != NULL) {
805 res = mInputStream->waitUntilIdle(kShutdownTimeout);
806 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700807 SET_ERR_L("Can't idle input stream %d in %f seconds!",
808 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 return res;
810 }
811 }
812 for (size_t i = 0; i < mOutputStreams.size(); i++) {
813 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
814 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700815 SET_ERR_L("Can't idle output stream %d in %f seconds!",
816 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 return res;
818 }
819 }
820
821 if (mStatus != STATUS_ERROR) {
822 mStatus = STATUS_IDLE;
823 }
824
825 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800826}
827
828status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
829 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700830 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700832 if (listener != NULL && mListener != NULL) {
833 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
834 }
835 mListener = listener;
836
837 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800838}
839
840status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700841 ATRACE_CALL();
842 status_t res;
843 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700845 while (mResultQueue.empty()) {
846 res = mResultSignal.waitRelative(mOutputLock, timeout);
847 if (res == TIMED_OUT) {
848 return res;
849 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700850 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
851 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700852 return res;
853 }
854 }
855 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800856}
857
858status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
859 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700860 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800861
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700862 if (mResultQueue.empty()) {
863 return NOT_ENOUGH_DATA;
864 }
865
866 CameraMetadata &result = *(mResultQueue.begin());
867 frame->acquire(result);
868 mResultQueue.erase(mResultQueue.begin());
869
870 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800871}
872
873status_t Camera3Device::triggerAutofocus(uint32_t id) {
874 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800875
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700876 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
877 // Mix-in this trigger into the next request and only the next request.
878 RequestTrigger trigger[] = {
879 {
880 ANDROID_CONTROL_AF_TRIGGER,
881 ANDROID_CONTROL_AF_TRIGGER_START
882 },
883 {
884 ANDROID_CONTROL_AF_TRIGGER_ID,
885 static_cast<int32_t>(id)
886 },
887 };
888
889 return mRequestThread->queueTrigger(trigger,
890 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800891}
892
893status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
894 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700896 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
897 // Mix-in this trigger into the next request and only the next request.
898 RequestTrigger trigger[] = {
899 {
900 ANDROID_CONTROL_AF_TRIGGER,
901 ANDROID_CONTROL_AF_TRIGGER_CANCEL
902 },
903 {
904 ANDROID_CONTROL_AF_TRIGGER_ID,
905 static_cast<int32_t>(id)
906 },
907 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800908
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700909 return mRequestThread->queueTrigger(trigger,
910 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911}
912
913status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
914 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700916 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
917 // Mix-in this trigger into the next request and only the next request.
918 RequestTrigger trigger[] = {
919 {
920 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
921 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
922 },
923 {
924 ANDROID_CONTROL_AE_PRECAPTURE_ID,
925 static_cast<int32_t>(id)
926 },
927 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800928
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700929 return mRequestThread->queueTrigger(trigger,
930 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800931}
932
933status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
934 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
935 ATRACE_CALL();
936 (void)reprocessStreamId; (void)buffer; (void)listener;
937
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700938 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800939 return INVALID_OPERATION;
940}
941
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942/**
943 * Camera3Device private methods
944 */
945
946sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
947 const CameraMetadata &request) {
948 ATRACE_CALL();
949 status_t res;
950
951 sp<CaptureRequest> newRequest = new CaptureRequest;
952 newRequest->mSettings = request;
953
954 camera_metadata_entry_t inputStreams =
955 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
956 if (inputStreams.count > 0) {
957 if (mInputStream == NULL ||
958 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700959 CLOGE("Request references unknown input stream %d",
960 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800961 return NULL;
962 }
963 // Lazy completion of stream configuration (allocation/registration)
964 // on first use
965 if (mInputStream->isConfiguring()) {
966 res = mInputStream->finishConfiguration(mHal3Device);
967 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700968 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700970 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800971 return NULL;
972 }
973 }
974
975 newRequest->mInputStream = mInputStream;
976 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
977 }
978
979 camera_metadata_entry_t streams =
980 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
981 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700982 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 return NULL;
984 }
985
986 for (size_t i = 0; i < streams.count; i++) {
987 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
988 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700989 CLOGE("Request references unknown stream %d",
990 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 return NULL;
992 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700993 sp<Camera3OutputStreamInterface> stream =
994 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995
996 // Lazy completion of stream configuration (allocation/registration)
997 // on first use
998 if (stream->isConfiguring()) {
999 res = stream->finishConfiguration(mHal3Device);
1000 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001001 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1002 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001003 return NULL;
1004 }
1005 }
1006
1007 newRequest->mOutputStreams.push(stream);
1008 }
1009 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1010
1011 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001012}
1013
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014status_t Camera3Device::configureStreamsLocked() {
1015 ATRACE_CALL();
1016 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001019 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 return INVALID_OPERATION;
1021 }
1022
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001023 if (!mNeedConfig) {
1024 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001025 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001026 return OK;
1027 }
1028
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029 // Start configuring the streams
1030
1031 camera3_stream_configuration config;
1032
1033 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1034
1035 Vector<camera3_stream_t*> streams;
1036 streams.setCapacity(config.num_streams);
1037
1038 if (mInputStream != NULL) {
1039 camera3_stream_t *inputStream;
1040 inputStream = mInputStream->startConfiguration();
1041 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
1044 }
1045 streams.add(inputStream);
1046 }
1047
1048 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001049
1050 // Don't configure bidi streams twice, nor add them twice to the list
1051 if (mOutputStreams[i].get() ==
1052 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1053
1054 config.num_streams--;
1055 continue;
1056 }
1057
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001058 camera3_stream_t *outputStream;
1059 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1060 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001061 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 return INVALID_OPERATION;
1063 }
1064 streams.add(outputStream);
1065 }
1066
1067 config.streams = streams.editArray();
1068
1069 // Do the HAL configuration; will potentially touch stream
1070 // max_buffers, usage, priv fields.
1071
1072 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1073
1074 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1076 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 return res;
1078 }
1079
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001080 // Finish all stream configuration immediately.
1081 // TODO: Try to relax this later back to lazy completion, which should be
1082 // faster
1083
Igor Murashkin073f8572013-05-02 14:59:28 -07001084 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001085 res = mInputStream->finishConfiguration(mHal3Device);
1086 if (res != OK) {
1087 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1088 mInputStream->getId(), strerror(-res), res);
1089 return res;
1090 }
1091 }
1092
1093 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001094 sp<Camera3OutputStreamInterface> outputStream =
1095 mOutputStreams.editValueAt(i);
1096 if (outputStream->isConfiguring()) {
1097 res = outputStream->finishConfiguration(mHal3Device);
1098 if (res != OK) {
1099 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1100 outputStream->getId(), strerror(-res), res);
1101 return res;
1102 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001103 }
1104 }
1105
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001106 // Request thread needs to know to avoid using repeat-last-settings protocol
1107 // across configure_streams() calls
1108 mRequestThread->configurationComplete();
1109
1110 // Finish configuring the streams lazily on first reference
1111
1112 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001113 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001114
1115 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001116}
1117
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001118void Camera3Device::setErrorState(const char *fmt, ...) {
1119 Mutex::Autolock l(mLock);
1120 va_list args;
1121 va_start(args, fmt);
1122
1123 setErrorStateLockedV(fmt, args);
1124
1125 va_end(args);
1126}
1127
1128void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1129 Mutex::Autolock l(mLock);
1130 setErrorStateLockedV(fmt, args);
1131}
1132
1133void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1134 va_list args;
1135 va_start(args, fmt);
1136
1137 setErrorStateLockedV(fmt, args);
1138
1139 va_end(args);
1140}
1141
1142void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001143 // Print out all error messages to log
1144 String8 errorCause = String8::formatV(fmt, args);
1145 ALOGE("Camera %d: %s", mId, errorCause.string());
1146
1147 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001148 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001149
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001150 mErrorCause = errorCause;
1151
1152 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001153 mStatus = STATUS_ERROR;
1154}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001155
1156/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001157 * In-flight request management
1158 */
1159
1160status_t Camera3Device::registerInFlight(int32_t frameNumber,
1161 int32_t numBuffers) {
1162 ATRACE_CALL();
1163 Mutex::Autolock l(mInFlightLock);
1164
1165 ssize_t res;
1166 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1167 if (res < 0) return res;
1168
1169 return OK;
1170}
1171
1172/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001173 * Camera HAL device callback methods
1174 */
1175
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001176void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001177 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001178
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001179 status_t res;
1180
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001181 uint32_t frameNumber = result->frame_number;
1182 if (result->result == NULL && result->num_output_buffers == 0) {
1183 SET_ERR("No result data provided by HAL for frame %d",
1184 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001185 return;
1186 }
1187
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001188 // Get capture timestamp from list of in-flight requests, where it was added
1189 // by the shutter notification for this frame. Then update the in-flight
1190 // status and remove the in-flight entry if all result data has been
1191 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001192 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001193 {
1194 Mutex::Autolock l(mInFlightLock);
1195 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1196 if (idx == NAME_NOT_FOUND) {
1197 SET_ERR("Unknown frame number for capture result: %d",
1198 frameNumber);
1199 return;
1200 }
1201 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1202 timestamp = request.captureTimestamp;
1203 if (timestamp == 0) {
1204 SET_ERR("Called before shutter notify for frame %d",
1205 frameNumber);
1206 return;
1207 }
1208
1209 if (result->result != NULL) {
1210 if (request.haveResultMetadata) {
1211 SET_ERR("Called multiple times with metadata for frame %d",
1212 frameNumber);
1213 return;
1214 }
1215 request.haveResultMetadata = true;
1216 }
1217
1218 request.numBuffersLeft -= result->num_output_buffers;
1219
1220 if (request.numBuffersLeft < 0) {
1221 SET_ERR("Too many buffers returned for frame %d",
1222 frameNumber);
1223 return;
1224 }
1225
1226 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1227 mInFlightMap.removeItemsAt(idx, 1);
1228 }
1229
1230 // Sanity check - if we have too many in-flight frames, something has
1231 // likely gone wrong
1232 if (mInFlightMap.size() > kInFlightWarnLimit) {
1233 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1234 }
1235
1236 }
1237
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001238 AlgState cur3aState;
1239 AlgState new3aState;
1240 int32_t aeTriggerId = 0;
1241 int32_t afTriggerId = 0;
1242
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001243 NotificationListener *listener = NULL;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001244
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001245 // Process the result metadata, if provided
1246 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001247 Mutex::Autolock l(mOutputLock);
1248
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001249 if (frameNumber != mNextResultFrameNumber) {
1250 SET_ERR("Out-of-order capture result metadata submitted! "
1251 "(got frame number %d, expecting %d)",
1252 frameNumber, mNextResultFrameNumber);
1253 return;
1254 }
1255 mNextResultFrameNumber++;
1256
1257 CameraMetadata &captureResult =
1258 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001259
1260 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001261 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001262 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001263 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001264 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001265 } else {
1266 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001267 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001268 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001269
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001270 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001271
1272 camera_metadata_entry entry =
1273 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1274 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001275 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001276 frameNumber);
1277 }
1278 if (timestamp != entry.data.i64[0]) {
1279 SET_ERR("Timestamp mismatch between shutter notify and result"
1280 " metadata for frame %d (%lld vs %lld respectively)",
1281 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001282 }
1283
1284 // Get 3A states from result metadata
1285
1286 entry = captureResult.find(ANDROID_CONTROL_AE_STATE);
1287 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001288 CLOGE("No AE state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001289 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001290 } else {
1291 new3aState.aeState =
1292 static_cast<camera_metadata_enum_android_control_ae_state>(
1293 entry.data.u8[0]);
1294 }
1295
1296 entry = captureResult.find(ANDROID_CONTROL_AF_STATE);
1297 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001298 CLOGE("No AF state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001299 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001300 } else {
1301 new3aState.afState =
1302 static_cast<camera_metadata_enum_android_control_af_state>(
1303 entry.data.u8[0]);
1304 }
1305
1306 entry = captureResult.find(ANDROID_CONTROL_AWB_STATE);
1307 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001308 CLOGE("No AWB state provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001309 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001310 } else {
1311 new3aState.awbState =
1312 static_cast<camera_metadata_enum_android_control_awb_state>(
1313 entry.data.u8[0]);
1314 }
1315
1316 entry = captureResult.find(ANDROID_CONTROL_AF_TRIGGER_ID);
1317 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001318 CLOGE("No AF trigger ID provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001319 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001320 } else {
1321 afTriggerId = entry.data.i32[0];
1322 }
1323
1324 entry = captureResult.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
1325 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001326 CLOGE("No AE precapture trigger ID provided by HAL"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001327 " for frame %d!", frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001328 } else {
1329 aeTriggerId = entry.data.i32[0];
1330 }
1331
1332 listener = mListener;
1333 cur3aState = m3AState;
1334
1335 m3AState = new3aState;
1336 } // scope for mOutputLock
1337
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001338 // Return completed buffers to their streams with the timestamp
1339
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001340 for (size_t i = 0; i < result->num_output_buffers; i++) {
1341 Camera3Stream *stream =
1342 Camera3Stream::cast(result->output_buffers[i].stream);
1343 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1344 // Note: stream may be deallocated at this point, if this buffer was the
1345 // last reference to it.
1346 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001347 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001348 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001349 }
1350 }
1351
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001352 // Finally, dispatch any 3A change events to listeners if we got metadata
1353
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001354 if (result->result != NULL) {
1355 mResultSignal.signal();
1356 }
1357
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001358 if (result->result != NULL && listener != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001359 if (new3aState.aeState != cur3aState.aeState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001360 ALOGVV("%s: AE state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001361 __FUNCTION__, cur3aState.aeState, new3aState.aeState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001362 listener->notifyAutoExposure(new3aState.aeState, aeTriggerId);
1363 }
1364 if (new3aState.afState != cur3aState.afState) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001365 ALOGVV("%s: AF state changed from 0x%x to 0x%x",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001366 __FUNCTION__, cur3aState.afState, new3aState.afState);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001367 listener->notifyAutoFocus(new3aState.afState, afTriggerId);
1368 }
1369 if (new3aState.awbState != cur3aState.awbState) {
1370 listener->notifyAutoWhitebalance(new3aState.awbState, aeTriggerId);
1371 }
1372 }
1373
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001374}
1375
1376void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001377 NotificationListener *listener;
1378 {
1379 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001380 listener = mListener;
1381 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001382
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001383 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001384 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001385 return;
1386 }
1387
1388 switch (msg->type) {
1389 case CAMERA3_MSG_ERROR: {
1390 int streamId = 0;
1391 if (msg->message.error.error_stream != NULL) {
1392 Camera3Stream *stream =
1393 Camera3Stream::cast(
1394 msg->message.error.error_stream);
1395 streamId = stream->getId();
1396 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001397 if (listener != NULL) {
1398 listener->notifyError(msg->message.error.error_code,
1399 msg->message.error.frame_number, streamId);
1400 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001401 break;
1402 }
1403 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001404 ssize_t idx;
1405 uint32_t frameNumber = msg->message.shutter.frame_number;
1406 nsecs_t timestamp = msg->message.shutter.timestamp;
1407 // Verify ordering of shutter notifications
1408 {
1409 Mutex::Autolock l(mOutputLock);
1410 if (frameNumber != mNextShutterFrameNumber) {
1411 SET_ERR("Shutter notification out-of-order. Expected "
1412 "notification for frame %d, got frame %d",
1413 mNextShutterFrameNumber, frameNumber);
1414 break;
1415 }
1416 mNextShutterFrameNumber++;
1417 }
1418
1419 // Set timestamp for the request in the in-flight tracking
1420 {
1421 Mutex::Autolock l(mInFlightLock);
1422 idx = mInFlightMap.indexOfKey(frameNumber);
1423 if (idx >= 0) {
1424 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1425 }
1426 }
1427 if (idx < 0) {
1428 SET_ERR("Shutter notification for non-existent frame number %d",
1429 frameNumber);
1430 break;
1431 }
1432
1433 // Call listener, if any
1434 if (listener != NULL) {
1435 listener->notifyShutter(frameNumber, timestamp);
1436 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001437 break;
1438 }
1439 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001440 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001441 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001442 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001443}
1444
1445/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001446 * RequestThread inner class methods
1447 */
1448
1449Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1450 camera3_device_t *hal3Device) :
1451 Thread(false),
1452 mParent(parent),
1453 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001454 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001455 mReconfigured(false),
1456 mDoPause(false),
1457 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001458 mFrameNumber(0),
1459 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460}
1461
1462void Camera3Device::RequestThread::configurationComplete() {
1463 Mutex::Autolock l(mRequestLock);
1464 mReconfigured = true;
1465}
1466
1467status_t Camera3Device::RequestThread::queueRequest(
1468 sp<CaptureRequest> request) {
1469 Mutex::Autolock l(mRequestLock);
1470 mRequestQueue.push_back(request);
1471
1472 return OK;
1473}
1474
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001475
1476status_t Camera3Device::RequestThread::queueTrigger(
1477 RequestTrigger trigger[],
1478 size_t count) {
1479
1480 Mutex::Autolock l(mTriggerMutex);
1481 status_t ret;
1482
1483 for (size_t i = 0; i < count; ++i) {
1484 ret = queueTriggerLocked(trigger[i]);
1485
1486 if (ret != OK) {
1487 return ret;
1488 }
1489 }
1490
1491 return OK;
1492}
1493
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001494int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1495 sp<Camera3Device> d = device.promote();
1496 if (d != NULL) return d->mId;
1497 return 0;
1498}
1499
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001500status_t Camera3Device::RequestThread::queueTriggerLocked(
1501 RequestTrigger trigger) {
1502
1503 uint32_t tag = trigger.metadataTag;
1504 ssize_t index = mTriggerMap.indexOfKey(tag);
1505
1506 switch (trigger.getTagType()) {
1507 case TYPE_BYTE:
1508 // fall-through
1509 case TYPE_INT32:
1510 break;
1511 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001512 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1513 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001514 return INVALID_OPERATION;
1515 }
1516
1517 /**
1518 * Collect only the latest trigger, since we only have 1 field
1519 * in the request settings per trigger tag, and can't send more than 1
1520 * trigger per request.
1521 */
1522 if (index != NAME_NOT_FOUND) {
1523 mTriggerMap.editValueAt(index) = trigger;
1524 } else {
1525 mTriggerMap.add(tag, trigger);
1526 }
1527
1528 return OK;
1529}
1530
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001531status_t Camera3Device::RequestThread::setRepeatingRequests(
1532 const RequestList &requests) {
1533 Mutex::Autolock l(mRequestLock);
1534 mRepeatingRequests.clear();
1535 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1536 requests.begin(), requests.end());
1537 return OK;
1538}
1539
1540status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1541 Mutex::Autolock l(mRequestLock);
1542 mRepeatingRequests.clear();
1543 return OK;
1544}
1545
1546void Camera3Device::RequestThread::setPaused(bool paused) {
1547 Mutex::Autolock l(mPauseLock);
1548 mDoPause = paused;
1549 mDoPauseSignal.signal();
1550}
1551
1552status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1553 status_t res;
1554 Mutex::Autolock l(mPauseLock);
1555 while (!mPaused) {
1556 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1557 if (res == TIMED_OUT) {
1558 return res;
1559 }
1560 }
1561 return OK;
1562}
1563
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001564status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1565 int32_t requestId, nsecs_t timeout) {
1566 Mutex::Autolock l(mLatestRequestMutex);
1567 status_t res;
1568 while (mLatestRequestId != requestId) {
1569 nsecs_t startTime = systemTime();
1570
1571 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1572 if (res != OK) return res;
1573
1574 timeout -= (systemTime() - startTime);
1575 }
1576
1577 return OK;
1578}
1579
1580
1581
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001582bool Camera3Device::RequestThread::threadLoop() {
1583
1584 status_t res;
1585
1586 // Handle paused state.
1587 if (waitIfPaused()) {
1588 return true;
1589 }
1590
1591 // Get work to do
1592
1593 sp<CaptureRequest> nextRequest = waitForNextRequest();
1594 if (nextRequest == NULL) {
1595 return true;
1596 }
1597
1598 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001599 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001600 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001601
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001602 // Insert any queued triggers (before metadata is locked)
1603 int32_t triggerCount;
1604 res = insertTriggers(nextRequest);
1605 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001606 SET_ERR("RequestThread: Unable to insert triggers "
1607 "(capture request %d, HAL device: %s (%d)",
1608 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001609 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1610 return false;
1611 }
1612 triggerCount = res;
1613
1614 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1615
1616 // If the request is the same as last, or we had triggers last time
1617 if (mPrevRequest != nextRequest || triggersMixedIn) {
1618 /**
1619 * The request should be presorted so accesses in HAL
1620 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1621 */
1622 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001623 request.settings = nextRequest->mSettings.getAndLock();
1624 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001625 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1626
1627 IF_ALOGV() {
1628 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1629 find_camera_metadata_ro_entry(
1630 request.settings,
1631 ANDROID_CONTROL_AF_TRIGGER,
1632 &e
1633 );
1634 if (e.count > 0) {
1635 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1636 __FUNCTION__,
1637 mFrameNumber+1,
1638 e.data.u8[0]);
1639 }
1640 }
1641 } else {
1642 // leave request.settings NULL to indicate 'reuse latest given'
1643 ALOGVV("%s: Request settings are REUSED",
1644 __FUNCTION__);
1645 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001646
1647 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648
1649 // Fill in buffers
1650
1651 if (nextRequest->mInputStream != NULL) {
1652 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001653 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001654 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001655 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 " %s (%d)", strerror(-res), res);
1657 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1658 return true;
1659 }
1660 } else {
1661 request.input_buffer = NULL;
1662 }
1663
1664 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1665 nextRequest->mOutputStreams.size());
1666 request.output_buffers = outputBuffers.array();
1667 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1668 res = nextRequest->mOutputStreams.editItemAt(i)->
1669 getBuffer(&outputBuffers.editItemAt(i));
1670 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001671 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001672 "%s (%d)", strerror(-res), res);
1673 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1674 return true;
1675 }
1676 request.num_output_buffers++;
1677 }
1678
1679 request.frame_number = mFrameNumber++;
1680
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001681 // Log request in the in-flight queue
1682 sp<Camera3Device> parent = mParent.promote();
1683 if (parent == NULL) {
1684 CLOGE("RequestThread: Parent is gone");
1685 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1686 return false;
1687 }
1688
1689 res = parent->registerInFlight(request.frame_number,
1690 request.num_output_buffers);
1691 if (res != OK) {
1692 SET_ERR("RequestThread: Unable to register new in-flight request:"
1693 " %s (%d)", strerror(-res), res);
1694 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1695 return false;
1696 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001697
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001698 // Submit request and block until ready for next one
1699
1700 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1701 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001702 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001703 " device: %s (%d)", request.frame_number, strerror(-res), res);
1704 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1705 return false;
1706 }
1707
1708 if (request.settings != NULL) {
1709 nextRequest->mSettings.unlock(request.settings);
1710 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001711
1712 // Remove any previously queued triggers (after unlock)
1713 res = removeTriggers(mPrevRequest);
1714 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001715 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001716 "(capture request %d, HAL device: %s (%d)",
1717 request.frame_number, strerror(-res), res);
1718 return false;
1719 }
1720 mPrevTriggers = triggerCount;
1721
1722 // Read android.request.id from the request settings metadata
1723 // - inform waitUntilRequestProcessed thread of a new request ID
1724 {
1725 Mutex::Autolock al(mLatestRequestMutex);
1726
1727 camera_metadata_entry_t requestIdEntry =
1728 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1729 if (requestIdEntry.count > 0) {
1730 mLatestRequestId = requestIdEntry.data.i32[0];
1731 } else {
1732 ALOGW("%s: Did not have android.request.id set in the request",
1733 __FUNCTION__);
1734 mLatestRequestId = NAME_NOT_FOUND;
1735 }
1736
1737 mLatestRequestSignal.signal();
1738 }
1739
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001740 // Return input buffer back to framework
1741 if (request.input_buffer != NULL) {
1742 Camera3Stream *stream =
1743 Camera3Stream::cast(request.input_buffer->stream);
1744 res = stream->returnInputBuffer(*(request.input_buffer));
1745 // Note: stream may be deallocated at this point, if this buffer was the
1746 // last reference to it.
1747 if (res != OK) {
1748 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1749 " its stream:%s (%d)", __FUNCTION__,
1750 request.frame_number, strerror(-res), res);
1751 // TODO: Report error upstream
1752 }
1753 }
1754
1755
1756
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001757 return true;
1758}
1759
1760void Camera3Device::RequestThread::cleanUpFailedRequest(
1761 camera3_capture_request_t &request,
1762 sp<CaptureRequest> &nextRequest,
1763 Vector<camera3_stream_buffer_t> &outputBuffers) {
1764
1765 if (request.settings != NULL) {
1766 nextRequest->mSettings.unlock(request.settings);
1767 }
1768 if (request.input_buffer != NULL) {
1769 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001770 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001771 }
1772 for (size_t i = 0; i < request.num_output_buffers; i++) {
1773 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1774 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1775 outputBuffers[i], 0);
1776 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001777}
1778
1779sp<Camera3Device::CaptureRequest>
1780 Camera3Device::RequestThread::waitForNextRequest() {
1781 status_t res;
1782 sp<CaptureRequest> nextRequest;
1783
1784 // Optimized a bit for the simple steady-state case (single repeating
1785 // request), to avoid putting that request in the queue temporarily.
1786 Mutex::Autolock l(mRequestLock);
1787
1788 while (mRequestQueue.empty()) {
1789 if (!mRepeatingRequests.empty()) {
1790 // Always atomically enqueue all requests in a repeating request
1791 // list. Guarantees a complete in-sequence set of captures to
1792 // application.
1793 const RequestList &requests = mRepeatingRequests;
1794 RequestList::const_iterator firstRequest =
1795 requests.begin();
1796 nextRequest = *firstRequest;
1797 mRequestQueue.insert(mRequestQueue.end(),
1798 ++firstRequest,
1799 requests.end());
1800 // No need to wait any longer
1801 break;
1802 }
1803
1804 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1805
1806 if (res == TIMED_OUT) {
1807 // Signal that we're paused by starvation
1808 Mutex::Autolock pl(mPauseLock);
1809 if (mPaused == false) {
1810 mPaused = true;
1811 mPausedSignal.signal();
1812 }
1813 // Stop waiting for now and let thread management happen
1814 return NULL;
1815 }
1816 }
1817
1818 if (nextRequest == NULL) {
1819 // Don't have a repeating request already in hand, so queue
1820 // must have an entry now.
1821 RequestList::iterator firstRequest =
1822 mRequestQueue.begin();
1823 nextRequest = *firstRequest;
1824 mRequestQueue.erase(firstRequest);
1825 }
1826
1827 // Not paused
1828 Mutex::Autolock pl(mPauseLock);
1829 mPaused = false;
1830
1831 // Check if we've reconfigured since last time, and reset the preview
1832 // request if so. Can't use 'NULL request == repeat' across configure calls.
1833 if (mReconfigured) {
1834 mPrevRequest.clear();
1835 mReconfigured = false;
1836 }
1837
1838 return nextRequest;
1839}
1840
1841bool Camera3Device::RequestThread::waitIfPaused() {
1842 status_t res;
1843 Mutex::Autolock l(mPauseLock);
1844 while (mDoPause) {
1845 // Signal that we're paused by request
1846 if (mPaused == false) {
1847 mPaused = true;
1848 mPausedSignal.signal();
1849 }
1850 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1851 if (res == TIMED_OUT) {
1852 return true;
1853 }
1854 }
1855 // We don't set mPaused to false here, because waitForNextRequest needs
1856 // to further manage the paused state in case of starvation.
1857 return false;
1858}
1859
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001860void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1861 sp<Camera3Device> parent = mParent.promote();
1862 if (parent != NULL) {
1863 va_list args;
1864 va_start(args, fmt);
1865
1866 parent->setErrorStateV(fmt, args);
1867
1868 va_end(args);
1869 }
1870}
1871
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001872status_t Camera3Device::RequestThread::insertTriggers(
1873 const sp<CaptureRequest> &request) {
1874
1875 Mutex::Autolock al(mTriggerMutex);
1876
1877 CameraMetadata &metadata = request->mSettings;
1878 size_t count = mTriggerMap.size();
1879
1880 for (size_t i = 0; i < count; ++i) {
1881 RequestTrigger trigger = mTriggerMap.valueAt(i);
1882
1883 uint32_t tag = trigger.metadataTag;
1884 camera_metadata_entry entry = metadata.find(tag);
1885
1886 if (entry.count > 0) {
1887 /**
1888 * Already has an entry for this trigger in the request.
1889 * Rewrite it with our requested trigger value.
1890 */
1891 RequestTrigger oldTrigger = trigger;
1892
1893 oldTrigger.entryValue = entry.data.u8[0];
1894
1895 mTriggerReplacedMap.add(tag, oldTrigger);
1896 } else {
1897 /**
1898 * More typical, no trigger entry, so we just add it
1899 */
1900 mTriggerRemovedMap.add(tag, trigger);
1901 }
1902
1903 status_t res;
1904
1905 switch (trigger.getTagType()) {
1906 case TYPE_BYTE: {
1907 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1908 res = metadata.update(tag,
1909 &entryValue,
1910 /*count*/1);
1911 break;
1912 }
1913 case TYPE_INT32:
1914 res = metadata.update(tag,
1915 &trigger.entryValue,
1916 /*count*/1);
1917 break;
1918 default:
1919 ALOGE("%s: Type not supported: 0x%x",
1920 __FUNCTION__,
1921 trigger.getTagType());
1922 return INVALID_OPERATION;
1923 }
1924
1925 if (res != OK) {
1926 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1927 ", value %d", __FUNCTION__, trigger.getTagName(),
1928 trigger.entryValue);
1929 return res;
1930 }
1931
1932 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1933 trigger.getTagName(),
1934 trigger.entryValue);
1935 }
1936
1937 mTriggerMap.clear();
1938
1939 return count;
1940}
1941
1942status_t Camera3Device::RequestThread::removeTriggers(
1943 const sp<CaptureRequest> &request) {
1944 Mutex::Autolock al(mTriggerMutex);
1945
1946 CameraMetadata &metadata = request->mSettings;
1947
1948 /**
1949 * Replace all old entries with their old values.
1950 */
1951 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1952 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1953
1954 status_t res;
1955
1956 uint32_t tag = trigger.metadataTag;
1957 switch (trigger.getTagType()) {
1958 case TYPE_BYTE: {
1959 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1960 res = metadata.update(tag,
1961 &entryValue,
1962 /*count*/1);
1963 break;
1964 }
1965 case TYPE_INT32:
1966 res = metadata.update(tag,
1967 &trigger.entryValue,
1968 /*count*/1);
1969 break;
1970 default:
1971 ALOGE("%s: Type not supported: 0x%x",
1972 __FUNCTION__,
1973 trigger.getTagType());
1974 return INVALID_OPERATION;
1975 }
1976
1977 if (res != OK) {
1978 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1979 ", trigger value %d", __FUNCTION__,
1980 trigger.getTagName(), trigger.entryValue);
1981 return res;
1982 }
1983 }
1984 mTriggerReplacedMap.clear();
1985
1986 /**
1987 * Remove all new entries.
1988 */
1989 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1990 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1991 status_t res = metadata.erase(trigger.metadataTag);
1992
1993 if (res != OK) {
1994 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1995 ", trigger value %d", __FUNCTION__,
1996 trigger.getTagName(), trigger.entryValue);
1997 return res;
1998 }
1999 }
2000 mTriggerRemovedMap.clear();
2001
2002 return OK;
2003}
2004
2005
2006
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002007/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002008 * Static callback forwarding methods from HAL to instance
2009 */
2010
2011void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2012 const camera3_capture_result *result) {
2013 Camera3Device *d =
2014 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2015 d->processCaptureResult(result);
2016}
2017
2018void Camera3Device::sNotify(const camera3_callback_ops *cb,
2019 const camera3_notify_msg *msg) {
2020 Camera3Device *d =
2021 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2022 d->notify(msg);
2023}
2024
2025}; // namespace android