blob: 9d0f3926b3ebbabef2bddf875abf6561354af911 [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
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700131 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700133 ATRACE_END();
134
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
137 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Get vendor metadata tags */
143
144 mVendorTagOps.get_camera_vendor_section_name = NULL;
145
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700148 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149
150 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
151 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
152 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700153 SET_ERR_L("Unable to set tag ops: %s (%d)",
154 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155 device->common.close(&device->common);
156 return res;
157 }
158 }
159
160 /** Start up request queue thread */
161
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800162 mRequestThread = new RequestThread(this, device);
163 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800164 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700165 SET_ERR_L("Unable to start request queue thread: %s (%d)",
166 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800168 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 return res;
170 }
171
172 /** Everything is good to go */
173
174 mDeviceInfo = info.static_camera_characteristics;
175 mHal3Device = device;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800176 mStatus = STATUS_IDLE;
177 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700178 mNeedConfig = true;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179
180 return OK;
181}
182
183status_t Camera3Device::disconnect() {
184 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800187 ALOGV("%s: E", __FUNCTION__);
188
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700189 status_t res = OK;
190 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191
192 if (mStatus == STATUS_ACTIVE ||
193 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
194 res = mRequestThread->clearRepeatingRequests();
195 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700196 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700197 // Continue to close device even in case of error
198 } else {
199 res = waitUntilDrainedLocked();
200 if (res != OK) {
201 SET_ERR_L("Timeout waiting for HAL to drain");
202 // Continue to close device even in case of error
203 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800204 }
205 }
206 assert(mStatus == STATUS_IDLE || mStatus == STATUS_ERROR);
207
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700208 if (mStatus == STATUS_ERROR) {
209 CLOGE("Shutting down in an error state");
210 }
211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 if (mRequestThread != NULL) {
213 mRequestThread->requestExit();
214 }
215
216 mOutputStreams.clear();
217 mInputStream.clear();
218
219 if (mRequestThread != NULL) {
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700220 if (mStatus != STATUS_ERROR) {
221 // HAL may be in a bad state, so waiting for request thread
222 // (which may be stuck in the HAL processCaptureRequest call)
223 // could be dangerous.
224 mRequestThread->join();
225 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800226 mRequestThread.clear();
227 }
228
229 if (mHal3Device != NULL) {
230 mHal3Device->common.close(&mHal3Device->common);
231 mHal3Device = NULL;
232 }
233
234 mStatus = STATUS_UNINITIALIZED;
235
236 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238}
239
240status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
241 ATRACE_CALL();
242 (void)args;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800243 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800244
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800245 const char *status =
246 mStatus == STATUS_ERROR ? "ERROR" :
247 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
248 mStatus == STATUS_IDLE ? "IDLE" :
249 mStatus == STATUS_ACTIVE ? "ACTIVE" :
250 "Unknown";
251 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700252 if (mStatus == STATUS_ERROR) {
253 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800255 lines.appendFormat(" Stream configuration:\n");
256
257 if (mInputStream != NULL) {
258 write(fd, lines.string(), lines.size());
259 mInputStream->dump(fd, args);
260 } else {
261 lines.appendFormat(" No input stream.\n");
262 write(fd, lines.string(), lines.size());
263 }
264 for (size_t i = 0; i < mOutputStreams.size(); i++) {
265 mOutputStreams[i]->dump(fd,args);
266 }
267
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700268 lines = String8(" In-flight requests:\n");
269 if (mInFlightMap.size() == 0) {
270 lines.append(" None\n");
271 } else {
272 for (size_t i = 0; i < mInFlightMap.size(); i++) {
273 InFlightRequest r = mInFlightMap.valueAt(i);
274 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
275 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
276 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
277 r.numBuffersLeft);
278 }
279 }
280 write(fd, lines.string(), lines.size());
281
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700283 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800284 write(fd, lines.string(), lines.size());
285 mHal3Device->ops->dump(mHal3Device, fd);
286 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800287
288 return OK;
289}
290
291const CameraMetadata& Camera3Device::info() const {
292 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800293 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
294 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700295 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800296 mStatus == STATUS_ERROR ?
297 "when in error state" : "before init");
298 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800299 return mDeviceInfo;
300}
301
302status_t Camera3Device::capture(CameraMetadata &request) {
303 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800305
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700306 // TODO: take ownership of the request
307
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800308 switch (mStatus) {
309 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700310 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 return INVALID_OPERATION;
312 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700313 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314 return INVALID_OPERATION;
315 case STATUS_IDLE:
316 case STATUS_ACTIVE:
317 // OK
318 break;
319 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700320 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321 return INVALID_OPERATION;
322 }
323
324 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
325 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700326 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800327 return BAD_VALUE;
328 }
329
330 return mRequestThread->queueRequest(newRequest);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800331}
332
333
334status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
335 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800336 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800338 switch (mStatus) {
339 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700340 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800341 return INVALID_OPERATION;
342 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700343 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800344 return INVALID_OPERATION;
345 case STATUS_IDLE:
346 case STATUS_ACTIVE:
347 // OK
348 break;
349 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700350 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800351 return INVALID_OPERATION;
352 }
353
354 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
355 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700356 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800357 return BAD_VALUE;
358 }
359
360 RequestList newRepeatingRequests;
361 newRepeatingRequests.push_back(newRepeatingRequest);
362
363 return mRequestThread->setRepeatingRequests(newRepeatingRequests);
364}
365
366
367sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
368 const CameraMetadata &request) {
369 status_t res;
370
371 if (mStatus == STATUS_IDLE) {
372 res = configureStreamsLocked();
373 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700374 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800375 return NULL;
376 }
377 }
378
379 sp<CaptureRequest> newRequest = createCaptureRequest(request);
380 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800381}
382
383status_t Camera3Device::clearStreamingRequest() {
384 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800386
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800387 switch (mStatus) {
388 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700389 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800390 return INVALID_OPERATION;
391 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700392 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800393 return INVALID_OPERATION;
394 case STATUS_IDLE:
395 case STATUS_ACTIVE:
396 // OK
397 break;
398 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700399 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800400 return INVALID_OPERATION;
401 }
402
403 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800404}
405
406status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
407 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700409 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800410}
411
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700412status_t Camera3Device::createInputStream(
413 uint32_t width, uint32_t height, int format, int *id) {
414 ATRACE_CALL();
415 Mutex::Autolock l(mLock);
416
417 status_t res;
418 bool wasActive = false;
419
420 switch (mStatus) {
421 case STATUS_ERROR:
422 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
423 return INVALID_OPERATION;
424 case STATUS_UNINITIALIZED:
425 ALOGE("%s: Device not initialized", __FUNCTION__);
426 return INVALID_OPERATION;
427 case STATUS_IDLE:
428 // OK
429 break;
430 case STATUS_ACTIVE:
431 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
432 mRequestThread->setPaused(true);
433 res = waitUntilDrainedLocked();
434 if (res != OK) {
435 ALOGE("%s: Can't pause captures to reconfigure streams!",
436 __FUNCTION__);
437 mStatus = STATUS_ERROR;
438 return res;
439 }
440 wasActive = true;
441 break;
442 default:
443 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
444 return INVALID_OPERATION;
445 }
446 assert(mStatus == STATUS_IDLE);
447
448 if (mInputStream != 0) {
449 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
450 return INVALID_OPERATION;
451 }
452
453 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
454 width, height, format);
455
456 mInputStream = newStream;
457
458 *id = mNextStreamId++;
459
460 // Continue captures if active at start
461 if (wasActive) {
462 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
463 res = configureStreamsLocked();
464 if (res != OK) {
465 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
466 __FUNCTION__, mNextStreamId, strerror(-res), res);
467 return res;
468 }
469 mRequestThread->setPaused(false);
470 }
471
472 return OK;
473}
474
Igor Murashkin2fba5842013-04-22 14:03:54 -0700475
476status_t Camera3Device::createZslStream(
477 uint32_t width, uint32_t height,
478 int depth,
479 /*out*/
480 int *id,
481 sp<Camera3ZslStream>* zslStream) {
482 ATRACE_CALL();
483 Mutex::Autolock l(mLock);
484
485 status_t res;
486 bool wasActive = false;
487
488 switch (mStatus) {
489 case STATUS_ERROR:
490 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
491 return INVALID_OPERATION;
492 case STATUS_UNINITIALIZED:
493 ALOGE("%s: Device not initialized", __FUNCTION__);
494 return INVALID_OPERATION;
495 case STATUS_IDLE:
496 // OK
497 break;
498 case STATUS_ACTIVE:
499 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
500 mRequestThread->setPaused(true);
501 res = waitUntilDrainedLocked();
502 if (res != OK) {
503 ALOGE("%s: Can't pause captures to reconfigure streams!",
504 __FUNCTION__);
505 mStatus = STATUS_ERROR;
506 return res;
507 }
508 wasActive = true;
509 break;
510 default:
511 ALOGE("%s: Unexpected status: %d", __FUNCTION__, mStatus);
512 return INVALID_OPERATION;
513 }
514 assert(mStatus == STATUS_IDLE);
515
516 if (mInputStream != 0) {
517 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
518 return INVALID_OPERATION;
519 }
520
521 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
522 width, height, depth);
523
524 res = mOutputStreams.add(mNextStreamId, newStream);
525 if (res < 0) {
526 ALOGE("%s: Can't add new stream to set: %s (%d)",
527 __FUNCTION__, strerror(-res), res);
528 return res;
529 }
530 mInputStream = newStream;
531
532 *id = mNextStreamId++;
533 *zslStream = newStream;
534
535 // Continue captures if active at start
536 if (wasActive) {
537 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
538 res = configureStreamsLocked();
539 if (res != OK) {
540 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
541 __FUNCTION__, mNextStreamId, strerror(-res), res);
542 return res;
543 }
544 mRequestThread->setPaused(false);
545 }
546
547 return OK;
548}
549
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800550status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
551 uint32_t width, uint32_t height, int format, size_t size, int *id) {
552 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800553 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800554
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 status_t res;
556 bool wasActive = false;
557
558 switch (mStatus) {
559 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700560 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 return INVALID_OPERATION;
562 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700563 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800564 return INVALID_OPERATION;
565 case STATUS_IDLE:
566 // OK
567 break;
568 case STATUS_ACTIVE:
569 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
570 mRequestThread->setPaused(true);
571 res = waitUntilDrainedLocked();
572 if (res != OK) {
573 ALOGE("%s: Can't pause captures to reconfigure streams!",
574 __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800575 return res;
576 }
577 wasActive = true;
578 break;
579 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700580 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800581 return INVALID_OPERATION;
582 }
583 assert(mStatus == STATUS_IDLE);
584
585 sp<Camera3OutputStream> newStream;
586 if (format == HAL_PIXEL_FORMAT_BLOB) {
587 newStream = new Camera3OutputStream(mNextStreamId, consumer,
588 width, height, size, format);
589 } else {
590 newStream = new Camera3OutputStream(mNextStreamId, consumer,
591 width, height, format);
592 }
593
594 res = mOutputStreams.add(mNextStreamId, newStream);
595 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700596 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597 return res;
598 }
599
600 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700601 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800602
603 // Continue captures if active at start
604 if (wasActive) {
605 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
606 res = configureStreamsLocked();
607 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700608 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
609 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800610 return res;
611 }
612 mRequestThread->setPaused(false);
613 }
614
615 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800616}
617
618status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
619 ATRACE_CALL();
620 (void)outputId; (void)id;
621
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700622 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800623 return INVALID_OPERATION;
624}
625
626
627status_t Camera3Device::getStreamInfo(int id,
628 uint32_t *width, uint32_t *height, uint32_t *format) {
629 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 switch (mStatus) {
633 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700634 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return INVALID_OPERATION;
636 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700637 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638 return INVALID_OPERATION;
639 case STATUS_IDLE:
640 case STATUS_ACTIVE:
641 // OK
642 break;
643 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700644 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 return INVALID_OPERATION;
646 }
647
648 ssize_t idx = mOutputStreams.indexOfKey(id);
649 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700650 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800651 return idx;
652 }
653
654 if (width) *width = mOutputStreams[idx]->getWidth();
655 if (height) *height = mOutputStreams[idx]->getHeight();
656 if (format) *format = mOutputStreams[idx]->getFormat();
657
658 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800659}
660
661status_t Camera3Device::setStreamTransform(int id,
662 int transform) {
663 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800664 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800665
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800666 switch (mStatus) {
667 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700668 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800669 return INVALID_OPERATION;
670 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700671 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 return INVALID_OPERATION;
673 case STATUS_IDLE:
674 case STATUS_ACTIVE:
675 // OK
676 break;
677 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700678 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 return INVALID_OPERATION;
680 }
681
682 ssize_t idx = mOutputStreams.indexOfKey(id);
683 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700684 CLOGE("Stream %d does not exist",
685 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800686 return BAD_VALUE;
687 }
688
689 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800690}
691
692status_t Camera3Device::deleteStream(int id) {
693 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800694 Mutex::Autolock l(mLock);
695 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800696
Igor Murashkine2172be2013-05-28 15:31:39 -0700697 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
698
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699 // CameraDevice semantics require device to already be idle before
700 // deleteStream is called, unlike for createStream.
701 if (mStatus != STATUS_IDLE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700702 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
703 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800704 }
705
Igor Murashkin2fba5842013-04-22 14:03:54 -0700706 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800707 if (mInputStream != NULL && id == mInputStream->getId()) {
708 deletedStream = mInputStream;
709 mInputStream.clear();
710 } else {
711 ssize_t idx = mOutputStreams.indexOfKey(id);
712 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700713 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 return BAD_VALUE;
715 }
716 deletedStream = mOutputStreams.editValueAt(idx);
717 mOutputStreams.removeItem(id);
718 }
719
720 // Free up the stream endpoint so that it can be used by some other stream
721 res = deletedStream->disconnect();
722 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700723 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724 // fall through since we want to still list the stream as deleted.
725 }
726 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700727 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728
729 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800730}
731
732status_t Camera3Device::deleteReprocessStream(int id) {
733 ATRACE_CALL();
734 (void)id;
735
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700736 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800737 return INVALID_OPERATION;
738}
739
740
741status_t Camera3Device::createDefaultRequest(int templateId,
742 CameraMetadata *request) {
743 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700744 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800745 Mutex::Autolock l(mLock);
746
747 switch (mStatus) {
748 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700749 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800750 return INVALID_OPERATION;
751 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700752 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 return INVALID_OPERATION;
754 case STATUS_IDLE:
755 case STATUS_ACTIVE:
756 // OK
757 break;
758 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700759 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 return INVALID_OPERATION;
761 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800762
763 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700764 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800765 rawRequest = mHal3Device->ops->construct_default_request_settings(
766 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700767 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700768 if (rawRequest == NULL) {
769 SET_ERR_L("HAL is unable to construct default settings for template %d",
770 templateId);
771 return DEAD_OBJECT;
772 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800773 *request = rawRequest;
774
775 return OK;
776}
777
778status_t Camera3Device::waitUntilDrained() {
779 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800780 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800781
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800782 return waitUntilDrainedLocked();
783}
784
785status_t Camera3Device::waitUntilDrainedLocked() {
786 ATRACE_CALL();
787 status_t res;
788
789 switch (mStatus) {
790 case STATUS_UNINITIALIZED:
791 case STATUS_IDLE:
792 ALOGV("%s: Already idle", __FUNCTION__);
793 return OK;
794 case STATUS_ERROR:
795 case STATUS_ACTIVE:
796 // Need to shut down
797 break;
798 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700799 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 return INVALID_OPERATION;
801 }
802
803 if (mRequestThread != NULL) {
804 res = mRequestThread->waitUntilPaused(kShutdownTimeout);
805 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700806 SET_ERR_L("Can't stop request thread in %f seconds!",
807 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800808 return res;
809 }
810 }
811 if (mInputStream != NULL) {
812 res = mInputStream->waitUntilIdle(kShutdownTimeout);
813 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700814 SET_ERR_L("Can't idle input stream %d in %f seconds!",
815 mInputStream->getId(), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 return res;
817 }
818 }
819 for (size_t i = 0; i < mOutputStreams.size(); i++) {
820 res = mOutputStreams.editValueAt(i)->waitUntilIdle(kShutdownTimeout);
821 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700822 SET_ERR_L("Can't idle output stream %d in %f seconds!",
823 mOutputStreams.keyAt(i), kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return res;
825 }
826 }
827
828 if (mStatus != STATUS_ERROR) {
829 mStatus = STATUS_IDLE;
830 }
831
832 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800833}
834
835status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
836 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700837 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800838
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700839 if (listener != NULL && mListener != NULL) {
840 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
841 }
842 mListener = listener;
843
844 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845}
846
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700847bool Camera3Device::willNotify3A() {
848 return false;
849}
850
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700852 ATRACE_CALL();
853 status_t res;
854 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800855
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700856 while (mResultQueue.empty()) {
857 res = mResultSignal.waitRelative(mOutputLock, timeout);
858 if (res == TIMED_OUT) {
859 return res;
860 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700861 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
862 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700863 return res;
864 }
865 }
866 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867}
868
869status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
870 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700871 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800872
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700873 if (mResultQueue.empty()) {
874 return NOT_ENOUGH_DATA;
875 }
876
877 CameraMetadata &result = *(mResultQueue.begin());
878 frame->acquire(result);
879 mResultQueue.erase(mResultQueue.begin());
880
881 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800882}
883
884status_t Camera3Device::triggerAutofocus(uint32_t id) {
885 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700887 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
888 // Mix-in this trigger into the next request and only the next request.
889 RequestTrigger trigger[] = {
890 {
891 ANDROID_CONTROL_AF_TRIGGER,
892 ANDROID_CONTROL_AF_TRIGGER_START
893 },
894 {
895 ANDROID_CONTROL_AF_TRIGGER_ID,
896 static_cast<int32_t>(id)
897 },
898 };
899
900 return mRequestThread->queueTrigger(trigger,
901 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800902}
903
904status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
905 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800906
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700907 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
908 // Mix-in this trigger into the next request and only the next request.
909 RequestTrigger trigger[] = {
910 {
911 ANDROID_CONTROL_AF_TRIGGER,
912 ANDROID_CONTROL_AF_TRIGGER_CANCEL
913 },
914 {
915 ANDROID_CONTROL_AF_TRIGGER_ID,
916 static_cast<int32_t>(id)
917 },
918 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800919
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700920 return mRequestThread->queueTrigger(trigger,
921 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800922}
923
924status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
925 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700927 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
928 // Mix-in this trigger into the next request and only the next request.
929 RequestTrigger trigger[] = {
930 {
931 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
932 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
933 },
934 {
935 ANDROID_CONTROL_AE_PRECAPTURE_ID,
936 static_cast<int32_t>(id)
937 },
938 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800939
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700940 return mRequestThread->queueTrigger(trigger,
941 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800942}
943
944status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
945 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
946 ATRACE_CALL();
947 (void)reprocessStreamId; (void)buffer; (void)listener;
948
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700949 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800950 return INVALID_OPERATION;
951}
952
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800953/**
954 * Camera3Device private methods
955 */
956
957sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
958 const CameraMetadata &request) {
959 ATRACE_CALL();
960 status_t res;
961
962 sp<CaptureRequest> newRequest = new CaptureRequest;
963 newRequest->mSettings = request;
964
965 camera_metadata_entry_t inputStreams =
966 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
967 if (inputStreams.count > 0) {
968 if (mInputStream == NULL ||
969 mInputStream->getId() != inputStreams.data.u8[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700970 CLOGE("Request references unknown input stream %d",
971 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 return NULL;
973 }
974 // Lazy completion of stream configuration (allocation/registration)
975 // on first use
976 if (mInputStream->isConfiguring()) {
977 res = mInputStream->finishConfiguration(mHal3Device);
978 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700979 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700981 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982 return NULL;
983 }
984 }
985
986 newRequest->mInputStream = mInputStream;
987 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
988 }
989
990 camera_metadata_entry_t streams =
991 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
992 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700993 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800994 return NULL;
995 }
996
997 for (size_t i = 0; i < streams.count; i++) {
998 int idx = mOutputStreams.indexOfKey(streams.data.u8[i]);
999 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001000 CLOGE("Request references unknown stream %d",
1001 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002 return NULL;
1003 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001004 sp<Camera3OutputStreamInterface> stream =
1005 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006
1007 // Lazy completion of stream configuration (allocation/registration)
1008 // on first use
1009 if (stream->isConfiguring()) {
1010 res = stream->finishConfiguration(mHal3Device);
1011 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001012 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1013 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001014 return NULL;
1015 }
1016 }
1017
1018 newRequest->mOutputStreams.push(stream);
1019 }
1020 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1021
1022 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001023}
1024
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025status_t Camera3Device::configureStreamsLocked() {
1026 ATRACE_CALL();
1027 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001028
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001029 if (mStatus != STATUS_IDLE) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001030 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 return INVALID_OPERATION;
1032 }
1033
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001034 if (!mNeedConfig) {
1035 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
Eino-Ville Talvala31fdb292013-06-12 17:06:41 -07001036 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001037 return OK;
1038 }
1039
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 // Start configuring the streams
1041
1042 camera3_stream_configuration config;
1043
1044 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1045
1046 Vector<camera3_stream_t*> streams;
1047 streams.setCapacity(config.num_streams);
1048
1049 if (mInputStream != NULL) {
1050 camera3_stream_t *inputStream;
1051 inputStream = mInputStream->startConfiguration();
1052 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001053 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 return INVALID_OPERATION;
1055 }
1056 streams.add(inputStream);
1057 }
1058
1059 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001060
1061 // Don't configure bidi streams twice, nor add them twice to the list
1062 if (mOutputStreams[i].get() ==
1063 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1064
1065 config.num_streams--;
1066 continue;
1067 }
1068
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001069 camera3_stream_t *outputStream;
1070 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1071 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001072 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 return INVALID_OPERATION;
1074 }
1075 streams.add(outputStream);
1076 }
1077
1078 config.streams = streams.editArray();
1079
1080 // Do the HAL configuration; will potentially touch stream
1081 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001082 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001083 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001084 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085
1086 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001087 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1088 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001089 return res;
1090 }
1091
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001092 // Finish all stream configuration immediately.
1093 // TODO: Try to relax this later back to lazy completion, which should be
1094 // faster
1095
Igor Murashkin073f8572013-05-02 14:59:28 -07001096 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001097 res = mInputStream->finishConfiguration(mHal3Device);
1098 if (res != OK) {
1099 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1100 mInputStream->getId(), strerror(-res), res);
1101 return res;
1102 }
1103 }
1104
1105 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001106 sp<Camera3OutputStreamInterface> outputStream =
1107 mOutputStreams.editValueAt(i);
1108 if (outputStream->isConfiguring()) {
1109 res = outputStream->finishConfiguration(mHal3Device);
1110 if (res != OK) {
1111 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1112 outputStream->getId(), strerror(-res), res);
1113 return res;
1114 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001115 }
1116 }
1117
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001118 // Request thread needs to know to avoid using repeat-last-settings protocol
1119 // across configure_streams() calls
1120 mRequestThread->configurationComplete();
1121
1122 // Finish configuring the streams lazily on first reference
1123
1124 mStatus = STATUS_ACTIVE;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001125 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001126
1127 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001128}
1129
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001130void Camera3Device::setErrorState(const char *fmt, ...) {
1131 Mutex::Autolock l(mLock);
1132 va_list args;
1133 va_start(args, fmt);
1134
1135 setErrorStateLockedV(fmt, args);
1136
1137 va_end(args);
1138}
1139
1140void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1141 Mutex::Autolock l(mLock);
1142 setErrorStateLockedV(fmt, args);
1143}
1144
1145void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1146 va_list args;
1147 va_start(args, fmt);
1148
1149 setErrorStateLockedV(fmt, args);
1150
1151 va_end(args);
1152}
1153
1154void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001155 // Print out all error messages to log
1156 String8 errorCause = String8::formatV(fmt, args);
1157 ALOGE("Camera %d: %s", mId, errorCause.string());
1158
1159 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001160 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001161
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001162 mErrorCause = errorCause;
1163
1164 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001165 mStatus = STATUS_ERROR;
1166}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001167
1168/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001169 * In-flight request management
1170 */
1171
1172status_t Camera3Device::registerInFlight(int32_t frameNumber,
1173 int32_t numBuffers) {
1174 ATRACE_CALL();
1175 Mutex::Autolock l(mInFlightLock);
1176
1177 ssize_t res;
1178 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers));
1179 if (res < 0) return res;
1180
1181 return OK;
1182}
1183
1184/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001185 * Camera HAL device callback methods
1186 */
1187
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001188void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001191 status_t res;
1192
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001193 uint32_t frameNumber = result->frame_number;
1194 if (result->result == NULL && result->num_output_buffers == 0) {
1195 SET_ERR("No result data provided by HAL for frame %d",
1196 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001197 return;
1198 }
1199
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001200 // Get capture timestamp from list of in-flight requests, where it was added
1201 // by the shutter notification for this frame. Then update the in-flight
1202 // status and remove the in-flight entry if all result data has been
1203 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001204 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001205 {
1206 Mutex::Autolock l(mInFlightLock);
1207 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1208 if (idx == NAME_NOT_FOUND) {
1209 SET_ERR("Unknown frame number for capture result: %d",
1210 frameNumber);
1211 return;
1212 }
1213 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1214 timestamp = request.captureTimestamp;
1215 if (timestamp == 0) {
1216 SET_ERR("Called before shutter notify for frame %d",
1217 frameNumber);
1218 return;
1219 }
1220
1221 if (result->result != NULL) {
1222 if (request.haveResultMetadata) {
1223 SET_ERR("Called multiple times with metadata for frame %d",
1224 frameNumber);
1225 return;
1226 }
1227 request.haveResultMetadata = true;
1228 }
1229
1230 request.numBuffersLeft -= result->num_output_buffers;
1231
1232 if (request.numBuffersLeft < 0) {
1233 SET_ERR("Too many buffers returned for frame %d",
1234 frameNumber);
1235 return;
1236 }
1237
1238 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001239 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001240 mInFlightMap.removeItemsAt(idx, 1);
1241 }
1242
1243 // Sanity check - if we have too many in-flight frames, something has
1244 // likely gone wrong
1245 if (mInFlightMap.size() > kInFlightWarnLimit) {
1246 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1247 }
1248
1249 }
1250
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001251 // Process the result metadata, if provided
1252 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001253 Mutex::Autolock l(mOutputLock);
1254
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001255 if (frameNumber != mNextResultFrameNumber) {
1256 SET_ERR("Out-of-order capture result metadata submitted! "
1257 "(got frame number %d, expecting %d)",
1258 frameNumber, mNextResultFrameNumber);
1259 return;
1260 }
1261 mNextResultFrameNumber++;
1262
1263 CameraMetadata &captureResult =
1264 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001265
1266 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001267 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001268 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001269 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001270 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001271 } else {
1272 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001273 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001274 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001275
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001276 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001277
1278 camera_metadata_entry entry =
1279 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1280 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001281 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001282 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001283 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001284 SET_ERR("Timestamp mismatch between shutter notify and result"
1285 " metadata for frame %d (%lld vs %lld respectively)",
1286 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001287 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001288 } // scope for mOutputLock
1289
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001290 // Return completed buffers to their streams with the timestamp
1291
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001292 for (size_t i = 0; i < result->num_output_buffers; i++) {
1293 Camera3Stream *stream =
1294 Camera3Stream::cast(result->output_buffers[i].stream);
1295 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1296 // Note: stream may be deallocated at this point, if this buffer was the
1297 // last reference to it.
1298 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001299 SET_ERR("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001300 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001301 }
1302 }
1303
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001304 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001305
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001306 if (result->result != NULL) {
1307 mResultSignal.signal();
1308 }
1309
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001310}
1311
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001312
1313
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001314void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001315 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001316 NotificationListener *listener;
1317 {
1318 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001319 listener = mListener;
1320 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001321
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001322 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001323 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001324 return;
1325 }
1326
1327 switch (msg->type) {
1328 case CAMERA3_MSG_ERROR: {
1329 int streamId = 0;
1330 if (msg->message.error.error_stream != NULL) {
1331 Camera3Stream *stream =
1332 Camera3Stream::cast(
1333 msg->message.error.error_stream);
1334 streamId = stream->getId();
1335 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001336 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1337 mId, __FUNCTION__, msg->message.error.frame_number,
1338 streamId, msg->message.error.error_code);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001339 if (listener != NULL) {
1340 listener->notifyError(msg->message.error.error_code,
1341 msg->message.error.frame_number, streamId);
1342 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001343 break;
1344 }
1345 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001346 ssize_t idx;
1347 uint32_t frameNumber = msg->message.shutter.frame_number;
1348 nsecs_t timestamp = msg->message.shutter.timestamp;
1349 // Verify ordering of shutter notifications
1350 {
1351 Mutex::Autolock l(mOutputLock);
1352 if (frameNumber != mNextShutterFrameNumber) {
1353 SET_ERR("Shutter notification out-of-order. Expected "
1354 "notification for frame %d, got frame %d",
1355 mNextShutterFrameNumber, frameNumber);
1356 break;
1357 }
1358 mNextShutterFrameNumber++;
1359 }
1360
1361 // Set timestamp for the request in the in-flight tracking
1362 {
1363 Mutex::Autolock l(mInFlightLock);
1364 idx = mInFlightMap.indexOfKey(frameNumber);
1365 if (idx >= 0) {
1366 mInFlightMap.editValueAt(idx).captureTimestamp = timestamp;
1367 }
1368 }
1369 if (idx < 0) {
1370 SET_ERR("Shutter notification for non-existent frame number %d",
1371 frameNumber);
1372 break;
1373 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001374 ALOGVV("Camera %d: %s: Shutter fired for frame %d at %lld",
1375 mId, __FUNCTION__, frameNumber, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001376 // Call listener, if any
1377 if (listener != NULL) {
1378 listener->notifyShutter(frameNumber, timestamp);
1379 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001380 break;
1381 }
1382 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001383 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001384 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001385 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001386}
1387
1388/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 * RequestThread inner class methods
1390 */
1391
1392Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
1393 camera3_device_t *hal3Device) :
1394 Thread(false),
1395 mParent(parent),
1396 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001397 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001398 mReconfigured(false),
1399 mDoPause(false),
1400 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001401 mFrameNumber(0),
1402 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403}
1404
1405void Camera3Device::RequestThread::configurationComplete() {
1406 Mutex::Autolock l(mRequestLock);
1407 mReconfigured = true;
1408}
1409
1410status_t Camera3Device::RequestThread::queueRequest(
1411 sp<CaptureRequest> request) {
1412 Mutex::Autolock l(mRequestLock);
1413 mRequestQueue.push_back(request);
1414
1415 return OK;
1416}
1417
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001418
1419status_t Camera3Device::RequestThread::queueTrigger(
1420 RequestTrigger trigger[],
1421 size_t count) {
1422
1423 Mutex::Autolock l(mTriggerMutex);
1424 status_t ret;
1425
1426 for (size_t i = 0; i < count; ++i) {
1427 ret = queueTriggerLocked(trigger[i]);
1428
1429 if (ret != OK) {
1430 return ret;
1431 }
1432 }
1433
1434 return OK;
1435}
1436
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001437int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1438 sp<Camera3Device> d = device.promote();
1439 if (d != NULL) return d->mId;
1440 return 0;
1441}
1442
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001443status_t Camera3Device::RequestThread::queueTriggerLocked(
1444 RequestTrigger trigger) {
1445
1446 uint32_t tag = trigger.metadataTag;
1447 ssize_t index = mTriggerMap.indexOfKey(tag);
1448
1449 switch (trigger.getTagType()) {
1450 case TYPE_BYTE:
1451 // fall-through
1452 case TYPE_INT32:
1453 break;
1454 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001455 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1456 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001457 return INVALID_OPERATION;
1458 }
1459
1460 /**
1461 * Collect only the latest trigger, since we only have 1 field
1462 * in the request settings per trigger tag, and can't send more than 1
1463 * trigger per request.
1464 */
1465 if (index != NAME_NOT_FOUND) {
1466 mTriggerMap.editValueAt(index) = trigger;
1467 } else {
1468 mTriggerMap.add(tag, trigger);
1469 }
1470
1471 return OK;
1472}
1473
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001474status_t Camera3Device::RequestThread::setRepeatingRequests(
1475 const RequestList &requests) {
1476 Mutex::Autolock l(mRequestLock);
1477 mRepeatingRequests.clear();
1478 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1479 requests.begin(), requests.end());
1480 return OK;
1481}
1482
1483status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1484 Mutex::Autolock l(mRequestLock);
1485 mRepeatingRequests.clear();
1486 return OK;
1487}
1488
1489void Camera3Device::RequestThread::setPaused(bool paused) {
1490 Mutex::Autolock l(mPauseLock);
1491 mDoPause = paused;
1492 mDoPauseSignal.signal();
1493}
1494
1495status_t Camera3Device::RequestThread::waitUntilPaused(nsecs_t timeout) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001496 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001497 status_t res;
1498 Mutex::Autolock l(mPauseLock);
1499 while (!mPaused) {
1500 res = mPausedSignal.waitRelative(mPauseLock, timeout);
1501 if (res == TIMED_OUT) {
1502 return res;
1503 }
1504 }
1505 return OK;
1506}
1507
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001508status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1509 int32_t requestId, nsecs_t timeout) {
1510 Mutex::Autolock l(mLatestRequestMutex);
1511 status_t res;
1512 while (mLatestRequestId != requestId) {
1513 nsecs_t startTime = systemTime();
1514
1515 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1516 if (res != OK) return res;
1517
1518 timeout -= (systemTime() - startTime);
1519 }
1520
1521 return OK;
1522}
1523
1524
1525
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001526bool Camera3Device::RequestThread::threadLoop() {
1527
1528 status_t res;
1529
1530 // Handle paused state.
1531 if (waitIfPaused()) {
1532 return true;
1533 }
1534
1535 // Get work to do
1536
1537 sp<CaptureRequest> nextRequest = waitForNextRequest();
1538 if (nextRequest == NULL) {
1539 return true;
1540 }
1541
1542 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001544 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001546 // Insert any queued triggers (before metadata is locked)
1547 int32_t triggerCount;
1548 res = insertTriggers(nextRequest);
1549 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001550 SET_ERR("RequestThread: Unable to insert triggers "
1551 "(capture request %d, HAL device: %s (%d)",
1552 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001553 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1554 return false;
1555 }
1556 triggerCount = res;
1557
1558 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1559
1560 // If the request is the same as last, or we had triggers last time
1561 if (mPrevRequest != nextRequest || triggersMixedIn) {
1562 /**
1563 * The request should be presorted so accesses in HAL
1564 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1565 */
1566 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001567 request.settings = nextRequest->mSettings.getAndLock();
1568 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001569 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1570
1571 IF_ALOGV() {
1572 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1573 find_camera_metadata_ro_entry(
1574 request.settings,
1575 ANDROID_CONTROL_AF_TRIGGER,
1576 &e
1577 );
1578 if (e.count > 0) {
1579 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1580 __FUNCTION__,
1581 mFrameNumber+1,
1582 e.data.u8[0]);
1583 }
1584 }
1585 } else {
1586 // leave request.settings NULL to indicate 'reuse latest given'
1587 ALOGVV("%s: Request settings are REUSED",
1588 __FUNCTION__);
1589 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590
1591 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592
1593 // Fill in buffers
1594
1595 if (nextRequest->mInputStream != NULL) {
1596 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001597 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001599 SET_ERR("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001600 " %s (%d)", strerror(-res), res);
1601 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1602 return true;
1603 }
1604 } else {
1605 request.input_buffer = NULL;
1606 }
1607
1608 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1609 nextRequest->mOutputStreams.size());
1610 request.output_buffers = outputBuffers.array();
1611 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1612 res = nextRequest->mOutputStreams.editItemAt(i)->
1613 getBuffer(&outputBuffers.editItemAt(i));
1614 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001615 SET_ERR("RequestThread: Can't get output buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616 "%s (%d)", strerror(-res), res);
1617 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1618 return true;
1619 }
1620 request.num_output_buffers++;
1621 }
1622
1623 request.frame_number = mFrameNumber++;
1624
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001625 // Log request in the in-flight queue
1626 sp<Camera3Device> parent = mParent.promote();
1627 if (parent == NULL) {
1628 CLOGE("RequestThread: Parent is gone");
1629 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1630 return false;
1631 }
1632
1633 res = parent->registerInFlight(request.frame_number,
1634 request.num_output_buffers);
1635 if (res != OK) {
1636 SET_ERR("RequestThread: Unable to register new in-flight request:"
1637 " %s (%d)", strerror(-res), res);
1638 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1639 return false;
1640 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001641
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001642 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001643 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1644 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001645 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001646 ATRACE_END();
1647
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001649 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001650 " device: %s (%d)", request.frame_number, strerror(-res), res);
1651 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1652 return false;
1653 }
1654
1655 if (request.settings != NULL) {
1656 nextRequest->mSettings.unlock(request.settings);
1657 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001658
1659 // Remove any previously queued triggers (after unlock)
1660 res = removeTriggers(mPrevRequest);
1661 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001662 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001663 "(capture request %d, HAL device: %s (%d)",
1664 request.frame_number, strerror(-res), res);
1665 return false;
1666 }
1667 mPrevTriggers = triggerCount;
1668
1669 // Read android.request.id from the request settings metadata
1670 // - inform waitUntilRequestProcessed thread of a new request ID
1671 {
1672 Mutex::Autolock al(mLatestRequestMutex);
1673
1674 camera_metadata_entry_t requestIdEntry =
1675 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1676 if (requestIdEntry.count > 0) {
1677 mLatestRequestId = requestIdEntry.data.i32[0];
1678 } else {
1679 ALOGW("%s: Did not have android.request.id set in the request",
1680 __FUNCTION__);
1681 mLatestRequestId = NAME_NOT_FOUND;
1682 }
1683
1684 mLatestRequestSignal.signal();
1685 }
1686
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001687 // Return input buffer back to framework
1688 if (request.input_buffer != NULL) {
1689 Camera3Stream *stream =
1690 Camera3Stream::cast(request.input_buffer->stream);
1691 res = stream->returnInputBuffer(*(request.input_buffer));
1692 // Note: stream may be deallocated at this point, if this buffer was the
1693 // last reference to it.
1694 if (res != OK) {
1695 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1696 " its stream:%s (%d)", __FUNCTION__,
1697 request.frame_number, strerror(-res), res);
1698 // TODO: Report error upstream
1699 }
1700 }
1701
1702
1703
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001704 return true;
1705}
1706
1707void Camera3Device::RequestThread::cleanUpFailedRequest(
1708 camera3_capture_request_t &request,
1709 sp<CaptureRequest> &nextRequest,
1710 Vector<camera3_stream_buffer_t> &outputBuffers) {
1711
1712 if (request.settings != NULL) {
1713 nextRequest->mSettings.unlock(request.settings);
1714 }
1715 if (request.input_buffer != NULL) {
1716 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001717 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001718 }
1719 for (size_t i = 0; i < request.num_output_buffers; i++) {
1720 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1721 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1722 outputBuffers[i], 0);
1723 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724}
1725
1726sp<Camera3Device::CaptureRequest>
1727 Camera3Device::RequestThread::waitForNextRequest() {
1728 status_t res;
1729 sp<CaptureRequest> nextRequest;
1730
1731 // Optimized a bit for the simple steady-state case (single repeating
1732 // request), to avoid putting that request in the queue temporarily.
1733 Mutex::Autolock l(mRequestLock);
1734
1735 while (mRequestQueue.empty()) {
1736 if (!mRepeatingRequests.empty()) {
1737 // Always atomically enqueue all requests in a repeating request
1738 // list. Guarantees a complete in-sequence set of captures to
1739 // application.
1740 const RequestList &requests = mRepeatingRequests;
1741 RequestList::const_iterator firstRequest =
1742 requests.begin();
1743 nextRequest = *firstRequest;
1744 mRequestQueue.insert(mRequestQueue.end(),
1745 ++firstRequest,
1746 requests.end());
1747 // No need to wait any longer
1748 break;
1749 }
1750
1751 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
1752
1753 if (res == TIMED_OUT) {
1754 // Signal that we're paused by starvation
1755 Mutex::Autolock pl(mPauseLock);
1756 if (mPaused == false) {
1757 mPaused = true;
1758 mPausedSignal.signal();
1759 }
1760 // Stop waiting for now and let thread management happen
1761 return NULL;
1762 }
1763 }
1764
1765 if (nextRequest == NULL) {
1766 // Don't have a repeating request already in hand, so queue
1767 // must have an entry now.
1768 RequestList::iterator firstRequest =
1769 mRequestQueue.begin();
1770 nextRequest = *firstRequest;
1771 mRequestQueue.erase(firstRequest);
1772 }
1773
1774 // Not paused
1775 Mutex::Autolock pl(mPauseLock);
1776 mPaused = false;
1777
1778 // Check if we've reconfigured since last time, and reset the preview
1779 // request if so. Can't use 'NULL request == repeat' across configure calls.
1780 if (mReconfigured) {
1781 mPrevRequest.clear();
1782 mReconfigured = false;
1783 }
1784
1785 return nextRequest;
1786}
1787
1788bool Camera3Device::RequestThread::waitIfPaused() {
1789 status_t res;
1790 Mutex::Autolock l(mPauseLock);
1791 while (mDoPause) {
1792 // Signal that we're paused by request
1793 if (mPaused == false) {
1794 mPaused = true;
1795 mPausedSignal.signal();
1796 }
1797 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
1798 if (res == TIMED_OUT) {
1799 return true;
1800 }
1801 }
1802 // We don't set mPaused to false here, because waitForNextRequest needs
1803 // to further manage the paused state in case of starvation.
1804 return false;
1805}
1806
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
1808 sp<Camera3Device> parent = mParent.promote();
1809 if (parent != NULL) {
1810 va_list args;
1811 va_start(args, fmt);
1812
1813 parent->setErrorStateV(fmt, args);
1814
1815 va_end(args);
1816 }
1817}
1818
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001819status_t Camera3Device::RequestThread::insertTriggers(
1820 const sp<CaptureRequest> &request) {
1821
1822 Mutex::Autolock al(mTriggerMutex);
1823
1824 CameraMetadata &metadata = request->mSettings;
1825 size_t count = mTriggerMap.size();
1826
1827 for (size_t i = 0; i < count; ++i) {
1828 RequestTrigger trigger = mTriggerMap.valueAt(i);
1829
1830 uint32_t tag = trigger.metadataTag;
1831 camera_metadata_entry entry = metadata.find(tag);
1832
1833 if (entry.count > 0) {
1834 /**
1835 * Already has an entry for this trigger in the request.
1836 * Rewrite it with our requested trigger value.
1837 */
1838 RequestTrigger oldTrigger = trigger;
1839
1840 oldTrigger.entryValue = entry.data.u8[0];
1841
1842 mTriggerReplacedMap.add(tag, oldTrigger);
1843 } else {
1844 /**
1845 * More typical, no trigger entry, so we just add it
1846 */
1847 mTriggerRemovedMap.add(tag, trigger);
1848 }
1849
1850 status_t res;
1851
1852 switch (trigger.getTagType()) {
1853 case TYPE_BYTE: {
1854 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1855 res = metadata.update(tag,
1856 &entryValue,
1857 /*count*/1);
1858 break;
1859 }
1860 case TYPE_INT32:
1861 res = metadata.update(tag,
1862 &trigger.entryValue,
1863 /*count*/1);
1864 break;
1865 default:
1866 ALOGE("%s: Type not supported: 0x%x",
1867 __FUNCTION__,
1868 trigger.getTagType());
1869 return INVALID_OPERATION;
1870 }
1871
1872 if (res != OK) {
1873 ALOGE("%s: Failed to update request metadata with trigger tag %s"
1874 ", value %d", __FUNCTION__, trigger.getTagName(),
1875 trigger.entryValue);
1876 return res;
1877 }
1878
1879 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
1880 trigger.getTagName(),
1881 trigger.entryValue);
1882 }
1883
1884 mTriggerMap.clear();
1885
1886 return count;
1887}
1888
1889status_t Camera3Device::RequestThread::removeTriggers(
1890 const sp<CaptureRequest> &request) {
1891 Mutex::Autolock al(mTriggerMutex);
1892
1893 CameraMetadata &metadata = request->mSettings;
1894
1895 /**
1896 * Replace all old entries with their old values.
1897 */
1898 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
1899 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
1900
1901 status_t res;
1902
1903 uint32_t tag = trigger.metadataTag;
1904 switch (trigger.getTagType()) {
1905 case TYPE_BYTE: {
1906 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
1907 res = metadata.update(tag,
1908 &entryValue,
1909 /*count*/1);
1910 break;
1911 }
1912 case TYPE_INT32:
1913 res = metadata.update(tag,
1914 &trigger.entryValue,
1915 /*count*/1);
1916 break;
1917 default:
1918 ALOGE("%s: Type not supported: 0x%x",
1919 __FUNCTION__,
1920 trigger.getTagType());
1921 return INVALID_OPERATION;
1922 }
1923
1924 if (res != OK) {
1925 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
1926 ", trigger value %d", __FUNCTION__,
1927 trigger.getTagName(), trigger.entryValue);
1928 return res;
1929 }
1930 }
1931 mTriggerReplacedMap.clear();
1932
1933 /**
1934 * Remove all new entries.
1935 */
1936 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
1937 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
1938 status_t res = metadata.erase(trigger.metadataTag);
1939
1940 if (res != OK) {
1941 ALOGE("%s: Failed to erase metadata with trigger tag %s"
1942 ", trigger value %d", __FUNCTION__,
1943 trigger.getTagName(), trigger.entryValue);
1944 return res;
1945 }
1946 }
1947 mTriggerRemovedMap.clear();
1948
1949 return OK;
1950}
1951
1952
1953
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001954/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001955 * Static callback forwarding methods from HAL to instance
1956 */
1957
1958void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
1959 const camera3_capture_result *result) {
1960 Camera3Device *d =
1961 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1962 d->processCaptureResult(result);
1963}
1964
1965void Camera3Device::sNotify(const camera3_callback_ops *cb,
1966 const camera3_notify_msg *msg) {
1967 Camera3Device *d =
1968 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
1969 d->notify(msg);
1970}
1971
1972}; // namespace android