blob: 303823ce0874bf232e15138c5f1197ddad1ebf8a [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
44#include "device3/Camera3Device.h"
45#include "device3/Camera3OutputStream.h"
46#include "device3/Camera3InputStream.h"
47#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080048
49using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080050
51namespace android {
52
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053Camera3Device::Camera3Device(int id):
54 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080055 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070056 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070057 mNextResultFrameNumber(0),
58 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070059 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060{
61 ATRACE_CALL();
62 camera3_callback_ops::notify = &sNotify;
63 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
64 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
65}
66
67Camera3Device::~Camera3Device()
68{
69 ATRACE_CALL();
70 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
71 disconnect();
72}
73
Igor Murashkin71381052013-03-04 14:53:08 -080074int Camera3Device::getId() const {
75 return mId;
76}
77
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080078/**
79 * CameraDeviceBase interface
80 */
81
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080082status_t Camera3Device::initialize(camera_module_t *module)
83{
84 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070085 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086 Mutex::Autolock l(mLock);
87
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080089 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070090 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091 return INVALID_OPERATION;
92 }
93
94 /** Open HAL device */
95
96 status_t res;
97 String8 deviceName = String8::format("%d", mId);
98
99 camera3_device_t *device;
100
101 res = module->common.methods->open(&module->common, deviceName.string(),
102 reinterpret_cast<hw_device_t**>(&device));
103
104 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700105 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 return res;
107 }
108
109 /** Cross-check device version */
110
111 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700112 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700114 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115 device->common.version);
116 device->common.close(&device->common);
117 return BAD_VALUE;
118 }
119
120 camera_info info;
121 res = module->get_camera_info(mId, &info);
122 if (res != OK) return res;
123
124 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700125 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
126 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800127 device->common.version, info.device_version);
128 device->common.close(&device->common);
129 return BAD_VALUE;
130 }
131
132 /** Initialize device with callback functions */
133
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700134 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700136 ATRACE_END();
137
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700139 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
140 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 device->common.close(&device->common);
142 return BAD_VALUE;
143 }
144
145 /** Get vendor metadata tags */
146
147 mVendorTagOps.get_camera_vendor_section_name = NULL;
148
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700149 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800150 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700151 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152
153 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
154 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
155 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700156 SET_ERR_L("Unable to set tag ops: %s (%d)",
157 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800158 device->common.close(&device->common);
159 return res;
160 }
161 }
162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700163 /** Start up status tracker thread */
164 mStatusTracker = new StatusTracker(this);
165 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
166 if (res != OK) {
167 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
168 strerror(-res), res);
169 device->common.close(&device->common);
170 mStatusTracker.clear();
171 return res;
172 }
173
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174 /** Start up request queue thread */
175
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700176 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800177 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700179 SET_ERR_L("Unable to start request queue thread: %s (%d)",
180 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800182 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800183 return res;
184 }
185
186 /** Everything is good to go */
187
188 mDeviceInfo = info.static_camera_characteristics;
189 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700190 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800191 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700192 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700193 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194
195 return OK;
196}
197
198status_t Camera3Device::disconnect() {
199 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700200 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800202 ALOGV("%s: E", __FUNCTION__);
203
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700204 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800205
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700206 {
207 Mutex::Autolock l(mLock);
208 if (mStatus == STATUS_UNINITIALIZED) return res;
209
210 if (mStatus == STATUS_ACTIVE ||
211 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
212 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700213 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700214 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700215 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700216 } else {
217 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
218 if (res != OK) {
219 SET_ERR_L("Timeout waiting for HAL to drain");
220 // Continue to close device even in case of error
221 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700222 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800223 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800224
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225 if (mStatus == STATUS_ERROR) {
226 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700227 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700228
229 if (mStatusTracker != NULL) {
230 mStatusTracker->requestExit();
231 }
232
233 if (mRequestThread != NULL) {
234 mRequestThread->requestExit();
235 }
236
237 mOutputStreams.clear();
238 mInputStream.clear();
239 }
240
241 // Joining done without holding mLock, otherwise deadlocks may ensue
242 // as the threads try to access parent state
243 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
244 // HAL may be in a bad state, so waiting for request thread
245 // (which may be stuck in the HAL processCaptureRequest call)
246 // could be dangerous.
247 mRequestThread->join();
248 }
249
250 if (mStatusTracker != NULL) {
251 mStatusTracker->join();
252 }
253
254 {
255 Mutex::Autolock l(mLock);
256
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800257 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800259
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700260 if (mHal3Device != NULL) {
261 mHal3Device->common.close(&mHal3Device->common);
262 mHal3Device = NULL;
263 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800264
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 mStatus = STATUS_UNINITIALIZED;
266 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267
268 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700269 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800270}
271
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272// For dumping/debugging only -
273// try to acquire a lock a few times, eventually give up to proceed with
274// debug/dump operations
275bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
276 bool gotLock = false;
277 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
278 if (lock.tryLock() == NO_ERROR) {
279 gotLock = true;
280 break;
281 } else {
282 usleep(kDumpSleepDuration);
283 }
284 }
285 return gotLock;
286}
287
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800288status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
289 ATRACE_CALL();
290 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700291
292 // Try to lock, but continue in case of failure (to avoid blocking in
293 // deadlocks)
294 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
295 bool gotLock = tryLockSpinRightRound(mLock);
296
297 ALOGW_IF(!gotInterfaceLock,
298 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
299 mId, __FUNCTION__);
300 ALOGW_IF(!gotLock,
301 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
302 mId, __FUNCTION__);
303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800304 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800305
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800306 const char *status =
307 mStatus == STATUS_ERROR ? "ERROR" :
308 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700309 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
310 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800311 mStatus == STATUS_ACTIVE ? "ACTIVE" :
312 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700313
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700315 if (mStatus == STATUS_ERROR) {
316 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
317 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318 lines.appendFormat(" Stream configuration:\n");
319
320 if (mInputStream != NULL) {
321 write(fd, lines.string(), lines.size());
322 mInputStream->dump(fd, args);
323 } else {
324 lines.appendFormat(" No input stream.\n");
325 write(fd, lines.string(), lines.size());
326 }
327 for (size_t i = 0; i < mOutputStreams.size(); i++) {
328 mOutputStreams[i]->dump(fd,args);
329 }
330
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700331 lines = String8(" In-flight requests:\n");
332 if (mInFlightMap.size() == 0) {
333 lines.append(" None\n");
334 } else {
335 for (size_t i = 0; i < mInFlightMap.size(); i++) {
336 InFlightRequest r = mInFlightMap.valueAt(i);
337 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
338 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
339 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
340 r.numBuffersLeft);
341 }
342 }
343 write(fd, lines.string(), lines.size());
344
Igor Murashkin1e479c02013-09-06 16:55:14 -0700345 {
346 lines = String8(" Last request sent:\n");
347 write(fd, lines.string(), lines.size());
348
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700349 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700350 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
351 }
352
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800353 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700354 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800355 write(fd, lines.string(), lines.size());
356 mHal3Device->ops->dump(mHal3Device, fd);
357 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800358
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359 if (gotLock) mLock.unlock();
360 if (gotInterfaceLock) mInterfaceLock.unlock();
361
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800362 return OK;
363}
364
365const CameraMetadata& Camera3Device::info() const {
366 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
368 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700369 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370 mStatus == STATUS_ERROR ?
371 "when in error state" : "before init");
372 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800373 return mDeviceInfo;
374}
375
376status_t Camera3Device::capture(CameraMetadata &request) {
377 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700378 status_t res;
379 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800380 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800381
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700382 // TODO: take ownership of the request
383
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800384 switch (mStatus) {
385 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700386 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800387 return INVALID_OPERATION;
388 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700389 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800390 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700391 case STATUS_UNCONFIGURED:
392 // May be lazily configuring streams, will check during setup
393 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800394 case STATUS_ACTIVE:
395 // OK
396 break;
397 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700398 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800399 return INVALID_OPERATION;
400 }
401
402 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
403 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700404 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 return BAD_VALUE;
406 }
407
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700408 res = mRequestThread->queueRequest(newRequest);
409 if (res == OK) {
410 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
411 if (res != OK) {
412 SET_ERR_L("Can't transition to active in %f seconds!",
413 kActiveTimeout/1e9);
414 }
415 ALOGV("Camera %d: Capture request enqueued", mId);
416 }
417 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800418}
419
420
421status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
422 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700423 status_t res;
424 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800425 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800426
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800427 switch (mStatus) {
428 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700429 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800430 return INVALID_OPERATION;
431 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700432 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700434 case STATUS_UNCONFIGURED:
435 // May be lazily configuring streams, will check during setup
436 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800437 case STATUS_ACTIVE:
438 // OK
439 break;
440 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700441 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800442 return INVALID_OPERATION;
443 }
444
445 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
446 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700447 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 return BAD_VALUE;
449 }
450
451 RequestList newRepeatingRequests;
452 newRepeatingRequests.push_back(newRepeatingRequest);
453
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700454 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
455 if (res == OK) {
456 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
457 if (res != OK) {
458 SET_ERR_L("Can't transition to active in %f seconds!",
459 kActiveTimeout/1e9);
460 }
461 ALOGV("Camera %d: Repeating request set", mId);
462 }
463 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800464}
465
466
467sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
468 const CameraMetadata &request) {
469 status_t res;
470
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700471 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800472 res = configureStreamsLocked();
473 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700474 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800475 return NULL;
476 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700477 if (mStatus == STATUS_UNCONFIGURED) {
478 CLOGE("No streams configured");
479 return NULL;
480 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800481 }
482
483 sp<CaptureRequest> newRequest = createCaptureRequest(request);
484 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800485}
486
487status_t Camera3Device::clearStreamingRequest() {
488 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700489 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800490 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800491
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800492 switch (mStatus) {
493 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700494 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800495 return INVALID_OPERATION;
496 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700497 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800498 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700499 case STATUS_UNCONFIGURED:
500 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800501 case STATUS_ACTIVE:
502 // OK
503 break;
504 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700505 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800506 return INVALID_OPERATION;
507 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700508 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800509 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800510}
511
512status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
513 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700514 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800515
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700516 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800517}
518
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700519status_t Camera3Device::createInputStream(
520 uint32_t width, uint32_t height, int format, int *id) {
521 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700522 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700523 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700524 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
525 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700526
527 status_t res;
528 bool wasActive = false;
529
530 switch (mStatus) {
531 case STATUS_ERROR:
532 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
533 return INVALID_OPERATION;
534 case STATUS_UNINITIALIZED:
535 ALOGE("%s: Device not initialized", __FUNCTION__);
536 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700537 case STATUS_UNCONFIGURED:
538 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700539 // OK
540 break;
541 case STATUS_ACTIVE:
542 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700543 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700544 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700545 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700546 return res;
547 }
548 wasActive = true;
549 break;
550 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700551 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700552 return INVALID_OPERATION;
553 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700554 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700555
556 if (mInputStream != 0) {
557 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
558 return INVALID_OPERATION;
559 }
560
561 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
562 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700563 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700564
565 mInputStream = newStream;
566
567 *id = mNextStreamId++;
568
569 // Continue captures if active at start
570 if (wasActive) {
571 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
572 res = configureStreamsLocked();
573 if (res != OK) {
574 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
575 __FUNCTION__, mNextStreamId, strerror(-res), res);
576 return res;
577 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700578 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700579 }
580
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700581 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700582 return OK;
583}
584
Igor Murashkin2fba5842013-04-22 14:03:54 -0700585
586status_t Camera3Device::createZslStream(
587 uint32_t width, uint32_t height,
588 int depth,
589 /*out*/
590 int *id,
591 sp<Camera3ZslStream>* zslStream) {
592 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700593 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700594 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
596 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700597
598 status_t res;
599 bool wasActive = false;
600
601 switch (mStatus) {
602 case STATUS_ERROR:
603 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
604 return INVALID_OPERATION;
605 case STATUS_UNINITIALIZED:
606 ALOGE("%s: Device not initialized", __FUNCTION__);
607 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 case STATUS_UNCONFIGURED:
609 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700610 // OK
611 break;
612 case STATUS_ACTIVE:
613 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700614 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700615 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700617 return res;
618 }
619 wasActive = true;
620 break;
621 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700622 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700623 return INVALID_OPERATION;
624 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700625 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700626
627 if (mInputStream != 0) {
628 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
629 return INVALID_OPERATION;
630 }
631
632 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
633 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700634 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700635
636 res = mOutputStreams.add(mNextStreamId, newStream);
637 if (res < 0) {
638 ALOGE("%s: Can't add new stream to set: %s (%d)",
639 __FUNCTION__, strerror(-res), res);
640 return res;
641 }
642 mInputStream = newStream;
643
644 *id = mNextStreamId++;
645 *zslStream = newStream;
646
647 // Continue captures if active at start
648 if (wasActive) {
649 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
650 res = configureStreamsLocked();
651 if (res != OK) {
652 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
653 __FUNCTION__, mNextStreamId, strerror(-res), res);
654 return res;
655 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700657 }
658
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700659 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700660 return OK;
661}
662
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800663status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
664 uint32_t width, uint32_t height, int format, size_t size, int *id) {
665 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700666 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800667 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
669 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800670
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800671 status_t res;
672 bool wasActive = false;
673
674 switch (mStatus) {
675 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700676 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800677 return INVALID_OPERATION;
678 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700679 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800680 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700681 case STATUS_UNCONFIGURED:
682 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800683 // OK
684 break;
685 case STATUS_ACTIVE:
686 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700687 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800688 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700689 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800690 return res;
691 }
692 wasActive = true;
693 break;
694 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700695 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800696 return INVALID_OPERATION;
697 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700698 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800699
700 sp<Camera3OutputStream> newStream;
701 if (format == HAL_PIXEL_FORMAT_BLOB) {
702 newStream = new Camera3OutputStream(mNextStreamId, consumer,
703 width, height, size, format);
704 } else {
705 newStream = new Camera3OutputStream(mNextStreamId, consumer,
706 width, height, format);
707 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700708 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800709
710 res = mOutputStreams.add(mNextStreamId, newStream);
711 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700712 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800713 return res;
714 }
715
716 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700717 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800718
719 // Continue captures if active at start
720 if (wasActive) {
721 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
722 res = configureStreamsLocked();
723 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700724 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
725 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800726 return res;
727 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700728 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800729 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700730 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800731 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800732}
733
734status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
735 ATRACE_CALL();
736 (void)outputId; (void)id;
737
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700738 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800739 return INVALID_OPERATION;
740}
741
742
743status_t Camera3Device::getStreamInfo(int id,
744 uint32_t *width, uint32_t *height, uint32_t *format) {
745 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800747 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800748
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749 switch (mStatus) {
750 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700751 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800752 return INVALID_OPERATION;
753 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700754 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800755 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 case STATUS_UNCONFIGURED:
757 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800758 case STATUS_ACTIVE:
759 // OK
760 break;
761 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700762 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800763 return INVALID_OPERATION;
764 }
765
766 ssize_t idx = mOutputStreams.indexOfKey(id);
767 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700768 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 return idx;
770 }
771
772 if (width) *width = mOutputStreams[idx]->getWidth();
773 if (height) *height = mOutputStreams[idx]->getHeight();
774 if (format) *format = mOutputStreams[idx]->getFormat();
775
776 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800777}
778
779status_t Camera3Device::setStreamTransform(int id,
780 int transform) {
781 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700782 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800783 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800784
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800785 switch (mStatus) {
786 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700787 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 return INVALID_OPERATION;
789 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700790 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800791 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700792 case STATUS_UNCONFIGURED:
793 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800794 case STATUS_ACTIVE:
795 // OK
796 break;
797 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700798 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800799 return INVALID_OPERATION;
800 }
801
802 ssize_t idx = mOutputStreams.indexOfKey(id);
803 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700804 CLOGE("Stream %d does not exist",
805 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 return BAD_VALUE;
807 }
808
809 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800810}
811
812status_t Camera3Device::deleteStream(int id) {
813 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700814 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800815 Mutex::Autolock l(mLock);
816 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800817
Igor Murashkine2172be2013-05-28 15:31:39 -0700818 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
819
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 // CameraDevice semantics require device to already be idle before
821 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700822 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700823 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
824 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 }
826
Igor Murashkin2fba5842013-04-22 14:03:54 -0700827 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800828 if (mInputStream != NULL && id == mInputStream->getId()) {
829 deletedStream = mInputStream;
830 mInputStream.clear();
831 } else {
832 ssize_t idx = mOutputStreams.indexOfKey(id);
833 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700834 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 return BAD_VALUE;
836 }
837 deletedStream = mOutputStreams.editValueAt(idx);
838 mOutputStreams.removeItem(id);
839 }
840
841 // Free up the stream endpoint so that it can be used by some other stream
842 res = deletedStream->disconnect();
843 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700844 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 // fall through since we want to still list the stream as deleted.
846 }
847 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700848 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849
850 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800851}
852
853status_t Camera3Device::deleteReprocessStream(int id) {
854 ATRACE_CALL();
855 (void)id;
856
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700857 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800858 return INVALID_OPERATION;
859}
860
861
862status_t Camera3Device::createDefaultRequest(int templateId,
863 CameraMetadata *request) {
864 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700865 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700866 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 Mutex::Autolock l(mLock);
868
869 switch (mStatus) {
870 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700871 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800872 return INVALID_OPERATION;
873 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700874 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 case STATUS_UNCONFIGURED:
877 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 case STATUS_ACTIVE:
879 // OK
880 break;
881 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700882 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 return INVALID_OPERATION;
884 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800885
886 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700887 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800888 rawRequest = mHal3Device->ops->construct_default_request_settings(
889 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700890 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700891 if (rawRequest == NULL) {
892 SET_ERR_L("HAL is unable to construct default settings for template %d",
893 templateId);
894 return DEAD_OBJECT;
895 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896 *request = rawRequest;
897
898 return OK;
899}
900
901status_t Camera3Device::waitUntilDrained() {
902 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700903 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800905
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906 switch (mStatus) {
907 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700908 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800909 ALOGV("%s: Already idle", __FUNCTION__);
910 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700911 case STATUS_CONFIGURED:
912 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 case STATUS_ERROR:
914 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700915 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800916 break;
917 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700918 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800919 return INVALID_OPERATION;
920 }
921
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
923 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
924 return res;
925}
926
927// Pause to reconfigure
928status_t Camera3Device::internalPauseAndWaitLocked() {
929 mRequestThread->setPaused(true);
930 mPauseStateNotify = true;
931
932 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
933 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
934 if (res != OK) {
935 SET_ERR_L("Can't idle device in %f seconds!",
936 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937 }
938
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700939 return res;
940}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800941
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700942// Resume after internalPauseAndWaitLocked
943status_t Camera3Device::internalResumeLocked() {
944 status_t res;
945
946 mRequestThread->setPaused(false);
947
948 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
949 if (res != OK) {
950 SET_ERR_L("Can't transition to active in %f seconds!",
951 kActiveTimeout/1e9);
952 }
953 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800955}
956
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700957status_t Camera3Device::waitUntilStateThenRelock(bool active,
958 nsecs_t timeout) {
959 status_t res = OK;
960 if (active == (mStatus == STATUS_ACTIVE)) {
961 // Desired state already reached
962 return res;
963 }
964
965 bool stateSeen = false;
966 do {
967 mRecentStatusUpdates.clear();
968
969 res = mStatusChanged.waitRelative(mLock, timeout);
970 if (res != OK) break;
971
972 // Check state change history during wait
973 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
974 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
975 stateSeen = true;
976 break;
977 }
978 }
979 } while (!stateSeen);
980
981 return res;
982}
983
984
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800985status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
986 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700987 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800988
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -0700989 if (listener != NULL && mListener != NULL) {
990 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
991 }
992 mListener = listener;
993
994 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800995}
996
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700997bool Camera3Device::willNotify3A() {
998 return false;
999}
1000
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001001status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001002 ATRACE_CALL();
1003 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
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001367 mErrorCause = errorCause;
1368
1369 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001370 mStatus = STATUS_ERROR;
1371}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001372
1373/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001374 * In-flight request management
1375 */
1376
1377status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001378 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001379 ATRACE_CALL();
1380 Mutex::Autolock l(mInFlightLock);
1381
1382 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001383 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001384 if (res < 0) return res;
1385
1386 return OK;
1387}
1388
1389/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 * Camera HAL device callback methods
1391 */
1392
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001393void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001394 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001395
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001396 status_t res;
1397
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001398 uint32_t frameNumber = result->frame_number;
1399 if (result->result == NULL && result->num_output_buffers == 0) {
1400 SET_ERR("No result data provided by HAL for frame %d",
1401 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001402 return;
1403 }
1404
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001405 // Get capture timestamp from list of in-flight requests, where it was added
1406 // by the shutter notification for this frame. Then update the in-flight
1407 // status and remove the in-flight entry if all result data has been
1408 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001409 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001410 {
1411 Mutex::Autolock l(mInFlightLock);
1412 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1413 if (idx == NAME_NOT_FOUND) {
1414 SET_ERR("Unknown frame number for capture result: %d",
1415 frameNumber);
1416 return;
1417 }
1418 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1419 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001420 /**
1421 * One of the following must happen before it's legal to call process_capture_result:
1422 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1423 * - CAMERA3_MSG_ERROR (expected during flush)
1424 */
1425 if (request.requestStatus == OK && timestamp == 0) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001426 SET_ERR("Called before shutter notify for frame %d",
1427 frameNumber);
1428 return;
1429 }
1430
1431 if (result->result != NULL) {
1432 if (request.haveResultMetadata) {
1433 SET_ERR("Called multiple times with metadata for frame %d",
1434 frameNumber);
1435 return;
1436 }
1437 request.haveResultMetadata = true;
1438 }
1439
1440 request.numBuffersLeft -= result->num_output_buffers;
1441
1442 if (request.numBuffersLeft < 0) {
1443 SET_ERR("Too many buffers returned for frame %d",
1444 frameNumber);
1445 return;
1446 }
1447
1448 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001449 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001450 mInFlightMap.removeItemsAt(idx, 1);
1451 }
1452
1453 // Sanity check - if we have too many in-flight frames, something has
1454 // likely gone wrong
1455 if (mInFlightMap.size() > kInFlightWarnLimit) {
1456 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1457 }
1458
1459 }
1460
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001461 // Process the result metadata, if provided
1462 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001463 Mutex::Autolock l(mOutputLock);
1464
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001465 if (frameNumber != mNextResultFrameNumber) {
1466 SET_ERR("Out-of-order capture result metadata submitted! "
1467 "(got frame number %d, expecting %d)",
1468 frameNumber, mNextResultFrameNumber);
1469 return;
1470 }
1471 mNextResultFrameNumber++;
1472
1473 CameraMetadata &captureResult =
1474 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001475
1476 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001477 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001478 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001479 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001480 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001481 } else {
1482 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001483 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001484 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001485
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001486 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001487
1488 camera_metadata_entry entry =
1489 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1490 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001491 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001492 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001493 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001494 SET_ERR("Timestamp mismatch between shutter notify and result"
1495 " metadata for frame %d (%lld vs %lld respectively)",
1496 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001497 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001498 } // scope for mOutputLock
1499
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001500 // Return completed buffers to their streams with the timestamp
1501
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001502 for (size_t i = 0; i < result->num_output_buffers; i++) {
1503 Camera3Stream *stream =
1504 Camera3Stream::cast(result->output_buffers[i].stream);
1505 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1506 // Note: stream may be deallocated at this point, if this buffer was the
1507 // last reference to it.
1508 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001509 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001510 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001511 }
1512 }
1513
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001514 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001515
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001516 if (result->result != NULL) {
1517 mResultSignal.signal();
1518 }
1519
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001520}
1521
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001522
1523
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001524void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001525 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001526 NotificationListener *listener;
1527 {
1528 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001529 listener = mListener;
1530 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001531
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001532 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001533 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001534 return;
1535 }
1536
1537 switch (msg->type) {
1538 case CAMERA3_MSG_ERROR: {
1539 int streamId = 0;
1540 if (msg->message.error.error_stream != NULL) {
1541 Camera3Stream *stream =
1542 Camera3Stream::cast(
1543 msg->message.error.error_stream);
1544 streamId = stream->getId();
1545 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001546 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1547 mId, __FUNCTION__, msg->message.error.frame_number,
1548 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001549
1550 // Set request error status for the request in the in-flight tracking
1551 {
1552 Mutex::Autolock l(mInFlightLock);
1553 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1554 if (idx >= 0) {
1555 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1556 }
1557 }
1558
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001559 if (listener != NULL) {
1560 listener->notifyError(msg->message.error.error_code,
1561 msg->message.error.frame_number, streamId);
1562 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001563 break;
1564 }
1565 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001566 ssize_t idx;
1567 uint32_t frameNumber = msg->message.shutter.frame_number;
1568 nsecs_t timestamp = msg->message.shutter.timestamp;
1569 // Verify ordering of shutter notifications
1570 {
1571 Mutex::Autolock l(mOutputLock);
1572 if (frameNumber != mNextShutterFrameNumber) {
1573 SET_ERR("Shutter notification out-of-order. Expected "
1574 "notification for frame %d, got frame %d",
1575 mNextShutterFrameNumber, frameNumber);
1576 break;
1577 }
1578 mNextShutterFrameNumber++;
1579 }
1580
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001581 int32_t requestId = -1;
1582
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001583 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001584 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001585 {
1586 Mutex::Autolock l(mInFlightLock);
1587 idx = mInFlightMap.indexOfKey(frameNumber);
1588 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001589 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1590 r.captureTimestamp = timestamp;
1591 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001592 }
1593 }
1594 if (idx < 0) {
1595 SET_ERR("Shutter notification for non-existent frame number %d",
1596 frameNumber);
1597 break;
1598 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001599 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1600 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001601 // Call listener, if any
1602 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001603 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001604 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001605 break;
1606 }
1607 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001608 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001609 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001610 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001611}
1612
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001613CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001614 ALOGV("%s", __FUNCTION__);
1615
Igor Murashkin1e479c02013-09-06 16:55:14 -07001616 CameraMetadata retVal;
1617
1618 if (mRequestThread != NULL) {
1619 retVal = mRequestThread->getLatestRequest();
1620 }
1621
Igor Murashkin1e479c02013-09-06 16:55:14 -07001622 return retVal;
1623}
1624
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001625/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626 * RequestThread inner class methods
1627 */
1628
1629Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001630 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001631 camera3_device_t *hal3Device) :
1632 Thread(false),
1633 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001634 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001635 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001636 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001637 mReconfigured(false),
1638 mDoPause(false),
1639 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001640 mFrameNumber(0),
1641 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001642 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001643}
1644
1645void Camera3Device::RequestThread::configurationComplete() {
1646 Mutex::Autolock l(mRequestLock);
1647 mReconfigured = true;
1648}
1649
1650status_t Camera3Device::RequestThread::queueRequest(
1651 sp<CaptureRequest> request) {
1652 Mutex::Autolock l(mRequestLock);
1653 mRequestQueue.push_back(request);
1654
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001655 unpauseForNewRequests();
1656
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001657 return OK;
1658}
1659
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001660
1661status_t Camera3Device::RequestThread::queueTrigger(
1662 RequestTrigger trigger[],
1663 size_t count) {
1664
1665 Mutex::Autolock l(mTriggerMutex);
1666 status_t ret;
1667
1668 for (size_t i = 0; i < count; ++i) {
1669 ret = queueTriggerLocked(trigger[i]);
1670
1671 if (ret != OK) {
1672 return ret;
1673 }
1674 }
1675
1676 return OK;
1677}
1678
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001679int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1680 sp<Camera3Device> d = device.promote();
1681 if (d != NULL) return d->mId;
1682 return 0;
1683}
1684
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001685status_t Camera3Device::RequestThread::queueTriggerLocked(
1686 RequestTrigger trigger) {
1687
1688 uint32_t tag = trigger.metadataTag;
1689 ssize_t index = mTriggerMap.indexOfKey(tag);
1690
1691 switch (trigger.getTagType()) {
1692 case TYPE_BYTE:
1693 // fall-through
1694 case TYPE_INT32:
1695 break;
1696 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001697 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1698 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001699 return INVALID_OPERATION;
1700 }
1701
1702 /**
1703 * Collect only the latest trigger, since we only have 1 field
1704 * in the request settings per trigger tag, and can't send more than 1
1705 * trigger per request.
1706 */
1707 if (index != NAME_NOT_FOUND) {
1708 mTriggerMap.editValueAt(index) = trigger;
1709 } else {
1710 mTriggerMap.add(tag, trigger);
1711 }
1712
1713 return OK;
1714}
1715
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001716status_t Camera3Device::RequestThread::setRepeatingRequests(
1717 const RequestList &requests) {
1718 Mutex::Autolock l(mRequestLock);
1719 mRepeatingRequests.clear();
1720 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1721 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001722
1723 unpauseForNewRequests();
1724
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001725 return OK;
1726}
1727
1728status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1729 Mutex::Autolock l(mRequestLock);
1730 mRepeatingRequests.clear();
1731 return OK;
1732}
1733
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001734status_t Camera3Device::RequestThread::clear() {
1735 Mutex::Autolock l(mRequestLock);
1736 mRepeatingRequests.clear();
1737 mRequestQueue.clear();
1738 mTriggerMap.clear();
1739 return OK;
1740}
1741
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001742void Camera3Device::RequestThread::setPaused(bool paused) {
1743 Mutex::Autolock l(mPauseLock);
1744 mDoPause = paused;
1745 mDoPauseSignal.signal();
1746}
1747
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001748status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1749 int32_t requestId, nsecs_t timeout) {
1750 Mutex::Autolock l(mLatestRequestMutex);
1751 status_t res;
1752 while (mLatestRequestId != requestId) {
1753 nsecs_t startTime = systemTime();
1754
1755 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1756 if (res != OK) return res;
1757
1758 timeout -= (systemTime() - startTime);
1759 }
1760
1761 return OK;
1762}
1763
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001764void Camera3Device::RequestThread::requestExit() {
1765 // Call parent to set up shutdown
1766 Thread::requestExit();
1767 // The exit from any possible waits
1768 mDoPauseSignal.signal();
1769 mRequestSignal.signal();
1770}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001771
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001772bool Camera3Device::RequestThread::threadLoop() {
1773
1774 status_t res;
1775
1776 // Handle paused state.
1777 if (waitIfPaused()) {
1778 return true;
1779 }
1780
1781 // Get work to do
1782
1783 sp<CaptureRequest> nextRequest = waitForNextRequest();
1784 if (nextRequest == NULL) {
1785 return true;
1786 }
1787
1788 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001789 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001790 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001791
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001792 // Get the request ID, if any
1793 int requestId;
1794 camera_metadata_entry_t requestIdEntry =
1795 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1796 if (requestIdEntry.count > 0) {
1797 requestId = requestIdEntry.data.i32[0];
1798 } else {
1799 ALOGW("%s: Did not have android.request.id set in the request",
1800 __FUNCTION__);
1801 requestId = NAME_NOT_FOUND;
1802 }
1803
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001804 // Insert any queued triggers (before metadata is locked)
1805 int32_t triggerCount;
1806 res = insertTriggers(nextRequest);
1807 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001808 SET_ERR("RequestThread: Unable to insert triggers "
1809 "(capture request %d, HAL device: %s (%d)",
1810 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001811 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1812 return false;
1813 }
1814 triggerCount = res;
1815
1816 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1817
1818 // If the request is the same as last, or we had triggers last time
1819 if (mPrevRequest != nextRequest || triggersMixedIn) {
1820 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07001821 * HAL workaround:
1822 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
1823 */
1824 res = addDummyTriggerIds(nextRequest);
1825 if (res != OK) {
1826 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
1827 "(capture request %d, HAL device: %s (%d)",
1828 (mFrameNumber+1), strerror(-res), res);
1829 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1830 return false;
1831 }
1832
1833 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001834 * The request should be presorted so accesses in HAL
1835 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1836 */
1837 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001838 request.settings = nextRequest->mSettings.getAndLock();
1839 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001840 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1841
1842 IF_ALOGV() {
1843 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1844 find_camera_metadata_ro_entry(
1845 request.settings,
1846 ANDROID_CONTROL_AF_TRIGGER,
1847 &e
1848 );
1849 if (e.count > 0) {
1850 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1851 __FUNCTION__,
1852 mFrameNumber+1,
1853 e.data.u8[0]);
1854 }
1855 }
1856 } else {
1857 // leave request.settings NULL to indicate 'reuse latest given'
1858 ALOGVV("%s: Request settings are REUSED",
1859 __FUNCTION__);
1860 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001861
1862 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001863
1864 // Fill in buffers
1865
1866 if (nextRequest->mInputStream != NULL) {
1867 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001868 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001869 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001870 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001871 " %s (%d)", strerror(-res), res);
1872 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1873 return true;
1874 }
1875 } else {
1876 request.input_buffer = NULL;
1877 }
1878
1879 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1880 nextRequest->mOutputStreams.size());
1881 request.output_buffers = outputBuffers.array();
1882 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1883 res = nextRequest->mOutputStreams.editItemAt(i)->
1884 getBuffer(&outputBuffers.editItemAt(i));
1885 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001886 ALOGE("RequestThread: Can't get output buffer, skipping request:"
1887 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001888 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1889 return true;
1890 }
1891 request.num_output_buffers++;
1892 }
1893
1894 request.frame_number = mFrameNumber++;
1895
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001896 // Log request in the in-flight queue
1897 sp<Camera3Device> parent = mParent.promote();
1898 if (parent == NULL) {
1899 CLOGE("RequestThread: Parent is gone");
1900 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1901 return false;
1902 }
1903
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001904 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001905 request.num_output_buffers);
1906 if (res != OK) {
1907 SET_ERR("RequestThread: Unable to register new in-flight request:"
1908 " %s (%d)", strerror(-res), res);
1909 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1910 return false;
1911 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001912
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001913 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001914 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1915 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001916 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001917 ATRACE_END();
1918
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001919 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001920 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001921 " device: %s (%d)", request.frame_number, strerror(-res), res);
1922 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1923 return false;
1924 }
1925
Igor Murashkin1e479c02013-09-06 16:55:14 -07001926 // Update the latest request sent to HAL
1927 if (request.settings != NULL) { // Don't update them if they were unchanged
1928 Mutex::Autolock al(mLatestRequestMutex);
1929
1930 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
1931 mLatestRequest.acquire(cloned);
1932 }
1933
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001934 if (request.settings != NULL) {
1935 nextRequest->mSettings.unlock(request.settings);
1936 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001937
1938 // Remove any previously queued triggers (after unlock)
1939 res = removeTriggers(mPrevRequest);
1940 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001941 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001942 "(capture request %d, HAL device: %s (%d)",
1943 request.frame_number, strerror(-res), res);
1944 return false;
1945 }
1946 mPrevTriggers = triggerCount;
1947
1948 // Read android.request.id from the request settings metadata
1949 // - inform waitUntilRequestProcessed thread of a new request ID
1950 {
1951 Mutex::Autolock al(mLatestRequestMutex);
1952
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001953 mLatestRequestId = requestId;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001954 mLatestRequestSignal.signal();
1955 }
1956
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001957 // Return input buffer back to framework
1958 if (request.input_buffer != NULL) {
1959 Camera3Stream *stream =
1960 Camera3Stream::cast(request.input_buffer->stream);
1961 res = stream->returnInputBuffer(*(request.input_buffer));
1962 // Note: stream may be deallocated at this point, if this buffer was the
1963 // last reference to it.
1964 if (res != OK) {
1965 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1966 " its stream:%s (%d)", __FUNCTION__,
1967 request.frame_number, strerror(-res), res);
1968 // TODO: Report error upstream
1969 }
1970 }
1971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001972 return true;
1973}
1974
Igor Murashkin1e479c02013-09-06 16:55:14 -07001975CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
1976 Mutex::Autolock al(mLatestRequestMutex);
1977
1978 ALOGV("RequestThread::%s", __FUNCTION__);
1979
1980 return mLatestRequest;
1981}
1982
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001983void Camera3Device::RequestThread::cleanUpFailedRequest(
1984 camera3_capture_request_t &request,
1985 sp<CaptureRequest> &nextRequest,
1986 Vector<camera3_stream_buffer_t> &outputBuffers) {
1987
1988 if (request.settings != NULL) {
1989 nextRequest->mSettings.unlock(request.settings);
1990 }
1991 if (request.input_buffer != NULL) {
1992 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001993 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001994 }
1995 for (size_t i = 0; i < request.num_output_buffers; i++) {
1996 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1997 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1998 outputBuffers[i], 0);
1999 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002000}
2001
2002sp<Camera3Device::CaptureRequest>
2003 Camera3Device::RequestThread::waitForNextRequest() {
2004 status_t res;
2005 sp<CaptureRequest> nextRequest;
2006
2007 // Optimized a bit for the simple steady-state case (single repeating
2008 // request), to avoid putting that request in the queue temporarily.
2009 Mutex::Autolock l(mRequestLock);
2010
2011 while (mRequestQueue.empty()) {
2012 if (!mRepeatingRequests.empty()) {
2013 // Always atomically enqueue all requests in a repeating request
2014 // list. Guarantees a complete in-sequence set of captures to
2015 // application.
2016 const RequestList &requests = mRepeatingRequests;
2017 RequestList::const_iterator firstRequest =
2018 requests.begin();
2019 nextRequest = *firstRequest;
2020 mRequestQueue.insert(mRequestQueue.end(),
2021 ++firstRequest,
2022 requests.end());
2023 // No need to wait any longer
2024 break;
2025 }
2026
2027 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2028
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002029 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2030 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002031 Mutex::Autolock pl(mPauseLock);
2032 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002033 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002034 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002035 // Let the tracker know
2036 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2037 if (statusTracker != 0) {
2038 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2039 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002040 }
2041 // Stop waiting for now and let thread management happen
2042 return NULL;
2043 }
2044 }
2045
2046 if (nextRequest == NULL) {
2047 // Don't have a repeating request already in hand, so queue
2048 // must have an entry now.
2049 RequestList::iterator firstRequest =
2050 mRequestQueue.begin();
2051 nextRequest = *firstRequest;
2052 mRequestQueue.erase(firstRequest);
2053 }
2054
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002055 // In case we've been unpaused by setPaused clearing mDoPause, need to
2056 // update internal pause state (capture/setRepeatingRequest unpause
2057 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002058 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002059 if (mPaused) {
2060 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2061 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2062 if (statusTracker != 0) {
2063 statusTracker->markComponentActive(mStatusId);
2064 }
2065 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002066 mPaused = false;
2067
2068 // Check if we've reconfigured since last time, and reset the preview
2069 // request if so. Can't use 'NULL request == repeat' across configure calls.
2070 if (mReconfigured) {
2071 mPrevRequest.clear();
2072 mReconfigured = false;
2073 }
2074
2075 return nextRequest;
2076}
2077
2078bool Camera3Device::RequestThread::waitIfPaused() {
2079 status_t res;
2080 Mutex::Autolock l(mPauseLock);
2081 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002082 if (mPaused == false) {
2083 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002084 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2085 // Let the tracker know
2086 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2087 if (statusTracker != 0) {
2088 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2089 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002090 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002091
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002093 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002094 return true;
2095 }
2096 }
2097 // We don't set mPaused to false here, because waitForNextRequest needs
2098 // to further manage the paused state in case of starvation.
2099 return false;
2100}
2101
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002102void Camera3Device::RequestThread::unpauseForNewRequests() {
2103 // With work to do, mark thread as unpaused.
2104 // If paused by request (setPaused), don't resume, to avoid
2105 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002106 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002107 Mutex::Autolock p(mPauseLock);
2108 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002109 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2110 if (mPaused) {
2111 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2112 if (statusTracker != 0) {
2113 statusTracker->markComponentActive(mStatusId);
2114 }
2115 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002116 mPaused = false;
2117 }
2118}
2119
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002120void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2121 sp<Camera3Device> parent = mParent.promote();
2122 if (parent != NULL) {
2123 va_list args;
2124 va_start(args, fmt);
2125
2126 parent->setErrorStateV(fmt, args);
2127
2128 va_end(args);
2129 }
2130}
2131
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002132status_t Camera3Device::RequestThread::insertTriggers(
2133 const sp<CaptureRequest> &request) {
2134
2135 Mutex::Autolock al(mTriggerMutex);
2136
2137 CameraMetadata &metadata = request->mSettings;
2138 size_t count = mTriggerMap.size();
2139
2140 for (size_t i = 0; i < count; ++i) {
2141 RequestTrigger trigger = mTriggerMap.valueAt(i);
2142
2143 uint32_t tag = trigger.metadataTag;
2144 camera_metadata_entry entry = metadata.find(tag);
2145
2146 if (entry.count > 0) {
2147 /**
2148 * Already has an entry for this trigger in the request.
2149 * Rewrite it with our requested trigger value.
2150 */
2151 RequestTrigger oldTrigger = trigger;
2152
2153 oldTrigger.entryValue = entry.data.u8[0];
2154
2155 mTriggerReplacedMap.add(tag, oldTrigger);
2156 } else {
2157 /**
2158 * More typical, no trigger entry, so we just add it
2159 */
2160 mTriggerRemovedMap.add(tag, trigger);
2161 }
2162
2163 status_t res;
2164
2165 switch (trigger.getTagType()) {
2166 case TYPE_BYTE: {
2167 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2168 res = metadata.update(tag,
2169 &entryValue,
2170 /*count*/1);
2171 break;
2172 }
2173 case TYPE_INT32:
2174 res = metadata.update(tag,
2175 &trigger.entryValue,
2176 /*count*/1);
2177 break;
2178 default:
2179 ALOGE("%s: Type not supported: 0x%x",
2180 __FUNCTION__,
2181 trigger.getTagType());
2182 return INVALID_OPERATION;
2183 }
2184
2185 if (res != OK) {
2186 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2187 ", value %d", __FUNCTION__, trigger.getTagName(),
2188 trigger.entryValue);
2189 return res;
2190 }
2191
2192 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2193 trigger.getTagName(),
2194 trigger.entryValue);
2195 }
2196
2197 mTriggerMap.clear();
2198
2199 return count;
2200}
2201
2202status_t Camera3Device::RequestThread::removeTriggers(
2203 const sp<CaptureRequest> &request) {
2204 Mutex::Autolock al(mTriggerMutex);
2205
2206 CameraMetadata &metadata = request->mSettings;
2207
2208 /**
2209 * Replace all old entries with their old values.
2210 */
2211 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2212 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2213
2214 status_t res;
2215
2216 uint32_t tag = trigger.metadataTag;
2217 switch (trigger.getTagType()) {
2218 case TYPE_BYTE: {
2219 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2220 res = metadata.update(tag,
2221 &entryValue,
2222 /*count*/1);
2223 break;
2224 }
2225 case TYPE_INT32:
2226 res = metadata.update(tag,
2227 &trigger.entryValue,
2228 /*count*/1);
2229 break;
2230 default:
2231 ALOGE("%s: Type not supported: 0x%x",
2232 __FUNCTION__,
2233 trigger.getTagType());
2234 return INVALID_OPERATION;
2235 }
2236
2237 if (res != OK) {
2238 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2239 ", trigger value %d", __FUNCTION__,
2240 trigger.getTagName(), trigger.entryValue);
2241 return res;
2242 }
2243 }
2244 mTriggerReplacedMap.clear();
2245
2246 /**
2247 * Remove all new entries.
2248 */
2249 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2250 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2251 status_t res = metadata.erase(trigger.metadataTag);
2252
2253 if (res != OK) {
2254 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2255 ", trigger value %d", __FUNCTION__,
2256 trigger.getTagName(), trigger.entryValue);
2257 return res;
2258 }
2259 }
2260 mTriggerRemovedMap.clear();
2261
2262 return OK;
2263}
2264
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002265status_t Camera3Device::RequestThread::addDummyTriggerIds(
2266 const sp<CaptureRequest> &request) {
2267 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2268 static const int32_t dummyTriggerId = 1;
2269 status_t res;
2270
2271 CameraMetadata &metadata = request->mSettings;
2272
2273 // If AF trigger is active, insert a dummy AF trigger ID if none already
2274 // exists
2275 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2276 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2277 if (afTrigger.count > 0 &&
2278 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2279 afId.count == 0) {
2280 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2281 if (res != OK) return res;
2282 }
2283
2284 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2285 // if none already exists
2286 camera_metadata_entry pcTrigger =
2287 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2288 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2289 if (pcTrigger.count > 0 &&
2290 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2291 pcId.count == 0) {
2292 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2293 &dummyTriggerId, 1);
2294 if (res != OK) return res;
2295 }
2296
2297 return OK;
2298}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002299
2300
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002301/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002302 * Static callback forwarding methods from HAL to instance
2303 */
2304
2305void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2306 const camera3_capture_result *result) {
2307 Camera3Device *d =
2308 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2309 d->processCaptureResult(result);
2310}
2311
2312void Camera3Device::sNotify(const camera3_callback_ops *cb,
2313 const camera3_notify_msg *msg) {
2314 Camera3Device *d =
2315 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2316 d->notify(msg);
2317}
2318
2319}; // namespace android