blob: c320d6c134221309e2c70c7c5b695bb65c619d81 [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>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Igor Murashkinff3e31d2013-10-23 16:40:06 -070044#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045#include "device3/Camera3Device.h"
46#include "device3/Camera3OutputStream.h"
47#include "device3/Camera3InputStream.h"
48#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080049
50using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051
52namespace android {
53
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054Camera3Device::Camera3Device(int id):
55 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070058 mNextResultFrameNumber(0),
59 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070060 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061{
62 ATRACE_CALL();
63 camera3_callback_ops::notify = &sNotify;
64 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
65 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
66}
67
68Camera3Device::~Camera3Device()
69{
70 ATRACE_CALL();
71 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
72 disconnect();
73}
74
Igor Murashkin71381052013-03-04 14:53:08 -080075int Camera3Device::getId() const {
76 return mId;
77}
78
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080079/**
80 * CameraDeviceBase interface
81 */
82
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083status_t Camera3Device::initialize(camera_module_t *module)
84{
85 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070086 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080087 Mutex::Autolock l(mLock);
88
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080090 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070091 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092 return INVALID_OPERATION;
93 }
94
95 /** Open HAL device */
96
97 status_t res;
98 String8 deviceName = String8::format("%d", mId);
99
100 camera3_device_t *device;
101
102 res = module->common.methods->open(&module->common, deviceName.string(),
103 reinterpret_cast<hw_device_t**>(&device));
104
105 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700106 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107 return res;
108 }
109
110 /** Cross-check device version */
111
112 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700113 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 device->common.version);
117 device->common.close(&device->common);
118 return BAD_VALUE;
119 }
120
121 camera_info info;
122 res = module->get_camera_info(mId, &info);
123 if (res != OK) return res;
124
125 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700126 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
127 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800128 device->common.version, info.device_version);
129 device->common.close(&device->common);
130 return BAD_VALUE;
131 }
132
133 /** Initialize device with callback functions */
134
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700135 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800136 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700137 ATRACE_END();
138
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700140 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
141 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 device->common.close(&device->common);
143 return BAD_VALUE;
144 }
145
146 /** Get vendor metadata tags */
147
148 mVendorTagOps.get_camera_vendor_section_name = NULL;
149
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700150 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153
154 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
155 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
156 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700157 SET_ERR_L("Unable to set tag ops: %s (%d)",
158 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800159 device->common.close(&device->common);
160 return res;
161 }
162 }
163
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700164 /** Start up status tracker thread */
165 mStatusTracker = new StatusTracker(this);
166 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
167 if (res != OK) {
168 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
169 strerror(-res), res);
170 device->common.close(&device->common);
171 mStatusTracker.clear();
172 return res;
173 }
174
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175 /** Start up request queue thread */
176
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700177 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800178 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800179 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700180 SET_ERR_L("Unable to start request queue thread: %s (%d)",
181 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800183 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 return res;
185 }
186
187 /** Everything is good to go */
188
189 mDeviceInfo = info.static_camera_characteristics;
190 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700191 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800192 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700193 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700194 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800195
196 return OK;
197}
198
199status_t Camera3Device::disconnect() {
200 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700201 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800202
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800203 ALOGV("%s: E", __FUNCTION__);
204
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700205 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800206
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700207 {
208 Mutex::Autolock l(mLock);
209 if (mStatus == STATUS_UNINITIALIZED) return res;
210
211 if (mStatus == STATUS_ACTIVE ||
212 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
213 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700214 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700215 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700216 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700217 } else {
218 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
219 if (res != OK) {
220 SET_ERR_L("Timeout waiting for HAL to drain");
221 // Continue to close device even in case of error
222 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700223 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800224 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800225
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700226 if (mStatus == STATUS_ERROR) {
227 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700228 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229
230 if (mStatusTracker != NULL) {
231 mStatusTracker->requestExit();
232 }
233
234 if (mRequestThread != NULL) {
235 mRequestThread->requestExit();
236 }
237
238 mOutputStreams.clear();
239 mInputStream.clear();
240 }
241
242 // Joining done without holding mLock, otherwise deadlocks may ensue
243 // as the threads try to access parent state
244 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
245 // HAL may be in a bad state, so waiting for request thread
246 // (which may be stuck in the HAL processCaptureRequest call)
247 // could be dangerous.
248 mRequestThread->join();
249 }
250
251 if (mStatusTracker != NULL) {
252 mStatusTracker->join();
253 }
254
255 {
256 Mutex::Autolock l(mLock);
257
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800258 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800260
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 if (mHal3Device != NULL) {
262 mHal3Device->common.close(&mHal3Device->common);
263 mHal3Device = NULL;
264 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800265
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700266 mStatus = STATUS_UNINITIALIZED;
267 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800268
269 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700270 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800271}
272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273// For dumping/debugging only -
274// try to acquire a lock a few times, eventually give up to proceed with
275// debug/dump operations
276bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
277 bool gotLock = false;
278 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
279 if (lock.tryLock() == NO_ERROR) {
280 gotLock = true;
281 break;
282 } else {
283 usleep(kDumpSleepDuration);
284 }
285 }
286 return gotLock;
287}
288
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800289status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
290 ATRACE_CALL();
291 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700292
293 // Try to lock, but continue in case of failure (to avoid blocking in
294 // deadlocks)
295 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
296 bool gotLock = tryLockSpinRightRound(mLock);
297
298 ALOGW_IF(!gotInterfaceLock,
299 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
300 mId, __FUNCTION__);
301 ALOGW_IF(!gotLock,
302 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
303 mId, __FUNCTION__);
304
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800306
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 const char *status =
308 mStatus == STATUS_ERROR ? "ERROR" :
309 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700310 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
311 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800312 mStatus == STATUS_ACTIVE ? "ACTIVE" :
313 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800315 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700316 if (mStatus == STATUS_ERROR) {
317 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
318 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 lines.appendFormat(" Stream configuration:\n");
320
321 if (mInputStream != NULL) {
322 write(fd, lines.string(), lines.size());
323 mInputStream->dump(fd, args);
324 } else {
325 lines.appendFormat(" No input stream.\n");
326 write(fd, lines.string(), lines.size());
327 }
328 for (size_t i = 0; i < mOutputStreams.size(); i++) {
329 mOutputStreams[i]->dump(fd,args);
330 }
331
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700332 lines = String8(" In-flight requests:\n");
333 if (mInFlightMap.size() == 0) {
334 lines.append(" None\n");
335 } else {
336 for (size_t i = 0; i < mInFlightMap.size(); i++) {
337 InFlightRequest r = mInFlightMap.valueAt(i);
338 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
339 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
340 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
341 r.numBuffersLeft);
342 }
343 }
344 write(fd, lines.string(), lines.size());
345
Igor Murashkin1e479c02013-09-06 16:55:14 -0700346 {
347 lines = String8(" Last request sent:\n");
348 write(fd, lines.string(), lines.size());
349
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700350 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700351 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
352 }
353
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800354 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700355 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800356 write(fd, lines.string(), lines.size());
357 mHal3Device->ops->dump(mHal3Device, fd);
358 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800359
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700360 if (gotLock) mLock.unlock();
361 if (gotInterfaceLock) mInterfaceLock.unlock();
362
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800363 return OK;
364}
365
366const CameraMetadata& Camera3Device::info() const {
367 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800368 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
369 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700370 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800371 mStatus == STATUS_ERROR ?
372 "when in error state" : "before init");
373 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800374 return mDeviceInfo;
375}
376
377status_t Camera3Device::capture(CameraMetadata &request) {
378 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700379 status_t res;
380 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800381 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800382
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700383 // TODO: take ownership of the request
384
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 switch (mStatus) {
386 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700387 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800388 return INVALID_OPERATION;
389 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700390 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800391 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700392 case STATUS_UNCONFIGURED:
393 // May be lazily configuring streams, will check during setup
394 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 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 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
404 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700405 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406 return BAD_VALUE;
407 }
408
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700409 res = mRequestThread->queueRequest(newRequest);
410 if (res == OK) {
411 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
412 if (res != OK) {
413 SET_ERR_L("Can't transition to active in %f seconds!",
414 kActiveTimeout/1e9);
415 }
416 ALOGV("Camera %d: Capture request enqueued", mId);
417 }
418 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800419}
420
421
422status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
423 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700424 status_t res;
425 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800426 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800427
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800428 switch (mStatus) {
429 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700430 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800431 return INVALID_OPERATION;
432 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700433 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800434 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700435 case STATUS_UNCONFIGURED:
436 // May be lazily configuring streams, will check during setup
437 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800438 case STATUS_ACTIVE:
439 // OK
440 break;
441 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700442 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800443 return INVALID_OPERATION;
444 }
445
446 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
447 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700448 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800449 return BAD_VALUE;
450 }
451
452 RequestList newRepeatingRequests;
453 newRepeatingRequests.push_back(newRepeatingRequest);
454
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700455 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
456 if (res == OK) {
457 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
458 if (res != OK) {
459 SET_ERR_L("Can't transition to active in %f seconds!",
460 kActiveTimeout/1e9);
461 }
462 ALOGV("Camera %d: Repeating request set", mId);
463 }
464 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800465}
466
467
468sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
469 const CameraMetadata &request) {
470 status_t res;
471
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700472 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800473 res = configureStreamsLocked();
474 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700475 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476 return NULL;
477 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700478 if (mStatus == STATUS_UNCONFIGURED) {
479 CLOGE("No streams configured");
480 return NULL;
481 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800482 }
483
484 sp<CaptureRequest> newRequest = createCaptureRequest(request);
485 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800486}
487
488status_t Camera3Device::clearStreamingRequest() {
489 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700490 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800491 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800492
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800493 switch (mStatus) {
494 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700495 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800496 return INVALID_OPERATION;
497 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700498 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800499 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700500 case STATUS_UNCONFIGURED:
501 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800502 case STATUS_ACTIVE:
503 // OK
504 break;
505 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700506 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800507 return INVALID_OPERATION;
508 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700509 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800511}
512
513status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
514 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700515 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800516
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700517 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800518}
519
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700520status_t Camera3Device::createInputStream(
521 uint32_t width, uint32_t height, int format, int *id) {
522 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700524 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700525 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
526 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700527
528 status_t res;
529 bool wasActive = false;
530
531 switch (mStatus) {
532 case STATUS_ERROR:
533 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
534 return INVALID_OPERATION;
535 case STATUS_UNINITIALIZED:
536 ALOGE("%s: Device not initialized", __FUNCTION__);
537 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700538 case STATUS_UNCONFIGURED:
539 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700540 // OK
541 break;
542 case STATUS_ACTIVE:
543 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700544 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700545 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700546 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700547 return res;
548 }
549 wasActive = true;
550 break;
551 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700552 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700553 return INVALID_OPERATION;
554 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700555 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700556
557 if (mInputStream != 0) {
558 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
559 return INVALID_OPERATION;
560 }
561
562 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
563 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700564 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700565
566 mInputStream = newStream;
567
568 *id = mNextStreamId++;
569
570 // Continue captures if active at start
571 if (wasActive) {
572 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
573 res = configureStreamsLocked();
574 if (res != OK) {
575 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
576 __FUNCTION__, mNextStreamId, strerror(-res), res);
577 return res;
578 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700579 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700580 }
581
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700582 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700583 return OK;
584}
585
Igor Murashkin2fba5842013-04-22 14:03:54 -0700586
587status_t Camera3Device::createZslStream(
588 uint32_t width, uint32_t height,
589 int depth,
590 /*out*/
591 int *id,
592 sp<Camera3ZslStream>* zslStream) {
593 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700595 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
597 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700598
599 status_t res;
600 bool wasActive = false;
601
602 switch (mStatus) {
603 case STATUS_ERROR:
604 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
605 return INVALID_OPERATION;
606 case STATUS_UNINITIALIZED:
607 ALOGE("%s: Device not initialized", __FUNCTION__);
608 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700609 case STATUS_UNCONFIGURED:
610 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700611 // OK
612 break;
613 case STATUS_ACTIVE:
614 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700615 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700616 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700618 return res;
619 }
620 wasActive = true;
621 break;
622 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700623 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700624 return INVALID_OPERATION;
625 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700627
628 if (mInputStream != 0) {
629 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
630 return INVALID_OPERATION;
631 }
632
633 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
634 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700635 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700636
637 res = mOutputStreams.add(mNextStreamId, newStream);
638 if (res < 0) {
639 ALOGE("%s: Can't add new stream to set: %s (%d)",
640 __FUNCTION__, strerror(-res), res);
641 return res;
642 }
643 mInputStream = newStream;
644
645 *id = mNextStreamId++;
646 *zslStream = newStream;
647
648 // Continue captures if active at start
649 if (wasActive) {
650 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
651 res = configureStreamsLocked();
652 if (res != OK) {
653 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
654 __FUNCTION__, mNextStreamId, strerror(-res), res);
655 return res;
656 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700657 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700658 }
659
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700660 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700661 return OK;
662}
663
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800664status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
665 uint32_t width, uint32_t height, int format, size_t size, int *id) {
666 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700667 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800668 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
670 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800672 status_t res;
673 bool wasActive = false;
674
675 switch (mStatus) {
676 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700677 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800678 return INVALID_OPERATION;
679 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700680 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700682 case STATUS_UNCONFIGURED:
683 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800684 // OK
685 break;
686 case STATUS_ACTIVE:
687 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700688 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800689 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700690 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800691 return res;
692 }
693 wasActive = true;
694 break;
695 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700696 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800697 return INVALID_OPERATION;
698 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700699 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800700
701 sp<Camera3OutputStream> newStream;
702 if (format == HAL_PIXEL_FORMAT_BLOB) {
703 newStream = new Camera3OutputStream(mNextStreamId, consumer,
704 width, height, size, format);
705 } else {
706 newStream = new Camera3OutputStream(mNextStreamId, consumer,
707 width, height, format);
708 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800710
711 res = mOutputStreams.add(mNextStreamId, newStream);
712 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700713 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 return res;
715 }
716
717 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700718 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800719
720 // Continue captures if active at start
721 if (wasActive) {
722 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
723 res = configureStreamsLocked();
724 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700725 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
726 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727 return res;
728 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700729 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700731 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800732 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800733}
734
735status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
736 ATRACE_CALL();
737 (void)outputId; (void)id;
738
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700739 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800740 return INVALID_OPERATION;
741}
742
743
744status_t Camera3Device::getStreamInfo(int id,
745 uint32_t *width, uint32_t *height, uint32_t *format) {
746 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700747 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800748 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800749
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800750 switch (mStatus) {
751 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700752 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 return INVALID_OPERATION;
754 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700755 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800756 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700757 case STATUS_UNCONFIGURED:
758 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800759 case STATUS_ACTIVE:
760 // OK
761 break;
762 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700763 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 return INVALID_OPERATION;
765 }
766
767 ssize_t idx = mOutputStreams.indexOfKey(id);
768 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700769 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 return idx;
771 }
772
773 if (width) *width = mOutputStreams[idx]->getWidth();
774 if (height) *height = mOutputStreams[idx]->getHeight();
775 if (format) *format = mOutputStreams[idx]->getFormat();
776
777 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800778}
779
780status_t Camera3Device::setStreamTransform(int id,
781 int transform) {
782 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700783 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800784 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800785
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800786 switch (mStatus) {
787 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700788 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800789 return INVALID_OPERATION;
790 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700791 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700793 case STATUS_UNCONFIGURED:
794 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 case STATUS_ACTIVE:
796 // OK
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 ssize_t idx = mOutputStreams.indexOfKey(id);
804 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700805 CLOGE("Stream %d does not exist",
806 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800807 return BAD_VALUE;
808 }
809
810 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800811}
812
813status_t Camera3Device::deleteStream(int id) {
814 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816 Mutex::Autolock l(mLock);
817 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800818
Igor Murashkine2172be2013-05-28 15:31:39 -0700819 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
820
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 // CameraDevice semantics require device to already be idle before
822 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700824 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
825 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 }
827
Igor Murashkin2fba5842013-04-22 14:03:54 -0700828 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800829 if (mInputStream != NULL && id == mInputStream->getId()) {
830 deletedStream = mInputStream;
831 mInputStream.clear();
832 } else {
833 ssize_t idx = mOutputStreams.indexOfKey(id);
834 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700835 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 return BAD_VALUE;
837 }
838 deletedStream = mOutputStreams.editValueAt(idx);
839 mOutputStreams.removeItem(id);
840 }
841
842 // Free up the stream endpoint so that it can be used by some other stream
843 res = deletedStream->disconnect();
844 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700845 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800846 // fall through since we want to still list the stream as deleted.
847 }
848 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700849 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850
851 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800852}
853
854status_t Camera3Device::deleteReprocessStream(int id) {
855 ATRACE_CALL();
856 (void)id;
857
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700858 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800859 return INVALID_OPERATION;
860}
861
862
863status_t Camera3Device::createDefaultRequest(int templateId,
864 CameraMetadata *request) {
865 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700866 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700867 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 Mutex::Autolock l(mLock);
869
870 switch (mStatus) {
871 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700872 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800873 return INVALID_OPERATION;
874 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700875 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 case STATUS_UNCONFIGURED:
878 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 case STATUS_ACTIVE:
880 // OK
881 break;
882 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700883 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800884 return INVALID_OPERATION;
885 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800886
887 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700888 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800889 rawRequest = mHal3Device->ops->construct_default_request_settings(
890 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700891 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700892 if (rawRequest == NULL) {
893 SET_ERR_L("HAL is unable to construct default settings for template %d",
894 templateId);
895 return DEAD_OBJECT;
896 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800897 *request = rawRequest;
898
899 return OK;
900}
901
902status_t Camera3Device::waitUntilDrained() {
903 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800905 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800906
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 switch (mStatus) {
908 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700909 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800910 ALOGV("%s: Already idle", __FUNCTION__);
911 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 case STATUS_CONFIGURED:
913 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800914 case STATUS_ERROR:
915 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700916 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 break;
918 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700919 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800920 return INVALID_OPERATION;
921 }
922
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700923 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
924 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
925 return res;
926}
927
928// Pause to reconfigure
929status_t Camera3Device::internalPauseAndWaitLocked() {
930 mRequestThread->setPaused(true);
931 mPauseStateNotify = true;
932
933 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
934 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
935 if (res != OK) {
936 SET_ERR_L("Can't idle device in %f seconds!",
937 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800938 }
939
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 return res;
941}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700943// Resume after internalPauseAndWaitLocked
944status_t Camera3Device::internalResumeLocked() {
945 status_t res;
946
947 mRequestThread->setPaused(false);
948
949 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
950 if (res != OK) {
951 SET_ERR_L("Can't transition to active in %f seconds!",
952 kActiveTimeout/1e9);
953 }
954 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800956}
957
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958status_t Camera3Device::waitUntilStateThenRelock(bool active,
959 nsecs_t timeout) {
960 status_t res = OK;
961 if (active == (mStatus == STATUS_ACTIVE)) {
962 // Desired state already reached
963 return res;
964 }
965
966 bool stateSeen = false;
967 do {
968 mRecentStatusUpdates.clear();
969
970 res = mStatusChanged.waitRelative(mLock, timeout);
971 if (res != OK) break;
972
973 // Check state change history during wait
974 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
975 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
976 stateSeen = true;
977 break;
978 }
979 }
980 } while (!stateSeen);
981
982 return res;
983}
984
985
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800986status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
987 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700988 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800989
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700990 if (listener != NULL && mListener != NULL) {
991 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
992 }
993 mListener = listener;
994
995 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800996}
997
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700998bool Camera3Device::willNotify3A() {
999 return false;
1000}
1001
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001003 status_t res;
1004 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001005
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001006 while (mResultQueue.empty()) {
1007 res = mResultSignal.waitRelative(mOutputLock, timeout);
1008 if (res == TIMED_OUT) {
1009 return res;
1010 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001011 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1012 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001013 return res;
1014 }
1015 }
1016 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001017}
1018
1019status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1020 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001021 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001022
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001023 if (mResultQueue.empty()) {
1024 return NOT_ENOUGH_DATA;
1025 }
1026
1027 CameraMetadata &result = *(mResultQueue.begin());
1028 frame->acquire(result);
1029 mResultQueue.erase(mResultQueue.begin());
1030
1031 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001032}
1033
1034status_t Camera3Device::triggerAutofocus(uint32_t id) {
1035 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001036 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001037
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001038 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1039 // Mix-in this trigger into the next request and only the next request.
1040 RequestTrigger trigger[] = {
1041 {
1042 ANDROID_CONTROL_AF_TRIGGER,
1043 ANDROID_CONTROL_AF_TRIGGER_START
1044 },
1045 {
1046 ANDROID_CONTROL_AF_TRIGGER_ID,
1047 static_cast<int32_t>(id)
1048 },
1049 };
1050
1051 return mRequestThread->queueTrigger(trigger,
1052 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053}
1054
1055status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1056 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001057 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001059 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1060 // Mix-in this trigger into the next request and only the next request.
1061 RequestTrigger trigger[] = {
1062 {
1063 ANDROID_CONTROL_AF_TRIGGER,
1064 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1065 },
1066 {
1067 ANDROID_CONTROL_AF_TRIGGER_ID,
1068 static_cast<int32_t>(id)
1069 },
1070 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001072 return mRequestThread->queueTrigger(trigger,
1073 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001074}
1075
1076status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1077 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001080 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1081 // Mix-in this trigger into the next request and only the next request.
1082 RequestTrigger trigger[] = {
1083 {
1084 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1085 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1086 },
1087 {
1088 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1089 static_cast<int32_t>(id)
1090 },
1091 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001093 return mRequestThread->queueTrigger(trigger,
1094 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001095}
1096
1097status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1098 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1099 ATRACE_CALL();
1100 (void)reprocessStreamId; (void)buffer; (void)listener;
1101
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001102 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001103 return INVALID_OPERATION;
1104}
1105
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001106status_t Camera3Device::flush() {
1107 ATRACE_CALL();
1108 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001109 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001110 Mutex::Autolock l(mLock);
1111
1112 mRequestThread->clear();
1113 return mHal3Device->ops->flush(mHal3Device);
1114}
1115
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001117 * Methods called by subclasses
1118 */
1119
1120void Camera3Device::notifyStatus(bool idle) {
1121 {
1122 // Need mLock to safely update state and synchronize to current
1123 // state of methods in flight.
1124 Mutex::Autolock l(mLock);
1125 // We can get various system-idle notices from the status tracker
1126 // while starting up. Only care about them if we've actually sent
1127 // in some requests recently.
1128 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1129 return;
1130 }
1131 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1132 idle ? "idle" : "active");
1133 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1134 mRecentStatusUpdates.add(mStatus);
1135 mStatusChanged.signal();
1136
1137 // Skip notifying listener if we're doing some user-transparent
1138 // state changes
1139 if (mPauseStateNotify) return;
1140 }
1141 NotificationListener *listener;
1142 {
1143 Mutex::Autolock l(mOutputLock);
1144 listener = mListener;
1145 }
1146 if (idle && listener != NULL) {
1147 listener->notifyIdle();
1148 }
1149}
1150
1151/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001152 * Camera3Device private methods
1153 */
1154
1155sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1156 const CameraMetadata &request) {
1157 ATRACE_CALL();
1158 status_t res;
1159
1160 sp<CaptureRequest> newRequest = new CaptureRequest;
1161 newRequest->mSettings = request;
1162
1163 camera_metadata_entry_t inputStreams =
1164 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1165 if (inputStreams.count > 0) {
1166 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001167 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001168 CLOGE("Request references unknown input stream %d",
1169 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 return NULL;
1171 }
1172 // Lazy completion of stream configuration (allocation/registration)
1173 // on first use
1174 if (mInputStream->isConfiguring()) {
1175 res = mInputStream->finishConfiguration(mHal3Device);
1176 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001177 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001178 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001179 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001180 return NULL;
1181 }
1182 }
1183
1184 newRequest->mInputStream = mInputStream;
1185 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1186 }
1187
1188 camera_metadata_entry_t streams =
1189 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1190 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001191 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001192 return NULL;
1193 }
1194
1195 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001196 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001198 CLOGE("Request references unknown stream %d",
1199 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 return NULL;
1201 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001202 sp<Camera3OutputStreamInterface> stream =
1203 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204
1205 // Lazy completion of stream configuration (allocation/registration)
1206 // on first use
1207 if (stream->isConfiguring()) {
1208 res = stream->finishConfiguration(mHal3Device);
1209 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001210 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1211 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001212 return NULL;
1213 }
1214 }
1215
1216 newRequest->mOutputStreams.push(stream);
1217 }
1218 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1219
1220 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221}
1222
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223status_t Camera3Device::configureStreamsLocked() {
1224 ATRACE_CALL();
1225 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001227 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001228 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229 return INVALID_OPERATION;
1230 }
1231
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001232 if (!mNeedConfig) {
1233 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1234 return OK;
1235 }
1236
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001239
1240 camera3_stream_configuration config;
1241
1242 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1243
1244 Vector<camera3_stream_t*> streams;
1245 streams.setCapacity(config.num_streams);
1246
1247 if (mInputStream != NULL) {
1248 camera3_stream_t *inputStream;
1249 inputStream = mInputStream->startConfiguration();
1250 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001251 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001252 return INVALID_OPERATION;
1253 }
1254 streams.add(inputStream);
1255 }
1256
1257 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001258
1259 // Don't configure bidi streams twice, nor add them twice to the list
1260 if (mOutputStreams[i].get() ==
1261 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1262
1263 config.num_streams--;
1264 continue;
1265 }
1266
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001267 camera3_stream_t *outputStream;
1268 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1269 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001270 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 return INVALID_OPERATION;
1272 }
1273 streams.add(outputStream);
1274 }
1275
1276 config.streams = streams.editArray();
1277
1278 // Do the HAL configuration; will potentially touch stream
1279 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001280 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001282 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001283
1284 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001285 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1286 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001287 return res;
1288 }
1289
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001290 // Finish all stream configuration immediately.
1291 // TODO: Try to relax this later back to lazy completion, which should be
1292 // faster
1293
Igor Murashkin073f8572013-05-02 14:59:28 -07001294 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001295 res = mInputStream->finishConfiguration(mHal3Device);
1296 if (res != OK) {
1297 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1298 mInputStream->getId(), strerror(-res), res);
1299 return res;
1300 }
1301 }
1302
1303 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001304 sp<Camera3OutputStreamInterface> outputStream =
1305 mOutputStreams.editValueAt(i);
1306 if (outputStream->isConfiguring()) {
1307 res = outputStream->finishConfiguration(mHal3Device);
1308 if (res != OK) {
1309 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1310 outputStream->getId(), strerror(-res), res);
1311 return res;
1312 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001313 }
1314 }
1315
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001316 // Request thread needs to know to avoid using repeat-last-settings protocol
1317 // across configure_streams() calls
1318 mRequestThread->configurationComplete();
1319
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001320 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001321
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001322 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001323
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001324 if (config.num_streams > 0) {
1325 mStatus = STATUS_CONFIGURED;
1326 } else {
1327 mStatus = STATUS_UNCONFIGURED;
1328 }
1329
1330 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1331
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001332 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001333}
1334
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001335void Camera3Device::setErrorState(const char *fmt, ...) {
1336 Mutex::Autolock l(mLock);
1337 va_list args;
1338 va_start(args, fmt);
1339
1340 setErrorStateLockedV(fmt, args);
1341
1342 va_end(args);
1343}
1344
1345void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1346 Mutex::Autolock l(mLock);
1347 setErrorStateLockedV(fmt, args);
1348}
1349
1350void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1351 va_list args;
1352 va_start(args, fmt);
1353
1354 setErrorStateLockedV(fmt, args);
1355
1356 va_end(args);
1357}
1358
1359void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001360 // Print out all error messages to log
1361 String8 errorCause = String8::formatV(fmt, args);
1362 ALOGE("Camera %d: %s", mId, errorCause.string());
1363
1364 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001365 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001366
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001367 // Save stack trace. View by dumping it later.
1368 CameraTraces::saveTrace();
1369 // TODO: consider adding errorCause and client pid/procname
1370
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001371 mErrorCause = errorCause;
1372
1373 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001374 mStatus = STATUS_ERROR;
1375}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001376
1377/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 * In-flight request management
1379 */
1380
1381status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001383 ATRACE_CALL();
1384 Mutex::Autolock l(mInFlightLock);
1385
1386 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001387 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001388 if (res < 0) return res;
1389
1390 return OK;
1391}
1392
1393/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394 * Camera HAL device callback methods
1395 */
1396
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001397void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001398 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001399
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001400 status_t res;
1401
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001402 uint32_t frameNumber = result->frame_number;
1403 if (result->result == NULL && result->num_output_buffers == 0) {
1404 SET_ERR("No result data provided by HAL for frame %d",
1405 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001406 return;
1407 }
1408
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001409 // Get capture timestamp from list of in-flight requests, where it was added
1410 // by the shutter notification for this frame. Then update the in-flight
1411 // status and remove the in-flight entry if all result data has been
1412 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001413 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001414 {
1415 Mutex::Autolock l(mInFlightLock);
1416 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1417 if (idx == NAME_NOT_FOUND) {
1418 SET_ERR("Unknown frame number for capture result: %d",
1419 frameNumber);
1420 return;
1421 }
1422 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1423 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001424 /**
1425 * One of the following must happen before it's legal to call process_capture_result:
1426 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1427 * - CAMERA3_MSG_ERROR (expected during flush)
1428 */
1429 if (request.requestStatus == OK && timestamp == 0) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001430 SET_ERR("Called before shutter notify for frame %d",
1431 frameNumber);
1432 return;
1433 }
1434
1435 if (result->result != NULL) {
1436 if (request.haveResultMetadata) {
1437 SET_ERR("Called multiple times with metadata for frame %d",
1438 frameNumber);
1439 return;
1440 }
1441 request.haveResultMetadata = true;
1442 }
1443
1444 request.numBuffersLeft -= result->num_output_buffers;
1445
1446 if (request.numBuffersLeft < 0) {
1447 SET_ERR("Too many buffers returned for frame %d",
1448 frameNumber);
1449 return;
1450 }
1451
1452 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001453 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001454 mInFlightMap.removeItemsAt(idx, 1);
1455 }
1456
1457 // Sanity check - if we have too many in-flight frames, something has
1458 // likely gone wrong
1459 if (mInFlightMap.size() > kInFlightWarnLimit) {
1460 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1461 }
1462
1463 }
1464
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001465 // Process the result metadata, if provided
1466 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001467 Mutex::Autolock l(mOutputLock);
1468
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001469 if (frameNumber != mNextResultFrameNumber) {
1470 SET_ERR("Out-of-order capture result metadata submitted! "
1471 "(got frame number %d, expecting %d)",
1472 frameNumber, mNextResultFrameNumber);
1473 return;
1474 }
1475 mNextResultFrameNumber++;
1476
1477 CameraMetadata &captureResult =
1478 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001479
1480 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001481 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001482 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001484 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001485 } else {
1486 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001487 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001488 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001489
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001490 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001491
1492 camera_metadata_entry entry =
1493 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1494 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001495 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001496 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001497 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001498 SET_ERR("Timestamp mismatch between shutter notify and result"
1499 " metadata for frame %d (%lld vs %lld respectively)",
1500 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001501 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001502 } // scope for mOutputLock
1503
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001504 // Return completed buffers to their streams with the timestamp
1505
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001506 for (size_t i = 0; i < result->num_output_buffers; i++) {
1507 Camera3Stream *stream =
1508 Camera3Stream::cast(result->output_buffers[i].stream);
1509 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1510 // Note: stream may be deallocated at this point, if this buffer was the
1511 // last reference to it.
1512 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001513 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001514 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001515 }
1516 }
1517
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001518 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001519
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001520 if (result->result != NULL) {
1521 mResultSignal.signal();
1522 }
1523
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524}
1525
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001526
1527
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001528void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001529 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001530 NotificationListener *listener;
1531 {
1532 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001533 listener = mListener;
1534 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001535
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001536 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001537 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001538 return;
1539 }
1540
1541 switch (msg->type) {
1542 case CAMERA3_MSG_ERROR: {
1543 int streamId = 0;
1544 if (msg->message.error.error_stream != NULL) {
1545 Camera3Stream *stream =
1546 Camera3Stream::cast(
1547 msg->message.error.error_stream);
1548 streamId = stream->getId();
1549 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001550 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1551 mId, __FUNCTION__, msg->message.error.frame_number,
1552 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001553
1554 // Set request error status for the request in the in-flight tracking
1555 {
1556 Mutex::Autolock l(mInFlightLock);
1557 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1558 if (idx >= 0) {
1559 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1560 }
1561 }
1562
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001563 if (listener != NULL) {
1564 listener->notifyError(msg->message.error.error_code,
1565 msg->message.error.frame_number, streamId);
1566 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001567 break;
1568 }
1569 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001570 ssize_t idx;
1571 uint32_t frameNumber = msg->message.shutter.frame_number;
1572 nsecs_t timestamp = msg->message.shutter.timestamp;
1573 // Verify ordering of shutter notifications
1574 {
1575 Mutex::Autolock l(mOutputLock);
1576 if (frameNumber != mNextShutterFrameNumber) {
1577 SET_ERR("Shutter notification out-of-order. Expected "
1578 "notification for frame %d, got frame %d",
1579 mNextShutterFrameNumber, frameNumber);
1580 break;
1581 }
1582 mNextShutterFrameNumber++;
1583 }
1584
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585 int32_t requestId = -1;
1586
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001587 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001588 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001589 {
1590 Mutex::Autolock l(mInFlightLock);
1591 idx = mInFlightMap.indexOfKey(frameNumber);
1592 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001593 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1594 r.captureTimestamp = timestamp;
1595 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001596 }
1597 }
1598 if (idx < 0) {
1599 SET_ERR("Shutter notification for non-existent frame number %d",
1600 frameNumber);
1601 break;
1602 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001603 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1604 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001605 // Call listener, if any
1606 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001607 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001608 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001609 break;
1610 }
1611 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001612 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001613 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001614 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001615}
1616
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001618 ALOGV("%s", __FUNCTION__);
1619
Igor Murashkin1e479c02013-09-06 16:55:14 -07001620 CameraMetadata retVal;
1621
1622 if (mRequestThread != NULL) {
1623 retVal = mRequestThread->getLatestRequest();
1624 }
1625
Igor Murashkin1e479c02013-09-06 16:55:14 -07001626 return retVal;
1627}
1628
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001629/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 * RequestThread inner class methods
1631 */
1632
1633Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001634 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001635 camera3_device_t *hal3Device) :
1636 Thread(false),
1637 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001638 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001639 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001640 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001641 mReconfigured(false),
1642 mDoPause(false),
1643 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001644 mFrameNumber(0),
1645 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001646 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001647}
1648
1649void Camera3Device::RequestThread::configurationComplete() {
1650 Mutex::Autolock l(mRequestLock);
1651 mReconfigured = true;
1652}
1653
1654status_t Camera3Device::RequestThread::queueRequest(
1655 sp<CaptureRequest> request) {
1656 Mutex::Autolock l(mRequestLock);
1657 mRequestQueue.push_back(request);
1658
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001659 unpauseForNewRequests();
1660
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001661 return OK;
1662}
1663
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001664
1665status_t Camera3Device::RequestThread::queueTrigger(
1666 RequestTrigger trigger[],
1667 size_t count) {
1668
1669 Mutex::Autolock l(mTriggerMutex);
1670 status_t ret;
1671
1672 for (size_t i = 0; i < count; ++i) {
1673 ret = queueTriggerLocked(trigger[i]);
1674
1675 if (ret != OK) {
1676 return ret;
1677 }
1678 }
1679
1680 return OK;
1681}
1682
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001683int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1684 sp<Camera3Device> d = device.promote();
1685 if (d != NULL) return d->mId;
1686 return 0;
1687}
1688
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001689status_t Camera3Device::RequestThread::queueTriggerLocked(
1690 RequestTrigger trigger) {
1691
1692 uint32_t tag = trigger.metadataTag;
1693 ssize_t index = mTriggerMap.indexOfKey(tag);
1694
1695 switch (trigger.getTagType()) {
1696 case TYPE_BYTE:
1697 // fall-through
1698 case TYPE_INT32:
1699 break;
1700 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001701 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1702 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001703 return INVALID_OPERATION;
1704 }
1705
1706 /**
1707 * Collect only the latest trigger, since we only have 1 field
1708 * in the request settings per trigger tag, and can't send more than 1
1709 * trigger per request.
1710 */
1711 if (index != NAME_NOT_FOUND) {
1712 mTriggerMap.editValueAt(index) = trigger;
1713 } else {
1714 mTriggerMap.add(tag, trigger);
1715 }
1716
1717 return OK;
1718}
1719
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001720status_t Camera3Device::RequestThread::setRepeatingRequests(
1721 const RequestList &requests) {
1722 Mutex::Autolock l(mRequestLock);
1723 mRepeatingRequests.clear();
1724 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1725 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001726
1727 unpauseForNewRequests();
1728
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001729 return OK;
1730}
1731
1732status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1733 Mutex::Autolock l(mRequestLock);
1734 mRepeatingRequests.clear();
1735 return OK;
1736}
1737
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001738status_t Camera3Device::RequestThread::clear() {
1739 Mutex::Autolock l(mRequestLock);
1740 mRepeatingRequests.clear();
1741 mRequestQueue.clear();
1742 mTriggerMap.clear();
1743 return OK;
1744}
1745
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001746void Camera3Device::RequestThread::setPaused(bool paused) {
1747 Mutex::Autolock l(mPauseLock);
1748 mDoPause = paused;
1749 mDoPauseSignal.signal();
1750}
1751
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001752status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1753 int32_t requestId, nsecs_t timeout) {
1754 Mutex::Autolock l(mLatestRequestMutex);
1755 status_t res;
1756 while (mLatestRequestId != requestId) {
1757 nsecs_t startTime = systemTime();
1758
1759 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1760 if (res != OK) return res;
1761
1762 timeout -= (systemTime() - startTime);
1763 }
1764
1765 return OK;
1766}
1767
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001768void Camera3Device::RequestThread::requestExit() {
1769 // Call parent to set up shutdown
1770 Thread::requestExit();
1771 // The exit from any possible waits
1772 mDoPauseSignal.signal();
1773 mRequestSignal.signal();
1774}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001775
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001776bool Camera3Device::RequestThread::threadLoop() {
1777
1778 status_t res;
1779
1780 // Handle paused state.
1781 if (waitIfPaused()) {
1782 return true;
1783 }
1784
1785 // Get work to do
1786
1787 sp<CaptureRequest> nextRequest = waitForNextRequest();
1788 if (nextRequest == NULL) {
1789 return true;
1790 }
1791
1792 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001793 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001794 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001795
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001796 // Get the request ID, if any
1797 int requestId;
1798 camera_metadata_entry_t requestIdEntry =
1799 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1800 if (requestIdEntry.count > 0) {
1801 requestId = requestIdEntry.data.i32[0];
1802 } else {
1803 ALOGW("%s: Did not have android.request.id set in the request",
1804 __FUNCTION__);
1805 requestId = NAME_NOT_FOUND;
1806 }
1807
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001808 // Insert any queued triggers (before metadata is locked)
1809 int32_t triggerCount;
1810 res = insertTriggers(nextRequest);
1811 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001812 SET_ERR("RequestThread: Unable to insert triggers "
1813 "(capture request %d, HAL device: %s (%d)",
1814 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001815 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1816 return false;
1817 }
1818 triggerCount = res;
1819
1820 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1821
1822 // If the request is the same as last, or we had triggers last time
1823 if (mPrevRequest != nextRequest || triggersMixedIn) {
1824 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07001825 * HAL workaround:
1826 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
1827 */
1828 res = addDummyTriggerIds(nextRequest);
1829 if (res != OK) {
1830 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
1831 "(capture request %d, HAL device: %s (%d)",
1832 (mFrameNumber+1), strerror(-res), res);
1833 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1834 return false;
1835 }
1836
1837 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001838 * The request should be presorted so accesses in HAL
1839 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1840 */
1841 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001842 request.settings = nextRequest->mSettings.getAndLock();
1843 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001844 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1845
1846 IF_ALOGV() {
1847 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1848 find_camera_metadata_ro_entry(
1849 request.settings,
1850 ANDROID_CONTROL_AF_TRIGGER,
1851 &e
1852 );
1853 if (e.count > 0) {
1854 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1855 __FUNCTION__,
1856 mFrameNumber+1,
1857 e.data.u8[0]);
1858 }
1859 }
1860 } else {
1861 // leave request.settings NULL to indicate 'reuse latest given'
1862 ALOGVV("%s: Request settings are REUSED",
1863 __FUNCTION__);
1864 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001865
1866 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001867
1868 // Fill in buffers
1869
1870 if (nextRequest->mInputStream != NULL) {
1871 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001872 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001874 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001875 " %s (%d)", strerror(-res), res);
1876 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1877 return true;
1878 }
1879 } else {
1880 request.input_buffer = NULL;
1881 }
1882
1883 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1884 nextRequest->mOutputStreams.size());
1885 request.output_buffers = outputBuffers.array();
1886 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1887 res = nextRequest->mOutputStreams.editItemAt(i)->
1888 getBuffer(&outputBuffers.editItemAt(i));
1889 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001890 ALOGE("RequestThread: Can't get output buffer, skipping request:"
1891 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001892 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1893 return true;
1894 }
1895 request.num_output_buffers++;
1896 }
1897
1898 request.frame_number = mFrameNumber++;
1899
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001900 // Log request in the in-flight queue
1901 sp<Camera3Device> parent = mParent.promote();
1902 if (parent == NULL) {
1903 CLOGE("RequestThread: Parent is gone");
1904 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1905 return false;
1906 }
1907
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001908 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001909 request.num_output_buffers);
1910 if (res != OK) {
1911 SET_ERR("RequestThread: Unable to register new in-flight request:"
1912 " %s (%d)", strerror(-res), res);
1913 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1914 return false;
1915 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001916
Zhijun Hecc27e112013-10-03 16:12:43 -07001917 // Inform waitUntilRequestProcessed thread of a new request ID
1918 {
1919 Mutex::Autolock al(mLatestRequestMutex);
1920
1921 mLatestRequestId = requestId;
1922 mLatestRequestSignal.signal();
1923 }
1924
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001925 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001926 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1927 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001929 ATRACE_END();
1930
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001931 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001932 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001933 " device: %s (%d)", request.frame_number, strerror(-res), res);
1934 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1935 return false;
1936 }
1937
Igor Murashkin1e479c02013-09-06 16:55:14 -07001938 // Update the latest request sent to HAL
1939 if (request.settings != NULL) { // Don't update them if they were unchanged
1940 Mutex::Autolock al(mLatestRequestMutex);
1941
1942 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
1943 mLatestRequest.acquire(cloned);
1944 }
1945
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001946 if (request.settings != NULL) {
1947 nextRequest->mSettings.unlock(request.settings);
1948 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001949
1950 // Remove any previously queued triggers (after unlock)
1951 res = removeTriggers(mPrevRequest);
1952 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001953 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001954 "(capture request %d, HAL device: %s (%d)",
1955 request.frame_number, strerror(-res), res);
1956 return false;
1957 }
1958 mPrevTriggers = triggerCount;
1959
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001960 // Return input buffer back to framework
1961 if (request.input_buffer != NULL) {
1962 Camera3Stream *stream =
1963 Camera3Stream::cast(request.input_buffer->stream);
1964 res = stream->returnInputBuffer(*(request.input_buffer));
1965 // Note: stream may be deallocated at this point, if this buffer was the
1966 // last reference to it.
1967 if (res != OK) {
1968 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1969 " its stream:%s (%d)", __FUNCTION__,
1970 request.frame_number, strerror(-res), res);
1971 // TODO: Report error upstream
1972 }
1973 }
1974
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001975 return true;
1976}
1977
Igor Murashkin1e479c02013-09-06 16:55:14 -07001978CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
1979 Mutex::Autolock al(mLatestRequestMutex);
1980
1981 ALOGV("RequestThread::%s", __FUNCTION__);
1982
1983 return mLatestRequest;
1984}
1985
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001986void Camera3Device::RequestThread::cleanUpFailedRequest(
1987 camera3_capture_request_t &request,
1988 sp<CaptureRequest> &nextRequest,
1989 Vector<camera3_stream_buffer_t> &outputBuffers) {
1990
1991 if (request.settings != NULL) {
1992 nextRequest->mSettings.unlock(request.settings);
1993 }
1994 if (request.input_buffer != NULL) {
1995 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001996 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001997 }
1998 for (size_t i = 0; i < request.num_output_buffers; i++) {
1999 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2000 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2001 outputBuffers[i], 0);
2002 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002003}
2004
2005sp<Camera3Device::CaptureRequest>
2006 Camera3Device::RequestThread::waitForNextRequest() {
2007 status_t res;
2008 sp<CaptureRequest> nextRequest;
2009
2010 // Optimized a bit for the simple steady-state case (single repeating
2011 // request), to avoid putting that request in the queue temporarily.
2012 Mutex::Autolock l(mRequestLock);
2013
2014 while (mRequestQueue.empty()) {
2015 if (!mRepeatingRequests.empty()) {
2016 // Always atomically enqueue all requests in a repeating request
2017 // list. Guarantees a complete in-sequence set of captures to
2018 // application.
2019 const RequestList &requests = mRepeatingRequests;
2020 RequestList::const_iterator firstRequest =
2021 requests.begin();
2022 nextRequest = *firstRequest;
2023 mRequestQueue.insert(mRequestQueue.end(),
2024 ++firstRequest,
2025 requests.end());
2026 // No need to wait any longer
2027 break;
2028 }
2029
2030 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2031
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002032 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2033 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002034 Mutex::Autolock pl(mPauseLock);
2035 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002036 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002037 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002038 // Let the tracker know
2039 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2040 if (statusTracker != 0) {
2041 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2042 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002043 }
2044 // Stop waiting for now and let thread management happen
2045 return NULL;
2046 }
2047 }
2048
2049 if (nextRequest == NULL) {
2050 // Don't have a repeating request already in hand, so queue
2051 // must have an entry now.
2052 RequestList::iterator firstRequest =
2053 mRequestQueue.begin();
2054 nextRequest = *firstRequest;
2055 mRequestQueue.erase(firstRequest);
2056 }
2057
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002058 // In case we've been unpaused by setPaused clearing mDoPause, need to
2059 // update internal pause state (capture/setRepeatingRequest unpause
2060 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002061 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002062 if (mPaused) {
2063 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2064 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2065 if (statusTracker != 0) {
2066 statusTracker->markComponentActive(mStatusId);
2067 }
2068 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002069 mPaused = false;
2070
2071 // Check if we've reconfigured since last time, and reset the preview
2072 // request if so. Can't use 'NULL request == repeat' across configure calls.
2073 if (mReconfigured) {
2074 mPrevRequest.clear();
2075 mReconfigured = false;
2076 }
2077
2078 return nextRequest;
2079}
2080
2081bool Camera3Device::RequestThread::waitIfPaused() {
2082 status_t res;
2083 Mutex::Autolock l(mPauseLock);
2084 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 if (mPaused == false) {
2086 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002087 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2088 // Let the tracker know
2089 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2090 if (statusTracker != 0) {
2091 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2092 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002093 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002094
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002095 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002096 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002097 return true;
2098 }
2099 }
2100 // We don't set mPaused to false here, because waitForNextRequest needs
2101 // to further manage the paused state in case of starvation.
2102 return false;
2103}
2104
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002105void Camera3Device::RequestThread::unpauseForNewRequests() {
2106 // With work to do, mark thread as unpaused.
2107 // If paused by request (setPaused), don't resume, to avoid
2108 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002109 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002110 Mutex::Autolock p(mPauseLock);
2111 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002112 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2113 if (mPaused) {
2114 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2115 if (statusTracker != 0) {
2116 statusTracker->markComponentActive(mStatusId);
2117 }
2118 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002119 mPaused = false;
2120 }
2121}
2122
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002123void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2124 sp<Camera3Device> parent = mParent.promote();
2125 if (parent != NULL) {
2126 va_list args;
2127 va_start(args, fmt);
2128
2129 parent->setErrorStateV(fmt, args);
2130
2131 va_end(args);
2132 }
2133}
2134
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002135status_t Camera3Device::RequestThread::insertTriggers(
2136 const sp<CaptureRequest> &request) {
2137
2138 Mutex::Autolock al(mTriggerMutex);
2139
2140 CameraMetadata &metadata = request->mSettings;
2141 size_t count = mTriggerMap.size();
2142
2143 for (size_t i = 0; i < count; ++i) {
2144 RequestTrigger trigger = mTriggerMap.valueAt(i);
2145
2146 uint32_t tag = trigger.metadataTag;
2147 camera_metadata_entry entry = metadata.find(tag);
2148
2149 if (entry.count > 0) {
2150 /**
2151 * Already has an entry for this trigger in the request.
2152 * Rewrite it with our requested trigger value.
2153 */
2154 RequestTrigger oldTrigger = trigger;
2155
2156 oldTrigger.entryValue = entry.data.u8[0];
2157
2158 mTriggerReplacedMap.add(tag, oldTrigger);
2159 } else {
2160 /**
2161 * More typical, no trigger entry, so we just add it
2162 */
2163 mTriggerRemovedMap.add(tag, trigger);
2164 }
2165
2166 status_t res;
2167
2168 switch (trigger.getTagType()) {
2169 case TYPE_BYTE: {
2170 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2171 res = metadata.update(tag,
2172 &entryValue,
2173 /*count*/1);
2174 break;
2175 }
2176 case TYPE_INT32:
2177 res = metadata.update(tag,
2178 &trigger.entryValue,
2179 /*count*/1);
2180 break;
2181 default:
2182 ALOGE("%s: Type not supported: 0x%x",
2183 __FUNCTION__,
2184 trigger.getTagType());
2185 return INVALID_OPERATION;
2186 }
2187
2188 if (res != OK) {
2189 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2190 ", value %d", __FUNCTION__, trigger.getTagName(),
2191 trigger.entryValue);
2192 return res;
2193 }
2194
2195 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2196 trigger.getTagName(),
2197 trigger.entryValue);
2198 }
2199
2200 mTriggerMap.clear();
2201
2202 return count;
2203}
2204
2205status_t Camera3Device::RequestThread::removeTriggers(
2206 const sp<CaptureRequest> &request) {
2207 Mutex::Autolock al(mTriggerMutex);
2208
2209 CameraMetadata &metadata = request->mSettings;
2210
2211 /**
2212 * Replace all old entries with their old values.
2213 */
2214 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2215 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2216
2217 status_t res;
2218
2219 uint32_t tag = trigger.metadataTag;
2220 switch (trigger.getTagType()) {
2221 case TYPE_BYTE: {
2222 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2223 res = metadata.update(tag,
2224 &entryValue,
2225 /*count*/1);
2226 break;
2227 }
2228 case TYPE_INT32:
2229 res = metadata.update(tag,
2230 &trigger.entryValue,
2231 /*count*/1);
2232 break;
2233 default:
2234 ALOGE("%s: Type not supported: 0x%x",
2235 __FUNCTION__,
2236 trigger.getTagType());
2237 return INVALID_OPERATION;
2238 }
2239
2240 if (res != OK) {
2241 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2242 ", trigger value %d", __FUNCTION__,
2243 trigger.getTagName(), trigger.entryValue);
2244 return res;
2245 }
2246 }
2247 mTriggerReplacedMap.clear();
2248
2249 /**
2250 * Remove all new entries.
2251 */
2252 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2253 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2254 status_t res = metadata.erase(trigger.metadataTag);
2255
2256 if (res != OK) {
2257 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2258 ", trigger value %d", __FUNCTION__,
2259 trigger.getTagName(), trigger.entryValue);
2260 return res;
2261 }
2262 }
2263 mTriggerRemovedMap.clear();
2264
2265 return OK;
2266}
2267
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002268status_t Camera3Device::RequestThread::addDummyTriggerIds(
2269 const sp<CaptureRequest> &request) {
2270 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2271 static const int32_t dummyTriggerId = 1;
2272 status_t res;
2273
2274 CameraMetadata &metadata = request->mSettings;
2275
2276 // If AF trigger is active, insert a dummy AF trigger ID if none already
2277 // exists
2278 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2279 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2280 if (afTrigger.count > 0 &&
2281 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2282 afId.count == 0) {
2283 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2284 if (res != OK) return res;
2285 }
2286
2287 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2288 // if none already exists
2289 camera_metadata_entry pcTrigger =
2290 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2291 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2292 if (pcTrigger.count > 0 &&
2293 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2294 pcId.count == 0) {
2295 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2296 &dummyTriggerId, 1);
2297 if (res != OK) return res;
2298 }
2299
2300 return OK;
2301}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002302
2303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002304/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002305 * Static callback forwarding methods from HAL to instance
2306 */
2307
2308void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2309 const camera3_capture_result *result) {
2310 Camera3Device *d =
2311 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2312 d->processCaptureResult(result);
2313}
2314
2315void Camera3Device::sNotify(const camera3_callback_ops *cb,
2316 const camera3_notify_msg *msg) {
2317 Camera3Device *d =
2318 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2319 d->notify(msg);
2320}
2321
2322}; // namespace android