blob: da3e1212fb78aaa5ce6a998097057722278ef9c2 [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040#include <utils/Log.h>
41#include <utils/Trace.h>
42#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070043
Igor Murashkinff3e31d2013-10-23 16:40:06 -070044#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045#include "device3/Camera3Device.h"
46#include "device3/Camera3OutputStream.h"
47#include "device3/Camera3InputStream.h"
48#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080049
50using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080051
52namespace android {
53
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054Camera3Device::Camera3Device(int id):
55 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080056 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070057 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070058 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070059 mNextResultFrameNumber(0),
60 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062{
63 ATRACE_CALL();
64 camera3_callback_ops::notify = &sNotify;
65 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
66 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
67}
68
69Camera3Device::~Camera3Device()
70{
71 ATRACE_CALL();
72 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
73 disconnect();
74}
75
Igor Murashkin71381052013-03-04 14:53:08 -080076int Camera3Device::getId() const {
77 return mId;
78}
79
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080080/**
81 * CameraDeviceBase interface
82 */
83
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084status_t Camera3Device::initialize(camera_module_t *module)
85{
86 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070087 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080088 Mutex::Autolock l(mLock);
89
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080091 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070092 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 return INVALID_OPERATION;
94 }
95
96 /** Open HAL device */
97
98 status_t res;
99 String8 deviceName = String8::format("%d", mId);
100
101 camera3_device_t *device;
102
Zhijun He213ce792013-11-19 08:45:15 -0800103 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104 res = module->common.methods->open(&module->common, deviceName.string(),
105 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800106 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800107
108 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700109 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 return res;
111 }
112
113 /** Cross-check device version */
114
115 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 device->common.version);
120 device->common.close(&device->common);
121 return BAD_VALUE;
122 }
123
124 camera_info info;
125 res = module->get_camera_info(mId, &info);
126 if (res != OK) return res;
127
128 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700129 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
130 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800131 device->common.version, info.device_version);
132 device->common.close(&device->common);
133 return BAD_VALUE;
134 }
135
136 /** Initialize device with callback functions */
137
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700138 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_END();
141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700143 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
144 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 device->common.close(&device->common);
146 return BAD_VALUE;
147 }
148
149 /** Get vendor metadata tags */
150
151 mVendorTagOps.get_camera_vendor_section_name = NULL;
152
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700153 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800154 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700155 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800156
157 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
158 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
159 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700160 SET_ERR_L("Unable to set tag ops: %s (%d)",
161 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 device->common.close(&device->common);
163 return res;
164 }
165 }
166
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700167 /** Start up status tracker thread */
168 mStatusTracker = new StatusTracker(this);
169 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
170 if (res != OK) {
171 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
172 strerror(-res), res);
173 device->common.close(&device->common);
174 mStatusTracker.clear();
175 return res;
176 }
177
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800178 /** Start up request queue thread */
179
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700180 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800181 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700183 SET_ERR_L("Unable to start request queue thread: %s (%d)",
184 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800185 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800186 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 return res;
188 }
189
190 /** Everything is good to go */
191
192 mDeviceInfo = info.static_camera_characteristics;
193 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700194 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800195 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700196 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700197 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700199 /** Check for quirks */
200
201 // Will the HAL be sending in early partial result metadata?
202 camera_metadata_entry partialResultsQuirk =
203 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
204 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
205 mUsePartialResultQuirk = true;
206 }
207
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208 return OK;
209}
210
211status_t Camera3Device::disconnect() {
212 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800214
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215 ALOGV("%s: E", __FUNCTION__);
216
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700217 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800218
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700219 {
220 Mutex::Autolock l(mLock);
221 if (mStatus == STATUS_UNINITIALIZED) return res;
222
223 if (mStatus == STATUS_ACTIVE ||
224 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
225 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700226 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700227 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700228 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700229 } else {
230 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
231 if (res != OK) {
232 SET_ERR_L("Timeout waiting for HAL to drain");
233 // Continue to close device even in case of error
234 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700235 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800236 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800237
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238 if (mStatus == STATUS_ERROR) {
239 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700240 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700241
242 if (mStatusTracker != NULL) {
243 mStatusTracker->requestExit();
244 }
245
246 if (mRequestThread != NULL) {
247 mRequestThread->requestExit();
248 }
249
250 mOutputStreams.clear();
251 mInputStream.clear();
252 }
253
254 // Joining done without holding mLock, otherwise deadlocks may ensue
255 // as the threads try to access parent state
256 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
257 // HAL may be in a bad state, so waiting for request thread
258 // (which may be stuck in the HAL processCaptureRequest call)
259 // could be dangerous.
260 mRequestThread->join();
261 }
262
263 if (mStatusTracker != NULL) {
264 mStatusTracker->join();
265 }
266
267 {
268 Mutex::Autolock l(mLock);
269
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800270 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800272
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800274 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800276 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 mHal3Device = NULL;
278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700280 mStatus = STATUS_UNINITIALIZED;
281 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800282
283 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700284 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800285}
286
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700287// For dumping/debugging only -
288// try to acquire a lock a few times, eventually give up to proceed with
289// debug/dump operations
290bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
291 bool gotLock = false;
292 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
293 if (lock.tryLock() == NO_ERROR) {
294 gotLock = true;
295 break;
296 } else {
297 usleep(kDumpSleepDuration);
298 }
299 }
300 return gotLock;
301}
302
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
304 ATRACE_CALL();
305 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700306
307 // Try to lock, but continue in case of failure (to avoid blocking in
308 // deadlocks)
309 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
310 bool gotLock = tryLockSpinRightRound(mLock);
311
312 ALOGW_IF(!gotInterfaceLock,
313 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
314 mId, __FUNCTION__);
315 ALOGW_IF(!gotLock,
316 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
317 mId, __FUNCTION__);
318
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800319 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800320
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800321 const char *status =
322 mStatus == STATUS_ERROR ? "ERROR" :
323 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700324 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
325 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 mStatus == STATUS_ACTIVE ? "ACTIVE" :
327 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700328
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800329 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700330 if (mStatus == STATUS_ERROR) {
331 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
332 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333 lines.appendFormat(" Stream configuration:\n");
334
335 if (mInputStream != NULL) {
336 write(fd, lines.string(), lines.size());
337 mInputStream->dump(fd, args);
338 } else {
339 lines.appendFormat(" No input stream.\n");
340 write(fd, lines.string(), lines.size());
341 }
342 for (size_t i = 0; i < mOutputStreams.size(); i++) {
343 mOutputStreams[i]->dump(fd,args);
344 }
345
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700346 lines = String8(" In-flight requests:\n");
347 if (mInFlightMap.size() == 0) {
348 lines.append(" None\n");
349 } else {
350 for (size_t i = 0; i < mInFlightMap.size(); i++) {
351 InFlightRequest r = mInFlightMap.valueAt(i);
352 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
353 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
354 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
355 r.numBuffersLeft);
356 }
357 }
358 write(fd, lines.string(), lines.size());
359
Igor Murashkin1e479c02013-09-06 16:55:14 -0700360 {
361 lines = String8(" Last request sent:\n");
362 write(fd, lines.string(), lines.size());
363
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700364 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700365 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
366 }
367
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800368 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700369 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800370 write(fd, lines.string(), lines.size());
371 mHal3Device->ops->dump(mHal3Device, fd);
372 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800373
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700374 if (gotLock) mLock.unlock();
375 if (gotInterfaceLock) mInterfaceLock.unlock();
376
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800377 return OK;
378}
379
380const CameraMetadata& Camera3Device::info() const {
381 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800382 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
383 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700384 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800385 mStatus == STATUS_ERROR ?
386 "when in error state" : "before init");
387 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800388 return mDeviceInfo;
389}
390
391status_t Camera3Device::capture(CameraMetadata &request) {
392 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700393 status_t res;
394 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800396
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700397 // TODO: take ownership of the request
398
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800399 switch (mStatus) {
400 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700401 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 return INVALID_OPERATION;
403 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700404 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700406 case STATUS_UNCONFIGURED:
407 // May be lazily configuring streams, will check during setup
408 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 case STATUS_ACTIVE:
410 // OK
411 break;
412 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700413 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800414 return INVALID_OPERATION;
415 }
416
417 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
418 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700419 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800420 return BAD_VALUE;
421 }
422
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700423 res = mRequestThread->queueRequest(newRequest);
424 if (res == OK) {
425 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
426 if (res != OK) {
427 SET_ERR_L("Can't transition to active in %f seconds!",
428 kActiveTimeout/1e9);
429 }
430 ALOGV("Camera %d: Capture request enqueued", mId);
431 }
432 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800433}
434
435
436status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
437 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700438 status_t res;
439 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800440 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800441
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800442 switch (mStatus) {
443 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700444 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 return INVALID_OPERATION;
446 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700447 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700449 case STATUS_UNCONFIGURED:
450 // May be lazily configuring streams, will check during setup
451 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800452 case STATUS_ACTIVE:
453 // OK
454 break;
455 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700456 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800457 return INVALID_OPERATION;
458 }
459
460 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
461 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700462 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800463 return BAD_VALUE;
464 }
465
466 RequestList newRepeatingRequests;
467 newRepeatingRequests.push_back(newRepeatingRequest);
468
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700469 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
470 if (res == OK) {
471 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
472 if (res != OK) {
473 SET_ERR_L("Can't transition to active in %f seconds!",
474 kActiveTimeout/1e9);
475 }
476 ALOGV("Camera %d: Repeating request set", mId);
477 }
478 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800479}
480
481
482sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
483 const CameraMetadata &request) {
484 status_t res;
485
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700486 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800487 res = configureStreamsLocked();
488 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700489 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800490 return NULL;
491 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700492 if (mStatus == STATUS_UNCONFIGURED) {
493 CLOGE("No streams configured");
494 return NULL;
495 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800496 }
497
498 sp<CaptureRequest> newRequest = createCaptureRequest(request);
499 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800500}
501
502status_t Camera3Device::clearStreamingRequest() {
503 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700504 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800505 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800506
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800507 switch (mStatus) {
508 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700509 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 return INVALID_OPERATION;
511 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700512 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800513 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700514 case STATUS_UNCONFIGURED:
515 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800516 case STATUS_ACTIVE:
517 // OK
518 break;
519 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700520 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800521 return INVALID_OPERATION;
522 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700523 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800524 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525}
526
527status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
528 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700529 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700531 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800532}
533
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700534status_t Camera3Device::createInputStream(
535 uint32_t width, uint32_t height, int format, int *id) {
536 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700537 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700538 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700539 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
540 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700541
542 status_t res;
543 bool wasActive = false;
544
545 switch (mStatus) {
546 case STATUS_ERROR:
547 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
548 return INVALID_OPERATION;
549 case STATUS_UNINITIALIZED:
550 ALOGE("%s: Device not initialized", __FUNCTION__);
551 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700552 case STATUS_UNCONFIGURED:
553 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700554 // OK
555 break;
556 case STATUS_ACTIVE:
557 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700558 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700559 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700560 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700561 return res;
562 }
563 wasActive = true;
564 break;
565 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700566 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700567 return INVALID_OPERATION;
568 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700569 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700570
571 if (mInputStream != 0) {
572 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
573 return INVALID_OPERATION;
574 }
575
576 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
577 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700578 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700579
580 mInputStream = newStream;
581
582 *id = mNextStreamId++;
583
584 // Continue captures if active at start
585 if (wasActive) {
586 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
587 res = configureStreamsLocked();
588 if (res != OK) {
589 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
590 __FUNCTION__, mNextStreamId, strerror(-res), res);
591 return res;
592 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700593 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700594 }
595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700596 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700597 return OK;
598}
599
Igor Murashkin2fba5842013-04-22 14:03:54 -0700600
601status_t Camera3Device::createZslStream(
602 uint32_t width, uint32_t height,
603 int depth,
604 /*out*/
605 int *id,
606 sp<Camera3ZslStream>* zslStream) {
607 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700609 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700610 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
611 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700612
613 status_t res;
614 bool wasActive = false;
615
616 switch (mStatus) {
617 case STATUS_ERROR:
618 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
619 return INVALID_OPERATION;
620 case STATUS_UNINITIALIZED:
621 ALOGE("%s: Device not initialized", __FUNCTION__);
622 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700623 case STATUS_UNCONFIGURED:
624 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700625 // OK
626 break;
627 case STATUS_ACTIVE:
628 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700630 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700631 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700632 return res;
633 }
634 wasActive = true;
635 break;
636 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700637 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700638 return INVALID_OPERATION;
639 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700640 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700641
642 if (mInputStream != 0) {
643 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
644 return INVALID_OPERATION;
645 }
646
647 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
648 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700649 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700650
651 res = mOutputStreams.add(mNextStreamId, newStream);
652 if (res < 0) {
653 ALOGE("%s: Can't add new stream to set: %s (%d)",
654 __FUNCTION__, strerror(-res), res);
655 return res;
656 }
657 mInputStream = newStream;
658
659 *id = mNextStreamId++;
660 *zslStream = newStream;
661
662 // Continue captures if active at start
663 if (wasActive) {
664 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
665 res = configureStreamsLocked();
666 if (res != OK) {
667 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
668 __FUNCTION__, mNextStreamId, strerror(-res), res);
669 return res;
670 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700671 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700672 }
673
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700674 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700675 return OK;
676}
677
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800678status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
679 uint32_t width, uint32_t height, int format, size_t size, int *id) {
680 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700681 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800682 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
684 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800685
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800686 status_t res;
687 bool wasActive = false;
688
689 switch (mStatus) {
690 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700691 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800692 return INVALID_OPERATION;
693 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700694 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700696 case STATUS_UNCONFIGURED:
697 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800698 // OK
699 break;
700 case STATUS_ACTIVE:
701 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700702 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800703 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800705 return res;
706 }
707 wasActive = true;
708 break;
709 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700710 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800711 return INVALID_OPERATION;
712 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700713 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714
715 sp<Camera3OutputStream> newStream;
716 if (format == HAL_PIXEL_FORMAT_BLOB) {
717 newStream = new Camera3OutputStream(mNextStreamId, consumer,
718 width, height, size, format);
719 } else {
720 newStream = new Camera3OutputStream(mNextStreamId, consumer,
721 width, height, format);
722 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724
725 res = mOutputStreams.add(mNextStreamId, newStream);
726 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700727 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728 return res;
729 }
730
731 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700732 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800733
734 // Continue captures if active at start
735 if (wasActive) {
736 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
737 res = configureStreamsLocked();
738 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700739 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
740 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800741 return res;
742 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700743 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800744 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800746 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800747}
748
749status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
750 ATRACE_CALL();
751 (void)outputId; (void)id;
752
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700753 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800754 return INVALID_OPERATION;
755}
756
757
758status_t Camera3Device::getStreamInfo(int id,
759 uint32_t *width, uint32_t *height, uint32_t *format) {
760 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700761 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800762 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800763
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 switch (mStatus) {
765 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 return INVALID_OPERATION;
768 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700769 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700771 case STATUS_UNCONFIGURED:
772 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800773 case STATUS_ACTIVE:
774 // OK
775 break;
776 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700777 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800778 return INVALID_OPERATION;
779 }
780
781 ssize_t idx = mOutputStreams.indexOfKey(id);
782 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700783 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800784 return idx;
785 }
786
787 if (width) *width = mOutputStreams[idx]->getWidth();
788 if (height) *height = mOutputStreams[idx]->getHeight();
789 if (format) *format = mOutputStreams[idx]->getFormat();
790
791 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800792}
793
794status_t Camera3Device::setStreamTransform(int id,
795 int transform) {
796 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700797 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800799
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 switch (mStatus) {
801 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
804 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700805 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700807 case STATUS_UNCONFIGURED:
808 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 case STATUS_ACTIVE:
810 // OK
811 break;
812 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700813 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 return INVALID_OPERATION;
815 }
816
817 ssize_t idx = mOutputStreams.indexOfKey(id);
818 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700819 CLOGE("Stream %d does not exist",
820 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800821 return BAD_VALUE;
822 }
823
824 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800825}
826
827status_t Camera3Device::deleteStream(int id) {
828 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700829 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 Mutex::Autolock l(mLock);
831 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800832
Igor Murashkine2172be2013-05-28 15:31:39 -0700833 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
834
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800835 // CameraDevice semantics require device to already be idle before
836 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700837 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700838 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
839 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 }
841
Igor Murashkin2fba5842013-04-22 14:03:54 -0700842 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800843 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844 if (mInputStream != NULL && id == mInputStream->getId()) {
845 deletedStream = mInputStream;
846 mInputStream.clear();
847 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800848 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700849 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 return BAD_VALUE;
851 }
Zhijun He5f446352014-01-22 09:49:33 -0800852 }
853
854 // Delete output stream or the output part of a bi-directional stream.
855 if (outputStreamIdx != NAME_NOT_FOUND) {
856 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857 mOutputStreams.removeItem(id);
858 }
859
860 // Free up the stream endpoint so that it can be used by some other stream
861 res = deletedStream->disconnect();
862 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700863 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800864 // fall through since we want to still list the stream as deleted.
865 }
866 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700867 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868
869 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800870}
871
872status_t Camera3Device::deleteReprocessStream(int id) {
873 ATRACE_CALL();
874 (void)id;
875
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700876 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800877 return INVALID_OPERATION;
878}
879
880
881status_t Camera3Device::createDefaultRequest(int templateId,
882 CameraMetadata *request) {
883 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700884 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700885 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 Mutex::Autolock l(mLock);
887
888 switch (mStatus) {
889 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700890 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800891 return INVALID_OPERATION;
892 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700893 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800894 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 case STATUS_UNCONFIGURED:
896 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 case STATUS_ACTIVE:
898 // OK
899 break;
900 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700901 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800902 return INVALID_OPERATION;
903 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800904
905 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700906 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800907 rawRequest = mHal3Device->ops->construct_default_request_settings(
908 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700909 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 if (rawRequest == NULL) {
911 SET_ERR_L("HAL is unable to construct default settings for template %d",
912 templateId);
913 return DEAD_OBJECT;
914 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915 *request = rawRequest;
916
917 return OK;
918}
919
920status_t Camera3Device::waitUntilDrained() {
921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 switch (mStatus) {
926 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700927 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 ALOGV("%s: Already idle", __FUNCTION__);
929 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 case STATUS_CONFIGURED:
931 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 case STATUS_ERROR:
933 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700934 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800935 break;
936 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700937 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800938 return INVALID_OPERATION;
939 }
940
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
942 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
943 return res;
944}
945
946// Pause to reconfigure
947status_t Camera3Device::internalPauseAndWaitLocked() {
948 mRequestThread->setPaused(true);
949 mPauseStateNotify = true;
950
951 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
952 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
953 if (res != OK) {
954 SET_ERR_L("Can't idle device in %f seconds!",
955 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800956 }
957
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700958 return res;
959}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800960
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700961// Resume after internalPauseAndWaitLocked
962status_t Camera3Device::internalResumeLocked() {
963 status_t res;
964
965 mRequestThread->setPaused(false);
966
967 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
968 if (res != OK) {
969 SET_ERR_L("Can't transition to active in %f seconds!",
970 kActiveTimeout/1e9);
971 }
972 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800974}
975
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700976status_t Camera3Device::waitUntilStateThenRelock(bool active,
977 nsecs_t timeout) {
978 status_t res = OK;
979 if (active == (mStatus == STATUS_ACTIVE)) {
980 // Desired state already reached
981 return res;
982 }
983
984 bool stateSeen = false;
985 do {
986 mRecentStatusUpdates.clear();
987
988 res = mStatusChanged.waitRelative(mLock, timeout);
989 if (res != OK) break;
990
991 // Check state change history during wait
992 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
993 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
994 stateSeen = true;
995 break;
996 }
997 }
998 } while (!stateSeen);
999
1000 return res;
1001}
1002
1003
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1005 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001006 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001007
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001008 if (listener != NULL && mListener != NULL) {
1009 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1010 }
1011 mListener = listener;
1012
1013 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014}
1015
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001016bool Camera3Device::willNotify3A() {
1017 return false;
1018}
1019
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001020status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001021 status_t res;
1022 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001023
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001024 while (mResultQueue.empty()) {
1025 res = mResultSignal.waitRelative(mOutputLock, timeout);
1026 if (res == TIMED_OUT) {
1027 return res;
1028 } else if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001029 ALOGW("%s: Camera %d: No frame in %lld ns: %s (%d)",
1030 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001031 return res;
1032 }
1033 }
1034 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001035}
1036
1037status_t Camera3Device::getNextFrame(CameraMetadata *frame) {
1038 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001039 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001040
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001041 if (mResultQueue.empty()) {
1042 return NOT_ENOUGH_DATA;
1043 }
1044
1045 CameraMetadata &result = *(mResultQueue.begin());
1046 frame->acquire(result);
1047 mResultQueue.erase(mResultQueue.begin());
1048
1049 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001050}
1051
1052status_t Camera3Device::triggerAutofocus(uint32_t id) {
1053 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001054 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001055
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001056 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1057 // Mix-in this trigger into the next request and only the next request.
1058 RequestTrigger trigger[] = {
1059 {
1060 ANDROID_CONTROL_AF_TRIGGER,
1061 ANDROID_CONTROL_AF_TRIGGER_START
1062 },
1063 {
1064 ANDROID_CONTROL_AF_TRIGGER_ID,
1065 static_cast<int32_t>(id)
1066 },
1067 };
1068
1069 return mRequestThread->queueTrigger(trigger,
1070 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071}
1072
1073status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1074 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001075 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001076
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001077 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1078 // Mix-in this trigger into the next request and only the next request.
1079 RequestTrigger trigger[] = {
1080 {
1081 ANDROID_CONTROL_AF_TRIGGER,
1082 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1083 },
1084 {
1085 ANDROID_CONTROL_AF_TRIGGER_ID,
1086 static_cast<int32_t>(id)
1087 },
1088 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001089
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001090 return mRequestThread->queueTrigger(trigger,
1091 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001092}
1093
1094status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1095 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001096 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001097
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001098 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1099 // Mix-in this trigger into the next request and only the next request.
1100 RequestTrigger trigger[] = {
1101 {
1102 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1103 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1104 },
1105 {
1106 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1107 static_cast<int32_t>(id)
1108 },
1109 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001110
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001111 return mRequestThread->queueTrigger(trigger,
1112 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001113}
1114
1115status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1116 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1117 ATRACE_CALL();
1118 (void)reprocessStreamId; (void)buffer; (void)listener;
1119
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001120 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001121 return INVALID_OPERATION;
1122}
1123
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001124status_t Camera3Device::flush() {
1125 ATRACE_CALL();
1126 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001127 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001128 Mutex::Autolock l(mLock);
1129
1130 mRequestThread->clear();
Zhijun He491e3412013-12-27 10:57:44 -08001131 status_t res;
1132 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1133 res = mHal3Device->ops->flush(mHal3Device);
1134 } else {
1135 res = waitUntilDrained();
1136 }
1137
1138 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001139}
1140
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001141/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001142 * Methods called by subclasses
1143 */
1144
1145void Camera3Device::notifyStatus(bool idle) {
1146 {
1147 // Need mLock to safely update state and synchronize to current
1148 // state of methods in flight.
1149 Mutex::Autolock l(mLock);
1150 // We can get various system-idle notices from the status tracker
1151 // while starting up. Only care about them if we've actually sent
1152 // in some requests recently.
1153 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1154 return;
1155 }
1156 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1157 idle ? "idle" : "active");
1158 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1159 mRecentStatusUpdates.add(mStatus);
1160 mStatusChanged.signal();
1161
1162 // Skip notifying listener if we're doing some user-transparent
1163 // state changes
1164 if (mPauseStateNotify) return;
1165 }
1166 NotificationListener *listener;
1167 {
1168 Mutex::Autolock l(mOutputLock);
1169 listener = mListener;
1170 }
1171 if (idle && listener != NULL) {
1172 listener->notifyIdle();
1173 }
1174}
1175
1176/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001177 * Camera3Device private methods
1178 */
1179
1180sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1181 const CameraMetadata &request) {
1182 ATRACE_CALL();
1183 status_t res;
1184
1185 sp<CaptureRequest> newRequest = new CaptureRequest;
1186 newRequest->mSettings = request;
1187
1188 camera_metadata_entry_t inputStreams =
1189 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1190 if (inputStreams.count > 0) {
1191 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001192 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001193 CLOGE("Request references unknown input stream %d",
1194 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001195 return NULL;
1196 }
1197 // Lazy completion of stream configuration (allocation/registration)
1198 // on first use
1199 if (mInputStream->isConfiguring()) {
1200 res = mInputStream->finishConfiguration(mHal3Device);
1201 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001202 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001203 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001204 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001205 return NULL;
1206 }
1207 }
1208
1209 newRequest->mInputStream = mInputStream;
1210 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1211 }
1212
1213 camera_metadata_entry_t streams =
1214 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1215 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001217 return NULL;
1218 }
1219
1220 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001221 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001223 CLOGE("Request references unknown stream %d",
1224 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 return NULL;
1226 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001227 sp<Camera3OutputStreamInterface> stream =
1228 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229
1230 // Lazy completion of stream configuration (allocation/registration)
1231 // on first use
1232 if (stream->isConfiguring()) {
1233 res = stream->finishConfiguration(mHal3Device);
1234 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001235 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1236 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001237 return NULL;
1238 }
1239 }
1240
1241 newRequest->mOutputStreams.push(stream);
1242 }
1243 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1244
1245 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246}
1247
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001248status_t Camera3Device::configureStreamsLocked() {
1249 ATRACE_CALL();
1250 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001253 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 return INVALID_OPERATION;
1255 }
1256
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001257 if (!mNeedConfig) {
1258 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1259 return OK;
1260 }
1261
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001263 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264
1265 camera3_stream_configuration config;
1266
1267 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1268
1269 Vector<camera3_stream_t*> streams;
1270 streams.setCapacity(config.num_streams);
1271
1272 if (mInputStream != NULL) {
1273 camera3_stream_t *inputStream;
1274 inputStream = mInputStream->startConfiguration();
1275 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001276 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001277 return INVALID_OPERATION;
1278 }
1279 streams.add(inputStream);
1280 }
1281
1282 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001283
1284 // Don't configure bidi streams twice, nor add them twice to the list
1285 if (mOutputStreams[i].get() ==
1286 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1287
1288 config.num_streams--;
1289 continue;
1290 }
1291
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 camera3_stream_t *outputStream;
1293 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1294 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001295 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001296 return INVALID_OPERATION;
1297 }
1298 streams.add(outputStream);
1299 }
1300
1301 config.streams = streams.editArray();
1302
1303 // Do the HAL configuration; will potentially touch stream
1304 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001305 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001307 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001308
1309 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001310 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1311 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001312 return res;
1313 }
1314
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001315 // Finish all stream configuration immediately.
1316 // TODO: Try to relax this later back to lazy completion, which should be
1317 // faster
1318
Igor Murashkin073f8572013-05-02 14:59:28 -07001319 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001320 res = mInputStream->finishConfiguration(mHal3Device);
1321 if (res != OK) {
1322 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1323 mInputStream->getId(), strerror(-res), res);
1324 return res;
1325 }
1326 }
1327
1328 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001329 sp<Camera3OutputStreamInterface> outputStream =
1330 mOutputStreams.editValueAt(i);
1331 if (outputStream->isConfiguring()) {
1332 res = outputStream->finishConfiguration(mHal3Device);
1333 if (res != OK) {
1334 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1335 outputStream->getId(), strerror(-res), res);
1336 return res;
1337 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001338 }
1339 }
1340
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341 // Request thread needs to know to avoid using repeat-last-settings protocol
1342 // across configure_streams() calls
1343 mRequestThread->configurationComplete();
1344
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001345 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001346
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001347 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001348
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001349 if (config.num_streams > 0) {
1350 mStatus = STATUS_CONFIGURED;
1351 } else {
1352 mStatus = STATUS_UNCONFIGURED;
1353 }
1354
1355 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1356
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001357 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001358}
1359
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001360void Camera3Device::setErrorState(const char *fmt, ...) {
1361 Mutex::Autolock l(mLock);
1362 va_list args;
1363 va_start(args, fmt);
1364
1365 setErrorStateLockedV(fmt, args);
1366
1367 va_end(args);
1368}
1369
1370void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1371 Mutex::Autolock l(mLock);
1372 setErrorStateLockedV(fmt, args);
1373}
1374
1375void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1376 va_list args;
1377 va_start(args, fmt);
1378
1379 setErrorStateLockedV(fmt, args);
1380
1381 va_end(args);
1382}
1383
1384void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001385 // Print out all error messages to log
1386 String8 errorCause = String8::formatV(fmt, args);
1387 ALOGE("Camera %d: %s", mId, errorCause.string());
1388
1389 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001390 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001391
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001392 // Save stack trace. View by dumping it later.
1393 CameraTraces::saveTrace();
1394 // TODO: consider adding errorCause and client pid/procname
1395
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001396 mErrorCause = errorCause;
1397
1398 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001399 mStatus = STATUS_ERROR;
1400}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001401
1402/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001403 * In-flight request management
1404 */
1405
1406status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001407 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001408 ATRACE_CALL();
1409 Mutex::Autolock l(mInFlightLock);
1410
1411 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001412 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001413 if (res < 0) return res;
1414
1415 return OK;
1416}
1417
1418/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001419 * QUIRK(partial results)
1420 * Check if all 3A fields are ready, and send off a partial 3A-only result
1421 * to the output frame queue
1422 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001423bool Camera3Device::processPartial3AQuirk(
1424 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001425 const CameraMetadata& partial) {
1426
1427 // Check if all 3A states are present
1428 // The full list of fields is
1429 // android.control.afMode
1430 // android.control.awbMode
1431 // android.control.aeState
1432 // android.control.awbState
1433 // android.control.afState
1434 // android.control.afTriggerID
1435 // android.control.aePrecaptureID
1436 // TODO: Add android.control.aeMode
1437
1438 bool gotAllStates = true;
1439
1440 uint8_t afMode;
1441 uint8_t awbMode;
1442 uint8_t aeState;
1443 uint8_t afState;
1444 uint8_t awbState;
1445 int32_t afTriggerId;
1446 int32_t aeTriggerId;
1447
1448 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1449 &afMode, frameNumber);
1450
1451 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1452 &awbMode, frameNumber);
1453
1454 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1455 &aeState, frameNumber);
1456
1457 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1458 &afState, frameNumber);
1459
1460 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1461 &awbState, frameNumber);
1462
1463 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1464 &afTriggerId, frameNumber);
1465
1466 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1467 &aeTriggerId, frameNumber);
1468
1469 if (!gotAllStates) return false;
1470
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001471 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001472 "AF state %d, AE state %d, AWB state %d, "
1473 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001474 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001475 afMode, awbMode,
1476 afState, aeState, awbState,
1477 afTriggerId, aeTriggerId);
1478
1479 // Got all states, so construct a minimal result to send
1480 // In addition to the above fields, this means adding in
1481 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001482 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001483 // android.quirks.partialResult
1484
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001485 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001486
1487 Mutex::Autolock l(mOutputLock);
1488
1489 CameraMetadata& min3AResult =
1490 *mResultQueue.insert(
1491 mResultQueue.end(),
1492 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1493
1494 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1495 &frameNumber, frameNumber)) {
1496 return false;
1497 }
1498
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001499 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1500 &requestId, frameNumber)) {
1501 return false;
1502 }
1503
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001504 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1505 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1506 &partialResult, frameNumber)) {
1507 return false;
1508 }
1509
1510 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1511 &afMode, frameNumber)) {
1512 return false;
1513 }
1514
1515 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1516 &awbMode, frameNumber)) {
1517 return false;
1518 }
1519
1520 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1521 &aeState, frameNumber)) {
1522 return false;
1523 }
1524
1525 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1526 &afState, frameNumber)) {
1527 return false;
1528 }
1529
1530 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1531 &awbState, frameNumber)) {
1532 return false;
1533 }
1534
1535 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1536 &afTriggerId, frameNumber)) {
1537 return false;
1538 }
1539
1540 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1541 &aeTriggerId, frameNumber)) {
1542 return false;
1543 }
1544
1545 mResultSignal.signal();
1546
1547 return true;
1548}
1549
1550template<typename T>
1551bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1552 T* value, int32_t frameNumber) {
1553 (void) frameNumber;
1554
1555 camera_metadata_ro_entry_t entry;
1556
1557 entry = result.find(tag);
1558 if (entry.count == 0) {
1559 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1560 mId, frameNumber, get_camera_metadata_tag_name(tag));
1561 return false;
1562 }
1563
1564 if (sizeof(T) == sizeof(uint8_t)) {
1565 *value = entry.data.u8[0];
1566 } else if (sizeof(T) == sizeof(int32_t)) {
1567 *value = entry.data.i32[0];
1568 } else {
1569 ALOGE("%s: Unexpected type", __FUNCTION__);
1570 return false;
1571 }
1572 return true;
1573}
1574
1575template<typename T>
1576bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1577 const T* value, int32_t frameNumber) {
1578 if (result.update(tag, value, 1) != NO_ERROR) {
1579 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1580 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1581 frameNumber, get_camera_metadata_tag_name(tag));
1582 return false;
1583 }
1584 return true;
1585}
1586
1587/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001588 * Camera HAL device callback methods
1589 */
1590
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001592 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001593
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001594 status_t res;
1595
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001596 uint32_t frameNumber = result->frame_number;
1597 if (result->result == NULL && result->num_output_buffers == 0) {
1598 SET_ERR("No result data provided by HAL for frame %d",
1599 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001600 return;
1601 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001602 bool partialResultQuirk = false;
1603 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001604
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001605 // Get capture timestamp from list of in-flight requests, where it was added
1606 // by the shutter notification for this frame. Then update the in-flight
1607 // status and remove the in-flight entry if all result data has been
1608 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001609 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001610 {
1611 Mutex::Autolock l(mInFlightLock);
1612 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1613 if (idx == NAME_NOT_FOUND) {
1614 SET_ERR("Unknown frame number for capture result: %d",
1615 frameNumber);
1616 return;
1617 }
1618 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001619
1620 // Check if this result carries only partial metadata
1621 if (mUsePartialResultQuirk && result->result != NULL) {
1622 camera_metadata_ro_entry_t partialResultEntry;
1623 res = find_camera_metadata_ro_entry(result->result,
1624 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1625 if (res != NAME_NOT_FOUND &&
1626 partialResultEntry.count > 0 &&
1627 partialResultEntry.data.u8[0] ==
1628 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1629 // A partial result. Flag this as such, and collect this
1630 // set of metadata into the in-flight entry.
1631 partialResultQuirk = true;
1632 request.partialResultQuirk.collectedResult.append(
1633 result->result);
1634 request.partialResultQuirk.collectedResult.erase(
1635 ANDROID_QUIRKS_PARTIAL_RESULT);
1636 // Fire off a 3A-only result if possible
1637 if (!request.partialResultQuirk.haveSent3A) {
1638 request.partialResultQuirk.haveSent3A =
1639 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001640 request.requestId,
1641 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001642 }
1643 }
1644 }
1645
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001646 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001647 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001648 * One of the following must happen before it's legal to call process_capture_result,
1649 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001650 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1651 * - CAMERA3_MSG_ERROR (expected during flush)
1652 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001653 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001654 SET_ERR("Called before shutter notify for frame %d",
1655 frameNumber);
1656 return;
1657 }
1658
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001659 // Did we get the (final) result metadata for this capture?
1660 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001661 if (request.haveResultMetadata) {
1662 SET_ERR("Called multiple times with metadata for frame %d",
1663 frameNumber);
1664 return;
1665 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001666 if (mUsePartialResultQuirk &&
1667 !request.partialResultQuirk.collectedResult.isEmpty()) {
1668 collectedQuirkResult.acquire(
1669 request.partialResultQuirk.collectedResult);
1670 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001671 request.haveResultMetadata = true;
1672 }
1673
1674 request.numBuffersLeft -= result->num_output_buffers;
1675
1676 if (request.numBuffersLeft < 0) {
1677 SET_ERR("Too many buffers returned for frame %d",
1678 frameNumber);
1679 return;
1680 }
1681
Zhijun He1b05dfc2013-11-21 12:57:51 -08001682 // Check if everything has arrived for this result (buffers and metadata), remove it from
1683 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1684 if ((request.requestStatus != OK) ||
1685 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001686 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001687 mInFlightMap.removeItemsAt(idx, 1);
1688 }
1689
1690 // Sanity check - if we have too many in-flight frames, something has
1691 // likely gone wrong
1692 if (mInFlightMap.size() > kInFlightWarnLimit) {
1693 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1694 }
1695
1696 }
1697
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001698 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001699 bool gotResult = false;
1700 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001701 Mutex::Autolock l(mOutputLock);
1702
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001703 gotResult = true;
1704
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001705 if (frameNumber != mNextResultFrameNumber) {
1706 SET_ERR("Out-of-order capture result metadata submitted! "
1707 "(got frame number %d, expecting %d)",
1708 frameNumber, mNextResultFrameNumber);
1709 return;
1710 }
1711 mNextResultFrameNumber++;
1712
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001713 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001714 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001715
Igor Murashkind2c90692013-04-02 12:32:32 -07001716 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001717 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001718 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001719 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001720 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001721 } else {
1722 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001723 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001724 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001725
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001726 // Append any previous partials to form a complete result
1727 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1728 captureResult.append(collectedQuirkResult);
1729 }
1730
1731 captureResult.sort();
1732
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001733 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001734
1735 camera_metadata_entry entry =
1736 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1737 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001738 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001739 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001740 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001741 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001742 SET_ERR("Timestamp mismatch between shutter notify and result"
1743 " metadata for frame %d (%lld vs %lld respectively)",
1744 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001745 gotResult = false;
1746 }
1747
1748 if (gotResult) {
1749 // Valid result, insert into queue
1750 CameraMetadata& queuedResult =
1751 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1752 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001753 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001754 } // scope for mOutputLock
1755
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001756 // Return completed buffers to their streams with the timestamp
1757
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001758 for (size_t i = 0; i < result->num_output_buffers; i++) {
1759 Camera3Stream *stream =
1760 Camera3Stream::cast(result->output_buffers[i].stream);
1761 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1762 // Note: stream may be deallocated at this point, if this buffer was the
1763 // last reference to it.
1764 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001765 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001766 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001767 }
1768 }
1769
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001770 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001771
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001772 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001773 mResultSignal.signal();
1774 }
1775
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001776}
1777
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001778
1779
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001780void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001781 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001782 NotificationListener *listener;
1783 {
1784 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001785 listener = mListener;
1786 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001787
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001788 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001789 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001790 return;
1791 }
1792
1793 switch (msg->type) {
1794 case CAMERA3_MSG_ERROR: {
1795 int streamId = 0;
1796 if (msg->message.error.error_stream != NULL) {
1797 Camera3Stream *stream =
1798 Camera3Stream::cast(
1799 msg->message.error.error_stream);
1800 streamId = stream->getId();
1801 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001802 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1803 mId, __FUNCTION__, msg->message.error.frame_number,
1804 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001805
1806 // Set request error status for the request in the in-flight tracking
1807 {
1808 Mutex::Autolock l(mInFlightLock);
1809 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1810 if (idx >= 0) {
1811 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1812 }
1813 }
1814
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001815 if (listener != NULL) {
1816 listener->notifyError(msg->message.error.error_code,
1817 msg->message.error.frame_number, streamId);
1818 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001819 break;
1820 }
1821 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001822 ssize_t idx;
1823 uint32_t frameNumber = msg->message.shutter.frame_number;
1824 nsecs_t timestamp = msg->message.shutter.timestamp;
1825 // Verify ordering of shutter notifications
1826 {
1827 Mutex::Autolock l(mOutputLock);
1828 if (frameNumber != mNextShutterFrameNumber) {
1829 SET_ERR("Shutter notification out-of-order. Expected "
1830 "notification for frame %d, got frame %d",
1831 mNextShutterFrameNumber, frameNumber);
1832 break;
1833 }
1834 mNextShutterFrameNumber++;
1835 }
1836
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001837 int32_t requestId = -1;
1838
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001839 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001840 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001841 {
1842 Mutex::Autolock l(mInFlightLock);
1843 idx = mInFlightMap.indexOfKey(frameNumber);
1844 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001845 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1846 r.captureTimestamp = timestamp;
1847 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001848 }
1849 }
1850 if (idx < 0) {
1851 SET_ERR("Shutter notification for non-existent frame number %d",
1852 frameNumber);
1853 break;
1854 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001855 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1856 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001857 // Call listener, if any
1858 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001859 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001860 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001861 break;
1862 }
1863 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001864 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001865 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001866 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001867}
1868
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001869CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001870 ALOGV("%s", __FUNCTION__);
1871
Igor Murashkin1e479c02013-09-06 16:55:14 -07001872 CameraMetadata retVal;
1873
1874 if (mRequestThread != NULL) {
1875 retVal = mRequestThread->getLatestRequest();
1876 }
1877
Igor Murashkin1e479c02013-09-06 16:55:14 -07001878 return retVal;
1879}
1880
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001881/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882 * RequestThread inner class methods
1883 */
1884
1885Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001887 camera3_device_t *hal3Device) :
1888 Thread(false),
1889 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001890 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001891 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001892 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001893 mReconfigured(false),
1894 mDoPause(false),
1895 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001896 mFrameNumber(0),
1897 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001898 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001899}
1900
1901void Camera3Device::RequestThread::configurationComplete() {
1902 Mutex::Autolock l(mRequestLock);
1903 mReconfigured = true;
1904}
1905
1906status_t Camera3Device::RequestThread::queueRequest(
1907 sp<CaptureRequest> request) {
1908 Mutex::Autolock l(mRequestLock);
1909 mRequestQueue.push_back(request);
1910
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001911 unpauseForNewRequests();
1912
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001913 return OK;
1914}
1915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001916
1917status_t Camera3Device::RequestThread::queueTrigger(
1918 RequestTrigger trigger[],
1919 size_t count) {
1920
1921 Mutex::Autolock l(mTriggerMutex);
1922 status_t ret;
1923
1924 for (size_t i = 0; i < count; ++i) {
1925 ret = queueTriggerLocked(trigger[i]);
1926
1927 if (ret != OK) {
1928 return ret;
1929 }
1930 }
1931
1932 return OK;
1933}
1934
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001935int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1936 sp<Camera3Device> d = device.promote();
1937 if (d != NULL) return d->mId;
1938 return 0;
1939}
1940
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001941status_t Camera3Device::RequestThread::queueTriggerLocked(
1942 RequestTrigger trigger) {
1943
1944 uint32_t tag = trigger.metadataTag;
1945 ssize_t index = mTriggerMap.indexOfKey(tag);
1946
1947 switch (trigger.getTagType()) {
1948 case TYPE_BYTE:
1949 // fall-through
1950 case TYPE_INT32:
1951 break;
1952 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001953 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1954 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001955 return INVALID_OPERATION;
1956 }
1957
1958 /**
1959 * Collect only the latest trigger, since we only have 1 field
1960 * in the request settings per trigger tag, and can't send more than 1
1961 * trigger per request.
1962 */
1963 if (index != NAME_NOT_FOUND) {
1964 mTriggerMap.editValueAt(index) = trigger;
1965 } else {
1966 mTriggerMap.add(tag, trigger);
1967 }
1968
1969 return OK;
1970}
1971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001972status_t Camera3Device::RequestThread::setRepeatingRequests(
1973 const RequestList &requests) {
1974 Mutex::Autolock l(mRequestLock);
1975 mRepeatingRequests.clear();
1976 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1977 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001978
1979 unpauseForNewRequests();
1980
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001981 return OK;
1982}
1983
1984status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1985 Mutex::Autolock l(mRequestLock);
1986 mRepeatingRequests.clear();
1987 return OK;
1988}
1989
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001990status_t Camera3Device::RequestThread::clear() {
1991 Mutex::Autolock l(mRequestLock);
1992 mRepeatingRequests.clear();
1993 mRequestQueue.clear();
1994 mTriggerMap.clear();
1995 return OK;
1996}
1997
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001998void Camera3Device::RequestThread::setPaused(bool paused) {
1999 Mutex::Autolock l(mPauseLock);
2000 mDoPause = paused;
2001 mDoPauseSignal.signal();
2002}
2003
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002004status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2005 int32_t requestId, nsecs_t timeout) {
2006 Mutex::Autolock l(mLatestRequestMutex);
2007 status_t res;
2008 while (mLatestRequestId != requestId) {
2009 nsecs_t startTime = systemTime();
2010
2011 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2012 if (res != OK) return res;
2013
2014 timeout -= (systemTime() - startTime);
2015 }
2016
2017 return OK;
2018}
2019
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002020void Camera3Device::RequestThread::requestExit() {
2021 // Call parent to set up shutdown
2022 Thread::requestExit();
2023 // The exit from any possible waits
2024 mDoPauseSignal.signal();
2025 mRequestSignal.signal();
2026}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002027
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002028bool Camera3Device::RequestThread::threadLoop() {
2029
2030 status_t res;
2031
2032 // Handle paused state.
2033 if (waitIfPaused()) {
2034 return true;
2035 }
2036
2037 // Get work to do
2038
2039 sp<CaptureRequest> nextRequest = waitForNextRequest();
2040 if (nextRequest == NULL) {
2041 return true;
2042 }
2043
2044 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002045 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002046 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002047
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002048 // Get the request ID, if any
2049 int requestId;
2050 camera_metadata_entry_t requestIdEntry =
2051 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2052 if (requestIdEntry.count > 0) {
2053 requestId = requestIdEntry.data.i32[0];
2054 } else {
2055 ALOGW("%s: Did not have android.request.id set in the request",
2056 __FUNCTION__);
2057 requestId = NAME_NOT_FOUND;
2058 }
2059
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002060 // Insert any queued triggers (before metadata is locked)
2061 int32_t triggerCount;
2062 res = insertTriggers(nextRequest);
2063 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002064 SET_ERR("RequestThread: Unable to insert triggers "
2065 "(capture request %d, HAL device: %s (%d)",
2066 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002067 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2068 return false;
2069 }
2070 triggerCount = res;
2071
2072 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2073
2074 // If the request is the same as last, or we had triggers last time
2075 if (mPrevRequest != nextRequest || triggersMixedIn) {
2076 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002077 * HAL workaround:
2078 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2079 */
2080 res = addDummyTriggerIds(nextRequest);
2081 if (res != OK) {
2082 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2083 "(capture request %d, HAL device: %s (%d)",
2084 (mFrameNumber+1), strerror(-res), res);
2085 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2086 return false;
2087 }
2088
2089 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002090 * The request should be presorted so accesses in HAL
2091 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2092 */
2093 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002094 request.settings = nextRequest->mSettings.getAndLock();
2095 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002096 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2097
2098 IF_ALOGV() {
2099 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2100 find_camera_metadata_ro_entry(
2101 request.settings,
2102 ANDROID_CONTROL_AF_TRIGGER,
2103 &e
2104 );
2105 if (e.count > 0) {
2106 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2107 __FUNCTION__,
2108 mFrameNumber+1,
2109 e.data.u8[0]);
2110 }
2111 }
2112 } else {
2113 // leave request.settings NULL to indicate 'reuse latest given'
2114 ALOGVV("%s: Request settings are REUSED",
2115 __FUNCTION__);
2116 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002117
2118 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002119
2120 // Fill in buffers
2121
2122 if (nextRequest->mInputStream != NULL) {
2123 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002124 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002125 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002126 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002127 " %s (%d)", strerror(-res), res);
2128 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2129 return true;
2130 }
2131 } else {
2132 request.input_buffer = NULL;
2133 }
2134
2135 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2136 nextRequest->mOutputStreams.size());
2137 request.output_buffers = outputBuffers.array();
2138 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2139 res = nextRequest->mOutputStreams.editItemAt(i)->
2140 getBuffer(&outputBuffers.editItemAt(i));
2141 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002142 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2143 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002144 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2145 return true;
2146 }
2147 request.num_output_buffers++;
2148 }
2149
2150 request.frame_number = mFrameNumber++;
2151
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002152 // Log request in the in-flight queue
2153 sp<Camera3Device> parent = mParent.promote();
2154 if (parent == NULL) {
2155 CLOGE("RequestThread: Parent is gone");
2156 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2157 return false;
2158 }
2159
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002160 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002161 request.num_output_buffers);
2162 if (res != OK) {
2163 SET_ERR("RequestThread: Unable to register new in-flight request:"
2164 " %s (%d)", strerror(-res), res);
2165 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2166 return false;
2167 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002168
Zhijun Hecc27e112013-10-03 16:12:43 -07002169 // Inform waitUntilRequestProcessed thread of a new request ID
2170 {
2171 Mutex::Autolock al(mLatestRequestMutex);
2172
2173 mLatestRequestId = requestId;
2174 mLatestRequestSignal.signal();
2175 }
2176
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002177 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002178 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2179 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002180 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002181 ATRACE_END();
2182
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002183 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002184 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002185 " device: %s (%d)", request.frame_number, strerror(-res), res);
2186 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2187 return false;
2188 }
2189
Igor Murashkin1e479c02013-09-06 16:55:14 -07002190 // Update the latest request sent to HAL
2191 if (request.settings != NULL) { // Don't update them if they were unchanged
2192 Mutex::Autolock al(mLatestRequestMutex);
2193
2194 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2195 mLatestRequest.acquire(cloned);
2196 }
2197
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002198 if (request.settings != NULL) {
2199 nextRequest->mSettings.unlock(request.settings);
2200 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002201
2202 // Remove any previously queued triggers (after unlock)
2203 res = removeTriggers(mPrevRequest);
2204 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002205 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002206 "(capture request %d, HAL device: %s (%d)",
2207 request.frame_number, strerror(-res), res);
2208 return false;
2209 }
2210 mPrevTriggers = triggerCount;
2211
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002212 // Return input buffer back to framework
2213 if (request.input_buffer != NULL) {
2214 Camera3Stream *stream =
2215 Camera3Stream::cast(request.input_buffer->stream);
2216 res = stream->returnInputBuffer(*(request.input_buffer));
2217 // Note: stream may be deallocated at this point, if this buffer was the
2218 // last reference to it.
2219 if (res != OK) {
2220 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2221 " its stream:%s (%d)", __FUNCTION__,
2222 request.frame_number, strerror(-res), res);
2223 // TODO: Report error upstream
2224 }
2225 }
2226
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002227 return true;
2228}
2229
Igor Murashkin1e479c02013-09-06 16:55:14 -07002230CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2231 Mutex::Autolock al(mLatestRequestMutex);
2232
2233 ALOGV("RequestThread::%s", __FUNCTION__);
2234
2235 return mLatestRequest;
2236}
2237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002238void Camera3Device::RequestThread::cleanUpFailedRequest(
2239 camera3_capture_request_t &request,
2240 sp<CaptureRequest> &nextRequest,
2241 Vector<camera3_stream_buffer_t> &outputBuffers) {
2242
2243 if (request.settings != NULL) {
2244 nextRequest->mSettings.unlock(request.settings);
2245 }
2246 if (request.input_buffer != NULL) {
2247 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002248 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002249 }
2250 for (size_t i = 0; i < request.num_output_buffers; i++) {
2251 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2252 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2253 outputBuffers[i], 0);
2254 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002255}
2256
2257sp<Camera3Device::CaptureRequest>
2258 Camera3Device::RequestThread::waitForNextRequest() {
2259 status_t res;
2260 sp<CaptureRequest> nextRequest;
2261
2262 // Optimized a bit for the simple steady-state case (single repeating
2263 // request), to avoid putting that request in the queue temporarily.
2264 Mutex::Autolock l(mRequestLock);
2265
2266 while (mRequestQueue.empty()) {
2267 if (!mRepeatingRequests.empty()) {
2268 // Always atomically enqueue all requests in a repeating request
2269 // list. Guarantees a complete in-sequence set of captures to
2270 // application.
2271 const RequestList &requests = mRepeatingRequests;
2272 RequestList::const_iterator firstRequest =
2273 requests.begin();
2274 nextRequest = *firstRequest;
2275 mRequestQueue.insert(mRequestQueue.end(),
2276 ++firstRequest,
2277 requests.end());
2278 // No need to wait any longer
2279 break;
2280 }
2281
2282 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002284 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2285 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002286 Mutex::Autolock pl(mPauseLock);
2287 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002288 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002289 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002290 // Let the tracker know
2291 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2292 if (statusTracker != 0) {
2293 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2294 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295 }
2296 // Stop waiting for now and let thread management happen
2297 return NULL;
2298 }
2299 }
2300
2301 if (nextRequest == NULL) {
2302 // Don't have a repeating request already in hand, so queue
2303 // must have an entry now.
2304 RequestList::iterator firstRequest =
2305 mRequestQueue.begin();
2306 nextRequest = *firstRequest;
2307 mRequestQueue.erase(firstRequest);
2308 }
2309
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002310 // In case we've been unpaused by setPaused clearing mDoPause, need to
2311 // update internal pause state (capture/setRepeatingRequest unpause
2312 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002313 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002314 if (mPaused) {
2315 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2316 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2317 if (statusTracker != 0) {
2318 statusTracker->markComponentActive(mStatusId);
2319 }
2320 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002321 mPaused = false;
2322
2323 // Check if we've reconfigured since last time, and reset the preview
2324 // request if so. Can't use 'NULL request == repeat' across configure calls.
2325 if (mReconfigured) {
2326 mPrevRequest.clear();
2327 mReconfigured = false;
2328 }
2329
2330 return nextRequest;
2331}
2332
2333bool Camera3Device::RequestThread::waitIfPaused() {
2334 status_t res;
2335 Mutex::Autolock l(mPauseLock);
2336 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002337 if (mPaused == false) {
2338 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002339 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2340 // Let the tracker know
2341 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2342 if (statusTracker != 0) {
2343 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2344 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002346
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002347 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002348 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349 return true;
2350 }
2351 }
2352 // We don't set mPaused to false here, because waitForNextRequest needs
2353 // to further manage the paused state in case of starvation.
2354 return false;
2355}
2356
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002357void Camera3Device::RequestThread::unpauseForNewRequests() {
2358 // With work to do, mark thread as unpaused.
2359 // If paused by request (setPaused), don't resume, to avoid
2360 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002361 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002362 Mutex::Autolock p(mPauseLock);
2363 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002364 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2365 if (mPaused) {
2366 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2367 if (statusTracker != 0) {
2368 statusTracker->markComponentActive(mStatusId);
2369 }
2370 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002371 mPaused = false;
2372 }
2373}
2374
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002375void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2376 sp<Camera3Device> parent = mParent.promote();
2377 if (parent != NULL) {
2378 va_list args;
2379 va_start(args, fmt);
2380
2381 parent->setErrorStateV(fmt, args);
2382
2383 va_end(args);
2384 }
2385}
2386
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002387status_t Camera3Device::RequestThread::insertTriggers(
2388 const sp<CaptureRequest> &request) {
2389
2390 Mutex::Autolock al(mTriggerMutex);
2391
2392 CameraMetadata &metadata = request->mSettings;
2393 size_t count = mTriggerMap.size();
2394
2395 for (size_t i = 0; i < count; ++i) {
2396 RequestTrigger trigger = mTriggerMap.valueAt(i);
2397
2398 uint32_t tag = trigger.metadataTag;
2399 camera_metadata_entry entry = metadata.find(tag);
2400
2401 if (entry.count > 0) {
2402 /**
2403 * Already has an entry for this trigger in the request.
2404 * Rewrite it with our requested trigger value.
2405 */
2406 RequestTrigger oldTrigger = trigger;
2407
2408 oldTrigger.entryValue = entry.data.u8[0];
2409
2410 mTriggerReplacedMap.add(tag, oldTrigger);
2411 } else {
2412 /**
2413 * More typical, no trigger entry, so we just add it
2414 */
2415 mTriggerRemovedMap.add(tag, trigger);
2416 }
2417
2418 status_t res;
2419
2420 switch (trigger.getTagType()) {
2421 case TYPE_BYTE: {
2422 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2423 res = metadata.update(tag,
2424 &entryValue,
2425 /*count*/1);
2426 break;
2427 }
2428 case TYPE_INT32:
2429 res = metadata.update(tag,
2430 &trigger.entryValue,
2431 /*count*/1);
2432 break;
2433 default:
2434 ALOGE("%s: Type not supported: 0x%x",
2435 __FUNCTION__,
2436 trigger.getTagType());
2437 return INVALID_OPERATION;
2438 }
2439
2440 if (res != OK) {
2441 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2442 ", value %d", __FUNCTION__, trigger.getTagName(),
2443 trigger.entryValue);
2444 return res;
2445 }
2446
2447 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2448 trigger.getTagName(),
2449 trigger.entryValue);
2450 }
2451
2452 mTriggerMap.clear();
2453
2454 return count;
2455}
2456
2457status_t Camera3Device::RequestThread::removeTriggers(
2458 const sp<CaptureRequest> &request) {
2459 Mutex::Autolock al(mTriggerMutex);
2460
2461 CameraMetadata &metadata = request->mSettings;
2462
2463 /**
2464 * Replace all old entries with their old values.
2465 */
2466 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2467 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2468
2469 status_t res;
2470
2471 uint32_t tag = trigger.metadataTag;
2472 switch (trigger.getTagType()) {
2473 case TYPE_BYTE: {
2474 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2475 res = metadata.update(tag,
2476 &entryValue,
2477 /*count*/1);
2478 break;
2479 }
2480 case TYPE_INT32:
2481 res = metadata.update(tag,
2482 &trigger.entryValue,
2483 /*count*/1);
2484 break;
2485 default:
2486 ALOGE("%s: Type not supported: 0x%x",
2487 __FUNCTION__,
2488 trigger.getTagType());
2489 return INVALID_OPERATION;
2490 }
2491
2492 if (res != OK) {
2493 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2494 ", trigger value %d", __FUNCTION__,
2495 trigger.getTagName(), trigger.entryValue);
2496 return res;
2497 }
2498 }
2499 mTriggerReplacedMap.clear();
2500
2501 /**
2502 * Remove all new entries.
2503 */
2504 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2505 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2506 status_t res = metadata.erase(trigger.metadataTag);
2507
2508 if (res != OK) {
2509 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2510 ", trigger value %d", __FUNCTION__,
2511 trigger.getTagName(), trigger.entryValue);
2512 return res;
2513 }
2514 }
2515 mTriggerRemovedMap.clear();
2516
2517 return OK;
2518}
2519
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002520status_t Camera3Device::RequestThread::addDummyTriggerIds(
2521 const sp<CaptureRequest> &request) {
2522 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2523 static const int32_t dummyTriggerId = 1;
2524 status_t res;
2525
2526 CameraMetadata &metadata = request->mSettings;
2527
2528 // If AF trigger is active, insert a dummy AF trigger ID if none already
2529 // exists
2530 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2531 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2532 if (afTrigger.count > 0 &&
2533 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2534 afId.count == 0) {
2535 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2536 if (res != OK) return res;
2537 }
2538
2539 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2540 // if none already exists
2541 camera_metadata_entry pcTrigger =
2542 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2543 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2544 if (pcTrigger.count > 0 &&
2545 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2546 pcId.count == 0) {
2547 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2548 &dummyTriggerId, 1);
2549 if (res != OK) return res;
2550 }
2551
2552 return OK;
2553}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002554
2555
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002556/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002557 * Static callback forwarding methods from HAL to instance
2558 */
2559
2560void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2561 const camera3_capture_result *result) {
2562 Camera3Device *d =
2563 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2564 d->processCaptureResult(result);
2565}
2566
2567void Camera3Device::sNotify(const camera3_callback_ops *cb,
2568 const camera3_notify_msg *msg) {
2569 Camera3Device *d =
2570 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2571 d->notify(msg);
2572}
2573
2574}; // namespace android