blob: 7eef0949ad005a560b1d045a53f0a015d98b6f14 [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
Chien-Yu Chen116a1892016-03-09 12:21:01 -080044#include "CameraService.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070045#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070046#include "device3/Camera3Device.h"
47#include "device3/Camera3OutputStream.h"
48#include "device3/Camera3InputStream.h"
49#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080050
51using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080052
53namespace android {
54
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055Camera3Device::Camera3Device(int id):
56 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080057 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070058 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070059 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070060 mNextResultFrameNumber(0),
61 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063{
64 ATRACE_CALL();
65 camera3_callback_ops::notify = &sNotify;
66 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
67 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
68}
69
70Camera3Device::~Camera3Device()
71{
72 ATRACE_CALL();
73 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
74 disconnect();
75}
76
Igor Murashkin71381052013-03-04 14:53:08 -080077int Camera3Device::getId() const {
78 return mId;
79}
80
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080081/**
82 * CameraDeviceBase interface
83 */
84
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085status_t Camera3Device::initialize(camera_module_t *module)
86{
87 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070088 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080089 Mutex::Autolock l(mLock);
90
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080092 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070093 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094 return INVALID_OPERATION;
95 }
96
97 /** Open HAL device */
98
99 status_t res;
100 String8 deviceName = String8::format("%d", mId);
101
102 camera3_device_t *device;
103
104 res = module->common.methods->open(&module->common, deviceName.string(),
105 reinterpret_cast<hw_device_t**>(&device));
106
107 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700108 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109 return res;
110 }
111
112 /** Cross-check device version */
113
114 if (device->common.version != CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 SET_ERR_L("Could not open camera: "
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 "Camera device is not version %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700117 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118 device->common.version);
119 device->common.close(&device->common);
120 return BAD_VALUE;
121 }
122
123 camera_info info;
124 res = module->get_camera_info(mId, &info);
125 if (res != OK) return res;
126
127 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700128 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
129 " and device version (%x).",
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800130 device->common.version, info.device_version);
131 device->common.close(&device->common);
132 return BAD_VALUE;
133 }
134
135 /** Initialize device with callback functions */
136
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700137 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700139 ATRACE_END();
140
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700142 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
143 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 device->common.close(&device->common);
145 return BAD_VALUE;
146 }
147
148 /** Get vendor metadata tags */
149
150 mVendorTagOps.get_camera_vendor_section_name = NULL;
151
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700152 ATRACE_BEGIN("camera3->get_metadata_vendor_tag_ops");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800153 device->ops->get_metadata_vendor_tag_ops(device, &mVendorTagOps);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700154 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800155
156 if (mVendorTagOps.get_camera_vendor_section_name != NULL) {
157 res = set_camera_metadata_vendor_tag_ops(&mVendorTagOps);
158 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700159 SET_ERR_L("Unable to set tag ops: %s (%d)",
160 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800161 device->common.close(&device->common);
162 return res;
163 }
164 }
165
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700166 /** Start up status tracker thread */
167 mStatusTracker = new StatusTracker(this);
168 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
169 if (res != OK) {
170 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
171 strerror(-res), res);
172 device->common.close(&device->common);
173 mStatusTracker.clear();
174 return res;
175 }
176
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800177 /** Start up request queue thread */
178
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700179 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800180 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700182 SET_ERR_L("Unable to start request queue thread: %s (%d)",
183 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800184 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800186 return res;
187 }
188
189 /** Everything is good to go */
190
191 mDeviceInfo = info.static_camera_characteristics;
192 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700193 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800194 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700195 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700196 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700198 /** Check for quirks */
199
200 // Will the HAL be sending in early partial result metadata?
201 camera_metadata_entry partialResultsQuirk =
202 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
203 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
204 mUsePartialResultQuirk = true;
205 }
206
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800207 return OK;
208}
209
210status_t Camera3Device::disconnect() {
211 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700212 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800213
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800214 ALOGV("%s: E", __FUNCTION__);
215
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700216 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800217
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 {
219 Mutex::Autolock l(mLock);
220 if (mStatus == STATUS_UNINITIALIZED) return res;
221
222 if (mStatus == STATUS_ACTIVE ||
223 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
224 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700225 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700226 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700227 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700228 } else {
229 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
230 if (res != OK) {
231 SET_ERR_L("Timeout waiting for HAL to drain");
232 // Continue to close device even in case of error
233 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700234 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800235 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800236
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700237 if (mStatus == STATUS_ERROR) {
238 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700239 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700240
241 if (mStatusTracker != NULL) {
242 mStatusTracker->requestExit();
243 }
244
245 if (mRequestThread != NULL) {
246 mRequestThread->requestExit();
247 }
248
249 mOutputStreams.clear();
250 mInputStream.clear();
251 }
252
253 // Joining done without holding mLock, otherwise deadlocks may ensue
254 // as the threads try to access parent state
255 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
256 // HAL may be in a bad state, so waiting for request thread
257 // (which may be stuck in the HAL processCaptureRequest call)
258 // could be dangerous.
259 mRequestThread->join();
260 }
261
262 if (mStatusTracker != NULL) {
263 mStatusTracker->join();
264 }
265
266 {
267 Mutex::Autolock l(mLock);
268
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700270 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800271
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 if (mHal3Device != NULL) {
273 mHal3Device->common.close(&mHal3Device->common);
274 mHal3Device = NULL;
275 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800276
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 mStatus = STATUS_UNINITIALIZED;
278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279
280 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800282}
283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284// For dumping/debugging only -
285// try to acquire a lock a few times, eventually give up to proceed with
286// debug/dump operations
287bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
288 bool gotLock = false;
289 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
290 if (lock.tryLock() == NO_ERROR) {
291 gotLock = true;
292 break;
293 } else {
294 usleep(kDumpSleepDuration);
295 }
296 }
297 return gotLock;
298}
299
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
301 ATRACE_CALL();
302 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700303
304 // Try to lock, but continue in case of failure (to avoid blocking in
305 // deadlocks)
306 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
307 bool gotLock = tryLockSpinRightRound(mLock);
308
309 ALOGW_IF(!gotInterfaceLock,
310 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
311 mId, __FUNCTION__);
312 ALOGW_IF(!gotLock,
313 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
314 mId, __FUNCTION__);
315
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800316 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800317
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800318 const char *status =
319 mStatus == STATUS_ERROR ? "ERROR" :
320 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700321 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
322 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800323 mStatus == STATUS_ACTIVE ? "ACTIVE" :
324 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700325
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800326 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700327 if (mStatus == STATUS_ERROR) {
328 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
329 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800330 lines.appendFormat(" Stream configuration:\n");
331
332 if (mInputStream != NULL) {
333 write(fd, lines.string(), lines.size());
334 mInputStream->dump(fd, args);
335 } else {
336 lines.appendFormat(" No input stream.\n");
337 write(fd, lines.string(), lines.size());
338 }
339 for (size_t i = 0; i < mOutputStreams.size(); i++) {
340 mOutputStreams[i]->dump(fd,args);
341 }
342
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700343 lines = String8(" In-flight requests:\n");
344 if (mInFlightMap.size() == 0) {
345 lines.append(" None\n");
346 } else {
347 for (size_t i = 0; i < mInFlightMap.size(); i++) {
348 InFlightRequest r = mInFlightMap.valueAt(i);
349 lines.appendFormat(" Frame %d | Timestamp: %lld, metadata"
350 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
351 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
352 r.numBuffersLeft);
353 }
354 }
355 write(fd, lines.string(), lines.size());
356
Igor Murashkin1e479c02013-09-06 16:55:14 -0700357 {
358 lines = String8(" Last request sent:\n");
359 write(fd, lines.string(), lines.size());
360
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700361 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700362 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
363 }
364
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800365 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700366 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800367 write(fd, lines.string(), lines.size());
368 mHal3Device->ops->dump(mHal3Device, fd);
369 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800370
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700371 if (gotLock) mLock.unlock();
372 if (gotInterfaceLock) mInterfaceLock.unlock();
373
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800374 return OK;
375}
376
377const CameraMetadata& Camera3Device::info() const {
378 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800379 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
380 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700381 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800382 mStatus == STATUS_ERROR ?
383 "when in error state" : "before init");
384 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800385 return mDeviceInfo;
386}
387
388status_t Camera3Device::capture(CameraMetadata &request) {
389 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700390 status_t res;
391 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800392 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800393
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700394 // TODO: take ownership of the request
395
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396 switch (mStatus) {
397 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700398 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800399 return INVALID_OPERATION;
400 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700401 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700403 case STATUS_UNCONFIGURED:
404 // May be lazily configuring streams, will check during setup
405 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406 case STATUS_ACTIVE:
407 // OK
408 break;
409 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700410 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800411 return INVALID_OPERATION;
412 }
413
414 sp<CaptureRequest> newRequest = setUpRequestLocked(request);
415 if (newRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700416 CLOGE("Can't create capture request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800417 return BAD_VALUE;
418 }
419
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700420 res = mRequestThread->queueRequest(newRequest);
421 if (res == OK) {
422 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
423 if (res != OK) {
424 SET_ERR_L("Can't transition to active in %f seconds!",
425 kActiveTimeout/1e9);
426 }
427 ALOGV("Camera %d: Capture request enqueued", mId);
428 }
429 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800430}
431
432
433status_t Camera3Device::setStreamingRequest(const CameraMetadata &request) {
434 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700435 status_t res;
436 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800437 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800438
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800439 switch (mStatus) {
440 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700441 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800442 return INVALID_OPERATION;
443 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700444 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700446 case STATUS_UNCONFIGURED:
447 // May be lazily configuring streams, will check during setup
448 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800449 case STATUS_ACTIVE:
450 // OK
451 break;
452 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700453 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800454 return INVALID_OPERATION;
455 }
456
457 sp<CaptureRequest> newRepeatingRequest = setUpRequestLocked(request);
458 if (newRepeatingRequest == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700459 CLOGE("Can't create repeating request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800460 return BAD_VALUE;
461 }
462
463 RequestList newRepeatingRequests;
464 newRepeatingRequests.push_back(newRepeatingRequest);
465
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700466 res = mRequestThread->setRepeatingRequests(newRepeatingRequests);
467 if (res == OK) {
468 waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
469 if (res != OK) {
470 SET_ERR_L("Can't transition to active in %f seconds!",
471 kActiveTimeout/1e9);
472 }
473 ALOGV("Camera %d: Repeating request set", mId);
474 }
475 return res;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476}
477
478
479sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
480 const CameraMetadata &request) {
481 status_t res;
482
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700483 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800484 res = configureStreamsLocked();
485 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700486 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800487 return NULL;
488 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700489 if (mStatus == STATUS_UNCONFIGURED) {
490 CLOGE("No streams configured");
491 return NULL;
492 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800493 }
494
495 sp<CaptureRequest> newRequest = createCaptureRequest(request);
496 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800497}
498
499status_t Camera3Device::clearStreamingRequest() {
500 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700501 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800502 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800503
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800504 switch (mStatus) {
505 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700506 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800507 return INVALID_OPERATION;
508 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700509 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800510 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700511 case STATUS_UNCONFIGURED:
512 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800513 case STATUS_ACTIVE:
514 // OK
515 break;
516 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700517 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800518 return INVALID_OPERATION;
519 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700520 ALOGV("Camera %d: Clearing repeating request", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800521 return mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800522}
523
524status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
525 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700526 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800527
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700528 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529}
530
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700531status_t Camera3Device::createInputStream(
532 uint32_t width, uint32_t height, int format, int *id) {
533 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700534 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700535 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700536 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
537 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700538
539 status_t res;
540 bool wasActive = false;
541
542 switch (mStatus) {
543 case STATUS_ERROR:
544 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
545 return INVALID_OPERATION;
546 case STATUS_UNINITIALIZED:
547 ALOGE("%s: Device not initialized", __FUNCTION__);
548 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700549 case STATUS_UNCONFIGURED:
550 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700551 // OK
552 break;
553 case STATUS_ACTIVE:
554 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700555 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700556 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700557 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700558 return res;
559 }
560 wasActive = true;
561 break;
562 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700563 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700564 return INVALID_OPERATION;
565 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700566 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700567
568 if (mInputStream != 0) {
569 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
570 return INVALID_OPERATION;
571 }
572
573 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
574 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700575 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700576
577 mInputStream = newStream;
578
579 *id = mNextStreamId++;
580
581 // Continue captures if active at start
582 if (wasActive) {
583 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
584 res = configureStreamsLocked();
585 if (res != OK) {
586 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
587 __FUNCTION__, mNextStreamId, strerror(-res), res);
588 return res;
589 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700590 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700591 }
592
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700593 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700594 return OK;
595}
596
Igor Murashkin2fba5842013-04-22 14:03:54 -0700597
598status_t Camera3Device::createZslStream(
599 uint32_t width, uint32_t height,
600 int depth,
601 /*out*/
602 int *id,
603 sp<Camera3ZslStream>* zslStream) {
604 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700605 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700606 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700607 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
608 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700609
610 status_t res;
611 bool wasActive = false;
612
613 switch (mStatus) {
614 case STATUS_ERROR:
615 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
616 return INVALID_OPERATION;
617 case STATUS_UNINITIALIZED:
618 ALOGE("%s: Device not initialized", __FUNCTION__);
619 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700620 case STATUS_UNCONFIGURED:
621 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700622 // OK
623 break;
624 case STATUS_ACTIVE:
625 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700627 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700628 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700629 return res;
630 }
631 wasActive = true;
632 break;
633 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700634 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700635 return INVALID_OPERATION;
636 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700637 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700638
639 if (mInputStream != 0) {
640 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
641 return INVALID_OPERATION;
642 }
643
644 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
645 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700646 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700647
648 res = mOutputStreams.add(mNextStreamId, newStream);
649 if (res < 0) {
650 ALOGE("%s: Can't add new stream to set: %s (%d)",
651 __FUNCTION__, strerror(-res), res);
652 return res;
653 }
654 mInputStream = newStream;
655
656 *id = mNextStreamId++;
657 *zslStream = newStream;
658
659 // Continue captures if active at start
660 if (wasActive) {
661 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
662 res = configureStreamsLocked();
663 if (res != OK) {
664 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
665 __FUNCTION__, mNextStreamId, strerror(-res), res);
666 return res;
667 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700669 }
670
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700671 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700672 return OK;
673}
674
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800675status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
676 uint32_t width, uint32_t height, int format, size_t size, int *id) {
677 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800679 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700680 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %d",
681 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800682
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800683 status_t res;
684 bool wasActive = false;
685
686 switch (mStatus) {
687 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700688 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800689 return INVALID_OPERATION;
690 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700691 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800692 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700693 case STATUS_UNCONFIGURED:
694 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800695 // OK
696 break;
697 case STATUS_ACTIVE:
698 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700699 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800700 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800702 return res;
703 }
704 wasActive = true;
705 break;
706 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700707 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800708 return INVALID_OPERATION;
709 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700710 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800711
712 sp<Camera3OutputStream> newStream;
713 if (format == HAL_PIXEL_FORMAT_BLOB) {
714 newStream = new Camera3OutputStream(mNextStreamId, consumer,
715 width, height, size, format);
716 } else {
717 newStream = new Camera3OutputStream(mNextStreamId, consumer,
718 width, height, format);
719 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700720 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800721
722 res = mOutputStreams.add(mNextStreamId, newStream);
723 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700724 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800725 return res;
726 }
727
728 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700729 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800730
731 // Continue captures if active at start
732 if (wasActive) {
733 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
734 res = configureStreamsLocked();
735 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700736 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
737 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800738 return res;
739 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700740 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800741 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700742 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800743 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800744}
745
746status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
747 ATRACE_CALL();
748 (void)outputId; (void)id;
749
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700750 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800751 return INVALID_OPERATION;
752}
753
754
755status_t Camera3Device::getStreamInfo(int id,
756 uint32_t *width, uint32_t *height, uint32_t *format) {
757 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800759 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800760
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800761 switch (mStatus) {
762 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700763 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800764 return INVALID_OPERATION;
765 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700766 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800767 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700768 case STATUS_UNCONFIGURED:
769 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800770 case STATUS_ACTIVE:
771 // OK
772 break;
773 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700774 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800775 return INVALID_OPERATION;
776 }
777
778 ssize_t idx = mOutputStreams.indexOfKey(id);
779 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700780 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800781 return idx;
782 }
783
784 if (width) *width = mOutputStreams[idx]->getWidth();
785 if (height) *height = mOutputStreams[idx]->getHeight();
786 if (format) *format = mOutputStreams[idx]->getFormat();
787
788 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800789}
790
791status_t Camera3Device::setStreamTransform(int id,
792 int transform) {
793 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700794 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800795 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800796
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800797 switch (mStatus) {
798 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700799 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 return INVALID_OPERATION;
801 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700802 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800803 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700804 case STATUS_UNCONFIGURED:
805 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800806 case STATUS_ACTIVE:
807 // OK
808 break;
809 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700810 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 return INVALID_OPERATION;
812 }
813
814 ssize_t idx = mOutputStreams.indexOfKey(id);
815 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700816 CLOGE("Stream %d does not exist",
817 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800818 return BAD_VALUE;
819 }
820
821 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800822}
823
824status_t Camera3Device::deleteStream(int id) {
825 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700826 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800827 Mutex::Autolock l(mLock);
828 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800829
Igor Murashkine2172be2013-05-28 15:31:39 -0700830 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
831
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 // CameraDevice semantics require device to already be idle before
833 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700834 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700835 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
836 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800837 }
838
Igor Murashkin2fba5842013-04-22 14:03:54 -0700839 sp<Camera3StreamInterface> deletedStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800840 if (mInputStream != NULL && id == mInputStream->getId()) {
841 deletedStream = mInputStream;
842 mInputStream.clear();
843 } else {
844 ssize_t idx = mOutputStreams.indexOfKey(id);
845 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700846 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 return BAD_VALUE;
848 }
849 deletedStream = mOutputStreams.editValueAt(idx);
850 mOutputStreams.removeItem(id);
851 }
852
853 // Free up the stream endpoint so that it can be used by some other stream
854 res = deletedStream->disconnect();
855 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700856 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857 // fall through since we want to still list the stream as deleted.
858 }
859 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700860 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861
862 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800863}
864
865status_t Camera3Device::deleteReprocessStream(int id) {
866 ATRACE_CALL();
867 (void)id;
868
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700869 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800870 return INVALID_OPERATION;
871}
872
873
874status_t Camera3Device::createDefaultRequest(int templateId,
875 CameraMetadata *request) {
876 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700877 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen116a1892016-03-09 12:21:01 -0800878
879 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
880 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
881 IPCThreadState::self()->getCallingUid(), NULL, 0);
882 return BAD_VALUE;
883 }
884
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();
1131 return mHal3Device->ops->flush(mHal3Device);
1132}
1133
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001134/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001135 * Methods called by subclasses
1136 */
1137
1138void Camera3Device::notifyStatus(bool idle) {
1139 {
1140 // Need mLock to safely update state and synchronize to current
1141 // state of methods in flight.
1142 Mutex::Autolock l(mLock);
1143 // We can get various system-idle notices from the status tracker
1144 // while starting up. Only care about them if we've actually sent
1145 // in some requests recently.
1146 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1147 return;
1148 }
1149 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1150 idle ? "idle" : "active");
1151 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1152 mRecentStatusUpdates.add(mStatus);
1153 mStatusChanged.signal();
1154
1155 // Skip notifying listener if we're doing some user-transparent
1156 // state changes
1157 if (mPauseStateNotify) return;
1158 }
1159 NotificationListener *listener;
1160 {
1161 Mutex::Autolock l(mOutputLock);
1162 listener = mListener;
1163 }
1164 if (idle && listener != NULL) {
1165 listener->notifyIdle();
1166 }
1167}
1168
1169/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001170 * Camera3Device private methods
1171 */
1172
1173sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1174 const CameraMetadata &request) {
1175 ATRACE_CALL();
1176 status_t res;
1177
1178 sp<CaptureRequest> newRequest = new CaptureRequest;
1179 newRequest->mSettings = request;
1180
1181 camera_metadata_entry_t inputStreams =
1182 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1183 if (inputStreams.count > 0) {
1184 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001185 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001186 CLOGE("Request references unknown input stream %d",
1187 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188 return NULL;
1189 }
1190 // Lazy completion of stream configuration (allocation/registration)
1191 // on first use
1192 if (mInputStream->isConfiguring()) {
1193 res = mInputStream->finishConfiguration(mHal3Device);
1194 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001195 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001196 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001197 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001198 return NULL;
1199 }
1200 }
1201
1202 newRequest->mInputStream = mInputStream;
1203 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1204 }
1205
1206 camera_metadata_entry_t streams =
1207 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1208 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001209 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 return NULL;
1211 }
1212
1213 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001214 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001215 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001216 CLOGE("Request references unknown stream %d",
1217 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return NULL;
1219 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001220 sp<Camera3OutputStreamInterface> stream =
1221 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001222
1223 // Lazy completion of stream configuration (allocation/registration)
1224 // on first use
1225 if (stream->isConfiguring()) {
1226 res = stream->finishConfiguration(mHal3Device);
1227 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001228 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1229 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 return NULL;
1231 }
1232 }
1233
1234 newRequest->mOutputStreams.push(stream);
1235 }
1236 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1237
1238 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239}
1240
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241status_t Camera3Device::configureStreamsLocked() {
1242 ATRACE_CALL();
1243 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001244
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001246 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001247 return INVALID_OPERATION;
1248 }
1249
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001250 if (!mNeedConfig) {
1251 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1252 return OK;
1253 }
1254
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001255 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001256 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001257
1258 camera3_stream_configuration config;
1259
1260 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1261
1262 Vector<camera3_stream_t*> streams;
1263 streams.setCapacity(config.num_streams);
1264
1265 if (mInputStream != NULL) {
1266 camera3_stream_t *inputStream;
1267 inputStream = mInputStream->startConfiguration();
1268 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001269 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 return INVALID_OPERATION;
1271 }
1272 streams.add(inputStream);
1273 }
1274
1275 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001276
1277 // Don't configure bidi streams twice, nor add them twice to the list
1278 if (mOutputStreams[i].get() ==
1279 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1280
1281 config.num_streams--;
1282 continue;
1283 }
1284
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001285 camera3_stream_t *outputStream;
1286 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1287 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001288 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001289 return INVALID_OPERATION;
1290 }
1291 streams.add(outputStream);
1292 }
1293
1294 config.streams = streams.editArray();
1295
1296 // Do the HAL configuration; will potentially touch stream
1297 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001298 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001300 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001301
1302 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001303 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1304 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001305 return res;
1306 }
1307
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001308 // Finish all stream configuration immediately.
1309 // TODO: Try to relax this later back to lazy completion, which should be
1310 // faster
1311
Igor Murashkin073f8572013-05-02 14:59:28 -07001312 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001313 res = mInputStream->finishConfiguration(mHal3Device);
1314 if (res != OK) {
1315 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1316 mInputStream->getId(), strerror(-res), res);
1317 return res;
1318 }
1319 }
1320
1321 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001322 sp<Camera3OutputStreamInterface> outputStream =
1323 mOutputStreams.editValueAt(i);
1324 if (outputStream->isConfiguring()) {
1325 res = outputStream->finishConfiguration(mHal3Device);
1326 if (res != OK) {
1327 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1328 outputStream->getId(), strerror(-res), res);
1329 return res;
1330 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001331 }
1332 }
1333
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 // Request thread needs to know to avoid using repeat-last-settings protocol
1335 // across configure_streams() calls
1336 mRequestThread->configurationComplete();
1337
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001338 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001339
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001340 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001341
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001342 if (config.num_streams > 0) {
1343 mStatus = STATUS_CONFIGURED;
1344 } else {
1345 mStatus = STATUS_UNCONFIGURED;
1346 }
1347
1348 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001350 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001351}
1352
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001353void Camera3Device::setErrorState(const char *fmt, ...) {
1354 Mutex::Autolock l(mLock);
1355 va_list args;
1356 va_start(args, fmt);
1357
1358 setErrorStateLockedV(fmt, args);
1359
1360 va_end(args);
1361}
1362
1363void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1364 Mutex::Autolock l(mLock);
1365 setErrorStateLockedV(fmt, args);
1366}
1367
1368void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1369 va_list args;
1370 va_start(args, fmt);
1371
1372 setErrorStateLockedV(fmt, args);
1373
1374 va_end(args);
1375}
1376
1377void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001378 // Print out all error messages to log
1379 String8 errorCause = String8::formatV(fmt, args);
1380 ALOGE("Camera %d: %s", mId, errorCause.string());
1381
1382 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001383 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001384
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001385 // Save stack trace. View by dumping it later.
1386 CameraTraces::saveTrace();
1387 // TODO: consider adding errorCause and client pid/procname
1388
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001389 mErrorCause = errorCause;
1390
1391 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001392 mStatus = STATUS_ERROR;
1393}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001394
1395/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001396 * In-flight request management
1397 */
1398
1399status_t Camera3Device::registerInFlight(int32_t frameNumber,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001400 int32_t requestId, int32_t numBuffers) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001401 ATRACE_CALL();
1402 Mutex::Autolock l(mInFlightLock);
1403
1404 ssize_t res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001405 res = mInFlightMap.add(frameNumber, InFlightRequest(requestId, numBuffers));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001406 if (res < 0) return res;
1407
1408 return OK;
1409}
1410
1411/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001412 * QUIRK(partial results)
1413 * Check if all 3A fields are ready, and send off a partial 3A-only result
1414 * to the output frame queue
1415 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001416bool Camera3Device::processPartial3AQuirk(
1417 int32_t frameNumber, int32_t requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001418 const CameraMetadata& partial) {
1419
1420 // Check if all 3A states are present
1421 // The full list of fields is
1422 // android.control.afMode
1423 // android.control.awbMode
1424 // android.control.aeState
1425 // android.control.awbState
1426 // android.control.afState
1427 // android.control.afTriggerID
1428 // android.control.aePrecaptureID
1429 // TODO: Add android.control.aeMode
1430
1431 bool gotAllStates = true;
1432
1433 uint8_t afMode;
1434 uint8_t awbMode;
1435 uint8_t aeState;
1436 uint8_t afState;
1437 uint8_t awbState;
1438 int32_t afTriggerId;
1439 int32_t aeTriggerId;
1440
1441 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1442 &afMode, frameNumber);
1443
1444 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1445 &awbMode, frameNumber);
1446
1447 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1448 &aeState, frameNumber);
1449
1450 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1451 &afState, frameNumber);
1452
1453 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1454 &awbState, frameNumber);
1455
1456 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1457 &afTriggerId, frameNumber);
1458
1459 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1460 &aeTriggerId, frameNumber);
1461
1462 if (!gotAllStates) return false;
1463
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001464 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001465 "AF state %d, AE state %d, AWB state %d, "
1466 "AF trigger %d, AE precapture trigger %d",
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001467 __FUNCTION__, mId, frameNumber, requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001468 afMode, awbMode,
1469 afState, aeState, awbState,
1470 afTriggerId, aeTriggerId);
1471
1472 // Got all states, so construct a minimal result to send
1473 // In addition to the above fields, this means adding in
1474 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001475 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001476 // android.quirks.partialResult
1477
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001478 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001479
1480 Mutex::Autolock l(mOutputLock);
1481
1482 CameraMetadata& min3AResult =
1483 *mResultQueue.insert(
1484 mResultQueue.end(),
1485 CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0));
1486
1487 if (!insert3AResult(min3AResult, ANDROID_REQUEST_FRAME_COUNT,
1488 &frameNumber, frameNumber)) {
1489 return false;
1490 }
1491
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001492 if (!insert3AResult(min3AResult, ANDROID_REQUEST_ID,
1493 &requestId, frameNumber)) {
1494 return false;
1495 }
1496
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001497 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1498 if (!insert3AResult(min3AResult, ANDROID_QUIRKS_PARTIAL_RESULT,
1499 &partialResult, frameNumber)) {
1500 return false;
1501 }
1502
1503 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_MODE,
1504 &afMode, frameNumber)) {
1505 return false;
1506 }
1507
1508 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_MODE,
1509 &awbMode, frameNumber)) {
1510 return false;
1511 }
1512
1513 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_STATE,
1514 &aeState, frameNumber)) {
1515 return false;
1516 }
1517
1518 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_STATE,
1519 &afState, frameNumber)) {
1520 return false;
1521 }
1522
1523 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AWB_STATE,
1524 &awbState, frameNumber)) {
1525 return false;
1526 }
1527
1528 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AF_TRIGGER_ID,
1529 &afTriggerId, frameNumber)) {
1530 return false;
1531 }
1532
1533 if (!insert3AResult(min3AResult, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1534 &aeTriggerId, frameNumber)) {
1535 return false;
1536 }
1537
1538 mResultSignal.signal();
1539
1540 return true;
1541}
1542
1543template<typename T>
1544bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1545 T* value, int32_t frameNumber) {
1546 (void) frameNumber;
1547
1548 camera_metadata_ro_entry_t entry;
1549
1550 entry = result.find(tag);
1551 if (entry.count == 0) {
1552 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1553 mId, frameNumber, get_camera_metadata_tag_name(tag));
1554 return false;
1555 }
1556
1557 if (sizeof(T) == sizeof(uint8_t)) {
1558 *value = entry.data.u8[0];
1559 } else if (sizeof(T) == sizeof(int32_t)) {
1560 *value = entry.data.i32[0];
1561 } else {
1562 ALOGE("%s: Unexpected type", __FUNCTION__);
1563 return false;
1564 }
1565 return true;
1566}
1567
1568template<typename T>
1569bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1570 const T* value, int32_t frameNumber) {
1571 if (result.update(tag, value, 1) != NO_ERROR) {
1572 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1573 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1574 frameNumber, get_camera_metadata_tag_name(tag));
1575 return false;
1576 }
1577 return true;
1578}
1579
1580/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001581 * Camera HAL device callback methods
1582 */
1583
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001584void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001585 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001586
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001587 status_t res;
1588
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001589 uint32_t frameNumber = result->frame_number;
1590 if (result->result == NULL && result->num_output_buffers == 0) {
1591 SET_ERR("No result data provided by HAL for frame %d",
1592 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001593 return;
1594 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001595 bool partialResultQuirk = false;
1596 CameraMetadata collectedQuirkResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001597
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001598 // Get capture timestamp from list of in-flight requests, where it was added
1599 // by the shutter notification for this frame. Then update the in-flight
1600 // status and remove the in-flight entry if all result data has been
1601 // received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001602 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001603 {
1604 Mutex::Autolock l(mInFlightLock);
1605 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1606 if (idx == NAME_NOT_FOUND) {
1607 SET_ERR("Unknown frame number for capture result: %d",
1608 frameNumber);
1609 return;
1610 }
1611 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001612
1613 // Check if this result carries only partial metadata
1614 if (mUsePartialResultQuirk && result->result != NULL) {
1615 camera_metadata_ro_entry_t partialResultEntry;
1616 res = find_camera_metadata_ro_entry(result->result,
1617 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1618 if (res != NAME_NOT_FOUND &&
1619 partialResultEntry.count > 0 &&
1620 partialResultEntry.data.u8[0] ==
1621 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1622 // A partial result. Flag this as such, and collect this
1623 // set of metadata into the in-flight entry.
1624 partialResultQuirk = true;
1625 request.partialResultQuirk.collectedResult.append(
1626 result->result);
1627 request.partialResultQuirk.collectedResult.erase(
1628 ANDROID_QUIRKS_PARTIAL_RESULT);
1629 // Fire off a 3A-only result if possible
1630 if (!request.partialResultQuirk.haveSent3A) {
1631 request.partialResultQuirk.haveSent3A =
1632 processPartial3AQuirk(frameNumber,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001633 request.requestId,
1634 request.partialResultQuirk.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001635 }
1636 }
1637 }
1638
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001639 timestamp = request.captureTimestamp;
Zhijun He1d1f8462013-10-02 16:29:51 -07001640 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001641 * One of the following must happen before it's legal to call process_capture_result,
1642 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001643 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1644 * - CAMERA3_MSG_ERROR (expected during flush)
1645 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001646 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001647 SET_ERR("Called before shutter notify for frame %d",
1648 frameNumber);
1649 return;
1650 }
1651
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001652 // Did we get the (final) result metadata for this capture?
1653 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001654 if (request.haveResultMetadata) {
1655 SET_ERR("Called multiple times with metadata for frame %d",
1656 frameNumber);
1657 return;
1658 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001659 if (mUsePartialResultQuirk &&
1660 !request.partialResultQuirk.collectedResult.isEmpty()) {
1661 collectedQuirkResult.acquire(
1662 request.partialResultQuirk.collectedResult);
1663 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001664 request.haveResultMetadata = true;
1665 }
1666
1667 request.numBuffersLeft -= result->num_output_buffers;
1668
1669 if (request.numBuffersLeft < 0) {
1670 SET_ERR("Too many buffers returned for frame %d",
1671 frameNumber);
1672 return;
1673 }
1674
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001675 // Check if everything has arrived for this result (buffers and metadata)
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001676 if (request.haveResultMetadata && request.numBuffersLeft == 0) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001677 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001678 mInFlightMap.removeItemsAt(idx, 1);
1679 }
1680
1681 // Sanity check - if we have too many in-flight frames, something has
1682 // likely gone wrong
1683 if (mInFlightMap.size() > kInFlightWarnLimit) {
1684 CLOGE("In-flight list too large: %d", mInFlightMap.size());
1685 }
1686
1687 }
1688
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001689 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001690 bool gotResult = false;
1691 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001692 Mutex::Autolock l(mOutputLock);
1693
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001694 gotResult = true;
1695
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001696 if (frameNumber != mNextResultFrameNumber) {
1697 SET_ERR("Out-of-order capture result metadata submitted! "
1698 "(got frame number %d, expecting %d)",
1699 frameNumber, mNextResultFrameNumber);
1700 return;
1701 }
1702 mNextResultFrameNumber++;
1703
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001704 CameraMetadata captureResult;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001705 captureResult = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001706
Igor Murashkind2c90692013-04-02 12:32:32 -07001707 if (captureResult.update(ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001708 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001709 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001710 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001711 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001712 } else {
1713 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001714 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001715 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001716
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001717 // Append any previous partials to form a complete result
1718 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
1719 captureResult.append(collectedQuirkResult);
1720 }
1721
1722 captureResult.sort();
1723
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001724 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001725
1726 camera_metadata_entry entry =
1727 captureResult.find(ANDROID_SENSOR_TIMESTAMP);
1728 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001729 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001730 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001731 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001732 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001733 SET_ERR("Timestamp mismatch between shutter notify and result"
1734 " metadata for frame %d (%lld vs %lld respectively)",
1735 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001736 gotResult = false;
1737 }
1738
1739 if (gotResult) {
1740 // Valid result, insert into queue
1741 CameraMetadata& queuedResult =
1742 *mResultQueue.insert(mResultQueue.end(), CameraMetadata());
1743 queuedResult.swap(captureResult);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001744 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001745 } // scope for mOutputLock
1746
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001747 // Return completed buffers to their streams with the timestamp
1748
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001749 for (size_t i = 0; i < result->num_output_buffers; i++) {
1750 Camera3Stream *stream =
1751 Camera3Stream::cast(result->output_buffers[i].stream);
1752 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1753 // Note: stream may be deallocated at this point, if this buffer was the
1754 // last reference to it.
1755 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07001756 ALOGE("Can't return buffer %d for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001757 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001758 }
1759 }
1760
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001761 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001762
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001763 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001764 mResultSignal.signal();
1765 }
1766
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001767}
1768
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001769
1770
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001771void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001772 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001773 NotificationListener *listener;
1774 {
1775 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001776 listener = mListener;
1777 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001778
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001779 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001780 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001781 return;
1782 }
1783
1784 switch (msg->type) {
1785 case CAMERA3_MSG_ERROR: {
1786 int streamId = 0;
1787 if (msg->message.error.error_stream != NULL) {
1788 Camera3Stream *stream =
1789 Camera3Stream::cast(
1790 msg->message.error.error_stream);
1791 streamId = stream->getId();
1792 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001793 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1794 mId, __FUNCTION__, msg->message.error.frame_number,
1795 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001796
1797 // Set request error status for the request in the in-flight tracking
1798 {
1799 Mutex::Autolock l(mInFlightLock);
1800 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1801 if (idx >= 0) {
1802 mInFlightMap.editValueAt(idx).requestStatus = msg->message.error.error_code;
1803 }
1804 }
1805
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001806 if (listener != NULL) {
1807 listener->notifyError(msg->message.error.error_code,
1808 msg->message.error.frame_number, streamId);
1809 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001810 break;
1811 }
1812 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001813 ssize_t idx;
1814 uint32_t frameNumber = msg->message.shutter.frame_number;
1815 nsecs_t timestamp = msg->message.shutter.timestamp;
1816 // Verify ordering of shutter notifications
1817 {
1818 Mutex::Autolock l(mOutputLock);
1819 if (frameNumber != mNextShutterFrameNumber) {
1820 SET_ERR("Shutter notification out-of-order. Expected "
1821 "notification for frame %d, got frame %d",
1822 mNextShutterFrameNumber, frameNumber);
1823 break;
1824 }
1825 mNextShutterFrameNumber++;
1826 }
1827
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001828 int32_t requestId = -1;
1829
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001830 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001831 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001832 {
1833 Mutex::Autolock l(mInFlightLock);
1834 idx = mInFlightMap.indexOfKey(frameNumber);
1835 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001836 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1837 r.captureTimestamp = timestamp;
1838 requestId = r.requestId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001839 }
1840 }
1841 if (idx < 0) {
1842 SET_ERR("Shutter notification for non-existent frame number %d",
1843 frameNumber);
1844 break;
1845 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001846 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %lld",
1847 mId, __FUNCTION__, frameNumber, requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001848 // Call listener, if any
1849 if (listener != NULL) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001850 listener->notifyShutter(requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001851 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001852 break;
1853 }
1854 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001855 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001856 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001857 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001858}
1859
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001860CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001861 ALOGV("%s", __FUNCTION__);
1862
Igor Murashkin1e479c02013-09-06 16:55:14 -07001863 CameraMetadata retVal;
1864
1865 if (mRequestThread != NULL) {
1866 retVal = mRequestThread->getLatestRequest();
1867 }
1868
Igor Murashkin1e479c02013-09-06 16:55:14 -07001869 return retVal;
1870}
1871
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001872/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001873 * RequestThread inner class methods
1874 */
1875
1876Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001877 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001878 camera3_device_t *hal3Device) :
1879 Thread(false),
1880 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001881 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001882 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001883 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001884 mReconfigured(false),
1885 mDoPause(false),
1886 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001887 mFrameNumber(0),
1888 mLatestRequestId(NAME_NOT_FOUND) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001889 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001890}
1891
1892void Camera3Device::RequestThread::configurationComplete() {
1893 Mutex::Autolock l(mRequestLock);
1894 mReconfigured = true;
1895}
1896
1897status_t Camera3Device::RequestThread::queueRequest(
1898 sp<CaptureRequest> request) {
1899 Mutex::Autolock l(mRequestLock);
1900 mRequestQueue.push_back(request);
1901
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001902 unpauseForNewRequests();
1903
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001904 return OK;
1905}
1906
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001907
1908status_t Camera3Device::RequestThread::queueTrigger(
1909 RequestTrigger trigger[],
1910 size_t count) {
1911
1912 Mutex::Autolock l(mTriggerMutex);
1913 status_t ret;
1914
1915 for (size_t i = 0; i < count; ++i) {
1916 ret = queueTriggerLocked(trigger[i]);
1917
1918 if (ret != OK) {
1919 return ret;
1920 }
1921 }
1922
1923 return OK;
1924}
1925
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001926int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
1927 sp<Camera3Device> d = device.promote();
1928 if (d != NULL) return d->mId;
1929 return 0;
1930}
1931
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001932status_t Camera3Device::RequestThread::queueTriggerLocked(
1933 RequestTrigger trigger) {
1934
1935 uint32_t tag = trigger.metadataTag;
1936 ssize_t index = mTriggerMap.indexOfKey(tag);
1937
1938 switch (trigger.getTagType()) {
1939 case TYPE_BYTE:
1940 // fall-through
1941 case TYPE_INT32:
1942 break;
1943 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001944 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
1945 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001946 return INVALID_OPERATION;
1947 }
1948
1949 /**
1950 * Collect only the latest trigger, since we only have 1 field
1951 * in the request settings per trigger tag, and can't send more than 1
1952 * trigger per request.
1953 */
1954 if (index != NAME_NOT_FOUND) {
1955 mTriggerMap.editValueAt(index) = trigger;
1956 } else {
1957 mTriggerMap.add(tag, trigger);
1958 }
1959
1960 return OK;
1961}
1962
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001963status_t Camera3Device::RequestThread::setRepeatingRequests(
1964 const RequestList &requests) {
1965 Mutex::Autolock l(mRequestLock);
1966 mRepeatingRequests.clear();
1967 mRepeatingRequests.insert(mRepeatingRequests.begin(),
1968 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07001969
1970 unpauseForNewRequests();
1971
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001972 return OK;
1973}
1974
1975status_t Camera3Device::RequestThread::clearRepeatingRequests() {
1976 Mutex::Autolock l(mRequestLock);
1977 mRepeatingRequests.clear();
1978 return OK;
1979}
1980
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001981status_t Camera3Device::RequestThread::clear() {
1982 Mutex::Autolock l(mRequestLock);
1983 mRepeatingRequests.clear();
1984 mRequestQueue.clear();
1985 mTriggerMap.clear();
1986 return OK;
1987}
1988
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001989void Camera3Device::RequestThread::setPaused(bool paused) {
1990 Mutex::Autolock l(mPauseLock);
1991 mDoPause = paused;
1992 mDoPauseSignal.signal();
1993}
1994
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001995status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
1996 int32_t requestId, nsecs_t timeout) {
1997 Mutex::Autolock l(mLatestRequestMutex);
1998 status_t res;
1999 while (mLatestRequestId != requestId) {
2000 nsecs_t startTime = systemTime();
2001
2002 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2003 if (res != OK) return res;
2004
2005 timeout -= (systemTime() - startTime);
2006 }
2007
2008 return OK;
2009}
2010
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002011void Camera3Device::RequestThread::requestExit() {
2012 // Call parent to set up shutdown
2013 Thread::requestExit();
2014 // The exit from any possible waits
2015 mDoPauseSignal.signal();
2016 mRequestSignal.signal();
2017}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002018
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002019bool Camera3Device::RequestThread::threadLoop() {
2020
2021 status_t res;
2022
2023 // Handle paused state.
2024 if (waitIfPaused()) {
2025 return true;
2026 }
2027
2028 // Get work to do
2029
2030 sp<CaptureRequest> nextRequest = waitForNextRequest();
2031 if (nextRequest == NULL) {
2032 return true;
2033 }
2034
2035 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002036 camera3_capture_request_t request = camera3_capture_request_t();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002037 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002038
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002039 // Get the request ID, if any
2040 int requestId;
2041 camera_metadata_entry_t requestIdEntry =
2042 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2043 if (requestIdEntry.count > 0) {
2044 requestId = requestIdEntry.data.i32[0];
2045 } else {
2046 ALOGW("%s: Did not have android.request.id set in the request",
2047 __FUNCTION__);
2048 requestId = NAME_NOT_FOUND;
2049 }
2050
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002051 // Insert any queued triggers (before metadata is locked)
2052 int32_t triggerCount;
2053 res = insertTriggers(nextRequest);
2054 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002055 SET_ERR("RequestThread: Unable to insert triggers "
2056 "(capture request %d, HAL device: %s (%d)",
2057 (mFrameNumber+1), strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002058 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2059 return false;
2060 }
2061 triggerCount = res;
2062
2063 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2064
2065 // If the request is the same as last, or we had triggers last time
2066 if (mPrevRequest != nextRequest || triggersMixedIn) {
2067 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002068 * HAL workaround:
2069 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2070 */
2071 res = addDummyTriggerIds(nextRequest);
2072 if (res != OK) {
2073 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2074 "(capture request %d, HAL device: %s (%d)",
2075 (mFrameNumber+1), strerror(-res), res);
2076 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2077 return false;
2078 }
2079
2080 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002081 * The request should be presorted so accesses in HAL
2082 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2083 */
2084 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002085 request.settings = nextRequest->mSettings.getAndLock();
2086 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002087 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2088
2089 IF_ALOGV() {
2090 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2091 find_camera_metadata_ro_entry(
2092 request.settings,
2093 ANDROID_CONTROL_AF_TRIGGER,
2094 &e
2095 );
2096 if (e.count > 0) {
2097 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2098 __FUNCTION__,
2099 mFrameNumber+1,
2100 e.data.u8[0]);
2101 }
2102 }
2103 } else {
2104 // leave request.settings NULL to indicate 'reuse latest given'
2105 ALOGVV("%s: Request settings are REUSED",
2106 __FUNCTION__);
2107 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002108
2109 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002110
2111 // Fill in buffers
2112
2113 if (nextRequest->mInputStream != NULL) {
2114 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002115 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002116 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002117 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002118 " %s (%d)", strerror(-res), res);
2119 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2120 return true;
2121 }
2122 } else {
2123 request.input_buffer = NULL;
2124 }
2125
2126 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2127 nextRequest->mOutputStreams.size());
2128 request.output_buffers = outputBuffers.array();
2129 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2130 res = nextRequest->mOutputStreams.editItemAt(i)->
2131 getBuffer(&outputBuffers.editItemAt(i));
2132 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002133 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2134 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002135 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2136 return true;
2137 }
2138 request.num_output_buffers++;
2139 }
2140
2141 request.frame_number = mFrameNumber++;
2142
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002143 // Log request in the in-flight queue
2144 sp<Camera3Device> parent = mParent.promote();
2145 if (parent == NULL) {
2146 CLOGE("RequestThread: Parent is gone");
2147 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2148 return false;
2149 }
2150
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002151 res = parent->registerInFlight(request.frame_number, requestId,
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002152 request.num_output_buffers);
2153 if (res != OK) {
2154 SET_ERR("RequestThread: Unable to register new in-flight request:"
2155 " %s (%d)", strerror(-res), res);
2156 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2157 return false;
2158 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002159
Zhijun Hecc27e112013-10-03 16:12:43 -07002160 // Inform waitUntilRequestProcessed thread of a new request ID
2161 {
2162 Mutex::Autolock al(mLatestRequestMutex);
2163
2164 mLatestRequestId = requestId;
2165 mLatestRequestSignal.signal();
2166 }
2167
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002168 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002169 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2170 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002171 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002172 ATRACE_END();
2173
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002174 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002175 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002176 " device: %s (%d)", request.frame_number, strerror(-res), res);
2177 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2178 return false;
2179 }
2180
Igor Murashkin1e479c02013-09-06 16:55:14 -07002181 // Update the latest request sent to HAL
2182 if (request.settings != NULL) { // Don't update them if they were unchanged
2183 Mutex::Autolock al(mLatestRequestMutex);
2184
2185 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2186 mLatestRequest.acquire(cloned);
2187 }
2188
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002189 if (request.settings != NULL) {
2190 nextRequest->mSettings.unlock(request.settings);
2191 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002192
2193 // Remove any previously queued triggers (after unlock)
2194 res = removeTriggers(mPrevRequest);
2195 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002196 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002197 "(capture request %d, HAL device: %s (%d)",
2198 request.frame_number, strerror(-res), res);
2199 return false;
2200 }
2201 mPrevTriggers = triggerCount;
2202
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002203 // Return input buffer back to framework
2204 if (request.input_buffer != NULL) {
2205 Camera3Stream *stream =
2206 Camera3Stream::cast(request.input_buffer->stream);
2207 res = stream->returnInputBuffer(*(request.input_buffer));
2208 // Note: stream may be deallocated at this point, if this buffer was the
2209 // last reference to it.
2210 if (res != OK) {
2211 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2212 " its stream:%s (%d)", __FUNCTION__,
2213 request.frame_number, strerror(-res), res);
2214 // TODO: Report error upstream
2215 }
2216 }
2217
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002218 return true;
2219}
2220
Igor Murashkin1e479c02013-09-06 16:55:14 -07002221CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2222 Mutex::Autolock al(mLatestRequestMutex);
2223
2224 ALOGV("RequestThread::%s", __FUNCTION__);
2225
2226 return mLatestRequest;
2227}
2228
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002229void Camera3Device::RequestThread::cleanUpFailedRequest(
2230 camera3_capture_request_t &request,
2231 sp<CaptureRequest> &nextRequest,
2232 Vector<camera3_stream_buffer_t> &outputBuffers) {
2233
2234 if (request.settings != NULL) {
2235 nextRequest->mSettings.unlock(request.settings);
2236 }
2237 if (request.input_buffer != NULL) {
2238 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002239 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002240 }
2241 for (size_t i = 0; i < request.num_output_buffers; i++) {
2242 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2243 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2244 outputBuffers[i], 0);
2245 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002246}
2247
2248sp<Camera3Device::CaptureRequest>
2249 Camera3Device::RequestThread::waitForNextRequest() {
2250 status_t res;
2251 sp<CaptureRequest> nextRequest;
2252
2253 // Optimized a bit for the simple steady-state case (single repeating
2254 // request), to avoid putting that request in the queue temporarily.
2255 Mutex::Autolock l(mRequestLock);
2256
2257 while (mRequestQueue.empty()) {
2258 if (!mRepeatingRequests.empty()) {
2259 // Always atomically enqueue all requests in a repeating request
2260 // list. Guarantees a complete in-sequence set of captures to
2261 // application.
2262 const RequestList &requests = mRepeatingRequests;
2263 RequestList::const_iterator firstRequest =
2264 requests.begin();
2265 nextRequest = *firstRequest;
2266 mRequestQueue.insert(mRequestQueue.end(),
2267 ++firstRequest,
2268 requests.end());
2269 // No need to wait any longer
2270 break;
2271 }
2272
2273 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002275 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2276 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002277 Mutex::Autolock pl(mPauseLock);
2278 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002279 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002280 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002281 // Let the tracker know
2282 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2283 if (statusTracker != 0) {
2284 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2285 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002286 }
2287 // Stop waiting for now and let thread management happen
2288 return NULL;
2289 }
2290 }
2291
2292 if (nextRequest == NULL) {
2293 // Don't have a repeating request already in hand, so queue
2294 // must have an entry now.
2295 RequestList::iterator firstRequest =
2296 mRequestQueue.begin();
2297 nextRequest = *firstRequest;
2298 mRequestQueue.erase(firstRequest);
2299 }
2300
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002301 // In case we've been unpaused by setPaused clearing mDoPause, need to
2302 // update internal pause state (capture/setRepeatingRequest unpause
2303 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002304 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002305 if (mPaused) {
2306 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2307 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2308 if (statusTracker != 0) {
2309 statusTracker->markComponentActive(mStatusId);
2310 }
2311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002312 mPaused = false;
2313
2314 // Check if we've reconfigured since last time, and reset the preview
2315 // request if so. Can't use 'NULL request == repeat' across configure calls.
2316 if (mReconfigured) {
2317 mPrevRequest.clear();
2318 mReconfigured = false;
2319 }
2320
2321 return nextRequest;
2322}
2323
2324bool Camera3Device::RequestThread::waitIfPaused() {
2325 status_t res;
2326 Mutex::Autolock l(mPauseLock);
2327 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002328 if (mPaused == false) {
2329 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002330 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2331 // Let the tracker know
2332 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2333 if (statusTracker != 0) {
2334 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2335 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002336 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002337
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002338 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002339 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002340 return true;
2341 }
2342 }
2343 // We don't set mPaused to false here, because waitForNextRequest needs
2344 // to further manage the paused state in case of starvation.
2345 return false;
2346}
2347
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002348void Camera3Device::RequestThread::unpauseForNewRequests() {
2349 // With work to do, mark thread as unpaused.
2350 // If paused by request (setPaused), don't resume, to avoid
2351 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002352 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002353 Mutex::Autolock p(mPauseLock);
2354 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002355 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2356 if (mPaused) {
2357 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2358 if (statusTracker != 0) {
2359 statusTracker->markComponentActive(mStatusId);
2360 }
2361 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002362 mPaused = false;
2363 }
2364}
2365
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002366void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2367 sp<Camera3Device> parent = mParent.promote();
2368 if (parent != NULL) {
2369 va_list args;
2370 va_start(args, fmt);
2371
2372 parent->setErrorStateV(fmt, args);
2373
2374 va_end(args);
2375 }
2376}
2377
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002378status_t Camera3Device::RequestThread::insertTriggers(
2379 const sp<CaptureRequest> &request) {
2380
2381 Mutex::Autolock al(mTriggerMutex);
2382
2383 CameraMetadata &metadata = request->mSettings;
2384 size_t count = mTriggerMap.size();
2385
2386 for (size_t i = 0; i < count; ++i) {
2387 RequestTrigger trigger = mTriggerMap.valueAt(i);
2388
2389 uint32_t tag = trigger.metadataTag;
2390 camera_metadata_entry entry = metadata.find(tag);
2391
2392 if (entry.count > 0) {
2393 /**
2394 * Already has an entry for this trigger in the request.
2395 * Rewrite it with our requested trigger value.
2396 */
2397 RequestTrigger oldTrigger = trigger;
2398
2399 oldTrigger.entryValue = entry.data.u8[0];
2400
2401 mTriggerReplacedMap.add(tag, oldTrigger);
2402 } else {
2403 /**
2404 * More typical, no trigger entry, so we just add it
2405 */
2406 mTriggerRemovedMap.add(tag, trigger);
2407 }
2408
2409 status_t res;
2410
2411 switch (trigger.getTagType()) {
2412 case TYPE_BYTE: {
2413 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2414 res = metadata.update(tag,
2415 &entryValue,
2416 /*count*/1);
2417 break;
2418 }
2419 case TYPE_INT32:
2420 res = metadata.update(tag,
2421 &trigger.entryValue,
2422 /*count*/1);
2423 break;
2424 default:
2425 ALOGE("%s: Type not supported: 0x%x",
2426 __FUNCTION__,
2427 trigger.getTagType());
2428 return INVALID_OPERATION;
2429 }
2430
2431 if (res != OK) {
2432 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2433 ", value %d", __FUNCTION__, trigger.getTagName(),
2434 trigger.entryValue);
2435 return res;
2436 }
2437
2438 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2439 trigger.getTagName(),
2440 trigger.entryValue);
2441 }
2442
2443 mTriggerMap.clear();
2444
2445 return count;
2446}
2447
2448status_t Camera3Device::RequestThread::removeTriggers(
2449 const sp<CaptureRequest> &request) {
2450 Mutex::Autolock al(mTriggerMutex);
2451
2452 CameraMetadata &metadata = request->mSettings;
2453
2454 /**
2455 * Replace all old entries with their old values.
2456 */
2457 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2458 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2459
2460 status_t res;
2461
2462 uint32_t tag = trigger.metadataTag;
2463 switch (trigger.getTagType()) {
2464 case TYPE_BYTE: {
2465 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2466 res = metadata.update(tag,
2467 &entryValue,
2468 /*count*/1);
2469 break;
2470 }
2471 case TYPE_INT32:
2472 res = metadata.update(tag,
2473 &trigger.entryValue,
2474 /*count*/1);
2475 break;
2476 default:
2477 ALOGE("%s: Type not supported: 0x%x",
2478 __FUNCTION__,
2479 trigger.getTagType());
2480 return INVALID_OPERATION;
2481 }
2482
2483 if (res != OK) {
2484 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2485 ", trigger value %d", __FUNCTION__,
2486 trigger.getTagName(), trigger.entryValue);
2487 return res;
2488 }
2489 }
2490 mTriggerReplacedMap.clear();
2491
2492 /**
2493 * Remove all new entries.
2494 */
2495 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2496 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2497 status_t res = metadata.erase(trigger.metadataTag);
2498
2499 if (res != OK) {
2500 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2501 ", trigger value %d", __FUNCTION__,
2502 trigger.getTagName(), trigger.entryValue);
2503 return res;
2504 }
2505 }
2506 mTriggerRemovedMap.clear();
2507
2508 return OK;
2509}
2510
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002511status_t Camera3Device::RequestThread::addDummyTriggerIds(
2512 const sp<CaptureRequest> &request) {
2513 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2514 static const int32_t dummyTriggerId = 1;
2515 status_t res;
2516
2517 CameraMetadata &metadata = request->mSettings;
2518
2519 // If AF trigger is active, insert a dummy AF trigger ID if none already
2520 // exists
2521 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2522 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2523 if (afTrigger.count > 0 &&
2524 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2525 afId.count == 0) {
2526 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2527 if (res != OK) return res;
2528 }
2529
2530 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2531 // if none already exists
2532 camera_metadata_entry pcTrigger =
2533 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2534 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2535 if (pcTrigger.count > 0 &&
2536 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2537 pcId.count == 0) {
2538 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2539 &dummyTriggerId, 1);
2540 if (res != OK) return res;
2541 }
2542
2543 return OK;
2544}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002545
2546
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002547/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002548 * Static callback forwarding methods from HAL to instance
2549 */
2550
2551void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2552 const camera3_capture_result *result) {
2553 Camera3Device *d =
2554 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2555 d->processCaptureResult(result);
2556}
2557
2558void Camera3Device::sNotify(const camera3_callback_ops *cb,
2559 const camera3_notify_msg *msg) {
2560 Camera3Device *d =
2561 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2562 d->notify(msg);
2563}
2564
2565}; // namespace android