blob: a5839ae14d4789a67fc94b3ea6e75d6f2154a8e5 [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
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080051
52using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053
54namespace android {
55
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056Camera3Device::Camera3Device(int id):
57 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080058 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070059 mStatus(STATUS_UNINITIALIZED),
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -070060 mUsePartialResultQuirk(false),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070061 mNextResultFrameNumber(0),
62 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070063 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064{
65 ATRACE_CALL();
66 camera3_callback_ops::notify = &sNotify;
67 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
68 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
69}
70
71Camera3Device::~Camera3Device()
72{
73 ATRACE_CALL();
74 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
75 disconnect();
76}
77
Igor Murashkin71381052013-03-04 14:53:08 -080078int Camera3Device::getId() const {
79 return mId;
80}
81
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080082/**
83 * CameraDeviceBase interface
84 */
85
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086status_t Camera3Device::initialize(camera_module_t *module)
87{
88 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070089 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080090 Mutex::Autolock l(mLock);
91
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080092 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070094 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 return INVALID_OPERATION;
96 }
97
98 /** Open HAL device */
99
100 status_t res;
101 String8 deviceName = String8::format("%d", mId);
102
103 camera3_device_t *device;
104
Zhijun He213ce792013-11-19 08:45:15 -0800105 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106 res = module->common.methods->open(&module->common, deviceName.string(),
107 reinterpret_cast<hw_device_t**>(&device));
Zhijun He213ce792013-11-19 08:45:15 -0800108 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109
110 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700111 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 return res;
113 }
114
115 /** Cross-check device version */
116
Zhijun He95dd5ba2014-03-26 18:18:00 -0700117 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700118 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700119 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700120 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121 device->common.version);
122 device->common.close(&device->common);
123 return BAD_VALUE;
124 }
125
126 camera_info info;
127 res = module->get_camera_info(mId, &info);
128 if (res != OK) return res;
129
130 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700131 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
132 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700133 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800134 device->common.close(&device->common);
135 return BAD_VALUE;
136 }
137
138 /** Initialize device with callback functions */
139
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700140 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800141 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700142 ATRACE_END();
143
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800144 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700145 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
146 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800147 device->common.close(&device->common);
148 return BAD_VALUE;
149 }
150
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700151 /** Start up status tracker thread */
152 mStatusTracker = new StatusTracker(this);
153 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
154 if (res != OK) {
155 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
156 strerror(-res), res);
157 device->common.close(&device->common);
158 mStatusTracker.clear();
159 return res;
160 }
161
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800162 /** Start up request queue thread */
163
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700164 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800165 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700167 SET_ERR_L("Unable to start request queue thread: %s (%d)",
168 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800169 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 return res;
172 }
173
174 /** Everything is good to go */
175
176 mDeviceInfo = info.static_camera_characteristics;
177 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700178 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800179 mNextStreamId = 0;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700180 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700181 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700183 /** Check for quirks */
184
185 // Will the HAL be sending in early partial result metadata?
186 camera_metadata_entry partialResultsQuirk =
187 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
188 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
189 mUsePartialResultQuirk = true;
190 }
191
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 return OK;
193}
194
195status_t Camera3Device::disconnect() {
196 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700197 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800198
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800199 ALOGV("%s: E", __FUNCTION__);
200
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700201 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800202
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700203 {
204 Mutex::Autolock l(mLock);
205 if (mStatus == STATUS_UNINITIALIZED) return res;
206
207 if (mStatus == STATUS_ACTIVE ||
208 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
209 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700210 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700212 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700213 } else {
214 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
215 if (res != OK) {
216 SET_ERR_L("Timeout waiting for HAL to drain");
217 // Continue to close device even in case of error
218 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700219 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800220 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800221
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700222 if (mStatus == STATUS_ERROR) {
223 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700224 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225
226 if (mStatusTracker != NULL) {
227 mStatusTracker->requestExit();
228 }
229
230 if (mRequestThread != NULL) {
231 mRequestThread->requestExit();
232 }
233
234 mOutputStreams.clear();
235 mInputStream.clear();
236 }
237
238 // Joining done without holding mLock, otherwise deadlocks may ensue
239 // as the threads try to access parent state
240 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
241 // HAL may be in a bad state, so waiting for request thread
242 // (which may be stuck in the HAL processCaptureRequest call)
243 // could be dangerous.
244 mRequestThread->join();
245 }
246
247 if (mStatusTracker != NULL) {
248 mStatusTracker->join();
249 }
250
251 {
252 Mutex::Autolock l(mLock);
253
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800254 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800256
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700257 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800258 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800260 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700261 mHal3Device = NULL;
262 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 mStatus = STATUS_UNINITIALIZED;
265 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800266
267 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700268 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800269}
270
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271// For dumping/debugging only -
272// try to acquire a lock a few times, eventually give up to proceed with
273// debug/dump operations
274bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
275 bool gotLock = false;
276 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
277 if (lock.tryLock() == NO_ERROR) {
278 gotLock = true;
279 break;
280 } else {
281 usleep(kDumpSleepDuration);
282 }
283 }
284 return gotLock;
285}
286
Zhijun Hef7da0962014-04-24 13:27:56 -0700287ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
288 // TODO: replace below with availableStreamConfiguration for HAL3.2+.
289 camera_metadata_ro_entry availableJpegSizes =
290 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
291 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
292 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
293 __FUNCTION__, mId);
294 return BAD_VALUE;
295 }
296
297 // Get max jpeg size (area-wise).
298 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
299 bool foundMax = false;
300 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
301 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
302 > (maxJpegWidth * maxJpegHeight)) {
303 maxJpegWidth = availableJpegSizes.data.i32[i];
304 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
305 foundMax = true;
306 }
307 }
308 if (!foundMax) {
309 return BAD_VALUE;
310 }
311
312 // Get max jpeg buffer size
313 ssize_t maxJpegBufferSize = 0;
314 camera_metadata_ro_entry jpegMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
315 if (jpegMaxSize.count == 0) {
316 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
317 return BAD_VALUE;
318 }
319 maxJpegBufferSize = jpegMaxSize.data.i32[0];
320
321 // Calculate final jpeg buffer size for the given resolution.
322 float scaleFactor = ((float) (width * height)) / (maxJpegWidth * maxJpegHeight);
323 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
324 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
325 if (jpegBufferSize > maxJpegBufferSize) {
326 jpegBufferSize = maxJpegBufferSize;
327 } else if (jpegBufferSize < kMinJpegBufferSize) {
328 jpegBufferSize = kMinJpegBufferSize;
329 }
330
331 return jpegBufferSize;
332}
333
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800334status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
335 ATRACE_CALL();
336 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700337
338 // Try to lock, but continue in case of failure (to avoid blocking in
339 // deadlocks)
340 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
341 bool gotLock = tryLockSpinRightRound(mLock);
342
343 ALOGW_IF(!gotInterfaceLock,
344 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
345 mId, __FUNCTION__);
346 ALOGW_IF(!gotLock,
347 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
348 mId, __FUNCTION__);
349
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800350 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800351
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800352 const char *status =
353 mStatus == STATUS_ERROR ? "ERROR" :
354 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700355 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
356 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800357 mStatus == STATUS_ACTIVE ? "ACTIVE" :
358 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700359
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800360 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700361 if (mStatus == STATUS_ERROR) {
362 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
363 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800364 lines.appendFormat(" Stream configuration:\n");
365
366 if (mInputStream != NULL) {
367 write(fd, lines.string(), lines.size());
368 mInputStream->dump(fd, args);
369 } else {
370 lines.appendFormat(" No input stream.\n");
371 write(fd, lines.string(), lines.size());
372 }
373 for (size_t i = 0; i < mOutputStreams.size(); i++) {
374 mOutputStreams[i]->dump(fd,args);
375 }
376
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700377 lines = String8(" In-flight requests:\n");
378 if (mInFlightMap.size() == 0) {
379 lines.append(" None\n");
380 } else {
381 for (size_t i = 0; i < mInFlightMap.size(); i++) {
382 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700383 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700384 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
385 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
386 r.numBuffersLeft);
387 }
388 }
389 write(fd, lines.string(), lines.size());
390
Igor Murashkin1e479c02013-09-06 16:55:14 -0700391 {
392 lines = String8(" Last request sent:\n");
393 write(fd, lines.string(), lines.size());
394
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700395 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700396 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
397 }
398
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800399 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700400 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800401 write(fd, lines.string(), lines.size());
402 mHal3Device->ops->dump(mHal3Device, fd);
403 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800404
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700405 if (gotLock) mLock.unlock();
406 if (gotInterfaceLock) mInterfaceLock.unlock();
407
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800408 return OK;
409}
410
411const CameraMetadata& Camera3Device::info() const {
412 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800413 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
414 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700415 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800416 mStatus == STATUS_ERROR ?
417 "when in error state" : "before init");
418 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800419 return mDeviceInfo;
420}
421
Jianing Wei90e59c92014-03-12 18:29:36 -0700422status_t Camera3Device::checkStatusOkToCaptureLocked() {
423 switch (mStatus) {
424 case STATUS_ERROR:
425 CLOGE("Device has encountered a serious error");
426 return INVALID_OPERATION;
427 case STATUS_UNINITIALIZED:
428 CLOGE("Device not initialized");
429 return INVALID_OPERATION;
430 case STATUS_UNCONFIGURED:
431 case STATUS_CONFIGURED:
432 case STATUS_ACTIVE:
433 // OK
434 break;
435 default:
436 SET_ERR_L("Unexpected status: %d", mStatus);
437 return INVALID_OPERATION;
438 }
439 return OK;
440}
441
442status_t Camera3Device::convertMetadataListToRequestListLocked(
443 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
444 if (requestList == NULL) {
445 CLOGE("requestList cannot be NULL.");
446 return BAD_VALUE;
447 }
448
Jianing Weicb0652e2014-03-12 18:29:36 -0700449 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700450 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
451 it != metadataList.end(); ++it) {
452 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
453 if (newRequest == 0) {
454 CLOGE("Can't create capture request");
455 return BAD_VALUE;
456 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700457
458 // Setup burst Id and request Id
459 newRequest->mResultExtras.burstId = burstId++;
460 if (it->exists(ANDROID_REQUEST_ID)) {
461 if (it->find(ANDROID_REQUEST_ID).count == 0) {
462 CLOGE("RequestID entry exists; but must not be empty in metadata");
463 return BAD_VALUE;
464 }
465 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
466 } else {
467 CLOGE("RequestID does not exist in metadata");
468 return BAD_VALUE;
469 }
470
Jianing Wei90e59c92014-03-12 18:29:36 -0700471 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700472
473 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700474 }
475 return OK;
476}
477
Jianing Weicb0652e2014-03-12 18:29:36 -0700478status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800479 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800480
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700481 List<const CameraMetadata> requests;
482 requests.push_back(request);
483 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800484}
485
Jianing Wei90e59c92014-03-12 18:29:36 -0700486status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700487 const List<const CameraMetadata> &requests, bool repeating,
488 /*out*/
489 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700490 ATRACE_CALL();
491 Mutex::Autolock il(mInterfaceLock);
492 Mutex::Autolock l(mLock);
493
494 status_t res = checkStatusOkToCaptureLocked();
495 if (res != OK) {
496 // error logged by previous call
497 return res;
498 }
499
500 RequestList requestList;
501
502 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
503 if (res != OK) {
504 // error logged by previous call
505 return res;
506 }
507
508 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700509 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700510 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700511 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700512 }
513
514 if (res == OK) {
515 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
516 if (res != OK) {
517 SET_ERR_L("Can't transition to active in %f seconds!",
518 kActiveTimeout/1e9);
519 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700520 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
521 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700522 } else {
523 CLOGE("Cannot queue request. Impossible.");
524 return BAD_VALUE;
525 }
526
527 return res;
528}
529
Jianing Weicb0652e2014-03-12 18:29:36 -0700530status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
531 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700532 ATRACE_CALL();
533
Jianing Weicb0652e2014-03-12 18:29:36 -0700534 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700535}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800536
Jianing Weicb0652e2014-03-12 18:29:36 -0700537status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
538 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800539 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800540
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700541 List<const CameraMetadata> requests;
542 requests.push_back(request);
543 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800544}
545
Jianing Weicb0652e2014-03-12 18:29:36 -0700546status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
547 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700548 ATRACE_CALL();
549
Jianing Weicb0652e2014-03-12 18:29:36 -0700550 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700551}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800552
553sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
554 const CameraMetadata &request) {
555 status_t res;
556
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700557 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800558 res = configureStreamsLocked();
559 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700560 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800561 return NULL;
562 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700563 if (mStatus == STATUS_UNCONFIGURED) {
564 CLOGE("No streams configured");
565 return NULL;
566 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800567 }
568
569 sp<CaptureRequest> newRequest = createCaptureRequest(request);
570 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800571}
572
Jianing Weicb0652e2014-03-12 18:29:36 -0700573status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800574 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700575 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800577
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800578 switch (mStatus) {
579 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700580 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800581 return INVALID_OPERATION;
582 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700583 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800584 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700585 case STATUS_UNCONFIGURED:
586 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800587 case STATUS_ACTIVE:
588 // OK
589 break;
590 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700591 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800592 return INVALID_OPERATION;
593 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700594 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700595
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700596 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800597}
598
599status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
600 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700601 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800602
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700603 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800604}
605
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700606status_t Camera3Device::createInputStream(
607 uint32_t width, uint32_t height, int format, int *id) {
608 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700609 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700610 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700611 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
612 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700613
614 status_t res;
615 bool wasActive = false;
616
617 switch (mStatus) {
618 case STATUS_ERROR:
619 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
620 return INVALID_OPERATION;
621 case STATUS_UNINITIALIZED:
622 ALOGE("%s: Device not initialized", __FUNCTION__);
623 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700624 case STATUS_UNCONFIGURED:
625 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700626 // OK
627 break;
628 case STATUS_ACTIVE:
629 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700630 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700631 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700632 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700633 return res;
634 }
635 wasActive = true;
636 break;
637 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700639 return INVALID_OPERATION;
640 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700641 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700642
643 if (mInputStream != 0) {
644 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
645 return INVALID_OPERATION;
646 }
647
648 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
649 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700650 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700651
652 mInputStream = newStream;
653
654 *id = mNextStreamId++;
655
656 // Continue captures if active at start
657 if (wasActive) {
658 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
659 res = configureStreamsLocked();
660 if (res != OK) {
661 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
662 __FUNCTION__, mNextStreamId, strerror(-res), res);
663 return res;
664 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700666 }
667
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700668 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700669 return OK;
670}
671
Igor Murashkin2fba5842013-04-22 14:03:54 -0700672
673status_t Camera3Device::createZslStream(
674 uint32_t width, uint32_t height,
675 int depth,
676 /*out*/
677 int *id,
678 sp<Camera3ZslStream>* zslStream) {
679 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700680 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700681 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700682 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
683 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700684
685 status_t res;
686 bool wasActive = false;
687
688 switch (mStatus) {
689 case STATUS_ERROR:
690 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
691 return INVALID_OPERATION;
692 case STATUS_UNINITIALIZED:
693 ALOGE("%s: Device not initialized", __FUNCTION__);
694 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 case STATUS_UNCONFIGURED:
696 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700697 // OK
698 break;
699 case STATUS_ACTIVE:
700 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700702 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700704 return res;
705 }
706 wasActive = true;
707 break;
708 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700709 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700710 return INVALID_OPERATION;
711 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700712 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700713
714 if (mInputStream != 0) {
715 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
716 return INVALID_OPERATION;
717 }
718
719 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
720 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700722
723 res = mOutputStreams.add(mNextStreamId, newStream);
724 if (res < 0) {
725 ALOGE("%s: Can't add new stream to set: %s (%d)",
726 __FUNCTION__, strerror(-res), res);
727 return res;
728 }
729 mInputStream = newStream;
730
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530731 mNeedConfig = true;
732
Igor Murashkin2fba5842013-04-22 14:03:54 -0700733 *id = mNextStreamId++;
734 *zslStream = newStream;
735
736 // Continue captures if active at start
737 if (wasActive) {
738 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
739 res = configureStreamsLocked();
740 if (res != OK) {
741 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
742 __FUNCTION__, mNextStreamId, strerror(-res), res);
743 return res;
744 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700745 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700746 }
747
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 return OK;
750}
751
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800752status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
753 uint32_t width, uint32_t height, int format, size_t size, int *id) {
754 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700755 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800756 Mutex::Autolock l(mLock);
Colin Crosse5729fa2014-03-21 15:04:25 -0700757 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu",
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700758 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800759
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760 status_t res;
761 bool wasActive = false;
762
763 switch (mStatus) {
764 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700765 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800766 return INVALID_OPERATION;
767 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700768 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800769 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700770 case STATUS_UNCONFIGURED:
771 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800772 // OK
773 break;
774 case STATUS_ACTIVE:
775 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800777 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700778 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800779 return res;
780 }
781 wasActive = true;
782 break;
783 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700784 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800785 return INVALID_OPERATION;
786 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700787 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788
789 sp<Camera3OutputStream> newStream;
790 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700791 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
792 if (jpegBufferSize > 0) {
793 ALOGV("%s: Overwrite Jpeg output buffer size from %zu to %zu",
794 __FUNCTION__, size, jpegBufferSize);
795 } else {
796 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
797 return BAD_VALUE;
798 }
799
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800800 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700801 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800802 } else {
803 newStream = new Camera3OutputStream(mNextStreamId, consumer,
804 width, height, format);
805 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700806 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800807
808 res = mOutputStreams.add(mNextStreamId, newStream);
809 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700810 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 return res;
812 }
813
814 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700815 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800816
817 // Continue captures if active at start
818 if (wasActive) {
819 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
820 res = configureStreamsLocked();
821 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700822 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
823 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return res;
825 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700826 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800827 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700828 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800829 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800830}
831
832status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
833 ATRACE_CALL();
834 (void)outputId; (void)id;
835
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700836 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800837 return INVALID_OPERATION;
838}
839
840
841status_t Camera3Device::getStreamInfo(int id,
842 uint32_t *width, uint32_t *height, uint32_t *format) {
843 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700844 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800845 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800846
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800847 switch (mStatus) {
848 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700849 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 return INVALID_OPERATION;
851 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700852 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700854 case STATUS_UNCONFIGURED:
855 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856 case STATUS_ACTIVE:
857 // OK
858 break;
859 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return INVALID_OPERATION;
862 }
863
864 ssize_t idx = mOutputStreams.indexOfKey(id);
865 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700866 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867 return idx;
868 }
869
870 if (width) *width = mOutputStreams[idx]->getWidth();
871 if (height) *height = mOutputStreams[idx]->getHeight();
872 if (format) *format = mOutputStreams[idx]->getFormat();
873
874 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800875}
876
877status_t Camera3Device::setStreamTransform(int id,
878 int transform) {
879 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700880 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800881 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800882
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800883 switch (mStatus) {
884 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700885 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800886 return INVALID_OPERATION;
887 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700888 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 case STATUS_UNCONFIGURED:
891 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 case STATUS_ACTIVE:
893 // OK
894 break;
895 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700896 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 return INVALID_OPERATION;
898 }
899
900 ssize_t idx = mOutputStreams.indexOfKey(id);
901 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Stream %d does not exist",
903 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 return BAD_VALUE;
905 }
906
907 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800908}
909
910status_t Camera3Device::deleteStream(int id) {
911 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 Mutex::Autolock l(mLock);
914 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800915
Igor Murashkine2172be2013-05-28 15:31:39 -0700916 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
917
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 // CameraDevice semantics require device to already be idle before
919 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700920 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700921 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
922 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 }
924
Igor Murashkin2fba5842013-04-22 14:03:54 -0700925 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800926 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800927 if (mInputStream != NULL && id == mInputStream->getId()) {
928 deletedStream = mInputStream;
929 mInputStream.clear();
930 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800931 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700932 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 return BAD_VALUE;
934 }
Zhijun He5f446352014-01-22 09:49:33 -0800935 }
936
937 // Delete output stream or the output part of a bi-directional stream.
938 if (outputStreamIdx != NAME_NOT_FOUND) {
939 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800940 mOutputStreams.removeItem(id);
941 }
942
943 // Free up the stream endpoint so that it can be used by some other stream
944 res = deletedStream->disconnect();
945 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700946 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 // fall through since we want to still list the stream as deleted.
948 }
949 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700950 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800951
952 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800953}
954
955status_t Camera3Device::deleteReprocessStream(int id) {
956 ATRACE_CALL();
957 (void)id;
958
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700959 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800960 return INVALID_OPERATION;
961}
962
963
964status_t Camera3Device::createDefaultRequest(int templateId,
965 CameraMetadata *request) {
966 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700967 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700968 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 Mutex::Autolock l(mLock);
970
971 switch (mStatus) {
972 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700973 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 return INVALID_OPERATION;
975 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700976 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800977 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700978 case STATUS_UNCONFIGURED:
979 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800980 case STATUS_ACTIVE:
981 // OK
982 break;
983 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700984 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800985 return INVALID_OPERATION;
986 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800987
988 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700989 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800990 rawRequest = mHal3Device->ops->construct_default_request_settings(
991 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700992 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700993 if (rawRequest == NULL) {
994 SET_ERR_L("HAL is unable to construct default settings for template %d",
995 templateId);
996 return DEAD_OBJECT;
997 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800998 *request = rawRequest;
999
1000 return OK;
1001}
1002
1003status_t Camera3Device::waitUntilDrained() {
1004 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001005 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001007
Zhijun He69a37482014-03-23 18:44:49 -07001008 return waitUntilDrainedLocked();
1009}
1010
1011status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012 switch (mStatus) {
1013 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001014 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001015 ALOGV("%s: Already idle", __FUNCTION__);
1016 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001017 case STATUS_CONFIGURED:
1018 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001019 case STATUS_ERROR:
1020 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001021 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001022 break;
1023 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001024 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 return INVALID_OPERATION;
1026 }
1027
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001028 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1029 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1030 return res;
1031}
1032
1033// Pause to reconfigure
1034status_t Camera3Device::internalPauseAndWaitLocked() {
1035 mRequestThread->setPaused(true);
1036 mPauseStateNotify = true;
1037
1038 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1039 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1040 if (res != OK) {
1041 SET_ERR_L("Can't idle device in %f seconds!",
1042 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 }
1044
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001045 return res;
1046}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001048// Resume after internalPauseAndWaitLocked
1049status_t Camera3Device::internalResumeLocked() {
1050 status_t res;
1051
1052 mRequestThread->setPaused(false);
1053
1054 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1055 if (res != OK) {
1056 SET_ERR_L("Can't transition to active in %f seconds!",
1057 kActiveTimeout/1e9);
1058 }
1059 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001061}
1062
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001063status_t Camera3Device::waitUntilStateThenRelock(bool active,
1064 nsecs_t timeout) {
1065 status_t res = OK;
1066 if (active == (mStatus == STATUS_ACTIVE)) {
1067 // Desired state already reached
1068 return res;
1069 }
1070
1071 bool stateSeen = false;
1072 do {
1073 mRecentStatusUpdates.clear();
1074
1075 res = mStatusChanged.waitRelative(mLock, timeout);
1076 if (res != OK) break;
1077
1078 // Check state change history during wait
1079 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1080 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1081 stateSeen = true;
1082 break;
1083 }
1084 }
1085 } while (!stateSeen);
1086
1087 return res;
1088}
1089
1090
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001091status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1092 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001093 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001094
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001095 if (listener != NULL && mListener != NULL) {
1096 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1097 }
1098 mListener = listener;
1099
1100 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001101}
1102
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001103bool Camera3Device::willNotify3A() {
1104 return false;
1105}
1106
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001107status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001108 status_t res;
1109 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001110
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001111 while (mResultQueue.empty()) {
1112 res = mResultSignal.waitRelative(mOutputLock, timeout);
1113 if (res == TIMED_OUT) {
1114 return res;
1115 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001116 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001117 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001118 return res;
1119 }
1120 }
1121 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001122}
1123
Jianing Weicb0652e2014-03-12 18:29:36 -07001124status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001125 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001126 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001127
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001128 if (mResultQueue.empty()) {
1129 return NOT_ENOUGH_DATA;
1130 }
1131
Jianing Weicb0652e2014-03-12 18:29:36 -07001132 if (frame == NULL) {
1133 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1134 return BAD_VALUE;
1135 }
1136
1137 CaptureResult &result = *(mResultQueue.begin());
1138 frame->mResultExtras = result.mResultExtras;
1139 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001140 mResultQueue.erase(mResultQueue.begin());
1141
1142 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001143}
1144
1145status_t Camera3Device::triggerAutofocus(uint32_t id) {
1146 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001147 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001148
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001149 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1150 // Mix-in this trigger into the next request and only the next request.
1151 RequestTrigger trigger[] = {
1152 {
1153 ANDROID_CONTROL_AF_TRIGGER,
1154 ANDROID_CONTROL_AF_TRIGGER_START
1155 },
1156 {
1157 ANDROID_CONTROL_AF_TRIGGER_ID,
1158 static_cast<int32_t>(id)
1159 },
1160 };
1161
1162 return mRequestThread->queueTrigger(trigger,
1163 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001164}
1165
1166status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1167 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001168 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001169
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001170 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1171 // Mix-in this trigger into the next request and only the next request.
1172 RequestTrigger trigger[] = {
1173 {
1174 ANDROID_CONTROL_AF_TRIGGER,
1175 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1176 },
1177 {
1178 ANDROID_CONTROL_AF_TRIGGER_ID,
1179 static_cast<int32_t>(id)
1180 },
1181 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001182
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001183 return mRequestThread->queueTrigger(trigger,
1184 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185}
1186
1187status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1188 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001189 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001191 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1192 // Mix-in this trigger into the next request and only the next request.
1193 RequestTrigger trigger[] = {
1194 {
1195 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1196 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1197 },
1198 {
1199 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1200 static_cast<int32_t>(id)
1201 },
1202 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001204 return mRequestThread->queueTrigger(trigger,
1205 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001206}
1207
1208status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1209 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1210 ATRACE_CALL();
1211 (void)reprocessStreamId; (void)buffer; (void)listener;
1212
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001213 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214 return INVALID_OPERATION;
1215}
1216
Jianing Weicb0652e2014-03-12 18:29:36 -07001217status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001218 ATRACE_CALL();
1219 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001221
Zhijun He7ef20392014-04-21 16:04:17 -07001222 {
1223 Mutex::Autolock l(mLock);
1224 mRequestThread->clear(/*out*/frameNumber);
1225 }
1226
Zhijun He491e3412013-12-27 10:57:44 -08001227 status_t res;
1228 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1229 res = mHal3Device->ops->flush(mHal3Device);
1230 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001231 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001232 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001233 }
1234
1235 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001236}
1237
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001239 * Methods called by subclasses
1240 */
1241
1242void Camera3Device::notifyStatus(bool idle) {
1243 {
1244 // Need mLock to safely update state and synchronize to current
1245 // state of methods in flight.
1246 Mutex::Autolock l(mLock);
1247 // We can get various system-idle notices from the status tracker
1248 // while starting up. Only care about them if we've actually sent
1249 // in some requests recently.
1250 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1251 return;
1252 }
1253 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1254 idle ? "idle" : "active");
1255 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1256 mRecentStatusUpdates.add(mStatus);
1257 mStatusChanged.signal();
1258
1259 // Skip notifying listener if we're doing some user-transparent
1260 // state changes
1261 if (mPauseStateNotify) return;
1262 }
1263 NotificationListener *listener;
1264 {
1265 Mutex::Autolock l(mOutputLock);
1266 listener = mListener;
1267 }
1268 if (idle && listener != NULL) {
1269 listener->notifyIdle();
1270 }
1271}
1272
1273/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001274 * Camera3Device private methods
1275 */
1276
1277sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1278 const CameraMetadata &request) {
1279 ATRACE_CALL();
1280 status_t res;
1281
1282 sp<CaptureRequest> newRequest = new CaptureRequest;
1283 newRequest->mSettings = request;
1284
1285 camera_metadata_entry_t inputStreams =
1286 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1287 if (inputStreams.count > 0) {
1288 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001289 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001290 CLOGE("Request references unknown input stream %d",
1291 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001292 return NULL;
1293 }
1294 // Lazy completion of stream configuration (allocation/registration)
1295 // on first use
1296 if (mInputStream->isConfiguring()) {
1297 res = mInputStream->finishConfiguration(mHal3Device);
1298 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001299 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001300 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001301 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302 return NULL;
1303 }
1304 }
1305
1306 newRequest->mInputStream = mInputStream;
1307 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1308 }
1309
1310 camera_metadata_entry_t streams =
1311 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1312 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001313 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001314 return NULL;
1315 }
1316
1317 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001318 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001320 CLOGE("Request references unknown stream %d",
1321 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001322 return NULL;
1323 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001324 sp<Camera3OutputStreamInterface> stream =
1325 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001326
1327 // Lazy completion of stream configuration (allocation/registration)
1328 // on first use
1329 if (stream->isConfiguring()) {
1330 res = stream->finishConfiguration(mHal3Device);
1331 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001332 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1333 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 return NULL;
1335 }
1336 }
1337
1338 newRequest->mOutputStreams.push(stream);
1339 }
1340 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1341
1342 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001343}
1344
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001345status_t Camera3Device::configureStreamsLocked() {
1346 ATRACE_CALL();
1347 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001348
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001349 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001350 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 return INVALID_OPERATION;
1352 }
1353
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001354 if (!mNeedConfig) {
1355 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1356 return OK;
1357 }
1358
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001359 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001360 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001361
1362 camera3_stream_configuration config;
1363
1364 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1365
1366 Vector<camera3_stream_t*> streams;
1367 streams.setCapacity(config.num_streams);
1368
1369 if (mInputStream != NULL) {
1370 camera3_stream_t *inputStream;
1371 inputStream = mInputStream->startConfiguration();
1372 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001373 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 return INVALID_OPERATION;
1375 }
1376 streams.add(inputStream);
1377 }
1378
1379 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001380
1381 // Don't configure bidi streams twice, nor add them twice to the list
1382 if (mOutputStreams[i].get() ==
1383 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1384
1385 config.num_streams--;
1386 continue;
1387 }
1388
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 camera3_stream_t *outputStream;
1390 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1391 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001392 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393 return INVALID_OPERATION;
1394 }
1395 streams.add(outputStream);
1396 }
1397
1398 config.streams = streams.editArray();
1399
1400 // Do the HAL configuration; will potentially touch stream
1401 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001402 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001404 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001405
1406 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001407 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1408 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 return res;
1410 }
1411
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001412 // Finish all stream configuration immediately.
1413 // TODO: Try to relax this later back to lazy completion, which should be
1414 // faster
1415
Igor Murashkin073f8572013-05-02 14:59:28 -07001416 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001417 res = mInputStream->finishConfiguration(mHal3Device);
1418 if (res != OK) {
1419 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1420 mInputStream->getId(), strerror(-res), res);
1421 return res;
1422 }
1423 }
1424
1425 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001426 sp<Camera3OutputStreamInterface> outputStream =
1427 mOutputStreams.editValueAt(i);
1428 if (outputStream->isConfiguring()) {
1429 res = outputStream->finishConfiguration(mHal3Device);
1430 if (res != OK) {
1431 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1432 outputStream->getId(), strerror(-res), res);
1433 return res;
1434 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001435 }
1436 }
1437
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438 // Request thread needs to know to avoid using repeat-last-settings protocol
1439 // across configure_streams() calls
1440 mRequestThread->configurationComplete();
1441
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001442 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001443
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001444 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001445
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001446 if (config.num_streams > 0) {
1447 mStatus = STATUS_CONFIGURED;
1448 } else {
1449 mStatus = STATUS_UNCONFIGURED;
1450 }
1451
1452 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1453
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001454 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001455}
1456
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001457void Camera3Device::setErrorState(const char *fmt, ...) {
1458 Mutex::Autolock l(mLock);
1459 va_list args;
1460 va_start(args, fmt);
1461
1462 setErrorStateLockedV(fmt, args);
1463
1464 va_end(args);
1465}
1466
1467void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1468 Mutex::Autolock l(mLock);
1469 setErrorStateLockedV(fmt, args);
1470}
1471
1472void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1473 va_list args;
1474 va_start(args, fmt);
1475
1476 setErrorStateLockedV(fmt, args);
1477
1478 va_end(args);
1479}
1480
1481void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001482 // Print out all error messages to log
1483 String8 errorCause = String8::formatV(fmt, args);
1484 ALOGE("Camera %d: %s", mId, errorCause.string());
1485
1486 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001487 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001488
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001489 // Save stack trace. View by dumping it later.
1490 CameraTraces::saveTrace();
1491 // TODO: consider adding errorCause and client pid/procname
1492
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001493 mErrorCause = errorCause;
1494
1495 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001496 mStatus = STATUS_ERROR;
1497}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498
1499/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001500 * In-flight request management
1501 */
1502
Jianing Weicb0652e2014-03-12 18:29:36 -07001503status_t Camera3Device::registerInFlight(uint32_t frameNumber,
1504 int32_t numBuffers, CaptureResultExtras resultExtras) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001505 ATRACE_CALL();
1506 Mutex::Autolock l(mInFlightLock);
1507
1508 ssize_t res;
Jianing Weicb0652e2014-03-12 18:29:36 -07001509 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001510 if (res < 0) return res;
1511
1512 return OK;
1513}
1514
1515/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001516 * QUIRK(partial results)
1517 * Check if all 3A fields are ready, and send off a partial 3A-only result
1518 * to the output frame queue
1519 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001520bool Camera3Device::processPartial3AQuirk(
Jianing Weicb0652e2014-03-12 18:29:36 -07001521 uint32_t frameNumber,
1522 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001523
1524 // Check if all 3A states are present
1525 // The full list of fields is
1526 // android.control.afMode
1527 // android.control.awbMode
1528 // android.control.aeState
1529 // android.control.awbState
1530 // android.control.afState
1531 // android.control.afTriggerID
1532 // android.control.aePrecaptureID
1533 // TODO: Add android.control.aeMode
1534
1535 bool gotAllStates = true;
1536
1537 uint8_t afMode;
1538 uint8_t awbMode;
1539 uint8_t aeState;
1540 uint8_t afState;
1541 uint8_t awbState;
1542 int32_t afTriggerId;
1543 int32_t aeTriggerId;
1544
1545 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1546 &afMode, frameNumber);
1547
1548 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1549 &awbMode, frameNumber);
1550
1551 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1552 &aeState, frameNumber);
1553
1554 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1555 &afState, frameNumber);
1556
1557 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1558 &awbState, frameNumber);
1559
1560 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_TRIGGER_ID,
1561 &afTriggerId, frameNumber);
1562
1563 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1564 &aeTriggerId, frameNumber);
1565
1566 if (!gotAllStates) return false;
1567
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001568 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001569 "AF state %d, AE state %d, AWB state %d, "
1570 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001571 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001572 afMode, awbMode,
1573 afState, aeState, awbState,
1574 afTriggerId, aeTriggerId);
1575
1576 // Got all states, so construct a minimal result to send
1577 // In addition to the above fields, this means adding in
1578 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001579 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001580 // android.quirks.partialResult
1581
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001582 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001583
1584 Mutex::Autolock l(mOutputLock);
1585
Jianing Weicb0652e2014-03-12 18:29:36 -07001586 CaptureResult captureResult;
1587 captureResult.mResultExtras = resultExtras;
1588 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1589 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1590 // but not limited to CameraDeviceBase::getNextResult
1591 CaptureResult& min3AResult =
1592 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001593
Jianing Weicb0652e2014-03-12 18:29:36 -07001594 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1595 // TODO: This is problematic casting. Need to fix CameraMetadata.
1596 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001597 return false;
1598 }
1599
Jianing Weicb0652e2014-03-12 18:29:36 -07001600 int32_t requestId = resultExtras.requestId;
1601 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001602 &requestId, frameNumber)) {
1603 return false;
1604 }
1605
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001606 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
Jianing Weicb0652e2014-03-12 18:29:36 -07001607 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001608 &partialResult, frameNumber)) {
1609 return false;
1610 }
1611
Jianing Weicb0652e2014-03-12 18:29:36 -07001612 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001613 &afMode, frameNumber)) {
1614 return false;
1615 }
1616
Jianing Weicb0652e2014-03-12 18:29:36 -07001617 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001618 &awbMode, frameNumber)) {
1619 return false;
1620 }
1621
Jianing Weicb0652e2014-03-12 18:29:36 -07001622 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001623 &aeState, frameNumber)) {
1624 return false;
1625 }
1626
Jianing Weicb0652e2014-03-12 18:29:36 -07001627 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001628 &afState, frameNumber)) {
1629 return false;
1630 }
1631
Jianing Weicb0652e2014-03-12 18:29:36 -07001632 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001633 &awbState, frameNumber)) {
1634 return false;
1635 }
1636
Jianing Weicb0652e2014-03-12 18:29:36 -07001637 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001638 &afTriggerId, frameNumber)) {
1639 return false;
1640 }
1641
Jianing Weicb0652e2014-03-12 18:29:36 -07001642 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001643 &aeTriggerId, frameNumber)) {
1644 return false;
1645 }
1646
1647 mResultSignal.signal();
1648
1649 return true;
1650}
1651
1652template<typename T>
1653bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001654 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001655 (void) frameNumber;
1656
1657 camera_metadata_ro_entry_t entry;
1658
1659 entry = result.find(tag);
1660 if (entry.count == 0) {
1661 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1662 mId, frameNumber, get_camera_metadata_tag_name(tag));
1663 return false;
1664 }
1665
1666 if (sizeof(T) == sizeof(uint8_t)) {
1667 *value = entry.data.u8[0];
1668 } else if (sizeof(T) == sizeof(int32_t)) {
1669 *value = entry.data.i32[0];
1670 } else {
1671 ALOGE("%s: Unexpected type", __FUNCTION__);
1672 return false;
1673 }
1674 return true;
1675}
1676
1677template<typename T>
1678bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001679 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001680 if (result.update(tag, value, 1) != NO_ERROR) {
1681 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1682 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1683 frameNumber, get_camera_metadata_tag_name(tag));
1684 return false;
1685 }
1686 return true;
1687}
1688
1689/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001690 * Camera HAL device callback methods
1691 */
1692
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001693void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001694 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001695
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001696 status_t res;
1697
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001698 uint32_t frameNumber = result->frame_number;
1699 if (result->result == NULL && result->num_output_buffers == 0) {
1700 SET_ERR("No result data provided by HAL for frame %d",
1701 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001702 return;
1703 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001704 bool partialResultQuirk = false;
1705 CameraMetadata collectedQuirkResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001706 CaptureResultExtras resultExtras;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001707
Jianing Weicb0652e2014-03-12 18:29:36 -07001708 // Get capture timestamp and resultExtras from list of in-flight requests,
1709 // where it was added by the shutter notification for this frame.
1710 // Then update the in-flight status and remove the in-flight entry if
1711 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001712 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001713 {
1714 Mutex::Autolock l(mInFlightLock);
1715 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1716 if (idx == NAME_NOT_FOUND) {
1717 SET_ERR("Unknown frame number for capture result: %d",
1718 frameNumber);
1719 return;
1720 }
1721 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001722 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1723 ", burstId = %" PRId32,
1724 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1725 request.resultExtras.burstId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001726
1727 // Check if this result carries only partial metadata
1728 if (mUsePartialResultQuirk && result->result != NULL) {
1729 camera_metadata_ro_entry_t partialResultEntry;
1730 res = find_camera_metadata_ro_entry(result->result,
1731 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1732 if (res != NAME_NOT_FOUND &&
1733 partialResultEntry.count > 0 &&
1734 partialResultEntry.data.u8[0] ==
1735 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1736 // A partial result. Flag this as such, and collect this
1737 // set of metadata into the in-flight entry.
1738 partialResultQuirk = true;
1739 request.partialResultQuirk.collectedResult.append(
1740 result->result);
1741 request.partialResultQuirk.collectedResult.erase(
1742 ANDROID_QUIRKS_PARTIAL_RESULT);
1743 // Fire off a 3A-only result if possible
1744 if (!request.partialResultQuirk.haveSent3A) {
1745 request.partialResultQuirk.haveSent3A =
1746 processPartial3AQuirk(frameNumber,
Jianing Weicb0652e2014-03-12 18:29:36 -07001747 request.partialResultQuirk.collectedResult,
1748 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001749 }
1750 }
1751 }
1752
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001753 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001754 resultExtras = request.resultExtras;
Jianing Weicb0652e2014-03-12 18:29:36 -07001755
Zhijun He1d1f8462013-10-02 16:29:51 -07001756 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001757 * One of the following must happen before it's legal to call process_capture_result,
1758 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001759 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1760 * - CAMERA3_MSG_ERROR (expected during flush)
1761 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001762 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001763 SET_ERR("Called before shutter notify for frame %d",
1764 frameNumber);
1765 return;
1766 }
1767
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001768 // Did we get the (final) result metadata for this capture?
1769 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001770 if (request.haveResultMetadata) {
1771 SET_ERR("Called multiple times with metadata for frame %d",
1772 frameNumber);
1773 return;
1774 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001775 if (mUsePartialResultQuirk &&
1776 !request.partialResultQuirk.collectedResult.isEmpty()) {
1777 collectedQuirkResult.acquire(
1778 request.partialResultQuirk.collectedResult);
1779 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001780 request.haveResultMetadata = true;
1781 }
1782
1783 request.numBuffersLeft -= result->num_output_buffers;
1784
1785 if (request.numBuffersLeft < 0) {
1786 SET_ERR("Too many buffers returned for frame %d",
1787 frameNumber);
1788 return;
1789 }
1790
Zhijun He1b05dfc2013-11-21 12:57:51 -08001791 // Check if everything has arrived for this result (buffers and metadata), remove it from
1792 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1793 if ((request.requestStatus != OK) ||
1794 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001795 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001796 mInFlightMap.removeItemsAt(idx, 1);
1797 }
1798
1799 // Sanity check - if we have too many in-flight frames, something has
1800 // likely gone wrong
1801 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001802 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001803 }
1804
1805 }
1806
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001807 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808 bool gotResult = false;
1809 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001810 Mutex::Autolock l(mOutputLock);
1811
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001812 gotResult = true;
1813
Jianing Wei3c76fa32014-04-21 11:34:34 -07001814 // TODO: need to track errors for tighter bounds on expected frame number
1815 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001816 SET_ERR("Out-of-order capture result metadata submitted! "
1817 "(got frame number %d, expecting %d)",
1818 frameNumber, mNextResultFrameNumber);
1819 return;
1820 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001821 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001822
Jianing Weicb0652e2014-03-12 18:29:36 -07001823 CaptureResult captureResult;
1824 captureResult.mResultExtras = resultExtras;
1825 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001826
Jianing Weicb0652e2014-03-12 18:29:36 -07001827 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1828 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001829 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001830 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001831 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001832 } else {
1833 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001834 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001835 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001836
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837 // Append any previous partials to form a complete result
1838 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001839 captureResult.mMetadata.append(collectedQuirkResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001840 }
1841
Jianing Weicb0652e2014-03-12 18:29:36 -07001842 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001844 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001845
1846 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07001847 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001848 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001849 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001850 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001851 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001852 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001853 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001854 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001855 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856 gotResult = false;
1857 }
1858
1859 if (gotResult) {
1860 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07001861 List<CaptureResult>::iterator queuedResult =
1862 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1863 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1864 ", burstId = %" PRId32, __FUNCTION__,
1865 queuedResult->mResultExtras.requestId,
1866 queuedResult->mResultExtras.frameNumber,
1867 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001868 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001869 } // scope for mOutputLock
1870
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001871 // Return completed buffers to their streams with the timestamp
1872
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001873 for (size_t i = 0; i < result->num_output_buffers; i++) {
1874 Camera3Stream *stream =
1875 Camera3Stream::cast(result->output_buffers[i].stream);
1876 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1877 // Note: stream may be deallocated at this point, if this buffer was the
1878 // last reference to it.
1879 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001880 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001881 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001882 }
1883 }
1884
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001885 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001886
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001887 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001888 mResultSignal.signal();
1889 }
1890
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001891}
1892
1893void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001894 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001895 NotificationListener *listener;
1896 {
1897 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001898 listener = mListener;
1899 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001900
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001901 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001902 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001903 return;
1904 }
1905
1906 switch (msg->type) {
1907 case CAMERA3_MSG_ERROR: {
1908 int streamId = 0;
1909 if (msg->message.error.error_stream != NULL) {
1910 Camera3Stream *stream =
1911 Camera3Stream::cast(
1912 msg->message.error.error_stream);
1913 streamId = stream->getId();
1914 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001915 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1916 mId, __FUNCTION__, msg->message.error.frame_number,
1917 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001918
Jianing Weicb0652e2014-03-12 18:29:36 -07001919 CaptureResultExtras resultExtras;
Zhijun He1d1f8462013-10-02 16:29:51 -07001920 // Set request error status for the request in the in-flight tracking
1921 {
1922 Mutex::Autolock l(mInFlightLock);
1923 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1924 if (idx >= 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1926 r.requestStatus = msg->message.error.error_code;
1927 resultExtras = r.resultExtras;
1928 } else {
1929 resultExtras.frameNumber = msg->message.error.frame_number;
1930 ALOGE("Camera %d: %s: cannot find in-flight request on frame %" PRId64
1931 " error", mId, __FUNCTION__, resultExtras.frameNumber);
Zhijun He1d1f8462013-10-02 16:29:51 -07001932 }
1933 }
1934
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001935 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001936 if (msg->message.error.error_code == CAMERA3_MSG_ERROR_DEVICE) {
1937 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1938 resultExtras);
Jianing Weicb0652e2014-03-12 18:29:36 -07001939 }
1940 } else {
1941 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001942 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001943 break;
1944 }
1945 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001946 ssize_t idx;
1947 uint32_t frameNumber = msg->message.shutter.frame_number;
1948 nsecs_t timestamp = msg->message.shutter.timestamp;
1949 // Verify ordering of shutter notifications
1950 {
1951 Mutex::Autolock l(mOutputLock);
Jianing Wei3c76fa32014-04-21 11:34:34 -07001952 // TODO: need to track errors for tighter bounds on expected frame number.
1953 if (frameNumber < mNextShutterFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001954 SET_ERR("Shutter notification out-of-order. Expected "
1955 "notification for frame %d, got frame %d",
1956 mNextShutterFrameNumber, frameNumber);
1957 break;
1958 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001959 mNextShutterFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001960 }
1961
Jianing Weicb0652e2014-03-12 18:29:36 -07001962 CaptureResultExtras resultExtras;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001964 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001965 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001966 {
1967 Mutex::Autolock l(mInFlightLock);
1968 idx = mInFlightMap.indexOfKey(frameNumber);
1969 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001970 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1971 r.captureTimestamp = timestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001972 resultExtras = r.resultExtras;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001973 }
1974 }
1975 if (idx < 0) {
1976 SET_ERR("Shutter notification for non-existent frame number %d",
1977 frameNumber);
1978 break;
1979 }
Colin Crosse5729fa2014-03-21 15:04:25 -07001980 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Jianing Weicb0652e2014-03-12 18:29:36 -07001981 mId, __FUNCTION__, frameNumber, resultExtras.requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001982 // Call listener, if any
1983 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001984 listener->notifyShutter(resultExtras, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001985 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001986 break;
1987 }
1988 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001989 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001990 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001991 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001992}
1993
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001994CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07001995 ALOGV("%s", __FUNCTION__);
1996
Igor Murashkin1e479c02013-09-06 16:55:14 -07001997 CameraMetadata retVal;
1998
1999 if (mRequestThread != NULL) {
2000 retVal = mRequestThread->getLatestRequest();
2001 }
2002
Igor Murashkin1e479c02013-09-06 16:55:14 -07002003 return retVal;
2004}
2005
Jianing Weicb0652e2014-03-12 18:29:36 -07002006
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002007/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002008 * RequestThread inner class methods
2009 */
2010
2011Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002012 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002013 camera3_device_t *hal3Device) :
2014 Thread(false),
2015 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002016 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002017 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002018 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002019 mReconfigured(false),
2020 mDoPause(false),
2021 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002022 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002023 mLatestRequestId(NAME_NOT_FOUND),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002024 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002025 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002026}
2027
2028void Camera3Device::RequestThread::configurationComplete() {
2029 Mutex::Autolock l(mRequestLock);
2030 mReconfigured = true;
2031}
2032
Jianing Wei90e59c92014-03-12 18:29:36 -07002033status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002034 List<sp<CaptureRequest> > &requests,
2035 /*out*/
2036 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002037 Mutex::Autolock l(mRequestLock);
2038 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2039 ++it) {
2040 mRequestQueue.push_back(*it);
2041 }
2042
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002043 if (lastFrameNumber != NULL) {
2044 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2045 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2046 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2047 *lastFrameNumber);
2048 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002049
Jianing Wei90e59c92014-03-12 18:29:36 -07002050 unpauseForNewRequests();
2051
2052 return OK;
2053}
2054
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002055
2056status_t Camera3Device::RequestThread::queueTrigger(
2057 RequestTrigger trigger[],
2058 size_t count) {
2059
2060 Mutex::Autolock l(mTriggerMutex);
2061 status_t ret;
2062
2063 for (size_t i = 0; i < count; ++i) {
2064 ret = queueTriggerLocked(trigger[i]);
2065
2066 if (ret != OK) {
2067 return ret;
2068 }
2069 }
2070
2071 return OK;
2072}
2073
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002074int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2075 sp<Camera3Device> d = device.promote();
2076 if (d != NULL) return d->mId;
2077 return 0;
2078}
2079
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002080status_t Camera3Device::RequestThread::queueTriggerLocked(
2081 RequestTrigger trigger) {
2082
2083 uint32_t tag = trigger.metadataTag;
2084 ssize_t index = mTriggerMap.indexOfKey(tag);
2085
2086 switch (trigger.getTagType()) {
2087 case TYPE_BYTE:
2088 // fall-through
2089 case TYPE_INT32:
2090 break;
2091 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002092 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2093 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002094 return INVALID_OPERATION;
2095 }
2096
2097 /**
2098 * Collect only the latest trigger, since we only have 1 field
2099 * in the request settings per trigger tag, and can't send more than 1
2100 * trigger per request.
2101 */
2102 if (index != NAME_NOT_FOUND) {
2103 mTriggerMap.editValueAt(index) = trigger;
2104 } else {
2105 mTriggerMap.add(tag, trigger);
2106 }
2107
2108 return OK;
2109}
2110
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002111status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002112 const RequestList &requests,
2113 /*out*/
2114 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002115 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002116 if (lastFrameNumber != NULL) {
2117 *lastFrameNumber = mRepeatingLastFrameNumber;
2118 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002119 mRepeatingRequests.clear();
2120 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2121 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002122
2123 unpauseForNewRequests();
2124
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002125 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002126 return OK;
2127}
2128
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002129bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2130 if (mRepeatingRequests.empty()) {
2131 return false;
2132 }
2133 int32_t requestId = requestIn->mResultExtras.requestId;
2134 const RequestList &repeatRequests = mRepeatingRequests;
2135 // All repeating requests are guaranteed to have same id so only check first quest
2136 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2137 return (firstRequest->mResultExtras.requestId == requestId);
2138}
2139
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002140status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141 Mutex::Autolock l(mRequestLock);
2142 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002143 if (lastFrameNumber != NULL) {
2144 *lastFrameNumber = mRepeatingLastFrameNumber;
2145 }
2146 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002147 return OK;
2148}
2149
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002150status_t Camera3Device::RequestThread::clear(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002151 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002152 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002153 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002154
2155 // Decrement repeating frame count for those requests never sent to device
2156 // TODO: Remove this after we have proper error handling so these requests
2157 // will generate an error callback. This might be the only place calling
2158 // isRepeatingRequestLocked. If so, isRepeatingRequestLocked should also be removed.
2159 const RequestList &requests = mRequestQueue;
2160 for (RequestList::const_iterator it = requests.begin();
2161 it != requests.end(); ++it) {
2162 if (isRepeatingRequestLocked(*it)) {
2163 mRepeatingLastFrameNumber--;
2164 }
2165 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002166 mRequestQueue.clear();
2167 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002168 if (lastFrameNumber != NULL) {
2169 *lastFrameNumber = mRepeatingLastFrameNumber;
2170 }
2171 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002172 return OK;
2173}
2174
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002175void Camera3Device::RequestThread::setPaused(bool paused) {
2176 Mutex::Autolock l(mPauseLock);
2177 mDoPause = paused;
2178 mDoPauseSignal.signal();
2179}
2180
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002181status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2182 int32_t requestId, nsecs_t timeout) {
2183 Mutex::Autolock l(mLatestRequestMutex);
2184 status_t res;
2185 while (mLatestRequestId != requestId) {
2186 nsecs_t startTime = systemTime();
2187
2188 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2189 if (res != OK) return res;
2190
2191 timeout -= (systemTime() - startTime);
2192 }
2193
2194 return OK;
2195}
2196
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002197void Camera3Device::RequestThread::requestExit() {
2198 // Call parent to set up shutdown
2199 Thread::requestExit();
2200 // The exit from any possible waits
2201 mDoPauseSignal.signal();
2202 mRequestSignal.signal();
2203}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002204
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002205bool Camera3Device::RequestThread::threadLoop() {
2206
2207 status_t res;
2208
2209 // Handle paused state.
2210 if (waitIfPaused()) {
2211 return true;
2212 }
2213
2214 // Get work to do
2215
2216 sp<CaptureRequest> nextRequest = waitForNextRequest();
2217 if (nextRequest == NULL) {
2218 return true;
2219 }
2220
2221 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002222 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002223 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002224 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002225
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002226 // Get the request ID, if any
2227 int requestId;
2228 camera_metadata_entry_t requestIdEntry =
2229 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2230 if (requestIdEntry.count > 0) {
2231 requestId = requestIdEntry.data.i32[0];
2232 } else {
2233 ALOGW("%s: Did not have android.request.id set in the request",
2234 __FUNCTION__);
2235 requestId = NAME_NOT_FOUND;
2236 }
2237
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002238 // Insert any queued triggers (before metadata is locked)
2239 int32_t triggerCount;
2240 res = insertTriggers(nextRequest);
2241 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002242 SET_ERR("RequestThread: Unable to insert triggers "
2243 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002244 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002245 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2246 return false;
2247 }
2248 triggerCount = res;
2249
2250 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2251
2252 // If the request is the same as last, or we had triggers last time
2253 if (mPrevRequest != nextRequest || triggersMixedIn) {
2254 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002255 * HAL workaround:
2256 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2257 */
2258 res = addDummyTriggerIds(nextRequest);
2259 if (res != OK) {
2260 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2261 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002262 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002263 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2264 return false;
2265 }
2266
2267 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002268 * The request should be presorted so accesses in HAL
2269 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2270 */
2271 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002272 request.settings = nextRequest->mSettings.getAndLock();
2273 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002274 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2275
2276 IF_ALOGV() {
2277 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2278 find_camera_metadata_ro_entry(
2279 request.settings,
2280 ANDROID_CONTROL_AF_TRIGGER,
2281 &e
2282 );
2283 if (e.count > 0) {
2284 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2285 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002286 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002287 e.data.u8[0]);
2288 }
2289 }
2290 } else {
2291 // leave request.settings NULL to indicate 'reuse latest given'
2292 ALOGVV("%s: Request settings are REUSED",
2293 __FUNCTION__);
2294 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002295
2296 camera3_stream_buffer_t inputBuffer;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002297
2298 // Fill in buffers
2299
2300 if (nextRequest->mInputStream != NULL) {
2301 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002302 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002303 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002304 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002305 " %s (%d)", strerror(-res), res);
2306 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2307 return true;
2308 }
2309 } else {
2310 request.input_buffer = NULL;
2311 }
2312
2313 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2314 nextRequest->mOutputStreams.size());
2315 request.output_buffers = outputBuffers.array();
2316 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2317 res = nextRequest->mOutputStreams.editItemAt(i)->
2318 getBuffer(&outputBuffers.editItemAt(i));
2319 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002320 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2321 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002322 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2323 return true;
2324 }
2325 request.num_output_buffers++;
2326 }
2327
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002328 // Log request in the in-flight queue
2329 sp<Camera3Device> parent = mParent.promote();
2330 if (parent == NULL) {
2331 CLOGE("RequestThread: Parent is gone");
2332 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2333 return false;
2334 }
2335
Jianing Weicb0652e2014-03-12 18:29:36 -07002336 res = parent->registerInFlight(request.frame_number,
2337 request.num_output_buffers, nextRequest->mResultExtras);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002338 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2339 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002340 __FUNCTION__,
2341 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2342 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002343 if (res != OK) {
2344 SET_ERR("RequestThread: Unable to register new in-flight request:"
2345 " %s (%d)", strerror(-res), res);
2346 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2347 return false;
2348 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002349
Zhijun Hecc27e112013-10-03 16:12:43 -07002350 // Inform waitUntilRequestProcessed thread of a new request ID
2351 {
2352 Mutex::Autolock al(mLatestRequestMutex);
2353
2354 mLatestRequestId = requestId;
2355 mLatestRequestSignal.signal();
2356 }
2357
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002358 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002359 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2360 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002361 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002362 ATRACE_END();
2363
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002364 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002365 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002366 " device: %s (%d)", request.frame_number, strerror(-res), res);
2367 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2368 return false;
2369 }
2370
Igor Murashkin1e479c02013-09-06 16:55:14 -07002371 // Update the latest request sent to HAL
2372 if (request.settings != NULL) { // Don't update them if they were unchanged
2373 Mutex::Autolock al(mLatestRequestMutex);
2374
2375 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2376 mLatestRequest.acquire(cloned);
2377 }
2378
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002379 if (request.settings != NULL) {
2380 nextRequest->mSettings.unlock(request.settings);
2381 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002382
2383 // Remove any previously queued triggers (after unlock)
2384 res = removeTriggers(mPrevRequest);
2385 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002386 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002387 "(capture request %d, HAL device: %s (%d)",
2388 request.frame_number, strerror(-res), res);
2389 return false;
2390 }
2391 mPrevTriggers = triggerCount;
2392
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002393 // Return input buffer back to framework
2394 if (request.input_buffer != NULL) {
2395 Camera3Stream *stream =
2396 Camera3Stream::cast(request.input_buffer->stream);
2397 res = stream->returnInputBuffer(*(request.input_buffer));
2398 // Note: stream may be deallocated at this point, if this buffer was the
2399 // last reference to it.
2400 if (res != OK) {
2401 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2402 " its stream:%s (%d)", __FUNCTION__,
2403 request.frame_number, strerror(-res), res);
2404 // TODO: Report error upstream
2405 }
2406 }
2407
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002408 return true;
2409}
2410
Igor Murashkin1e479c02013-09-06 16:55:14 -07002411CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2412 Mutex::Autolock al(mLatestRequestMutex);
2413
2414 ALOGV("RequestThread::%s", __FUNCTION__);
2415
2416 return mLatestRequest;
2417}
2418
Jianing Weicb0652e2014-03-12 18:29:36 -07002419
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002420void Camera3Device::RequestThread::cleanUpFailedRequest(
2421 camera3_capture_request_t &request,
2422 sp<CaptureRequest> &nextRequest,
2423 Vector<camera3_stream_buffer_t> &outputBuffers) {
2424
2425 if (request.settings != NULL) {
2426 nextRequest->mSettings.unlock(request.settings);
2427 }
2428 if (request.input_buffer != NULL) {
2429 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002430 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002431 }
2432 for (size_t i = 0; i < request.num_output_buffers; i++) {
2433 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2434 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2435 outputBuffers[i], 0);
2436 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002437}
2438
2439sp<Camera3Device::CaptureRequest>
2440 Camera3Device::RequestThread::waitForNextRequest() {
2441 status_t res;
2442 sp<CaptureRequest> nextRequest;
2443
2444 // Optimized a bit for the simple steady-state case (single repeating
2445 // request), to avoid putting that request in the queue temporarily.
2446 Mutex::Autolock l(mRequestLock);
2447
2448 while (mRequestQueue.empty()) {
2449 if (!mRepeatingRequests.empty()) {
2450 // Always atomically enqueue all requests in a repeating request
2451 // list. Guarantees a complete in-sequence set of captures to
2452 // application.
2453 const RequestList &requests = mRepeatingRequests;
2454 RequestList::const_iterator firstRequest =
2455 requests.begin();
2456 nextRequest = *firstRequest;
2457 mRequestQueue.insert(mRequestQueue.end(),
2458 ++firstRequest,
2459 requests.end());
2460 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002461
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002462 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002463
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002464 break;
2465 }
2466
2467 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2468
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002469 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2470 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002471 Mutex::Autolock pl(mPauseLock);
2472 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002473 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002474 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002475 // Let the tracker know
2476 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2477 if (statusTracker != 0) {
2478 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2479 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480 }
2481 // Stop waiting for now and let thread management happen
2482 return NULL;
2483 }
2484 }
2485
2486 if (nextRequest == NULL) {
2487 // Don't have a repeating request already in hand, so queue
2488 // must have an entry now.
2489 RequestList::iterator firstRequest =
2490 mRequestQueue.begin();
2491 nextRequest = *firstRequest;
2492 mRequestQueue.erase(firstRequest);
2493 }
2494
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002495 // In case we've been unpaused by setPaused clearing mDoPause, need to
2496 // update internal pause state (capture/setRepeatingRequest unpause
2497 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002498 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002499 if (mPaused) {
2500 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2501 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2502 if (statusTracker != 0) {
2503 statusTracker->markComponentActive(mStatusId);
2504 }
2505 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002506 mPaused = false;
2507
2508 // Check if we've reconfigured since last time, and reset the preview
2509 // request if so. Can't use 'NULL request == repeat' across configure calls.
2510 if (mReconfigured) {
2511 mPrevRequest.clear();
2512 mReconfigured = false;
2513 }
2514
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002515 if (nextRequest != NULL) {
2516 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
2517 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002518 return nextRequest;
2519}
2520
2521bool Camera3Device::RequestThread::waitIfPaused() {
2522 status_t res;
2523 Mutex::Autolock l(mPauseLock);
2524 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002525 if (mPaused == false) {
2526 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002527 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2528 // Let the tracker know
2529 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2530 if (statusTracker != 0) {
2531 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2532 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002533 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002534
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002535 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002536 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002537 return true;
2538 }
2539 }
2540 // We don't set mPaused to false here, because waitForNextRequest needs
2541 // to further manage the paused state in case of starvation.
2542 return false;
2543}
2544
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002545void Camera3Device::RequestThread::unpauseForNewRequests() {
2546 // With work to do, mark thread as unpaused.
2547 // If paused by request (setPaused), don't resume, to avoid
2548 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002549 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002550 Mutex::Autolock p(mPauseLock);
2551 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002552 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2553 if (mPaused) {
2554 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2555 if (statusTracker != 0) {
2556 statusTracker->markComponentActive(mStatusId);
2557 }
2558 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002559 mPaused = false;
2560 }
2561}
2562
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002563void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2564 sp<Camera3Device> parent = mParent.promote();
2565 if (parent != NULL) {
2566 va_list args;
2567 va_start(args, fmt);
2568
2569 parent->setErrorStateV(fmt, args);
2570
2571 va_end(args);
2572 }
2573}
2574
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002575status_t Camera3Device::RequestThread::insertTriggers(
2576 const sp<CaptureRequest> &request) {
2577
2578 Mutex::Autolock al(mTriggerMutex);
2579
2580 CameraMetadata &metadata = request->mSettings;
2581 size_t count = mTriggerMap.size();
2582
2583 for (size_t i = 0; i < count; ++i) {
2584 RequestTrigger trigger = mTriggerMap.valueAt(i);
2585
2586 uint32_t tag = trigger.metadataTag;
2587 camera_metadata_entry entry = metadata.find(tag);
2588
2589 if (entry.count > 0) {
2590 /**
2591 * Already has an entry for this trigger in the request.
2592 * Rewrite it with our requested trigger value.
2593 */
2594 RequestTrigger oldTrigger = trigger;
2595
2596 oldTrigger.entryValue = entry.data.u8[0];
2597
2598 mTriggerReplacedMap.add(tag, oldTrigger);
2599 } else {
2600 /**
2601 * More typical, no trigger entry, so we just add it
2602 */
2603 mTriggerRemovedMap.add(tag, trigger);
2604 }
2605
2606 status_t res;
2607
2608 switch (trigger.getTagType()) {
2609 case TYPE_BYTE: {
2610 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2611 res = metadata.update(tag,
2612 &entryValue,
2613 /*count*/1);
2614 break;
2615 }
2616 case TYPE_INT32:
2617 res = metadata.update(tag,
2618 &trigger.entryValue,
2619 /*count*/1);
2620 break;
2621 default:
2622 ALOGE("%s: Type not supported: 0x%x",
2623 __FUNCTION__,
2624 trigger.getTagType());
2625 return INVALID_OPERATION;
2626 }
2627
2628 if (res != OK) {
2629 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2630 ", value %d", __FUNCTION__, trigger.getTagName(),
2631 trigger.entryValue);
2632 return res;
2633 }
2634
2635 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2636 trigger.getTagName(),
2637 trigger.entryValue);
2638 }
2639
2640 mTriggerMap.clear();
2641
2642 return count;
2643}
2644
2645status_t Camera3Device::RequestThread::removeTriggers(
2646 const sp<CaptureRequest> &request) {
2647 Mutex::Autolock al(mTriggerMutex);
2648
2649 CameraMetadata &metadata = request->mSettings;
2650
2651 /**
2652 * Replace all old entries with their old values.
2653 */
2654 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2655 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2656
2657 status_t res;
2658
2659 uint32_t tag = trigger.metadataTag;
2660 switch (trigger.getTagType()) {
2661 case TYPE_BYTE: {
2662 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2663 res = metadata.update(tag,
2664 &entryValue,
2665 /*count*/1);
2666 break;
2667 }
2668 case TYPE_INT32:
2669 res = metadata.update(tag,
2670 &trigger.entryValue,
2671 /*count*/1);
2672 break;
2673 default:
2674 ALOGE("%s: Type not supported: 0x%x",
2675 __FUNCTION__,
2676 trigger.getTagType());
2677 return INVALID_OPERATION;
2678 }
2679
2680 if (res != OK) {
2681 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2682 ", trigger value %d", __FUNCTION__,
2683 trigger.getTagName(), trigger.entryValue);
2684 return res;
2685 }
2686 }
2687 mTriggerReplacedMap.clear();
2688
2689 /**
2690 * Remove all new entries.
2691 */
2692 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2693 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2694 status_t res = metadata.erase(trigger.metadataTag);
2695
2696 if (res != OK) {
2697 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2698 ", trigger value %d", __FUNCTION__,
2699 trigger.getTagName(), trigger.entryValue);
2700 return res;
2701 }
2702 }
2703 mTriggerRemovedMap.clear();
2704
2705 return OK;
2706}
2707
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002708status_t Camera3Device::RequestThread::addDummyTriggerIds(
2709 const sp<CaptureRequest> &request) {
2710 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2711 static const int32_t dummyTriggerId = 1;
2712 status_t res;
2713
2714 CameraMetadata &metadata = request->mSettings;
2715
2716 // If AF trigger is active, insert a dummy AF trigger ID if none already
2717 // exists
2718 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2719 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2720 if (afTrigger.count > 0 &&
2721 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2722 afId.count == 0) {
2723 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2724 if (res != OK) return res;
2725 }
2726
2727 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2728 // if none already exists
2729 camera_metadata_entry pcTrigger =
2730 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2731 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2732 if (pcTrigger.count > 0 &&
2733 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2734 pcId.count == 0) {
2735 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2736 &dummyTriggerId, 1);
2737 if (res != OK) return res;
2738 }
2739
2740 return OK;
2741}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002742
2743
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002744/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002745 * Static callback forwarding methods from HAL to instance
2746 */
2747
2748void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2749 const camera3_capture_result *result) {
2750 Camera3Device *d =
2751 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2752 d->processCaptureResult(result);
2753}
2754
2755void Camera3Device::sNotify(const camera3_callback_ops *cb,
2756 const camera3_notify_msg *msg) {
2757 Camera3Device *d =
2758 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2759 d->notify(msg);
2760}
2761
2762}; // namespace android