blob: 8fce191d4a3656a4a29844bbcea109ea37976d8d [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 */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700116 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700117 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700118 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700119 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800120 device->common.version);
121 device->common.close(&device->common);
122 return BAD_VALUE;
123 }
124
125 camera_info info;
126 res = module->get_camera_info(mId, &info);
127 if (res != OK) return res;
128
129 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700130 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
131 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700132 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 device->common.close(&device->common);
134 return BAD_VALUE;
135 }
136
137 /** Initialize device with callback functions */
138
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700139 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800140 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700141 ATRACE_END();
142
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800143 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700144 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
145 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 device->common.close(&device->common);
147 return BAD_VALUE;
148 }
149
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700150 /** Start up status tracker thread */
151 mStatusTracker = new StatusTracker(this);
152 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
153 if (res != OK) {
154 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
155 strerror(-res), res);
156 device->common.close(&device->common);
157 mStatusTracker.clear();
158 return res;
159 }
160
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800161 /** Start up request queue thread */
162
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700163 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800164 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800165 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700166 SET_ERR_L("Unable to start request queue thread: %s (%d)",
167 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800168 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800169 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800170 return res;
171 }
172
173 /** Everything is good to go */
174
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700175 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 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
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700287Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
288 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
289 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
290 const int STREAM_CONFIGURATION_SIZE = 4;
291 const int STREAM_FORMAT_OFFSET = 0;
292 const int STREAM_WIDTH_OFFSET = 1;
293 const int STREAM_HEIGHT_OFFSET = 2;
294 const int STREAM_IS_INPUT_OFFSET = 3;
295 camera_metadata_ro_entry_t availableStreamConfigs =
296 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
297 if (availableStreamConfigs.count == 0 ||
298 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
299 return Size(0, 0);
300 }
301
302 // Get max jpeg size (area-wise).
303 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
304 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
305 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
306 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
307 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
308 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
309 && format == HAL_PIXEL_FORMAT_BLOB &&
310 (width * height > maxJpegWidth * maxJpegHeight)) {
311 maxJpegWidth = width;
312 maxJpegHeight = height;
313 }
314 }
315 } else {
316 camera_metadata_ro_entry availableJpegSizes =
317 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
318 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
319 return Size(0, 0);
320 }
321
322 // Get max jpeg size (area-wise).
323 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
324 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
325 > (maxJpegWidth * maxJpegHeight)) {
326 maxJpegWidth = availableJpegSizes.data.i32[i];
327 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
328 }
329 }
330 }
331 return Size(maxJpegWidth, maxJpegHeight);
332}
333
Zhijun Hef7da0962014-04-24 13:27:56 -0700334ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700335 // Get max jpeg size (area-wise).
336 Size maxJpegResolution = getMaxJpegResolution();
337 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700338 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
339 __FUNCTION__, mId);
340 return BAD_VALUE;
341 }
342
Zhijun Hef7da0962014-04-24 13:27:56 -0700343 // Get max jpeg buffer size
344 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700345 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
346 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700347 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
348 return BAD_VALUE;
349 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700350 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Zhijun Hef7da0962014-04-24 13:27:56 -0700351
352 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700353 float scaleFactor = ((float) (width * height)) /
354 (maxJpegResolution.width * maxJpegResolution.height);
Zhijun Hef7da0962014-04-24 13:27:56 -0700355 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
356 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
357 if (jpegBufferSize > maxJpegBufferSize) {
358 jpegBufferSize = maxJpegBufferSize;
359 } else if (jpegBufferSize < kMinJpegBufferSize) {
360 jpegBufferSize = kMinJpegBufferSize;
361 }
362
363 return jpegBufferSize;
364}
365
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800366status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
367 ATRACE_CALL();
368 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700369
370 // Try to lock, but continue in case of failure (to avoid blocking in
371 // deadlocks)
372 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
373 bool gotLock = tryLockSpinRightRound(mLock);
374
375 ALOGW_IF(!gotInterfaceLock,
376 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
377 mId, __FUNCTION__);
378 ALOGW_IF(!gotLock,
379 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
380 mId, __FUNCTION__);
381
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800382 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800383
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800384 const char *status =
385 mStatus == STATUS_ERROR ? "ERROR" :
386 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700387 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
388 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800389 mStatus == STATUS_ACTIVE ? "ACTIVE" :
390 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700391
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800392 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700393 if (mStatus == STATUS_ERROR) {
394 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
395 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396 lines.appendFormat(" Stream configuration:\n");
397
398 if (mInputStream != NULL) {
399 write(fd, lines.string(), lines.size());
400 mInputStream->dump(fd, args);
401 } else {
402 lines.appendFormat(" No input stream.\n");
403 write(fd, lines.string(), lines.size());
404 }
405 for (size_t i = 0; i < mOutputStreams.size(); i++) {
406 mOutputStreams[i]->dump(fd,args);
407 }
408
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700409 lines = String8(" In-flight requests:\n");
410 if (mInFlightMap.size() == 0) {
411 lines.append(" None\n");
412 } else {
413 for (size_t i = 0; i < mInFlightMap.size(); i++) {
414 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700415 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700416 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
417 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
418 r.numBuffersLeft);
419 }
420 }
421 write(fd, lines.string(), lines.size());
422
Igor Murashkin1e479c02013-09-06 16:55:14 -0700423 {
424 lines = String8(" Last request sent:\n");
425 write(fd, lines.string(), lines.size());
426
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700427 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700428 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
429 }
430
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800431 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700432 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433 write(fd, lines.string(), lines.size());
434 mHal3Device->ops->dump(mHal3Device, fd);
435 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800436
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700437 if (gotLock) mLock.unlock();
438 if (gotInterfaceLock) mInterfaceLock.unlock();
439
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800440 return OK;
441}
442
443const CameraMetadata& Camera3Device::info() const {
444 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
446 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700447 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448 mStatus == STATUS_ERROR ?
449 "when in error state" : "before init");
450 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800451 return mDeviceInfo;
452}
453
Jianing Wei90e59c92014-03-12 18:29:36 -0700454status_t Camera3Device::checkStatusOkToCaptureLocked() {
455 switch (mStatus) {
456 case STATUS_ERROR:
457 CLOGE("Device has encountered a serious error");
458 return INVALID_OPERATION;
459 case STATUS_UNINITIALIZED:
460 CLOGE("Device not initialized");
461 return INVALID_OPERATION;
462 case STATUS_UNCONFIGURED:
463 case STATUS_CONFIGURED:
464 case STATUS_ACTIVE:
465 // OK
466 break;
467 default:
468 SET_ERR_L("Unexpected status: %d", mStatus);
469 return INVALID_OPERATION;
470 }
471 return OK;
472}
473
474status_t Camera3Device::convertMetadataListToRequestListLocked(
475 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
476 if (requestList == NULL) {
477 CLOGE("requestList cannot be NULL.");
478 return BAD_VALUE;
479 }
480
Jianing Weicb0652e2014-03-12 18:29:36 -0700481 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700482 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
483 it != metadataList.end(); ++it) {
484 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
485 if (newRequest == 0) {
486 CLOGE("Can't create capture request");
487 return BAD_VALUE;
488 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700489
490 // Setup burst Id and request Id
491 newRequest->mResultExtras.burstId = burstId++;
492 if (it->exists(ANDROID_REQUEST_ID)) {
493 if (it->find(ANDROID_REQUEST_ID).count == 0) {
494 CLOGE("RequestID entry exists; but must not be empty in metadata");
495 return BAD_VALUE;
496 }
497 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
498 } else {
499 CLOGE("RequestID does not exist in metadata");
500 return BAD_VALUE;
501 }
502
Jianing Wei90e59c92014-03-12 18:29:36 -0700503 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700504
505 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700506 }
507 return OK;
508}
509
Jianing Weicb0652e2014-03-12 18:29:36 -0700510status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800511 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800512
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700513 List<const CameraMetadata> requests;
514 requests.push_back(request);
515 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800516}
517
Jianing Wei90e59c92014-03-12 18:29:36 -0700518status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700519 const List<const CameraMetadata> &requests, bool repeating,
520 /*out*/
521 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700522 ATRACE_CALL();
523 Mutex::Autolock il(mInterfaceLock);
524 Mutex::Autolock l(mLock);
525
526 status_t res = checkStatusOkToCaptureLocked();
527 if (res != OK) {
528 // error logged by previous call
529 return res;
530 }
531
532 RequestList requestList;
533
534 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
535 if (res != OK) {
536 // error logged by previous call
537 return res;
538 }
539
540 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700541 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700542 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700543 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700544 }
545
546 if (res == OK) {
547 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
548 if (res != OK) {
549 SET_ERR_L("Can't transition to active in %f seconds!",
550 kActiveTimeout/1e9);
551 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700552 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
553 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700554 } else {
555 CLOGE("Cannot queue request. Impossible.");
556 return BAD_VALUE;
557 }
558
559 return res;
560}
561
Jianing Weicb0652e2014-03-12 18:29:36 -0700562status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
563 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700564 ATRACE_CALL();
565
Jianing Weicb0652e2014-03-12 18:29:36 -0700566 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800568
Jianing Weicb0652e2014-03-12 18:29:36 -0700569status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
570 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800571 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800572
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700573 List<const CameraMetadata> requests;
574 requests.push_back(request);
575 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800576}
577
Jianing Weicb0652e2014-03-12 18:29:36 -0700578status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
579 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700580 ATRACE_CALL();
581
Jianing Weicb0652e2014-03-12 18:29:36 -0700582 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700583}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800584
585sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
586 const CameraMetadata &request) {
587 status_t res;
588
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700589 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590 res = configureStreamsLocked();
591 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700592 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800593 return NULL;
594 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700595 if (mStatus == STATUS_UNCONFIGURED) {
596 CLOGE("No streams configured");
597 return NULL;
598 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800599 }
600
601 sp<CaptureRequest> newRequest = createCaptureRequest(request);
602 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800603}
604
Jianing Weicb0652e2014-03-12 18:29:36 -0700605status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800606 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700607 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800608 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800609
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800610 switch (mStatus) {
611 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700612 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 return INVALID_OPERATION;
614 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700615 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800616 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617 case STATUS_UNCONFIGURED:
618 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800619 case STATUS_ACTIVE:
620 // OK
621 break;
622 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700623 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800624 return INVALID_OPERATION;
625 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700626 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700627
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700628 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800629}
630
631status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
632 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700633 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800634
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700635 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800636}
637
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700638status_t Camera3Device::createInputStream(
639 uint32_t width, uint32_t height, int format, int *id) {
640 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700641 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700642 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700643 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
644 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700645
646 status_t res;
647 bool wasActive = false;
648
649 switch (mStatus) {
650 case STATUS_ERROR:
651 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
652 return INVALID_OPERATION;
653 case STATUS_UNINITIALIZED:
654 ALOGE("%s: Device not initialized", __FUNCTION__);
655 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 case STATUS_UNCONFIGURED:
657 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700658 // OK
659 break;
660 case STATUS_ACTIVE:
661 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700663 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700665 return res;
666 }
667 wasActive = true;
668 break;
669 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700670 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700671 return INVALID_OPERATION;
672 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700673 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700674
675 if (mInputStream != 0) {
676 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
677 return INVALID_OPERATION;
678 }
679
680 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
681 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700682 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700683
684 mInputStream = newStream;
685
686 *id = mNextStreamId++;
687
688 // Continue captures if active at start
689 if (wasActive) {
690 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
691 res = configureStreamsLocked();
692 if (res != OK) {
693 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
694 __FUNCTION__, mNextStreamId, strerror(-res), res);
695 return res;
696 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700697 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700698 }
699
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700700 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700701 return OK;
702}
703
Igor Murashkin2fba5842013-04-22 14:03:54 -0700704
705status_t Camera3Device::createZslStream(
706 uint32_t width, uint32_t height,
707 int depth,
708 /*out*/
709 int *id,
710 sp<Camera3ZslStream>* zslStream) {
711 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700712 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700713 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700714 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
715 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700716
717 status_t res;
718 bool wasActive = false;
719
720 switch (mStatus) {
721 case STATUS_ERROR:
722 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
723 return INVALID_OPERATION;
724 case STATUS_UNINITIALIZED:
725 ALOGE("%s: Device not initialized", __FUNCTION__);
726 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700727 case STATUS_UNCONFIGURED:
728 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700729 // OK
730 break;
731 case STATUS_ACTIVE:
732 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700734 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700735 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700736 return res;
737 }
738 wasActive = true;
739 break;
740 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700741 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700742 return INVALID_OPERATION;
743 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700745
746 if (mInputStream != 0) {
747 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
748 return INVALID_OPERATION;
749 }
750
751 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
752 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700753 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700754
755 res = mOutputStreams.add(mNextStreamId, newStream);
756 if (res < 0) {
757 ALOGE("%s: Can't add new stream to set: %s (%d)",
758 __FUNCTION__, strerror(-res), res);
759 return res;
760 }
761 mInputStream = newStream;
762
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530763 mNeedConfig = true;
764
Igor Murashkin2fba5842013-04-22 14:03:54 -0700765 *id = mNextStreamId++;
766 *zslStream = newStream;
767
768 // Continue captures if active at start
769 if (wasActive) {
770 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
771 res = configureStreamsLocked();
772 if (res != OK) {
773 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
774 __FUNCTION__, mNextStreamId, strerror(-res), res);
775 return res;
776 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700777 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 }
779
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700780 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700781 return OK;
782}
783
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800784status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
785 uint32_t width, uint32_t height, int format, size_t size, int *id) {
786 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700787 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800788 Mutex::Autolock l(mLock);
Colin Crosse5729fa2014-03-21 15:04:25 -0700789 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, size %zu",
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700790 mId, mNextStreamId, width, height, format, size);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800791
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800792 status_t res;
793 bool wasActive = false;
794
795 switch (mStatus) {
796 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700797 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800798 return INVALID_OPERATION;
799 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700800 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800801 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700802 case STATUS_UNCONFIGURED:
803 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800804 // OK
805 break;
806 case STATUS_ACTIVE:
807 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700808 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700810 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 return res;
812 }
813 wasActive = true;
814 break;
815 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700816 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 return INVALID_OPERATION;
818 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700819 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820
821 sp<Camera3OutputStream> newStream;
822 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700823 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
824 if (jpegBufferSize > 0) {
825 ALOGV("%s: Overwrite Jpeg output buffer size from %zu to %zu",
826 __FUNCTION__, size, jpegBufferSize);
827 } else {
828 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
829 return BAD_VALUE;
830 }
831
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700833 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800834 } else {
835 newStream = new Camera3OutputStream(mNextStreamId, consumer,
836 width, height, format);
837 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700838 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839
840 res = mOutputStreams.add(mNextStreamId, newStream);
841 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700842 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 return res;
844 }
845
846 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700847 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800848
849 // Continue captures if active at start
850 if (wasActive) {
851 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
852 res = configureStreamsLocked();
853 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700854 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
855 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800856 return res;
857 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700858 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800859 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700860 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800862}
863
864status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
865 ATRACE_CALL();
866 (void)outputId; (void)id;
867
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700868 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800869 return INVALID_OPERATION;
870}
871
872
873status_t Camera3Device::getStreamInfo(int id,
874 uint32_t *width, uint32_t *height, uint32_t *format) {
875 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800878
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 switch (mStatus) {
880 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700881 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800882 return INVALID_OPERATION;
883 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700884 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 case STATUS_UNCONFIGURED:
887 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 case STATUS_ACTIVE:
889 // OK
890 break;
891 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700892 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800893 return INVALID_OPERATION;
894 }
895
896 ssize_t idx = mOutputStreams.indexOfKey(id);
897 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700898 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800899 return idx;
900 }
901
902 if (width) *width = mOutputStreams[idx]->getWidth();
903 if (height) *height = mOutputStreams[idx]->getHeight();
904 if (format) *format = mOutputStreams[idx]->getFormat();
905
906 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800907}
908
909status_t Camera3Device::setStreamTransform(int id,
910 int transform) {
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);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800914
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800915 switch (mStatus) {
916 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700917 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 return INVALID_OPERATION;
919 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700920 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 case STATUS_UNCONFIGURED:
923 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 case STATUS_ACTIVE:
925 // OK
926 break;
927 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700928 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929 return INVALID_OPERATION;
930 }
931
932 ssize_t idx = mOutputStreams.indexOfKey(id);
933 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700934 CLOGE("Stream %d does not exist",
935 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 return BAD_VALUE;
937 }
938
939 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800940}
941
942status_t Camera3Device::deleteStream(int id) {
943 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700944 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800945 Mutex::Autolock l(mLock);
946 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800947
Igor Murashkine2172be2013-05-28 15:31:39 -0700948 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
949
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800950 // CameraDevice semantics require device to already be idle before
951 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700952 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700953 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
954 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 }
956
Igor Murashkin2fba5842013-04-22 14:03:54 -0700957 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800958 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800959 if (mInputStream != NULL && id == mInputStream->getId()) {
960 deletedStream = mInputStream;
961 mInputStream.clear();
962 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800963 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700964 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 return BAD_VALUE;
966 }
Zhijun He5f446352014-01-22 09:49:33 -0800967 }
968
969 // Delete output stream or the output part of a bi-directional stream.
970 if (outputStreamIdx != NAME_NOT_FOUND) {
971 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800972 mOutputStreams.removeItem(id);
973 }
974
975 // Free up the stream endpoint so that it can be used by some other stream
976 res = deletedStream->disconnect();
977 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700978 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800979 // fall through since we want to still list the stream as deleted.
980 }
981 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700982 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983
984 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800985}
986
987status_t Camera3Device::deleteReprocessStream(int id) {
988 ATRACE_CALL();
989 (void)id;
990
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700991 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800992 return INVALID_OPERATION;
993}
994
995
996status_t Camera3Device::createDefaultRequest(int templateId,
997 CameraMetadata *request) {
998 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -0700999 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001000 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001 Mutex::Autolock l(mLock);
1002
1003 switch (mStatus) {
1004 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001005 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001006 return INVALID_OPERATION;
1007 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001008 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001009 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001010 case STATUS_UNCONFIGURED:
1011 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001012 case STATUS_ACTIVE:
1013 // OK
1014 break;
1015 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001016 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001017 return INVALID_OPERATION;
1018 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001019
1020 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001021 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001022 rawRequest = mHal3Device->ops->construct_default_request_settings(
1023 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001024 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001025 if (rawRequest == NULL) {
1026 SET_ERR_L("HAL is unable to construct default settings for template %d",
1027 templateId);
1028 return DEAD_OBJECT;
1029 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001030 *request = rawRequest;
1031
1032 return OK;
1033}
1034
1035status_t Camera3Device::waitUntilDrained() {
1036 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001038 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001039
Zhijun He69a37482014-03-23 18:44:49 -07001040 return waitUntilDrainedLocked();
1041}
1042
1043status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 switch (mStatus) {
1045 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001046 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 ALOGV("%s: Already idle", __FUNCTION__);
1048 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001049 case STATUS_CONFIGURED:
1050 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 case STATUS_ERROR:
1052 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001053 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001054 break;
1055 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001056 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 return INVALID_OPERATION;
1058 }
1059
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001060 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1061 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1062 return res;
1063}
1064
1065// Pause to reconfigure
1066status_t Camera3Device::internalPauseAndWaitLocked() {
1067 mRequestThread->setPaused(true);
1068 mPauseStateNotify = true;
1069
1070 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1071 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1072 if (res != OK) {
1073 SET_ERR_L("Can't idle device in %f seconds!",
1074 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001075 }
1076
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001077 return res;
1078}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080// Resume after internalPauseAndWaitLocked
1081status_t Camera3Device::internalResumeLocked() {
1082 status_t res;
1083
1084 mRequestThread->setPaused(false);
1085
1086 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1087 if (res != OK) {
1088 SET_ERR_L("Can't transition to active in %f seconds!",
1089 kActiveTimeout/1e9);
1090 }
1091 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001093}
1094
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001095status_t Camera3Device::waitUntilStateThenRelock(bool active,
1096 nsecs_t timeout) {
1097 status_t res = OK;
1098 if (active == (mStatus == STATUS_ACTIVE)) {
1099 // Desired state already reached
1100 return res;
1101 }
1102
1103 bool stateSeen = false;
1104 do {
1105 mRecentStatusUpdates.clear();
1106
1107 res = mStatusChanged.waitRelative(mLock, timeout);
1108 if (res != OK) break;
1109
1110 // Check state change history during wait
1111 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1112 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1113 stateSeen = true;
1114 break;
1115 }
1116 }
1117 } while (!stateSeen);
1118
1119 return res;
1120}
1121
1122
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001123status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1124 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001125 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001126
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001127 if (listener != NULL && mListener != NULL) {
1128 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1129 }
1130 mListener = listener;
1131
1132 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001133}
1134
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001135bool Camera3Device::willNotify3A() {
1136 return false;
1137}
1138
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001139status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001140 status_t res;
1141 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001142
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001143 while (mResultQueue.empty()) {
1144 res = mResultSignal.waitRelative(mOutputLock, timeout);
1145 if (res == TIMED_OUT) {
1146 return res;
1147 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001148 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001149 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001150 return res;
1151 }
1152 }
1153 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001154}
1155
Jianing Weicb0652e2014-03-12 18:29:36 -07001156status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001157 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001158 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001160 if (mResultQueue.empty()) {
1161 return NOT_ENOUGH_DATA;
1162 }
1163
Jianing Weicb0652e2014-03-12 18:29:36 -07001164 if (frame == NULL) {
1165 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1166 return BAD_VALUE;
1167 }
1168
1169 CaptureResult &result = *(mResultQueue.begin());
1170 frame->mResultExtras = result.mResultExtras;
1171 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001172 mResultQueue.erase(mResultQueue.begin());
1173
1174 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001175}
1176
1177status_t Camera3Device::triggerAutofocus(uint32_t id) {
1178 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001179 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001180
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001181 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1182 // Mix-in this trigger into the next request and only the next request.
1183 RequestTrigger trigger[] = {
1184 {
1185 ANDROID_CONTROL_AF_TRIGGER,
1186 ANDROID_CONTROL_AF_TRIGGER_START
1187 },
1188 {
1189 ANDROID_CONTROL_AF_TRIGGER_ID,
1190 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001191 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001192 };
1193
1194 return mRequestThread->queueTrigger(trigger,
1195 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001196}
1197
1198status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1199 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001200 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001201
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001202 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1203 // Mix-in this trigger into the next request and only the next request.
1204 RequestTrigger trigger[] = {
1205 {
1206 ANDROID_CONTROL_AF_TRIGGER,
1207 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1208 },
1209 {
1210 ANDROID_CONTROL_AF_TRIGGER_ID,
1211 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001212 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001213 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001214
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001215 return mRequestThread->queueTrigger(trigger,
1216 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001217}
1218
1219status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1220 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001221 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001223 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1224 // Mix-in this trigger into the next request and only the next request.
1225 RequestTrigger trigger[] = {
1226 {
1227 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1228 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1229 },
1230 {
1231 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1232 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001233 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001234 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001235
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001236 return mRequestThread->queueTrigger(trigger,
1237 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001238}
1239
1240status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1241 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1242 ATRACE_CALL();
1243 (void)reprocessStreamId; (void)buffer; (void)listener;
1244
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246 return INVALID_OPERATION;
1247}
1248
Jianing Weicb0652e2014-03-12 18:29:36 -07001249status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001250 ATRACE_CALL();
1251 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001252 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001253
Zhijun He7ef20392014-04-21 16:04:17 -07001254 {
1255 Mutex::Autolock l(mLock);
1256 mRequestThread->clear(/*out*/frameNumber);
1257 }
1258
Zhijun He491e3412013-12-27 10:57:44 -08001259 status_t res;
1260 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1261 res = mHal3Device->ops->flush(mHal3Device);
1262 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001263 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001264 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001265 }
1266
1267 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001268}
1269
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001271 * Methods called by subclasses
1272 */
1273
1274void Camera3Device::notifyStatus(bool idle) {
1275 {
1276 // Need mLock to safely update state and synchronize to current
1277 // state of methods in flight.
1278 Mutex::Autolock l(mLock);
1279 // We can get various system-idle notices from the status tracker
1280 // while starting up. Only care about them if we've actually sent
1281 // in some requests recently.
1282 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1283 return;
1284 }
1285 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1286 idle ? "idle" : "active");
1287 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1288 mRecentStatusUpdates.add(mStatus);
1289 mStatusChanged.signal();
1290
1291 // Skip notifying listener if we're doing some user-transparent
1292 // state changes
1293 if (mPauseStateNotify) return;
1294 }
1295 NotificationListener *listener;
1296 {
1297 Mutex::Autolock l(mOutputLock);
1298 listener = mListener;
1299 }
1300 if (idle && listener != NULL) {
1301 listener->notifyIdle();
1302 }
1303}
1304
1305/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001306 * Camera3Device private methods
1307 */
1308
1309sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1310 const CameraMetadata &request) {
1311 ATRACE_CALL();
1312 status_t res;
1313
1314 sp<CaptureRequest> newRequest = new CaptureRequest;
1315 newRequest->mSettings = request;
1316
1317 camera_metadata_entry_t inputStreams =
1318 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1319 if (inputStreams.count > 0) {
1320 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001321 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001322 CLOGE("Request references unknown input stream %d",
1323 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001324 return NULL;
1325 }
1326 // Lazy completion of stream configuration (allocation/registration)
1327 // on first use
1328 if (mInputStream->isConfiguring()) {
1329 res = mInputStream->finishConfiguration(mHal3Device);
1330 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001331 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001332 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001333 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 return NULL;
1335 }
1336 }
1337
1338 newRequest->mInputStream = mInputStream;
1339 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1340 }
1341
1342 camera_metadata_entry_t streams =
1343 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1344 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001345 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001346 return NULL;
1347 }
1348
1349 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001350 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001351 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001352 CLOGE("Request references unknown stream %d",
1353 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001354 return NULL;
1355 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001356 sp<Camera3OutputStreamInterface> stream =
1357 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001358
1359 // Lazy completion of stream configuration (allocation/registration)
1360 // on first use
1361 if (stream->isConfiguring()) {
1362 res = stream->finishConfiguration(mHal3Device);
1363 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001364 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1365 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001366 return NULL;
1367 }
1368 }
1369
1370 newRequest->mOutputStreams.push(stream);
1371 }
1372 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1373
1374 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001375}
1376
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001377status_t Camera3Device::configureStreamsLocked() {
1378 ATRACE_CALL();
1379 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001380
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001381 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001382 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383 return INVALID_OPERATION;
1384 }
1385
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001386 if (!mNeedConfig) {
1387 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1388 return OK;
1389 }
1390
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001392 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393
1394 camera3_stream_configuration config;
1395
1396 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1397
1398 Vector<camera3_stream_t*> streams;
1399 streams.setCapacity(config.num_streams);
1400
1401 if (mInputStream != NULL) {
1402 camera3_stream_t *inputStream;
1403 inputStream = mInputStream->startConfiguration();
1404 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001405 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 return INVALID_OPERATION;
1407 }
1408 streams.add(inputStream);
1409 }
1410
1411 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001412
1413 // Don't configure bidi streams twice, nor add them twice to the list
1414 if (mOutputStreams[i].get() ==
1415 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1416
1417 config.num_streams--;
1418 continue;
1419 }
1420
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001421 camera3_stream_t *outputStream;
1422 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1423 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001424 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001425 return INVALID_OPERATION;
1426 }
1427 streams.add(outputStream);
1428 }
1429
1430 config.streams = streams.editArray();
1431
1432 // Do the HAL configuration; will potentially touch stream
1433 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001434 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001436 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001437
1438 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001439 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1440 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001441 return res;
1442 }
1443
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001444 // Finish all stream configuration immediately.
1445 // TODO: Try to relax this later back to lazy completion, which should be
1446 // faster
1447
Igor Murashkin073f8572013-05-02 14:59:28 -07001448 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001449 res = mInputStream->finishConfiguration(mHal3Device);
1450 if (res != OK) {
1451 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1452 mInputStream->getId(), strerror(-res), res);
1453 return res;
1454 }
1455 }
1456
1457 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001458 sp<Camera3OutputStreamInterface> outputStream =
1459 mOutputStreams.editValueAt(i);
1460 if (outputStream->isConfiguring()) {
1461 res = outputStream->finishConfiguration(mHal3Device);
1462 if (res != OK) {
1463 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1464 outputStream->getId(), strerror(-res), res);
1465 return res;
1466 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001467 }
1468 }
1469
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001470 // Request thread needs to know to avoid using repeat-last-settings protocol
1471 // across configure_streams() calls
1472 mRequestThread->configurationComplete();
1473
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001474 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001475
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001476 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001477
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001478 if (config.num_streams > 0) {
1479 mStatus = STATUS_CONFIGURED;
1480 } else {
1481 mStatus = STATUS_UNCONFIGURED;
1482 }
1483
1484 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1485
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001486 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001487}
1488
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001489void Camera3Device::setErrorState(const char *fmt, ...) {
1490 Mutex::Autolock l(mLock);
1491 va_list args;
1492 va_start(args, fmt);
1493
1494 setErrorStateLockedV(fmt, args);
1495
1496 va_end(args);
1497}
1498
1499void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1500 Mutex::Autolock l(mLock);
1501 setErrorStateLockedV(fmt, args);
1502}
1503
1504void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1505 va_list args;
1506 va_start(args, fmt);
1507
1508 setErrorStateLockedV(fmt, args);
1509
1510 va_end(args);
1511}
1512
1513void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001514 // Print out all error messages to log
1515 String8 errorCause = String8::formatV(fmt, args);
1516 ALOGE("Camera %d: %s", mId, errorCause.string());
1517
1518 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001519 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001520
Igor Murashkinff3e31d2013-10-23 16:40:06 -07001521 // Save stack trace. View by dumping it later.
1522 CameraTraces::saveTrace();
1523 // TODO: consider adding errorCause and client pid/procname
1524
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001525 mErrorCause = errorCause;
1526
1527 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001528 mStatus = STATUS_ERROR;
1529}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001530
1531/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001532 * In-flight request management
1533 */
1534
Jianing Weicb0652e2014-03-12 18:29:36 -07001535status_t Camera3Device::registerInFlight(uint32_t frameNumber,
1536 int32_t numBuffers, CaptureResultExtras resultExtras) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001537 ATRACE_CALL();
1538 Mutex::Autolock l(mInFlightLock);
1539
1540 ssize_t res;
Jianing Weicb0652e2014-03-12 18:29:36 -07001541 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001542 if (res < 0) return res;
1543
1544 return OK;
1545}
1546
1547/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001548 * QUIRK(partial results)
1549 * Check if all 3A fields are ready, and send off a partial 3A-only result
1550 * to the output frame queue
1551 */
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001552bool Camera3Device::processPartial3AQuirk(
Jianing Weicb0652e2014-03-12 18:29:36 -07001553 uint32_t frameNumber,
1554 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001555
1556 // Check if all 3A states are present
1557 // The full list of fields is
1558 // android.control.afMode
1559 // android.control.awbMode
1560 // android.control.aeState
1561 // android.control.awbState
1562 // android.control.afState
1563 // android.control.afTriggerID
1564 // android.control.aePrecaptureID
1565 // TODO: Add android.control.aeMode
1566
1567 bool gotAllStates = true;
1568
1569 uint8_t afMode;
1570 uint8_t awbMode;
1571 uint8_t aeState;
1572 uint8_t afState;
1573 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001574
1575 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1576 &afMode, frameNumber);
1577
1578 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1579 &awbMode, frameNumber);
1580
1581 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1582 &aeState, frameNumber);
1583
1584 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1585 &afState, frameNumber);
1586
1587 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1588 &awbState, frameNumber);
1589
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001590 if (!gotAllStates) return false;
1591
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001592 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001593 "AF state %d, AE state %d, AWB state %d, "
1594 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001595 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001596 afMode, awbMode,
1597 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001598 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001599
1600 // Got all states, so construct a minimal result to send
1601 // In addition to the above fields, this means adding in
1602 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001603 // android.request.requestId
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001604 // android.quirks.partialResult
1605
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001606 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001607
1608 Mutex::Autolock l(mOutputLock);
1609
Jianing Weicb0652e2014-03-12 18:29:36 -07001610 CaptureResult captureResult;
1611 captureResult.mResultExtras = resultExtras;
1612 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1613 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1614 // but not limited to CameraDeviceBase::getNextResult
1615 CaptureResult& min3AResult =
1616 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001617
Jianing Weicb0652e2014-03-12 18:29:36 -07001618 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1619 // TODO: This is problematic casting. Need to fix CameraMetadata.
1620 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001621 return false;
1622 }
1623
Jianing Weicb0652e2014-03-12 18:29:36 -07001624 int32_t requestId = resultExtras.requestId;
1625 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001626 &requestId, frameNumber)) {
1627 return false;
1628 }
1629
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001630 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
Jianing Weicb0652e2014-03-12 18:29:36 -07001631 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001632 &partialResult, frameNumber)) {
1633 return false;
1634 }
1635
Jianing Weicb0652e2014-03-12 18:29:36 -07001636 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001637 &afMode, frameNumber)) {
1638 return false;
1639 }
1640
Jianing Weicb0652e2014-03-12 18:29:36 -07001641 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001642 &awbMode, frameNumber)) {
1643 return false;
1644 }
1645
Jianing Weicb0652e2014-03-12 18:29:36 -07001646 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001647 &aeState, frameNumber)) {
1648 return false;
1649 }
1650
Jianing Weicb0652e2014-03-12 18:29:36 -07001651 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001652 &afState, frameNumber)) {
1653 return false;
1654 }
1655
Jianing Weicb0652e2014-03-12 18:29:36 -07001656 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001657 &awbState, frameNumber)) {
1658 return false;
1659 }
1660
Jianing Weicb0652e2014-03-12 18:29:36 -07001661 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001662 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001663 return false;
1664 }
1665
Jianing Weicb0652e2014-03-12 18:29:36 -07001666 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001667 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001668 return false;
1669 }
1670
1671 mResultSignal.signal();
1672
1673 return true;
1674}
1675
1676template<typename T>
1677bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001678 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001679 (void) frameNumber;
1680
1681 camera_metadata_ro_entry_t entry;
1682
1683 entry = result.find(tag);
1684 if (entry.count == 0) {
1685 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1686 mId, frameNumber, get_camera_metadata_tag_name(tag));
1687 return false;
1688 }
1689
1690 if (sizeof(T) == sizeof(uint8_t)) {
1691 *value = entry.data.u8[0];
1692 } else if (sizeof(T) == sizeof(int32_t)) {
1693 *value = entry.data.i32[0];
1694 } else {
1695 ALOGE("%s: Unexpected type", __FUNCTION__);
1696 return false;
1697 }
1698 return true;
1699}
1700
1701template<typename T>
1702bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001703 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001704 if (result.update(tag, value, 1) != NO_ERROR) {
1705 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1706 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1707 frameNumber, get_camera_metadata_tag_name(tag));
1708 return false;
1709 }
1710 return true;
1711}
1712
1713/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001714 * Camera HAL device callback methods
1715 */
1716
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001717void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001718 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001719
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001720 status_t res;
1721
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001722 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001723 if (result->result == NULL && result->num_output_buffers == 0 &&
1724 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001725 SET_ERR("No result data provided by HAL for frame %d",
1726 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001727 return;
1728 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001729 bool partialResultQuirk = false;
1730 CameraMetadata collectedQuirkResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001731 CaptureResultExtras resultExtras;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001732
Jianing Weicb0652e2014-03-12 18:29:36 -07001733 // Get capture timestamp and resultExtras from list of in-flight requests,
1734 // where it was added by the shutter notification for this frame.
1735 // Then update the in-flight status and remove the in-flight entry if
1736 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001737 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001738 {
1739 Mutex::Autolock l(mInFlightLock);
1740 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1741 if (idx == NAME_NOT_FOUND) {
1742 SET_ERR("Unknown frame number for capture result: %d",
1743 frameNumber);
1744 return;
1745 }
1746 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001747 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1748 ", burstId = %" PRId32,
1749 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1750 request.resultExtras.burstId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001751
1752 // Check if this result carries only partial metadata
1753 if (mUsePartialResultQuirk && result->result != NULL) {
1754 camera_metadata_ro_entry_t partialResultEntry;
1755 res = find_camera_metadata_ro_entry(result->result,
1756 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1757 if (res != NAME_NOT_FOUND &&
1758 partialResultEntry.count > 0 &&
1759 partialResultEntry.data.u8[0] ==
1760 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1761 // A partial result. Flag this as such, and collect this
1762 // set of metadata into the in-flight entry.
1763 partialResultQuirk = true;
1764 request.partialResultQuirk.collectedResult.append(
1765 result->result);
1766 request.partialResultQuirk.collectedResult.erase(
1767 ANDROID_QUIRKS_PARTIAL_RESULT);
1768 // Fire off a 3A-only result if possible
1769 if (!request.partialResultQuirk.haveSent3A) {
1770 request.partialResultQuirk.haveSent3A =
1771 processPartial3AQuirk(frameNumber,
Jianing Weicb0652e2014-03-12 18:29:36 -07001772 request.partialResultQuirk.collectedResult,
1773 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001774 }
1775 }
1776 }
1777
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001778 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001779 resultExtras = request.resultExtras;
Jianing Weicb0652e2014-03-12 18:29:36 -07001780
Zhijun He1d1f8462013-10-02 16:29:51 -07001781 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001782 * One of the following must happen before it's legal to call process_capture_result,
1783 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001784 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1785 * - CAMERA3_MSG_ERROR (expected during flush)
1786 */
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001787 if (request.requestStatus == OK && timestamp == 0 && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001788 SET_ERR("Called before shutter notify for frame %d",
1789 frameNumber);
1790 return;
1791 }
1792
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001793 // Did we get the (final) result metadata for this capture?
1794 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001795 if (request.haveResultMetadata) {
1796 SET_ERR("Called multiple times with metadata for frame %d",
1797 frameNumber);
1798 return;
1799 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001800 if (mUsePartialResultQuirk &&
1801 !request.partialResultQuirk.collectedResult.isEmpty()) {
1802 collectedQuirkResult.acquire(
1803 request.partialResultQuirk.collectedResult);
1804 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001805 request.haveResultMetadata = true;
1806 }
1807
1808 request.numBuffersLeft -= result->num_output_buffers;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001809 request.numBuffersLeft -= (result->input_buffer != NULL) ? 1 : 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001810 if (request.numBuffersLeft < 0) {
1811 SET_ERR("Too many buffers returned for frame %d",
1812 frameNumber);
1813 return;
1814 }
1815
Zhijun He1b05dfc2013-11-21 12:57:51 -08001816 // Check if everything has arrived for this result (buffers and metadata), remove it from
1817 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
1818 if ((request.requestStatus != OK) ||
1819 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001820 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001821 mInFlightMap.removeItemsAt(idx, 1);
1822 }
1823
1824 // Sanity check - if we have too many in-flight frames, something has
1825 // likely gone wrong
1826 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001827 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001828 }
1829
1830 }
1831
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001832 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001833 bool gotResult = false;
1834 if (result->result != NULL && !partialResultQuirk) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001835 Mutex::Autolock l(mOutputLock);
1836
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837 gotResult = true;
1838
Jianing Wei3c76fa32014-04-21 11:34:34 -07001839 // TODO: need to track errors for tighter bounds on expected frame number
1840 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001841 SET_ERR("Out-of-order capture result metadata submitted! "
1842 "(got frame number %d, expecting %d)",
1843 frameNumber, mNextResultFrameNumber);
1844 return;
1845 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001846 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001847
Jianing Weicb0652e2014-03-12 18:29:36 -07001848 CaptureResult captureResult;
1849 captureResult.mResultExtras = resultExtras;
1850 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001851
Jianing Weicb0652e2014-03-12 18:29:36 -07001852 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1853 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001854 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001855 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07001857 } else {
1858 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001859 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07001860 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001861
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001862 // Append any previous partials to form a complete result
1863 if (mUsePartialResultQuirk && !collectedQuirkResult.isEmpty()) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001864 captureResult.mMetadata.append(collectedQuirkResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001865 }
1866
Jianing Weicb0652e2014-03-12 18:29:36 -07001867 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001868
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001869 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001870
1871 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07001872 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001873 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001874 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001875 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001876 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07001877 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001878 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07001879 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001880 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001881 gotResult = false;
1882 }
1883
1884 if (gotResult) {
1885 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07001886 List<CaptureResult>::iterator queuedResult =
1887 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1888 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
1889 ", burstId = %" PRId32, __FUNCTION__,
1890 queuedResult->mResultExtras.requestId,
1891 queuedResult->mResultExtras.frameNumber,
1892 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001893 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001894 } // scope for mOutputLock
1895
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001896 // Return completed buffers to their streams with the timestamp
1897
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001898 for (size_t i = 0; i < result->num_output_buffers; i++) {
1899 Camera3Stream *stream =
1900 Camera3Stream::cast(result->output_buffers[i].stream);
1901 res = stream->returnBuffer(result->output_buffers[i], timestamp);
1902 // Note: stream may be deallocated at this point, if this buffer was the
1903 // last reference to it.
1904 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001905 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001906 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001907 }
1908 }
1909
Zhijun Hef0d962a2014-06-30 10:24:11 -07001910 if (result->input_buffer != NULL) {
1911 Camera3Stream *stream =
1912 Camera3Stream::cast(result->input_buffer->stream);
1913 res = stream->returnInputBuffer(*(result->input_buffer));
1914 // Note: stream may be deallocated at this point, if this buffer was the
1915 // last reference to it.
1916 if (res != OK) {
1917 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
1918 " its stream:%s (%d)", __FUNCTION__,
1919 frameNumber, strerror(-res), res);
1920 }
1921 }
1922
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001923 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001924
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001925 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07001926 mResultSignal.signal();
1927 }
1928
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001929}
1930
1931void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001932 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001933 NotificationListener *listener;
1934 {
1935 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001936 listener = mListener;
1937 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001938
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001939 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001940 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001941 return;
1942 }
1943
1944 switch (msg->type) {
1945 case CAMERA3_MSG_ERROR: {
1946 int streamId = 0;
1947 if (msg->message.error.error_stream != NULL) {
1948 Camera3Stream *stream =
1949 Camera3Stream::cast(
1950 msg->message.error.error_stream);
1951 streamId = stream->getId();
1952 }
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001953 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
1954 mId, __FUNCTION__, msg->message.error.frame_number,
1955 streamId, msg->message.error.error_code);
Zhijun He1d1f8462013-10-02 16:29:51 -07001956
Jianing Weicb0652e2014-03-12 18:29:36 -07001957 CaptureResultExtras resultExtras;
Zhijun He1d1f8462013-10-02 16:29:51 -07001958 // Set request error status for the request in the in-flight tracking
1959 {
1960 Mutex::Autolock l(mInFlightLock);
1961 ssize_t idx = mInFlightMap.indexOfKey(msg->message.error.frame_number);
1962 if (idx >= 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001963 InFlightRequest &r = mInFlightMap.editValueAt(idx);
1964 r.requestStatus = msg->message.error.error_code;
1965 resultExtras = r.resultExtras;
1966 } else {
1967 resultExtras.frameNumber = msg->message.error.frame_number;
1968 ALOGE("Camera %d: %s: cannot find in-flight request on frame %" PRId64
1969 " error", mId, __FUNCTION__, resultExtras.frameNumber);
Zhijun He1d1f8462013-10-02 16:29:51 -07001970 }
1971 }
1972
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001973 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07001974 if (msg->message.error.error_code == CAMERA3_MSG_ERROR_DEVICE) {
1975 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1976 resultExtras);
Jianing Weicb0652e2014-03-12 18:29:36 -07001977 }
1978 } else {
1979 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001980 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001981 break;
1982 }
1983 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001984 ssize_t idx;
1985 uint32_t frameNumber = msg->message.shutter.frame_number;
1986 nsecs_t timestamp = msg->message.shutter.timestamp;
1987 // Verify ordering of shutter notifications
1988 {
1989 Mutex::Autolock l(mOutputLock);
Jianing Wei3c76fa32014-04-21 11:34:34 -07001990 // TODO: need to track errors for tighter bounds on expected frame number.
1991 if (frameNumber < mNextShutterFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001992 SET_ERR("Shutter notification out-of-order. Expected "
1993 "notification for frame %d, got frame %d",
1994 mNextShutterFrameNumber, frameNumber);
1995 break;
1996 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07001997 mNextShutterFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001998 }
1999
Jianing Weicb0652e2014-03-12 18:29:36 -07002000 CaptureResultExtras resultExtras;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002001
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002002 // Set timestamp for the request in the in-flight tracking
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002003 // and get the request ID to send upstream
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002004 {
2005 Mutex::Autolock l(mInFlightLock);
2006 idx = mInFlightMap.indexOfKey(frameNumber);
2007 if (idx >= 0) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002008 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2009 r.captureTimestamp = timestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07002010 resultExtras = r.resultExtras;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002011 }
2012 }
2013 if (idx < 0) {
2014 SET_ERR("Shutter notification for non-existent frame number %d",
2015 frameNumber);
2016 break;
2017 }
Colin Crosse5729fa2014-03-21 15:04:25 -07002018 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
Jianing Weicb0652e2014-03-12 18:29:36 -07002019 mId, __FUNCTION__, frameNumber, resultExtras.requestId, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002020 // Call listener, if any
2021 if (listener != NULL) {
Jianing Weicb0652e2014-03-12 18:29:36 -07002022 listener->notifyShutter(resultExtras, timestamp);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002023 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002024 break;
2025 }
2026 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002027 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002028 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002029 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002030}
2031
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002032CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002033 ALOGV("%s", __FUNCTION__);
2034
Igor Murashkin1e479c02013-09-06 16:55:14 -07002035 CameraMetadata retVal;
2036
2037 if (mRequestThread != NULL) {
2038 retVal = mRequestThread->getLatestRequest();
2039 }
2040
Igor Murashkin1e479c02013-09-06 16:55:14 -07002041 return retVal;
2042}
2043
Jianing Weicb0652e2014-03-12 18:29:36 -07002044
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002045/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002046 * RequestThread inner class methods
2047 */
2048
2049Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002050 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002051 camera3_device_t *hal3Device) :
2052 Thread(false),
2053 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002054 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002055 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002056 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002057 mReconfigured(false),
2058 mDoPause(false),
2059 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002060 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002061 mLatestRequestId(NAME_NOT_FOUND),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002062 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002063 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002064}
2065
2066void Camera3Device::RequestThread::configurationComplete() {
2067 Mutex::Autolock l(mRequestLock);
2068 mReconfigured = true;
2069}
2070
Jianing Wei90e59c92014-03-12 18:29:36 -07002071status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002072 List<sp<CaptureRequest> > &requests,
2073 /*out*/
2074 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002075 Mutex::Autolock l(mRequestLock);
2076 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2077 ++it) {
2078 mRequestQueue.push_back(*it);
2079 }
2080
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002081 if (lastFrameNumber != NULL) {
2082 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2083 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2084 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2085 *lastFrameNumber);
2086 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002087
Jianing Wei90e59c92014-03-12 18:29:36 -07002088 unpauseForNewRequests();
2089
2090 return OK;
2091}
2092
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002093
2094status_t Camera3Device::RequestThread::queueTrigger(
2095 RequestTrigger trigger[],
2096 size_t count) {
2097
2098 Mutex::Autolock l(mTriggerMutex);
2099 status_t ret;
2100
2101 for (size_t i = 0; i < count; ++i) {
2102 ret = queueTriggerLocked(trigger[i]);
2103
2104 if (ret != OK) {
2105 return ret;
2106 }
2107 }
2108
2109 return OK;
2110}
2111
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002112int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2113 sp<Camera3Device> d = device.promote();
2114 if (d != NULL) return d->mId;
2115 return 0;
2116}
2117
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002118status_t Camera3Device::RequestThread::queueTriggerLocked(
2119 RequestTrigger trigger) {
2120
2121 uint32_t tag = trigger.metadataTag;
2122 ssize_t index = mTriggerMap.indexOfKey(tag);
2123
2124 switch (trigger.getTagType()) {
2125 case TYPE_BYTE:
2126 // fall-through
2127 case TYPE_INT32:
2128 break;
2129 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002130 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2131 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002132 return INVALID_OPERATION;
2133 }
2134
2135 /**
2136 * Collect only the latest trigger, since we only have 1 field
2137 * in the request settings per trigger tag, and can't send more than 1
2138 * trigger per request.
2139 */
2140 if (index != NAME_NOT_FOUND) {
2141 mTriggerMap.editValueAt(index) = trigger;
2142 } else {
2143 mTriggerMap.add(tag, trigger);
2144 }
2145
2146 return OK;
2147}
2148
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002149status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002150 const RequestList &requests,
2151 /*out*/
2152 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002153 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002154 if (lastFrameNumber != NULL) {
2155 *lastFrameNumber = mRepeatingLastFrameNumber;
2156 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002157 mRepeatingRequests.clear();
2158 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2159 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002160
2161 unpauseForNewRequests();
2162
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002163 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002164 return OK;
2165}
2166
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002167bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2168 if (mRepeatingRequests.empty()) {
2169 return false;
2170 }
2171 int32_t requestId = requestIn->mResultExtras.requestId;
2172 const RequestList &repeatRequests = mRepeatingRequests;
2173 // All repeating requests are guaranteed to have same id so only check first quest
2174 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2175 return (firstRequest->mResultExtras.requestId == requestId);
2176}
2177
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002178status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002179 Mutex::Autolock l(mRequestLock);
2180 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002181 if (lastFrameNumber != NULL) {
2182 *lastFrameNumber = mRepeatingLastFrameNumber;
2183 }
2184 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002185 return OK;
2186}
2187
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002188status_t Camera3Device::RequestThread::clear(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002189 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002190 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002191 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002192
2193 // Decrement repeating frame count for those requests never sent to device
2194 // TODO: Remove this after we have proper error handling so these requests
2195 // will generate an error callback. This might be the only place calling
2196 // isRepeatingRequestLocked. If so, isRepeatingRequestLocked should also be removed.
2197 const RequestList &requests = mRequestQueue;
2198 for (RequestList::const_iterator it = requests.begin();
2199 it != requests.end(); ++it) {
2200 if (isRepeatingRequestLocked(*it)) {
2201 mRepeatingLastFrameNumber--;
2202 }
2203 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002204 mRequestQueue.clear();
2205 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002206 if (lastFrameNumber != NULL) {
2207 *lastFrameNumber = mRepeatingLastFrameNumber;
2208 }
2209 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002210 return OK;
2211}
2212
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213void Camera3Device::RequestThread::setPaused(bool paused) {
2214 Mutex::Autolock l(mPauseLock);
2215 mDoPause = paused;
2216 mDoPauseSignal.signal();
2217}
2218
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002219status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2220 int32_t requestId, nsecs_t timeout) {
2221 Mutex::Autolock l(mLatestRequestMutex);
2222 status_t res;
2223 while (mLatestRequestId != requestId) {
2224 nsecs_t startTime = systemTime();
2225
2226 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2227 if (res != OK) return res;
2228
2229 timeout -= (systemTime() - startTime);
2230 }
2231
2232 return OK;
2233}
2234
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002235void Camera3Device::RequestThread::requestExit() {
2236 // Call parent to set up shutdown
2237 Thread::requestExit();
2238 // The exit from any possible waits
2239 mDoPauseSignal.signal();
2240 mRequestSignal.signal();
2241}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002242
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002243bool Camera3Device::RequestThread::threadLoop() {
2244
2245 status_t res;
2246
2247 // Handle paused state.
2248 if (waitIfPaused()) {
2249 return true;
2250 }
2251
2252 // Get work to do
2253
2254 sp<CaptureRequest> nextRequest = waitForNextRequest();
2255 if (nextRequest == NULL) {
2256 return true;
2257 }
2258
2259 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002260 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002261 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002262 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002264 // Get the request ID, if any
2265 int requestId;
2266 camera_metadata_entry_t requestIdEntry =
2267 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2268 if (requestIdEntry.count > 0) {
2269 requestId = requestIdEntry.data.i32[0];
2270 } else {
2271 ALOGW("%s: Did not have android.request.id set in the request",
2272 __FUNCTION__);
2273 requestId = NAME_NOT_FOUND;
2274 }
2275
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002276 // Insert any queued triggers (before metadata is locked)
2277 int32_t triggerCount;
2278 res = insertTriggers(nextRequest);
2279 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002280 SET_ERR("RequestThread: Unable to insert triggers "
2281 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002282 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002283 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2284 return false;
2285 }
2286 triggerCount = res;
2287
2288 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2289
2290 // If the request is the same as last, or we had triggers last time
2291 if (mPrevRequest != nextRequest || triggersMixedIn) {
2292 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002293 * HAL workaround:
2294 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2295 */
2296 res = addDummyTriggerIds(nextRequest);
2297 if (res != OK) {
2298 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2299 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002300 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002301 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2302 return false;
2303 }
2304
2305 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002306 * The request should be presorted so accesses in HAL
2307 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2308 */
2309 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002310 request.settings = nextRequest->mSettings.getAndLock();
2311 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002312 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2313
2314 IF_ALOGV() {
2315 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2316 find_camera_metadata_ro_entry(
2317 request.settings,
2318 ANDROID_CONTROL_AF_TRIGGER,
2319 &e
2320 );
2321 if (e.count > 0) {
2322 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2323 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002324 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002325 e.data.u8[0]);
2326 }
2327 }
2328 } else {
2329 // leave request.settings NULL to indicate 'reuse latest given'
2330 ALOGVV("%s: Request settings are REUSED",
2331 __FUNCTION__);
2332 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333
2334 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002335 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002336
2337 // Fill in buffers
2338
2339 if (nextRequest->mInputStream != NULL) {
2340 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002341 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002342 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002343 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002344 " %s (%d)", strerror(-res), res);
2345 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2346 return true;
2347 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002348 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002349 } else {
2350 request.input_buffer = NULL;
2351 }
2352
2353 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2354 nextRequest->mOutputStreams.size());
2355 request.output_buffers = outputBuffers.array();
2356 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2357 res = nextRequest->mOutputStreams.editItemAt(i)->
2358 getBuffer(&outputBuffers.editItemAt(i));
2359 if (res != OK) {
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002360 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2361 " %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002362 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2363 return true;
2364 }
2365 request.num_output_buffers++;
2366 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002367 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002369 // Log request in the in-flight queue
2370 sp<Camera3Device> parent = mParent.promote();
2371 if (parent == NULL) {
2372 CLOGE("RequestThread: Parent is gone");
2373 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2374 return false;
2375 }
2376
Jianing Weicb0652e2014-03-12 18:29:36 -07002377 res = parent->registerInFlight(request.frame_number,
Zhijun Hef0d962a2014-06-30 10:24:11 -07002378 totalNumBuffers, nextRequest->mResultExtras);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002379 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2380 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002381 __FUNCTION__,
2382 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2383 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002384 if (res != OK) {
2385 SET_ERR("RequestThread: Unable to register new in-flight request:"
2386 " %s (%d)", strerror(-res), res);
2387 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2388 return false;
2389 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002390
Zhijun Hecc27e112013-10-03 16:12:43 -07002391 // Inform waitUntilRequestProcessed thread of a new request ID
2392 {
2393 Mutex::Autolock al(mLatestRequestMutex);
2394
2395 mLatestRequestId = requestId;
2396 mLatestRequestSignal.signal();
2397 }
2398
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002399 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002400 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2401 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002402 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002403 ATRACE_END();
2404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002405 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002406 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002407 " device: %s (%d)", request.frame_number, strerror(-res), res);
2408 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2409 return false;
2410 }
2411
Igor Murashkin1e479c02013-09-06 16:55:14 -07002412 // Update the latest request sent to HAL
2413 if (request.settings != NULL) { // Don't update them if they were unchanged
2414 Mutex::Autolock al(mLatestRequestMutex);
2415
2416 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2417 mLatestRequest.acquire(cloned);
2418 }
2419
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002420 if (request.settings != NULL) {
2421 nextRequest->mSettings.unlock(request.settings);
2422 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002423
2424 // Remove any previously queued triggers (after unlock)
2425 res = removeTriggers(mPrevRequest);
2426 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002427 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002428 "(capture request %d, HAL device: %s (%d)",
2429 request.frame_number, strerror(-res), res);
2430 return false;
2431 }
2432 mPrevTriggers = triggerCount;
2433
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002434 return true;
2435}
2436
Igor Murashkin1e479c02013-09-06 16:55:14 -07002437CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2438 Mutex::Autolock al(mLatestRequestMutex);
2439
2440 ALOGV("RequestThread::%s", __FUNCTION__);
2441
2442 return mLatestRequest;
2443}
2444
Jianing Weicb0652e2014-03-12 18:29:36 -07002445
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002446void Camera3Device::RequestThread::cleanUpFailedRequest(
2447 camera3_capture_request_t &request,
2448 sp<CaptureRequest> &nextRequest,
2449 Vector<camera3_stream_buffer_t> &outputBuffers) {
2450
2451 if (request.settings != NULL) {
2452 nextRequest->mSettings.unlock(request.settings);
2453 }
2454 if (request.input_buffer != NULL) {
2455 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002456 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002457 }
2458 for (size_t i = 0; i < request.num_output_buffers; i++) {
2459 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2460 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2461 outputBuffers[i], 0);
2462 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002463}
2464
2465sp<Camera3Device::CaptureRequest>
2466 Camera3Device::RequestThread::waitForNextRequest() {
2467 status_t res;
2468 sp<CaptureRequest> nextRequest;
2469
2470 // Optimized a bit for the simple steady-state case (single repeating
2471 // request), to avoid putting that request in the queue temporarily.
2472 Mutex::Autolock l(mRequestLock);
2473
2474 while (mRequestQueue.empty()) {
2475 if (!mRepeatingRequests.empty()) {
2476 // Always atomically enqueue all requests in a repeating request
2477 // list. Guarantees a complete in-sequence set of captures to
2478 // application.
2479 const RequestList &requests = mRepeatingRequests;
2480 RequestList::const_iterator firstRequest =
2481 requests.begin();
2482 nextRequest = *firstRequest;
2483 mRequestQueue.insert(mRequestQueue.end(),
2484 ++firstRequest,
2485 requests.end());
2486 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002487
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002488 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002489
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002490 break;
2491 }
2492
2493 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2494
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002495 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2496 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002497 Mutex::Autolock pl(mPauseLock);
2498 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002499 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002500 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002501 // Let the tracker know
2502 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2503 if (statusTracker != 0) {
2504 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2505 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002506 }
2507 // Stop waiting for now and let thread management happen
2508 return NULL;
2509 }
2510 }
2511
2512 if (nextRequest == NULL) {
2513 // Don't have a repeating request already in hand, so queue
2514 // must have an entry now.
2515 RequestList::iterator firstRequest =
2516 mRequestQueue.begin();
2517 nextRequest = *firstRequest;
2518 mRequestQueue.erase(firstRequest);
2519 }
2520
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002521 // In case we've been unpaused by setPaused clearing mDoPause, need to
2522 // update internal pause state (capture/setRepeatingRequest unpause
2523 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002524 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002525 if (mPaused) {
2526 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2527 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2528 if (statusTracker != 0) {
2529 statusTracker->markComponentActive(mStatusId);
2530 }
2531 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002532 mPaused = false;
2533
2534 // Check if we've reconfigured since last time, and reset the preview
2535 // request if so. Can't use 'NULL request == repeat' across configure calls.
2536 if (mReconfigured) {
2537 mPrevRequest.clear();
2538 mReconfigured = false;
2539 }
2540
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002541 if (nextRequest != NULL) {
2542 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
2543 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002544 return nextRequest;
2545}
2546
2547bool Camera3Device::RequestThread::waitIfPaused() {
2548 status_t res;
2549 Mutex::Autolock l(mPauseLock);
2550 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002551 if (mPaused == false) {
2552 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002553 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2554 // Let the tracker know
2555 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2556 if (statusTracker != 0) {
2557 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2558 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002559 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002560
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002561 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002562 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002563 return true;
2564 }
2565 }
2566 // We don't set mPaused to false here, because waitForNextRequest needs
2567 // to further manage the paused state in case of starvation.
2568 return false;
2569}
2570
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002571void Camera3Device::RequestThread::unpauseForNewRequests() {
2572 // With work to do, mark thread as unpaused.
2573 // If paused by request (setPaused), don't resume, to avoid
2574 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002575 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002576 Mutex::Autolock p(mPauseLock);
2577 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002578 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2579 if (mPaused) {
2580 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2581 if (statusTracker != 0) {
2582 statusTracker->markComponentActive(mStatusId);
2583 }
2584 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002585 mPaused = false;
2586 }
2587}
2588
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002589void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2590 sp<Camera3Device> parent = mParent.promote();
2591 if (parent != NULL) {
2592 va_list args;
2593 va_start(args, fmt);
2594
2595 parent->setErrorStateV(fmt, args);
2596
2597 va_end(args);
2598 }
2599}
2600
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002601status_t Camera3Device::RequestThread::insertTriggers(
2602 const sp<CaptureRequest> &request) {
2603
2604 Mutex::Autolock al(mTriggerMutex);
2605
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002606 sp<Camera3Device> parent = mParent.promote();
2607 if (parent == NULL) {
2608 CLOGE("RequestThread: Parent is gone");
2609 return DEAD_OBJECT;
2610 }
2611
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002612 CameraMetadata &metadata = request->mSettings;
2613 size_t count = mTriggerMap.size();
2614
2615 for (size_t i = 0; i < count; ++i) {
2616 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002617 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002618
2619 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2620 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2621 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
2622 isAeTrigger ? request->mResultExtras.precaptureTriggerId = triggerId :
2623 request->mResultExtras.afTriggerId = triggerId;
2624 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2625 continue; // Trigger ID tag is deprecated since device HAL 3.2
2626 }
2627 }
2628
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002629 camera_metadata_entry entry = metadata.find(tag);
2630
2631 if (entry.count > 0) {
2632 /**
2633 * Already has an entry for this trigger in the request.
2634 * Rewrite it with our requested trigger value.
2635 */
2636 RequestTrigger oldTrigger = trigger;
2637
2638 oldTrigger.entryValue = entry.data.u8[0];
2639
2640 mTriggerReplacedMap.add(tag, oldTrigger);
2641 } else {
2642 /**
2643 * More typical, no trigger entry, so we just add it
2644 */
2645 mTriggerRemovedMap.add(tag, trigger);
2646 }
2647
2648 status_t res;
2649
2650 switch (trigger.getTagType()) {
2651 case TYPE_BYTE: {
2652 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2653 res = metadata.update(tag,
2654 &entryValue,
2655 /*count*/1);
2656 break;
2657 }
2658 case TYPE_INT32:
2659 res = metadata.update(tag,
2660 &trigger.entryValue,
2661 /*count*/1);
2662 break;
2663 default:
2664 ALOGE("%s: Type not supported: 0x%x",
2665 __FUNCTION__,
2666 trigger.getTagType());
2667 return INVALID_OPERATION;
2668 }
2669
2670 if (res != OK) {
2671 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2672 ", value %d", __FUNCTION__, trigger.getTagName(),
2673 trigger.entryValue);
2674 return res;
2675 }
2676
2677 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2678 trigger.getTagName(),
2679 trigger.entryValue);
2680 }
2681
2682 mTriggerMap.clear();
2683
2684 return count;
2685}
2686
2687status_t Camera3Device::RequestThread::removeTriggers(
2688 const sp<CaptureRequest> &request) {
2689 Mutex::Autolock al(mTriggerMutex);
2690
2691 CameraMetadata &metadata = request->mSettings;
2692
2693 /**
2694 * Replace all old entries with their old values.
2695 */
2696 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2697 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2698
2699 status_t res;
2700
2701 uint32_t tag = trigger.metadataTag;
2702 switch (trigger.getTagType()) {
2703 case TYPE_BYTE: {
2704 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2705 res = metadata.update(tag,
2706 &entryValue,
2707 /*count*/1);
2708 break;
2709 }
2710 case TYPE_INT32:
2711 res = metadata.update(tag,
2712 &trigger.entryValue,
2713 /*count*/1);
2714 break;
2715 default:
2716 ALOGE("%s: Type not supported: 0x%x",
2717 __FUNCTION__,
2718 trigger.getTagType());
2719 return INVALID_OPERATION;
2720 }
2721
2722 if (res != OK) {
2723 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
2724 ", trigger value %d", __FUNCTION__,
2725 trigger.getTagName(), trigger.entryValue);
2726 return res;
2727 }
2728 }
2729 mTriggerReplacedMap.clear();
2730
2731 /**
2732 * Remove all new entries.
2733 */
2734 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
2735 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
2736 status_t res = metadata.erase(trigger.metadataTag);
2737
2738 if (res != OK) {
2739 ALOGE("%s: Failed to erase metadata with trigger tag %s"
2740 ", trigger value %d", __FUNCTION__,
2741 trigger.getTagName(), trigger.entryValue);
2742 return res;
2743 }
2744 }
2745 mTriggerRemovedMap.clear();
2746
2747 return OK;
2748}
2749
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002750status_t Camera3Device::RequestThread::addDummyTriggerIds(
2751 const sp<CaptureRequest> &request) {
2752 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
2753 static const int32_t dummyTriggerId = 1;
2754 status_t res;
2755
2756 CameraMetadata &metadata = request->mSettings;
2757
2758 // If AF trigger is active, insert a dummy AF trigger ID if none already
2759 // exists
2760 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
2761 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
2762 if (afTrigger.count > 0 &&
2763 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
2764 afId.count == 0) {
2765 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
2766 if (res != OK) return res;
2767 }
2768
2769 // If AE precapture trigger is active, insert a dummy precapture trigger ID
2770 // if none already exists
2771 camera_metadata_entry pcTrigger =
2772 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
2773 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
2774 if (pcTrigger.count > 0 &&
2775 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
2776 pcId.count == 0) {
2777 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
2778 &dummyTriggerId, 1);
2779 if (res != OK) return res;
2780 }
2781
2782 return OK;
2783}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002784
2785
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002786/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002787 * Static callback forwarding methods from HAL to instance
2788 */
2789
2790void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
2791 const camera3_capture_result *result) {
2792 Camera3Device *d =
2793 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2794 d->processCaptureResult(result);
2795}
2796
2797void Camera3Device::sNotify(const camera3_callback_ops *cb,
2798 const camera3_notify_msg *msg) {
2799 Camera3Device *d =
2800 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
2801 d->notify(msg);
2802}
2803
2804}; // namespace android