blob: bba39054895d9179e44b16588d6447c0e6d80753 [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),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800430 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700431 r.numBuffersLeft);
432 }
433 }
434 write(fd, lines.string(), lines.size());
435
Igor Murashkin1e479c02013-09-06 16:55:14 -0700436 {
437 lines = String8(" Last request sent:\n");
438 write(fd, lines.string(), lines.size());
439
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700440 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700441 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
442 }
443
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800444 if (mHal3Device != NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700445 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446 write(fd, lines.string(), lines.size());
447 mHal3Device->ops->dump(mHal3Device, fd);
448 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800449
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 if (gotLock) mLock.unlock();
451 if (gotInterfaceLock) mInterfaceLock.unlock();
452
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800453 return OK;
454}
455
456const CameraMetadata& Camera3Device::info() const {
457 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800458 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
459 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700460 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800461 mStatus == STATUS_ERROR ?
462 "when in error state" : "before init");
463 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800464 return mDeviceInfo;
465}
466
Jianing Wei90e59c92014-03-12 18:29:36 -0700467status_t Camera3Device::checkStatusOkToCaptureLocked() {
468 switch (mStatus) {
469 case STATUS_ERROR:
470 CLOGE("Device has encountered a serious error");
471 return INVALID_OPERATION;
472 case STATUS_UNINITIALIZED:
473 CLOGE("Device not initialized");
474 return INVALID_OPERATION;
475 case STATUS_UNCONFIGURED:
476 case STATUS_CONFIGURED:
477 case STATUS_ACTIVE:
478 // OK
479 break;
480 default:
481 SET_ERR_L("Unexpected status: %d", mStatus);
482 return INVALID_OPERATION;
483 }
484 return OK;
485}
486
487status_t Camera3Device::convertMetadataListToRequestListLocked(
488 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
489 if (requestList == NULL) {
490 CLOGE("requestList cannot be NULL.");
491 return BAD_VALUE;
492 }
493
Jianing Weicb0652e2014-03-12 18:29:36 -0700494 int32_t burstId = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700495 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
496 it != metadataList.end(); ++it) {
497 sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
498 if (newRequest == 0) {
499 CLOGE("Can't create capture request");
500 return BAD_VALUE;
501 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700502
503 // Setup burst Id and request Id
504 newRequest->mResultExtras.burstId = burstId++;
505 if (it->exists(ANDROID_REQUEST_ID)) {
506 if (it->find(ANDROID_REQUEST_ID).count == 0) {
507 CLOGE("RequestID entry exists; but must not be empty in metadata");
508 return BAD_VALUE;
509 }
510 newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
511 } else {
512 CLOGE("RequestID does not exist in metadata");
513 return BAD_VALUE;
514 }
515
Jianing Wei90e59c92014-03-12 18:29:36 -0700516 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700517
518 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700519 }
520 return OK;
521}
522
Jianing Weicb0652e2014-03-12 18:29:36 -0700523status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800524 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800525
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700526 List<const CameraMetadata> requests;
527 requests.push_back(request);
528 return captureList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800529}
530
Jianing Wei90e59c92014-03-12 18:29:36 -0700531status_t Camera3Device::submitRequestsHelper(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700532 const List<const CameraMetadata> &requests, bool repeating,
533 /*out*/
534 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700535 ATRACE_CALL();
536 Mutex::Autolock il(mInterfaceLock);
537 Mutex::Autolock l(mLock);
538
539 status_t res = checkStatusOkToCaptureLocked();
540 if (res != OK) {
541 // error logged by previous call
542 return res;
543 }
544
545 RequestList requestList;
546
547 res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
548 if (res != OK) {
549 // error logged by previous call
550 return res;
551 }
552
553 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700554 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700555 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700556 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700557 }
558
559 if (res == OK) {
560 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
561 if (res != OK) {
562 SET_ERR_L("Can't transition to active in %f seconds!",
563 kActiveTimeout/1e9);
564 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700565 ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
566 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700567 } else {
568 CLOGE("Cannot queue request. Impossible.");
569 return BAD_VALUE;
570 }
571
572 return res;
573}
574
Jianing Weicb0652e2014-03-12 18:29:36 -0700575status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
576 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700577 ATRACE_CALL();
578
Jianing Weicb0652e2014-03-12 18:29:36 -0700579 return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700580}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800581
Jianing Weicb0652e2014-03-12 18:29:36 -0700582status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
583 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800584 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800585
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700586 List<const CameraMetadata> requests;
587 requests.push_back(request);
588 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800589}
590
Jianing Weicb0652e2014-03-12 18:29:36 -0700591status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
592 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700593 ATRACE_CALL();
594
Jianing Weicb0652e2014-03-12 18:29:36 -0700595 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700596}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800597
598sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
599 const CameraMetadata &request) {
600 status_t res;
601
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700602 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800603 res = configureStreamsLocked();
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700604 // Stream configuration failed due to unsupported configuration.
605 // Device back to unconfigured state. Client might try other configuraitons
606 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
607 CLOGE("No streams configured");
608 return NULL;
609 }
610 // Stream configuration failed for other reason. Fatal.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800611 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700612 SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800613 return NULL;
614 }
Yin-Chia Yeh3ea3fcd2014-09-05 14:14:44 -0700615 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700616 if (mStatus == STATUS_UNCONFIGURED) {
617 CLOGE("No streams configured");
618 return NULL;
619 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800620 }
621
622 sp<CaptureRequest> newRequest = createCaptureRequest(request);
623 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800624}
625
Jianing Weicb0652e2014-03-12 18:29:36 -0700626status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800627 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700628 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800629 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800630
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800631 switch (mStatus) {
632 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700633 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800634 return INVALID_OPERATION;
635 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700636 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800637 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700638 case STATUS_UNCONFIGURED:
639 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800640 case STATUS_ACTIVE:
641 // OK
642 break;
643 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700644 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800645 return INVALID_OPERATION;
646 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700647 ALOGV("Camera %d: Clearing repeating request", mId);
Jianing Weicb0652e2014-03-12 18:29:36 -0700648
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700649 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800650}
651
652status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
653 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700654 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800655
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700656 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800657}
658
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700659status_t Camera3Device::createInputStream(
660 uint32_t width, uint32_t height, int format, int *id) {
661 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700662 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700663 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700664 ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
665 mId, mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700666
667 status_t res;
668 bool wasActive = false;
669
670 switch (mStatus) {
671 case STATUS_ERROR:
672 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
673 return INVALID_OPERATION;
674 case STATUS_UNINITIALIZED:
675 ALOGE("%s: Device not initialized", __FUNCTION__);
676 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700677 case STATUS_UNCONFIGURED:
678 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700679 // OK
680 break;
681 case STATUS_ACTIVE:
682 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700683 res = internalPauseAndWaitLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700684 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700685 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700686 return res;
687 }
688 wasActive = true;
689 break;
690 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700692 return INVALID_OPERATION;
693 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700694 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700695
696 if (mInputStream != 0) {
697 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
698 return INVALID_OPERATION;
699 }
700
701 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
702 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700703 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700704
705 mInputStream = newStream;
706
707 *id = mNextStreamId++;
708
709 // Continue captures if active at start
710 if (wasActive) {
711 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
712 res = configureStreamsLocked();
713 if (res != OK) {
714 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
715 __FUNCTION__, mNextStreamId, strerror(-res), res);
716 return res;
717 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700718 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700719 }
720
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700721 ALOGV("Camera %d: Created input stream", mId);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700722 return OK;
723}
724
Igor Murashkin2fba5842013-04-22 14:03:54 -0700725
726status_t Camera3Device::createZslStream(
727 uint32_t width, uint32_t height,
728 int depth,
729 /*out*/
730 int *id,
731 sp<Camera3ZslStream>* zslStream) {
732 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700733 Mutex::Autolock il(mInterfaceLock);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700734 Mutex::Autolock l(mLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700735 ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
736 mId, mNextStreamId, width, height, depth);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700737
738 status_t res;
739 bool wasActive = false;
740
741 switch (mStatus) {
742 case STATUS_ERROR:
743 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
744 return INVALID_OPERATION;
745 case STATUS_UNINITIALIZED:
746 ALOGE("%s: Device not initialized", __FUNCTION__);
747 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748 case STATUS_UNCONFIGURED:
749 case STATUS_CONFIGURED:
Igor Murashkin2fba5842013-04-22 14:03:54 -0700750 // OK
751 break;
752 case STATUS_ACTIVE:
753 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700754 res = internalPauseAndWaitLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700755 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700756 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin2fba5842013-04-22 14:03:54 -0700757 return res;
758 }
759 wasActive = true;
760 break;
761 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700762 SET_ERR_L("Unexpected status: %d", mStatus);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700763 return INVALID_OPERATION;
764 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700765 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700766
767 if (mInputStream != 0) {
768 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
769 return INVALID_OPERATION;
770 }
771
772 sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
773 width, height, depth);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700774 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700775
776 res = mOutputStreams.add(mNextStreamId, newStream);
777 if (res < 0) {
778 ALOGE("%s: Can't add new stream to set: %s (%d)",
779 __FUNCTION__, strerror(-res), res);
780 return res;
781 }
782 mInputStream = newStream;
783
Yuvraj Pasie5e3d082014-04-15 18:37:45 +0530784 mNeedConfig = true;
785
Igor Murashkin2fba5842013-04-22 14:03:54 -0700786 *id = mNextStreamId++;
787 *zslStream = newStream;
788
789 // Continue captures if active at start
790 if (wasActive) {
791 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
792 res = configureStreamsLocked();
793 if (res != OK) {
794 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
795 __FUNCTION__, mNextStreamId, strerror(-res), res);
796 return res;
797 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700798 internalResumeLocked();
Igor Murashkin2fba5842013-04-22 14:03:54 -0700799 }
800
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700801 ALOGV("Camera %d: Created ZSL stream", mId);
Igor Murashkin2fba5842013-04-22 14:03:54 -0700802 return OK;
803}
804
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800805status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -0700806 uint32_t width, uint32_t height, int format, int *id) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700808 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800809 Mutex::Autolock l(mLock);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700810 ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
811 mId, mNextStreamId, width, height, format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800812
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 status_t res;
814 bool wasActive = false;
815
816 switch (mStatus) {
817 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700818 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800819 return INVALID_OPERATION;
820 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700821 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800822 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700823 case STATUS_UNCONFIGURED:
824 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800825 // OK
826 break;
827 case STATUS_ACTIVE:
828 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700829 res = internalPauseAndWaitLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800830 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700831 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800832 return res;
833 }
834 wasActive = true;
835 break;
836 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700837 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800838 return INVALID_OPERATION;
839 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700840 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841
842 sp<Camera3OutputStream> newStream;
843 if (format == HAL_PIXEL_FORMAT_BLOB) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700844 ssize_t jpegBufferSize = getJpegBufferSize(width, height);
Zhijun He28c9b6f2014-08-08 12:00:47 -0700845 if (jpegBufferSize <= 0) {
Zhijun Hef7da0962014-04-24 13:27:56 -0700846 SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
847 return BAD_VALUE;
848 }
849
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800850 newStream = new Camera3OutputStream(mNextStreamId, consumer,
Zhijun Hef7da0962014-04-24 13:27:56 -0700851 width, height, jpegBufferSize, format);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800852 } else {
853 newStream = new Camera3OutputStream(mNextStreamId, consumer,
854 width, height, format);
855 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700856 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800857
858 res = mOutputStreams.add(mNextStreamId, newStream);
859 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700860 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800861 return res;
862 }
863
864 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700865 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800866
867 // Continue captures if active at start
868 if (wasActive) {
869 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
870 res = configureStreamsLocked();
871 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700872 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
873 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800874 return res;
875 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700876 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800877 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700878 ALOGV("Camera %d: Created new stream", mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800879 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800880}
881
882status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
883 ATRACE_CALL();
884 (void)outputId; (void)id;
885
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700886 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800887 return INVALID_OPERATION;
888}
889
890
891status_t Camera3Device::getStreamInfo(int id,
892 uint32_t *width, uint32_t *height, uint32_t *format) {
893 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700894 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800895 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800896
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800897 switch (mStatus) {
898 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700899 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800900 return INVALID_OPERATION;
901 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700902 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800903 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700904 case STATUS_UNCONFIGURED:
905 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800906 case STATUS_ACTIVE:
907 // OK
908 break;
909 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700910 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800911 return INVALID_OPERATION;
912 }
913
914 ssize_t idx = mOutputStreams.indexOfKey(id);
915 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700916 CLOGE("Stream %d is unknown", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800917 return idx;
918 }
919
920 if (width) *width = mOutputStreams[idx]->getWidth();
921 if (height) *height = mOutputStreams[idx]->getHeight();
922 if (format) *format = mOutputStreams[idx]->getFormat();
923
924 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800925}
926
927status_t Camera3Device::setStreamTransform(int id,
928 int transform) {
929 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700930 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800931 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800932
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800933 switch (mStatus) {
934 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700935 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800936 return INVALID_OPERATION;
937 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700938 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800939 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700940 case STATUS_UNCONFIGURED:
941 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800942 case STATUS_ACTIVE:
943 // OK
944 break;
945 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700946 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800947 return INVALID_OPERATION;
948 }
949
950 ssize_t idx = mOutputStreams.indexOfKey(id);
951 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700952 CLOGE("Stream %d does not exist",
953 id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800954 return BAD_VALUE;
955 }
956
957 return mOutputStreams.editValueAt(idx)->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800958}
959
960status_t Camera3Device::deleteStream(int id) {
961 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800963 Mutex::Autolock l(mLock);
964 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800965
Igor Murashkine2172be2013-05-28 15:31:39 -0700966 ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
967
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800968 // CameraDevice semantics require device to already be idle before
969 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700970 if (mStatus == STATUS_ACTIVE) {
Igor Murashkin52827132013-05-13 14:53:44 -0700971 ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
972 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800973 }
974
Igor Murashkin2fba5842013-04-22 14:03:54 -0700975 sp<Camera3StreamInterface> deletedStream;
Zhijun He5f446352014-01-22 09:49:33 -0800976 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800977 if (mInputStream != NULL && id == mInputStream->getId()) {
978 deletedStream = mInputStream;
979 mInputStream.clear();
980 } else {
Zhijun He5f446352014-01-22 09:49:33 -0800981 if (outputStreamIdx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700982 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800983 return BAD_VALUE;
984 }
Zhijun He5f446352014-01-22 09:49:33 -0800985 }
986
987 // Delete output stream or the output part of a bi-directional stream.
988 if (outputStreamIdx != NAME_NOT_FOUND) {
989 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800990 mOutputStreams.removeItem(id);
991 }
992
993 // Free up the stream endpoint so that it can be used by some other stream
994 res = deletedStream->disconnect();
995 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700996 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800997 // fall through since we want to still list the stream as deleted.
998 }
999 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001000 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001001
1002 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001003}
1004
1005status_t Camera3Device::deleteReprocessStream(int id) {
1006 ATRACE_CALL();
1007 (void)id;
1008
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001009 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001010 return INVALID_OPERATION;
1011}
1012
Igor Murashkine2d167e2014-08-19 16:19:59 -07001013status_t Camera3Device::configureStreams() {
1014 ATRACE_CALL();
1015 ALOGV("%s: E", __FUNCTION__);
1016
1017 Mutex::Autolock il(mInterfaceLock);
1018 Mutex::Autolock l(mLock);
1019
1020 return configureStreamsLocked();
1021}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001022
1023status_t Camera3Device::createDefaultRequest(int templateId,
1024 CameraMetadata *request) {
1025 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001026 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001027 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001028 Mutex::Autolock l(mLock);
1029
1030 switch (mStatus) {
1031 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001032 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001033 return INVALID_OPERATION;
1034 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001035 CLOGE("Device is not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001036 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001037 case STATUS_UNCONFIGURED:
1038 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001039 case STATUS_ACTIVE:
1040 // OK
1041 break;
1042 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001043 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001044 return INVALID_OPERATION;
1045 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001046
Zhijun Hea1530f12014-09-14 12:44:20 -07001047 if (!mRequestTemplateCache[templateId].isEmpty()) {
1048 *request = mRequestTemplateCache[templateId];
1049 return OK;
1050 }
1051
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001052 const camera_metadata_t *rawRequest;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001053 ATRACE_BEGIN("camera3->construct_default_request_settings");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001054 rawRequest = mHal3Device->ops->construct_default_request_settings(
1055 mHal3Device, templateId);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001056 ATRACE_END();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001057 if (rawRequest == NULL) {
1058 SET_ERR_L("HAL is unable to construct default settings for template %d",
1059 templateId);
1060 return DEAD_OBJECT;
1061 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001062 *request = rawRequest;
Zhijun Hea1530f12014-09-14 12:44:20 -07001063 mRequestTemplateCache[templateId] = rawRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001064
1065 return OK;
1066}
1067
1068status_t Camera3Device::waitUntilDrained() {
1069 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001070 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001071 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001072
Zhijun He69a37482014-03-23 18:44:49 -07001073 return waitUntilDrainedLocked();
1074}
1075
1076status_t Camera3Device::waitUntilDrainedLocked() {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001077 switch (mStatus) {
1078 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001079 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001080 ALOGV("%s: Already idle", __FUNCTION__);
1081 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001082 case STATUS_CONFIGURED:
1083 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001084 case STATUS_ERROR:
1085 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001086 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001087 break;
1088 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001089 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090 return INVALID_OPERATION;
1091 }
1092
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001093 ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1094 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001095 if (res != OK) {
1096 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1097 res);
1098 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001099 return res;
1100}
1101
1102// Pause to reconfigure
1103status_t Camera3Device::internalPauseAndWaitLocked() {
1104 mRequestThread->setPaused(true);
1105 mPauseStateNotify = true;
1106
1107 ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1108 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1109 if (res != OK) {
1110 SET_ERR_L("Can't idle device in %f seconds!",
1111 kShutdownTimeout/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001112 }
1113
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001114 return res;
1115}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001116
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001117// Resume after internalPauseAndWaitLocked
1118status_t Camera3Device::internalResumeLocked() {
1119 status_t res;
1120
1121 mRequestThread->setPaused(false);
1122
1123 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1124 if (res != OK) {
1125 SET_ERR_L("Can't transition to active in %f seconds!",
1126 kActiveTimeout/1e9);
1127 }
1128 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001129 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001130}
1131
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001132status_t Camera3Device::waitUntilStateThenRelock(bool active,
1133 nsecs_t timeout) {
1134 status_t res = OK;
1135 if (active == (mStatus == STATUS_ACTIVE)) {
1136 // Desired state already reached
1137 return res;
1138 }
1139
1140 bool stateSeen = false;
1141 do {
1142 mRecentStatusUpdates.clear();
1143
1144 res = mStatusChanged.waitRelative(mLock, timeout);
1145 if (res != OK) break;
1146
1147 // Check state change history during wait
1148 for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1149 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1150 stateSeen = true;
1151 break;
1152 }
1153 }
1154 } while (!stateSeen);
1155
1156 return res;
1157}
1158
1159
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001160status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1161 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001162 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001163
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001164 if (listener != NULL && mListener != NULL) {
1165 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1166 }
1167 mListener = listener;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001168 mRequestThread->setNotifyCallback(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001169
1170 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001171}
1172
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001173bool Camera3Device::willNotify3A() {
1174 return false;
1175}
1176
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001177status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001178 status_t res;
1179 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001180
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001181 while (mResultQueue.empty()) {
1182 res = mResultSignal.waitRelative(mOutputLock, timeout);
1183 if (res == TIMED_OUT) {
1184 return res;
1185 } else if (res != OK) {
Colin Crosse5729fa2014-03-21 15:04:25 -07001186 ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001187 __FUNCTION__, mId, timeout, strerror(-res), res);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001188 return res;
1189 }
1190 }
1191 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001192}
1193
Jianing Weicb0652e2014-03-12 18:29:36 -07001194status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001195 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001196 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001197
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001198 if (mResultQueue.empty()) {
1199 return NOT_ENOUGH_DATA;
1200 }
1201
Jianing Weicb0652e2014-03-12 18:29:36 -07001202 if (frame == NULL) {
1203 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1204 return BAD_VALUE;
1205 }
1206
1207 CaptureResult &result = *(mResultQueue.begin());
1208 frame->mResultExtras = result.mResultExtras;
1209 frame->mMetadata.acquire(result.mMetadata);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001210 mResultQueue.erase(mResultQueue.begin());
1211
1212 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001213}
1214
1215status_t Camera3Device::triggerAutofocus(uint32_t id) {
1216 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001217 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001218
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001219 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1220 // Mix-in this trigger into the next request and only the next request.
1221 RequestTrigger trigger[] = {
1222 {
1223 ANDROID_CONTROL_AF_TRIGGER,
1224 ANDROID_CONTROL_AF_TRIGGER_START
1225 },
1226 {
1227 ANDROID_CONTROL_AF_TRIGGER_ID,
1228 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001229 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001230 };
1231
1232 return mRequestThread->queueTrigger(trigger,
1233 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001234}
1235
1236status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1237 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001238 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001240 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1241 // Mix-in this trigger into the next request and only the next request.
1242 RequestTrigger trigger[] = {
1243 {
1244 ANDROID_CONTROL_AF_TRIGGER,
1245 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1246 },
1247 {
1248 ANDROID_CONTROL_AF_TRIGGER_ID,
1249 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001250 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001251 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001252
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001253 return mRequestThread->queueTrigger(trigger,
1254 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001255}
1256
1257status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1258 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001259 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001260
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001261 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1262 // Mix-in this trigger into the next request and only the next request.
1263 RequestTrigger trigger[] = {
1264 {
1265 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1266 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1267 },
1268 {
1269 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1270 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001271 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001272 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001273
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001274 return mRequestThread->queueTrigger(trigger,
1275 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001276}
1277
1278status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1279 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1280 ATRACE_CALL();
1281 (void)reprocessStreamId; (void)buffer; (void)listener;
1282
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001283 CLOGE("Unimplemented");
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001284 return INVALID_OPERATION;
1285}
1286
Jianing Weicb0652e2014-03-12 18:29:36 -07001287status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001288 ATRACE_CALL();
1289 ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001290 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001291
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001292 NotificationListener* listener;
1293 {
1294 Mutex::Autolock l(mOutputLock);
1295 listener = mListener;
1296 }
1297
Zhijun He7ef20392014-04-21 16:04:17 -07001298 {
1299 Mutex::Autolock l(mLock);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001300 mRequestThread->clear(listener, /*out*/frameNumber);
Zhijun He7ef20392014-04-21 16:04:17 -07001301 }
1302
Zhijun He491e3412013-12-27 10:57:44 -08001303 status_t res;
1304 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1305 res = mHal3Device->ops->flush(mHal3Device);
1306 } else {
Zhijun He7ef20392014-04-21 16:04:17 -07001307 Mutex::Autolock l(mLock);
Zhijun He69a37482014-03-23 18:44:49 -07001308 res = waitUntilDrainedLocked();
Zhijun He491e3412013-12-27 10:57:44 -08001309 }
1310
1311 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001312}
1313
Zhijun He204e3292014-07-14 17:09:23 -07001314uint32_t Camera3Device::getDeviceVersion() {
1315 ATRACE_CALL();
1316 Mutex::Autolock il(mInterfaceLock);
1317 return mDeviceVersion;
1318}
1319
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001320/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001321 * Methods called by subclasses
1322 */
1323
1324void Camera3Device::notifyStatus(bool idle) {
1325 {
1326 // Need mLock to safely update state and synchronize to current
1327 // state of methods in flight.
1328 Mutex::Autolock l(mLock);
1329 // We can get various system-idle notices from the status tracker
1330 // while starting up. Only care about them if we've actually sent
1331 // in some requests recently.
1332 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1333 return;
1334 }
1335 ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1336 idle ? "idle" : "active");
1337 mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1338 mRecentStatusUpdates.add(mStatus);
1339 mStatusChanged.signal();
1340
1341 // Skip notifying listener if we're doing some user-transparent
1342 // state changes
1343 if (mPauseStateNotify) return;
1344 }
1345 NotificationListener *listener;
1346 {
1347 Mutex::Autolock l(mOutputLock);
1348 listener = mListener;
1349 }
1350 if (idle && listener != NULL) {
1351 listener->notifyIdle();
1352 }
1353}
1354
1355/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001356 * Camera3Device private methods
1357 */
1358
1359sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1360 const CameraMetadata &request) {
1361 ATRACE_CALL();
1362 status_t res;
1363
1364 sp<CaptureRequest> newRequest = new CaptureRequest;
1365 newRequest->mSettings = request;
1366
1367 camera_metadata_entry_t inputStreams =
1368 newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1369 if (inputStreams.count > 0) {
1370 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07001371 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001372 CLOGE("Request references unknown input stream %d",
1373 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001374 return NULL;
1375 }
1376 // Lazy completion of stream configuration (allocation/registration)
1377 // on first use
1378 if (mInputStream->isConfiguring()) {
1379 res = mInputStream->finishConfiguration(mHal3Device);
1380 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001381 SET_ERR_L("Unable to finish configuring input stream %d:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001382 " %s (%d)",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001383 mInputStream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001384 return NULL;
1385 }
1386 }
1387
1388 newRequest->mInputStream = mInputStream;
1389 newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1390 }
1391
1392 camera_metadata_entry_t streams =
1393 newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1394 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001395 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001396 return NULL;
1397 }
1398
1399 for (size_t i = 0; i < streams.count; i++) {
Zhijun Hed1d64672013-09-06 15:00:01 -07001400 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001401 if (idx == NAME_NOT_FOUND) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001402 CLOGE("Request references unknown stream %d",
1403 streams.data.u8[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404 return NULL;
1405 }
Igor Murashkin2fba5842013-04-22 14:03:54 -07001406 sp<Camera3OutputStreamInterface> stream =
1407 mOutputStreams.editValueAt(idx);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001408
1409 // Lazy completion of stream configuration (allocation/registration)
1410 // on first use
1411 if (stream->isConfiguring()) {
1412 res = stream->finishConfiguration(mHal3Device);
1413 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001414 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1415 stream->getId(), strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001416 return NULL;
1417 }
1418 }
1419
1420 newRequest->mOutputStreams.push(stream);
1421 }
1422 newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1423
1424 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001425}
1426
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001427status_t Camera3Device::configureStreamsLocked() {
1428 ATRACE_CALL();
1429 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001430
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001431 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001432 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001433 return INVALID_OPERATION;
1434 }
1435
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001436 if (!mNeedConfig) {
1437 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1438 return OK;
1439 }
1440
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001441 // Workaround for device HALv3.2 or older spec bug - zero streams requires
1442 // adding a dummy stream instead.
1443 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1444 if (mOutputStreams.size() == 0) {
1445 addDummyStreamLocked();
1446 } else {
1447 tryRemoveDummyStreamLocked();
1448 }
1449
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001450 // Start configuring the streams
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001451 ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001452
1453 camera3_stream_configuration config;
1454
1455 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1456
1457 Vector<camera3_stream_t*> streams;
1458 streams.setCapacity(config.num_streams);
1459
1460 if (mInputStream != NULL) {
1461 camera3_stream_t *inputStream;
1462 inputStream = mInputStream->startConfiguration();
1463 if (inputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001464 SET_ERR_L("Can't start input stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001465 return INVALID_OPERATION;
1466 }
1467 streams.add(inputStream);
1468 }
1469
1470 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07001471
1472 // Don't configure bidi streams twice, nor add them twice to the list
1473 if (mOutputStreams[i].get() ==
1474 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1475
1476 config.num_streams--;
1477 continue;
1478 }
1479
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001480 camera3_stream_t *outputStream;
1481 outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1482 if (outputStream == NULL) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001483 SET_ERR_L("Can't start output stream configuration");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001484 return INVALID_OPERATION;
1485 }
1486 streams.add(outputStream);
1487 }
1488
1489 config.streams = streams.editArray();
1490
1491 // Do the HAL configuration; will potentially touch stream
1492 // max_buffers, usage, priv fields.
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001493 ATRACE_BEGIN("camera3->configure_streams");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001494 res = mHal3Device->ops->configure_streams(mHal3Device, &config);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07001495 ATRACE_END();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001497 if (res == BAD_VALUE) {
1498 // HAL rejected this set of streams as unsupported, clean up config
1499 // attempt and return to unconfigured state
1500 if (mInputStream != NULL && mInputStream->isConfiguring()) {
1501 res = mInputStream->cancelConfiguration();
1502 if (res != OK) {
1503 SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1504 mInputStream->getId(), strerror(-res), res);
1505 return res;
1506 }
1507 }
1508
1509 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1510 sp<Camera3OutputStreamInterface> outputStream =
1511 mOutputStreams.editValueAt(i);
1512 if (outputStream->isConfiguring()) {
1513 res = outputStream->cancelConfiguration();
1514 if (res != OK) {
1515 SET_ERR_L(
1516 "Can't cancel configuring output stream %d: %s (%d)",
1517 outputStream->getId(), strerror(-res), res);
1518 return res;
1519 }
1520 }
1521 }
1522
1523 // Return state to that at start of call, so that future configures
1524 // properly clean things up
1525 mStatus = STATUS_UNCONFIGURED;
1526 mNeedConfig = true;
1527
1528 ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1529 return BAD_VALUE;
1530 } else if (res != OK) {
1531 // Some other kind of error from configure_streams - this is not
1532 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001533 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1534 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 return res;
1536 }
1537
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001538 // Finish all stream configuration immediately.
1539 // TODO: Try to relax this later back to lazy completion, which should be
1540 // faster
1541
Igor Murashkin073f8572013-05-02 14:59:28 -07001542 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001543 res = mInputStream->finishConfiguration(mHal3Device);
1544 if (res != OK) {
1545 SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1546 mInputStream->getId(), strerror(-res), res);
1547 return res;
1548 }
1549 }
1550
1551 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin073f8572013-05-02 14:59:28 -07001552 sp<Camera3OutputStreamInterface> outputStream =
1553 mOutputStreams.editValueAt(i);
1554 if (outputStream->isConfiguring()) {
1555 res = outputStream->finishConfiguration(mHal3Device);
1556 if (res != OK) {
1557 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1558 outputStream->getId(), strerror(-res), res);
1559 return res;
1560 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07001561 }
1562 }
1563
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001564 // Request thread needs to know to avoid using repeat-last-settings protocol
1565 // across configure_streams() calls
1566 mRequestThread->configurationComplete();
1567
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001568 // Update device state
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001570 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001572 if (mDummyStreamId == NO_STREAM) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001573 mStatus = STATUS_CONFIGURED;
1574 } else {
1575 mStatus = STATUS_UNCONFIGURED;
1576 }
1577
1578 ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1579
Zhijun He0a210512014-07-24 13:45:15 -07001580 // tear down the deleted streams after configure streams.
1581 mDeletedStreams.clear();
1582
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001583 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001584}
1585
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07001586status_t Camera3Device::addDummyStreamLocked() {
1587 ATRACE_CALL();
1588 status_t res;
1589
1590 if (mDummyStreamId != NO_STREAM) {
1591 // Should never be adding a second dummy stream when one is already
1592 // active
1593 SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1594 __FUNCTION__, mId);
1595 return INVALID_OPERATION;
1596 }
1597
1598 ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1599
1600 sp<Camera3OutputStreamInterface> dummyStream =
1601 new Camera3DummyStream(mNextStreamId);
1602
1603 res = mOutputStreams.add(mNextStreamId, dummyStream);
1604 if (res < 0) {
1605 SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1606 return res;
1607 }
1608
1609 mDummyStreamId = mNextStreamId;
1610 mNextStreamId++;
1611
1612 return OK;
1613}
1614
1615status_t Camera3Device::tryRemoveDummyStreamLocked() {
1616 ATRACE_CALL();
1617 status_t res;
1618
1619 if (mDummyStreamId == NO_STREAM) return OK;
1620 if (mOutputStreams.size() == 1) return OK;
1621
1622 ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1623
1624 // Ok, have a dummy stream and there's at least one other output stream,
1625 // so remove the dummy
1626
1627 sp<Camera3StreamInterface> deletedStream;
1628 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1629 if (outputStreamIdx == NAME_NOT_FOUND) {
1630 SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1631 return INVALID_OPERATION;
1632 }
1633
1634 deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1635 mOutputStreams.removeItemsAt(outputStreamIdx);
1636
1637 // Free up the stream endpoint so that it can be used by some other stream
1638 res = deletedStream->disconnect();
1639 if (res != OK) {
1640 SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1641 // fall through since we want to still list the stream as deleted.
1642 }
1643 mDeletedStreams.add(deletedStream);
1644 mDummyStreamId = NO_STREAM;
1645
1646 return res;
1647}
1648
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001649void Camera3Device::setErrorState(const char *fmt, ...) {
1650 Mutex::Autolock l(mLock);
1651 va_list args;
1652 va_start(args, fmt);
1653
1654 setErrorStateLockedV(fmt, args);
1655
1656 va_end(args);
1657}
1658
1659void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1660 Mutex::Autolock l(mLock);
1661 setErrorStateLockedV(fmt, args);
1662}
1663
1664void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1665 va_list args;
1666 va_start(args, fmt);
1667
1668 setErrorStateLockedV(fmt, args);
1669
1670 va_end(args);
1671}
1672
1673void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001674 // Print out all error messages to log
1675 String8 errorCause = String8::formatV(fmt, args);
1676 ALOGE("Camera %d: %s", mId, errorCause.string());
1677
1678 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07001679 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001680
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001681 mErrorCause = errorCause;
1682
1683 mRequestThread->setPaused(true);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001684 mStatus = STATUS_ERROR;
Eino-Ville Talvala17543512014-08-06 14:32:02 -07001685
1686 // Notify upstream about a device error
1687 if (mListener != NULL) {
1688 mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1689 CaptureResultExtras());
1690 }
1691
1692 // Save stack trace. View by dumping it later.
1693 CameraTraces::saveTrace();
1694 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001695}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001696
1697/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001698 * In-flight request management
1699 */
1700
Jianing Weicb0652e2014-03-12 18:29:36 -07001701status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001702 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001703 ATRACE_CALL();
1704 Mutex::Autolock l(mInFlightLock);
1705
1706 ssize_t res;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07001707 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07001708 if (res < 0) return res;
1709
1710 return OK;
1711}
1712
1713/**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001714 * Check if all 3A fields are ready, and send off a partial 3A-only result
1715 * to the output frame queue
1716 */
Zhijun He204e3292014-07-14 17:09:23 -07001717bool Camera3Device::processPartial3AResult(
Jianing Weicb0652e2014-03-12 18:29:36 -07001718 uint32_t frameNumber,
1719 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001720
1721 // Check if all 3A states are present
1722 // The full list of fields is
1723 // android.control.afMode
1724 // android.control.awbMode
1725 // android.control.aeState
1726 // android.control.awbState
1727 // android.control.afState
1728 // android.control.afTriggerID
1729 // android.control.aePrecaptureID
1730 // TODO: Add android.control.aeMode
1731
1732 bool gotAllStates = true;
1733
1734 uint8_t afMode;
1735 uint8_t awbMode;
1736 uint8_t aeState;
1737 uint8_t afState;
1738 uint8_t awbState;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001739
1740 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1741 &afMode, frameNumber);
1742
1743 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1744 &awbMode, frameNumber);
1745
1746 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1747 &aeState, frameNumber);
1748
1749 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1750 &afState, frameNumber);
1751
1752 gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1753 &awbState, frameNumber);
1754
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001755 if (!gotAllStates) return false;
1756
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001757 ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001758 "AF state %d, AE state %d, AWB state %d, "
1759 "AF trigger %d, AE precapture trigger %d",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001760 __FUNCTION__, mId, frameNumber, resultExtras.requestId,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001761 afMode, awbMode,
1762 afState, aeState, awbState,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001763 resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001764
1765 // Got all states, so construct a minimal result to send
1766 // In addition to the above fields, this means adding in
1767 // android.request.frameCount
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001768 // android.request.requestId
Zhijun He204e3292014-07-14 17:09:23 -07001769 // android.quirks.partialResult (for HAL version below HAL3.2)
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001770
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001771 const size_t kMinimal3AResultEntries = 10;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001772
1773 Mutex::Autolock l(mOutputLock);
1774
Jianing Weicb0652e2014-03-12 18:29:36 -07001775 CaptureResult captureResult;
1776 captureResult.mResultExtras = resultExtras;
1777 captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1778 // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1779 // but not limited to CameraDeviceBase::getNextResult
1780 CaptureResult& min3AResult =
1781 *mResultQueue.insert(mResultQueue.end(), captureResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001782
Jianing Weicb0652e2014-03-12 18:29:36 -07001783 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1784 // TODO: This is problematic casting. Need to fix CameraMetadata.
1785 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001786 return false;
1787 }
1788
Jianing Weicb0652e2014-03-12 18:29:36 -07001789 int32_t requestId = resultExtras.requestId;
1790 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
Eino-Ville Talvala184dfe42013-11-07 15:13:16 -08001791 &requestId, frameNumber)) {
1792 return false;
1793 }
1794
Zhijun He204e3292014-07-14 17:09:23 -07001795 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1796 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1797 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1798 &partialResult, frameNumber)) {
1799 return false;
1800 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001801 }
1802
Jianing Weicb0652e2014-03-12 18:29:36 -07001803 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001804 &afMode, frameNumber)) {
1805 return false;
1806 }
1807
Jianing Weicb0652e2014-03-12 18:29:36 -07001808 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001809 &awbMode, frameNumber)) {
1810 return false;
1811 }
1812
Jianing Weicb0652e2014-03-12 18:29:36 -07001813 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001814 &aeState, frameNumber)) {
1815 return false;
1816 }
1817
Jianing Weicb0652e2014-03-12 18:29:36 -07001818 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001819 &afState, frameNumber)) {
1820 return false;
1821 }
1822
Jianing Weicb0652e2014-03-12 18:29:36 -07001823 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001824 &awbState, frameNumber)) {
1825 return false;
1826 }
1827
Jianing Weicb0652e2014-03-12 18:29:36 -07001828 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001829 &resultExtras.afTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001830 return false;
1831 }
1832
Jianing Weicb0652e2014-03-12 18:29:36 -07001833 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001834 &resultExtras.precaptureTriggerId, frameNumber)) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001835 return false;
1836 }
1837
Zhijun He204e3292014-07-14 17:09:23 -07001838 // We only send the aggregated partial when all 3A related metadata are available
1839 // For both API1 and API2.
1840 // TODO: we probably should pass through all partials to API2 unconditionally.
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001841 mResultSignal.signal();
1842
1843 return true;
1844}
1845
1846template<typename T>
1847bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001848 T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001849 (void) frameNumber;
1850
1851 camera_metadata_ro_entry_t entry;
1852
1853 entry = result.find(tag);
1854 if (entry.count == 0) {
1855 ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1856 mId, frameNumber, get_camera_metadata_tag_name(tag));
1857 return false;
1858 }
1859
1860 if (sizeof(T) == sizeof(uint8_t)) {
1861 *value = entry.data.u8[0];
1862 } else if (sizeof(T) == sizeof(int32_t)) {
1863 *value = entry.data.i32[0];
1864 } else {
1865 ALOGE("%s: Unexpected type", __FUNCTION__);
1866 return false;
1867 }
1868 return true;
1869}
1870
1871template<typename T>
1872bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
Jianing Weicb0652e2014-03-12 18:29:36 -07001873 const T* value, uint32_t frameNumber) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07001874 if (result.update(tag, value, 1) != NO_ERROR) {
1875 mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1876 SET_ERR("Frame %d: Failed to set %s in partial metadata",
1877 frameNumber, get_camera_metadata_tag_name(tag));
1878 return false;
1879 }
1880 return true;
1881}
1882
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08001883
1884void Camera3Device::returnOutputBuffers(
1885 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
1886 nsecs_t timestamp) {
1887 for (size_t i = 0; i < numBuffers; i++)
1888 {
1889 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
1890 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
1891 // Note: stream may be deallocated at this point, if this buffer was
1892 // the last reference to it.
1893 if (res != OK) {
1894 ALOGE("Can't return buffer to its stream: %s (%d)",
1895 strerror(-res), res);
1896 }
1897 }
1898}
1899
1900
1901void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
1902
1903 const InFlightRequest &request = mInFlightMap.valueAt(idx);
1904 const uint32_t frameNumber = mInFlightMap.keyAt(idx);
1905
1906 nsecs_t sensorTimestamp = request.sensorTimestamp;
1907 nsecs_t shutterTimestamp = request.shutterTimestamp;
1908
1909 // Check if it's okay to remove the request from InFlightMap:
1910 // In the case of a successful request:
1911 // all input and output buffers, all result metadata, shutter callback
1912 // arrived.
1913 // In the case of a unsuccessful request:
1914 // all input and output buffers arrived.
1915 if (request.numBuffersLeft == 0 &&
1916 (request.requestStatus != OK ||
1917 (request.haveResultMetadata && shutterTimestamp != 0))) {
1918 ATRACE_ASYNC_END("frame capture", frameNumber);
1919
1920 // Sanity check - if sensor timestamp matches shutter timestamp
1921 if (request.requestStatus == OK &&
1922 sensorTimestamp != shutterTimestamp) {
1923 SET_ERR("sensor timestamp (%" PRId64
1924 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
1925 sensorTimestamp, frameNumber, shutterTimestamp);
1926 }
1927
1928 // for an unsuccessful request, it may have pending output buffers to
1929 // return.
1930 assert(request.requestStatus != OK ||
1931 request.pendingOutputBuffers.size() == 0);
1932 returnOutputBuffers(request.pendingOutputBuffers.array(),
1933 request.pendingOutputBuffers.size(), 0);
1934
1935 mInFlightMap.removeItemsAt(idx, 1);
1936
1937 ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
1938 }
1939
1940 // Sanity check - if we have too many in-flight frames, something has
1941 // likely gone wrong
1942 if (mInFlightMap.size() > kInFlightWarnLimit) {
1943 CLOGE("In-flight list too large: %zu", mInFlightMap.size());
1944 }
1945}
1946
1947
1948void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
1949 CaptureResultExtras &resultExtras,
1950 CameraMetadata &collectedPartialResult,
1951 uint32_t frameNumber) {
1952 if (pendingMetadata.isEmpty())
1953 return;
1954
1955 Mutex::Autolock l(mOutputLock);
1956
1957 // TODO: need to track errors for tighter bounds on expected frame number
1958 if (frameNumber < mNextResultFrameNumber) {
1959 SET_ERR("Out-of-order capture result metadata submitted! "
1960 "(got frame number %d, expecting %d)",
1961 frameNumber, mNextResultFrameNumber);
1962 return;
1963 }
1964 mNextResultFrameNumber = frameNumber + 1;
1965
1966 CaptureResult captureResult;
1967 captureResult.mResultExtras = resultExtras;
1968 captureResult.mMetadata = pendingMetadata;
1969
1970 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
1971 (int32_t*)&frameNumber, 1) != OK) {
1972 SET_ERR("Failed to set frame# in metadata (%d)",
1973 frameNumber);
1974 return;
1975 } else {
1976 ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
1977 __FUNCTION__, mId, frameNumber);
1978 }
1979
1980 // Append any previous partials to form a complete result
1981 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
1982 captureResult.mMetadata.append(collectedPartialResult);
1983 }
1984
1985 captureResult.mMetadata.sort();
1986
1987 // Check that there's a timestamp in the result metadata
1988 camera_metadata_entry entry =
1989 captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
1990 if (entry.count == 0) {
1991 SET_ERR("No timestamp provided by HAL for frame %d!",
1992 frameNumber);
1993 return;
1994 }
1995
1996 // Valid result, insert into queue
1997 List<CaptureResult>::iterator queuedResult =
1998 mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
1999 ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2000 ", burstId = %" PRId32, __FUNCTION__,
2001 queuedResult->mResultExtras.requestId,
2002 queuedResult->mResultExtras.frameNumber,
2003 queuedResult->mResultExtras.burstId);
2004
2005 mResultSignal.signal();
2006}
2007
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002008/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002009 * Camera HAL device callback methods
2010 */
2011
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002012void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002013 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002014
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002015 status_t res;
2016
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002017 uint32_t frameNumber = result->frame_number;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002018 if (result->result == NULL && result->num_output_buffers == 0 &&
2019 result->input_buffer == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002020 SET_ERR("No result data provided by HAL for frame %d",
2021 frameNumber);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002022 return;
2023 }
Zhijun He204e3292014-07-14 17:09:23 -07002024
2025 // For HAL3.2 or above, If HAL doesn't support partial, it must always set
2026 // partial_result to 1 when metadata is included in this result.
2027 if (!mUsePartialResult &&
2028 mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
2029 result->result != NULL &&
2030 result->partial_result != 1) {
2031 SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
2032 " if partial result is not supported",
2033 frameNumber, result->partial_result);
2034 return;
2035 }
2036
2037 bool isPartialResult = false;
2038 CameraMetadata collectedPartialResult;
Jianing Weicb0652e2014-03-12 18:29:36 -07002039 CaptureResultExtras resultExtras;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002040 bool hasInputBufferInRequest = false;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002041
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002042 // Get shutter timestamp and resultExtras from list of in-flight requests,
2043 // where it was added by the shutter notification for this frame. If the
2044 // shutter timestamp isn't received yet, append the output buffers to the
2045 // in-flight request and they will be returned when the shutter timestamp
2046 // arrives. Update the in-flight status and remove the in-flight entry if
2047 // all result data and shutter timestamp have been received.
2048 nsecs_t shutterTimestamp = 0;
2049
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002050 {
2051 Mutex::Autolock l(mInFlightLock);
2052 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
2053 if (idx == NAME_NOT_FOUND) {
2054 SET_ERR("Unknown frame number for capture result: %d",
2055 frameNumber);
2056 return;
2057 }
2058 InFlightRequest &request = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002059 ALOGVV("%s: got InFlightRequest requestId = %" PRId32
2060 ", frameNumber = %" PRId64 ", burstId = %" PRId32
2061 ", partialResultCount = %d",
2062 __FUNCTION__, request.resultExtras.requestId,
2063 request.resultExtras.frameNumber, request.resultExtras.burstId,
2064 result->partial_result);
2065 // Always update the partial count to the latest one if it's not 0
2066 // (buffers only). When framework aggregates adjacent partial results
2067 // into one, the latest partial count will be used.
2068 if (result->partial_result != 0)
2069 request.resultExtras.partialResultCount = result->partial_result;
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002070
2071 // Check if this result carries only partial metadata
Zhijun He204e3292014-07-14 17:09:23 -07002072 if (mUsePartialResult && result->result != NULL) {
2073 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2074 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
2075 SET_ERR("Result is malformed for frame %d: partial_result %u must be in"
2076 " the range of [1, %d] when metadata is included in the result",
2077 frameNumber, result->partial_result, mNumPartialResults);
2078 return;
2079 }
2080 isPartialResult = (result->partial_result < mNumPartialResults);
Zhijun He5d76e1a2014-07-22 16:08:13 -07002081 if (isPartialResult) {
2082 request.partialResult.collectedResult.append(result->result);
2083 }
Zhijun He204e3292014-07-14 17:09:23 -07002084 } else {
2085 camera_metadata_ro_entry_t partialResultEntry;
2086 res = find_camera_metadata_ro_entry(result->result,
2087 ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
2088 if (res != NAME_NOT_FOUND &&
2089 partialResultEntry.count > 0 &&
2090 partialResultEntry.data.u8[0] ==
2091 ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
2092 // A partial result. Flag this as such, and collect this
2093 // set of metadata into the in-flight entry.
2094 isPartialResult = true;
2095 request.partialResult.collectedResult.append(
2096 result->result);
2097 request.partialResult.collectedResult.erase(
2098 ANDROID_QUIRKS_PARTIAL_RESULT);
2099 }
2100 }
2101
2102 if (isPartialResult) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002103 // Fire off a 3A-only result if possible
Zhijun He204e3292014-07-14 17:09:23 -07002104 if (!request.partialResult.haveSent3A) {
2105 request.partialResult.haveSent3A =
2106 processPartial3AResult(frameNumber,
2107 request.partialResult.collectedResult,
Jianing Weicb0652e2014-03-12 18:29:36 -07002108 request.resultExtras);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002109 }
2110 }
2111 }
2112
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002113 shutterTimestamp = request.shutterTimestamp;
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002114 hasInputBufferInRequest = request.hasInputBuffer;
Jianing Weicb0652e2014-03-12 18:29:36 -07002115
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002116 // Did we get the (final) result metadata for this capture?
Zhijun He204e3292014-07-14 17:09:23 -07002117 if (result->result != NULL && !isPartialResult) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002118 if (request.haveResultMetadata) {
2119 SET_ERR("Called multiple times with metadata for frame %d",
2120 frameNumber);
2121 return;
2122 }
Zhijun He204e3292014-07-14 17:09:23 -07002123 if (mUsePartialResult &&
2124 !request.partialResult.collectedResult.isEmpty()) {
2125 collectedPartialResult.acquire(
2126 request.partialResult.collectedResult);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002127 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002128 request.haveResultMetadata = true;
2129 }
2130
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002131 uint32_t numBuffersReturned = result->num_output_buffers;
2132 if (result->input_buffer != NULL) {
2133 if (hasInputBufferInRequest) {
2134 numBuffersReturned += 1;
2135 } else {
2136 ALOGW("%s: Input buffer should be NULL if there is no input"
2137 " buffer sent in the request",
2138 __FUNCTION__);
2139 }
2140 }
2141 request.numBuffersLeft -= numBuffersReturned;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002142 if (request.numBuffersLeft < 0) {
2143 SET_ERR("Too many buffers returned for frame %d",
2144 frameNumber);
2145 return;
2146 }
2147
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002148 camera_metadata_ro_entry_t entry;
2149 res = find_camera_metadata_ro_entry(result->result,
2150 ANDROID_SENSOR_TIMESTAMP, &entry);
2151 if (res == OK && entry.count == 1) {
2152 request.sensorTimestamp = entry.data.i64[0];
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002153 }
2154
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002155 // If shutter event isn't received yet, append the output buffers to
2156 // the in-flight request. Otherwise, return the output buffers to
2157 // streams.
2158 if (shutterTimestamp == 0) {
2159 request.pendingOutputBuffers.appendArray(result->output_buffers,
2160 result->num_output_buffers);
Igor Murashkind2c90692013-04-02 12:32:32 -07002161 } else {
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002162 returnOutputBuffers(result->output_buffers,
2163 result->num_output_buffers, shutterTimestamp);
Igor Murashkind2c90692013-04-02 12:32:32 -07002164 }
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002165
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002166 if (result->result != NULL && !isPartialResult) {
2167 if (shutterTimestamp == 0) {
2168 request.pendingMetadata = result->result;
2169 request.partialResult.collectedResult = collectedPartialResult;
2170 } else {
2171 CameraMetadata metadata;
2172 metadata = result->result;
2173 sendCaptureResult(metadata, request.resultExtras,
2174 collectedPartialResult, frameNumber);
2175 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -07002176 }
2177
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002178 removeInFlightRequestIfReadyLocked(idx);
2179 } // scope for mInFlightLock
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002180
Zhijun Hef0d962a2014-06-30 10:24:11 -07002181 if (result->input_buffer != NULL) {
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002182 if (hasInputBufferInRequest) {
2183 Camera3Stream *stream =
2184 Camera3Stream::cast(result->input_buffer->stream);
2185 res = stream->returnInputBuffer(*(result->input_buffer));
2186 // Note: stream may be deallocated at this point, if this buffer was the
2187 // last reference to it.
2188 if (res != OK) {
2189 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2190 " its stream:%s (%d)", __FUNCTION__,
2191 frameNumber, strerror(-res), res);
Zhijun He0ea8fa42014-07-07 17:05:38 -07002192 }
2193 } else {
2194 ALOGW("%s: Input buffer should be NULL if there is no input"
2195 " buffer sent in the request, skipping input buffer return.",
2196 __FUNCTION__);
Zhijun Hef0d962a2014-06-30 10:24:11 -07002197 }
2198 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002199}
2200
2201void Camera3Device::notify(const camera3_notify_msg *msg) {
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002202 ATRACE_CALL();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002203 NotificationListener *listener;
2204 {
2205 Mutex::Autolock l(mOutputLock);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002206 listener = mListener;
2207 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002208
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002209 if (msg == NULL) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002210 SET_ERR("HAL sent NULL notify message!");
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002211 return;
2212 }
2213
2214 switch (msg->type) {
2215 case CAMERA3_MSG_ERROR: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002216 notifyError(msg->message.error, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002217 break;
2218 }
2219 case CAMERA3_MSG_SHUTTER: {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002220 notifyShutter(msg->message.shutter, listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002221 break;
2222 }
2223 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002224 SET_ERR("Unknown notify message from HAL: %d",
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002225 msg->type);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002226 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002227}
2228
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002229void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2230 NotificationListener *listener) {
2231
2232 // Map camera HAL error codes to ICameraDeviceCallback error codes
2233 // Index into this with the HAL error code
2234 static const ICameraDeviceCallbacks::CameraErrorCode
2235 halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2236 // 0 = Unused error code
2237 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2238 // 1 = CAMERA3_MSG_ERROR_DEVICE
2239 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2240 // 2 = CAMERA3_MSG_ERROR_REQUEST
2241 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2242 // 3 = CAMERA3_MSG_ERROR_RESULT
2243 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2244 // 4 = CAMERA3_MSG_ERROR_BUFFER
2245 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2246 };
2247
2248 ICameraDeviceCallbacks::CameraErrorCode errorCode =
2249 ((msg.error_code >= 0) &&
2250 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2251 halErrorMap[msg.error_code] :
2252 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2253
2254 int streamId = 0;
2255 if (msg.error_stream != NULL) {
2256 Camera3Stream *stream =
2257 Camera3Stream::cast(msg.error_stream);
2258 streamId = stream->getId();
2259 }
2260 ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2261 mId, __FUNCTION__, msg.frame_number,
2262 streamId, msg.error_code);
2263
2264 CaptureResultExtras resultExtras;
2265 switch (errorCode) {
2266 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2267 // SET_ERR calls notifyError
2268 SET_ERR("Camera HAL reported serious device error");
2269 break;
2270 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2271 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2272 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2273 {
2274 Mutex::Autolock l(mInFlightLock);
2275 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2276 if (idx >= 0) {
2277 InFlightRequest &r = mInFlightMap.editValueAt(idx);
2278 r.requestStatus = msg.error_code;
2279 resultExtras = r.resultExtras;
2280 } else {
2281 resultExtras.frameNumber = msg.frame_number;
2282 ALOGE("Camera %d: %s: cannot find in-flight request on "
2283 "frame %" PRId64 " error", mId, __FUNCTION__,
2284 resultExtras.frameNumber);
2285 }
2286 }
2287 if (listener != NULL) {
2288 listener->notifyError(errorCode, resultExtras);
2289 } else {
2290 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2291 }
2292 break;
2293 default:
2294 // SET_ERR calls notifyError
2295 SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2296 break;
2297 }
2298}
2299
2300void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2301 NotificationListener *listener) {
2302 ssize_t idx;
2303 // Verify ordering of shutter notifications
2304 {
2305 Mutex::Autolock l(mOutputLock);
2306 // TODO: need to track errors for tighter bounds on expected frame number.
2307 if (msg.frame_number < mNextShutterFrameNumber) {
2308 SET_ERR("Shutter notification out-of-order. Expected "
2309 "notification for frame %d, got frame %d",
2310 mNextShutterFrameNumber, msg.frame_number);
2311 return;
2312 }
2313 mNextShutterFrameNumber = msg.frame_number + 1;
2314 }
2315
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002316 // Set timestamp for the request in the in-flight tracking
2317 // and get the request ID to send upstream
2318 {
2319 Mutex::Autolock l(mInFlightLock);
2320 idx = mInFlightMap.indexOfKey(msg.frame_number);
2321 if (idx >= 0) {
2322 InFlightRequest &r = mInFlightMap.editValueAt(idx);
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002323
2324 ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2325 mId, __FUNCTION__,
2326 msg.frame_number, r.resultExtras.requestId, msg.timestamp);
2327 // Call listener, if any
2328 if (listener != NULL) {
2329 listener->notifyShutter(r.resultExtras, msg.timestamp);
2330 }
2331
2332 r.shutterTimestamp = msg.timestamp;
2333
2334 // send pending result and buffers
2335 sendCaptureResult(r.pendingMetadata, r.resultExtras,
2336 r.partialResult.collectedResult, msg.frame_number);
2337 returnOutputBuffers(r.pendingOutputBuffers.array(),
2338 r.pendingOutputBuffers.size(), r.shutterTimestamp);
2339 r.pendingOutputBuffers.clear();
2340
2341 removeInFlightRequestIfReadyLocked(idx);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002342 }
2343 }
2344 if (idx < 0) {
2345 SET_ERR("Shutter notification for non-existent frame number %d",
2346 msg.frame_number);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002347 }
2348}
2349
2350
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002351CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002352 ALOGV("%s", __FUNCTION__);
2353
Igor Murashkin1e479c02013-09-06 16:55:14 -07002354 CameraMetadata retVal;
2355
2356 if (mRequestThread != NULL) {
2357 retVal = mRequestThread->getLatestRequest();
2358 }
2359
Igor Murashkin1e479c02013-09-06 16:55:14 -07002360 return retVal;
2361}
2362
Jianing Weicb0652e2014-03-12 18:29:36 -07002363
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002364/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002365 * RequestThread inner class methods
2366 */
2367
2368Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002369 sp<StatusTracker> statusTracker,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002370 camera3_device_t *hal3Device) :
2371 Thread(false),
2372 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002373 mStatusTracker(statusTracker),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002374 mHal3Device(hal3Device),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002375 mId(getId(parent)),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 mReconfigured(false),
2377 mDoPause(false),
2378 mPaused(true),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002379 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07002380 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002381 mCurrentAfTriggerId(0),
2382 mCurrentPreCaptureTriggerId(0),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002383 mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002384 mStatusId = statusTracker->addComponent();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002385}
2386
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002387void Camera3Device::RequestThread::setNotifyCallback(
2388 NotificationListener *listener) {
2389 Mutex::Autolock l(mRequestLock);
2390 mListener = listener;
2391}
2392
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002393void Camera3Device::RequestThread::configurationComplete() {
2394 Mutex::Autolock l(mRequestLock);
2395 mReconfigured = true;
2396}
2397
Jianing Wei90e59c92014-03-12 18:29:36 -07002398status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002399 List<sp<CaptureRequest> > &requests,
2400 /*out*/
2401 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07002402 Mutex::Autolock l(mRequestLock);
2403 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2404 ++it) {
2405 mRequestQueue.push_back(*it);
2406 }
2407
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002408 if (lastFrameNumber != NULL) {
2409 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2410 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2411 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2412 *lastFrameNumber);
2413 }
Jianing Weicb0652e2014-03-12 18:29:36 -07002414
Jianing Wei90e59c92014-03-12 18:29:36 -07002415 unpauseForNewRequests();
2416
2417 return OK;
2418}
2419
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002420
2421status_t Camera3Device::RequestThread::queueTrigger(
2422 RequestTrigger trigger[],
2423 size_t count) {
2424
2425 Mutex::Autolock l(mTriggerMutex);
2426 status_t ret;
2427
2428 for (size_t i = 0; i < count; ++i) {
2429 ret = queueTriggerLocked(trigger[i]);
2430
2431 if (ret != OK) {
2432 return ret;
2433 }
2434 }
2435
2436 return OK;
2437}
2438
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002439int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2440 sp<Camera3Device> d = device.promote();
2441 if (d != NULL) return d->mId;
2442 return 0;
2443}
2444
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002445status_t Camera3Device::RequestThread::queueTriggerLocked(
2446 RequestTrigger trigger) {
2447
2448 uint32_t tag = trigger.metadataTag;
2449 ssize_t index = mTriggerMap.indexOfKey(tag);
2450
2451 switch (trigger.getTagType()) {
2452 case TYPE_BYTE:
2453 // fall-through
2454 case TYPE_INT32:
2455 break;
2456 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002457 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2458 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002459 return INVALID_OPERATION;
2460 }
2461
2462 /**
2463 * Collect only the latest trigger, since we only have 1 field
2464 * in the request settings per trigger tag, and can't send more than 1
2465 * trigger per request.
2466 */
2467 if (index != NAME_NOT_FOUND) {
2468 mTriggerMap.editValueAt(index) = trigger;
2469 } else {
2470 mTriggerMap.add(tag, trigger);
2471 }
2472
2473 return OK;
2474}
2475
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002476status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002477 const RequestList &requests,
2478 /*out*/
2479 int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002480 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002481 if (lastFrameNumber != NULL) {
2482 *lastFrameNumber = mRepeatingLastFrameNumber;
2483 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002484 mRepeatingRequests.clear();
2485 mRepeatingRequests.insert(mRepeatingRequests.begin(),
2486 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002487
2488 unpauseForNewRequests();
2489
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002490 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002491 return OK;
2492}
2493
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002494bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2495 if (mRepeatingRequests.empty()) {
2496 return false;
2497 }
2498 int32_t requestId = requestIn->mResultExtras.requestId;
2499 const RequestList &repeatRequests = mRepeatingRequests;
2500 // All repeating requests are guaranteed to have same id so only check first quest
2501 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2502 return (firstRequest->mResultExtras.requestId == requestId);
2503}
2504
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002505status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002506 Mutex::Autolock l(mRequestLock);
2507 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002508 if (lastFrameNumber != NULL) {
2509 *lastFrameNumber = mRepeatingLastFrameNumber;
2510 }
2511 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002512 return OK;
2513}
2514
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002515status_t Camera3Device::RequestThread::clear(
2516 NotificationListener *listener,
2517 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002518 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002519 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002520
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002521 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002522
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002523 // Send errors for all requests pending in the request queue, including
2524 // pending repeating requests
2525 if (listener != NULL) {
2526 for (RequestList::iterator it = mRequestQueue.begin();
2527 it != mRequestQueue.end(); ++it) {
2528 // Set the frame number this request would have had, if it
2529 // had been submitted; this frame number will not be reused.
2530 // The requestId and burstId fields were set when the request was
2531 // submitted originally (in convertMetadataListToRequestListLocked)
2532 (*it)->mResultExtras.frameNumber = mFrameNumber++;
2533 listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2534 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07002535 }
2536 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002537 mRequestQueue.clear();
2538 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002539 if (lastFrameNumber != NULL) {
2540 *lastFrameNumber = mRepeatingLastFrameNumber;
2541 }
2542 mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002543 return OK;
2544}
2545
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002546void Camera3Device::RequestThread::setPaused(bool paused) {
2547 Mutex::Autolock l(mPauseLock);
2548 mDoPause = paused;
2549 mDoPauseSignal.signal();
2550}
2551
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002552status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2553 int32_t requestId, nsecs_t timeout) {
2554 Mutex::Autolock l(mLatestRequestMutex);
2555 status_t res;
2556 while (mLatestRequestId != requestId) {
2557 nsecs_t startTime = systemTime();
2558
2559 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2560 if (res != OK) return res;
2561
2562 timeout -= (systemTime() - startTime);
2563 }
2564
2565 return OK;
2566}
2567
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002568void Camera3Device::RequestThread::requestExit() {
2569 // Call parent to set up shutdown
2570 Thread::requestExit();
2571 // The exit from any possible waits
2572 mDoPauseSignal.signal();
2573 mRequestSignal.signal();
2574}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002575
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002576bool Camera3Device::RequestThread::threadLoop() {
2577
2578 status_t res;
2579
2580 // Handle paused state.
2581 if (waitIfPaused()) {
2582 return true;
2583 }
2584
2585 // Get work to do
2586
2587 sp<CaptureRequest> nextRequest = waitForNextRequest();
2588 if (nextRequest == NULL) {
2589 return true;
2590 }
2591
2592 // Create request to HAL
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002593 camera3_capture_request_t request = camera3_capture_request_t();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002594 request.frame_number = nextRequest->mResultExtras.frameNumber;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002595 Vector<camera3_stream_buffer_t> outputBuffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002596
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002597 // Get the request ID, if any
2598 int requestId;
2599 camera_metadata_entry_t requestIdEntry =
2600 nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2601 if (requestIdEntry.count > 0) {
2602 requestId = requestIdEntry.data.i32[0];
2603 } else {
2604 ALOGW("%s: Did not have android.request.id set in the request",
2605 __FUNCTION__);
2606 requestId = NAME_NOT_FOUND;
2607 }
2608
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002609 // Insert any queued triggers (before metadata is locked)
2610 int32_t triggerCount;
2611 res = insertTriggers(nextRequest);
2612 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002613 SET_ERR("RequestThread: Unable to insert triggers "
2614 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002615 request.frame_number, strerror(-res), res);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002616 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2617 return false;
2618 }
2619 triggerCount = res;
2620
2621 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2622
2623 // If the request is the same as last, or we had triggers last time
2624 if (mPrevRequest != nextRequest || triggersMixedIn) {
2625 /**
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002626 * HAL workaround:
2627 * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2628 */
2629 res = addDummyTriggerIds(nextRequest);
2630 if (res != OK) {
2631 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2632 "(capture request %d, HAL device: %s (%d)",
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002633 request.frame_number, strerror(-res), res);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07002634 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2635 return false;
2636 }
2637
2638 /**
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002639 * The request should be presorted so accesses in HAL
2640 * are O(logn). Sidenote, sorting a sorted metadata is nop.
2641 */
2642 nextRequest->mSettings.sort();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002643 request.settings = nextRequest->mSettings.getAndLock();
2644 mPrevRequest = nextRequest;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002645 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2646
2647 IF_ALOGV() {
2648 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2649 find_camera_metadata_ro_entry(
2650 request.settings,
2651 ANDROID_CONTROL_AF_TRIGGER,
2652 &e
2653 );
2654 if (e.count > 0) {
2655 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2656 __FUNCTION__,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002657 request.frame_number,
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002658 e.data.u8[0]);
2659 }
2660 }
2661 } else {
2662 // leave request.settings NULL to indicate 'reuse latest given'
2663 ALOGVV("%s: Request settings are REUSED",
2664 __FUNCTION__);
2665 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002666
2667 camera3_stream_buffer_t inputBuffer;
Zhijun Hef0d962a2014-06-30 10:24:11 -07002668 uint32_t totalNumBuffers = 0;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002669
2670 // Fill in buffers
2671
2672 if (nextRequest->mInputStream != NULL) {
2673 request.input_buffer = &inputBuffer;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002674 res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002675 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002676 // Can't get input buffer from gralloc queue - this could be due to
2677 // disconnected queue or other producer misbehavior, so not a fatal
2678 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002679 ALOGE("RequestThread: Can't get input buffer, skipping request:"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002681 Mutex::Autolock l(mRequestLock);
2682 if (mListener != NULL) {
2683 mListener->notifyError(
2684 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2685 nextRequest->mResultExtras);
2686 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2688 return true;
2689 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002690 totalNumBuffers += 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002691 } else {
2692 request.input_buffer = NULL;
2693 }
2694
2695 outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2696 nextRequest->mOutputStreams.size());
2697 request.output_buffers = outputBuffers.array();
2698 for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2699 res = nextRequest->mOutputStreams.editItemAt(i)->
2700 getBuffer(&outputBuffers.editItemAt(i));
2701 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002702 // Can't get output buffer from gralloc queue - this could be due to
2703 // abandoned queue or other consumer misbehavior, so not a fatal
2704 // error
Eino-Ville Talvala07d21692013-09-24 18:04:19 -07002705 ALOGE("RequestThread: Can't get output buffer, skipping request:"
2706 " %s (%d)", strerror(-res), res);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002707 Mutex::Autolock l(mRequestLock);
2708 if (mListener != NULL) {
2709 mListener->notifyError(
2710 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2711 nextRequest->mResultExtras);
2712 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002713 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2714 return true;
2715 }
2716 request.num_output_buffers++;
2717 }
Zhijun Hef0d962a2014-06-30 10:24:11 -07002718 totalNumBuffers += request.num_output_buffers;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002719
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002720 // Log request in the in-flight queue
2721 sp<Camera3Device> parent = mParent.promote();
2722 if (parent == NULL) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002723 // Should not happen, and nowhere to send errors to, so just log it
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002724 CLOGE("RequestThread: Parent is gone");
2725 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2726 return false;
2727 }
2728
Jianing Weicb0652e2014-03-12 18:29:36 -07002729 res = parent->registerInFlight(request.frame_number,
Zhijun Hec98bd8d2014-07-07 12:44:10 -07002730 totalNumBuffers, nextRequest->mResultExtras,
2731 /*hasInput*/request.input_buffer != NULL);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002732 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2733 ", burstId = %" PRId32 ".",
Jianing Weicb0652e2014-03-12 18:29:36 -07002734 __FUNCTION__,
2735 nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2736 nextRequest->mResultExtras.burstId);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002737 if (res != OK) {
2738 SET_ERR("RequestThread: Unable to register new in-flight request:"
2739 " %s (%d)", strerror(-res), res);
2740 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2741 return false;
2742 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002743
Zhijun Hecc27e112013-10-03 16:12:43 -07002744 // Inform waitUntilRequestProcessed thread of a new request ID
2745 {
2746 Mutex::Autolock al(mLatestRequestMutex);
2747
2748 mLatestRequestId = requestId;
2749 mLatestRequestSignal.signal();
2750 }
2751
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002752 // Submit request and block until ready for next one
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002753 ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2754 ATRACE_BEGIN("camera3->process_capture_request");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755 res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07002756 ATRACE_END();
2757
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002758 if (res != OK) {
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002759 // Should only get a failure here for malformed requests or device-level
2760 // errors, so consider all errors fatal. Bad metadata failures should
2761 // come through notify.
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002762 SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002763 " device: %s (%d)", request.frame_number, strerror(-res), res);
2764 cleanUpFailedRequest(request, nextRequest, outputBuffers);
2765 return false;
2766 }
2767
Igor Murashkin1e479c02013-09-06 16:55:14 -07002768 // Update the latest request sent to HAL
2769 if (request.settings != NULL) { // Don't update them if they were unchanged
2770 Mutex::Autolock al(mLatestRequestMutex);
2771
2772 camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2773 mLatestRequest.acquire(cloned);
2774 }
2775
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002776 if (request.settings != NULL) {
2777 nextRequest->mSettings.unlock(request.settings);
2778 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002779
2780 // Remove any previously queued triggers (after unlock)
2781 res = removeTriggers(mPrevRequest);
2782 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002783 SET_ERR("RequestThread: Unable to remove triggers "
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002784 "(capture request %d, HAL device: %s (%d)",
2785 request.frame_number, strerror(-res), res);
2786 return false;
2787 }
2788 mPrevTriggers = triggerCount;
2789
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002790 return true;
2791}
2792
Igor Murashkin1e479c02013-09-06 16:55:14 -07002793CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2794 Mutex::Autolock al(mLatestRequestMutex);
2795
2796 ALOGV("RequestThread::%s", __FUNCTION__);
2797
2798 return mLatestRequest;
2799}
2800
Jianing Weicb0652e2014-03-12 18:29:36 -07002801
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002802void Camera3Device::RequestThread::cleanUpFailedRequest(
2803 camera3_capture_request_t &request,
2804 sp<CaptureRequest> &nextRequest,
2805 Vector<camera3_stream_buffer_t> &outputBuffers) {
2806
2807 if (request.settings != NULL) {
2808 nextRequest->mSettings.unlock(request.settings);
2809 }
2810 if (request.input_buffer != NULL) {
2811 request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07002812 nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002813 }
2814 for (size_t i = 0; i < request.num_output_buffers; i++) {
2815 outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2816 nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2817 outputBuffers[i], 0);
2818 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002819}
2820
2821sp<Camera3Device::CaptureRequest>
2822 Camera3Device::RequestThread::waitForNextRequest() {
2823 status_t res;
2824 sp<CaptureRequest> nextRequest;
2825
2826 // Optimized a bit for the simple steady-state case (single repeating
2827 // request), to avoid putting that request in the queue temporarily.
2828 Mutex::Autolock l(mRequestLock);
2829
2830 while (mRequestQueue.empty()) {
2831 if (!mRepeatingRequests.empty()) {
2832 // Always atomically enqueue all requests in a repeating request
2833 // list. Guarantees a complete in-sequence set of captures to
2834 // application.
2835 const RequestList &requests = mRepeatingRequests;
2836 RequestList::const_iterator firstRequest =
2837 requests.begin();
2838 nextRequest = *firstRequest;
2839 mRequestQueue.insert(mRequestQueue.end(),
2840 ++firstRequest,
2841 requests.end());
2842 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07002843
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002844 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07002845
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002846 break;
2847 }
2848
2849 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2850
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002851 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2852 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002853 Mutex::Autolock pl(mPauseLock);
2854 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002855 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002856 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002857 // Let the tracker know
2858 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2859 if (statusTracker != 0) {
2860 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2861 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002862 }
2863 // Stop waiting for now and let thread management happen
2864 return NULL;
2865 }
2866 }
2867
2868 if (nextRequest == NULL) {
2869 // Don't have a repeating request already in hand, so queue
2870 // must have an entry now.
2871 RequestList::iterator firstRequest =
2872 mRequestQueue.begin();
2873 nextRequest = *firstRequest;
2874 mRequestQueue.erase(firstRequest);
2875 }
2876
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002877 // In case we've been unpaused by setPaused clearing mDoPause, need to
2878 // update internal pause state (capture/setRepeatingRequest unpause
2879 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002880 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002881 if (mPaused) {
2882 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2883 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2884 if (statusTracker != 0) {
2885 statusTracker->markComponentActive(mStatusId);
2886 }
2887 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002888 mPaused = false;
2889
2890 // Check if we've reconfigured since last time, and reset the preview
2891 // request if so. Can't use 'NULL request == repeat' across configure calls.
2892 if (mReconfigured) {
2893 mPrevRequest.clear();
2894 mReconfigured = false;
2895 }
2896
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002897 if (nextRequest != NULL) {
2898 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002899 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2900 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07002901 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002902 return nextRequest;
2903}
2904
2905bool Camera3Device::RequestThread::waitIfPaused() {
2906 status_t res;
2907 Mutex::Autolock l(mPauseLock);
2908 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002909 if (mPaused == false) {
2910 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002911 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2912 // Let the tracker know
2913 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2914 if (statusTracker != 0) {
2915 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2916 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002917 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002918
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002919 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002920 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002921 return true;
2922 }
2923 }
2924 // We don't set mPaused to false here, because waitForNextRequest needs
2925 // to further manage the paused state in case of starvation.
2926 return false;
2927}
2928
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002929void Camera3Device::RequestThread::unpauseForNewRequests() {
2930 // With work to do, mark thread as unpaused.
2931 // If paused by request (setPaused), don't resume, to avoid
2932 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002933 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002934 Mutex::Autolock p(mPauseLock);
2935 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002936 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2937 if (mPaused) {
2938 sp<StatusTracker> statusTracker = mStatusTracker.promote();
2939 if (statusTracker != 0) {
2940 statusTracker->markComponentActive(mStatusId);
2941 }
2942 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07002943 mPaused = false;
2944 }
2945}
2946
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002947void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2948 sp<Camera3Device> parent = mParent.promote();
2949 if (parent != NULL) {
2950 va_list args;
2951 va_start(args, fmt);
2952
2953 parent->setErrorStateV(fmt, args);
2954
2955 va_end(args);
2956 }
2957}
2958
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002959status_t Camera3Device::RequestThread::insertTriggers(
2960 const sp<CaptureRequest> &request) {
2961
2962 Mutex::Autolock al(mTriggerMutex);
2963
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002964 sp<Camera3Device> parent = mParent.promote();
2965 if (parent == NULL) {
2966 CLOGE("RequestThread: Parent is gone");
2967 return DEAD_OBJECT;
2968 }
2969
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002970 CameraMetadata &metadata = request->mSettings;
2971 size_t count = mTriggerMap.size();
2972
2973 for (size_t i = 0; i < count; ++i) {
2974 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002975 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002976
2977 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2978 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2979 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07002980 if (isAeTrigger) {
2981 request->mResultExtras.precaptureTriggerId = triggerId;
2982 mCurrentPreCaptureTriggerId = triggerId;
2983 } else {
2984 request->mResultExtras.afTriggerId = triggerId;
2985 mCurrentAfTriggerId = triggerId;
2986 }
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002987 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2988 continue; // Trigger ID tag is deprecated since device HAL 3.2
2989 }
2990 }
2991
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002992 camera_metadata_entry entry = metadata.find(tag);
2993
2994 if (entry.count > 0) {
2995 /**
2996 * Already has an entry for this trigger in the request.
2997 * Rewrite it with our requested trigger value.
2998 */
2999 RequestTrigger oldTrigger = trigger;
3000
3001 oldTrigger.entryValue = entry.data.u8[0];
3002
3003 mTriggerReplacedMap.add(tag, oldTrigger);
3004 } else {
3005 /**
3006 * More typical, no trigger entry, so we just add it
3007 */
3008 mTriggerRemovedMap.add(tag, trigger);
3009 }
3010
3011 status_t res;
3012
3013 switch (trigger.getTagType()) {
3014 case TYPE_BYTE: {
3015 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3016 res = metadata.update(tag,
3017 &entryValue,
3018 /*count*/1);
3019 break;
3020 }
3021 case TYPE_INT32:
3022 res = metadata.update(tag,
3023 &trigger.entryValue,
3024 /*count*/1);
3025 break;
3026 default:
3027 ALOGE("%s: Type not supported: 0x%x",
3028 __FUNCTION__,
3029 trigger.getTagType());
3030 return INVALID_OPERATION;
3031 }
3032
3033 if (res != OK) {
3034 ALOGE("%s: Failed to update request metadata with trigger tag %s"
3035 ", value %d", __FUNCTION__, trigger.getTagName(),
3036 trigger.entryValue);
3037 return res;
3038 }
3039
3040 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
3041 trigger.getTagName(),
3042 trigger.entryValue);
3043 }
3044
3045 mTriggerMap.clear();
3046
3047 return count;
3048}
3049
3050status_t Camera3Device::RequestThread::removeTriggers(
3051 const sp<CaptureRequest> &request) {
3052 Mutex::Autolock al(mTriggerMutex);
3053
3054 CameraMetadata &metadata = request->mSettings;
3055
3056 /**
3057 * Replace all old entries with their old values.
3058 */
3059 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
3060 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
3061
3062 status_t res;
3063
3064 uint32_t tag = trigger.metadataTag;
3065 switch (trigger.getTagType()) {
3066 case TYPE_BYTE: {
3067 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
3068 res = metadata.update(tag,
3069 &entryValue,
3070 /*count*/1);
3071 break;
3072 }
3073 case TYPE_INT32:
3074 res = metadata.update(tag,
3075 &trigger.entryValue,
3076 /*count*/1);
3077 break;
3078 default:
3079 ALOGE("%s: Type not supported: 0x%x",
3080 __FUNCTION__,
3081 trigger.getTagType());
3082 return INVALID_OPERATION;
3083 }
3084
3085 if (res != OK) {
3086 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3087 ", trigger value %d", __FUNCTION__,
3088 trigger.getTagName(), trigger.entryValue);
3089 return res;
3090 }
3091 }
3092 mTriggerReplacedMap.clear();
3093
3094 /**
3095 * Remove all new entries.
3096 */
3097 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3098 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3099 status_t res = metadata.erase(trigger.metadataTag);
3100
3101 if (res != OK) {
3102 ALOGE("%s: Failed to erase metadata with trigger tag %s"
3103 ", trigger value %d", __FUNCTION__,
3104 trigger.getTagName(), trigger.entryValue);
3105 return res;
3106 }
3107 }
3108 mTriggerRemovedMap.clear();
3109
3110 return OK;
3111}
3112
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07003113status_t Camera3Device::RequestThread::addDummyTriggerIds(
3114 const sp<CaptureRequest> &request) {
3115 // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3116 static const int32_t dummyTriggerId = 1;
3117 status_t res;
3118
3119 CameraMetadata &metadata = request->mSettings;
3120
3121 // If AF trigger is active, insert a dummy AF trigger ID if none already
3122 // exists
3123 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3124 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3125 if (afTrigger.count > 0 &&
3126 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3127 afId.count == 0) {
3128 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3129 if (res != OK) return res;
3130 }
3131
3132 // If AE precapture trigger is active, insert a dummy precapture trigger ID
3133 // if none already exists
3134 camera_metadata_entry pcTrigger =
3135 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3136 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3137 if (pcTrigger.count > 0 &&
3138 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3139 pcId.count == 0) {
3140 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3141 &dummyTriggerId, 1);
3142 if (res != OK) return res;
3143 }
3144
3145 return OK;
3146}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003147
3148
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003149/**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003150 * Static callback forwarding methods from HAL to instance
3151 */
3152
3153void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3154 const camera3_capture_result *result) {
3155 Camera3Device *d =
3156 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3157 d->processCaptureResult(result);
3158}
3159
3160void Camera3Device::sNotify(const camera3_callback_ops *cb,
3161 const camera3_notify_msg *msg) {
3162 Camera3Device *d =
3163 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3164 d->notify(msg);
3165}
3166
3167}; // namespace android