blob: 6f2dc853bfdebcf0bdc4213637716a2e33935fd6 [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 status_t res;
1003 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001005 while (mResultQueue.empty()) {
1006 res = mResultSignal.waitRelative(mOutputLock, timeout);
1007 if (res == TIMED_OUT) {
1008 return res;
1009 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001010 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1011 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001012 return res;
1013 }
1014 }
1015 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001016}
1017
1018status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1019 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001020 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001021
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001022 if (mResultQueue.empty()) {
1023 return NOT_ENOUGH_DATA;
1024 }
1025
1026 CameraMetadata &result = *(mResultQueue.begin());
1027 frame->acquire(result);
1028 mResultQueue.erase(mResultQueue.begin());
1029
1030 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001031}
1032
1033status_t Camera3Device::triggerAutofocus(uint32_t id) {
1034 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001036
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001037 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1038 // Mix-in this trigger into the next request and only the next request.
1039 RequestTrigger trigger[] = {
1040 {
1041 ANDROID_CONTROL_AF_TRIGGER,
1042 ANDROID_CONTROL_AF_TRIGGER_START
1043 },
1044 {
1045 ANDROID_CONTROL_AF_TRIGGER_ID,
1046 static_cast<int32_t>(id)
1047 },
1048 };
1049
1050 return mRequestThread->queueTrigger(trigger,
1051 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052}
1053
1054status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1055 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001056 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001057
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001058 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1059 // Mix-in this trigger into the next request and only the next request.
1060 RequestTrigger trigger[] = {
1061 {
1062 ANDROID_CONTROL_AF_TRIGGER,
1063 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1064 },
1065 {
1066 ANDROID_CONTROL_AF_TRIGGER_ID,
1067 static_cast<int32_t>(id)
1068 },
1069 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001070
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001071 return mRequestThread->queueTrigger(trigger,
1072 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001073}
1074
1075status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1076 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001077 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001078
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001079 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1080 // Mix-in this trigger into the next request and only the next request.
1081 RequestTrigger trigger[] = {
1082 {
1083 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1084 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1085 },
1086 {
1087 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1088 static_cast<int32_t>(id)
1089 },
1090 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001091
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001092 return mRequestThread->queueTrigger(trigger,
1093 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001094}
1095
1096status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1097 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1098 ATRACE_CALL();
1099 (void)reprocessStreamId; (void)buffer; (void)listener;
1100
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001101 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001102 return INVALID_OPERATION;
1103}
1104
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001105status_t Camera3Device::flush() {
1106 ATRACE_CALL();
1107 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001108 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001109 Mutex::Autolock l(mLock);
1110
1111 mRequestThread->clear();
1112 return mHal3Device->ops->flush(mHal3Device);
1113}
1114
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001115/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001116 * Methods called by subclasses
1117 */
1118
1119void Camera3Device::notifyStatus(bool idle) {
1120 {
1121 // Need mLock to safely update state and synchronize to current
1122 // state of methods in flight.
1123 Mutex::Autolock l(mLock);
1124 // We can get various system-idle notices from the status tracker
1125 // while starting up. Only care about them if we've actually sent
1126 // in some requests recently.
1127 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1128 return;
1129 }
1130 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1131 idle ? "idle" : "active");
1132 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1133 mRecentStatusUpdates.add(mStatus);
1134 mStatusChanged.signal();
1135
1136 // Skip notifying listener if we're doing some user-transparent
1137 // state changes
1138 if (mPauseStateNotify) return;
1139 }
1140 NotificationListener *listener;
1141 {
1142 Mutex::Autolock l(mOutputLock);
1143 listener = mListener;
1144 }
1145 if (idle && listener != NULL) {
1146 listener->notifyIdle();
1147 }
1148}
1149
1150/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001151 * Camera3Device private methods
1152 */
1153
1154sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1155 const CameraMetadata &request) {
1156 ATRACE_CALL();
1157 status_t res;
1158
1159 sp<CaptureRequest> newRequest = new CaptureRequest;
1160 newRequest->mSettings = request;
1161
1162 camera_metadata_entry_t inputStreams =
1163 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1164 if (inputStreams.count > 0) {
1165 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001166 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001167 CLOGE("Request references unknown input stream %d",
1168 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001169 return NULL;
1170 }
1171 // Lazy completion of stream configuration (allocation/registration)
1172 // on first use
1173 if (mInputStream->isConfiguring()) {
1174 res = mInputStream->finishConfiguration(mHal3Device);
1175 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001176 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001178 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001179 return NULL;
1180 }
1181 }
1182
1183 newRequest->mInputStream = mInputStream;
1184 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1185 }
1186
1187 camera_metadata_entry_t streams =
1188 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1189 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001190 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001191 return NULL;
1192 }
1193
1194 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001195 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001197 CLOGE("Request references unknown stream %d",
1198 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001199 return NULL;
1200 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001201 sp<Camera3OutputStreamInterface> stream =
1202 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203
1204 // Lazy completion of stream configuration (allocation/registration)
1205 // on first use
1206 if (stream->isConfiguring()) {
1207 res = stream->finishConfiguration(mHal3Device);
1208 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001209 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1210 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001211 return NULL;
1212 }
1213 }
1214
1215 newRequest->mOutputStreams.push(stream);
1216 }
1217 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1218
1219 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220}
1221
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222status_t Camera3Device::configureStreamsLocked() {
1223 ATRACE_CALL();
1224 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001227 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001228 return INVALID_OPERATION;
1229 }
1230
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001231 if (!mNeedConfig) {
1232 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1233 return OK;
1234 }
1235
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001237 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238
1239 camera3_stream_configuration config;
1240
1241 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1242
1243 Vector<camera3_stream_t*> streams;
1244 streams.setCapacity(config.num_streams);
1245
1246 if (mInputStream != NULL) {
1247 camera3_stream_t *inputStream;
1248 inputStream = mInputStream->startConfiguration();
1249 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001250 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001251 return INVALID_OPERATION;
1252 }
1253 streams.add(inputStream);
1254 }
1255
1256 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001257
1258 // Don't configure bidi streams twice, nor add them twice to the list
1259 if (mOutputStreams[i].get() ==
1260 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1261
1262 config.num_streams--;
1263 continue;
1264 }
1265
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001266 camera3_stream_t *outputStream;
1267 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1268 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001269 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 return INVALID_OPERATION;
1271 }
1272 streams.add(outputStream);
1273 }
1274
1275 config.streams = streams.editArray();
1276
1277 // Do the HAL configuration; will potentially touch stream
1278 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001279 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001281 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001282
1283 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001284 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1285 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 return res;
1287 }
1288
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001289 // Finish all stream configuration immediately.
1290 // TODO: Try to relax this later back to lazy completion, which should be
1291 // faster
1292
Igor Murashkin073f8572013-05-02 14:59:28 -07001293 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001294 res = mInputStream->finishConfiguration(mHal3Device);
1295 if (res != OK) {
1296 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1297 mInputStream->getId(), strerror(-res), res);
1298 return res;
1299 }
1300 }
1301
1302 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001303 sp<Camera3OutputStreamInterface> outputStream =
1304 mOutputStreams.editValueAt(i);
1305 if (outputStream->isConfiguring()) {
1306 res = outputStream->finishConfiguration(mHal3Device);
1307 if (res != OK) {
1308 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1309 outputStream->getId(), strerror(-res), res);
1310 return res;
1311 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001312 }
1313 }
1314
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001315 // Request thread needs to know to avoid using repeat-last-settings protocol
1316 // across configure_streams() calls
1317 mRequestThread->configurationComplete();
1318
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001319 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001321 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001323 if (config.num_streams > 0) {
1324 mStatus = STATUS_CONFIGURED;
1325 } else {
1326 mStatus = STATUS_UNCONFIGURED;
1327 }
1328
1329 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1330
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001331 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001332}
1333
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001334void Camera3Device::setErrorState(const char *fmt, ...) {
1335 Mutex::Autolock l(mLock);
1336 va_list args;
1337 va_start(args, fmt);
1338
1339 setErrorStateLockedV(fmt, args);
1340
1341 va_end(args);
1342}
1343
1344void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1345 Mutex::Autolock l(mLock);
1346 setErrorStateLockedV(fmt, args);
1347}
1348
1349void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1350 va_list args;
1351 va_start(args, fmt);
1352
1353 setErrorStateLockedV(fmt, args);
1354
1355 va_end(args);
1356}
1357
1358void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001359 // Print out all error messages to log
1360 String8 errorCause = String8::formatV(fmt, args);
1361 ALOGE("Camera %d: %s", mId, errorCause.string());
1362
1363 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001364 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001365
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001366 mErrorCause = errorCause;
1367
1368 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 mStatus = STATUS_ERROR;
1370}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001371
1372/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001373 * In-flight request management
1374 */
1375
1376status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001377 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 ATRACE_CALL();
1379 Mutex::Autolock l(mInFlightLock);
1380
1381 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001382 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001383 if (res < 0) return res;
1384
1385 return OK;
1386}
1387
1388/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 * Camera HAL device callback methods
1390 */
1391
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001392void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001393 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001394
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001395 status_t res;
1396
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001397 uint32_t frameNumber = result->frame_number;
1398 if (result->result == NULL && result->num_output_buffers == 0) {
1399 SET_ERR("No result data provided by HAL for frame %d",
1400 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001401 return;
1402 }
1403
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001404 // Get capture timestamp from list of in-flight requests, where it was added
1405 // by the shutter notification for this frame. Then update the in-flight
1406 // status and remove the in-flight entry if all result data has been
1407 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001408 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001409 {
1410 Mutex::Autolock l(mInFlightLock);
1411 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1412 if (idx == NAME_NOT_FOUND) {
1413 SET_ERR("Unknown frame number for capture result: %d",
1414 frameNumber);
1415 return;
1416 }
1417 InFlightRequest &request = mInFlightMap.editValueAt(idx);
1418 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001419 /**
1420 * One of the following must happen before it's legal to call process_capture_result:
1421 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1422 * - CAMERA3_MSG_ERROR (expected during flush)
1423 */
1424 if (request.requestStatus == OK && timestamp == 0) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001425 SET_ERR("Called before shutter notify for frame %d",
1426 frameNumber);
1427 return;
1428 }
1429
1430 if (result->result != NULL) {
1431 if (request.haveResultMetadata) {
1432 SET_ERR("Called multiple times with metadata for frame %d",
1433 frameNumber);
1434 return;
1435 }
1436 request.haveResultMetadata = true;
1437 }
1438
1439 request.numBuffersLeft -= result->num_output_buffers;
1440
1441 if (request.numBuffersLeft < 0) {
1442 SET_ERR("Too many buffers returned for frame %d",
1443 frameNumber);
1444 return;
1445 }
1446
1447 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001448 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001449 mInFlightMap.removeItemsAt(idx, 1);
1450 }
1451
1452 // Sanity check - if we have too many in-flight frames, something has
1453 // likely gone wrong
1454 if (mInFlightMap.size() > kInFlightWarnLimit) {
1455 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1456 }
1457
1458 }
1459
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001460 // Process the result metadata, if provided
1461 if (result->result != NULL) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001462 Mutex::Autolock l(mOutputLock);
1463
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001464 if (frameNumber != mNextResultFrameNumber) {
1465 SET_ERR("Out-of-order capture result metadata submitted! "
1466 "(got frame number %d, expecting %d)",
1467 frameNumber, mNextResultFrameNumber);
1468 return;
1469 }
1470 mNextResultFrameNumber++;
1471
1472 CameraMetadata &captureResult =
1473 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001474
1475 captureResult = result->result;
Igor Murashkind2c90692013-04-02 12:32:32 -07001476 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001477 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001478 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001479 frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001480 } else {
1481 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001482 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001483 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001484
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001485 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001486
1487 camera_metadata_entry entry =
1488 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1489 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001490 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001491 frameNumber);
Alex Rayfe7e0c62013-05-30 00:12:13 -07001492 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001493 SET_ERR("Timestamp mismatch between shutter notify and result"
1494 " metadata for frame %d (%lld vs %lld respectively)",
1495 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001496 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001497 } // scope for mOutputLock
1498
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001499 // Return completed buffers to their streams with the timestamp
1500
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001501 for (size_t i = 0; i < result->num_output_buffers; i++) {
1502 Camera3Stream *stream =
1503 Camera3Stream::cast(result->output_buffers[i].stream);
1504 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1505 // Note: stream may be deallocated at this point, if this buffer was the
1506 // last reference to it.
1507 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001508 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001509 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001510 }
1511 }
1512
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001513 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001514
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001515 if (result->result != NULL) {
1516 mResultSignal.signal();
1517 }
1518
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001519}
1520
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001521
1522
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001523void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001524 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001525 NotificationListener *listener;
1526 {
1527 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001528 listener = mListener;
1529 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001531 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001532 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001533 return;
1534 }
1535
1536 switch (msg->type) {
1537 case CAMERA3_MSG_ERROR: {
1538 int streamId = 0;
1539 if (msg->message.error.error_stream != NULL) {
1540 Camera3Stream *stream =
1541 Camera3Stream::cast(
1542 msg->message.error.error_stream);
1543 streamId = stream->getId();
1544 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001545 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1546 mId, __FUNCTION__, msg->message.error.frame_number,
1547 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001548
1549 // Set request error status for the request in the in-flight tracking
1550 {
1551 Mutex::Autolock l(mInFlightLock);
1552 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1553 if (idx >= 0) {
1554 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1555 }
1556 }
1557
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001558 if (listener != NULL) {
1559 listener->notifyError(msg->message.error.error_code,
1560 msg->message.error.frame_number, streamId);
1561 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001562 break;
1563 }
1564 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001565 ssize_t idx;
1566 uint32_t frameNumber = msg->message.shutter.frame_number;
1567 nsecs_t timestamp = msg->message.shutter.timestamp;
1568 // Verify ordering of shutter notifications
1569 {
1570 Mutex::Autolock l(mOutputLock);
1571 if (frameNumber != mNextShutterFrameNumber) {
1572 SET_ERR("Shutter notification out-of-order. Expected "
1573 "notification for frame %d, got frame %d",
1574 mNextShutterFrameNumber, frameNumber);
1575 break;
1576 }
1577 mNextShutterFrameNumber++;
1578 }
1579
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001580 int32_t requestId = -1;
1581
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001582 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001583 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001584 {
1585 Mutex::Autolock l(mInFlightLock);
1586 idx = mInFlightMap.indexOfKey(frameNumber);
1587 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001588 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1589 r.captureTimestamp = timestamp;
1590 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001591 }
1592 }
1593 if (idx < 0) {
1594 SET_ERR("Shutter notification for non-existent frame number %d",
1595 frameNumber);
1596 break;
1597 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001598 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1599 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001600 // Call listener, if any
1601 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001602 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001603 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001604 break;
1605 }
1606 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001607 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001608 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001609 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001610}
1611
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001612CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001613 ALOGV("%s", __FUNCTION__);
1614
Igor Murashkin1e479c02013-09-06 16:55:14 -07001615 CameraMetadata retVal;
1616
1617 if (mRequestThread != NULL) {
1618 retVal = mRequestThread->getLatestRequest();
1619 }
1620
Igor Murashkin1e479c02013-09-06 16:55:14 -07001621 return retVal;
1622}
1623
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001624/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001625 * RequestThread inner class methods
1626 */
1627
1628Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001629 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 camera3_device_t *hal3Device) :
1631 Thread(false),
1632 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001633 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001634 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001635 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001636 mReconfigured(false),
1637 mDoPause(false),
1638 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001639 mFrameNumber(0),
1640 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001641 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001642}
1643
1644void Camera3Device::RequestThread::configurationComplete() {
1645 Mutex::Autolock l(mRequestLock);
1646 mReconfigured = true;
1647}
1648
1649status_t Camera3Device::RequestThread::queueRequest(
1650 sp<CaptureRequest> request) {
1651 Mutex::Autolock l(mRequestLock);
1652 mRequestQueue.push_back(request);
1653
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001654 unpauseForNewRequests();
1655
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001656 return OK;
1657}
1658
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001659
1660status_t Camera3Device::RequestThread::queueTrigger(
1661 RequestTrigger trigger[],
1662 size_t count) {
1663
1664 Mutex::Autolock l(mTriggerMutex);
1665 status_t ret;
1666
1667 for (size_t i = 0; i < count; ++i) {
1668 ret = queueTriggerLocked(trigger[i]);
1669
1670 if (ret != OK) {
1671 return ret;
1672 }
1673 }
1674
1675 return OK;
1676}
1677
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001678int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1679 sp<Camera3Device> d = device.promote();
1680 if (d != NULL) return d->mId;
1681 return 0;
1682}
1683
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001684status_t Camera3Device::RequestThread::queueTriggerLocked(
1685 RequestTrigger trigger) {
1686
1687 uint32_t tag = trigger.metadataTag;
1688 ssize_t index = mTriggerMap.indexOfKey(tag);
1689
1690 switch (trigger.getTagType()) {
1691 case TYPE_BYTE:
1692 // fall-through
1693 case TYPE_INT32:
1694 break;
1695 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001696 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1697 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001698 return INVALID_OPERATION;
1699 }
1700
1701 /**
1702 * Collect only the latest trigger, since we only have 1 field
1703 * in the request settings per trigger tag, and can't send more than 1
1704 * trigger per request.
1705 */
1706 if (index != NAME_NOT_FOUND) {
1707 mTriggerMap.editValueAt(index) = trigger;
1708 } else {
1709 mTriggerMap.add(tag, trigger);
1710 }
1711
1712 return OK;
1713}
1714
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001715status_t Camera3Device::RequestThread::setRepeatingRequests(
1716 const RequestList &requests) {
1717 Mutex::Autolock l(mRequestLock);
1718 mRepeatingRequests.clear();
1719 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1720 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001721
1722 unpauseForNewRequests();
1723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001724 return OK;
1725}
1726
1727status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1728 Mutex::Autolock l(mRequestLock);
1729 mRepeatingRequests.clear();
1730 return OK;
1731}
1732
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001733status_t Camera3Device::RequestThread::clear() {
1734 Mutex::Autolock l(mRequestLock);
1735 mRepeatingRequests.clear();
1736 mRequestQueue.clear();
1737 mTriggerMap.clear();
1738 return OK;
1739}
1740
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001741void Camera3Device::RequestThread::setPaused(bool paused) {
1742 Mutex::Autolock l(mPauseLock);
1743 mDoPause = paused;
1744 mDoPauseSignal.signal();
1745}
1746
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001747status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1748 int32_t requestId, nsecs_t timeout) {
1749 Mutex::Autolock l(mLatestRequestMutex);
1750 status_t res;
1751 while (mLatestRequestId != requestId) {
1752 nsecs_t startTime = systemTime();
1753
1754 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
1755 if (res != OK) return res;
1756
1757 timeout -= (systemTime() - startTime);
1758 }
1759
1760 return OK;
1761}
1762
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001763void Camera3Device::RequestThread::requestExit() {
1764 // Call parent to set up shutdown
1765 Thread::requestExit();
1766 // The exit from any possible waits
1767 mDoPauseSignal.signal();
1768 mRequestSignal.signal();
1769}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001770
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001771bool Camera3Device::RequestThread::threadLoop() {
1772
1773 status_t res;
1774
1775 // Handle paused state.
1776 if (waitIfPaused()) {
1777 return true;
1778 }
1779
1780 // Get work to do
1781
1782 sp<CaptureRequest> nextRequest = waitForNextRequest();
1783 if (nextRequest == NULL) {
1784 return true;
1785 }
1786
1787 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001788 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001789 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001790
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001791 // Get the request ID, if any
1792 int requestId;
1793 camera_metadata_entry_t requestIdEntry =
1794 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
1795 if (requestIdEntry.count > 0) {
1796 requestId = requestIdEntry.data.i32[0];
1797 } else {
1798 ALOGW("%s: Did not have android.request.id set in the request",
1799 __FUNCTION__);
1800 requestId = NAME_NOT_FOUND;
1801 }
1802
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001803 // Insert any queued triggers (before metadata is locked)
1804 int32_t triggerCount;
1805 res = insertTriggers(nextRequest);
1806 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001807 SET_ERR("RequestThread: Unable to insert triggers "
1808 "(capture request %d, HAL device: %s (%d)",
1809 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001810 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1811 return false;
1812 }
1813 triggerCount = res;
1814
1815 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
1816
1817 // If the request is the same as last, or we had triggers last time
1818 if (mPrevRequest != nextRequest || triggersMixedIn) {
1819 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07001820 * HAL workaround:
1821 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
1822 */
1823 res = addDummyTriggerIds(nextRequest);
1824 if (res != OK) {
1825 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
1826 "(capture request %d, HAL device: %s (%d)",
1827 (mFrameNumber+1), strerror(-res), res);
1828 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1829 return false;
1830 }
1831
1832 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001833 * The request should be presorted so accesses in HAL
1834 * are O(logn). Sidenote, sorting a sorted metadata is nop.
1835 */
1836 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001837 request.settings = nextRequest->mSettings.getAndLock();
1838 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001839 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
1840
1841 IF_ALOGV() {
1842 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
1843 find_camera_metadata_ro_entry(
1844 request.settings,
1845 ANDROID_CONTROL_AF_TRIGGER,
1846 &e
1847 );
1848 if (e.count > 0) {
1849 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
1850 __FUNCTION__,
1851 mFrameNumber+1,
1852 e.data.u8[0]);
1853 }
1854 }
1855 } else {
1856 // leave request.settings NULL to indicate 'reuse latest given'
1857 ALOGVV("%s: Request settings are REUSED",
1858 __FUNCTION__);
1859 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001860
1861 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001862
1863 // Fill in buffers
1864
1865 if (nextRequest->mInputStream != NULL) {
1866 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001867 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001868 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001869 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001870 " %s (%d)", strerror(-res), res);
1871 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1872 return true;
1873 }
1874 } else {
1875 request.input_buffer = NULL;
1876 }
1877
1878 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
1879 nextRequest->mOutputStreams.size());
1880 request.output_buffers = outputBuffers.array();
1881 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
1882 res = nextRequest->mOutputStreams.editItemAt(i)->
1883 getBuffer(&outputBuffers.editItemAt(i));
1884 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001885 ALOGE("RequestThread: Can't get output buffer, skipping request:"
1886 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001887 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1888 return true;
1889 }
1890 request.num_output_buffers++;
1891 }
1892
1893 request.frame_number = mFrameNumber++;
1894
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001895 // Log request in the in-flight queue
1896 sp<Camera3Device> parent = mParent.promote();
1897 if (parent == NULL) {
1898 CLOGE("RequestThread: Parent is gone");
1899 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1900 return false;
1901 }
1902
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001903 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001904 request.num_output_buffers);
1905 if (res != OK) {
1906 SET_ERR("RequestThread: Unable to register new in-flight request:"
1907 " %s (%d)", strerror(-res), res);
1908 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1909 return false;
1910 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001911
Zhijun Hecc27e112013-10-03 16:12:43 -07001912 // Inform waitUntilRequestProcessed thread of a new request ID
1913 {
1914 Mutex::Autolock al(mLatestRequestMutex);
1915
1916 mLatestRequestId = requestId;
1917 mLatestRequestSignal.signal();
1918 }
1919
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001920 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001921 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
1922 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001923 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001924 ATRACE_END();
1925
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001926 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001927 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928 " device: %s (%d)", request.frame_number, strerror(-res), res);
1929 cleanUpFailedRequest(request, nextRequest, outputBuffers);
1930 return false;
1931 }
1932
Igor Murashkin1e479c02013-09-06 16:55:14 -07001933 // Update the latest request sent to HAL
1934 if (request.settings != NULL) { // Don't update them if they were unchanged
1935 Mutex::Autolock al(mLatestRequestMutex);
1936
1937 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
1938 mLatestRequest.acquire(cloned);
1939 }
1940
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001941 if (request.settings != NULL) {
1942 nextRequest->mSettings.unlock(request.settings);
1943 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001944
1945 // Remove any previously queued triggers (after unlock)
1946 res = removeTriggers(mPrevRequest);
1947 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001948 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001949 "(capture request %d, HAL device: %s (%d)",
1950 request.frame_number, strerror(-res), res);
1951 return false;
1952 }
1953 mPrevTriggers = triggerCount;
1954
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001955 // Return input buffer back to framework
1956 if (request.input_buffer != NULL) {
1957 Camera3Stream *stream =
1958 Camera3Stream::cast(request.input_buffer->stream);
1959 res = stream->returnInputBuffer(*(request.input_buffer));
1960 // Note: stream may be deallocated at this point, if this buffer was the
1961 // last reference to it.
1962 if (res != OK) {
1963 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1964 " its stream:%s (%d)", __FUNCTION__,
1965 request.frame_number, strerror(-res), res);
1966 // TODO: Report error upstream
1967 }
1968 }
1969
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001970 return true;
1971}
1972
Igor Murashkin1e479c02013-09-06 16:55:14 -07001973CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
1974 Mutex::Autolock al(mLatestRequestMutex);
1975
1976 ALOGV("RequestThread::%s", __FUNCTION__);
1977
1978 return mLatestRequest;
1979}
1980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981void Camera3Device::RequestThread::cleanUpFailedRequest(
1982 camera3_capture_request_t &request,
1983 sp<CaptureRequest> &nextRequest,
1984 Vector<camera3_stream_buffer_t> &outputBuffers) {
1985
1986 if (request.settings != NULL) {
1987 nextRequest->mSettings.unlock(request.settings);
1988 }
1989 if (request.input_buffer != NULL) {
1990 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001991 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001992 }
1993 for (size_t i = 0; i < request.num_output_buffers; i++) {
1994 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
1995 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
1996 outputBuffers[i], 0);
1997 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001998}
1999
2000sp<Camera3Device::CaptureRequest>
2001 Camera3Device::RequestThread::waitForNextRequest() {
2002 status_t res;
2003 sp<CaptureRequest> nextRequest;
2004
2005 // Optimized a bit for the simple steady-state case (single repeating
2006 // request), to avoid putting that request in the queue temporarily.
2007 Mutex::Autolock l(mRequestLock);
2008
2009 while (mRequestQueue.empty()) {
2010 if (!mRepeatingRequests.empty()) {
2011 // Always atomically enqueue all requests in a repeating request
2012 // list. Guarantees a complete in-sequence set of captures to
2013 // application.
2014 const RequestList &requests = mRepeatingRequests;
2015 RequestList::const_iterator firstRequest =
2016 requests.begin();
2017 nextRequest = *firstRequest;
2018 mRequestQueue.insert(mRequestQueue.end(),
2019 ++firstRequest,
2020 requests.end());
2021 // No need to wait any longer
2022 break;
2023 }
2024
2025 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2026
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002027 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2028 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002029 Mutex::Autolock pl(mPauseLock);
2030 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002031 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002032 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002033 // Let the tracker know
2034 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2035 if (statusTracker != 0) {
2036 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2037 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002038 }
2039 // Stop waiting for now and let thread management happen
2040 return NULL;
2041 }
2042 }
2043
2044 if (nextRequest == NULL) {
2045 // Don't have a repeating request already in hand, so queue
2046 // must have an entry now.
2047 RequestList::iterator firstRequest =
2048 mRequestQueue.begin();
2049 nextRequest = *firstRequest;
2050 mRequestQueue.erase(firstRequest);
2051 }
2052
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002053 // In case we've been unpaused by setPaused clearing mDoPause, need to
2054 // update internal pause state (capture/setRepeatingRequest unpause
2055 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002056 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002057 if (mPaused) {
2058 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2059 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2060 if (statusTracker != 0) {
2061 statusTracker->markComponentActive(mStatusId);
2062 }
2063 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064 mPaused = false;
2065
2066 // Check if we've reconfigured since last time, and reset the preview
2067 // request if so. Can't use 'NULL request == repeat' across configure calls.
2068 if (mReconfigured) {
2069 mPrevRequest.clear();
2070 mReconfigured = false;
2071 }
2072
2073 return nextRequest;
2074}
2075
2076bool Camera3Device::RequestThread::waitIfPaused() {
2077 status_t res;
2078 Mutex::Autolock l(mPauseLock);
2079 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002080 if (mPaused == false) {
2081 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002082 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2083 // Let the tracker know
2084 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2085 if (statusTracker != 0) {
2086 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2087 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002088 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002089
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002090 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002091 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002092 return true;
2093 }
2094 }
2095 // We don't set mPaused to false here, because waitForNextRequest needs
2096 // to further manage the paused state in case of starvation.
2097 return false;
2098}
2099
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002100void Camera3Device::RequestThread::unpauseForNewRequests() {
2101 // With work to do, mark thread as unpaused.
2102 // If paused by request (setPaused), don't resume, to avoid
2103 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002104 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002105 Mutex::Autolock p(mPauseLock);
2106 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002107 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2108 if (mPaused) {
2109 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2110 if (statusTracker != 0) {
2111 statusTracker->markComponentActive(mStatusId);
2112 }
2113 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002114 mPaused = false;
2115 }
2116}
2117
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002118void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2119 sp<Camera3Device> parent = mParent.promote();
2120 if (parent != NULL) {
2121 va_list args;
2122 va_start(args, fmt);
2123
2124 parent->setErrorStateV(fmt, args);
2125
2126 va_end(args);
2127 }
2128}
2129
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002130status_t Camera3Device::RequestThread::insertTriggers(
2131 const sp<CaptureRequest> &request) {
2132
2133 Mutex::Autolock al(mTriggerMutex);
2134
2135 CameraMetadata &metadata = request->mSettings;
2136 size_t count = mTriggerMap.size();
2137
2138 for (size_t i = 0; i < count; ++i) {
2139 RequestTrigger trigger = mTriggerMap.valueAt(i);
2140
2141 uint32_t tag = trigger.metadataTag;
2142 camera_metadata_entry entry = metadata.find(tag);
2143
2144 if (entry.count > 0) {
2145 /**
2146 * Already has an entry for this trigger in the request.
2147 * Rewrite it with our requested trigger value.
2148 */
2149 RequestTrigger oldTrigger = trigger;
2150
2151 oldTrigger.entryValue = entry.data.u8[0];
2152
2153 mTriggerReplacedMap.add(tag, oldTrigger);
2154 } else {
2155 /**
2156 * More typical, no trigger entry, so we just add it
2157 */
2158 mTriggerRemovedMap.add(tag, trigger);
2159 }
2160
2161 status_t res;
2162
2163 switch (trigger.getTagType()) {
2164 case TYPE_BYTE: {
2165 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2166 res = metadata.update(tag,
2167 &entryValue,
2168 /*count*/1);
2169 break;
2170 }
2171 case TYPE_INT32:
2172 res = metadata.update(tag,
2173 &trigger.entryValue,
2174 /*count*/1);
2175 break;
2176 default:
2177 ALOGE("%s: Type not supported: 0x%x",
2178 __FUNCTION__,
2179 trigger.getTagType());
2180 return INVALID_OPERATION;
2181 }
2182
2183 if (res != OK) {
2184 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2185 ", value %d", __FUNCTION__, trigger.getTagName(),
2186 trigger.entryValue);
2187 return res;
2188 }
2189
2190 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2191 trigger.getTagName(),
2192 trigger.entryValue);
2193 }
2194
2195 mTriggerMap.clear();
2196
2197 return count;
2198}
2199
2200status_t Camera3Device::RequestThread::removeTriggers(
2201 const sp<CaptureRequest> &request) {
2202 Mutex::Autolock al(mTriggerMutex);
2203
2204 CameraMetadata &metadata = request->mSettings;
2205
2206 /**
2207 * Replace all old entries with their old values.
2208 */
2209 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2210 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2211
2212 status_t res;
2213
2214 uint32_t tag = trigger.metadataTag;
2215 switch (trigger.getTagType()) {
2216 case TYPE_BYTE: {
2217 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2218 res = metadata.update(tag,
2219 &entryValue,
2220 /*count*/1);
2221 break;
2222 }
2223 case TYPE_INT32:
2224 res = metadata.update(tag,
2225 &trigger.entryValue,
2226 /*count*/1);
2227 break;
2228 default:
2229 ALOGE("%s: Type not supported: 0x%x",
2230 __FUNCTION__,
2231 trigger.getTagType());
2232 return INVALID_OPERATION;
2233 }
2234
2235 if (res != OK) {
2236 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2237 ", trigger value %d", __FUNCTION__,
2238 trigger.getTagName(), trigger.entryValue);
2239 return res;
2240 }
2241 }
2242 mTriggerReplacedMap.clear();
2243
2244 /**
2245 * Remove all new entries.
2246 */
2247 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2248 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2249 status_t res = metadata.erase(trigger.metadataTag);
2250
2251 if (res != OK) {
2252 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2253 ", trigger value %d", __FUNCTION__,
2254 trigger.getTagName(), trigger.entryValue);
2255 return res;
2256 }
2257 }
2258 mTriggerRemovedMap.clear();
2259
2260 return OK;
2261}
2262
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002263status_t Camera3Device::RequestThread::addDummyTriggerIds(
2264 const sp<CaptureRequest> &request) {
2265 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2266 static const int32_t dummyTriggerId = 1;
2267 status_t res;
2268
2269 CameraMetadata &metadata = request->mSettings;
2270
2271 // If AF trigger is active, insert a dummy AF trigger ID if none already
2272 // exists
2273 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2274 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2275 if (afTrigger.count > 0 &&
2276 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2277 afId.count == 0) {
2278 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2279 if (res != OK) return res;
2280 }
2281
2282 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2283 // if none already exists
2284 camera_metadata_entry pcTrigger =
2285 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2286 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2287 if (pcTrigger.count > 0 &&
2288 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2289 pcId.count == 0) {
2290 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2291 &dummyTriggerId, 1);
2292 if (res != OK) return res;
2293 }
2294
2295 return OK;
2296}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002297
2298
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002299/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002300 * Static callback forwarding methods from HAL to instance
2301 */
2302
2303void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2304 const camera3_capture_result *result) {
2305 Camera3Device *d =
2306 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2307 d->processCaptureResult(result);
2308}
2309
2310void Camera3Device::sNotify(const camera3_callback_ops *cb,
2311 const camera3_notify_msg *msg) {
2312 Camera3Device *d =
2313 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2314 d->notify(msg);
2315}
2316
2317}; // namespace android