blob: 51d53e232de028dc341673dbf3e04f1d7eb84548 [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];
Zhijun Hef7da0962014-04-24 13:27:56 -0700365
366 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700367 float scaleFactor = ((float) (width * height)) /
368 (maxJpegResolution.width * maxJpegResolution.height);
Zhijun Hef7da0962014-04-24 13:27:56 -0700369 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
370 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
371 if (jpegBufferSize > maxJpegBufferSize) {
372 jpegBufferSize = maxJpegBufferSize;
373 } else if (jpegBufferSize < kMinJpegBufferSize) {
374 jpegBufferSize = kMinJpegBufferSize;
375 }
376
377 return jpegBufferSize;
378}
379
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800380status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
381 ATRACE_CALL();
382 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700383
384 // Try to lock, but continue in case of failure (to avoid blocking in
385 // deadlocks)
386 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
387 bool gotLock = tryLockSpinRightRound(mLock);
388
389 ALOGW_IF(!gotInterfaceLock,
390 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
391 mId, __FUNCTION__);
392 ALOGW_IF(!gotLock,
393 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
394 mId, __FUNCTION__);
395
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800397
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800398 const char *status =
399 mStatus == STATUS_ERROR ? "ERROR" :
400 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700401 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
402 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800403 mStatus == STATUS_ACTIVE ? "ACTIVE" :
404 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700405
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800406 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700407 if (mStatus == STATUS_ERROR) {
408 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
409 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800410 lines.appendFormat(" Stream configuration:\n");
411
412 if (mInputStream != NULL) {
413 write(fd, lines.string(), lines.size());
414 mInputStream->dump(fd, args);
415 } else {
416 lines.appendFormat(" No input stream.\n");
417 write(fd, lines.string(), lines.size());
418 }
419 for (size_t i = 0; i < mOutputStreams.size(); i++) {
420 mOutputStreams[i]->dump(fd,args);
421 }
422
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700423 lines = String8(" In-flight requests:\n");
424 if (mInFlightMap.size() == 0) {
425 lines.append(" None\n");
426 } else {
427 for (size_t i = 0; i < mInFlightMap.size(); i++) {
428 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700429 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700430 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
431 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
432 r.numBuffersLeft);
433 }
434 }
435 write(fd, lines.string(), lines.size());
436
Igor Murashkin1e479c02013-09-06 16:55:14 -0700437 {
438 lines = String8(" Last request sent:\n");
439 write(fd, lines.string(), lines.size());
440
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700441 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700442 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
443 }
444
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800445 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700446 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800447 write(fd, lines.string(), lines.size());
448 mHal3Device->ops->dump(mHal3Device, fd);
449 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800450
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700451 if (gotLock) mLock.unlock();
452 if (gotInterfaceLock) mInterfaceLock.unlock();
453
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800454 return OK;
455}
456
457const CameraMetadata& Camera3Device::info() const {
458 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800459 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
460 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700461 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800462 mStatus == STATUS_ERROR ?
463 "when in error state" : "before init");
464 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800465 return mDeviceInfo;
466}
467
Jianing Wei90e59c92014-03-12 18:29:36 -0700468status_t Camera3Device::checkStatusOkToCaptureLocked() {
469 switch (mStatus) {
470 case STATUS_ERROR:
471 CLOGE("Device has encountered a serious error");
472 return INVALID_OPERATION;
473 case STATUS_UNINITIALIZED:
474 CLOGE("Device not initialized");
475 return INVALID_OPERATION;
476 case STATUS_UNCONFIGURED:
477 case STATUS_CONFIGURED:
478 case STATUS_ACTIVE:
479 // OK
480 break;
481 default:
482 SET_ERR_L("Unexpected status: %d", mStatus);
483 return INVALID_OPERATION;
484 }
485 return OK;
486}
487
488status_t Camera3Device::convertMetadataListToRequestListLocked(
489 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
490 if (requestList == NULL) {
491 CLOGE("requestList cannot be NULL.");
492 return BAD_VALUE;
493 }
494
Jianing Weicb0652e2014-03-12 18:29:36 -0700495 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700496 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
497 it != metadataList.end(); ++it) {
498 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
499 if (newRequest == 0) {
500 CLOGE("Can't create capture request");
501 return BAD_VALUE;
502 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700503
504 // Setup burst Id and request Id
505 newRequest->mResultExtras.burstId = burstId++;
506 if (it->exists(ANDROID_REQUEST_ID)) {
507 if (it->find(ANDROID_REQUEST_ID).count == 0) {
508 CLOGE("RequestID entry exists; but must not be empty in metadata");
509 return BAD_VALUE;
510 }
511 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
512 } else {
513 CLOGE("RequestID does not exist in metadata");
514 return BAD_VALUE;
515 }
516
Jianing Wei90e59c92014-03-12 18:29:36 -0700517 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700518
519 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700520 }
521 return OK;
522}
523
Jianing Weicb0652e2014-03-12 18:29:36 -0700524status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800526
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700527 List<const CameraMetadata> requests;
528 requests.push_back(request);
529 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800530}
531
Jianing Wei90e59c92014-03-12 18:29:36 -0700532status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700533 const List<const CameraMetadata> &requests, bool repeating,
534 /*out*/
535 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700536 ATRACE_CALL();
537 Mutex::Autolock il(mInterfaceLock);
538 Mutex::Autolock l(mLock);
539
540 status_t res = checkStatusOkToCaptureLocked();
541 if (res != OK) {
542 // error logged by previous call
543 return res;
544 }
545
546 RequestList requestList;
547
548 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
549 if (res != OK) {
550 // error logged by previous call
551 return res;
552 }
553
554 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700555 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700556 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700557 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700558 }
559
560 if (res == OK) {
561 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
562 if (res != OK) {
563 SET_ERR_L("Can't transition to active in %f seconds!",
564 kActiveTimeout/1e9);
565 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700566 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
567 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700568 } else {
569 CLOGE("Cannot queue request. Impossible.");
570 return BAD_VALUE;
571 }
572
573 return res;
574}
575
Jianing Weicb0652e2014-03-12 18:29:36 -0700576status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
577 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700578 ATRACE_CALL();
579
Jianing Weicb0652e2014-03-12 18:29:36 -0700580 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700581}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800582
Jianing Weicb0652e2014-03-12 18:29:36 -0700583status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
584 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800586
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700587 List<const CameraMetadata> requests;
588 requests.push_back(request);
589 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800590}
591
Jianing Weicb0652e2014-03-12 18:29:36 -0700592status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
593 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700594 ATRACE_CALL();
595
Jianing Weicb0652e2014-03-12 18:29:36 -0700596 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700597}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800598
599sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
600 const CameraMetadata &request) {
601 status_t res;
602
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700603 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800604 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700605 // Stream configuration failed due to unsupported configuration.
606 // Device back to unconfigured state. Client might try other configuraitons
607 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
608 CLOGE("No streams configured");
609 return NULL;
610 }
611 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700613 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800614 return NULL;
615 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700616 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700617 if (mStatus == STATUS_UNCONFIGURED) {
618 CLOGE("No streams configured");
619 return NULL;
620 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800621 }
622
623 sp<CaptureRequest> newRequest = createCaptureRequest(request);
624 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800625}
626
Jianing Weicb0652e2014-03-12 18:29:36 -0700627status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800628 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700629 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800630 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800631
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 switch (mStatus) {
633 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700634 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800635 return INVALID_OPERATION;
636 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700637 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800638 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639 case STATUS_UNCONFIGURED:
640 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800641 case STATUS_ACTIVE:
642 // OK
643 break;
644 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700645 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800646 return INVALID_OPERATION;
647 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700648 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700649
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700650 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800651}
652
653status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
654 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700655 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800656
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700657 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800658}
659
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700660status_t Camera3Device::createInputStream(
661 uint32_t width, uint32_t height, int format, int *id) {
662 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700663 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700664 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700665 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
666 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700667
668 status_t res;
669 bool wasActive = false;
670
671 switch (mStatus) {
672 case STATUS_ERROR:
673 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
674 return INVALID_OPERATION;
675 case STATUS_UNINITIALIZED:
676 ALOGE("%s: Device not initialized", __FUNCTION__);
677 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700678 case STATUS_UNCONFIGURED:
679 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700680 // OK
681 break;
682 case STATUS_ACTIVE:
683 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700684 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700685 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700686 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700687 return res;
688 }
689 wasActive = true;
690 break;
691 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700693 return INVALID_OPERATION;
694 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700696
697 if (mInputStream != 0) {
698 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
699 return INVALID_OPERATION;
700 }
701
702 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
703 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700705
706 mInputStream = newStream;
707
708 *id = mNextStreamId++;
709
710 // Continue captures if active at start
711 if (wasActive) {
712 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
713 res = configureStreamsLocked();
714 if (res != OK) {
715 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
716 __FUNCTION__, mNextStreamId, strerror(-res), res);
717 return res;
718 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700719 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700720 }
721
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700722 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700723 return OK;
724}
725
Igor Murashkin2fba5842013-04-22 14:03:54 -0700726
727status_t Camera3Device::createZslStream(
728 uint32_t width, uint32_t height,
729 int depth,
730 /*out*/
731 int *id,
732 sp<Camera3ZslStream>* zslStream) {
733 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700734 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700735 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
737 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700738
739 status_t res;
740 bool wasActive = false;
741
742 switch (mStatus) {
743 case STATUS_ERROR:
744 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
745 return INVALID_OPERATION;
746 case STATUS_UNINITIALIZED:
747 ALOGE("%s: Device not initialized", __FUNCTION__);
748 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700749 case STATUS_UNCONFIGURED:
750 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700751 // OK
752 break;
753 case STATUS_ACTIVE:
754 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700755 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700756 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700757 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700758 return res;
759 }
760 wasActive = true;
761 break;
762 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700763 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700764 return INVALID_OPERATION;
765 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700766 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700767
768 if (mInputStream != 0) {
769 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
770 return INVALID_OPERATION;
771 }
772
773 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
774 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700775 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700776
777 res = mOutputStreams.add(mNextStreamId, newStream);
778 if (res < 0) {
779 ALOGE("%s: Can't add new stream to set: %s (%d)",
780 __FUNCTION__, strerror(-res), res);
781 return res;
782 }
783 mInputStream = newStream;
784
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530785 mNeedConfig = true;
786
Igor Murashkin2fba5842013-04-22 14:03:54 -0700787 *id = mNextStreamId++;
788 *zslStream = newStream;
789
790 // Continue captures if active at start
791 if (wasActive) {
792 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
793 res = configureStreamsLocked();
794 if (res != OK) {
795 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
796 __FUNCTION__, mNextStreamId, strerror(-res), res);
797 return res;
798 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700799 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700800 }
801
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700802 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700803 return OK;
804}
805
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800806status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700807 uint32_t width, uint32_t height, int format, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800808 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700809 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800810 Mutex::Autolock l(mLock);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700811 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
812 mId, mNextStreamId, width, height, format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800813
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 status_t res;
815 bool wasActive = false;
816
817 switch (mStatus) {
818 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700819 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800820 return INVALID_OPERATION;
821 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700822 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700824 case STATUS_UNCONFIGURED:
825 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 // OK
827 break;
828 case STATUS_ACTIVE:
829 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700830 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800831 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833 return res;
834 }
835 wasActive = true;
836 break;
837 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 return INVALID_OPERATION;
840 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700841 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842
843 sp<Camera3OutputStream> newStream;
844 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700845 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700846 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700847 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
848 return BAD_VALUE;
849 }
850
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800851 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700852 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 } else {
854 newStream = new Camera3OutputStream(mNextStreamId, consumer,
855 width, height, format);
856 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700857 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800858
859 res = mOutputStreams.add(mNextStreamId, newStream);
860 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700861 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800862 return res;
863 }
864
865 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700866 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800867
868 // Continue captures if active at start
869 if (wasActive) {
870 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
871 res = configureStreamsLocked();
872 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700873 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
874 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800875 return res;
876 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700877 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800878 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700879 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800880 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800881}
882
883status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
884 ATRACE_CALL();
885 (void)outputId; (void)id;
886
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700887 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800888 return INVALID_OPERATION;
889}
890
891
892status_t Camera3Device::getStreamInfo(int id,
893 uint32_t *width, uint32_t *height, uint32_t *format) {
894 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700895 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800897
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 switch (mStatus) {
899 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700900 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800901 return INVALID_OPERATION;
902 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700903 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700905 case STATUS_UNCONFIGURED:
906 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 case STATUS_ACTIVE:
908 // OK
909 break;
910 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700911 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800912 return INVALID_OPERATION;
913 }
914
915 ssize_t idx = mOutputStreams.indexOfKey(id);
916 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700917 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 return idx;
919 }
920
921 if (width) *width = mOutputStreams[idx]->getWidth();
922 if (height) *height = mOutputStreams[idx]->getHeight();
923 if (format) *format = mOutputStreams[idx]->getFormat();
924
925 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926}
927
928status_t Camera3Device::setStreamTransform(int id,
929 int transform) {
930 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700931 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800932 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800933
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800934 switch (mStatus) {
935 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700936 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800937 return INVALID_OPERATION;
938 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700939 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800940 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700941 case STATUS_UNCONFIGURED:
942 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800943 case STATUS_ACTIVE:
944 // OK
945 break;
946 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700947 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800948 return INVALID_OPERATION;
949 }
950
951 ssize_t idx = mOutputStreams.indexOfKey(id);
952 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700953 CLOGE("Stream %d does not exist",
954 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 return BAD_VALUE;
956 }
957
958 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800959}
960
961status_t Camera3Device::deleteStream(int id) {
962 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700963 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800964 Mutex::Autolock l(mLock);
965 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800966
Igor Murashkine2172be2013-05-28 15:31:39 -0700967 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
968
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 // CameraDevice semantics require device to already be idle before
970 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700972 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
973 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800974 }
975
Igor Murashkin2fba5842013-04-22 14:03:54 -0700976 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800977 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800978 if (mInputStream != NULL && id == mInputStream->getId()) {
979 deletedStream = mInputStream;
980 mInputStream.clear();
981 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800982 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700983 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800984 return BAD_VALUE;
985 }
Zhijun He5f446352014-01-22 09:49:33 -0800986 }
987
988 // Delete output stream or the output part of a bi-directional stream.
989 if (outputStreamIdx != NAME_NOT_FOUND) {
990 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800991 mOutputStreams.removeItem(id);
992 }
993
994 // Free up the stream endpoint so that it can be used by some other stream
995 res = deletedStream->disconnect();
996 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700997 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800998 // fall through since we want to still list the stream as deleted.
999 }
1000 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001001 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001002
1003 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001004}
1005
1006status_t Camera3Device::deleteReprocessStream(int id) {
1007 ATRACE_CALL();
1008 (void)id;
1009
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001010 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001011 return INVALID_OPERATION;
1012}
1013
Igor Murashkine2d167e2014-08-19 16:19:59 -07001014status_t Camera3Device::configureStreams() {
1015 ATRACE_CALL();
1016 ALOGV("%s: E", __FUNCTION__);
1017
1018 Mutex::Autolock il(mInterfaceLock);
1019 Mutex::Autolock l(mLock);
1020
1021 return configureStreamsLocked();
1022}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001023
1024status_t Camera3Device::createDefaultRequest(int templateId,
1025 CameraMetadata *request) {
1026 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001027 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen116a1892016-03-09 12:21:01 -08001028
1029 if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
1030 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1031 IPCThreadState::self()->getCallingUid(), NULL, 0);
1032 return BAD_VALUE;
1033 }
1034
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001035 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 Mutex::Autolock l(mLock);
1037
1038 switch (mStatus) {
1039 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001040 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001041 return INVALID_OPERATION;
1042 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001043 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001045 case STATUS_UNCONFIGURED:
1046 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001047 case STATUS_ACTIVE:
1048 // OK
1049 break;
1050 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001051 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001052 return INVALID_OPERATION;
1053 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001054
Zhijun Hea1530f12014-09-14 12:44:20 -07001055 if (!mRequestTemplateCache[templateId].isEmpty()) {
1056 *request = mRequestTemplateCache[templateId];
1057 return OK;
1058 }
1059
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001060 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001061 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001062 rawRequest = mHal3Device->ops->construct_default_request_settings(
1063 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001064 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001065 if (rawRequest == NULL) {
1066 SET_ERR_L("HAL is unable to construct default settings for template %d",
1067 templateId);
1068 return DEAD_OBJECT;
1069 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001070 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001071 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001072
1073 return OK;
1074}
1075
1076status_t Camera3Device::waitUntilDrained() {
1077 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001078 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001080
Zhijun He69a37482014-03-23 18:44:49 -07001081 return waitUntilDrainedLocked();
1082}
1083
1084status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001085 switch (mStatus) {
1086 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001087 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 ALOGV("%s: Already idle", __FUNCTION__);
1089 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001090 case STATUS_CONFIGURED:
1091 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001092 case STATUS_ERROR:
1093 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001094 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001095 break;
1096 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001097 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098 return INVALID_OPERATION;
1099 }
1100
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001101 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1102 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001103 if (res != OK) {
1104 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1105 res);
1106 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001107 return res;
1108}
1109
1110// Pause to reconfigure
1111status_t Camera3Device::internalPauseAndWaitLocked() {
1112 mRequestThread->setPaused(true);
1113 mPauseStateNotify = true;
1114
1115 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1116 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1117 if (res != OK) {
1118 SET_ERR_L("Can't idle device in %f seconds!",
1119 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001120 }
1121
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001122 return res;
1123}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001124
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001125// Resume after internalPauseAndWaitLocked
1126status_t Camera3Device::internalResumeLocked() {
1127 status_t res;
1128
1129 mRequestThread->setPaused(false);
1130
1131 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1132 if (res != OK) {
1133 SET_ERR_L("Can't transition to active in %f seconds!",
1134 kActiveTimeout/1e9);
1135 }
1136 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001137 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001138}
1139
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001140status_t Camera3Device::waitUntilStateThenRelock(bool active,
1141 nsecs_t timeout) {
1142 status_t res = OK;
1143 if (active == (mStatus == STATUS_ACTIVE)) {
1144 // Desired state already reached
1145 return res;
1146 }
1147
1148 bool stateSeen = false;
1149 do {
1150 mRecentStatusUpdates.clear();
1151
1152 res = mStatusChanged.waitRelative(mLock, timeout);
1153 if (res != OK) break;
1154
1155 // Check state change history during wait
1156 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1157 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1158 stateSeen = true;
1159 break;
1160 }
1161 }
1162 } while (!stateSeen);
1163
1164 return res;
1165}
1166
1167
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001168status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1169 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001170 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001172 if (listener != NULL && mListener != NULL) {
1173 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1174 }
1175 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001176 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001177
1178 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001179}
1180
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001181bool Camera3Device::willNotify3A() {
1182 return false;
1183}
1184
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001185status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001186 status_t res;
1187 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001188
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001189 while (mResultQueue.empty()) {
1190 res = mResultSignal.waitRelative(mOutputLock, timeout);
1191 if (res == TIMED_OUT) {
1192 return res;
1193 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001194 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001195 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001196 return res;
1197 }
1198 }
1199 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200}
1201
Jianing Weicb0652e2014-03-12 18:29:36 -07001202status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001203 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001204 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001205
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001206 if (mResultQueue.empty()) {
1207 return NOT_ENOUGH_DATA;
1208 }
1209
Jianing Weicb0652e2014-03-12 18:29:36 -07001210 if (frame == NULL) {
1211 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1212 return BAD_VALUE;
1213 }
1214
1215 CaptureResult &result = *(mResultQueue.begin());
1216 frame->mResultExtras = result.mResultExtras;
1217 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001218 mResultQueue.erase(mResultQueue.begin());
1219
1220 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221}
1222
1223status_t Camera3Device::triggerAutofocus(uint32_t id) {
1224 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001225 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001227 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1228 // Mix-in this trigger into the next request and only the next request.
1229 RequestTrigger trigger[] = {
1230 {
1231 ANDROID_CONTROL_AF_TRIGGER,
1232 ANDROID_CONTROL_AF_TRIGGER_START
1233 },
1234 {
1235 ANDROID_CONTROL_AF_TRIGGER_ID,
1236 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001237 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001238 };
1239
1240 return mRequestThread->queueTrigger(trigger,
1241 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242}
1243
1244status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1245 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001246 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001247
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001248 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1249 // Mix-in this trigger into the next request and only the next request.
1250 RequestTrigger trigger[] = {
1251 {
1252 ANDROID_CONTROL_AF_TRIGGER,
1253 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1254 },
1255 {
1256 ANDROID_CONTROL_AF_TRIGGER_ID,
1257 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001258 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001259 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001260
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001261 return mRequestThread->queueTrigger(trigger,
1262 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263}
1264
1265status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1266 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001267 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001269 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1270 // Mix-in this trigger into the next request and only the next request.
1271 RequestTrigger trigger[] = {
1272 {
1273 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1274 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1275 },
1276 {
1277 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1278 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001279 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001280 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001281
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001282 return mRequestThread->queueTrigger(trigger,
1283 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284}
1285
1286status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1287 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1288 ATRACE_CALL();
1289 (void)reprocessStreamId; (void)buffer; (void)listener;
1290
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001291 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001292 return INVALID_OPERATION;
1293}
1294
Jianing Weicb0652e2014-03-12 18:29:36 -07001295status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001296 ATRACE_CALL();
1297 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001298 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001299
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001300 NotificationListener* listener;
1301 {
1302 Mutex::Autolock l(mOutputLock);
1303 listener = mListener;
1304 }
1305
Zhijun He7ef20392014-04-21 16:04:17 -07001306 {
1307 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001308 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001309 }
1310
Zhijun He491e3412013-12-27 10:57:44 -08001311 status_t res;
1312 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1313 res = mHal3Device->ops->flush(mHal3Device);
1314 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001315 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001316 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001317 }
1318
1319 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001320}
1321
Zhijun He204e3292014-07-14 17:09:23 -07001322uint32_t Camera3Device::getDeviceVersion() {
1323 ATRACE_CALL();
1324 Mutex::Autolock il(mInterfaceLock);
1325 return mDeviceVersion;
1326}
1327
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001328/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001329 * Methods called by subclasses
1330 */
1331
1332void Camera3Device::notifyStatus(bool idle) {
1333 {
1334 // Need mLock to safely update state and synchronize to current
1335 // state of methods in flight.
1336 Mutex::Autolock l(mLock);
1337 // We can get various system-idle notices from the status tracker
1338 // while starting up. Only care about them if we've actually sent
1339 // in some requests recently.
1340 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1341 return;
1342 }
1343 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1344 idle ? "idle" : "active");
1345 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1346 mRecentStatusUpdates.add(mStatus);
1347 mStatusChanged.signal();
1348
1349 // Skip notifying listener if we're doing some user-transparent
1350 // state changes
1351 if (mPauseStateNotify) return;
1352 }
1353 NotificationListener *listener;
1354 {
1355 Mutex::Autolock l(mOutputLock);
1356 listener = mListener;
1357 }
1358 if (idle && listener != NULL) {
1359 listener->notifyIdle();
1360 }
1361}
1362
1363/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 * Camera3Device private methods
1365 */
1366
1367sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1368 const CameraMetadata &request) {
1369 ATRACE_CALL();
1370 status_t res;
1371
1372 sp<CaptureRequest> newRequest = new CaptureRequest;
1373 newRequest->mSettings = request;
1374
1375 camera_metadata_entry_t inputStreams =
1376 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1377 if (inputStreams.count > 0) {
1378 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001379 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001380 CLOGE("Request references unknown input stream %d",
1381 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001382 return NULL;
1383 }
1384 // Lazy completion of stream configuration (allocation/registration)
1385 // on first use
1386 if (mInputStream->isConfiguring()) {
1387 res = mInputStream->finishConfiguration(mHal3Device);
1388 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001389 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001391 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001392 return NULL;
1393 }
1394 }
1395
1396 newRequest->mInputStream = mInputStream;
1397 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1398 }
1399
1400 camera_metadata_entry_t streams =
1401 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1402 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001403 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404 return NULL;
1405 }
1406
1407 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001408 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001410 CLOGE("Request references unknown stream %d",
1411 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 return NULL;
1413 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001414 sp<Camera3OutputStreamInterface> stream =
1415 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416
1417 // Lazy completion of stream configuration (allocation/registration)
1418 // on first use
1419 if (stream->isConfiguring()) {
1420 res = stream->finishConfiguration(mHal3Device);
1421 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001422 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1423 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001424 return NULL;
1425 }
1426 }
1427
1428 newRequest->mOutputStreams.push(stream);
1429 }
1430 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1431
1432 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001433}
1434
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001435status_t Camera3Device::configureStreamsLocked() {
1436 ATRACE_CALL();
1437 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001438
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001439 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001440 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001441 return INVALID_OPERATION;
1442 }
1443
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001444 if (!mNeedConfig) {
1445 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1446 return OK;
1447 }
1448
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001449 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1450 // adding a dummy stream instead.
1451 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1452 if (mOutputStreams.size() == 0) {
1453 addDummyStreamLocked();
1454 } else {
1455 tryRemoveDummyStreamLocked();
1456 }
1457
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001458 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001459 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001460
1461 camera3_stream_configuration config;
1462
1463 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1464
1465 Vector<camera3_stream_t*> streams;
1466 streams.setCapacity(config.num_streams);
1467
1468 if (mInputStream != NULL) {
1469 camera3_stream_t *inputStream;
1470 inputStream = mInputStream->startConfiguration();
1471 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001472 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473 return INVALID_OPERATION;
1474 }
1475 streams.add(inputStream);
1476 }
1477
1478 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001479
1480 // Don't configure bidi streams twice, nor add them twice to the list
1481 if (mOutputStreams[i].get() ==
1482 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1483
1484 config.num_streams--;
1485 continue;
1486 }
1487
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001488 camera3_stream_t *outputStream;
1489 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1490 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001491 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001492 return INVALID_OPERATION;
1493 }
1494 streams.add(outputStream);
1495 }
1496
1497 config.streams = streams.editArray();
1498
1499 // Do the HAL configuration; will potentially touch stream
1500 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001501 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001502 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001503 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001505 if (res == BAD_VALUE) {
1506 // HAL rejected this set of streams as unsupported, clean up config
1507 // attempt and return to unconfigured state
1508 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1509 res = mInputStream->cancelConfiguration();
1510 if (res != OK) {
1511 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1512 mInputStream->getId(), strerror(-res), res);
1513 return res;
1514 }
1515 }
1516
1517 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1518 sp<Camera3OutputStreamInterface> outputStream =
1519 mOutputStreams.editValueAt(i);
1520 if (outputStream->isConfiguring()) {
1521 res = outputStream->cancelConfiguration();
1522 if (res != OK) {
1523 SET_ERR_L(
1524 "Can't cancel configuring output stream %d: %s (%d)",
1525 outputStream->getId(), strerror(-res), res);
1526 return res;
1527 }
1528 }
1529 }
1530
1531 // Return state to that at start of call, so that future configures
1532 // properly clean things up
1533 mStatus = STATUS_UNCONFIGURED;
1534 mNeedConfig = true;
1535
1536 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1537 return BAD_VALUE;
1538 } else if (res != OK) {
1539 // Some other kind of error from configure_streams - this is not
1540 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001541 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1542 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 return res;
1544 }
1545
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001546 // Finish all stream configuration immediately.
1547 // TODO: Try to relax this later back to lazy completion, which should be
1548 // faster
1549
Igor Murashkin073f8572013-05-02 14:59:28 -07001550 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001551 res = mInputStream->finishConfiguration(mHal3Device);
1552 if (res != OK) {
1553 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1554 mInputStream->getId(), strerror(-res), res);
1555 return res;
1556 }
1557 }
1558
1559 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001560 sp<Camera3OutputStreamInterface> outputStream =
1561 mOutputStreams.editValueAt(i);
1562 if (outputStream->isConfiguring()) {
1563 res = outputStream->finishConfiguration(mHal3Device);
1564 if (res != OK) {
1565 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1566 outputStream->getId(), strerror(-res), res);
1567 return res;
1568 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001569 }
1570 }
1571
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001572 // Request thread needs to know to avoid using repeat-last-settings protocol
1573 // across configure_streams() calls
1574 mRequestThread->configurationComplete();
1575
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001576 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001577
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001578 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001580 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001581 mStatus = STATUS_CONFIGURED;
1582 } else {
1583 mStatus = STATUS_UNCONFIGURED;
1584 }
1585
1586 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1587
Zhijun He0a210512014-07-24 13:45:15 -07001588 // tear down the deleted streams after configure streams.
1589 mDeletedStreams.clear();
1590
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001591 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001592}
1593
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001594status_t Camera3Device::addDummyStreamLocked() {
1595 ATRACE_CALL();
1596 status_t res;
1597
1598 if (mDummyStreamId != NO_STREAM) {
1599 // Should never be adding a second dummy stream when one is already
1600 // active
1601 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1602 __FUNCTION__, mId);
1603 return INVALID_OPERATION;
1604 }
1605
1606 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1607
1608 sp<Camera3OutputStreamInterface> dummyStream =
1609 new Camera3DummyStream(mNextStreamId);
1610
1611 res = mOutputStreams.add(mNextStreamId, dummyStream);
1612 if (res < 0) {
1613 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1614 return res;
1615 }
1616
1617 mDummyStreamId = mNextStreamId;
1618 mNextStreamId++;
1619
1620 return OK;
1621}
1622
1623status_t Camera3Device::tryRemoveDummyStreamLocked() {
1624 ATRACE_CALL();
1625 status_t res;
1626
1627 if (mDummyStreamId == NO_STREAM) return OK;
1628 if (mOutputStreams.size() == 1) return OK;
1629
1630 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1631
1632 // Ok, have a dummy stream and there's at least one other output stream,
1633 // so remove the dummy
1634
1635 sp<Camera3StreamInterface> deletedStream;
1636 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1637 if (outputStreamIdx == NAME_NOT_FOUND) {
1638 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1639 return INVALID_OPERATION;
1640 }
1641
1642 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1643 mOutputStreams.removeItemsAt(outputStreamIdx);
1644
1645 // Free up the stream endpoint so that it can be used by some other stream
1646 res = deletedStream->disconnect();
1647 if (res != OK) {
1648 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1649 // fall through since we want to still list the stream as deleted.
1650 }
1651 mDeletedStreams.add(deletedStream);
1652 mDummyStreamId = NO_STREAM;
1653
1654 return res;
1655}
1656
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001657void Camera3Device::setErrorState(const char *fmt, ...) {
1658 Mutex::Autolock l(mLock);
1659 va_list args;
1660 va_start(args, fmt);
1661
1662 setErrorStateLockedV(fmt, args);
1663
1664 va_end(args);
1665}
1666
1667void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1668 Mutex::Autolock l(mLock);
1669 setErrorStateLockedV(fmt, args);
1670}
1671
1672void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1673 va_list args;
1674 va_start(args, fmt);
1675
1676 setErrorStateLockedV(fmt, args);
1677
1678 va_end(args);
1679}
1680
1681void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001682 // Print out all error messages to log
1683 String8 errorCause = String8::formatV(fmt, args);
1684 ALOGE("Camera %d: %s", mId, errorCause.string());
1685
1686 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001687 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001688
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001689 mErrorCause = errorCause;
1690
1691 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001692 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001693
1694 // Notify upstream about a device error
1695 if (mListener != NULL) {
1696 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1697 CaptureResultExtras());
1698 }
1699
1700 // Save stack trace. View by dumping it later.
1701 CameraTraces::saveTrace();
1702 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001703}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001704
1705/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001706 * In-flight request management
1707 */
1708
Jianing Weicb0652e2014-03-12 18:29:36 -07001709status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001710 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001711 ATRACE_CALL();
1712 Mutex::Autolock l(mInFlightLock);
1713
1714 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001715 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001716 if (res < 0) return res;
1717
1718 return OK;
1719}
1720
1721/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001722 * Check if all 3A fields are ready, and send off a partial 3A-only result
1723 * to the output frame queue
1724 */
Zhijun He204e3292014-07-14 17:09:23 -07001725bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001726 uint32_t frameNumber,
1727 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001728
1729 // Check if all 3A states are present
1730 // The full list of fields is
1731 // android.control.afMode
1732 // android.control.awbMode
1733 // android.control.aeState
1734 // android.control.awbState
1735 // android.control.afState
1736 // android.control.afTriggerID
1737 // android.control.aePrecaptureID
1738 // TODO: Add android.control.aeMode
1739
1740 bool gotAllStates = true;
1741
1742 uint8_t afMode;
1743 uint8_t awbMode;
1744 uint8_t aeState;
1745 uint8_t afState;
1746 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001747
1748 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1749 &afMode, frameNumber);
1750
1751 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1752 &awbMode, frameNumber);
1753
1754 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1755 &aeState, frameNumber);
1756
1757 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1758 &afState, frameNumber);
1759
1760 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1761 &awbState, frameNumber);
1762
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001763 if (!gotAllStates) return false;
1764
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001765 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001766 "AF state %d, AE state %d, AWB state %d, "
1767 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001768 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001769 afMode, awbMode,
1770 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001771 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001772
1773 // Got all states, so construct a minimal result to send
1774 // In addition to the above fields, this means adding in
1775 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001776 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001777 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001778
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001779 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001780
1781 Mutex::Autolock l(mOutputLock);
1782
Jianing Weicb0652e2014-03-12 18:29:36 -07001783 CaptureResult captureResult;
1784 captureResult.mResultExtras = resultExtras;
1785 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1786 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1787 // but not limited to CameraDeviceBase::getNextResult
1788 CaptureResult& min3AResult =
1789 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001790
Jianing Weicb0652e2014-03-12 18:29:36 -07001791 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1792 // TODO: This is problematic casting. Need to fix CameraMetadata.
1793 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001794 return false;
1795 }
1796
Jianing Weicb0652e2014-03-12 18:29:36 -07001797 int32_t requestId = resultExtras.requestId;
1798 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001799 &requestId, frameNumber)) {
1800 return false;
1801 }
1802
Zhijun He204e3292014-07-14 17:09:23 -07001803 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1804 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1805 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1806 &partialResult, frameNumber)) {
1807 return false;
1808 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001809 }
1810
Jianing Weicb0652e2014-03-12 18:29:36 -07001811 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001812 &afMode, frameNumber)) {
1813 return false;
1814 }
1815
Jianing Weicb0652e2014-03-12 18:29:36 -07001816 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001817 &awbMode, frameNumber)) {
1818 return false;
1819 }
1820
Jianing Weicb0652e2014-03-12 18:29:36 -07001821 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001822 &aeState, frameNumber)) {
1823 return false;
1824 }
1825
Jianing Weicb0652e2014-03-12 18:29:36 -07001826 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001827 &afState, frameNumber)) {
1828 return false;
1829 }
1830
Jianing Weicb0652e2014-03-12 18:29:36 -07001831 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001832 &awbState, frameNumber)) {
1833 return false;
1834 }
1835
Jianing Weicb0652e2014-03-12 18:29:36 -07001836 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001837 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001838 return false;
1839 }
1840
Jianing Weicb0652e2014-03-12 18:29:36 -07001841 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001842 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001843 return false;
1844 }
1845
Zhijun He204e3292014-07-14 17:09:23 -07001846 // We only send the aggregated partial when all 3A related metadata are available
1847 // For both API1 and API2.
1848 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849 mResultSignal.signal();
1850
1851 return true;
1852}
1853
1854template<typename T>
1855bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001856 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001857 (void) frameNumber;
1858
1859 camera_metadata_ro_entry_t entry;
1860
1861 entry = result.find(tag);
1862 if (entry.count == 0) {
1863 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1864 mId, frameNumber, get_camera_metadata_tag_name(tag));
1865 return false;
1866 }
1867
1868 if (sizeof(T) == sizeof(uint8_t)) {
1869 *value = entry.data.u8[0];
1870 } else if (sizeof(T) == sizeof(int32_t)) {
1871 *value = entry.data.i32[0];
1872 } else {
1873 ALOGE("%s: Unexpected type", __FUNCTION__);
1874 return false;
1875 }
1876 return true;
1877}
1878
1879template<typename T>
1880bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001881 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001882 if (result.update(tag, value, 1) != NO_ERROR) {
1883 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1884 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1885 frameNumber, get_camera_metadata_tag_name(tag));
1886 return false;
1887 }
1888 return true;
1889}
1890
1891/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001892 * Camera HAL device callback methods
1893 */
1894
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001895void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001896 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001897
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001898 status_t res;
1899
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001900 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001901 if (result->result == NULL && result->num_output_buffers == 0 &&
1902 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001903 SET_ERR("No result data provided by HAL for frame %d",
1904 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001905 return;
1906 }
Zhijun He204e3292014-07-14 17:09:23 -07001907
1908 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
1909 // partial_result to 1 when metadata is included in this result.
1910 if (!mUsePartialResult &&
1911 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
1912 result->result != NULL &&
1913 result->partial_result != 1) {
1914 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
1915 " if partial result is not supported",
1916 frameNumber, result->partial_result);
1917 return;
1918 }
1919
1920 bool isPartialResult = false;
1921 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001922 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001923 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001924
Jianing Weicb0652e2014-03-12 18:29:36 -07001925 // Get capture timestamp and resultExtras from list of in-flight requests,
1926 // where it was added by the shutter notification for this frame.
1927 // Then update the in-flight status and remove the in-flight entry if
1928 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001929 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001930 {
1931 Mutex::Autolock l(mInFlightLock);
1932 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1933 if (idx == NAME_NOT_FOUND) {
1934 SET_ERR("Unknown frame number for capture result: %d",
1935 frameNumber);
1936 return;
1937 }
1938 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001939 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1940 ", burstId = %" PRId32,
1941 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1942 request.resultExtras.burstId);
Zhijun He204e3292014-07-14 17:09:23 -07001943 // Always update the partial count to the latest one. When framework aggregates adjacent
1944 // partial results into one, the latest partial count will be used.
1945 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001946
1947 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07001948 if (mUsePartialResult && result->result != NULL) {
1949 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1950 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
1951 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
1952 " the range of [1, %d] when metadata is included in the result",
1953 frameNumber, result->partial_result, mNumPartialResults);
1954 return;
1955 }
1956 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07001957 if (isPartialResult) {
1958 request.partialResult.collectedResult.append(result->result);
1959 }
Zhijun He204e3292014-07-14 17:09:23 -07001960 } else {
1961 camera_metadata_ro_entry_t partialResultEntry;
1962 res = find_camera_metadata_ro_entry(result->result,
1963 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1964 if (res != NAME_NOT_FOUND &&
1965 partialResultEntry.count > 0 &&
1966 partialResultEntry.data.u8[0] ==
1967 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1968 // A partial result. Flag this as such, and collect this
1969 // set of metadata into the in-flight entry.
1970 isPartialResult = true;
1971 request.partialResult.collectedResult.append(
1972 result->result);
1973 request.partialResult.collectedResult.erase(
1974 ANDROID_QUIRKS_PARTIAL_RESULT);
1975 }
1976 }
1977
1978 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001979 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07001980 if (!request.partialResult.haveSent3A) {
1981 request.partialResult.haveSent3A =
1982 processPartial3AResult(frameNumber,
1983 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07001984 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001985 }
1986 }
1987 }
1988
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001989 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001990 resultExtras = request.resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001991 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07001992
Zhijun He1d1f8462013-10-02 16:29:51 -07001993 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001994 * One of the following must happen before it's legal to call process_capture_result,
1995 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001996 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1997 * - CAMERA3_MSG_ERROR (expected during flush)
1998 */
Zhijun He204e3292014-07-14 17:09:23 -07001999 if (request.requestStatus == OK && timestamp == 0 && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002000 SET_ERR("Called before shutter notify for frame %d",
2001 frameNumber);
2002 return;
2003 }
2004
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002005 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002006 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002007 if (request.haveResultMetadata) {
2008 SET_ERR("Called multiple times with metadata for frame %d",
2009 frameNumber);
2010 return;
2011 }
Zhijun He204e3292014-07-14 17:09:23 -07002012 if (mUsePartialResult &&
2013 !request.partialResult.collectedResult.isEmpty()) {
2014 collectedPartialResult.acquire(
2015 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002016 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002017 request.haveResultMetadata = true;
2018 }
2019
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002020 uint32_t numBuffersReturned = result->num_output_buffers;
2021 if (result->input_buffer != NULL) {
2022 if (hasInputBufferInRequest) {
2023 numBuffersReturned += 1;
2024 } else {
2025 ALOGW("%s: Input buffer should be NULL if there is no input"
2026 " buffer sent in the request",
2027 __FUNCTION__);
2028 }
2029 }
2030 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002031 if (request.numBuffersLeft < 0) {
2032 SET_ERR("Too many buffers returned for frame %d",
2033 frameNumber);
2034 return;
2035 }
2036
Zhijun He1b05dfc2013-11-21 12:57:51 -08002037 // Check if everything has arrived for this result (buffers and metadata), remove it from
2038 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
2039 if ((request.requestStatus != OK) ||
2040 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002041 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002042 mInFlightMap.removeItemsAt(idx, 1);
2043 }
2044
2045 // Sanity check - if we have too many in-flight frames, something has
2046 // likely gone wrong
2047 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002048 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002049 }
2050
2051 }
2052
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002053 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002054 bool gotResult = false;
Zhijun He204e3292014-07-14 17:09:23 -07002055 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002056 Mutex::Autolock l(mOutputLock);
2057
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002058 gotResult = true;
2059
Jianing Wei3c76fa32014-04-21 11:34:34 -07002060 // TODO: need to track errors for tighter bounds on expected frame number
2061 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002062 SET_ERR("Out-of-order capture result metadata submitted! "
2063 "(got frame number %d, expecting %d)",
2064 frameNumber, mNextResultFrameNumber);
2065 return;
2066 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07002067 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002068
Jianing Weicb0652e2014-03-12 18:29:36 -07002069 CaptureResult captureResult;
2070 captureResult.mResultExtras = resultExtras;
2071 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002072
Jianing Weicb0652e2014-03-12 18:29:36 -07002073 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2074 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002075 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002076 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002077 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07002078 } else {
2079 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002080 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07002081 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002082
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002083 // Append any previous partials to form a complete result
Zhijun He204e3292014-07-14 17:09:23 -07002084 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2085 captureResult.mMetadata.append(collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002086 }
2087
Jianing Weicb0652e2014-03-12 18:29:36 -07002088 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002089
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002090 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002091
2092 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07002093 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002094 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002095 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002096 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002097 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07002098 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002099 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07002100 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002101 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002102 gotResult = false;
2103 }
2104
2105 if (gotResult) {
2106 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07002107 List<CaptureResult>::iterator queuedResult =
2108 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2109 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2110 ", burstId = %" PRId32, __FUNCTION__,
2111 queuedResult->mResultExtras.requestId,
2112 queuedResult->mResultExtras.frameNumber,
2113 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002114 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002115 } // scope for mOutputLock
2116
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002117 // Return completed buffers to their streams with the timestamp
2118
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002119 for (size_t i = 0; i < result->num_output_buffers; i++) {
2120 Camera3Stream *stream =
2121 Camera3Stream::cast(result->output_buffers[i].stream);
2122 res = stream->returnBuffer(result->output_buffers[i], timestamp);
2123 // Note: stream may be deallocated at this point, if this buffer was the
2124 // last reference to it.
2125 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002126 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002127 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002128 }
2129 }
2130
Zhijun Hef0d962a2014-06-30 10:24:11 -07002131 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002132 if (hasInputBufferInRequest) {
2133 Camera3Stream *stream =
2134 Camera3Stream::cast(result->input_buffer->stream);
2135 res = stream->returnInputBuffer(*(result->input_buffer));
2136 // Note: stream may be deallocated at this point, if this buffer was the
2137 // last reference to it.
2138 if (res != OK) {
2139 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2140 " its stream:%s (%d)", __FUNCTION__,
2141 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002142 }
2143 } else {
2144 ALOGW("%s: Input buffer should be NULL if there is no input"
2145 " buffer sent in the request, skipping input buffer return.",
2146 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002147 }
2148 }
2149
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002150 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002151
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002152 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07002153 mResultSignal.signal();
2154 }
2155
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002156}
2157
2158void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002159 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002160 NotificationListener *listener;
2161 {
2162 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002163 listener = mListener;
2164 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002165
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002166 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002167 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002168 return;
2169 }
2170
2171 switch (msg->type) {
2172 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002173 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002174 break;
2175 }
2176 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002177 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002178 break;
2179 }
2180 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002181 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002182 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002183 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002184}
2185
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002186void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2187 NotificationListener *listener) {
2188
2189 // Map camera HAL error codes to ICameraDeviceCallback error codes
2190 // Index into this with the HAL error code
2191 static const ICameraDeviceCallbacks::CameraErrorCode
2192 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2193 // 0 = Unused error code
2194 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2195 // 1 = CAMERA3_MSG_ERROR_DEVICE
2196 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2197 // 2 = CAMERA3_MSG_ERROR_REQUEST
2198 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2199 // 3 = CAMERA3_MSG_ERROR_RESULT
2200 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2201 // 4 = CAMERA3_MSG_ERROR_BUFFER
2202 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2203 };
2204
2205 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2206 ((msg.error_code >= 0) &&
2207 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2208 halErrorMap[msg.error_code] :
2209 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2210
2211 int streamId = 0;
2212 if (msg.error_stream != NULL) {
2213 Camera3Stream *stream =
2214 Camera3Stream::cast(msg.error_stream);
2215 streamId = stream->getId();
2216 }
2217 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2218 mId, __FUNCTION__, msg.frame_number,
2219 streamId, msg.error_code);
2220
2221 CaptureResultExtras resultExtras;
2222 switch (errorCode) {
2223 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2224 // SET_ERR calls notifyError
2225 SET_ERR("Camera HAL reported serious device error");
2226 break;
2227 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2228 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2229 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2230 {
2231 Mutex::Autolock l(mInFlightLock);
2232 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2233 if (idx >= 0) {
2234 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2235 r.requestStatus = msg.error_code;
2236 resultExtras = r.resultExtras;
2237 } else {
2238 resultExtras.frameNumber = msg.frame_number;
2239 ALOGE("Camera %d: %s: cannot find in-flight request on "
2240 "frame %" PRId64 " error", mId, __FUNCTION__,
2241 resultExtras.frameNumber);
2242 }
2243 }
2244 if (listener != NULL) {
2245 listener->notifyError(errorCode, resultExtras);
2246 } else {
2247 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2248 }
2249 break;
2250 default:
2251 // SET_ERR calls notifyError
2252 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2253 break;
2254 }
2255}
2256
2257void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2258 NotificationListener *listener) {
2259 ssize_t idx;
2260 // Verify ordering of shutter notifications
2261 {
2262 Mutex::Autolock l(mOutputLock);
2263 // TODO: need to track errors for tighter bounds on expected frame number.
2264 if (msg.frame_number < mNextShutterFrameNumber) {
2265 SET_ERR("Shutter notification out-of-order. Expected "
2266 "notification for frame %d, got frame %d",
2267 mNextShutterFrameNumber, msg.frame_number);
2268 return;
2269 }
2270 mNextShutterFrameNumber = msg.frame_number + 1;
2271 }
2272
2273 CaptureResultExtras resultExtras;
2274
2275 // Set timestamp for the request in the in-flight tracking
2276 // and get the request ID to send upstream
2277 {
2278 Mutex::Autolock l(mInFlightLock);
2279 idx = mInFlightMap.indexOfKey(msg.frame_number);
2280 if (idx >= 0) {
2281 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2282 r.captureTimestamp = msg.timestamp;
2283 resultExtras = r.resultExtras;
2284 }
2285 }
2286 if (idx < 0) {
2287 SET_ERR("Shutter notification for non-existent frame number %d",
2288 msg.frame_number);
2289 return;
2290 }
2291 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2292 mId, __FUNCTION__,
2293 msg.frame_number, resultExtras.requestId, msg.timestamp);
2294 // Call listener, if any
2295 if (listener != NULL) {
2296 listener->notifyShutter(resultExtras, msg.timestamp);
2297 }
2298}
2299
2300
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002301CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002302 ALOGV("%s", __FUNCTION__);
2303
Igor Murashkin1e479c02013-09-06 16:55:14 -07002304 CameraMetadata retVal;
2305
2306 if (mRequestThread != NULL) {
2307 retVal = mRequestThread->getLatestRequest();
2308 }
2309
Igor Murashkin1e479c02013-09-06 16:55:14 -07002310 return retVal;
2311}
2312
Jianing Weicb0652e2014-03-12 18:29:36 -07002313
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002314/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002315 * RequestThread inner class methods
2316 */
2317
2318Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002319 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002320 camera3_device_t *hal3Device) :
2321 Thread(false),
2322 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002323 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002324 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002325 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002326 mReconfigured(false),
2327 mDoPause(false),
2328 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002329 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002330 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002331 mCurrentAfTriggerId(0),
2332 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002333 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002334 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002335}
2336
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002337void Camera3Device::RequestThread::setNotifyCallback(
2338 NotificationListener *listener) {
2339 Mutex::Autolock l(mRequestLock);
2340 mListener = listener;
2341}
2342
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002343void Camera3Device::RequestThread::configurationComplete() {
2344 Mutex::Autolock l(mRequestLock);
2345 mReconfigured = true;
2346}
2347
Jianing Wei90e59c92014-03-12 18:29:36 -07002348status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002349 List<sp<CaptureRequest> > &requests,
2350 /*out*/
2351 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002352 Mutex::Autolock l(mRequestLock);
2353 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2354 ++it) {
2355 mRequestQueue.push_back(*it);
2356 }
2357
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002358 if (lastFrameNumber != NULL) {
2359 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2360 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2361 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2362 *lastFrameNumber);
2363 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002364
Jianing Wei90e59c92014-03-12 18:29:36 -07002365 unpauseForNewRequests();
2366
2367 return OK;
2368}
2369
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002370
2371status_t Camera3Device::RequestThread::queueTrigger(
2372 RequestTrigger trigger[],
2373 size_t count) {
2374
2375 Mutex::Autolock l(mTriggerMutex);
2376 status_t ret;
2377
2378 for (size_t i = 0; i < count; ++i) {
2379 ret = queueTriggerLocked(trigger[i]);
2380
2381 if (ret != OK) {
2382 return ret;
2383 }
2384 }
2385
2386 return OK;
2387}
2388
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002389int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2390 sp<Camera3Device> d = device.promote();
2391 if (d != NULL) return d->mId;
2392 return 0;
2393}
2394
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002395status_t Camera3Device::RequestThread::queueTriggerLocked(
2396 RequestTrigger trigger) {
2397
2398 uint32_t tag = trigger.metadataTag;
2399 ssize_t index = mTriggerMap.indexOfKey(tag);
2400
2401 switch (trigger.getTagType()) {
2402 case TYPE_BYTE:
2403 // fall-through
2404 case TYPE_INT32:
2405 break;
2406 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002407 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2408 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002409 return INVALID_OPERATION;
2410 }
2411
2412 /**
2413 * Collect only the latest trigger, since we only have 1 field
2414 * in the request settings per trigger tag, and can't send more than 1
2415 * trigger per request.
2416 */
2417 if (index != NAME_NOT_FOUND) {
2418 mTriggerMap.editValueAt(index) = trigger;
2419 } else {
2420 mTriggerMap.add(tag, trigger);
2421 }
2422
2423 return OK;
2424}
2425
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002426status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002427 const RequestList &requests,
2428 /*out*/
2429 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002430 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002431 if (lastFrameNumber != NULL) {
2432 *lastFrameNumber = mRepeatingLastFrameNumber;
2433 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002434 mRepeatingRequests.clear();
2435 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2436 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002437
2438 unpauseForNewRequests();
2439
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002440 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002441 return OK;
2442}
2443
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002444bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2445 if (mRepeatingRequests.empty()) {
2446 return false;
2447 }
2448 int32_t requestId = requestIn->mResultExtras.requestId;
2449 const RequestList &repeatRequests = mRepeatingRequests;
2450 // All repeating requests are guaranteed to have same id so only check first quest
2451 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2452 return (firstRequest->mResultExtras.requestId == requestId);
2453}
2454
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002455status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002456 Mutex::Autolock l(mRequestLock);
2457 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002458 if (lastFrameNumber != NULL) {
2459 *lastFrameNumber = mRepeatingLastFrameNumber;
2460 }
2461 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002462 return OK;
2463}
2464
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002465status_t Camera3Device::RequestThread::clear(
2466 NotificationListener *listener,
2467 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002468 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002469 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002470
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002471 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002472
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002473 // Send errors for all requests pending in the request queue, including
2474 // pending repeating requests
2475 if (listener != NULL) {
2476 for (RequestList::iterator it = mRequestQueue.begin();
2477 it != mRequestQueue.end(); ++it) {
2478 // Set the frame number this request would have had, if it
2479 // had been submitted; this frame number will not be reused.
2480 // The requestId and burstId fields were set when the request was
2481 // submitted originally (in convertMetadataListToRequestListLocked)
2482 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2483 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2484 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002485 }
2486 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002487 mRequestQueue.clear();
2488 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002489 if (lastFrameNumber != NULL) {
2490 *lastFrameNumber = mRepeatingLastFrameNumber;
2491 }
2492 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002493 return OK;
2494}
2495
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002496void Camera3Device::RequestThread::setPaused(bool paused) {
2497 Mutex::Autolock l(mPauseLock);
2498 mDoPause = paused;
2499 mDoPauseSignal.signal();
2500}
2501
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002502status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2503 int32_t requestId, nsecs_t timeout) {
2504 Mutex::Autolock l(mLatestRequestMutex);
2505 status_t res;
2506 while (mLatestRequestId != requestId) {
2507 nsecs_t startTime = systemTime();
2508
2509 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2510 if (res != OK) return res;
2511
2512 timeout -= (systemTime() - startTime);
2513 }
2514
2515 return OK;
2516}
2517
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002518void Camera3Device::RequestThread::requestExit() {
2519 // Call parent to set up shutdown
2520 Thread::requestExit();
2521 // The exit from any possible waits
2522 mDoPauseSignal.signal();
2523 mRequestSignal.signal();
2524}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002525
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002526bool Camera3Device::RequestThread::threadLoop() {
2527
2528 status_t res;
2529
2530 // Handle paused state.
2531 if (waitIfPaused()) {
2532 return true;
2533 }
2534
2535 // Get work to do
2536
2537 sp<CaptureRequest> nextRequest = waitForNextRequest();
2538 if (nextRequest == NULL) {
2539 return true;
2540 }
2541
2542 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002543 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002544 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002545 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002546
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002547 // Get the request ID, if any
2548 int requestId;
2549 camera_metadata_entry_t requestIdEntry =
2550 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2551 if (requestIdEntry.count > 0) {
2552 requestId = requestIdEntry.data.i32[0];
2553 } else {
2554 ALOGW("%s: Did not have android.request.id set in the request",
2555 __FUNCTION__);
2556 requestId = NAME_NOT_FOUND;
2557 }
2558
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002559 // Insert any queued triggers (before metadata is locked)
2560 int32_t triggerCount;
2561 res = insertTriggers(nextRequest);
2562 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002563 SET_ERR("RequestThread: Unable to insert triggers "
2564 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002565 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002566 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2567 return false;
2568 }
2569 triggerCount = res;
2570
2571 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2572
2573 // If the request is the same as last, or we had triggers last time
2574 if (mPrevRequest != nextRequest || triggersMixedIn) {
2575 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002576 * HAL workaround:
2577 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2578 */
2579 res = addDummyTriggerIds(nextRequest);
2580 if (res != OK) {
2581 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2582 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002583 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002584 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2585 return false;
2586 }
2587
2588 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002589 * The request should be presorted so accesses in HAL
2590 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2591 */
2592 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002593 request.settings = nextRequest->mSettings.getAndLock();
2594 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002595 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2596
2597 IF_ALOGV() {
2598 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2599 find_camera_metadata_ro_entry(
2600 request.settings,
2601 ANDROID_CONTROL_AF_TRIGGER,
2602 &e
2603 );
2604 if (e.count > 0) {
2605 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2606 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002607 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002608 e.data.u8[0]);
2609 }
2610 }
2611 } else {
2612 // leave request.settings NULL to indicate 'reuse latest given'
2613 ALOGVV("%s: Request settings are REUSED",
2614 __FUNCTION__);
2615 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002616
2617 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002618 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002619
2620 // Fill in buffers
2621
2622 if (nextRequest->mInputStream != NULL) {
2623 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002624 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002625 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002626 // Can't get input buffer from gralloc queue - this could be due to
2627 // disconnected queue or other producer misbehavior, so not a fatal
2628 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002629 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002631 Mutex::Autolock l(mRequestLock);
2632 if (mListener != NULL) {
2633 mListener->notifyError(
2634 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2635 nextRequest->mResultExtras);
2636 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002637 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2638 return true;
2639 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002640 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002641 } else {
2642 request.input_buffer = NULL;
2643 }
2644
2645 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2646 nextRequest->mOutputStreams.size());
2647 request.output_buffers = outputBuffers.array();
2648 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2649 res = nextRequest->mOutputStreams.editItemAt(i)->
2650 getBuffer(&outputBuffers.editItemAt(i));
2651 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002652 // Can't get output buffer from gralloc queue - this could be due to
2653 // abandoned queue or other consumer misbehavior, so not a fatal
2654 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002655 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2656 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002657 Mutex::Autolock l(mRequestLock);
2658 if (mListener != NULL) {
2659 mListener->notifyError(
2660 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2661 nextRequest->mResultExtras);
2662 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002663 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2664 return true;
2665 }
2666 request.num_output_buffers++;
2667 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002668 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002669
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002670 // Log request in the in-flight queue
2671 sp<Camera3Device> parent = mParent.promote();
2672 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002673 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002674 CLOGE("RequestThread: Parent is gone");
2675 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2676 return false;
2677 }
2678
Jianing Weicb0652e2014-03-12 18:29:36 -07002679 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002680 totalNumBuffers, nextRequest->mResultExtras,
2681 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002682 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2683 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002684 __FUNCTION__,
2685 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2686 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002687 if (res != OK) {
2688 SET_ERR("RequestThread: Unable to register new in-flight request:"
2689 " %s (%d)", strerror(-res), res);
2690 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2691 return false;
2692 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002693
Zhijun Hecc27e112013-10-03 16:12:43 -07002694 // Inform waitUntilRequestProcessed thread of a new request ID
2695 {
2696 Mutex::Autolock al(mLatestRequestMutex);
2697
2698 mLatestRequestId = requestId;
2699 mLatestRequestSignal.signal();
2700 }
2701
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002702 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002703 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2704 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002705 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002706 ATRACE_END();
2707
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002708 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002709 // Should only get a failure here for malformed requests or device-level
2710 // errors, so consider all errors fatal. Bad metadata failures should
2711 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002712 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002713 " device: %s (%d)", request.frame_number, strerror(-res), res);
2714 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2715 return false;
2716 }
2717
Igor Murashkin1e479c02013-09-06 16:55:14 -07002718 // Update the latest request sent to HAL
2719 if (request.settings != NULL) { // Don't update them if they were unchanged
2720 Mutex::Autolock al(mLatestRequestMutex);
2721
2722 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2723 mLatestRequest.acquire(cloned);
2724 }
2725
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002726 if (request.settings != NULL) {
2727 nextRequest->mSettings.unlock(request.settings);
2728 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002729
2730 // Remove any previously queued triggers (after unlock)
2731 res = removeTriggers(mPrevRequest);
2732 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002733 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002734 "(capture request %d, HAL device: %s (%d)",
2735 request.frame_number, strerror(-res), res);
2736 return false;
2737 }
2738 mPrevTriggers = triggerCount;
2739
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002740 return true;
2741}
2742
Igor Murashkin1e479c02013-09-06 16:55:14 -07002743CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2744 Mutex::Autolock al(mLatestRequestMutex);
2745
2746 ALOGV("RequestThread::%s", __FUNCTION__);
2747
2748 return mLatestRequest;
2749}
2750
Jianing Weicb0652e2014-03-12 18:29:36 -07002751
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002752void Camera3Device::RequestThread::cleanUpFailedRequest(
2753 camera3_capture_request_t &request,
2754 sp<CaptureRequest> &nextRequest,
2755 Vector<camera3_stream_buffer_t> &outputBuffers) {
2756
2757 if (request.settings != NULL) {
2758 nextRequest->mSettings.unlock(request.settings);
2759 }
2760 if (request.input_buffer != NULL) {
2761 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002762 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763 }
2764 for (size_t i = 0; i < request.num_output_buffers; i++) {
2765 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2766 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2767 outputBuffers[i], 0);
2768 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002769}
2770
2771sp<Camera3Device::CaptureRequest>
2772 Camera3Device::RequestThread::waitForNextRequest() {
2773 status_t res;
2774 sp<CaptureRequest> nextRequest;
2775
2776 // Optimized a bit for the simple steady-state case (single repeating
2777 // request), to avoid putting that request in the queue temporarily.
2778 Mutex::Autolock l(mRequestLock);
2779
2780 while (mRequestQueue.empty()) {
2781 if (!mRepeatingRequests.empty()) {
2782 // Always atomically enqueue all requests in a repeating request
2783 // list. Guarantees a complete in-sequence set of captures to
2784 // application.
2785 const RequestList &requests = mRepeatingRequests;
2786 RequestList::const_iterator firstRequest =
2787 requests.begin();
2788 nextRequest = *firstRequest;
2789 mRequestQueue.insert(mRequestQueue.end(),
2790 ++firstRequest,
2791 requests.end());
2792 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002793
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002794 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002795
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002796 break;
2797 }
2798
2799 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2800
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002801 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2802 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002803 Mutex::Autolock pl(mPauseLock);
2804 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002805 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002806 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002807 // Let the tracker know
2808 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2809 if (statusTracker != 0) {
2810 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2811 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002812 }
2813 // Stop waiting for now and let thread management happen
2814 return NULL;
2815 }
2816 }
2817
2818 if (nextRequest == NULL) {
2819 // Don't have a repeating request already in hand, so queue
2820 // must have an entry now.
2821 RequestList::iterator firstRequest =
2822 mRequestQueue.begin();
2823 nextRequest = *firstRequest;
2824 mRequestQueue.erase(firstRequest);
2825 }
2826
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002827 // In case we've been unpaused by setPaused clearing mDoPause, need to
2828 // update internal pause state (capture/setRepeatingRequest unpause
2829 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002830 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002831 if (mPaused) {
2832 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2833 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2834 if (statusTracker != 0) {
2835 statusTracker->markComponentActive(mStatusId);
2836 }
2837 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002838 mPaused = false;
2839
2840 // Check if we've reconfigured since last time, and reset the preview
2841 // request if so. Can't use 'NULL request == repeat' across configure calls.
2842 if (mReconfigured) {
2843 mPrevRequest.clear();
2844 mReconfigured = false;
2845 }
2846
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002847 if (nextRequest != NULL) {
2848 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002849 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2850 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002851 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002852 return nextRequest;
2853}
2854
2855bool Camera3Device::RequestThread::waitIfPaused() {
2856 status_t res;
2857 Mutex::Autolock l(mPauseLock);
2858 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002859 if (mPaused == false) {
2860 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002861 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2862 // Let the tracker know
2863 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2864 if (statusTracker != 0) {
2865 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2866 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002867 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002868
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002869 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002870 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002871 return true;
2872 }
2873 }
2874 // We don't set mPaused to false here, because waitForNextRequest needs
2875 // to further manage the paused state in case of starvation.
2876 return false;
2877}
2878
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002879void Camera3Device::RequestThread::unpauseForNewRequests() {
2880 // With work to do, mark thread as unpaused.
2881 // If paused by request (setPaused), don't resume, to avoid
2882 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002883 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002884 Mutex::Autolock p(mPauseLock);
2885 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002886 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2887 if (mPaused) {
2888 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2889 if (statusTracker != 0) {
2890 statusTracker->markComponentActive(mStatusId);
2891 }
2892 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002893 mPaused = false;
2894 }
2895}
2896
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002897void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2898 sp<Camera3Device> parent = mParent.promote();
2899 if (parent != NULL) {
2900 va_list args;
2901 va_start(args, fmt);
2902
2903 parent->setErrorStateV(fmt, args);
2904
2905 va_end(args);
2906 }
2907}
2908
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002909status_t Camera3Device::RequestThread::insertTriggers(
2910 const sp<CaptureRequest> &request) {
2911
2912 Mutex::Autolock al(mTriggerMutex);
2913
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002914 sp<Camera3Device> parent = mParent.promote();
2915 if (parent == NULL) {
2916 CLOGE("RequestThread: Parent is gone");
2917 return DEAD_OBJECT;
2918 }
2919
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002920 CameraMetadata &metadata = request->mSettings;
2921 size_t count = mTriggerMap.size();
2922
2923 for (size_t i = 0; i < count; ++i) {
2924 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002925 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002926
2927 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2928 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2929 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002930 if (isAeTrigger) {
2931 request->mResultExtras.precaptureTriggerId = triggerId;
2932 mCurrentPreCaptureTriggerId = triggerId;
2933 } else {
2934 request->mResultExtras.afTriggerId = triggerId;
2935 mCurrentAfTriggerId = triggerId;
2936 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002937 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2938 continue; // Trigger ID tag is deprecated since device HAL 3.2
2939 }
2940 }
2941
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002942 camera_metadata_entry entry = metadata.find(tag);
2943
2944 if (entry.count > 0) {
2945 /**
2946 * Already has an entry for this trigger in the request.
2947 * Rewrite it with our requested trigger value.
2948 */
2949 RequestTrigger oldTrigger = trigger;
2950
2951 oldTrigger.entryValue = entry.data.u8[0];
2952
2953 mTriggerReplacedMap.add(tag, oldTrigger);
2954 } else {
2955 /**
2956 * More typical, no trigger entry, so we just add it
2957 */
2958 mTriggerRemovedMap.add(tag, trigger);
2959 }
2960
2961 status_t res;
2962
2963 switch (trigger.getTagType()) {
2964 case TYPE_BYTE: {
2965 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2966 res = metadata.update(tag,
2967 &entryValue,
2968 /*count*/1);
2969 break;
2970 }
2971 case TYPE_INT32:
2972 res = metadata.update(tag,
2973 &trigger.entryValue,
2974 /*count*/1);
2975 break;
2976 default:
2977 ALOGE("%s: Type not supported: 0x%x",
2978 __FUNCTION__,
2979 trigger.getTagType());
2980 return INVALID_OPERATION;
2981 }
2982
2983 if (res != OK) {
2984 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2985 ", value %d", __FUNCTION__, trigger.getTagName(),
2986 trigger.entryValue);
2987 return res;
2988 }
2989
2990 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2991 trigger.getTagName(),
2992 trigger.entryValue);
2993 }
2994
2995 mTriggerMap.clear();
2996
2997 return count;
2998}
2999
3000status_t Camera3Device::RequestThread::removeTriggers(
3001 const sp<CaptureRequest> &request) {
3002 Mutex::Autolock al(mTriggerMutex);
3003
3004 CameraMetadata &metadata = request->mSettings;
3005
3006 /**
3007 * Replace all old entries with their old values.
3008 */
3009 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3010 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3011
3012 status_t res;
3013
3014 uint32_t tag = trigger.metadataTag;
3015 switch (trigger.getTagType()) {
3016 case TYPE_BYTE: {
3017 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3018 res = metadata.update(tag,
3019 &entryValue,
3020 /*count*/1);
3021 break;
3022 }
3023 case TYPE_INT32:
3024 res = metadata.update(tag,
3025 &trigger.entryValue,
3026 /*count*/1);
3027 break;
3028 default:
3029 ALOGE("%s: Type not supported: 0x%x",
3030 __FUNCTION__,
3031 trigger.getTagType());
3032 return INVALID_OPERATION;
3033 }
3034
3035 if (res != OK) {
3036 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3037 ", trigger value %d", __FUNCTION__,
3038 trigger.getTagName(), trigger.entryValue);
3039 return res;
3040 }
3041 }
3042 mTriggerReplacedMap.clear();
3043
3044 /**
3045 * Remove all new entries.
3046 */
3047 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3048 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3049 status_t res = metadata.erase(trigger.metadataTag);
3050
3051 if (res != OK) {
3052 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3053 ", trigger value %d", __FUNCTION__,
3054 trigger.getTagName(), trigger.entryValue);
3055 return res;
3056 }
3057 }
3058 mTriggerRemovedMap.clear();
3059
3060 return OK;
3061}
3062
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003063status_t Camera3Device::RequestThread::addDummyTriggerIds(
3064 const sp<CaptureRequest> &request) {
3065 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3066 static const int32_t dummyTriggerId = 1;
3067 status_t res;
3068
3069 CameraMetadata &metadata = request->mSettings;
3070
3071 // If AF trigger is active, insert a dummy AF trigger ID if none already
3072 // exists
3073 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3074 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3075 if (afTrigger.count > 0 &&
3076 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3077 afId.count == 0) {
3078 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3079 if (res != OK) return res;
3080 }
3081
3082 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3083 // if none already exists
3084 camera_metadata_entry pcTrigger =
3085 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3086 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3087 if (pcTrigger.count > 0 &&
3088 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3089 pcId.count == 0) {
3090 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3091 &dummyTriggerId, 1);
3092 if (res != OK) return res;
3093 }
3094
3095 return OK;
3096}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003097
3098
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003099/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003100 * Static callback forwarding methods from HAL to instance
3101 */
3102
3103void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3104 const camera3_capture_result *result) {
3105 Camera3Device *d =
3106 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3107 d->processCaptureResult(result);
3108}
3109
3110void Camera3Device::sNotify(const camera3_callback_ops *cb,
3111 const camera3_notify_msg *msg) {
3112 Camera3Device *d =
3113 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3114 d->notify(msg);
3115}
3116
3117}; // namespace android