blob: 964fa35a75801aa910c9e6714bd4357efb39dd6f [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
Chien-Yu Chen116a1892016-03-09 12:21:01 -080046#include "CameraService.h"
Igor Murashkinff3e31d2013-10-23 16:40:06 -070047#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070048#include "device3/Camera3Device.h"
49#include "device3/Camera3OutputStream.h"
50#include "device3/Camera3InputStream.h"
51#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070052#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070053#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080054
55using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056
57namespace android {
58
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059Camera3Device::Camera3Device(int id):
60 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080061 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070062 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070063 mUsePartialResult(false),
64 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070065 mNextResultFrameNumber(0),
66 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070067 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080068{
69 ATRACE_CALL();
70 camera3_callback_ops::notify = &sNotify;
71 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
72 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
73}
74
75Camera3Device::~Camera3Device()
76{
77 ATRACE_CALL();
78 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
79 disconnect();
80}
81
Igor Murashkin71381052013-03-04 14:53:08 -080082int Camera3Device::getId() const {
83 return mId;
84}
85
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080086/**
87 * CameraDeviceBase interface
88 */
89
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090status_t Camera3Device::initialize(camera_module_t *module)
91{
92 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070093 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080094 Mutex::Autolock l(mLock);
95
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080097 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070098 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 return INVALID_OPERATION;
100 }
101
102 /** Open HAL device */
103
104 status_t res;
105 String8 deviceName = String8::format("%d", mId);
106
107 camera3_device_t *device;
108
Zhijun He213ce792013-11-19 08:45:15 -0800109 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700110 res = CameraService::filterOpenErrorCode(module->common.methods->open(
111 &module->common, deviceName.string(),
112 reinterpret_cast<hw_device_t**>(&device)));
Zhijun He213ce792013-11-19 08:45:15 -0800113 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700116 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 return res;
118 }
119
120 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700121 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700122 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700123 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700124 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 device->common.version);
126 device->common.close(&device->common);
127 return BAD_VALUE;
128 }
129
130 camera_info info;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700131 res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
132 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 if (res != OK) return res;
134
135 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700136 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
137 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700138 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800139 device->common.close(&device->common);
140 return BAD_VALUE;
141 }
142
143 /** Initialize device with callback functions */
144
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700145 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800146 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700147 ATRACE_END();
148
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800149 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700150 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
151 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800152 device->common.close(&device->common);
153 return BAD_VALUE;
154 }
155
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700156 /** Start up status tracker thread */
157 mStatusTracker = new StatusTracker(this);
158 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
159 if (res != OK) {
160 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
161 strerror(-res), res);
162 device->common.close(&device->common);
163 mStatusTracker.clear();
164 return res;
165 }
166
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800167 /** Start up request queue thread */
168
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700169 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800170 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800171 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700172 SET_ERR_L("Unable to start request queue thread: %s (%d)",
173 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800174 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800175 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800176 return res;
177 }
178
179 /** Everything is good to go */
180
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700181 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800182 mDeviceInfo = info.static_camera_characteristics;
183 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700184 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800185 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700186 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700187 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700188 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800189
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700190 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700191 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
192 camera_metadata_entry partialResultsCount =
193 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
194 if (partialResultsCount.count > 0) {
195 mNumPartialResults = partialResultsCount.data.i32[0];
196 mUsePartialResult = (mNumPartialResults > 1);
197 }
198 } else {
199 camera_metadata_entry partialResultsQuirk =
200 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
201 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
202 mUsePartialResult = true;
203 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700204 }
205
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800206 return OK;
207}
208
209status_t Camera3Device::disconnect() {
210 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700211 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800212
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800213 ALOGV("%s: E", __FUNCTION__);
214
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700215 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800216
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700217 {
218 Mutex::Autolock l(mLock);
219 if (mStatus == STATUS_UNINITIALIZED) return res;
220
221 if (mStatus == STATUS_ACTIVE ||
222 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
223 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700224 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700225 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700226 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700227 } else {
228 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
229 if (res != OK) {
230 SET_ERR_L("Timeout waiting for HAL to drain");
231 // Continue to close device even in case of error
232 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700233 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800234 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800235
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700236 if (mStatus == STATUS_ERROR) {
237 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700238 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700239
240 if (mStatusTracker != NULL) {
241 mStatusTracker->requestExit();
242 }
243
244 if (mRequestThread != NULL) {
245 mRequestThread->requestExit();
246 }
247
248 mOutputStreams.clear();
249 mInputStream.clear();
250 }
251
252 // Joining done without holding mLock, otherwise deadlocks may ensue
253 // as the threads try to access parent state
254 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
255 // HAL may be in a bad state, so waiting for request thread
256 // (which may be stuck in the HAL processCaptureRequest call)
257 // could be dangerous.
258 mRequestThread->join();
259 }
260
261 if (mStatusTracker != NULL) {
262 mStatusTracker->join();
263 }
264
265 {
266 Mutex::Autolock l(mLock);
267
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800268 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800270
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800272 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700273 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800274 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 mHal3Device = NULL;
276 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800277
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278 mStatus = STATUS_UNINITIALIZED;
279 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800280
281 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700282 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800283}
284
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700285// For dumping/debugging only -
286// try to acquire a lock a few times, eventually give up to proceed with
287// debug/dump operations
288bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
289 bool gotLock = false;
290 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
291 if (lock.tryLock() == NO_ERROR) {
292 gotLock = true;
293 break;
294 } else {
295 usleep(kDumpSleepDuration);
296 }
297 }
298 return gotLock;
299}
300
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700301Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
302 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
303 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
304 const int STREAM_CONFIGURATION_SIZE = 4;
305 const int STREAM_FORMAT_OFFSET = 0;
306 const int STREAM_WIDTH_OFFSET = 1;
307 const int STREAM_HEIGHT_OFFSET = 2;
308 const int STREAM_IS_INPUT_OFFSET = 3;
309 camera_metadata_ro_entry_t availableStreamConfigs =
310 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
311 if (availableStreamConfigs.count == 0 ||
312 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
313 return Size(0, 0);
314 }
315
316 // Get max jpeg size (area-wise).
317 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
318 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
319 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
320 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
321 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
322 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
323 && format == HAL_PIXEL_FORMAT_BLOB &&
324 (width * height > maxJpegWidth * maxJpegHeight)) {
325 maxJpegWidth = width;
326 maxJpegHeight = height;
327 }
328 }
329 } else {
330 camera_metadata_ro_entry availableJpegSizes =
331 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
332 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
333 return Size(0, 0);
334 }
335
336 // Get max jpeg size (area-wise).
337 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
338 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
339 > (maxJpegWidth * maxJpegHeight)) {
340 maxJpegWidth = availableJpegSizes.data.i32[i];
341 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
342 }
343 }
344 }
345 return Size(maxJpegWidth, maxJpegHeight);
346}
347
Zhijun Hef7da0962014-04-24 13:27:56 -0700348ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700349 // Get max jpeg size (area-wise).
350 Size maxJpegResolution = getMaxJpegResolution();
351 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700352 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
353 __FUNCTION__, mId);
354 return BAD_VALUE;
355 }
356
Zhijun Hef7da0962014-04-24 13:27:56 -0700357 // Get max jpeg buffer size
358 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700359 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
360 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700361 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
362 return BAD_VALUE;
363 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700364 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800365 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700366
367 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700368 float scaleFactor = ((float) (width * height)) /
369 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800370 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
371 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700372 if (jpegBufferSize > maxJpegBufferSize) {
373 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700374 }
375
376 return jpegBufferSize;
377}
378
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800379status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
380 ATRACE_CALL();
381 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382
383 // Try to lock, but continue in case of failure (to avoid blocking in
384 // deadlocks)
385 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
386 bool gotLock = tryLockSpinRightRound(mLock);
387
388 ALOGW_IF(!gotInterfaceLock,
389 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
390 mId, __FUNCTION__);
391 ALOGW_IF(!gotLock,
392 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
393 mId, __FUNCTION__);
394
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800396
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800397 const char *status =
398 mStatus == STATUS_ERROR ? "ERROR" :
399 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700400 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
401 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 mStatus == STATUS_ACTIVE ? "ACTIVE" :
403 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700406 if (mStatus == STATUS_ERROR) {
407 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
408 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 lines.appendFormat(" Stream configuration:\n");
410
411 if (mInputStream != NULL) {
412 write(fd, lines.string(), lines.size());
413 mInputStream->dump(fd, args);
414 } else {
415 lines.appendFormat(" No input stream.\n");
416 write(fd, lines.string(), lines.size());
417 }
418 for (size_t i = 0; i < mOutputStreams.size(); i++) {
419 mOutputStreams[i]->dump(fd,args);
420 }
421
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700422 lines = String8(" In-flight requests:\n");
423 if (mInFlightMap.size() == 0) {
424 lines.append(" None\n");
425 } else {
426 for (size_t i = 0; i < mInFlightMap.size(); i++) {
427 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700428 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700429 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800430 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700431 r.numBuffersLeft);
432 }
433 }
434 write(fd, lines.string(), lines.size());
435
Igor Murashkin1e479c02013-09-06 16:55:14 -0700436 {
437 lines = String8(" Last request sent:\n");
438 write(fd, lines.string(), lines.size());
439
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700441 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
442 }
443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 write(fd, lines.string(), lines.size());
447 mHal3Device->ops->dump(mHal3Device, fd);
448 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 if (gotLock) mLock.unlock();
451 if (gotInterfaceLock) mInterfaceLock.unlock();
452
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800453 return OK;
454}
455
456const CameraMetadata& Camera3Device::info() const {
457 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
459 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700460 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 mStatus == STATUS_ERROR ?
462 "when in error state" : "before init");
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464 return mDeviceInfo;
465}
466
Jianing Wei90e59c92014-03-12 18:29:36 -0700467status_t Camera3Device::checkStatusOkToCaptureLocked() {
468 switch (mStatus) {
469 case STATUS_ERROR:
470 CLOGE("Device has encountered a serious error");
471 return INVALID_OPERATION;
472 case STATUS_UNINITIALIZED:
473 CLOGE("Device not initialized");
474 return INVALID_OPERATION;
475 case STATUS_UNCONFIGURED:
476 case STATUS_CONFIGURED:
477 case STATUS_ACTIVE:
478 // OK
479 break;
480 default:
481 SET_ERR_L("Unexpected status: %d", mStatus);
482 return INVALID_OPERATION;
483 }
484 return OK;
485}
486
487status_t Camera3Device::convertMetadataListToRequestListLocked(
488 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
489 if (requestList == NULL) {
490 CLOGE("requestList cannot be NULL.");
491 return BAD_VALUE;
492 }
493
Jianing Weicb0652e2014-03-12 18:29:36 -0700494 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700495 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
496 it != metadataList.end(); ++it) {
497 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
498 if (newRequest == 0) {
499 CLOGE("Can't create capture request");
500 return BAD_VALUE;
501 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700502
503 // Setup burst Id and request Id
504 newRequest->mResultExtras.burstId = burstId++;
505 if (it->exists(ANDROID_REQUEST_ID)) {
506 if (it->find(ANDROID_REQUEST_ID).count == 0) {
507 CLOGE("RequestID entry exists; but must not be empty in metadata");
508 return BAD_VALUE;
509 }
510 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
511 } else {
512 CLOGE("RequestID does not exist in metadata");
513 return BAD_VALUE;
514 }
515
Jianing Wei90e59c92014-03-12 18:29:36 -0700516 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700517
518 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700519 }
520 return OK;
521}
522
Jianing Weicb0652e2014-03-12 18:29:36 -0700523status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700526 List<const CameraMetadata> requests;
527 requests.push_back(request);
528 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529}
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532 const List<const CameraMetadata> &requests, bool repeating,
533 /*out*/
534 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700535 ATRACE_CALL();
536 Mutex::Autolock il(mInterfaceLock);
537 Mutex::Autolock l(mLock);
538
539 status_t res = checkStatusOkToCaptureLocked();
540 if (res != OK) {
541 // error logged by previous call
542 return res;
543 }
544
545 RequestList requestList;
546
547 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
548 if (res != OK) {
549 // error logged by previous call
550 return res;
551 }
552
553 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700554 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700556 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558
559 if (res == OK) {
560 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
561 if (res != OK) {
562 SET_ERR_L("Can't transition to active in %f seconds!",
563 kActiveTimeout/1e9);
564 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700565 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
566 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 } else {
568 CLOGE("Cannot queue request. Impossible.");
569 return BAD_VALUE;
570 }
571
572 return res;
573}
574
Jianing Weicb0652e2014-03-12 18:29:36 -0700575status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
576 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700577 ATRACE_CALL();
578
Jianing Weicb0652e2014-03-12 18:29:36 -0700579 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800581
Jianing Weicb0652e2014-03-12 18:29:36 -0700582status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
583 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800584 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700586 List<const CameraMetadata> requests;
587 requests.push_back(request);
588 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589}
590
Jianing Weicb0652e2014-03-12 18:29:36 -0700591status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
592 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700593 ATRACE_CALL();
594
Jianing Weicb0652e2014-03-12 18:29:36 -0700595 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700596}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597
598sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
599 const CameraMetadata &request) {
600 status_t res;
601
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700604 // Stream configuration failed due to unsupported configuration.
605 // Device back to unconfigured state. Client might try other configuraitons
606 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
607 CLOGE("No streams configured");
608 return NULL;
609 }
610 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700612 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 return NULL;
614 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700615 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 if (mStatus == STATUS_UNCONFIGURED) {
617 CLOGE("No streams configured");
618 return NULL;
619 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620 }
621
622 sp<CaptureRequest> newRequest = createCaptureRequest(request);
623 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800624}
625
Jianing Weicb0652e2014-03-12 18:29:36 -0700626status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800627 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700628 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800630
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800631 switch (mStatus) {
632 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700633 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 return INVALID_OPERATION;
635 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700636 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 case STATUS_UNCONFIGURED:
639 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800640 case STATUS_ACTIVE:
641 // OK
642 break;
643 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700644 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 return INVALID_OPERATION;
646 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700647 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700648
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700649 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650}
651
652status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
653 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700656 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800657}
658
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700659status_t Camera3Device::createInputStream(
660 uint32_t width, uint32_t height, int format, int *id) {
661 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700663 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
665 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700666
667 status_t res;
668 bool wasActive = false;
669
670 switch (mStatus) {
671 case STATUS_ERROR:
672 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
673 return INVALID_OPERATION;
674 case STATUS_UNINITIALIZED:
675 ALOGE("%s: Device not initialized", __FUNCTION__);
676 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 case STATUS_UNCONFIGURED:
678 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700679 // OK
680 break;
681 case STATUS_ACTIVE:
682 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700684 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700686 return res;
687 }
688 wasActive = true;
689 break;
690 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700692 return INVALID_OPERATION;
693 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700694 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700695
696 if (mInputStream != 0) {
697 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
698 return INVALID_OPERATION;
699 }
700
701 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
702 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700704
705 mInputStream = newStream;
706
707 *id = mNextStreamId++;
708
709 // Continue captures if active at start
710 if (wasActive) {
711 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
712 res = configureStreamsLocked();
713 if (res != OK) {
714 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
715 __FUNCTION__, mNextStreamId, strerror(-res), res);
716 return res;
717 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719 }
720
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700722 return OK;
723}
724
Igor Murashkin2fba5842013-04-22 14:03:54 -0700725
726status_t Camera3Device::createZslStream(
727 uint32_t width, uint32_t height,
728 int depth,
729 /*out*/
730 int *id,
731 sp<Camera3ZslStream>* zslStream) {
732 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700734 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700735 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
736 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700737
738 status_t res;
739 bool wasActive = false;
740
741 switch (mStatus) {
742 case STATUS_ERROR:
743 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
744 return INVALID_OPERATION;
745 case STATUS_UNINITIALIZED:
746 ALOGE("%s: Device not initialized", __FUNCTION__);
747 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 case STATUS_UNCONFIGURED:
749 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700750 // OK
751 break;
752 case STATUS_ACTIVE:
753 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700755 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700757 return res;
758 }
759 wasActive = true;
760 break;
761 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700763 return INVALID_OPERATION;
764 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700765 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700766
767 if (mInputStream != 0) {
768 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
769 return INVALID_OPERATION;
770 }
771
772 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
773 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700775
776 res = mOutputStreams.add(mNextStreamId, newStream);
777 if (res < 0) {
778 ALOGE("%s: Can't add new stream to set: %s (%d)",
779 __FUNCTION__, strerror(-res), res);
780 return res;
781 }
782 mInputStream = newStream;
783
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530784 mNeedConfig = true;
785
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 *id = mNextStreamId++;
787 *zslStream = newStream;
788
789 // Continue captures if active at start
790 if (wasActive) {
791 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
792 res = configureStreamsLocked();
793 if (res != OK) {
794 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
795 __FUNCTION__, mNextStreamId, strerror(-res), res);
796 return res;
797 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700799 }
800
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700801 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700802 return OK;
803}
804
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800805status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700806 uint32_t width, uint32_t height, int format, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700808 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 Mutex::Autolock l(mLock);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700810 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
811 mId, mNextStreamId, width, height, format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800812
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 status_t res;
814 bool wasActive = false;
815
816 switch (mStatus) {
817 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700818 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 return INVALID_OPERATION;
820 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700821 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 case STATUS_UNCONFIGURED:
824 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 // OK
826 break;
827 case STATUS_ACTIVE:
828 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700829 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700831 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 return res;
833 }
834 wasActive = true;
835 break;
836 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 return INVALID_OPERATION;
839 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841
842 sp<Camera3OutputStream> newStream;
843 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700844 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700845 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700846 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
847 return BAD_VALUE;
848 }
849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700851 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852 } else {
853 newStream = new Camera3OutputStream(mNextStreamId, consumer,
854 width, height, format);
855 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857
858 res = mOutputStreams.add(mNextStreamId, newStream);
859 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return res;
862 }
863
864 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700865 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866
867 // Continue captures if active at start
868 if (wasActive) {
869 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
870 res = configureStreamsLocked();
871 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700872 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
873 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return res;
875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800880}
881
882status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
883 ATRACE_CALL();
884 (void)outputId; (void)id;
885
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700886 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887 return INVALID_OPERATION;
888}
889
890
891status_t Camera3Device::getStreamInfo(int id,
892 uint32_t *width, uint32_t *height, uint32_t *format) {
893 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 switch (mStatus) {
898 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700899 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 return INVALID_OPERATION;
901 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 case STATUS_UNCONFIGURED:
905 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906 case STATUS_ACTIVE:
907 // OK
908 break;
909 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 return INVALID_OPERATION;
912 }
913
914 ssize_t idx = mOutputStreams.indexOfKey(id);
915 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700916 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 return idx;
918 }
919
920 if (width) *width = mOutputStreams[idx]->getWidth();
921 if (height) *height = mOutputStreams[idx]->getHeight();
922 if (format) *format = mOutputStreams[idx]->getFormat();
923
924 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800925}
926
927status_t Camera3Device::setStreamTransform(int id,
928 int transform) {
929 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800932
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 switch (mStatus) {
934 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 return INVALID_OPERATION;
937 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700938 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800939 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 case STATUS_UNCONFIGURED:
941 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 case STATUS_ACTIVE:
943 // OK
944 break;
945 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700946 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 return INVALID_OPERATION;
948 }
949
950 ssize_t idx = mOutputStreams.indexOfKey(id);
951 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700952 CLOGE("Stream %d does not exist",
953 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 return BAD_VALUE;
955 }
956
957 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800958}
959
960status_t Camera3Device::deleteStream(int id) {
961 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 Mutex::Autolock l(mLock);
964 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800965
Igor Murashkine2172be2013-05-28 15:31:39 -0700966 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
967
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 // CameraDevice semantics require device to already be idle before
969 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700971 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
972 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 }
974
Igor Murashkin2fba5842013-04-22 14:03:54 -0700975 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800976 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800977 if (mInputStream != NULL && id == mInputStream->getId()) {
978 deletedStream = mInputStream;
979 mInputStream.clear();
980 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800981 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700982 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 return BAD_VALUE;
984 }
Zhijun He5f446352014-01-22 09:49:33 -0800985 }
986
987 // Delete output stream or the output part of a bi-directional stream.
988 if (outputStreamIdx != NAME_NOT_FOUND) {
989 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 mOutputStreams.removeItem(id);
991 }
992
993 // Free up the stream endpoint so that it can be used by some other stream
994 res = deletedStream->disconnect();
995 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 // fall through since we want to still list the stream as deleted.
998 }
999 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001000 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001
1002 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001003}
1004
1005status_t Camera3Device::deleteReprocessStream(int id) {
1006 ATRACE_CALL();
1007 (void)id;
1008
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001009 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001010 return INVALID_OPERATION;
1011}
1012
Igor Murashkine2d167e2014-08-19 16:19:59 -07001013status_t Camera3Device::configureStreams() {
1014 ATRACE_CALL();
1015 ALOGV("%s: E", __FUNCTION__);
1016
1017 Mutex::Autolock il(mInterfaceLock);
1018 Mutex::Autolock l(mLock);
1019
1020 return configureStreamsLocked();
1021}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001022
1023status_t Camera3Device::createDefaultRequest(int templateId,
1024 CameraMetadata *request) {
1025 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001026 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen116a1892016-03-09 12:21:01 -08001027
1028 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1029 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1030 IPCThreadState::self()->getCallingUid(), NULL, 0);
1031 return BAD_VALUE;
1032 }
1033
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001034 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001035 Mutex::Autolock l(mLock);
1036
1037 switch (mStatus) {
1038 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001039 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001040 return INVALID_OPERATION;
1041 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001042 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001043 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001044 case STATUS_UNCONFIGURED:
1045 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001046 case STATUS_ACTIVE:
1047 // OK
1048 break;
1049 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001050 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001051 return INVALID_OPERATION;
1052 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001053
Zhijun Hea1530f12014-09-14 12:44:20 -07001054 if (!mRequestTemplateCache[templateId].isEmpty()) {
1055 *request = mRequestTemplateCache[templateId];
1056 return OK;
1057 }
1058
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001059 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001060 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001061 rawRequest = mHal3Device->ops->construct_default_request_settings(
1062 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001063 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001064 if (rawRequest == NULL) {
1065 SET_ERR_L("HAL is unable to construct default settings for template %d",
1066 templateId);
1067 return DEAD_OBJECT;
1068 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001069 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001070 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001071
1072 return OK;
1073}
1074
1075status_t Camera3Device::waitUntilDrained() {
1076 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001077 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001078 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001079
Zhijun He69a37482014-03-23 18:44:49 -07001080 return waitUntilDrainedLocked();
1081}
1082
1083status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 switch (mStatus) {
1085 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001086 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 ALOGV("%s: Already idle", __FUNCTION__);
1088 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001089 case STATUS_CONFIGURED:
1090 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001091 case STATUS_ERROR:
1092 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001093 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 break;
1095 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001096 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001097 return INVALID_OPERATION;
1098 }
1099
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001100 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1101 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001102 if (res != OK) {
1103 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1104 res);
1105 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001106 return res;
1107}
1108
1109// Pause to reconfigure
1110status_t Camera3Device::internalPauseAndWaitLocked() {
1111 mRequestThread->setPaused(true);
1112 mPauseStateNotify = true;
1113
1114 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1115 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1116 if (res != OK) {
1117 SET_ERR_L("Can't idle device in %f seconds!",
1118 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 }
1120
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001121 return res;
1122}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001123
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001124// Resume after internalPauseAndWaitLocked
1125status_t Camera3Device::internalResumeLocked() {
1126 status_t res;
1127
1128 mRequestThread->setPaused(false);
1129
1130 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1131 if (res != OK) {
1132 SET_ERR_L("Can't transition to active in %f seconds!",
1133 kActiveTimeout/1e9);
1134 }
1135 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001136 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001137}
1138
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001139status_t Camera3Device::waitUntilStateThenRelock(bool active,
1140 nsecs_t timeout) {
1141 status_t res = OK;
1142 if (active == (mStatus == STATUS_ACTIVE)) {
1143 // Desired state already reached
1144 return res;
1145 }
1146
1147 bool stateSeen = false;
1148 do {
1149 mRecentStatusUpdates.clear();
1150
1151 res = mStatusChanged.waitRelative(mLock, timeout);
1152 if (res != OK) break;
1153
1154 // Check state change history during wait
1155 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1156 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1157 stateSeen = true;
1158 break;
1159 }
1160 }
1161 } while (!stateSeen);
1162
1163 return res;
1164}
1165
1166
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001167status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1168 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001169 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001170
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001171 if (listener != NULL && mListener != NULL) {
1172 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1173 }
1174 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001175 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001176
1177 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001178}
1179
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001180bool Camera3Device::willNotify3A() {
1181 return false;
1182}
1183
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001184status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001185 status_t res;
1186 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001187
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001188 while (mResultQueue.empty()) {
1189 res = mResultSignal.waitRelative(mOutputLock, timeout);
1190 if (res == TIMED_OUT) {
1191 return res;
1192 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001193 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001194 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001195 return res;
1196 }
1197 }
1198 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001199}
1200
Jianing Weicb0652e2014-03-12 18:29:36 -07001201status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001202 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001203 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001204
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001205 if (mResultQueue.empty()) {
1206 return NOT_ENOUGH_DATA;
1207 }
1208
Jianing Weicb0652e2014-03-12 18:29:36 -07001209 if (frame == NULL) {
1210 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1211 return BAD_VALUE;
1212 }
1213
1214 CaptureResult &result = *(mResultQueue.begin());
1215 frame->mResultExtras = result.mResultExtras;
1216 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001217 mResultQueue.erase(mResultQueue.begin());
1218
1219 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220}
1221
1222status_t Camera3Device::triggerAutofocus(uint32_t id) {
1223 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001224 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001226 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1227 // Mix-in this trigger into the next request and only the next request.
1228 RequestTrigger trigger[] = {
1229 {
1230 ANDROID_CONTROL_AF_TRIGGER,
1231 ANDROID_CONTROL_AF_TRIGGER_START
1232 },
1233 {
1234 ANDROID_CONTROL_AF_TRIGGER_ID,
1235 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001236 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001237 };
1238
1239 return mRequestThread->queueTrigger(trigger,
1240 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001241}
1242
1243status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1244 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001245 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001247 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1248 // Mix-in this trigger into the next request and only the next request.
1249 RequestTrigger trigger[] = {
1250 {
1251 ANDROID_CONTROL_AF_TRIGGER,
1252 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1253 },
1254 {
1255 ANDROID_CONTROL_AF_TRIGGER_ID,
1256 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001257 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001258 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001260 return mRequestThread->queueTrigger(trigger,
1261 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001262}
1263
1264status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1265 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001266 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001267
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001268 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1269 // Mix-in this trigger into the next request and only the next request.
1270 RequestTrigger trigger[] = {
1271 {
1272 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1273 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1274 },
1275 {
1276 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1277 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001278 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001279 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001280
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001281 return mRequestThread->queueTrigger(trigger,
1282 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001283}
1284
1285status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1286 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1287 ATRACE_CALL();
1288 (void)reprocessStreamId; (void)buffer; (void)listener;
1289
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001290 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001291 return INVALID_OPERATION;
1292}
1293
Jianing Weicb0652e2014-03-12 18:29:36 -07001294status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001295 ATRACE_CALL();
1296 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001297 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001298
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001299 NotificationListener* listener;
1300 {
1301 Mutex::Autolock l(mOutputLock);
1302 listener = mListener;
1303 }
1304
Zhijun He7ef20392014-04-21 16:04:17 -07001305 {
1306 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001307 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001308 }
1309
Zhijun He491e3412013-12-27 10:57:44 -08001310 status_t res;
1311 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1312 res = mHal3Device->ops->flush(mHal3Device);
1313 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001314 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001315 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001316 }
1317
1318 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001319}
1320
Zhijun He204e3292014-07-14 17:09:23 -07001321uint32_t Camera3Device::getDeviceVersion() {
1322 ATRACE_CALL();
1323 Mutex::Autolock il(mInterfaceLock);
1324 return mDeviceVersion;
1325}
1326
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001327/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001328 * Methods called by subclasses
1329 */
1330
1331void Camera3Device::notifyStatus(bool idle) {
1332 {
1333 // Need mLock to safely update state and synchronize to current
1334 // state of methods in flight.
1335 Mutex::Autolock l(mLock);
1336 // We can get various system-idle notices from the status tracker
1337 // while starting up. Only care about them if we've actually sent
1338 // in some requests recently.
1339 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1340 return;
1341 }
1342 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1343 idle ? "idle" : "active");
1344 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1345 mRecentStatusUpdates.add(mStatus);
1346 mStatusChanged.signal();
1347
1348 // Skip notifying listener if we're doing some user-transparent
1349 // state changes
1350 if (mPauseStateNotify) return;
1351 }
1352 NotificationListener *listener;
1353 {
1354 Mutex::Autolock l(mOutputLock);
1355 listener = mListener;
1356 }
1357 if (idle && listener != NULL) {
1358 listener->notifyIdle();
1359 }
1360}
1361
1362/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001363 * Camera3Device private methods
1364 */
1365
1366sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1367 const CameraMetadata &request) {
1368 ATRACE_CALL();
1369 status_t res;
1370
1371 sp<CaptureRequest> newRequest = new CaptureRequest;
1372 newRequest->mSettings = request;
1373
1374 camera_metadata_entry_t inputStreams =
1375 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1376 if (inputStreams.count > 0) {
1377 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001378 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001379 CLOGE("Request references unknown input stream %d",
1380 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 return NULL;
1382 }
1383 // Lazy completion of stream configuration (allocation/registration)
1384 // on first use
1385 if (mInputStream->isConfiguring()) {
1386 res = mInputStream->finishConfiguration(mHal3Device);
1387 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001388 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001390 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001391 return NULL;
1392 }
1393 }
1394
1395 newRequest->mInputStream = mInputStream;
1396 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1397 }
1398
1399 camera_metadata_entry_t streams =
1400 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1401 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001402 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001403 return NULL;
1404 }
1405
1406 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001407 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001408 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001409 CLOGE("Request references unknown stream %d",
1410 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001411 return NULL;
1412 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001413 sp<Camera3OutputStreamInterface> stream =
1414 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415
1416 // Lazy completion of stream configuration (allocation/registration)
1417 // on first use
1418 if (stream->isConfiguring()) {
1419 res = stream->finishConfiguration(mHal3Device);
1420 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001421 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1422 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001423 return NULL;
1424 }
1425 }
1426
1427 newRequest->mOutputStreams.push(stream);
1428 }
1429 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1430
1431 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001432}
1433
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434status_t Camera3Device::configureStreamsLocked() {
1435 ATRACE_CALL();
1436 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001437
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001438 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001439 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001440 return INVALID_OPERATION;
1441 }
1442
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001443 if (!mNeedConfig) {
1444 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1445 return OK;
1446 }
1447
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001448 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1449 // adding a dummy stream instead.
1450 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1451 if (mOutputStreams.size() == 0) {
1452 addDummyStreamLocked();
1453 } else {
1454 tryRemoveDummyStreamLocked();
1455 }
1456
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001457 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001458 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001459
1460 camera3_stream_configuration config;
1461
1462 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1463
1464 Vector<camera3_stream_t*> streams;
1465 streams.setCapacity(config.num_streams);
1466
1467 if (mInputStream != NULL) {
1468 camera3_stream_t *inputStream;
1469 inputStream = mInputStream->startConfiguration();
1470 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001471 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001472 return INVALID_OPERATION;
1473 }
1474 streams.add(inputStream);
1475 }
1476
1477 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001478
1479 // Don't configure bidi streams twice, nor add them twice to the list
1480 if (mOutputStreams[i].get() ==
1481 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1482
1483 config.num_streams--;
1484 continue;
1485 }
1486
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001487 camera3_stream_t *outputStream;
1488 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1489 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001490 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001491 return INVALID_OPERATION;
1492 }
1493 streams.add(outputStream);
1494 }
1495
1496 config.streams = streams.editArray();
1497
1498 // Do the HAL configuration; will potentially touch stream
1499 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001500 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001502 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001503
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001504 if (res == BAD_VALUE) {
1505 // HAL rejected this set of streams as unsupported, clean up config
1506 // attempt and return to unconfigured state
1507 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1508 res = mInputStream->cancelConfiguration();
1509 if (res != OK) {
1510 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1511 mInputStream->getId(), strerror(-res), res);
1512 return res;
1513 }
1514 }
1515
1516 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1517 sp<Camera3OutputStreamInterface> outputStream =
1518 mOutputStreams.editValueAt(i);
1519 if (outputStream->isConfiguring()) {
1520 res = outputStream->cancelConfiguration();
1521 if (res != OK) {
1522 SET_ERR_L(
1523 "Can't cancel configuring output stream %d: %s (%d)",
1524 outputStream->getId(), strerror(-res), res);
1525 return res;
1526 }
1527 }
1528 }
1529
1530 // Return state to that at start of call, so that future configures
1531 // properly clean things up
1532 mStatus = STATUS_UNCONFIGURED;
1533 mNeedConfig = true;
1534
1535 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1536 return BAD_VALUE;
1537 } else if (res != OK) {
1538 // Some other kind of error from configure_streams - this is not
1539 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001540 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1541 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001542 return res;
1543 }
1544
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001545 // Finish all stream configuration immediately.
1546 // TODO: Try to relax this later back to lazy completion, which should be
1547 // faster
1548
Igor Murashkin073f8572013-05-02 14:59:28 -07001549 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001550 res = mInputStream->finishConfiguration(mHal3Device);
1551 if (res != OK) {
1552 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1553 mInputStream->getId(), strerror(-res), res);
1554 return res;
1555 }
1556 }
1557
1558 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001559 sp<Camera3OutputStreamInterface> outputStream =
1560 mOutputStreams.editValueAt(i);
1561 if (outputStream->isConfiguring()) {
1562 res = outputStream->finishConfiguration(mHal3Device);
1563 if (res != OK) {
1564 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1565 outputStream->getId(), strerror(-res), res);
1566 return res;
1567 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001568 }
1569 }
1570
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 // Request thread needs to know to avoid using repeat-last-settings protocol
1572 // across configure_streams() calls
1573 mRequestThread->configurationComplete();
1574
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001575 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001577 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001579 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001580 mStatus = STATUS_CONFIGURED;
1581 } else {
1582 mStatus = STATUS_UNCONFIGURED;
1583 }
1584
1585 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1586
Zhijun He0a210512014-07-24 13:45:15 -07001587 // tear down the deleted streams after configure streams.
1588 mDeletedStreams.clear();
1589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001590 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001591}
1592
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001593status_t Camera3Device::addDummyStreamLocked() {
1594 ATRACE_CALL();
1595 status_t res;
1596
1597 if (mDummyStreamId != NO_STREAM) {
1598 // Should never be adding a second dummy stream when one is already
1599 // active
1600 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1601 __FUNCTION__, mId);
1602 return INVALID_OPERATION;
1603 }
1604
1605 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1606
1607 sp<Camera3OutputStreamInterface> dummyStream =
1608 new Camera3DummyStream(mNextStreamId);
1609
1610 res = mOutputStreams.add(mNextStreamId, dummyStream);
1611 if (res < 0) {
1612 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1613 return res;
1614 }
1615
1616 mDummyStreamId = mNextStreamId;
1617 mNextStreamId++;
1618
1619 return OK;
1620}
1621
1622status_t Camera3Device::tryRemoveDummyStreamLocked() {
1623 ATRACE_CALL();
1624 status_t res;
1625
1626 if (mDummyStreamId == NO_STREAM) return OK;
1627 if (mOutputStreams.size() == 1) return OK;
1628
1629 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1630
1631 // Ok, have a dummy stream and there's at least one other output stream,
1632 // so remove the dummy
1633
1634 sp<Camera3StreamInterface> deletedStream;
1635 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1636 if (outputStreamIdx == NAME_NOT_FOUND) {
1637 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1638 return INVALID_OPERATION;
1639 }
1640
1641 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1642 mOutputStreams.removeItemsAt(outputStreamIdx);
1643
1644 // Free up the stream endpoint so that it can be used by some other stream
1645 res = deletedStream->disconnect();
1646 if (res != OK) {
1647 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1648 // fall through since we want to still list the stream as deleted.
1649 }
1650 mDeletedStreams.add(deletedStream);
1651 mDummyStreamId = NO_STREAM;
1652
1653 return res;
1654}
1655
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001656void Camera3Device::setErrorState(const char *fmt, ...) {
1657 Mutex::Autolock l(mLock);
1658 va_list args;
1659 va_start(args, fmt);
1660
1661 setErrorStateLockedV(fmt, args);
1662
1663 va_end(args);
1664}
1665
1666void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1667 Mutex::Autolock l(mLock);
1668 setErrorStateLockedV(fmt, args);
1669}
1670
1671void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1672 va_list args;
1673 va_start(args, fmt);
1674
1675 setErrorStateLockedV(fmt, args);
1676
1677 va_end(args);
1678}
1679
1680void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001681 // Print out all error messages to log
1682 String8 errorCause = String8::formatV(fmt, args);
1683 ALOGE("Camera %d: %s", mId, errorCause.string());
1684
1685 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001686 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001687
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001688 mErrorCause = errorCause;
1689
1690 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001691 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001692
1693 // Notify upstream about a device error
1694 if (mListener != NULL) {
1695 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1696 CaptureResultExtras());
1697 }
1698
1699 // Save stack trace. View by dumping it later.
1700 CameraTraces::saveTrace();
1701 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001702}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001703
1704/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001705 * In-flight request management
1706 */
1707
Jianing Weicb0652e2014-03-12 18:29:36 -07001708status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001709 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001710 ATRACE_CALL();
1711 Mutex::Autolock l(mInFlightLock);
1712
1713 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001714 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001715 if (res < 0) return res;
1716
1717 return OK;
1718}
1719
1720/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001721 * Check if all 3A fields are ready, and send off a partial 3A-only result
1722 * to the output frame queue
1723 */
Zhijun He204e3292014-07-14 17:09:23 -07001724bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001725 uint32_t frameNumber,
1726 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001727
1728 // Check if all 3A states are present
1729 // The full list of fields is
1730 // android.control.afMode
1731 // android.control.awbMode
1732 // android.control.aeState
1733 // android.control.awbState
1734 // android.control.afState
1735 // android.control.afTriggerID
1736 // android.control.aePrecaptureID
1737 // TODO: Add android.control.aeMode
1738
1739 bool gotAllStates = true;
1740
1741 uint8_t afMode;
1742 uint8_t awbMode;
1743 uint8_t aeState;
1744 uint8_t afState;
1745 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001746
1747 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1748 &afMode, frameNumber);
1749
1750 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1751 &awbMode, frameNumber);
1752
1753 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1754 &aeState, frameNumber);
1755
1756 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1757 &afState, frameNumber);
1758
1759 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1760 &awbState, frameNumber);
1761
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001762 if (!gotAllStates) return false;
1763
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001764 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001765 "AF state %d, AE state %d, AWB state %d, "
1766 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001767 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001768 afMode, awbMode,
1769 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001770 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001771
1772 // Got all states, so construct a minimal result to send
1773 // In addition to the above fields, this means adding in
1774 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001775 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001776 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001777
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001778 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001779
1780 Mutex::Autolock l(mOutputLock);
1781
Jianing Weicb0652e2014-03-12 18:29:36 -07001782 CaptureResult captureResult;
1783 captureResult.mResultExtras = resultExtras;
1784 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1785 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1786 // but not limited to CameraDeviceBase::getNextResult
1787 CaptureResult& min3AResult =
1788 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001789
Jianing Weicb0652e2014-03-12 18:29:36 -07001790 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1791 // TODO: This is problematic casting. Need to fix CameraMetadata.
1792 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001793 return false;
1794 }
1795
Jianing Weicb0652e2014-03-12 18:29:36 -07001796 int32_t requestId = resultExtras.requestId;
1797 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001798 &requestId, frameNumber)) {
1799 return false;
1800 }
1801
Zhijun He204e3292014-07-14 17:09:23 -07001802 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1803 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1804 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1805 &partialResult, frameNumber)) {
1806 return false;
1807 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001808 }
1809
Jianing Weicb0652e2014-03-12 18:29:36 -07001810 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001811 &afMode, frameNumber)) {
1812 return false;
1813 }
1814
Jianing Weicb0652e2014-03-12 18:29:36 -07001815 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001816 &awbMode, frameNumber)) {
1817 return false;
1818 }
1819
Jianing Weicb0652e2014-03-12 18:29:36 -07001820 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001821 &aeState, frameNumber)) {
1822 return false;
1823 }
1824
Jianing Weicb0652e2014-03-12 18:29:36 -07001825 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001826 &afState, frameNumber)) {
1827 return false;
1828 }
1829
Jianing Weicb0652e2014-03-12 18:29:36 -07001830 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001831 &awbState, frameNumber)) {
1832 return false;
1833 }
1834
Jianing Weicb0652e2014-03-12 18:29:36 -07001835 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001836 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001837 return false;
1838 }
1839
Jianing Weicb0652e2014-03-12 18:29:36 -07001840 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001841 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001842 return false;
1843 }
1844
Zhijun He204e3292014-07-14 17:09:23 -07001845 // We only send the aggregated partial when all 3A related metadata are available
1846 // For both API1 and API2.
1847 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001848 mResultSignal.signal();
1849
1850 return true;
1851}
1852
1853template<typename T>
1854bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001855 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856 (void) frameNumber;
1857
1858 camera_metadata_ro_entry_t entry;
1859
1860 entry = result.find(tag);
1861 if (entry.count == 0) {
1862 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1863 mId, frameNumber, get_camera_metadata_tag_name(tag));
1864 return false;
1865 }
1866
1867 if (sizeof(T) == sizeof(uint8_t)) {
1868 *value = entry.data.u8[0];
1869 } else if (sizeof(T) == sizeof(int32_t)) {
1870 *value = entry.data.i32[0];
1871 } else {
1872 ALOGE("%s: Unexpected type", __FUNCTION__);
1873 return false;
1874 }
1875 return true;
1876}
1877
1878template<typename T>
1879bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001880 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001881 if (result.update(tag, value, 1) != NO_ERROR) {
1882 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1883 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1884 frameNumber, get_camera_metadata_tag_name(tag));
1885 return false;
1886 }
1887 return true;
1888}
1889
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001890
1891void Camera3Device::returnOutputBuffers(
1892 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1893 nsecs_t timestamp) {
1894 for (size_t i = 0; i < numBuffers; i++)
1895 {
1896 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1897 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1898 // Note: stream may be deallocated at this point, if this buffer was
1899 // the last reference to it.
1900 if (res != OK) {
1901 ALOGE("Can't return buffer to its stream: %s (%d)",
1902 strerror(-res), res);
1903 }
1904 }
1905}
1906
1907
1908void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1909
1910 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1911 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1912
1913 nsecs_t sensorTimestamp = request.sensorTimestamp;
1914 nsecs_t shutterTimestamp = request.shutterTimestamp;
1915
1916 // Check if it's okay to remove the request from InFlightMap:
1917 // In the case of a successful request:
1918 // all input and output buffers, all result metadata, shutter callback
1919 // arrived.
1920 // In the case of a unsuccessful request:
1921 // all input and output buffers arrived.
1922 if (request.numBuffersLeft == 0 &&
1923 (request.requestStatus != OK ||
1924 (request.haveResultMetadata && shutterTimestamp != 0))) {
1925 ATRACE_ASYNC_END("frame capture", frameNumber);
1926
1927 // Sanity check - if sensor timestamp matches shutter timestamp
1928 if (request.requestStatus == OK &&
1929 sensorTimestamp != shutterTimestamp) {
1930 SET_ERR("sensor timestamp (%" PRId64
1931 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
1932 sensorTimestamp, frameNumber, shutterTimestamp);
1933 }
1934
1935 // for an unsuccessful request, it may have pending output buffers to
1936 // return.
1937 assert(request.requestStatus != OK ||
1938 request.pendingOutputBuffers.size() == 0);
1939 returnOutputBuffers(request.pendingOutputBuffers.array(),
1940 request.pendingOutputBuffers.size(), 0);
1941
1942 mInFlightMap.removeItemsAt(idx, 1);
1943
1944 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
1945 }
1946
1947 // Sanity check - if we have too many in-flight frames, something has
1948 // likely gone wrong
1949 if (mInFlightMap.size() > kInFlightWarnLimit) {
1950 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
1951 }
1952}
1953
1954
1955void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
1956 CaptureResultExtras &resultExtras,
1957 CameraMetadata &collectedPartialResult,
1958 uint32_t frameNumber) {
1959 if (pendingMetadata.isEmpty())
1960 return;
1961
1962 Mutex::Autolock l(mOutputLock);
1963
1964 // TODO: need to track errors for tighter bounds on expected frame number
1965 if (frameNumber < mNextResultFrameNumber) {
1966 SET_ERR("Out-of-order capture result metadata submitted! "
1967 "(got frame number %d, expecting %d)",
1968 frameNumber, mNextResultFrameNumber);
1969 return;
1970 }
1971 mNextResultFrameNumber = frameNumber + 1;
1972
1973 CaptureResult captureResult;
1974 captureResult.mResultExtras = resultExtras;
1975 captureResult.mMetadata = pendingMetadata;
1976
1977 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1978 (int32_t*)&frameNumber, 1) != OK) {
1979 SET_ERR("Failed to set frame# in metadata (%d)",
1980 frameNumber);
1981 return;
1982 } else {
1983 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
1984 __FUNCTION__, mId, frameNumber);
1985 }
1986
1987 // Append any previous partials to form a complete result
1988 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
1989 captureResult.mMetadata.append(collectedPartialResult);
1990 }
1991
1992 captureResult.mMetadata.sort();
1993
1994 // Check that there's a timestamp in the result metadata
1995 camera_metadata_entry entry =
1996 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
1997 if (entry.count == 0) {
1998 SET_ERR("No timestamp provided by HAL for frame %d!",
1999 frameNumber);
2000 return;
2001 }
2002
2003 // Valid result, insert into queue
2004 List<CaptureResult>::iterator queuedResult =
2005 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2006 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2007 ", burstId = %" PRId32, __FUNCTION__,
2008 queuedResult->mResultExtras.requestId,
2009 queuedResult->mResultExtras.frameNumber,
2010 queuedResult->mResultExtras.burstId);
2011
2012 mResultSignal.signal();
2013}
2014
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002015/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002016 * Camera HAL device callback methods
2017 */
2018
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002019void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002020 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002021
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002022 status_t res;
2023
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002024 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002025 if (result->result == NULL && result->num_output_buffers == 0 &&
2026 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002027 SET_ERR("No result data provided by HAL for frame %d",
2028 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002029 return;
2030 }
Zhijun He204e3292014-07-14 17:09:23 -07002031
2032 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2033 // partial_result to 1 when metadata is included in this result.
2034 if (!mUsePartialResult &&
2035 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2036 result->result != NULL &&
2037 result->partial_result != 1) {
2038 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2039 " if partial result is not supported",
2040 frameNumber, result->partial_result);
2041 return;
2042 }
2043
2044 bool isPartialResult = false;
2045 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002046 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002047 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002048
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002049 // Get shutter timestamp and resultExtras from list of in-flight requests,
2050 // where it was added by the shutter notification for this frame. If the
2051 // shutter timestamp isn't received yet, append the output buffers to the
2052 // in-flight request and they will be returned when the shutter timestamp
2053 // arrives. Update the in-flight status and remove the in-flight entry if
2054 // all result data and shutter timestamp have been received.
2055 nsecs_t shutterTimestamp = 0;
2056
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002057 {
2058 Mutex::Autolock l(mInFlightLock);
2059 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2060 if (idx == NAME_NOT_FOUND) {
2061 SET_ERR("Unknown frame number for capture result: %d",
2062 frameNumber);
2063 return;
2064 }
2065 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002066 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2067 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2068 ", partialResultCount = %d",
2069 __FUNCTION__, request.resultExtras.requestId,
2070 request.resultExtras.frameNumber, request.resultExtras.burstId,
2071 result->partial_result);
2072 // Always update the partial count to the latest one if it's not 0
2073 // (buffers only). When framework aggregates adjacent partial results
2074 // into one, the latest partial count will be used.
2075 if (result->partial_result != 0)
2076 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002077
2078 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002079 if (mUsePartialResult && result->result != NULL) {
2080 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2081 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2082 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2083 " the range of [1, %d] when metadata is included in the result",
2084 frameNumber, result->partial_result, mNumPartialResults);
2085 return;
2086 }
2087 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002088 if (isPartialResult) {
2089 request.partialResult.collectedResult.append(result->result);
2090 }
Zhijun He204e3292014-07-14 17:09:23 -07002091 } else {
2092 camera_metadata_ro_entry_t partialResultEntry;
2093 res = find_camera_metadata_ro_entry(result->result,
2094 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2095 if (res != NAME_NOT_FOUND &&
2096 partialResultEntry.count > 0 &&
2097 partialResultEntry.data.u8[0] ==
2098 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2099 // A partial result. Flag this as such, and collect this
2100 // set of metadata into the in-flight entry.
2101 isPartialResult = true;
2102 request.partialResult.collectedResult.append(
2103 result->result);
2104 request.partialResult.collectedResult.erase(
2105 ANDROID_QUIRKS_PARTIAL_RESULT);
2106 }
2107 }
2108
2109 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002110 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002111 if (!request.partialResult.haveSent3A) {
2112 request.partialResult.haveSent3A =
2113 processPartial3AResult(frameNumber,
2114 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002115 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002116 }
2117 }
2118 }
2119
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002120 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002121 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002122
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002123 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002124 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002125 if (request.haveResultMetadata) {
2126 SET_ERR("Called multiple times with metadata for frame %d",
2127 frameNumber);
2128 return;
2129 }
Zhijun He204e3292014-07-14 17:09:23 -07002130 if (mUsePartialResult &&
2131 !request.partialResult.collectedResult.isEmpty()) {
2132 collectedPartialResult.acquire(
2133 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002134 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002135 request.haveResultMetadata = true;
2136 }
2137
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002138 uint32_t numBuffersReturned = result->num_output_buffers;
2139 if (result->input_buffer != NULL) {
2140 if (hasInputBufferInRequest) {
2141 numBuffersReturned += 1;
2142 } else {
2143 ALOGW("%s: Input buffer should be NULL if there is no input"
2144 " buffer sent in the request",
2145 __FUNCTION__);
2146 }
2147 }
2148 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002149 if (request.numBuffersLeft < 0) {
2150 SET_ERR("Too many buffers returned for frame %d",
2151 frameNumber);
2152 return;
2153 }
2154
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002155 camera_metadata_ro_entry_t entry;
2156 res = find_camera_metadata_ro_entry(result->result,
2157 ANDROID_SENSOR_TIMESTAMP, &entry);
2158 if (res == OK && entry.count == 1) {
2159 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002160 }
2161
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002162 // If shutter event isn't received yet, append the output buffers to
2163 // the in-flight request. Otherwise, return the output buffers to
2164 // streams.
2165 if (shutterTimestamp == 0) {
2166 request.pendingOutputBuffers.appendArray(result->output_buffers,
2167 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002168 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002169 returnOutputBuffers(result->output_buffers,
2170 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002171 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002172
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002173 if (result->result != NULL && !isPartialResult) {
2174 if (shutterTimestamp == 0) {
2175 request.pendingMetadata = result->result;
2176 request.partialResult.collectedResult = collectedPartialResult;
2177 } else {
2178 CameraMetadata metadata;
2179 metadata = result->result;
2180 sendCaptureResult(metadata, request.resultExtras,
2181 collectedPartialResult, frameNumber);
2182 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002183 }
2184
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002185 removeInFlightRequestIfReadyLocked(idx);
2186 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002187
Zhijun Hef0d962a2014-06-30 10:24:11 -07002188 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002189 if (hasInputBufferInRequest) {
2190 Camera3Stream *stream =
2191 Camera3Stream::cast(result->input_buffer->stream);
2192 res = stream->returnInputBuffer(*(result->input_buffer));
2193 // Note: stream may be deallocated at this point, if this buffer was the
2194 // last reference to it.
2195 if (res != OK) {
2196 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2197 " its stream:%s (%d)", __FUNCTION__,
2198 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002199 }
2200 } else {
2201 ALOGW("%s: Input buffer should be NULL if there is no input"
2202 " buffer sent in the request, skipping input buffer return.",
2203 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002204 }
2205 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002206}
2207
2208void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002209 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002210 NotificationListener *listener;
2211 {
2212 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002213 listener = mListener;
2214 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002215
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002216 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002217 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002218 return;
2219 }
2220
2221 switch (msg->type) {
2222 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002223 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002224 break;
2225 }
2226 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002227 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002228 break;
2229 }
2230 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002231 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002232 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002233 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002234}
2235
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002236void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2237 NotificationListener *listener) {
2238
2239 // Map camera HAL error codes to ICameraDeviceCallback error codes
2240 // Index into this with the HAL error code
2241 static const ICameraDeviceCallbacks::CameraErrorCode
2242 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2243 // 0 = Unused error code
2244 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2245 // 1 = CAMERA3_MSG_ERROR_DEVICE
2246 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2247 // 2 = CAMERA3_MSG_ERROR_REQUEST
2248 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2249 // 3 = CAMERA3_MSG_ERROR_RESULT
2250 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2251 // 4 = CAMERA3_MSG_ERROR_BUFFER
2252 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2253 };
2254
2255 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2256 ((msg.error_code >= 0) &&
2257 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2258 halErrorMap[msg.error_code] :
2259 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2260
2261 int streamId = 0;
2262 if (msg.error_stream != NULL) {
2263 Camera3Stream *stream =
2264 Camera3Stream::cast(msg.error_stream);
2265 streamId = stream->getId();
2266 }
2267 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2268 mId, __FUNCTION__, msg.frame_number,
2269 streamId, msg.error_code);
2270
2271 CaptureResultExtras resultExtras;
2272 switch (errorCode) {
2273 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2274 // SET_ERR calls notifyError
2275 SET_ERR("Camera HAL reported serious device error");
2276 break;
2277 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2278 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2279 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2280 {
2281 Mutex::Autolock l(mInFlightLock);
2282 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2283 if (idx >= 0) {
2284 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2285 r.requestStatus = msg.error_code;
2286 resultExtras = r.resultExtras;
2287 } else {
2288 resultExtras.frameNumber = msg.frame_number;
2289 ALOGE("Camera %d: %s: cannot find in-flight request on "
2290 "frame %" PRId64 " error", mId, __FUNCTION__,
2291 resultExtras.frameNumber);
2292 }
2293 }
2294 if (listener != NULL) {
2295 listener->notifyError(errorCode, resultExtras);
2296 } else {
2297 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2298 }
2299 break;
2300 default:
2301 // SET_ERR calls notifyError
2302 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2303 break;
2304 }
2305}
2306
2307void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2308 NotificationListener *listener) {
2309 ssize_t idx;
2310 // Verify ordering of shutter notifications
2311 {
2312 Mutex::Autolock l(mOutputLock);
2313 // TODO: need to track errors for tighter bounds on expected frame number.
2314 if (msg.frame_number < mNextShutterFrameNumber) {
2315 SET_ERR("Shutter notification out-of-order. Expected "
2316 "notification for frame %d, got frame %d",
2317 mNextShutterFrameNumber, msg.frame_number);
2318 return;
2319 }
2320 mNextShutterFrameNumber = msg.frame_number + 1;
2321 }
2322
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002323 // Set timestamp for the request in the in-flight tracking
2324 // and get the request ID to send upstream
2325 {
2326 Mutex::Autolock l(mInFlightLock);
2327 idx = mInFlightMap.indexOfKey(msg.frame_number);
2328 if (idx >= 0) {
2329 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002330
2331 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2332 mId, __FUNCTION__,
2333 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2334 // Call listener, if any
2335 if (listener != NULL) {
2336 listener->notifyShutter(r.resultExtras, msg.timestamp);
2337 }
2338
2339 r.shutterTimestamp = msg.timestamp;
2340
2341 // send pending result and buffers
2342 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2343 r.partialResult.collectedResult, msg.frame_number);
2344 returnOutputBuffers(r.pendingOutputBuffers.array(),
2345 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2346 r.pendingOutputBuffers.clear();
2347
2348 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002349 }
2350 }
2351 if (idx < 0) {
2352 SET_ERR("Shutter notification for non-existent frame number %d",
2353 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002354 }
2355}
2356
2357
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002358CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002359 ALOGV("%s", __FUNCTION__);
2360
Igor Murashkin1e479c02013-09-06 16:55:14 -07002361 CameraMetadata retVal;
2362
2363 if (mRequestThread != NULL) {
2364 retVal = mRequestThread->getLatestRequest();
2365 }
2366
Igor Murashkin1e479c02013-09-06 16:55:14 -07002367 return retVal;
2368}
2369
Jianing Weicb0652e2014-03-12 18:29:36 -07002370
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002371/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002372 * RequestThread inner class methods
2373 */
2374
2375Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002376 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002377 camera3_device_t *hal3Device) :
2378 Thread(false),
2379 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002380 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002381 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002382 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 mReconfigured(false),
2384 mDoPause(false),
2385 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002386 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002387 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002388 mCurrentAfTriggerId(0),
2389 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002390 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002391 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002392}
2393
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002394void Camera3Device::RequestThread::setNotifyCallback(
2395 NotificationListener *listener) {
2396 Mutex::Autolock l(mRequestLock);
2397 mListener = listener;
2398}
2399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002400void Camera3Device::RequestThread::configurationComplete() {
2401 Mutex::Autolock l(mRequestLock);
2402 mReconfigured = true;
2403}
2404
Jianing Wei90e59c92014-03-12 18:29:36 -07002405status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002406 List<sp<CaptureRequest> > &requests,
2407 /*out*/
2408 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002409 Mutex::Autolock l(mRequestLock);
2410 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2411 ++it) {
2412 mRequestQueue.push_back(*it);
2413 }
2414
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002415 if (lastFrameNumber != NULL) {
2416 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2417 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2418 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2419 *lastFrameNumber);
2420 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002421
Jianing Wei90e59c92014-03-12 18:29:36 -07002422 unpauseForNewRequests();
2423
2424 return OK;
2425}
2426
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002427
2428status_t Camera3Device::RequestThread::queueTrigger(
2429 RequestTrigger trigger[],
2430 size_t count) {
2431
2432 Mutex::Autolock l(mTriggerMutex);
2433 status_t ret;
2434
2435 for (size_t i = 0; i < count; ++i) {
2436 ret = queueTriggerLocked(trigger[i]);
2437
2438 if (ret != OK) {
2439 return ret;
2440 }
2441 }
2442
2443 return OK;
2444}
2445
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002446int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2447 sp<Camera3Device> d = device.promote();
2448 if (d != NULL) return d->mId;
2449 return 0;
2450}
2451
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002452status_t Camera3Device::RequestThread::queueTriggerLocked(
2453 RequestTrigger trigger) {
2454
2455 uint32_t tag = trigger.metadataTag;
2456 ssize_t index = mTriggerMap.indexOfKey(tag);
2457
2458 switch (trigger.getTagType()) {
2459 case TYPE_BYTE:
2460 // fall-through
2461 case TYPE_INT32:
2462 break;
2463 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002464 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2465 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002466 return INVALID_OPERATION;
2467 }
2468
2469 /**
2470 * Collect only the latest trigger, since we only have 1 field
2471 * in the request settings per trigger tag, and can't send more than 1
2472 * trigger per request.
2473 */
2474 if (index != NAME_NOT_FOUND) {
2475 mTriggerMap.editValueAt(index) = trigger;
2476 } else {
2477 mTriggerMap.add(tag, trigger);
2478 }
2479
2480 return OK;
2481}
2482
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002483status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002484 const RequestList &requests,
2485 /*out*/
2486 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002487 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002488 if (lastFrameNumber != NULL) {
2489 *lastFrameNumber = mRepeatingLastFrameNumber;
2490 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002491 mRepeatingRequests.clear();
2492 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2493 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002494
2495 unpauseForNewRequests();
2496
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002497 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002498 return OK;
2499}
2500
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002501bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2502 if (mRepeatingRequests.empty()) {
2503 return false;
2504 }
2505 int32_t requestId = requestIn->mResultExtras.requestId;
2506 const RequestList &repeatRequests = mRepeatingRequests;
2507 // All repeating requests are guaranteed to have same id so only check first quest
2508 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2509 return (firstRequest->mResultExtras.requestId == requestId);
2510}
2511
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002512status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513 Mutex::Autolock l(mRequestLock);
2514 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002515 if (lastFrameNumber != NULL) {
2516 *lastFrameNumber = mRepeatingLastFrameNumber;
2517 }
2518 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002519 return OK;
2520}
2521
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002522status_t Camera3Device::RequestThread::clear(
2523 NotificationListener *listener,
2524 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002525 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002526 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002527
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002528 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002529
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002530 // Send errors for all requests pending in the request queue, including
2531 // pending repeating requests
2532 if (listener != NULL) {
2533 for (RequestList::iterator it = mRequestQueue.begin();
2534 it != mRequestQueue.end(); ++it) {
2535 // Set the frame number this request would have had, if it
2536 // had been submitted; this frame number will not be reused.
2537 // The requestId and burstId fields were set when the request was
2538 // submitted originally (in convertMetadataListToRequestListLocked)
2539 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2540 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2541 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002542 }
2543 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002544 mRequestQueue.clear();
2545 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002546 if (lastFrameNumber != NULL) {
2547 *lastFrameNumber = mRepeatingLastFrameNumber;
2548 }
2549 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002550 return OK;
2551}
2552
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002553void Camera3Device::RequestThread::setPaused(bool paused) {
2554 Mutex::Autolock l(mPauseLock);
2555 mDoPause = paused;
2556 mDoPauseSignal.signal();
2557}
2558
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002559status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2560 int32_t requestId, nsecs_t timeout) {
2561 Mutex::Autolock l(mLatestRequestMutex);
2562 status_t res;
2563 while (mLatestRequestId != requestId) {
2564 nsecs_t startTime = systemTime();
2565
2566 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2567 if (res != OK) return res;
2568
2569 timeout -= (systemTime() - startTime);
2570 }
2571
2572 return OK;
2573}
2574
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002575void Camera3Device::RequestThread::requestExit() {
2576 // Call parent to set up shutdown
2577 Thread::requestExit();
2578 // The exit from any possible waits
2579 mDoPauseSignal.signal();
2580 mRequestSignal.signal();
2581}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002582
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002583bool Camera3Device::RequestThread::threadLoop() {
2584
2585 status_t res;
2586
2587 // Handle paused state.
2588 if (waitIfPaused()) {
2589 return true;
2590 }
2591
2592 // Get work to do
2593
2594 sp<CaptureRequest> nextRequest = waitForNextRequest();
2595 if (nextRequest == NULL) {
2596 return true;
2597 }
2598
2599 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002600 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002601 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002602 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002603
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002604 // Get the request ID, if any
2605 int requestId;
2606 camera_metadata_entry_t requestIdEntry =
2607 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2608 if (requestIdEntry.count > 0) {
2609 requestId = requestIdEntry.data.i32[0];
2610 } else {
2611 ALOGW("%s: Did not have android.request.id set in the request",
2612 __FUNCTION__);
2613 requestId = NAME_NOT_FOUND;
2614 }
2615
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002616 // Insert any queued triggers (before metadata is locked)
2617 int32_t triggerCount;
2618 res = insertTriggers(nextRequest);
2619 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002620 SET_ERR("RequestThread: Unable to insert triggers "
2621 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002622 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002623 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2624 return false;
2625 }
2626 triggerCount = res;
2627
2628 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2629
2630 // If the request is the same as last, or we had triggers last time
2631 if (mPrevRequest != nextRequest || triggersMixedIn) {
2632 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002633 * HAL workaround:
2634 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2635 */
2636 res = addDummyTriggerIds(nextRequest);
2637 if (res != OK) {
2638 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2639 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002640 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002641 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2642 return false;
2643 }
2644
2645 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002646 * The request should be presorted so accesses in HAL
2647 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2648 */
2649 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002650 request.settings = nextRequest->mSettings.getAndLock();
2651 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002652 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2653
2654 IF_ALOGV() {
2655 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2656 find_camera_metadata_ro_entry(
2657 request.settings,
2658 ANDROID_CONTROL_AF_TRIGGER,
2659 &e
2660 );
2661 if (e.count > 0) {
2662 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2663 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002664 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002665 e.data.u8[0]);
2666 }
2667 }
2668 } else {
2669 // leave request.settings NULL to indicate 'reuse latest given'
2670 ALOGVV("%s: Request settings are REUSED",
2671 __FUNCTION__);
2672 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002673
2674 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002675 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676
2677 // Fill in buffers
2678
2679 if (nextRequest->mInputStream != NULL) {
2680 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002681 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002682 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002683 // Can't get input buffer from gralloc queue - this could be due to
2684 // disconnected queue or other producer misbehavior, so not a fatal
2685 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002686 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002688 Mutex::Autolock l(mRequestLock);
2689 if (mListener != NULL) {
2690 mListener->notifyError(
2691 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2692 nextRequest->mResultExtras);
2693 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002694 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2695 return true;
2696 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002697 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002698 } else {
2699 request.input_buffer = NULL;
2700 }
2701
2702 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2703 nextRequest->mOutputStreams.size());
2704 request.output_buffers = outputBuffers.array();
2705 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2706 res = nextRequest->mOutputStreams.editItemAt(i)->
2707 getBuffer(&outputBuffers.editItemAt(i));
2708 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002709 // Can't get output buffer from gralloc queue - this could be due to
2710 // abandoned queue or other consumer misbehavior, so not a fatal
2711 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002712 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2713 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002714 Mutex::Autolock l(mRequestLock);
2715 if (mListener != NULL) {
2716 mListener->notifyError(
2717 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2718 nextRequest->mResultExtras);
2719 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002720 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2721 return true;
2722 }
2723 request.num_output_buffers++;
2724 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002725 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002726
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002727 // Log request in the in-flight queue
2728 sp<Camera3Device> parent = mParent.promote();
2729 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002730 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002731 CLOGE("RequestThread: Parent is gone");
2732 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2733 return false;
2734 }
2735
Jianing Weicb0652e2014-03-12 18:29:36 -07002736 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002737 totalNumBuffers, nextRequest->mResultExtras,
2738 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002739 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2740 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002741 __FUNCTION__,
2742 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2743 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002744 if (res != OK) {
2745 SET_ERR("RequestThread: Unable to register new in-flight request:"
2746 " %s (%d)", strerror(-res), res);
2747 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2748 return false;
2749 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002750
Zhijun Hecc27e112013-10-03 16:12:43 -07002751 // Inform waitUntilRequestProcessed thread of a new request ID
2752 {
2753 Mutex::Autolock al(mLatestRequestMutex);
2754
2755 mLatestRequestId = requestId;
2756 mLatestRequestSignal.signal();
2757 }
2758
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002759 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002760 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2761 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002762 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002763 ATRACE_END();
2764
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002765 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002766 // Should only get a failure here for malformed requests or device-level
2767 // errors, so consider all errors fatal. Bad metadata failures should
2768 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002769 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002770 " device: %s (%d)", request.frame_number, strerror(-res), res);
2771 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2772 return false;
2773 }
2774
Igor Murashkin1e479c02013-09-06 16:55:14 -07002775 // Update the latest request sent to HAL
2776 if (request.settings != NULL) { // Don't update them if they were unchanged
2777 Mutex::Autolock al(mLatestRequestMutex);
2778
2779 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2780 mLatestRequest.acquire(cloned);
2781 }
2782
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002783 if (request.settings != NULL) {
2784 nextRequest->mSettings.unlock(request.settings);
2785 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002786
2787 // Remove any previously queued triggers (after unlock)
2788 res = removeTriggers(mPrevRequest);
2789 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002790 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002791 "(capture request %d, HAL device: %s (%d)",
2792 request.frame_number, strerror(-res), res);
2793 return false;
2794 }
2795 mPrevTriggers = triggerCount;
2796
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002797 return true;
2798}
2799
Igor Murashkin1e479c02013-09-06 16:55:14 -07002800CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2801 Mutex::Autolock al(mLatestRequestMutex);
2802
2803 ALOGV("RequestThread::%s", __FUNCTION__);
2804
2805 return mLatestRequest;
2806}
2807
Jianing Weicb0652e2014-03-12 18:29:36 -07002808
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002809void Camera3Device::RequestThread::cleanUpFailedRequest(
2810 camera3_capture_request_t &request,
2811 sp<CaptureRequest> &nextRequest,
2812 Vector<camera3_stream_buffer_t> &outputBuffers) {
2813
2814 if (request.settings != NULL) {
2815 nextRequest->mSettings.unlock(request.settings);
2816 }
2817 if (request.input_buffer != NULL) {
2818 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002819 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002820 }
2821 for (size_t i = 0; i < request.num_output_buffers; i++) {
2822 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2823 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2824 outputBuffers[i], 0);
2825 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002826}
2827
2828sp<Camera3Device::CaptureRequest>
2829 Camera3Device::RequestThread::waitForNextRequest() {
2830 status_t res;
2831 sp<CaptureRequest> nextRequest;
2832
2833 // Optimized a bit for the simple steady-state case (single repeating
2834 // request), to avoid putting that request in the queue temporarily.
2835 Mutex::Autolock l(mRequestLock);
2836
2837 while (mRequestQueue.empty()) {
2838 if (!mRepeatingRequests.empty()) {
2839 // Always atomically enqueue all requests in a repeating request
2840 // list. Guarantees a complete in-sequence set of captures to
2841 // application.
2842 const RequestList &requests = mRepeatingRequests;
2843 RequestList::const_iterator firstRequest =
2844 requests.begin();
2845 nextRequest = *firstRequest;
2846 mRequestQueue.insert(mRequestQueue.end(),
2847 ++firstRequest,
2848 requests.end());
2849 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002850
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002851 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002852
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853 break;
2854 }
2855
2856 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2857
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002858 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2859 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002860 Mutex::Autolock pl(mPauseLock);
2861 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002862 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002863 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002864 // Let the tracker know
2865 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2866 if (statusTracker != 0) {
2867 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2868 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002869 }
2870 // Stop waiting for now and let thread management happen
2871 return NULL;
2872 }
2873 }
2874
2875 if (nextRequest == NULL) {
2876 // Don't have a repeating request already in hand, so queue
2877 // must have an entry now.
2878 RequestList::iterator firstRequest =
2879 mRequestQueue.begin();
2880 nextRequest = *firstRequest;
2881 mRequestQueue.erase(firstRequest);
2882 }
2883
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002884 // In case we've been unpaused by setPaused clearing mDoPause, need to
2885 // update internal pause state (capture/setRepeatingRequest unpause
2886 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002887 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002888 if (mPaused) {
2889 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2890 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2891 if (statusTracker != 0) {
2892 statusTracker->markComponentActive(mStatusId);
2893 }
2894 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002895 mPaused = false;
2896
2897 // Check if we've reconfigured since last time, and reset the preview
2898 // request if so. Can't use 'NULL request == repeat' across configure calls.
2899 if (mReconfigured) {
2900 mPrevRequest.clear();
2901 mReconfigured = false;
2902 }
2903
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002904 if (nextRequest != NULL) {
2905 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002906 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2907 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002908 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909 return nextRequest;
2910}
2911
2912bool Camera3Device::RequestThread::waitIfPaused() {
2913 status_t res;
2914 Mutex::Autolock l(mPauseLock);
2915 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002916 if (mPaused == false) {
2917 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002918 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2919 // Let the tracker know
2920 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2921 if (statusTracker != 0) {
2922 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2923 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002924 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002925
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002926 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002927 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002928 return true;
2929 }
2930 }
2931 // We don't set mPaused to false here, because waitForNextRequest needs
2932 // to further manage the paused state in case of starvation.
2933 return false;
2934}
2935
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002936void Camera3Device::RequestThread::unpauseForNewRequests() {
2937 // With work to do, mark thread as unpaused.
2938 // If paused by request (setPaused), don't resume, to avoid
2939 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002940 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002941 Mutex::Autolock p(mPauseLock);
2942 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002943 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2944 if (mPaused) {
2945 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2946 if (statusTracker != 0) {
2947 statusTracker->markComponentActive(mStatusId);
2948 }
2949 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002950 mPaused = false;
2951 }
2952}
2953
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002954void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2955 sp<Camera3Device> parent = mParent.promote();
2956 if (parent != NULL) {
2957 va_list args;
2958 va_start(args, fmt);
2959
2960 parent->setErrorStateV(fmt, args);
2961
2962 va_end(args);
2963 }
2964}
2965
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002966status_t Camera3Device::RequestThread::insertTriggers(
2967 const sp<CaptureRequest> &request) {
2968
2969 Mutex::Autolock al(mTriggerMutex);
2970
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002971 sp<Camera3Device> parent = mParent.promote();
2972 if (parent == NULL) {
2973 CLOGE("RequestThread: Parent is gone");
2974 return DEAD_OBJECT;
2975 }
2976
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002977 CameraMetadata &metadata = request->mSettings;
2978 size_t count = mTriggerMap.size();
2979
2980 for (size_t i = 0; i < count; ++i) {
2981 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002982 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002983
2984 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2985 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2986 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002987 if (isAeTrigger) {
2988 request->mResultExtras.precaptureTriggerId = triggerId;
2989 mCurrentPreCaptureTriggerId = triggerId;
2990 } else {
2991 request->mResultExtras.afTriggerId = triggerId;
2992 mCurrentAfTriggerId = triggerId;
2993 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002994 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2995 continue; // Trigger ID tag is deprecated since device HAL 3.2
2996 }
2997 }
2998
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002999 camera_metadata_entry entry = metadata.find(tag);
3000
3001 if (entry.count > 0) {
3002 /**
3003 * Already has an entry for this trigger in the request.
3004 * Rewrite it with our requested trigger value.
3005 */
3006 RequestTrigger oldTrigger = trigger;
3007
3008 oldTrigger.entryValue = entry.data.u8[0];
3009
3010 mTriggerReplacedMap.add(tag, oldTrigger);
3011 } else {
3012 /**
3013 * More typical, no trigger entry, so we just add it
3014 */
3015 mTriggerRemovedMap.add(tag, trigger);
3016 }
3017
3018 status_t res;
3019
3020 switch (trigger.getTagType()) {
3021 case TYPE_BYTE: {
3022 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3023 res = metadata.update(tag,
3024 &entryValue,
3025 /*count*/1);
3026 break;
3027 }
3028 case TYPE_INT32:
3029 res = metadata.update(tag,
3030 &trigger.entryValue,
3031 /*count*/1);
3032 break;
3033 default:
3034 ALOGE("%s: Type not supported: 0x%x",
3035 __FUNCTION__,
3036 trigger.getTagType());
3037 return INVALID_OPERATION;
3038 }
3039
3040 if (res != OK) {
3041 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3042 ", value %d", __FUNCTION__, trigger.getTagName(),
3043 trigger.entryValue);
3044 return res;
3045 }
3046
3047 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3048 trigger.getTagName(),
3049 trigger.entryValue);
3050 }
3051
3052 mTriggerMap.clear();
3053
3054 return count;
3055}
3056
3057status_t Camera3Device::RequestThread::removeTriggers(
3058 const sp<CaptureRequest> &request) {
3059 Mutex::Autolock al(mTriggerMutex);
3060
3061 CameraMetadata &metadata = request->mSettings;
3062
3063 /**
3064 * Replace all old entries with their old values.
3065 */
3066 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3067 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3068
3069 status_t res;
3070
3071 uint32_t tag = trigger.metadataTag;
3072 switch (trigger.getTagType()) {
3073 case TYPE_BYTE: {
3074 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3075 res = metadata.update(tag,
3076 &entryValue,
3077 /*count*/1);
3078 break;
3079 }
3080 case TYPE_INT32:
3081 res = metadata.update(tag,
3082 &trigger.entryValue,
3083 /*count*/1);
3084 break;
3085 default:
3086 ALOGE("%s: Type not supported: 0x%x",
3087 __FUNCTION__,
3088 trigger.getTagType());
3089 return INVALID_OPERATION;
3090 }
3091
3092 if (res != OK) {
3093 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3094 ", trigger value %d", __FUNCTION__,
3095 trigger.getTagName(), trigger.entryValue);
3096 return res;
3097 }
3098 }
3099 mTriggerReplacedMap.clear();
3100
3101 /**
3102 * Remove all new entries.
3103 */
3104 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3105 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3106 status_t res = metadata.erase(trigger.metadataTag);
3107
3108 if (res != OK) {
3109 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3110 ", trigger value %d", __FUNCTION__,
3111 trigger.getTagName(), trigger.entryValue);
3112 return res;
3113 }
3114 }
3115 mTriggerRemovedMap.clear();
3116
3117 return OK;
3118}
3119
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003120status_t Camera3Device::RequestThread::addDummyTriggerIds(
3121 const sp<CaptureRequest> &request) {
3122 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3123 static const int32_t dummyTriggerId = 1;
3124 status_t res;
3125
3126 CameraMetadata &metadata = request->mSettings;
3127
3128 // If AF trigger is active, insert a dummy AF trigger ID if none already
3129 // exists
3130 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3131 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3132 if (afTrigger.count > 0 &&
3133 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3134 afId.count == 0) {
3135 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3136 if (res != OK) return res;
3137 }
3138
3139 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3140 // if none already exists
3141 camera_metadata_entry pcTrigger =
3142 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3143 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3144 if (pcTrigger.count > 0 &&
3145 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3146 pcId.count == 0) {
3147 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3148 &dummyTriggerId, 1);
3149 if (res != OK) return res;
3150 }
3151
3152 return OK;
3153}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003154
3155
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003156/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003157 * Static callback forwarding methods from HAL to instance
3158 */
3159
3160void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3161 const camera3_capture_result *result) {
3162 Camera3Device *d =
3163 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3164 d->processCaptureResult(result);
3165}
3166
3167void Camera3Device::sNotify(const camera3_callback_ops *cb,
3168 const camera3_notify_msg *msg) {
3169 Camera3Device *d =
3170 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3171 d->notify(msg);
3172}
3173
3174}; // namespace android