blob: 353fe742ba7fc8959c67391e74c1d3902ac24a04 [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
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700840bool Camera3Device::willNotify3A() {
841 return false;
842}
843
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700845 ATRACE_CALL();
846 status_t res;
847 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700849 while (mResultQueue.empty()) {
850 res = mResultSignal.waitRelative(mOutputLock, timeout);
851 if (res == TIMED_OUT) {
852 return res;
853 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700854 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
855 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700856 return res;
857 }
858 }
859 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800860}
861
862status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
863 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700864 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700866 if (mResultQueue.empty()) {
867 return NOT_ENOUGH_DATA;
868 }
869
870 CameraMetadata &result = *(mResultQueue.begin());
871 frame->acquire(result);
872 mResultQueue.erase(mResultQueue.begin());
873
874 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800875}
876
877status_t Camera3Device::triggerAutofocus(uint32_t id) {
878 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800879
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700880 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
881 // Mix-in this trigger into the next request and only the next request.
882 RequestTrigger trigger[] = {
883 {
884 ANDROID_CONTROL_AF_TRIGGER,
885 ANDROID_CONTROL_AF_TRIGGER_START
886 },
887 {
888 ANDROID_CONTROL_AF_TRIGGER_ID,
889 static_cast<int32_t>(id)
890 },
891 };
892
893 return mRequestThread->queueTrigger(trigger,
894 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800895}
896
897status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
898 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800899
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700900 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
901 // Mix-in this trigger into the next request and only the next request.
902 RequestTrigger trigger[] = {
903 {
904 ANDROID_CONTROL_AF_TRIGGER,
905 ANDROID_CONTROL_AF_TRIGGER_CANCEL
906 },
907 {
908 ANDROID_CONTROL_AF_TRIGGER_ID,
909 static_cast<int32_t>(id)
910 },
911 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800912
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700913 return mRequestThread->queueTrigger(trigger,
914 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915}
916
917status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
918 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800919
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700920 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
921 // Mix-in this trigger into the next request and only the next request.
922 RequestTrigger trigger[] = {
923 {
924 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
925 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
926 },
927 {
928 ANDROID_CONTROL_AE_PRECAPTURE_ID,
929 static_cast<int32_t>(id)
930 },
931 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800932
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700933 return mRequestThread->queueTrigger(trigger,
934 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800935}
936
937status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
938 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
939 ATRACE_CALL();
940 (void)reprocessStreamId; (void)buffer; (void)listener;
941
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700942 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800943 return INVALID_OPERATION;
944}
945
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946/**
947 * Camera3Device private methods
948 */
949
950sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
951 const CameraMetadata &request) {
952 ATRACE_CALL();
953 status_t res;
954
955 sp<CaptureRequest> newRequest = new CaptureRequest;
956 newRequest->mSettings = request;
957
958 camera_metadata_entry_t inputStreams =
959 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
960 if (inputStreams.count > 0) {
961 if (mInputStream == NULL ||
962 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700963 CLOGE("Request references unknown input stream %d",
964 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 return NULL;
966 }
967 // Lazy completion of stream configuration (allocation/registration)
968 // on first use
969 if (mInputStream->isConfiguring()) {
970 res = mInputStream->finishConfiguration(mHal3Device);
971 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700972 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700974 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800975 return NULL;
976 }
977 }
978
979 newRequest->mInputStream = mInputStream;
980 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
981 }
982
983 camera_metadata_entry_t streams =
984 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
985 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700986 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800987 return NULL;
988 }
989
990 for (size_t i = 0; i < streams.count; i++) {
991 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
992 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700993 CLOGE("Request references unknown stream %d",
994 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800995 return NULL;
996 }
Igor Murashkin2fba5842013-04-22 14:03:54 -0700997 sp<Camera3OutputStreamInterface> stream =
998 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800999
1000 // Lazy completion of stream configuration (allocation/registration)
1001 // on first use
1002 if (stream->isConfiguring()) {
1003 res = stream->finishConfiguration(mHal3Device);
1004 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001005 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1006 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001007 return NULL;
1008 }
1009 }
1010
1011 newRequest->mOutputStreams.push(stream);
1012 }
1013 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1014
1015 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001016}
1017
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001018status_t Camera3Device::configureStreamsLocked() {
1019 ATRACE_CALL();
1020 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001021
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001023 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001024 return INVALID_OPERATION;
1025 }
1026
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001027 if (!mNeedConfig) {
1028 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001029 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001030 return OK;
1031 }
1032
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 // Start configuring the streams
1034
1035 camera3_stream_configuration config;
1036
1037 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1038
1039 Vector<camera3_stream_t*> streams;
1040 streams.setCapacity(config.num_streams);
1041
1042 if (mInputStream != NULL) {
1043 camera3_stream_t *inputStream;
1044 inputStream = mInputStream->startConfiguration();
1045 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001046 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 return INVALID_OPERATION;
1048 }
1049 streams.add(inputStream);
1050 }
1051
1052 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001053
1054 // Don't configure bidi streams twice, nor add them twice to the list
1055 if (mOutputStreams[i].get() ==
1056 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1057
1058 config.num_streams--;
1059 continue;
1060 }
1061
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001062 camera3_stream_t *outputStream;
1063 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1064 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 return INVALID_OPERATION;
1067 }
1068 streams.add(outputStream);
1069 }
1070
1071 config.streams = streams.editArray();
1072
1073 // Do the HAL configuration; will potentially touch stream
1074 // max_buffers, usage, priv fields.
1075
1076 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1077
1078 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001079 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1080 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001081 return res;
1082 }
1083
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001084 // Finish all stream configuration immediately.
1085 // TODO: Try to relax this later back to lazy completion, which should be
1086 // faster
1087
Igor Murashkin073f8572013-05-02 14:59:28 -07001088 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001089 res = mInputStream->finishConfiguration(mHal3Device);
1090 if (res != OK) {
1091 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1092 mInputStream->getId(), strerror(-res), res);
1093 return res;
1094 }
1095 }
1096
1097 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001098 sp<Camera3OutputStreamInterface> outputStream =
1099 mOutputStreams.editValueAt(i);
1100 if (outputStream->isConfiguring()) {
1101 res = outputStream->finishConfiguration(mHal3Device);
1102 if (res != OK) {
1103 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1104 outputStream->getId(), strerror(-res), res);
1105 return res;
1106 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001107 }
1108 }
1109
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001110 // Request thread needs to know to avoid using repeat-last-settings protocol
1111 // across configure_streams() calls
1112 mRequestThread->configurationComplete();
1113
1114 // Finish configuring the streams lazily on first reference
1115
1116 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001117 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118
1119 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001120}
1121
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001122void Camera3Device::setErrorState(const char *fmt, ...) {
1123 Mutex::Autolock l(mLock);
1124 va_list args;
1125 va_start(args, fmt);
1126
1127 setErrorStateLockedV(fmt, args);
1128
1129 va_end(args);
1130}
1131
1132void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1133 Mutex::Autolock l(mLock);
1134 setErrorStateLockedV(fmt, args);
1135}
1136
1137void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1138 va_list args;
1139 va_start(args, fmt);
1140
1141 setErrorStateLockedV(fmt, args);
1142
1143 va_end(args);
1144}
1145
1146void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001147 // Print out all error messages to log
1148 String8 errorCause = String8::formatV(fmt, args);
1149 ALOGE("Camera %d: %s", mId, errorCause.string());
1150
1151 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001152 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001153
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001154 mErrorCause = errorCause;
1155
1156 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001157 mStatus = STATUS_ERROR;
1158}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001159
1160/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001161 * In-flight request management
1162 */
1163
1164status_t Camera3Device::registerInFlight(int32_t frameNumber,
1165 int32_t numBuffers) {
1166 ATRACE_CALL();
1167 Mutex::Autolock l(mInFlightLock);
1168
1169 ssize_t res;
1170 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1171 if (res < 0) return res;
1172
1173 return OK;
1174}
1175
1176/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 * Camera HAL device callback methods
1178 */
1179
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001180void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001181 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001182
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001183 status_t res;
1184
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001185 uint32_t frameNumber = result->frame_number;
1186 if (result->result == NULL && result->num_output_buffers == 0) {
1187 SET_ERR("No result data provided by HAL for frame %d",
1188 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 return;
1190 }
1191
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001192 // Get capture timestamp from list of in-flight requests, where it was added
1193 // by the shutter notification for this frame. Then update the in-flight
1194 // status and remove the in-flight entry if all result data has been
1195 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001196 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001197 {
1198 Mutex::Autolock l(mInFlightLock);
1199 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1200 if (idx == NAME_NOT_FOUND) {
1201 SET_ERR("Unknown frame number for capture result: %d",
1202 frameNumber);
1203 return;
1204 }
1205 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1206 timestamp = request.captureTimestamp;
1207 if (timestamp == 0) {
1208 SET_ERR("Called before shutter notify for frame %d",
1209 frameNumber);
1210 return;
1211 }
1212
1213 if (result->result != NULL) {
1214 if (request.haveResultMetadata) {
1215 SET_ERR("Called multiple times with metadata for frame %d",
1216 frameNumber);
1217 return;
1218 }
1219 request.haveResultMetadata = true;
1220 }
1221
1222 request.numBuffersLeft -= result->num_output_buffers;
1223
1224 if (request.numBuffersLeft < 0) {
1225 SET_ERR("Too many buffers returned for frame %d",
1226 frameNumber);
1227 return;
1228 }
1229
1230 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
1231 mInFlightMap.removeItemsAt(idx, 1);
1232 }
1233
1234 // Sanity check - if we have too many in-flight frames, something has
1235 // likely gone wrong
1236 if (mInFlightMap.size() > kInFlightWarnLimit) {
1237 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1238 }
1239
1240 }
1241
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001242 // Process the result metadata, if provided
1243 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001244 Mutex::Autolock l(mOutputLock);
1245
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001246 if (frameNumber != mNextResultFrameNumber) {
1247 SET_ERR("Out-of-order capture result metadata submitted! "
1248 "(got frame number %d, expecting %d)",
1249 frameNumber, mNextResultFrameNumber);
1250 return;
1251 }
1252 mNextResultFrameNumber++;
1253
1254 CameraMetadata &captureResult =
1255 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001256
1257 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001258 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001259 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001260 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001261 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001262 } else {
1263 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001264 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001265 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001266
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001267 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001268
1269 camera_metadata_entry entry =
1270 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1271 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001272 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001273 frameNumber);
1274 }
1275 if (timestamp != entry.data.i64[0]) {
1276 SET_ERR("Timestamp mismatch between shutter notify and result"
1277 " metadata for frame %d (%lld vs %lld respectively)",
1278 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001279 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001280 } // scope for mOutputLock
1281
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001282 // Return completed buffers to their streams with the timestamp
1283
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001284 for (size_t i = 0; i < result->num_output_buffers; i++) {
1285 Camera3Stream *stream =
1286 Camera3Stream::cast(result->output_buffers[i].stream);
1287 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1288 // Note: stream may be deallocated at this point, if this buffer was the
1289 // last reference to it.
1290 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001291 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001292 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001293 }
1294 }
1295
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001296 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001297
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001298 if (result->result != NULL) {
1299 mResultSignal.signal();
1300 }
1301
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001302}
1303
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001304
1305
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001306void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001307 NotificationListener *listener;
1308 {
1309 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001310 listener = mListener;
1311 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001312
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001313 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001314 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001315 return;
1316 }
1317
1318 switch (msg->type) {
1319 case CAMERA3_MSG_ERROR: {
1320 int streamId = 0;
1321 if (msg->message.error.error_stream != NULL) {
1322 Camera3Stream *stream =
1323 Camera3Stream::cast(
1324 msg->message.error.error_stream);
1325 streamId = stream->getId();
1326 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001327 if (listener != NULL) {
1328 listener->notifyError(msg->message.error.error_code,
1329 msg->message.error.frame_number, streamId);
1330 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001331 break;
1332 }
1333 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001334 ssize_t idx;
1335 uint32_t frameNumber = msg->message.shutter.frame_number;
1336 nsecs_t timestamp = msg->message.shutter.timestamp;
1337 // Verify ordering of shutter notifications
1338 {
1339 Mutex::Autolock l(mOutputLock);
1340 if (frameNumber != mNextShutterFrameNumber) {
1341 SET_ERR("Shutter notification out-of-order. Expected "
1342 "notification for frame %d, got frame %d",
1343 mNextShutterFrameNumber, frameNumber);
1344 break;
1345 }
1346 mNextShutterFrameNumber++;
1347 }
1348
1349 // Set timestamp for the request in the in-flight tracking
1350 {
1351 Mutex::Autolock l(mInFlightLock);
1352 idx = mInFlightMap.indexOfKey(frameNumber);
1353 if (idx >= 0) {
1354 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1355 }
1356 }
1357 if (idx < 0) {
1358 SET_ERR("Shutter notification for non-existent frame number %d",
1359 frameNumber);
1360 break;
1361 }
1362
1363 // Call listener, if any
1364 if (listener != NULL) {
1365 listener->notifyShutter(frameNumber, timestamp);
1366 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001367 break;
1368 }
1369 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001370 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001371 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001372 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001373}
1374
1375/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376 * RequestThread inner class methods
1377 */
1378
1379Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1380 camera3_device_t *hal3Device) :
1381 Thread(false),
1382 mParent(parent),
1383 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001384 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001385 mReconfigured(false),
1386 mDoPause(false),
1387 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001388 mFrameNumber(0),
1389 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390}
1391
1392void Camera3Device::RequestThread::configurationComplete() {
1393 Mutex::Autolock l(mRequestLock);
1394 mReconfigured = true;
1395}
1396
1397status_t Camera3Device::RequestThread::queueRequest(
1398 sp<CaptureRequest> request) {
1399 Mutex::Autolock l(mRequestLock);
1400 mRequestQueue.push_back(request);
1401
1402 return OK;
1403}
1404
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001405
1406status_t Camera3Device::RequestThread::queueTrigger(
1407 RequestTrigger trigger[],
1408 size_t count) {
1409
1410 Mutex::Autolock l(mTriggerMutex);
1411 status_t ret;
1412
1413 for (size_t i = 0; i < count; ++i) {
1414 ret = queueTriggerLocked(trigger[i]);
1415
1416 if (ret != OK) {
1417 return ret;
1418 }
1419 }
1420
1421 return OK;
1422}
1423
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001424int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1425 sp<Camera3Device> d = device.promote();
1426 if (d != NULL) return d->mId;
1427 return 0;
1428}
1429
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001430status_t Camera3Device::RequestThread::queueTriggerLocked(
1431 RequestTrigger trigger) {
1432
1433 uint32_t tag = trigger.metadataTag;
1434 ssize_t index = mTriggerMap.indexOfKey(tag);
1435
1436 switch (trigger.getTagType()) {
1437 case TYPE_BYTE:
1438 // fall-through
1439 case TYPE_INT32:
1440 break;
1441 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001442 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1443 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001444 return INVALID_OPERATION;
1445 }
1446
1447 /**
1448 * Collect only the latest trigger, since we only have 1 field
1449 * in the request settings per trigger tag, and can't send more than 1
1450 * trigger per request.
1451 */
1452 if (index != NAME_NOT_FOUND) {
1453 mTriggerMap.editValueAt(index) = trigger;
1454 } else {
1455 mTriggerMap.add(tag, trigger);
1456 }
1457
1458 return OK;
1459}
1460
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001461status_t Camera3Device::RequestThread::setRepeatingRequests(
1462 const RequestList &requests) {
1463 Mutex::Autolock l(mRequestLock);
1464 mRepeatingRequests.clear();
1465 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1466 requests.begin(), requests.end());
1467 return OK;
1468}
1469
1470status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1471 Mutex::Autolock l(mRequestLock);
1472 mRepeatingRequests.clear();
1473 return OK;
1474}
1475
1476void Camera3Device::RequestThread::setPaused(bool paused) {
1477 Mutex::Autolock l(mPauseLock);
1478 mDoPause = paused;
1479 mDoPauseSignal.signal();
1480}
1481
1482status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
1483 status_t res;
1484 Mutex::Autolock l(mPauseLock);
1485 while (!mPaused) {
1486 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1487 if (res == TIMED_OUT) {
1488 return res;
1489 }
1490 }
1491 return OK;
1492}
1493
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001494status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1495 int32_t requestId, nsecs_t timeout) {
1496 Mutex::Autolock l(mLatestRequestMutex);
1497 status_t res;
1498 while (mLatestRequestId != requestId) {
1499 nsecs_t startTime = systemTime();
1500
1501 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1502 if (res != OK) return res;
1503
1504 timeout -= (systemTime() - startTime);
1505 }
1506
1507 return OK;
1508}
1509
1510
1511
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001512bool Camera3Device::RequestThread::threadLoop() {
1513
1514 status_t res;
1515
1516 // Handle paused state.
1517 if (waitIfPaused()) {
1518 return true;
1519 }
1520
1521 // Get work to do
1522
1523 sp<CaptureRequest> nextRequest = waitForNextRequest();
1524 if (nextRequest == NULL) {
1525 return true;
1526 }
1527
1528 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001529 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001530 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001531
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001532 // Insert any queued triggers (before metadata is locked)
1533 int32_t triggerCount;
1534 res = insertTriggers(nextRequest);
1535 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001536 SET_ERR("RequestThread: Unable to insert triggers "
1537 "(capture request %d, HAL device: %s (%d)",
1538 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001539 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1540 return false;
1541 }
1542 triggerCount = res;
1543
1544 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1545
1546 // If the request is the same as last, or we had triggers last time
1547 if (mPrevRequest != nextRequest || triggersMixedIn) {
1548 /**
1549 * The request should be presorted so accesses in HAL
1550 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1551 */
1552 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 request.settings = nextRequest->mSettings.getAndLock();
1554 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001555 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1556
1557 IF_ALOGV() {
1558 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1559 find_camera_metadata_ro_entry(
1560 request.settings,
1561 ANDROID_CONTROL_AF_TRIGGER,
1562 &e
1563 );
1564 if (e.count > 0) {
1565 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1566 __FUNCTION__,
1567 mFrameNumber+1,
1568 e.data.u8[0]);
1569 }
1570 }
1571 } else {
1572 // leave request.settings NULL to indicate 'reuse latest given'
1573 ALOGVV("%s: Request settings are REUSED",
1574 __FUNCTION__);
1575 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576
1577 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578
1579 // Fill in buffers
1580
1581 if (nextRequest->mInputStream != NULL) {
1582 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001583 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001584 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001585 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001586 " %s (%d)", strerror(-res), res);
1587 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1588 return true;
1589 }
1590 } else {
1591 request.input_buffer = NULL;
1592 }
1593
1594 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1595 nextRequest->mOutputStreams.size());
1596 request.output_buffers = outputBuffers.array();
1597 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1598 res = nextRequest->mOutputStreams.editItemAt(i)->
1599 getBuffer(&outputBuffers.editItemAt(i));
1600 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001601 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001602 "%s (%d)", strerror(-res), res);
1603 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1604 return true;
1605 }
1606 request.num_output_buffers++;
1607 }
1608
1609 request.frame_number = mFrameNumber++;
1610
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001611 // Log request in the in-flight queue
1612 sp<Camera3Device> parent = mParent.promote();
1613 if (parent == NULL) {
1614 CLOGE("RequestThread: Parent is gone");
1615 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1616 return false;
1617 }
1618
1619 res = parent->registerInFlight(request.frame_number,
1620 request.num_output_buffers);
1621 if (res != OK) {
1622 SET_ERR("RequestThread: Unable to register new in-flight request:"
1623 " %s (%d)", strerror(-res), res);
1624 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1625 return false;
1626 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001627
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001628 // Submit request and block until ready for next one
1629
1630 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
1631 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001632 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001633 " device: %s (%d)", request.frame_number, strerror(-res), res);
1634 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1635 return false;
1636 }
1637
1638 if (request.settings != NULL) {
1639 nextRequest->mSettings.unlock(request.settings);
1640 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001641
1642 // Remove any previously queued triggers (after unlock)
1643 res = removeTriggers(mPrevRequest);
1644 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001645 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001646 "(capture request %d, HAL device: %s (%d)",
1647 request.frame_number, strerror(-res), res);
1648 return false;
1649 }
1650 mPrevTriggers = triggerCount;
1651
1652 // Read android.request.id from the request settings metadata
1653 // - inform waitUntilRequestProcessed thread of a new request ID
1654 {
1655 Mutex::Autolock al(mLatestRequestMutex);
1656
1657 camera_metadata_entry_t requestIdEntry =
1658 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1659 if (requestIdEntry.count > 0) {
1660 mLatestRequestId = requestIdEntry.data.i32[0];
1661 } else {
1662 ALOGW("%s: Did not have android.request.id set in the request",
1663 __FUNCTION__);
1664 mLatestRequestId = NAME_NOT_FOUND;
1665 }
1666
1667 mLatestRequestSignal.signal();
1668 }
1669
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001670 // Return input buffer back to framework
1671 if (request.input_buffer != NULL) {
1672 Camera3Stream *stream =
1673 Camera3Stream::cast(request.input_buffer->stream);
1674 res = stream->returnInputBuffer(*(request.input_buffer));
1675 // Note: stream may be deallocated at this point, if this buffer was the
1676 // last reference to it.
1677 if (res != OK) {
1678 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1679 " its stream:%s (%d)", __FUNCTION__,
1680 request.frame_number, strerror(-res), res);
1681 // TODO: Report error upstream
1682 }
1683 }
1684
1685
1686
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001687 return true;
1688}
1689
1690void Camera3Device::RequestThread::cleanUpFailedRequest(
1691 camera3_capture_request_t &request,
1692 sp<CaptureRequest> &nextRequest,
1693 Vector<camera3_stream_buffer_t> &outputBuffers) {
1694
1695 if (request.settings != NULL) {
1696 nextRequest->mSettings.unlock(request.settings);
1697 }
1698 if (request.input_buffer != NULL) {
1699 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001700 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001701 }
1702 for (size_t i = 0; i < request.num_output_buffers; i++) {
1703 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1704 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1705 outputBuffers[i], 0);
1706 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001707}
1708
1709sp<Camera3Device::CaptureRequest>
1710 Camera3Device::RequestThread::waitForNextRequest() {
1711 status_t res;
1712 sp<CaptureRequest> nextRequest;
1713
1714 // Optimized a bit for the simple steady-state case (single repeating
1715 // request), to avoid putting that request in the queue temporarily.
1716 Mutex::Autolock l(mRequestLock);
1717
1718 while (mRequestQueue.empty()) {
1719 if (!mRepeatingRequests.empty()) {
1720 // Always atomically enqueue all requests in a repeating request
1721 // list. Guarantees a complete in-sequence set of captures to
1722 // application.
1723 const RequestList &requests = mRepeatingRequests;
1724 RequestList::const_iterator firstRequest =
1725 requests.begin();
1726 nextRequest = *firstRequest;
1727 mRequestQueue.insert(mRequestQueue.end(),
1728 ++firstRequest,
1729 requests.end());
1730 // No need to wait any longer
1731 break;
1732 }
1733
1734 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1735
1736 if (res == TIMED_OUT) {
1737 // Signal that we're paused by starvation
1738 Mutex::Autolock pl(mPauseLock);
1739 if (mPaused == false) {
1740 mPaused = true;
1741 mPausedSignal.signal();
1742 }
1743 // Stop waiting for now and let thread management happen
1744 return NULL;
1745 }
1746 }
1747
1748 if (nextRequest == NULL) {
1749 // Don't have a repeating request already in hand, so queue
1750 // must have an entry now.
1751 RequestList::iterator firstRequest =
1752 mRequestQueue.begin();
1753 nextRequest = *firstRequest;
1754 mRequestQueue.erase(firstRequest);
1755 }
1756
1757 // Not paused
1758 Mutex::Autolock pl(mPauseLock);
1759 mPaused = false;
1760
1761 // Check if we've reconfigured since last time, and reset the preview
1762 // request if so. Can't use 'NULL request == repeat' across configure calls.
1763 if (mReconfigured) {
1764 mPrevRequest.clear();
1765 mReconfigured = false;
1766 }
1767
1768 return nextRequest;
1769}
1770
1771bool Camera3Device::RequestThread::waitIfPaused() {
1772 status_t res;
1773 Mutex::Autolock l(mPauseLock);
1774 while (mDoPause) {
1775 // Signal that we're paused by request
1776 if (mPaused == false) {
1777 mPaused = true;
1778 mPausedSignal.signal();
1779 }
1780 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1781 if (res == TIMED_OUT) {
1782 return true;
1783 }
1784 }
1785 // We don't set mPaused to false here, because waitForNextRequest needs
1786 // to further manage the paused state in case of starvation.
1787 return false;
1788}
1789
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001790void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1791 sp<Camera3Device> parent = mParent.promote();
1792 if (parent != NULL) {
1793 va_list args;
1794 va_start(args, fmt);
1795
1796 parent->setErrorStateV(fmt, args);
1797
1798 va_end(args);
1799 }
1800}
1801
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001802status_t Camera3Device::RequestThread::insertTriggers(
1803 const sp<CaptureRequest> &request) {
1804
1805 Mutex::Autolock al(mTriggerMutex);
1806
1807 CameraMetadata &metadata = request->mSettings;
1808 size_t count = mTriggerMap.size();
1809
1810 for (size_t i = 0; i < count; ++i) {
1811 RequestTrigger trigger = mTriggerMap.valueAt(i);
1812
1813 uint32_t tag = trigger.metadataTag;
1814 camera_metadata_entry entry = metadata.find(tag);
1815
1816 if (entry.count > 0) {
1817 /**
1818 * Already has an entry for this trigger in the request.
1819 * Rewrite it with our requested trigger value.
1820 */
1821 RequestTrigger oldTrigger = trigger;
1822
1823 oldTrigger.entryValue = entry.data.u8[0];
1824
1825 mTriggerReplacedMap.add(tag, oldTrigger);
1826 } else {
1827 /**
1828 * More typical, no trigger entry, so we just add it
1829 */
1830 mTriggerRemovedMap.add(tag, trigger);
1831 }
1832
1833 status_t res;
1834
1835 switch (trigger.getTagType()) {
1836 case TYPE_BYTE: {
1837 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1838 res = metadata.update(tag,
1839 &entryValue,
1840 /*count*/1);
1841 break;
1842 }
1843 case TYPE_INT32:
1844 res = metadata.update(tag,
1845 &trigger.entryValue,
1846 /*count*/1);
1847 break;
1848 default:
1849 ALOGE("%s: Type not supported: 0x%x",
1850 __FUNCTION__,
1851 trigger.getTagType());
1852 return INVALID_OPERATION;
1853 }
1854
1855 if (res != OK) {
1856 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1857 ", value %d", __FUNCTION__, trigger.getTagName(),
1858 trigger.entryValue);
1859 return res;
1860 }
1861
1862 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1863 trigger.getTagName(),
1864 trigger.entryValue);
1865 }
1866
1867 mTriggerMap.clear();
1868
1869 return count;
1870}
1871
1872status_t Camera3Device::RequestThread::removeTriggers(
1873 const sp<CaptureRequest> &request) {
1874 Mutex::Autolock al(mTriggerMutex);
1875
1876 CameraMetadata &metadata = request->mSettings;
1877
1878 /**
1879 * Replace all old entries with their old values.
1880 */
1881 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1882 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1883
1884 status_t res;
1885
1886 uint32_t tag = trigger.metadataTag;
1887 switch (trigger.getTagType()) {
1888 case TYPE_BYTE: {
1889 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1890 res = metadata.update(tag,
1891 &entryValue,
1892 /*count*/1);
1893 break;
1894 }
1895 case TYPE_INT32:
1896 res = metadata.update(tag,
1897 &trigger.entryValue,
1898 /*count*/1);
1899 break;
1900 default:
1901 ALOGE("%s: Type not supported: 0x%x",
1902 __FUNCTION__,
1903 trigger.getTagType());
1904 return INVALID_OPERATION;
1905 }
1906
1907 if (res != OK) {
1908 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1909 ", trigger value %d", __FUNCTION__,
1910 trigger.getTagName(), trigger.entryValue);
1911 return res;
1912 }
1913 }
1914 mTriggerReplacedMap.clear();
1915
1916 /**
1917 * Remove all new entries.
1918 */
1919 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1920 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1921 status_t res = metadata.erase(trigger.metadataTag);
1922
1923 if (res != OK) {
1924 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1925 ", trigger value %d", __FUNCTION__,
1926 trigger.getTagName(), trigger.entryValue);
1927 return res;
1928 }
1929 }
1930 mTriggerRemovedMap.clear();
1931
1932 return OK;
1933}
1934
1935
1936
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001937/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001938 * Static callback forwarding methods from HAL to instance
1939 */
1940
1941void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1942 const camera3_capture_result *result) {
1943 Camera3Device *d =
1944 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1945 d->processCaptureResult(result);
1946}
1947
1948void Camera3Device::sNotify(const camera3_callback_ops *cb,
1949 const camera3_notify_msg *msg) {
1950 Camera3Device *d =
1951 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1952 d->notify(msg);
1953}
1954
1955}; // namespace android