blob: 6f78db5cd0b3056c1da11922eb84fe759376436e [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Camera3-Device"
18#define ATRACE_TAG ATRACE_TAG_CAMERA
19//#define LOG_NDEBUG 0
20//#define LOG_NNDEBUG 0 // Per-frame verbose logging
21
22#ifdef LOG_NNDEBUG
23#define ALOGVV(...) ALOGV(__VA_ARGS__)
24#else
25#define ALOGVV(...) ((void)0)
26#endif
27
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
29#define CLOGE(fmt, ...) ALOGE("Camera %d: %s: " fmt, mId, __FUNCTION__, \
30 ##__VA_ARGS__)
31
32// Convenience macros for transitioning to the error state
33#define SET_ERR(fmt, ...) setErrorState( \
34 "%s: " fmt, __FUNCTION__, \
35 ##__VA_ARGS__)
36#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39
Colin Crosse5729fa2014-03-21 15:04:25 -070040#include <inttypes.h>
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042#include <utils/Log.h>
43#include <utils/Trace.h>
44#include <utils/Timers.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070045
Igor Murashkinff3e31d2013-10-23 16:40:06 -070046#include "utils/CameraTraces.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070047#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -070051#include "device3/Camera3DummyStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070052#include "CameraService.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080053
54using namespace android::camera3;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055
56namespace android {
57
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058Camera3Device::Camera3Device(int id):
59 mId(id),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080060 mHal3Device(NULL),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070061 mStatus(STATUS_UNINITIALIZED),
Zhijun He204e3292014-07-14 17:09:23 -070062 mUsePartialResult(false),
63 mNumPartialResults(1),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070064 mNextResultFrameNumber(0),
65 mNextShutterFrameNumber(0),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070066 mListener(NULL)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080067{
68 ATRACE_CALL();
69 camera3_callback_ops::notify = &sNotify;
70 camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
71 ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
72}
73
74Camera3Device::~Camera3Device()
75{
76 ATRACE_CALL();
77 ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
78 disconnect();
79}
80
Igor Murashkin71381052013-03-04 14:53:08 -080081int Camera3Device::getId() const {
82 return mId;
83}
84
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080085/**
86 * CameraDeviceBase interface
87 */
88
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089status_t Camera3Device::initialize(camera_module_t *module)
90{
91 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070092 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080093 Mutex::Autolock l(mLock);
94
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080095 ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080096 if (mStatus != STATUS_UNINITIALIZED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070097 CLOGE("Already initialized!");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098 return INVALID_OPERATION;
99 }
100
101 /** Open HAL device */
102
103 status_t res;
104 String8 deviceName = String8::format("%d", mId);
105
106 camera3_device_t *device;
107
Zhijun He213ce792013-11-19 08:45:15 -0800108 ATRACE_BEGIN("camera3->open");
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700109 res = CameraService::filterOpenErrorCode(module->common.methods->open(
110 &module->common, deviceName.string(),
111 reinterpret_cast<hw_device_t**>(&device)));
Zhijun He213ce792013-11-19 08:45:15 -0800112 ATRACE_END();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113
114 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700115 SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 return res;
117 }
118
119 /** Cross-check device version */
Zhijun He95dd5ba2014-03-26 18:18:00 -0700120 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700121 SET_ERR_L("Could not open camera: "
Zhijun He95dd5ba2014-03-26 18:18:00 -0700122 "Camera device should be at least %x, reports %x instead",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700123 CAMERA_DEVICE_API_VERSION_3_0,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 device->common.version);
125 device->common.close(&device->common);
126 return BAD_VALUE;
127 }
128
129 camera_info info;
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -0700130 res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
131 mId, &info));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800132 if (res != OK) return res;
133
134 if (info.device_version != device->common.version) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700135 SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
136 " and device version (%x).",
Zhijun He95dd5ba2014-03-26 18:18:00 -0700137 info.device_version, device->common.version);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800138 device->common.close(&device->common);
139 return BAD_VALUE;
140 }
141
142 /** Initialize device with callback functions */
143
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700144 ATRACE_BEGIN("camera3->initialize");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800145 res = device->ops->initialize(device, this);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -0700146 ATRACE_END();
147
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700149 SET_ERR_L("Unable to initialize HAL device: %s (%d)",
150 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800151 device->common.close(&device->common);
152 return BAD_VALUE;
153 }
154
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700155 /** Start up status tracker thread */
156 mStatusTracker = new StatusTracker(this);
157 res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
158 if (res != OK) {
159 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
160 strerror(-res), res);
161 device->common.close(&device->common);
162 mStatusTracker.clear();
163 return res;
164 }
165
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800166 /** Start up request queue thread */
167
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700168 mRequestThread = new RequestThread(this, mStatusTracker, device);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800169 res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800170 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700171 SET_ERR_L("Unable to start request queue thread: %s (%d)",
172 strerror(-res), res);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800173 device->common.close(&device->common);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800174 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800175 return res;
176 }
177
178 /** Everything is good to go */
179
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700180 mDeviceVersion = device->common.version;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800181 mDeviceInfo = info.static_camera_characteristics;
182 mHal3Device = device;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700183 mStatus = STATUS_UNCONFIGURED;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800184 mNextStreamId = 0;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -0700185 mDummyStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700186 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700187 mPauseStateNotify = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800188
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700189 // Will the HAL be sending in early partial result metadata?
Zhijun He204e3292014-07-14 17:09:23 -0700190 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
191 camera_metadata_entry partialResultsCount =
192 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
193 if (partialResultsCount.count > 0) {
194 mNumPartialResults = partialResultsCount.data.i32[0];
195 mUsePartialResult = (mNumPartialResults > 1);
196 }
197 } else {
198 camera_metadata_entry partialResultsQuirk =
199 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
200 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
201 mUsePartialResult = true;
202 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700203 }
204
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800205 return OK;
206}
207
208status_t Camera3Device::disconnect() {
209 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700210 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800211
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800212 ALOGV("%s: E", __FUNCTION__);
213
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700214 status_t res = OK;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800215
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700216 {
217 Mutex::Autolock l(mLock);
218 if (mStatus == STATUS_UNINITIALIZED) return res;
219
220 if (mStatus == STATUS_ACTIVE ||
221 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
222 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700223 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700225 // Continue to close device even in case of error
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700226 } else {
227 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
228 if (res != OK) {
229 SET_ERR_L("Timeout waiting for HAL to drain");
230 // Continue to close device even in case of error
231 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700232 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800233 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800234
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700235 if (mStatus == STATUS_ERROR) {
236 CLOGE("Shutting down in an error state");
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700237 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700238
239 if (mStatusTracker != NULL) {
240 mStatusTracker->requestExit();
241 }
242
243 if (mRequestThread != NULL) {
244 mRequestThread->requestExit();
245 }
246
247 mOutputStreams.clear();
248 mInputStream.clear();
249 }
250
251 // Joining done without holding mLock, otherwise deadlocks may ensue
252 // as the threads try to access parent state
253 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
254 // HAL may be in a bad state, so waiting for request thread
255 // (which may be stuck in the HAL processCaptureRequest call)
256 // could be dangerous.
257 mRequestThread->join();
258 }
259
260 if (mStatusTracker != NULL) {
261 mStatusTracker->join();
262 }
263
264 {
265 Mutex::Autolock l(mLock);
266
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800267 mRequestThread.clear();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700268 mStatusTracker.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800269
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700270 if (mHal3Device != NULL) {
Zhijun He213ce792013-11-19 08:45:15 -0800271 ATRACE_BEGIN("camera3->close");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700272 mHal3Device->common.close(&mHal3Device->common);
Zhijun He213ce792013-11-19 08:45:15 -0800273 ATRACE_END();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 mHal3Device = NULL;
275 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800276
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 mStatus = STATUS_UNINITIALIZED;
278 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800279
280 ALOGV("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700281 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800282}
283
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700284// For dumping/debugging only -
285// try to acquire a lock a few times, eventually give up to proceed with
286// debug/dump operations
287bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
288 bool gotLock = false;
289 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
290 if (lock.tryLock() == NO_ERROR) {
291 gotLock = true;
292 break;
293 } else {
294 usleep(kDumpSleepDuration);
295 }
296 }
297 return gotLock;
298}
299
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700300Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
301 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
302 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
303 const int STREAM_CONFIGURATION_SIZE = 4;
304 const int STREAM_FORMAT_OFFSET = 0;
305 const int STREAM_WIDTH_OFFSET = 1;
306 const int STREAM_HEIGHT_OFFSET = 2;
307 const int STREAM_IS_INPUT_OFFSET = 3;
308 camera_metadata_ro_entry_t availableStreamConfigs =
309 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
310 if (availableStreamConfigs.count == 0 ||
311 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
312 return Size(0, 0);
313 }
314
315 // Get max jpeg size (area-wise).
316 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
317 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
318 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
319 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
320 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
321 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
322 && format == HAL_PIXEL_FORMAT_BLOB &&
323 (width * height > maxJpegWidth * maxJpegHeight)) {
324 maxJpegWidth = width;
325 maxJpegHeight = height;
326 }
327 }
328 } else {
329 camera_metadata_ro_entry availableJpegSizes =
330 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
331 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
332 return Size(0, 0);
333 }
334
335 // Get max jpeg size (area-wise).
336 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
337 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
338 > (maxJpegWidth * maxJpegHeight)) {
339 maxJpegWidth = availableJpegSizes.data.i32[i];
340 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
341 }
342 }
343 }
344 return Size(maxJpegWidth, maxJpegHeight);
345}
346
Zhijun Hef7da0962014-04-24 13:27:56 -0700347ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700348 // Get max jpeg size (area-wise).
349 Size maxJpegResolution = getMaxJpegResolution();
350 if (maxJpegResolution.width == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700351 ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
352 __FUNCTION__, mId);
353 return BAD_VALUE;
354 }
355
Zhijun Hef7da0962014-04-24 13:27:56 -0700356 // Get max jpeg buffer size
357 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700358 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
359 if (jpegBufMaxSize.count == 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700360 ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
361 return BAD_VALUE;
362 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700363 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Zhijun Hef7da0962014-04-24 13:27:56 -0700364
365 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700366 float scaleFactor = ((float) (width * height)) /
367 (maxJpegResolution.width * maxJpegResolution.height);
Zhijun Hef7da0962014-04-24 13:27:56 -0700368 ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
369 // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
370 if (jpegBufferSize > maxJpegBufferSize) {
371 jpegBufferSize = maxJpegBufferSize;
372 } else if (jpegBufferSize < kMinJpegBufferSize) {
373 jpegBufferSize = kMinJpegBufferSize;
374 }
375
376 return jpegBufferSize;
377}
378
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800379status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
380 ATRACE_CALL();
381 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382
383 // Try to lock, but continue in case of failure (to avoid blocking in
384 // deadlocks)
385 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
386 bool gotLock = tryLockSpinRightRound(mLock);
387
388 ALOGW_IF(!gotInterfaceLock,
389 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
390 mId, __FUNCTION__);
391 ALOGW_IF(!gotLock,
392 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
393 mId, __FUNCTION__);
394
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800396
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800397 const char *status =
398 mStatus == STATUS_ERROR ? "ERROR" :
399 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700400 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
401 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800402 mStatus == STATUS_ACTIVE ? "ACTIVE" :
403 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800405 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700406 if (mStatus == STATUS_ERROR) {
407 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
408 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800409 lines.appendFormat(" Stream configuration:\n");
410
411 if (mInputStream != NULL) {
412 write(fd, lines.string(), lines.size());
413 mInputStream->dump(fd, args);
414 } else {
415 lines.appendFormat(" No input stream.\n");
416 write(fd, lines.string(), lines.size());
417 }
418 for (size_t i = 0; i < mOutputStreams.size(); i++) {
419 mOutputStreams[i]->dump(fd,args);
420 }
421
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700422 lines = String8(" In-flight requests:\n");
423 if (mInFlightMap.size() == 0) {
424 lines.append(" None\n");
425 } else {
426 for (size_t i = 0; i < mInFlightMap.size(); i++) {
427 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700428 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700429 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
430 r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
431 r.numBuffersLeft);
432 }
433 }
434 write(fd, lines.string(), lines.size());
435
Igor Murashkin1e479c02013-09-06 16:55:14 -0700436 {
437 lines = String8(" Last request sent:\n");
438 write(fd, lines.string(), lines.size());
439
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700441 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
442 }
443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 write(fd, lines.string(), lines.size());
447 mHal3Device->ops->dump(mHal3Device, fd);
448 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 if (gotLock) mLock.unlock();
451 if (gotInterfaceLock) mInterfaceLock.unlock();
452
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800453 return OK;
454}
455
456const CameraMetadata& Camera3Device::info() const {
457 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
459 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700460 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 mStatus == STATUS_ERROR ?
462 "when in error state" : "before init");
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464 return mDeviceInfo;
465}
466
Jianing Wei90e59c92014-03-12 18:29:36 -0700467status_t Camera3Device::checkStatusOkToCaptureLocked() {
468 switch (mStatus) {
469 case STATUS_ERROR:
470 CLOGE("Device has encountered a serious error");
471 return INVALID_OPERATION;
472 case STATUS_UNINITIALIZED:
473 CLOGE("Device not initialized");
474 return INVALID_OPERATION;
475 case STATUS_UNCONFIGURED:
476 case STATUS_CONFIGURED:
477 case STATUS_ACTIVE:
478 // OK
479 break;
480 default:
481 SET_ERR_L("Unexpected status: %d", mStatus);
482 return INVALID_OPERATION;
483 }
484 return OK;
485}
486
487status_t Camera3Device::convertMetadataListToRequestListLocked(
488 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
489 if (requestList == NULL) {
490 CLOGE("requestList cannot be NULL.");
491 return BAD_VALUE;
492 }
493
Jianing Weicb0652e2014-03-12 18:29:36 -0700494 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700495 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
496 it != metadataList.end(); ++it) {
497 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
498 if (newRequest == 0) {
499 CLOGE("Can't create capture request");
500 return BAD_VALUE;
501 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700502
503 // Setup burst Id and request Id
504 newRequest->mResultExtras.burstId = burstId++;
505 if (it->exists(ANDROID_REQUEST_ID)) {
506 if (it->find(ANDROID_REQUEST_ID).count == 0) {
507 CLOGE("RequestID entry exists; but must not be empty in metadata");
508 return BAD_VALUE;
509 }
510 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
511 } else {
512 CLOGE("RequestID does not exist in metadata");
513 return BAD_VALUE;
514 }
515
Jianing Wei90e59c92014-03-12 18:29:36 -0700516 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700517
518 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700519 }
520 return OK;
521}
522
Jianing Weicb0652e2014-03-12 18:29:36 -0700523status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700526 List<const CameraMetadata> requests;
527 requests.push_back(request);
528 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529}
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532 const List<const CameraMetadata> &requests, bool repeating,
533 /*out*/
534 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700535 ATRACE_CALL();
536 Mutex::Autolock il(mInterfaceLock);
537 Mutex::Autolock l(mLock);
538
539 status_t res = checkStatusOkToCaptureLocked();
540 if (res != OK) {
541 // error logged by previous call
542 return res;
543 }
544
545 RequestList requestList;
546
547 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
548 if (res != OK) {
549 // error logged by previous call
550 return res;
551 }
552
553 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700554 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700556 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558
559 if (res == OK) {
560 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
561 if (res != OK) {
562 SET_ERR_L("Can't transition to active in %f seconds!",
563 kActiveTimeout/1e9);
564 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700565 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
566 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 } else {
568 CLOGE("Cannot queue request. Impossible.");
569 return BAD_VALUE;
570 }
571
572 return res;
573}
574
Jianing Weicb0652e2014-03-12 18:29:36 -0700575status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
576 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700577 ATRACE_CALL();
578
Jianing Weicb0652e2014-03-12 18:29:36 -0700579 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800581
Jianing Weicb0652e2014-03-12 18:29:36 -0700582status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
583 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800584 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700586 List<const CameraMetadata> requests;
587 requests.push_back(request);
588 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589}
590
Jianing Weicb0652e2014-03-12 18:29:36 -0700591status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
592 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700593 ATRACE_CALL();
594
Jianing Weicb0652e2014-03-12 18:29:36 -0700595 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700596}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597
598sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
599 const CameraMetadata &request) {
600 status_t res;
601
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 res = configureStreamsLocked();
604 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700605 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800606 return NULL;
607 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700608 if (mStatus == STATUS_UNCONFIGURED) {
609 CLOGE("No streams configured");
610 return NULL;
611 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800612 }
613
614 sp<CaptureRequest> newRequest = createCaptureRequest(request);
615 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800616}
617
Jianing Weicb0652e2014-03-12 18:29:36 -0700618status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800619 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700620 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800621 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800622
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800623 switch (mStatus) {
624 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700625 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800626 return INVALID_OPERATION;
627 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700628 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700630 case STATUS_UNCONFIGURED:
631 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800632 case STATUS_ACTIVE:
633 // OK
634 break;
635 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700636 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 return INVALID_OPERATION;
638 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700639 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700640
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700641 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800642}
643
644status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
645 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700646 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800647
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700648 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800649}
650
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700651status_t Camera3Device::createInputStream(
652 uint32_t width, uint32_t height, int format, int *id) {
653 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700655 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700656 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
657 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700658
659 status_t res;
660 bool wasActive = false;
661
662 switch (mStatus) {
663 case STATUS_ERROR:
664 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
665 return INVALID_OPERATION;
666 case STATUS_UNINITIALIZED:
667 ALOGE("%s: Device not initialized", __FUNCTION__);
668 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700669 case STATUS_UNCONFIGURED:
670 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700671 // OK
672 break;
673 case STATUS_ACTIVE:
674 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700675 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700676 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700678 return res;
679 }
680 wasActive = true;
681 break;
682 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700684 return INVALID_OPERATION;
685 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700686 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700687
688 if (mInputStream != 0) {
689 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
690 return INVALID_OPERATION;
691 }
692
693 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
694 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700695 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700696
697 mInputStream = newStream;
698
699 *id = mNextStreamId++;
700
701 // Continue captures if active at start
702 if (wasActive) {
703 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
704 res = configureStreamsLocked();
705 if (res != OK) {
706 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
707 __FUNCTION__, mNextStreamId, strerror(-res), res);
708 return res;
709 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700710 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700711 }
712
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700713 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700714 return OK;
715}
716
Igor Murashkin2fba5842013-04-22 14:03:54 -0700717
718status_t Camera3Device::createZslStream(
719 uint32_t width, uint32_t height,
720 int depth,
721 /*out*/
722 int *id,
723 sp<Camera3ZslStream>* zslStream) {
724 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700725 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700726 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700727 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
728 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700729
730 status_t res;
731 bool wasActive = false;
732
733 switch (mStatus) {
734 case STATUS_ERROR:
735 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
736 return INVALID_OPERATION;
737 case STATUS_UNINITIALIZED:
738 ALOGE("%s: Device not initialized", __FUNCTION__);
739 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700740 case STATUS_UNCONFIGURED:
741 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700742 // OK
743 break;
744 case STATUS_ACTIVE:
745 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700746 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700747 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700749 return res;
750 }
751 wasActive = true;
752 break;
753 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700755 return INVALID_OPERATION;
756 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700757 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700758
759 if (mInputStream != 0) {
760 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
761 return INVALID_OPERATION;
762 }
763
764 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
765 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700766 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700767
768 res = mOutputStreams.add(mNextStreamId, newStream);
769 if (res < 0) {
770 ALOGE("%s: Can't add new stream to set: %s (%d)",
771 __FUNCTION__, strerror(-res), res);
772 return res;
773 }
774 mInputStream = newStream;
775
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530776 mNeedConfig = true;
777
Igor Murashkin2fba5842013-04-22 14:03:54 -0700778 *id = mNextStreamId++;
779 *zslStream = newStream;
780
781 // Continue captures if active at start
782 if (wasActive) {
783 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
784 res = configureStreamsLocked();
785 if (res != OK) {
786 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
787 __FUNCTION__, mNextStreamId, strerror(-res), res);
788 return res;
789 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700790 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700791 }
792
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700793 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700794 return OK;
795}
796
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800797status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700798 uint32_t width, uint32_t height, int format, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800799 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700800 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800801 Mutex::Autolock l(mLock);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700802 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
803 mId, mNextStreamId, width, height, format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800804
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800805 status_t res;
806 bool wasActive = false;
807
808 switch (mStatus) {
809 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700810 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 return INVALID_OPERATION;
812 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700813 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800814 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 case STATUS_UNCONFIGURED:
816 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800817 // OK
818 break;
819 case STATUS_ACTIVE:
820 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700821 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 return res;
825 }
826 wasActive = true;
827 break;
828 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700829 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 return INVALID_OPERATION;
831 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700832 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800833
834 sp<Camera3OutputStream> newStream;
835 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700836 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700837 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700838 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
839 return BAD_VALUE;
840 }
841
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800842 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700843 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800844 } else {
845 newStream = new Camera3OutputStream(mNextStreamId, consumer,
846 width, height, format);
847 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700848 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800849
850 res = mOutputStreams.add(mNextStreamId, newStream);
851 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700852 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800853 return res;
854 }
855
856 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700857 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800858
859 // Continue captures if active at start
860 if (wasActive) {
861 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
862 res = configureStreamsLocked();
863 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700864 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
865 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866 return res;
867 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700868 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800869 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700870 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800872}
873
874status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
875 ATRACE_CALL();
876 (void)outputId; (void)id;
877
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700878 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800879 return INVALID_OPERATION;
880}
881
882
883status_t Camera3Device::getStreamInfo(int id,
884 uint32_t *width, uint32_t *height, uint32_t *format) {
885 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700886 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800887 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800888
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800889 switch (mStatus) {
890 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700891 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800892 return INVALID_OPERATION;
893 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700894 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700896 case STATUS_UNCONFIGURED:
897 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 case STATUS_ACTIVE:
899 // OK
900 break;
901 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return INVALID_OPERATION;
904 }
905
906 ssize_t idx = mOutputStreams.indexOfKey(id);
907 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700908 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800909 return idx;
910 }
911
912 if (width) *width = mOutputStreams[idx]->getWidth();
913 if (height) *height = mOutputStreams[idx]->getHeight();
914 if (format) *format = mOutputStreams[idx]->getFormat();
915
916 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800917}
918
919status_t Camera3Device::setStreamTransform(int id,
920 int transform) {
921 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800923 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800924
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800925 switch (mStatus) {
926 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700927 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800928 return INVALID_OPERATION;
929 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700930 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700932 case STATUS_UNCONFIGURED:
933 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800934 case STATUS_ACTIVE:
935 // OK
936 break;
937 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700938 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800939 return INVALID_OPERATION;
940 }
941
942 ssize_t idx = mOutputStreams.indexOfKey(id);
943 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700944 CLOGE("Stream %d does not exist",
945 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800946 return BAD_VALUE;
947 }
948
949 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800950}
951
952status_t Camera3Device::deleteStream(int id) {
953 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700954 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800955 Mutex::Autolock l(mLock);
956 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800957
Igor Murashkine2172be2013-05-28 15:31:39 -0700958 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
959
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800960 // CameraDevice semantics require device to already be idle before
961 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700963 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
964 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800965 }
966
Igor Murashkin2fba5842013-04-22 14:03:54 -0700967 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800968 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800969 if (mInputStream != NULL && id == mInputStream->getId()) {
970 deletedStream = mInputStream;
971 mInputStream.clear();
972 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800973 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700974 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800975 return BAD_VALUE;
976 }
Zhijun He5f446352014-01-22 09:49:33 -0800977 }
978
979 // Delete output stream or the output part of a bi-directional stream.
980 if (outputStreamIdx != NAME_NOT_FOUND) {
981 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800982 mOutputStreams.removeItem(id);
983 }
984
985 // Free up the stream endpoint so that it can be used by some other stream
986 res = deletedStream->disconnect();
987 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700988 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800989 // fall through since we want to still list the stream as deleted.
990 }
991 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700992 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800993
994 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800995}
996
997status_t Camera3Device::deleteReprocessStream(int id) {
998 ATRACE_CALL();
999 (void)id;
1000
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001001 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001002 return INVALID_OPERATION;
1003}
1004
Igor Murashkine2d167e2014-08-19 16:19:59 -07001005status_t Camera3Device::configureStreams() {
1006 ATRACE_CALL();
1007 ALOGV("%s: E", __FUNCTION__);
1008
1009 Mutex::Autolock il(mInterfaceLock);
1010 Mutex::Autolock l(mLock);
1011
1012 return configureStreamsLocked();
1013}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001014
1015status_t Camera3Device::createDefaultRequest(int templateId,
1016 CameraMetadata *request) {
1017 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001018 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001019 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001020 Mutex::Autolock l(mLock);
1021
1022 switch (mStatus) {
1023 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001024 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001025 return INVALID_OPERATION;
1026 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001027 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001028 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001029 case STATUS_UNCONFIGURED:
1030 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001031 case STATUS_ACTIVE:
1032 // OK
1033 break;
1034 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001035 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 return INVALID_OPERATION;
1037 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001038
1039 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001040 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001041 rawRequest = mHal3Device->ops->construct_default_request_settings(
1042 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001043 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001044 if (rawRequest == NULL) {
1045 SET_ERR_L("HAL is unable to construct default settings for template %d",
1046 templateId);
1047 return DEAD_OBJECT;
1048 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001049 *request = rawRequest;
1050
1051 return OK;
1052}
1053
1054status_t Camera3Device::waitUntilDrained() {
1055 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001056 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001057 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001058
Zhijun He69a37482014-03-23 18:44:49 -07001059 return waitUntilDrainedLocked();
1060}
1061
1062status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001063 switch (mStatus) {
1064 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001065 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001066 ALOGV("%s: Already idle", __FUNCTION__);
1067 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001068 case STATUS_CONFIGURED:
1069 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 case STATUS_ERROR:
1071 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001072 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001073 break;
1074 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 }
1078
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001079 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1080 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1081 return res;
1082}
1083
1084// Pause to reconfigure
1085status_t Camera3Device::internalPauseAndWaitLocked() {
1086 mRequestThread->setPaused(true);
1087 mPauseStateNotify = true;
1088
1089 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1090 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1091 if (res != OK) {
1092 SET_ERR_L("Can't idle device in %f seconds!",
1093 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001094 }
1095
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001096 return res;
1097}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001098
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001099// Resume after internalPauseAndWaitLocked
1100status_t Camera3Device::internalResumeLocked() {
1101 status_t res;
1102
1103 mRequestThread->setPaused(false);
1104
1105 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1106 if (res != OK) {
1107 SET_ERR_L("Can't transition to active in %f seconds!",
1108 kActiveTimeout/1e9);
1109 }
1110 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001111 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001112}
1113
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114status_t Camera3Device::waitUntilStateThenRelock(bool active,
1115 nsecs_t timeout) {
1116 status_t res = OK;
1117 if (active == (mStatus == STATUS_ACTIVE)) {
1118 // Desired state already reached
1119 return res;
1120 }
1121
1122 bool stateSeen = false;
1123 do {
1124 mRecentStatusUpdates.clear();
1125
1126 res = mStatusChanged.waitRelative(mLock, timeout);
1127 if (res != OK) break;
1128
1129 // Check state change history during wait
1130 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1131 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1132 stateSeen = true;
1133 break;
1134 }
1135 }
1136 } while (!stateSeen);
1137
1138 return res;
1139}
1140
1141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001142status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1143 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001144 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001145
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001146 if (listener != NULL && mListener != NULL) {
1147 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1148 }
1149 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001150 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001151
1152 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001153}
1154
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001155bool Camera3Device::willNotify3A() {
1156 return false;
1157}
1158
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001159status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001160 status_t res;
1161 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001162
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001163 while (mResultQueue.empty()) {
1164 res = mResultSignal.waitRelative(mOutputLock, timeout);
1165 if (res == TIMED_OUT) {
1166 return res;
1167 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001168 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001169 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001170 return res;
1171 }
1172 }
1173 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001174}
1175
Jianing Weicb0652e2014-03-12 18:29:36 -07001176status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001177 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001178 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001179
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001180 if (mResultQueue.empty()) {
1181 return NOT_ENOUGH_DATA;
1182 }
1183
Jianing Weicb0652e2014-03-12 18:29:36 -07001184 if (frame == NULL) {
1185 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1186 return BAD_VALUE;
1187 }
1188
1189 CaptureResult &result = *(mResultQueue.begin());
1190 frame->mResultExtras = result.mResultExtras;
1191 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001192 mResultQueue.erase(mResultQueue.begin());
1193
1194 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195}
1196
1197status_t Camera3Device::triggerAutofocus(uint32_t id) {
1198 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001199 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001200
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001201 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1202 // Mix-in this trigger into the next request and only the next request.
1203 RequestTrigger trigger[] = {
1204 {
1205 ANDROID_CONTROL_AF_TRIGGER,
1206 ANDROID_CONTROL_AF_TRIGGER_START
1207 },
1208 {
1209 ANDROID_CONTROL_AF_TRIGGER_ID,
1210 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001211 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001212 };
1213
1214 return mRequestThread->queueTrigger(trigger,
1215 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001216}
1217
1218status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1219 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001220 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001221
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001222 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1223 // Mix-in this trigger into the next request and only the next request.
1224 RequestTrigger trigger[] = {
1225 {
1226 ANDROID_CONTROL_AF_TRIGGER,
1227 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1228 },
1229 {
1230 ANDROID_CONTROL_AF_TRIGGER_ID,
1231 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001232 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001233 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001235 return mRequestThread->queueTrigger(trigger,
1236 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001237}
1238
1239status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1240 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001241 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001242
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001243 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1244 // Mix-in this trigger into the next request and only the next request.
1245 RequestTrigger trigger[] = {
1246 {
1247 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1248 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1249 },
1250 {
1251 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1252 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001253 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001254 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001256 return mRequestThread->queueTrigger(trigger,
1257 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001258}
1259
1260status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1261 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1262 ATRACE_CALL();
1263 (void)reprocessStreamId; (void)buffer; (void)listener;
1264
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001265 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266 return INVALID_OPERATION;
1267}
1268
Jianing Weicb0652e2014-03-12 18:29:36 -07001269status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001270 ATRACE_CALL();
1271 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001272 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001273
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001274 NotificationListener* listener;
1275 {
1276 Mutex::Autolock l(mOutputLock);
1277 listener = mListener;
1278 }
1279
Zhijun He7ef20392014-04-21 16:04:17 -07001280 {
1281 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001282 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001283 }
1284
Zhijun He491e3412013-12-27 10:57:44 -08001285 status_t res;
1286 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1287 res = mHal3Device->ops->flush(mHal3Device);
1288 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001289 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001290 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001291 }
1292
1293 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001294}
1295
Zhijun He204e3292014-07-14 17:09:23 -07001296uint32_t Camera3Device::getDeviceVersion() {
1297 ATRACE_CALL();
1298 Mutex::Autolock il(mInterfaceLock);
1299 return mDeviceVersion;
1300}
1301
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001302/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001303 * Methods called by subclasses
1304 */
1305
1306void Camera3Device::notifyStatus(bool idle) {
1307 {
1308 // Need mLock to safely update state and synchronize to current
1309 // state of methods in flight.
1310 Mutex::Autolock l(mLock);
1311 // We can get various system-idle notices from the status tracker
1312 // while starting up. Only care about them if we've actually sent
1313 // in some requests recently.
1314 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1315 return;
1316 }
1317 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1318 idle ? "idle" : "active");
1319 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1320 mRecentStatusUpdates.add(mStatus);
1321 mStatusChanged.signal();
1322
1323 // Skip notifying listener if we're doing some user-transparent
1324 // state changes
1325 if (mPauseStateNotify) return;
1326 }
1327 NotificationListener *listener;
1328 {
1329 Mutex::Autolock l(mOutputLock);
1330 listener = mListener;
1331 }
1332 if (idle && listener != NULL) {
1333 listener->notifyIdle();
1334 }
1335}
1336
1337/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001338 * Camera3Device private methods
1339 */
1340
1341sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1342 const CameraMetadata &request) {
1343 ATRACE_CALL();
1344 status_t res;
1345
1346 sp<CaptureRequest> newRequest = new CaptureRequest;
1347 newRequest->mSettings = request;
1348
1349 camera_metadata_entry_t inputStreams =
1350 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1351 if (inputStreams.count > 0) {
1352 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001353 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001354 CLOGE("Request references unknown input stream %d",
1355 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 return NULL;
1357 }
1358 // Lazy completion of stream configuration (allocation/registration)
1359 // on first use
1360 if (mInputStream->isConfiguring()) {
1361 res = mInputStream->finishConfiguration(mHal3Device);
1362 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001363 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001364 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001365 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001366 return NULL;
1367 }
1368 }
1369
1370 newRequest->mInputStream = mInputStream;
1371 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1372 }
1373
1374 camera_metadata_entry_t streams =
1375 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1376 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001377 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001378 return NULL;
1379 }
1380
1381 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001382 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001383 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001384 CLOGE("Request references unknown stream %d",
1385 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001386 return NULL;
1387 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001388 sp<Camera3OutputStreamInterface> stream =
1389 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001390
1391 // Lazy completion of stream configuration (allocation/registration)
1392 // on first use
1393 if (stream->isConfiguring()) {
1394 res = stream->finishConfiguration(mHal3Device);
1395 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001396 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1397 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001398 return NULL;
1399 }
1400 }
1401
1402 newRequest->mOutputStreams.push(stream);
1403 }
1404 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1405
1406 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001407}
1408
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001409status_t Camera3Device::configureStreamsLocked() {
1410 ATRACE_CALL();
1411 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001412
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001413 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001414 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415 return INVALID_OPERATION;
1416 }
1417
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001418 if (!mNeedConfig) {
1419 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1420 return OK;
1421 }
1422
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001423 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1424 // adding a dummy stream instead.
1425 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1426 if (mOutputStreams.size() == 0) {
1427 addDummyStreamLocked();
1428 } else {
1429 tryRemoveDummyStreamLocked();
1430 }
1431
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001432 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001433 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434
1435 camera3_stream_configuration config;
1436
1437 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1438
1439 Vector<camera3_stream_t*> streams;
1440 streams.setCapacity(config.num_streams);
1441
1442 if (mInputStream != NULL) {
1443 camera3_stream_t *inputStream;
1444 inputStream = mInputStream->startConfiguration();
1445 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001446 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447 return INVALID_OPERATION;
1448 }
1449 streams.add(inputStream);
1450 }
1451
1452 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001453
1454 // Don't configure bidi streams twice, nor add them twice to the list
1455 if (mOutputStreams[i].get() ==
1456 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1457
1458 config.num_streams--;
1459 continue;
1460 }
1461
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001462 camera3_stream_t *outputStream;
1463 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1464 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return INVALID_OPERATION;
1467 }
1468 streams.add(outputStream);
1469 }
1470
1471 config.streams = streams.editArray();
1472
1473 // Do the HAL configuration; will potentially touch stream
1474 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001475 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001476 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001477 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001479 if (res == BAD_VALUE) {
1480 // HAL rejected this set of streams as unsupported, clean up config
1481 // attempt and return to unconfigured state
1482 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1483 res = mInputStream->cancelConfiguration();
1484 if (res != OK) {
1485 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1486 mInputStream->getId(), strerror(-res), res);
1487 return res;
1488 }
1489 }
1490
1491 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1492 sp<Camera3OutputStreamInterface> outputStream =
1493 mOutputStreams.editValueAt(i);
1494 if (outputStream->isConfiguring()) {
1495 res = outputStream->cancelConfiguration();
1496 if (res != OK) {
1497 SET_ERR_L(
1498 "Can't cancel configuring output stream %d: %s (%d)",
1499 outputStream->getId(), strerror(-res), res);
1500 return res;
1501 }
1502 }
1503 }
1504
1505 // Return state to that at start of call, so that future configures
1506 // properly clean things up
1507 mStatus = STATUS_UNCONFIGURED;
1508 mNeedConfig = true;
1509
1510 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1511 return BAD_VALUE;
1512 } else if (res != OK) {
1513 // Some other kind of error from configure_streams - this is not
1514 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001515 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1516 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001517 return res;
1518 }
1519
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001520 // Finish all stream configuration immediately.
1521 // TODO: Try to relax this later back to lazy completion, which should be
1522 // faster
1523
Igor Murashkin073f8572013-05-02 14:59:28 -07001524 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001525 res = mInputStream->finishConfiguration(mHal3Device);
1526 if (res != OK) {
1527 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1528 mInputStream->getId(), strerror(-res), res);
1529 return res;
1530 }
1531 }
1532
1533 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001534 sp<Camera3OutputStreamInterface> outputStream =
1535 mOutputStreams.editValueAt(i);
1536 if (outputStream->isConfiguring()) {
1537 res = outputStream->finishConfiguration(mHal3Device);
1538 if (res != OK) {
1539 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1540 outputStream->getId(), strerror(-res), res);
1541 return res;
1542 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001543 }
1544 }
1545
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001546 // Request thread needs to know to avoid using repeat-last-settings protocol
1547 // across configure_streams() calls
1548 mRequestThread->configurationComplete();
1549
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001550 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001552 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001554 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001555 mStatus = STATUS_CONFIGURED;
1556 } else {
1557 mStatus = STATUS_UNCONFIGURED;
1558 }
1559
1560 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1561
Zhijun He0a210512014-07-24 13:45:15 -07001562 // tear down the deleted streams after configure streams.
1563 mDeletedStreams.clear();
1564
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001565 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001566}
1567
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001568status_t Camera3Device::addDummyStreamLocked() {
1569 ATRACE_CALL();
1570 status_t res;
1571
1572 if (mDummyStreamId != NO_STREAM) {
1573 // Should never be adding a second dummy stream when one is already
1574 // active
1575 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1576 __FUNCTION__, mId);
1577 return INVALID_OPERATION;
1578 }
1579
1580 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1581
1582 sp<Camera3OutputStreamInterface> dummyStream =
1583 new Camera3DummyStream(mNextStreamId);
1584
1585 res = mOutputStreams.add(mNextStreamId, dummyStream);
1586 if (res < 0) {
1587 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1588 return res;
1589 }
1590
1591 mDummyStreamId = mNextStreamId;
1592 mNextStreamId++;
1593
1594 return OK;
1595}
1596
1597status_t Camera3Device::tryRemoveDummyStreamLocked() {
1598 ATRACE_CALL();
1599 status_t res;
1600
1601 if (mDummyStreamId == NO_STREAM) return OK;
1602 if (mOutputStreams.size() == 1) return OK;
1603
1604 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1605
1606 // Ok, have a dummy stream and there's at least one other output stream,
1607 // so remove the dummy
1608
1609 sp<Camera3StreamInterface> deletedStream;
1610 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1611 if (outputStreamIdx == NAME_NOT_FOUND) {
1612 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1613 return INVALID_OPERATION;
1614 }
1615
1616 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1617 mOutputStreams.removeItemsAt(outputStreamIdx);
1618
1619 // Free up the stream endpoint so that it can be used by some other stream
1620 res = deletedStream->disconnect();
1621 if (res != OK) {
1622 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1623 // fall through since we want to still list the stream as deleted.
1624 }
1625 mDeletedStreams.add(deletedStream);
1626 mDummyStreamId = NO_STREAM;
1627
1628 return res;
1629}
1630
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001631void Camera3Device::setErrorState(const char *fmt, ...) {
1632 Mutex::Autolock l(mLock);
1633 va_list args;
1634 va_start(args, fmt);
1635
1636 setErrorStateLockedV(fmt, args);
1637
1638 va_end(args);
1639}
1640
1641void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1642 Mutex::Autolock l(mLock);
1643 setErrorStateLockedV(fmt, args);
1644}
1645
1646void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1647 va_list args;
1648 va_start(args, fmt);
1649
1650 setErrorStateLockedV(fmt, args);
1651
1652 va_end(args);
1653}
1654
1655void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001656 // Print out all error messages to log
1657 String8 errorCause = String8::formatV(fmt, args);
1658 ALOGE("Camera %d: %s", mId, errorCause.string());
1659
1660 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001661 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001662
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001663 mErrorCause = errorCause;
1664
1665 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001666 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001667
1668 // Notify upstream about a device error
1669 if (mListener != NULL) {
1670 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1671 CaptureResultExtras());
1672 }
1673
1674 // Save stack trace. View by dumping it later.
1675 CameraTraces::saveTrace();
1676 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001677}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001678
1679/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001680 * In-flight request management
1681 */
1682
Jianing Weicb0652e2014-03-12 18:29:36 -07001683status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001684 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001685 ATRACE_CALL();
1686 Mutex::Autolock l(mInFlightLock);
1687
1688 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001689 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001690 if (res < 0) return res;
1691
1692 return OK;
1693}
1694
1695/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001696 * Check if all 3A fields are ready, and send off a partial 3A-only result
1697 * to the output frame queue
1698 */
Zhijun He204e3292014-07-14 17:09:23 -07001699bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001700 uint32_t frameNumber,
1701 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001702
1703 // Check if all 3A states are present
1704 // The full list of fields is
1705 // android.control.afMode
1706 // android.control.awbMode
1707 // android.control.aeState
1708 // android.control.awbState
1709 // android.control.afState
1710 // android.control.afTriggerID
1711 // android.control.aePrecaptureID
1712 // TODO: Add android.control.aeMode
1713
1714 bool gotAllStates = true;
1715
1716 uint8_t afMode;
1717 uint8_t awbMode;
1718 uint8_t aeState;
1719 uint8_t afState;
1720 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001721
1722 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1723 &afMode, frameNumber);
1724
1725 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1726 &awbMode, frameNumber);
1727
1728 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1729 &aeState, frameNumber);
1730
1731 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1732 &afState, frameNumber);
1733
1734 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1735 &awbState, frameNumber);
1736
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001737 if (!gotAllStates) return false;
1738
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001739 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001740 "AF state %d, AE state %d, AWB state %d, "
1741 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001742 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001743 afMode, awbMode,
1744 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001745 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001746
1747 // Got all states, so construct a minimal result to send
1748 // In addition to the above fields, this means adding in
1749 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001750 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001751 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001752
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001753 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001754
1755 Mutex::Autolock l(mOutputLock);
1756
Jianing Weicb0652e2014-03-12 18:29:36 -07001757 CaptureResult captureResult;
1758 captureResult.mResultExtras = resultExtras;
1759 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1760 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1761 // but not limited to CameraDeviceBase::getNextResult
1762 CaptureResult& min3AResult =
1763 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001764
Jianing Weicb0652e2014-03-12 18:29:36 -07001765 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1766 // TODO: This is problematic casting. Need to fix CameraMetadata.
1767 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001768 return false;
1769 }
1770
Jianing Weicb0652e2014-03-12 18:29:36 -07001771 int32_t requestId = resultExtras.requestId;
1772 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001773 &requestId, frameNumber)) {
1774 return false;
1775 }
1776
Zhijun He204e3292014-07-14 17:09:23 -07001777 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1778 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1779 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1780 &partialResult, frameNumber)) {
1781 return false;
1782 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001783 }
1784
Jianing Weicb0652e2014-03-12 18:29:36 -07001785 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001786 &afMode, frameNumber)) {
1787 return false;
1788 }
1789
Jianing Weicb0652e2014-03-12 18:29:36 -07001790 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001791 &awbMode, frameNumber)) {
1792 return false;
1793 }
1794
Jianing Weicb0652e2014-03-12 18:29:36 -07001795 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001796 &aeState, frameNumber)) {
1797 return false;
1798 }
1799
Jianing Weicb0652e2014-03-12 18:29:36 -07001800 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001801 &afState, frameNumber)) {
1802 return false;
1803 }
1804
Jianing Weicb0652e2014-03-12 18:29:36 -07001805 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001806 &awbState, frameNumber)) {
1807 return false;
1808 }
1809
Jianing Weicb0652e2014-03-12 18:29:36 -07001810 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001811 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001812 return false;
1813 }
1814
Jianing Weicb0652e2014-03-12 18:29:36 -07001815 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001816 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001817 return false;
1818 }
1819
Zhijun He204e3292014-07-14 17:09:23 -07001820 // We only send the aggregated partial when all 3A related metadata are available
1821 // For both API1 and API2.
1822 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001823 mResultSignal.signal();
1824
1825 return true;
1826}
1827
1828template<typename T>
1829bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001830 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001831 (void) frameNumber;
1832
1833 camera_metadata_ro_entry_t entry;
1834
1835 entry = result.find(tag);
1836 if (entry.count == 0) {
1837 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1838 mId, frameNumber, get_camera_metadata_tag_name(tag));
1839 return false;
1840 }
1841
1842 if (sizeof(T) == sizeof(uint8_t)) {
1843 *value = entry.data.u8[0];
1844 } else if (sizeof(T) == sizeof(int32_t)) {
1845 *value = entry.data.i32[0];
1846 } else {
1847 ALOGE("%s: Unexpected type", __FUNCTION__);
1848 return false;
1849 }
1850 return true;
1851}
1852
1853template<typename T>
1854bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001855 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001856 if (result.update(tag, value, 1) != NO_ERROR) {
1857 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1858 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1859 frameNumber, get_camera_metadata_tag_name(tag));
1860 return false;
1861 }
1862 return true;
1863}
1864
1865/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001866 * Camera HAL device callback methods
1867 */
1868
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001869void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001870 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001871
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001872 status_t res;
1873
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001874 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07001875 if (result->result == NULL && result->num_output_buffers == 0 &&
1876 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001877 SET_ERR("No result data provided by HAL for frame %d",
1878 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001879 return;
1880 }
Zhijun He204e3292014-07-14 17:09:23 -07001881
1882 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
1883 // partial_result to 1 when metadata is included in this result.
1884 if (!mUsePartialResult &&
1885 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
1886 result->result != NULL &&
1887 result->partial_result != 1) {
1888 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
1889 " if partial result is not supported",
1890 frameNumber, result->partial_result);
1891 return;
1892 }
1893
1894 bool isPartialResult = false;
1895 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07001896 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001897 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001898
Jianing Weicb0652e2014-03-12 18:29:36 -07001899 // Get capture timestamp and resultExtras from list of in-flight requests,
1900 // where it was added by the shutter notification for this frame.
1901 // Then update the in-flight status and remove the in-flight entry if
1902 // all result data has been received.
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001903 nsecs_t timestamp = 0;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001904 {
1905 Mutex::Autolock l(mInFlightLock);
1906 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1907 if (idx == NAME_NOT_FOUND) {
1908 SET_ERR("Unknown frame number for capture result: %d",
1909 frameNumber);
1910 return;
1911 }
1912 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Jianing Weicb0652e2014-03-12 18:29:36 -07001913 ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1914 ", burstId = %" PRId32,
1915 __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1916 request.resultExtras.burstId);
Zhijun He204e3292014-07-14 17:09:23 -07001917 // Always update the partial count to the latest one. When framework aggregates adjacent
1918 // partial results into one, the latest partial count will be used.
1919 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001920
1921 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07001922 if (mUsePartialResult && result->result != NULL) {
1923 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1924 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
1925 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
1926 " the range of [1, %d] when metadata is included in the result",
1927 frameNumber, result->partial_result, mNumPartialResults);
1928 return;
1929 }
1930 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07001931 if (isPartialResult) {
1932 request.partialResult.collectedResult.append(result->result);
1933 }
Zhijun He204e3292014-07-14 17:09:23 -07001934 } else {
1935 camera_metadata_ro_entry_t partialResultEntry;
1936 res = find_camera_metadata_ro_entry(result->result,
1937 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1938 if (res != NAME_NOT_FOUND &&
1939 partialResultEntry.count > 0 &&
1940 partialResultEntry.data.u8[0] ==
1941 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1942 // A partial result. Flag this as such, and collect this
1943 // set of metadata into the in-flight entry.
1944 isPartialResult = true;
1945 request.partialResult.collectedResult.append(
1946 result->result);
1947 request.partialResult.collectedResult.erase(
1948 ANDROID_QUIRKS_PARTIAL_RESULT);
1949 }
1950 }
1951
1952 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001953 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07001954 if (!request.partialResult.haveSent3A) {
1955 request.partialResult.haveSent3A =
1956 processPartial3AResult(frameNumber,
1957 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07001958 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001959 }
1960 }
1961 }
1962
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001963 timestamp = request.captureTimestamp;
Jianing Weicb0652e2014-03-12 18:29:36 -07001964 resultExtras = request.resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001965 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07001966
Zhijun He1d1f8462013-10-02 16:29:51 -07001967 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001968 * One of the following must happen before it's legal to call process_capture_result,
1969 * unless partial metadata is being provided:
Zhijun He1d1f8462013-10-02 16:29:51 -07001970 * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1971 * - CAMERA3_MSG_ERROR (expected during flush)
1972 */
Zhijun He204e3292014-07-14 17:09:23 -07001973 if (request.requestStatus == OK && timestamp == 0 && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001974 SET_ERR("Called before shutter notify for frame %d",
1975 frameNumber);
1976 return;
1977 }
1978
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001979 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07001980 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001981 if (request.haveResultMetadata) {
1982 SET_ERR("Called multiple times with metadata for frame %d",
1983 frameNumber);
1984 return;
1985 }
Zhijun He204e3292014-07-14 17:09:23 -07001986 if (mUsePartialResult &&
1987 !request.partialResult.collectedResult.isEmpty()) {
1988 collectedPartialResult.acquire(
1989 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001990 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001991 request.haveResultMetadata = true;
1992 }
1993
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001994 uint32_t numBuffersReturned = result->num_output_buffers;
1995 if (result->input_buffer != NULL) {
1996 if (hasInputBufferInRequest) {
1997 numBuffersReturned += 1;
1998 } else {
1999 ALOGW("%s: Input buffer should be NULL if there is no input"
2000 " buffer sent in the request",
2001 __FUNCTION__);
2002 }
2003 }
2004 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002005 if (request.numBuffersLeft < 0) {
2006 SET_ERR("Too many buffers returned for frame %d",
2007 frameNumber);
2008 return;
2009 }
2010
Zhijun He1b05dfc2013-11-21 12:57:51 -08002011 // Check if everything has arrived for this result (buffers and metadata), remove it from
2012 // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
2013 if ((request.requestStatus != OK) ||
2014 (request.haveResultMetadata && request.numBuffersLeft == 0)) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002015 ATRACE_ASYNC_END("frame capture", frameNumber);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002016 mInFlightMap.removeItemsAt(idx, 1);
2017 }
2018
2019 // Sanity check - if we have too many in-flight frames, something has
2020 // likely gone wrong
2021 if (mInFlightMap.size() > kInFlightWarnLimit) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002022 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002023 }
2024
2025 }
2026
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002027 // Process the result metadata, if provided
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002028 bool gotResult = false;
Zhijun He204e3292014-07-14 17:09:23 -07002029 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002030 Mutex::Autolock l(mOutputLock);
2031
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002032 gotResult = true;
2033
Jianing Wei3c76fa32014-04-21 11:34:34 -07002034 // TODO: need to track errors for tighter bounds on expected frame number
2035 if (frameNumber < mNextResultFrameNumber) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002036 SET_ERR("Out-of-order capture result metadata submitted! "
2037 "(got frame number %d, expecting %d)",
2038 frameNumber, mNextResultFrameNumber);
2039 return;
2040 }
Jianing Wei3c76fa32014-04-21 11:34:34 -07002041 mNextResultFrameNumber = frameNumber + 1;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002042
Jianing Weicb0652e2014-03-12 18:29:36 -07002043 CaptureResult captureResult;
2044 captureResult.mResultExtras = resultExtras;
2045 captureResult.mMetadata = result->result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002046
Jianing Weicb0652e2014-03-12 18:29:36 -07002047 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2048 (int32_t*)&frameNumber, 1) != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002049 SET_ERR("Failed to set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002050 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002051 gotResult = false;
Igor Murashkind2c90692013-04-02 12:32:32 -07002052 } else {
2053 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002054 __FUNCTION__, mId, frameNumber);
Igor Murashkind2c90692013-04-02 12:32:32 -07002055 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002056
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002057 // Append any previous partials to form a complete result
Zhijun He204e3292014-07-14 17:09:23 -07002058 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2059 captureResult.mMetadata.append(collectedPartialResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002060 }
2061
Jianing Weicb0652e2014-03-12 18:29:36 -07002062 captureResult.mMetadata.sort();
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002063
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002064 // Check that there's a timestamp in the result metadata
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002065
2066 camera_metadata_entry entry =
Jianing Weicb0652e2014-03-12 18:29:36 -07002067 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002068 if (entry.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002069 SET_ERR("No timestamp provided by HAL for frame %d!",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002070 frameNumber);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002071 gotResult = false;
Alex Rayfe7e0c62013-05-30 00:12:13 -07002072 } else if (timestamp != entry.data.i64[0]) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002073 SET_ERR("Timestamp mismatch between shutter notify and result"
Colin Crosse5729fa2014-03-21 15:04:25 -07002074 " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002075 frameNumber, timestamp, entry.data.i64[0]);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002076 gotResult = false;
2077 }
2078
2079 if (gotResult) {
2080 // Valid result, insert into queue
Jianing Weicb0652e2014-03-12 18:29:36 -07002081 List<CaptureResult>::iterator queuedResult =
2082 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2083 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2084 ", burstId = %" PRId32, __FUNCTION__,
2085 queuedResult->mResultExtras.requestId,
2086 queuedResult->mResultExtras.frameNumber,
2087 queuedResult->mResultExtras.burstId);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002088 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002089 } // scope for mOutputLock
2090
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002091 // Return completed buffers to their streams with the timestamp
2092
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002093 for (size_t i = 0; i < result->num_output_buffers; i++) {
2094 Camera3Stream *stream =
2095 Camera3Stream::cast(result->output_buffers[i].stream);
2096 res = stream->returnBuffer(result->output_buffers[i], timestamp);
2097 // Note: stream may be deallocated at this point, if this buffer was the
2098 // last reference to it.
2099 if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07002100 ALOGE("Can't return buffer %zu for frame %d to its stream: "
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002101 " %s (%d)", i, frameNumber, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002102 }
2103 }
2104
Zhijun Hef0d962a2014-06-30 10:24:11 -07002105 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002106 if (hasInputBufferInRequest) {
2107 Camera3Stream *stream =
2108 Camera3Stream::cast(result->input_buffer->stream);
2109 res = stream->returnInputBuffer(*(result->input_buffer));
2110 // Note: stream may be deallocated at this point, if this buffer was the
2111 // last reference to it.
2112 if (res != OK) {
2113 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2114 " its stream:%s (%d)", __FUNCTION__,
2115 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002116 }
2117 } else {
2118 ALOGW("%s: Input buffer should be NULL if there is no input"
2119 " buffer sent in the request, skipping input buffer return.",
2120 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002121 }
2122 }
2123
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07002124 // Finally, signal any waiters for new frames
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002125
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002126 if (gotResult) {
Igor Murashkin4345d5b2013-05-17 14:39:53 -07002127 mResultSignal.signal();
2128 }
2129
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002130}
2131
2132void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002133 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002134 NotificationListener *listener;
2135 {
2136 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002137 listener = mListener;
2138 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002139
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002140 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002141 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002142 return;
2143 }
2144
2145 switch (msg->type) {
2146 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002147 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002148 break;
2149 }
2150 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002151 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002152 break;
2153 }
2154 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002155 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002156 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002157 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002158}
2159
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002160void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2161 NotificationListener *listener) {
2162
2163 // Map camera HAL error codes to ICameraDeviceCallback error codes
2164 // Index into this with the HAL error code
2165 static const ICameraDeviceCallbacks::CameraErrorCode
2166 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2167 // 0 = Unused error code
2168 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2169 // 1 = CAMERA3_MSG_ERROR_DEVICE
2170 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2171 // 2 = CAMERA3_MSG_ERROR_REQUEST
2172 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2173 // 3 = CAMERA3_MSG_ERROR_RESULT
2174 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2175 // 4 = CAMERA3_MSG_ERROR_BUFFER
2176 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2177 };
2178
2179 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2180 ((msg.error_code >= 0) &&
2181 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2182 halErrorMap[msg.error_code] :
2183 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2184
2185 int streamId = 0;
2186 if (msg.error_stream != NULL) {
2187 Camera3Stream *stream =
2188 Camera3Stream::cast(msg.error_stream);
2189 streamId = stream->getId();
2190 }
2191 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2192 mId, __FUNCTION__, msg.frame_number,
2193 streamId, msg.error_code);
2194
2195 CaptureResultExtras resultExtras;
2196 switch (errorCode) {
2197 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2198 // SET_ERR calls notifyError
2199 SET_ERR("Camera HAL reported serious device error");
2200 break;
2201 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2202 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2203 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2204 {
2205 Mutex::Autolock l(mInFlightLock);
2206 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2207 if (idx >= 0) {
2208 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2209 r.requestStatus = msg.error_code;
2210 resultExtras = r.resultExtras;
2211 } else {
2212 resultExtras.frameNumber = msg.frame_number;
2213 ALOGE("Camera %d: %s: cannot find in-flight request on "
2214 "frame %" PRId64 " error", mId, __FUNCTION__,
2215 resultExtras.frameNumber);
2216 }
2217 }
2218 if (listener != NULL) {
2219 listener->notifyError(errorCode, resultExtras);
2220 } else {
2221 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2222 }
2223 break;
2224 default:
2225 // SET_ERR calls notifyError
2226 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2227 break;
2228 }
2229}
2230
2231void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2232 NotificationListener *listener) {
2233 ssize_t idx;
2234 // Verify ordering of shutter notifications
2235 {
2236 Mutex::Autolock l(mOutputLock);
2237 // TODO: need to track errors for tighter bounds on expected frame number.
2238 if (msg.frame_number < mNextShutterFrameNumber) {
2239 SET_ERR("Shutter notification out-of-order. Expected "
2240 "notification for frame %d, got frame %d",
2241 mNextShutterFrameNumber, msg.frame_number);
2242 return;
2243 }
2244 mNextShutterFrameNumber = msg.frame_number + 1;
2245 }
2246
2247 CaptureResultExtras resultExtras;
2248
2249 // Set timestamp for the request in the in-flight tracking
2250 // and get the request ID to send upstream
2251 {
2252 Mutex::Autolock l(mInFlightLock);
2253 idx = mInFlightMap.indexOfKey(msg.frame_number);
2254 if (idx >= 0) {
2255 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2256 r.captureTimestamp = msg.timestamp;
2257 resultExtras = r.resultExtras;
2258 }
2259 }
2260 if (idx < 0) {
2261 SET_ERR("Shutter notification for non-existent frame number %d",
2262 msg.frame_number);
2263 return;
2264 }
2265 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2266 mId, __FUNCTION__,
2267 msg.frame_number, resultExtras.requestId, msg.timestamp);
2268 // Call listener, if any
2269 if (listener != NULL) {
2270 listener->notifyShutter(resultExtras, msg.timestamp);
2271 }
2272}
2273
2274
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002275CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002276 ALOGV("%s", __FUNCTION__);
2277
Igor Murashkin1e479c02013-09-06 16:55:14 -07002278 CameraMetadata retVal;
2279
2280 if (mRequestThread != NULL) {
2281 retVal = mRequestThread->getLatestRequest();
2282 }
2283
Igor Murashkin1e479c02013-09-06 16:55:14 -07002284 return retVal;
2285}
2286
Jianing Weicb0652e2014-03-12 18:29:36 -07002287
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002288/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002289 * RequestThread inner class methods
2290 */
2291
2292Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002293 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002294 camera3_device_t *hal3Device) :
2295 Thread(false),
2296 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002297 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002298 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002299 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002300 mReconfigured(false),
2301 mDoPause(false),
2302 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002303 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002304 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002305 mCurrentAfTriggerId(0),
2306 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002307 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002308 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002309}
2310
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002311void Camera3Device::RequestThread::setNotifyCallback(
2312 NotificationListener *listener) {
2313 Mutex::Autolock l(mRequestLock);
2314 mListener = listener;
2315}
2316
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002317void Camera3Device::RequestThread::configurationComplete() {
2318 Mutex::Autolock l(mRequestLock);
2319 mReconfigured = true;
2320}
2321
Jianing Wei90e59c92014-03-12 18:29:36 -07002322status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002323 List<sp<CaptureRequest> > &requests,
2324 /*out*/
2325 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002326 Mutex::Autolock l(mRequestLock);
2327 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2328 ++it) {
2329 mRequestQueue.push_back(*it);
2330 }
2331
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002332 if (lastFrameNumber != NULL) {
2333 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2334 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2335 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2336 *lastFrameNumber);
2337 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002338
Jianing Wei90e59c92014-03-12 18:29:36 -07002339 unpauseForNewRequests();
2340
2341 return OK;
2342}
2343
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002344
2345status_t Camera3Device::RequestThread::queueTrigger(
2346 RequestTrigger trigger[],
2347 size_t count) {
2348
2349 Mutex::Autolock l(mTriggerMutex);
2350 status_t ret;
2351
2352 for (size_t i = 0; i < count; ++i) {
2353 ret = queueTriggerLocked(trigger[i]);
2354
2355 if (ret != OK) {
2356 return ret;
2357 }
2358 }
2359
2360 return OK;
2361}
2362
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002363int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2364 sp<Camera3Device> d = device.promote();
2365 if (d != NULL) return d->mId;
2366 return 0;
2367}
2368
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002369status_t Camera3Device::RequestThread::queueTriggerLocked(
2370 RequestTrigger trigger) {
2371
2372 uint32_t tag = trigger.metadataTag;
2373 ssize_t index = mTriggerMap.indexOfKey(tag);
2374
2375 switch (trigger.getTagType()) {
2376 case TYPE_BYTE:
2377 // fall-through
2378 case TYPE_INT32:
2379 break;
2380 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002381 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2382 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002383 return INVALID_OPERATION;
2384 }
2385
2386 /**
2387 * Collect only the latest trigger, since we only have 1 field
2388 * in the request settings per trigger tag, and can't send more than 1
2389 * trigger per request.
2390 */
2391 if (index != NAME_NOT_FOUND) {
2392 mTriggerMap.editValueAt(index) = trigger;
2393 } else {
2394 mTriggerMap.add(tag, trigger);
2395 }
2396
2397 return OK;
2398}
2399
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002400status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002401 const RequestList &requests,
2402 /*out*/
2403 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002404 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002405 if (lastFrameNumber != NULL) {
2406 *lastFrameNumber = mRepeatingLastFrameNumber;
2407 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002408 mRepeatingRequests.clear();
2409 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2410 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002411
2412 unpauseForNewRequests();
2413
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002414 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002415 return OK;
2416}
2417
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002418bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2419 if (mRepeatingRequests.empty()) {
2420 return false;
2421 }
2422 int32_t requestId = requestIn->mResultExtras.requestId;
2423 const RequestList &repeatRequests = mRepeatingRequests;
2424 // All repeating requests are guaranteed to have same id so only check first quest
2425 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2426 return (firstRequest->mResultExtras.requestId == requestId);
2427}
2428
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002429status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002430 Mutex::Autolock l(mRequestLock);
2431 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002432 if (lastFrameNumber != NULL) {
2433 *lastFrameNumber = mRepeatingLastFrameNumber;
2434 }
2435 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002436 return OK;
2437}
2438
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002439status_t Camera3Device::RequestThread::clear(
2440 NotificationListener *listener,
2441 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002442 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002443 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002444
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002445 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002446
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002447 // Send errors for all requests pending in the request queue, including
2448 // pending repeating requests
2449 if (listener != NULL) {
2450 for (RequestList::iterator it = mRequestQueue.begin();
2451 it != mRequestQueue.end(); ++it) {
2452 // Set the frame number this request would have had, if it
2453 // had been submitted; this frame number will not be reused.
2454 // The requestId and burstId fields were set when the request was
2455 // submitted originally (in convertMetadataListToRequestListLocked)
2456 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2457 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2458 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002459 }
2460 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002461 mRequestQueue.clear();
2462 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002463 if (lastFrameNumber != NULL) {
2464 *lastFrameNumber = mRepeatingLastFrameNumber;
2465 }
2466 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002467 return OK;
2468}
2469
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002470void Camera3Device::RequestThread::setPaused(bool paused) {
2471 Mutex::Autolock l(mPauseLock);
2472 mDoPause = paused;
2473 mDoPauseSignal.signal();
2474}
2475
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002476status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2477 int32_t requestId, nsecs_t timeout) {
2478 Mutex::Autolock l(mLatestRequestMutex);
2479 status_t res;
2480 while (mLatestRequestId != requestId) {
2481 nsecs_t startTime = systemTime();
2482
2483 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2484 if (res != OK) return res;
2485
2486 timeout -= (systemTime() - startTime);
2487 }
2488
2489 return OK;
2490}
2491
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002492void Camera3Device::RequestThread::requestExit() {
2493 // Call parent to set up shutdown
2494 Thread::requestExit();
2495 // The exit from any possible waits
2496 mDoPauseSignal.signal();
2497 mRequestSignal.signal();
2498}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002499
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002500bool Camera3Device::RequestThread::threadLoop() {
2501
2502 status_t res;
2503
2504 // Handle paused state.
2505 if (waitIfPaused()) {
2506 return true;
2507 }
2508
2509 // Get work to do
2510
2511 sp<CaptureRequest> nextRequest = waitForNextRequest();
2512 if (nextRequest == NULL) {
2513 return true;
2514 }
2515
2516 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002517 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002518 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002519 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002520
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002521 // Get the request ID, if any
2522 int requestId;
2523 camera_metadata_entry_t requestIdEntry =
2524 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2525 if (requestIdEntry.count > 0) {
2526 requestId = requestIdEntry.data.i32[0];
2527 } else {
2528 ALOGW("%s: Did not have android.request.id set in the request",
2529 __FUNCTION__);
2530 requestId = NAME_NOT_FOUND;
2531 }
2532
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002533 // Insert any queued triggers (before metadata is locked)
2534 int32_t triggerCount;
2535 res = insertTriggers(nextRequest);
2536 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002537 SET_ERR("RequestThread: Unable to insert triggers "
2538 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002539 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002540 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2541 return false;
2542 }
2543 triggerCount = res;
2544
2545 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2546
2547 // If the request is the same as last, or we had triggers last time
2548 if (mPrevRequest != nextRequest || triggersMixedIn) {
2549 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002550 * HAL workaround:
2551 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2552 */
2553 res = addDummyTriggerIds(nextRequest);
2554 if (res != OK) {
2555 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2556 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002557 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002558 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2559 return false;
2560 }
2561
2562 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002563 * The request should be presorted so accesses in HAL
2564 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2565 */
2566 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002567 request.settings = nextRequest->mSettings.getAndLock();
2568 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002569 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2570
2571 IF_ALOGV() {
2572 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2573 find_camera_metadata_ro_entry(
2574 request.settings,
2575 ANDROID_CONTROL_AF_TRIGGER,
2576 &e
2577 );
2578 if (e.count > 0) {
2579 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2580 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002581 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002582 e.data.u8[0]);
2583 }
2584 }
2585 } else {
2586 // leave request.settings NULL to indicate 'reuse latest given'
2587 ALOGVV("%s: Request settings are REUSED",
2588 __FUNCTION__);
2589 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002590
2591 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002592 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002593
2594 // Fill in buffers
2595
2596 if (nextRequest->mInputStream != NULL) {
2597 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002598 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002599 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002600 // Can't get input buffer from gralloc queue - this could be due to
2601 // disconnected queue or other producer misbehavior, so not a fatal
2602 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002603 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002604 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002605 Mutex::Autolock l(mRequestLock);
2606 if (mListener != NULL) {
2607 mListener->notifyError(
2608 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2609 nextRequest->mResultExtras);
2610 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002611 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2612 return true;
2613 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002614 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002615 } else {
2616 request.input_buffer = NULL;
2617 }
2618
2619 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2620 nextRequest->mOutputStreams.size());
2621 request.output_buffers = outputBuffers.array();
2622 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2623 res = nextRequest->mOutputStreams.editItemAt(i)->
2624 getBuffer(&outputBuffers.editItemAt(i));
2625 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002626 // Can't get output buffer from gralloc queue - this could be due to
2627 // abandoned queue or other consumer misbehavior, so not a fatal
2628 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002629 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2630 " %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 }
2640 request.num_output_buffers++;
2641 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002642 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002643
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002644 // Log request in the in-flight queue
2645 sp<Camera3Device> parent = mParent.promote();
2646 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002647 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002648 CLOGE("RequestThread: Parent is gone");
2649 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2650 return false;
2651 }
2652
Jianing Weicb0652e2014-03-12 18:29:36 -07002653 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002654 totalNumBuffers, nextRequest->mResultExtras,
2655 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002656 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2657 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002658 __FUNCTION__,
2659 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2660 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002661 if (res != OK) {
2662 SET_ERR("RequestThread: Unable to register new in-flight request:"
2663 " %s (%d)", strerror(-res), res);
2664 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2665 return false;
2666 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002667
Zhijun Hecc27e112013-10-03 16:12:43 -07002668 // Inform waitUntilRequestProcessed thread of a new request ID
2669 {
2670 Mutex::Autolock al(mLatestRequestMutex);
2671
2672 mLatestRequestId = requestId;
2673 mLatestRequestSignal.signal();
2674 }
2675
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002676 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002677 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2678 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002679 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002680 ATRACE_END();
2681
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002682 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002683 // Should only get a failure here for malformed requests or device-level
2684 // errors, so consider all errors fatal. Bad metadata failures should
2685 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002686 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 " device: %s (%d)", request.frame_number, strerror(-res), res);
2688 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2689 return false;
2690 }
2691
Igor Murashkin1e479c02013-09-06 16:55:14 -07002692 // Update the latest request sent to HAL
2693 if (request.settings != NULL) { // Don't update them if they were unchanged
2694 Mutex::Autolock al(mLatestRequestMutex);
2695
2696 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2697 mLatestRequest.acquire(cloned);
2698 }
2699
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002700 if (request.settings != NULL) {
2701 nextRequest->mSettings.unlock(request.settings);
2702 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002703
2704 // Remove any previously queued triggers (after unlock)
2705 res = removeTriggers(mPrevRequest);
2706 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002707 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002708 "(capture request %d, HAL device: %s (%d)",
2709 request.frame_number, strerror(-res), res);
2710 return false;
2711 }
2712 mPrevTriggers = triggerCount;
2713
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002714 return true;
2715}
2716
Igor Murashkin1e479c02013-09-06 16:55:14 -07002717CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2718 Mutex::Autolock al(mLatestRequestMutex);
2719
2720 ALOGV("RequestThread::%s", __FUNCTION__);
2721
2722 return mLatestRequest;
2723}
2724
Jianing Weicb0652e2014-03-12 18:29:36 -07002725
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002726void Camera3Device::RequestThread::cleanUpFailedRequest(
2727 camera3_capture_request_t &request,
2728 sp<CaptureRequest> &nextRequest,
2729 Vector<camera3_stream_buffer_t> &outputBuffers) {
2730
2731 if (request.settings != NULL) {
2732 nextRequest->mSettings.unlock(request.settings);
2733 }
2734 if (request.input_buffer != NULL) {
2735 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002736 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002737 }
2738 for (size_t i = 0; i < request.num_output_buffers; i++) {
2739 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2740 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2741 outputBuffers[i], 0);
2742 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002743}
2744
2745sp<Camera3Device::CaptureRequest>
2746 Camera3Device::RequestThread::waitForNextRequest() {
2747 status_t res;
2748 sp<CaptureRequest> nextRequest;
2749
2750 // Optimized a bit for the simple steady-state case (single repeating
2751 // request), to avoid putting that request in the queue temporarily.
2752 Mutex::Autolock l(mRequestLock);
2753
2754 while (mRequestQueue.empty()) {
2755 if (!mRepeatingRequests.empty()) {
2756 // Always atomically enqueue all requests in a repeating request
2757 // list. Guarantees a complete in-sequence set of captures to
2758 // application.
2759 const RequestList &requests = mRepeatingRequests;
2760 RequestList::const_iterator firstRequest =
2761 requests.begin();
2762 nextRequest = *firstRequest;
2763 mRequestQueue.insert(mRequestQueue.end(),
2764 ++firstRequest,
2765 requests.end());
2766 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002767
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002768 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002769
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002770 break;
2771 }
2772
2773 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2774
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002775 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2776 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002777 Mutex::Autolock pl(mPauseLock);
2778 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002779 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002780 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002781 // Let the tracker know
2782 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2783 if (statusTracker != 0) {
2784 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2785 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002786 }
2787 // Stop waiting for now and let thread management happen
2788 return NULL;
2789 }
2790 }
2791
2792 if (nextRequest == NULL) {
2793 // Don't have a repeating request already in hand, so queue
2794 // must have an entry now.
2795 RequestList::iterator firstRequest =
2796 mRequestQueue.begin();
2797 nextRequest = *firstRequest;
2798 mRequestQueue.erase(firstRequest);
2799 }
2800
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002801 // In case we've been unpaused by setPaused clearing mDoPause, need to
2802 // update internal pause state (capture/setRepeatingRequest unpause
2803 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002804 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002805 if (mPaused) {
2806 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2807 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2808 if (statusTracker != 0) {
2809 statusTracker->markComponentActive(mStatusId);
2810 }
2811 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002812 mPaused = false;
2813
2814 // Check if we've reconfigured since last time, and reset the preview
2815 // request if so. Can't use 'NULL request == repeat' across configure calls.
2816 if (mReconfigured) {
2817 mPrevRequest.clear();
2818 mReconfigured = false;
2819 }
2820
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002821 if (nextRequest != NULL) {
2822 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002823 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2824 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002825 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002826 return nextRequest;
2827}
2828
2829bool Camera3Device::RequestThread::waitIfPaused() {
2830 status_t res;
2831 Mutex::Autolock l(mPauseLock);
2832 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002833 if (mPaused == false) {
2834 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002835 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2836 // Let the tracker know
2837 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2838 if (statusTracker != 0) {
2839 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2840 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002841 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002842
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002843 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002844 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002845 return true;
2846 }
2847 }
2848 // We don't set mPaused to false here, because waitForNextRequest needs
2849 // to further manage the paused state in case of starvation.
2850 return false;
2851}
2852
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002853void Camera3Device::RequestThread::unpauseForNewRequests() {
2854 // With work to do, mark thread as unpaused.
2855 // If paused by request (setPaused), don't resume, to avoid
2856 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002857 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002858 Mutex::Autolock p(mPauseLock);
2859 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002860 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2861 if (mPaused) {
2862 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2863 if (statusTracker != 0) {
2864 statusTracker->markComponentActive(mStatusId);
2865 }
2866 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002867 mPaused = false;
2868 }
2869}
2870
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002871void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2872 sp<Camera3Device> parent = mParent.promote();
2873 if (parent != NULL) {
2874 va_list args;
2875 va_start(args, fmt);
2876
2877 parent->setErrorStateV(fmt, args);
2878
2879 va_end(args);
2880 }
2881}
2882
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002883status_t Camera3Device::RequestThread::insertTriggers(
2884 const sp<CaptureRequest> &request) {
2885
2886 Mutex::Autolock al(mTriggerMutex);
2887
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002888 sp<Camera3Device> parent = mParent.promote();
2889 if (parent == NULL) {
2890 CLOGE("RequestThread: Parent is gone");
2891 return DEAD_OBJECT;
2892 }
2893
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002894 CameraMetadata &metadata = request->mSettings;
2895 size_t count = mTriggerMap.size();
2896
2897 for (size_t i = 0; i < count; ++i) {
2898 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002899 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002900
2901 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2902 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2903 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002904 if (isAeTrigger) {
2905 request->mResultExtras.precaptureTriggerId = triggerId;
2906 mCurrentPreCaptureTriggerId = triggerId;
2907 } else {
2908 request->mResultExtras.afTriggerId = triggerId;
2909 mCurrentAfTriggerId = triggerId;
2910 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002911 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2912 continue; // Trigger ID tag is deprecated since device HAL 3.2
2913 }
2914 }
2915
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002916 camera_metadata_entry entry = metadata.find(tag);
2917
2918 if (entry.count > 0) {
2919 /**
2920 * Already has an entry for this trigger in the request.
2921 * Rewrite it with our requested trigger value.
2922 */
2923 RequestTrigger oldTrigger = trigger;
2924
2925 oldTrigger.entryValue = entry.data.u8[0];
2926
2927 mTriggerReplacedMap.add(tag, oldTrigger);
2928 } else {
2929 /**
2930 * More typical, no trigger entry, so we just add it
2931 */
2932 mTriggerRemovedMap.add(tag, trigger);
2933 }
2934
2935 status_t res;
2936
2937 switch (trigger.getTagType()) {
2938 case TYPE_BYTE: {
2939 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2940 res = metadata.update(tag,
2941 &entryValue,
2942 /*count*/1);
2943 break;
2944 }
2945 case TYPE_INT32:
2946 res = metadata.update(tag,
2947 &trigger.entryValue,
2948 /*count*/1);
2949 break;
2950 default:
2951 ALOGE("%s: Type not supported: 0x%x",
2952 __FUNCTION__,
2953 trigger.getTagType());
2954 return INVALID_OPERATION;
2955 }
2956
2957 if (res != OK) {
2958 ALOGE("%s: Failed to update request metadata with trigger tag %s"
2959 ", value %d", __FUNCTION__, trigger.getTagName(),
2960 trigger.entryValue);
2961 return res;
2962 }
2963
2964 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2965 trigger.getTagName(),
2966 trigger.entryValue);
2967 }
2968
2969 mTriggerMap.clear();
2970
2971 return count;
2972}
2973
2974status_t Camera3Device::RequestThread::removeTriggers(
2975 const sp<CaptureRequest> &request) {
2976 Mutex::Autolock al(mTriggerMutex);
2977
2978 CameraMetadata &metadata = request->mSettings;
2979
2980 /**
2981 * Replace all old entries with their old values.
2982 */
2983 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2984 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2985
2986 status_t res;
2987
2988 uint32_t tag = trigger.metadataTag;
2989 switch (trigger.getTagType()) {
2990 case TYPE_BYTE: {
2991 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2992 res = metadata.update(tag,
2993 &entryValue,
2994 /*count*/1);
2995 break;
2996 }
2997 case TYPE_INT32:
2998 res = metadata.update(tag,
2999 &trigger.entryValue,
3000 /*count*/1);
3001 break;
3002 default:
3003 ALOGE("%s: Type not supported: 0x%x",
3004 __FUNCTION__,
3005 trigger.getTagType());
3006 return INVALID_OPERATION;
3007 }
3008
3009 if (res != OK) {
3010 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3011 ", trigger value %d", __FUNCTION__,
3012 trigger.getTagName(), trigger.entryValue);
3013 return res;
3014 }
3015 }
3016 mTriggerReplacedMap.clear();
3017
3018 /**
3019 * Remove all new entries.
3020 */
3021 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3022 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3023 status_t res = metadata.erase(trigger.metadataTag);
3024
3025 if (res != OK) {
3026 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3027 ", trigger value %d", __FUNCTION__,
3028 trigger.getTagName(), trigger.entryValue);
3029 return res;
3030 }
3031 }
3032 mTriggerRemovedMap.clear();
3033
3034 return OK;
3035}
3036
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003037status_t Camera3Device::RequestThread::addDummyTriggerIds(
3038 const sp<CaptureRequest> &request) {
3039 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3040 static const int32_t dummyTriggerId = 1;
3041 status_t res;
3042
3043 CameraMetadata &metadata = request->mSettings;
3044
3045 // If AF trigger is active, insert a dummy AF trigger ID if none already
3046 // exists
3047 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3048 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3049 if (afTrigger.count > 0 &&
3050 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3051 afId.count == 0) {
3052 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3053 if (res != OK) return res;
3054 }
3055
3056 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3057 // if none already exists
3058 camera_metadata_entry pcTrigger =
3059 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3060 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3061 if (pcTrigger.count > 0 &&
3062 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3063 pcId.count == 0) {
3064 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3065 &dummyTriggerId, 1);
3066 if (res != OK) return res;
3067 }
3068
3069 return OK;
3070}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003071
3072
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003073/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003074 * Static callback forwarding methods from HAL to instance
3075 */
3076
3077void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3078 const camera3_capture_result *result) {
3079 Camera3Device *d =
3080 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3081 d->processCaptureResult(result);
3082}
3083
3084void Camera3Device::sNotify(const camera3_callback_ops *cb,
3085 const camera3_notify_msg *msg) {
3086 Camera3Device *d =
3087 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3088 d->notify(msg);
3089}
3090
3091}; // namespace android